lib

KoPageLayout.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
3 Copyright 2002, 2003 David Faure <faure@kde.org>
4 Copyright 2003 Nicolas GOUTTE <goutte@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21#include "KoPageLayout.h"
22#include <KoUnit.h>
23#include <tdelocale.h>
24#include <kprinter.h>
25#include <kdebug.h>
26#include <tdeglobal.h>
27#include <KoDom.h>
28#include <KoXmlNS.h>
29#include <tqdom.h>
30
31// paper formats ( mm )
32#define PG_A3_WIDTH 297.0
33#define PG_A3_HEIGHT 420.0
34#define PG_A4_WIDTH 210.0
35#define PG_A4_HEIGHT 297.0
36#define PG_A5_WIDTH 148.0
37#define PG_A5_HEIGHT 210.0
38#define PG_B5_WIDTH 182.0
39#define PG_B5_HEIGHT 257.0
40#define PG_US_LETTER_WIDTH 216.0
41#define PG_US_LETTER_HEIGHT 279.0
42#define PG_US_LEGAL_WIDTH 216.0
43#define PG_US_LEGAL_HEIGHT 356.0
44#define PG_US_EXECUTIVE_WIDTH 191.0
45#define PG_US_EXECUTIVE_HEIGHT 254.0
46
48{
49 KoGenStyle style(KoGenStyle::STYLE_PAGELAYOUT);
50 style.addPropertyPt("fo:page-width", ptWidth);
51 style.addPropertyPt("fo:page-height", ptHeight);
52 style.addPropertyPt("fo:margin-left", ptLeft);
53 style.addPropertyPt("fo:margin-right", ptRight);
54 style.addPropertyPt("fo:margin-top", ptTop);
55 style.addPropertyPt("fo:margin-bottom", ptBottom);
56 style.addProperty("style:print-orientation", (orientation == PG_LANDSCAPE ? "landscape" : "portrait"));
57 return style;
58}
59
60void KoPageLayout::loadOasis(const TQDomElement &style)
61{
62 TQDomElement properties( KoDom::namedItemNS( style, KoXmlNS::style, "page-layout-properties" ) );
63 if ( !properties.isNull() )
64 {
65 ptWidth = KoUnit::parseValue(properties.attributeNS( KoXmlNS::fo, "page-width", TQString() ) );
66 ptHeight = KoUnit::parseValue(properties.attributeNS( KoXmlNS::fo, "page-height", TQString() ) );
67 if (properties.attributeNS( KoXmlNS::style, "print-orientation", TQString())=="portrait")
68 orientation=PG_PORTRAIT;
69 else
70 orientation=PG_LANDSCAPE;
71 ptRight = KoUnit::parseValue( properties.attributeNS( KoXmlNS::fo, "margin-right", TQString() ) );
72 ptBottom = KoUnit::parseValue( properties.attributeNS( KoXmlNS::fo, "margin-bottom", TQString() ) );
73 ptLeft = KoUnit::parseValue( properties.attributeNS( KoXmlNS::fo, "margin-left", TQString() ) );
74 ptTop = KoUnit::parseValue( properties.attributeNS( KoXmlNS::fo, "margin-top", TQString() ) );
75 // guessFormat takes millimeters
76 if ( orientation == PG_LANDSCAPE )
77 format = KoPageFormat::guessFormat( POINT_TO_MM(ptHeight), POINT_TO_MM(ptWidth) );
78 else
79 format = KoPageFormat::guessFormat( POINT_TO_MM(ptWidth), POINT_TO_MM(ptHeight) );
80 }
81}
82
84{
85 KoPageLayout layout;
86 layout.format = KoPageFormat::defaultFormat();
87 layout.orientation = PG_PORTRAIT;
88 layout.ptWidth = MM_TO_POINT( KoPageFormat::width( layout.format, layout.orientation ) );
89 layout.ptHeight = MM_TO_POINT( KoPageFormat::height( layout.format, layout.orientation ) );
90 layout.ptLeft = MM_TO_POINT( 20.0 );
91 layout.ptRight = MM_TO_POINT( 20.0 );
92 layout.ptTop = MM_TO_POINT( 20.0 );
93 layout.ptBottom = MM_TO_POINT( 20.0 );
94 layout.ptPageEdge = -1;
95 layout.ptBindingSide = -1;
96 return layout;
97}
98
99struct PageFormatInfo
100{
101 KoFormat format;
102 KPrinter::PageSize kprinter;
103 const char* shortName; // Short name
104 const char* descriptiveName; // Full name, which will be translated
105 double width; // in mm
106 double height; // in mm
107};
108
109// NOTES:
110// - the width and height of non-ISO formats are rounded
111// http://en.wikipedia.org/wiki/Paper_size can help
112// - the comments "should be..." indicates the exact values if the inch sizes would be multiplied by 25.4 mm/inch
113
114const PageFormatInfo pageFormatInfo[]=
115{
116 { PG_DIN_A3, KPrinter::A3, "A3", I18N_NOOP("ISO A3"), 297.0, 420.0 },
117 { PG_DIN_A4, KPrinter::A4, "A4", I18N_NOOP("ISO A4"), 210.0, 297.0 },
118 { PG_DIN_A5, KPrinter::A5, "A5", I18N_NOOP("ISO A5"), 148.0, 210.0 },
119 { PG_US_LETTER, KPrinter::Letter, "Letter", I18N_NOOP("US Letter"), 215.9, 279.4 },
120 { PG_US_LEGAL, KPrinter::Legal, "Legal", I18N_NOOP("US Legal"), 215.9, 355.6 },
121 { PG_SCREEN, KPrinter::A4, "Screen", I18N_NOOP("Screen"), PG_A4_HEIGHT, PG_A4_WIDTH }, // Custom, so fall back to A4
122 { PG_CUSTOM, KPrinter::A4, "Custom", I18N_NOOP2("Custom size", "Custom"), PG_A4_WIDTH, PG_A4_HEIGHT }, // Custom, so fall back to A4
123 { PG_DIN_B5, KPrinter::B5, "B5", I18N_NOOP("ISO B5"), 182.0, 257.0 },
124 // Hmm, wikipedia says 184.15 * 266.7 for executive !
125 { PG_US_EXECUTIVE, KPrinter::Executive, "Executive", I18N_NOOP("US Executive"), 191.0, 254.0 }, // should be 190.5 mm x 254.0 mm
126 { PG_DIN_A0, KPrinter::A0, "A0", I18N_NOOP("ISO A0"), 841.0, 1189.0 },
127 { PG_DIN_A1, KPrinter::A1, "A1", I18N_NOOP("ISO A1"), 594.0, 841.0 },
128 { PG_DIN_A2, KPrinter::A2, "A2", I18N_NOOP("ISO A2"), 420.0, 594.0 },
129 { PG_DIN_A6, KPrinter::A6, "A6", I18N_NOOP("ISO A6"), 105.0, 148.0 },
130 { PG_DIN_A7, KPrinter::A7, "A7", I18N_NOOP("ISO A7"), 74.0, 105.0 },
131 { PG_DIN_A8, KPrinter::A8, "A8", I18N_NOOP("ISO A8"), 52.0, 74.0 },
132 { PG_DIN_A9, KPrinter::A9, "A9", I18N_NOOP("ISO A9"), 37.0, 52.0 },
133 { PG_DIN_B0, KPrinter::B0, "B0", I18N_NOOP("ISO B0"), 1030.0, 1456.0 },
134 { PG_DIN_B1, KPrinter::B1, "B1", I18N_NOOP("ISO B1"), 728.0, 1030.0 },
135 { PG_DIN_B10, KPrinter::B10, "B10", I18N_NOOP("ISO B10"), 32.0, 45.0 },
136 { PG_DIN_B2, KPrinter::B2, "B2", I18N_NOOP("ISO B2"), 515.0, 728.0 },
137 { PG_DIN_B3, KPrinter::B3, "B3", I18N_NOOP("ISO B3"), 364.0, 515.0 },
138 { PG_DIN_B4, KPrinter::B4, "B4", I18N_NOOP("ISO B4"), 257.0, 364.0 },
139 { PG_DIN_B6, KPrinter::B6, "B6", I18N_NOOP("ISO B6"), 128.0, 182.0 },
140 { PG_ISO_C5, KPrinter::C5E, "C5", I18N_NOOP("ISO C5"), 163.0, 229.0 }, // Some sources tells: 162 mm x 228 mm
141 { PG_US_COMM10, KPrinter::Comm10E, "Comm10", I18N_NOOP("US Common 10"), 105.0, 241.0 }, // should be 104.775 mm x 241.3 mm
142 { PG_ISO_DL, KPrinter::DLE, "DL", I18N_NOOP("ISO DL"), 110.0, 220.0 },
143 { PG_US_FOLIO, KPrinter::Folio, "Folio", I18N_NOOP("US Folio"), 210.0, 330.0 }, // should be 209.54 mm x 330.2 mm
144 { PG_US_LEDGER, KPrinter::Ledger, "Ledger", I18N_NOOP("US Ledger"), 432.0, 279.0 }, // should be 431.8 mm x 297.4 mm
145 { PG_US_TABLOID, KPrinter::Tabloid, "Tabloid", I18N_NOOP("US Tabloid"), 279.0, 432.0 } // should be 297.4 mm x 431.8 mm
146};
147
148int KoPageFormat::printerPageSize( KoFormat format )
149{
150 if ( format == PG_SCREEN )
151 {
152 kdWarning() << "You use the page layout SCREEN. Printing in DIN A4 LANDSCAPE." << endl;
153 return KPrinter::A4;
154 }
155 else if ( format == PG_CUSTOM )
156 {
157 kdWarning() << "The used page layout (CUSTOM) is not supported by KPrinter. Printing in A4." << endl;
158 return KPrinter::A4;
159 }
160 else if ( format <= PG_LAST_FORMAT )
161 return pageFormatInfo[ format ].kprinter;
162 else
163 return KPrinter::A4;
164}
165
166double KoPageFormat::width( KoFormat format, KoOrientation orientation )
167{
168 if ( orientation == PG_LANDSCAPE )
169 return height( format, PG_PORTRAIT );
170 if ( format <= PG_LAST_FORMAT )
171 return pageFormatInfo[ format ].width;
172 return PG_A4_WIDTH; // should never happen
173}
174
175double KoPageFormat::height( KoFormat format, KoOrientation orientation )
176{
177 if ( orientation == PG_LANDSCAPE )
178 return width( format, PG_PORTRAIT );
179 if ( format <= PG_LAST_FORMAT )
180 return pageFormatInfo[ format ].height;
181 return PG_A4_HEIGHT;
182}
183
184KoFormat KoPageFormat::guessFormat( double width, double height )
185{
186 for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
187 {
188 // We have some tolerance. 1pt is a third of a mm, this is
189 // barely noticeable for a page size.
190 if ( i != PG_CUSTOM
191 && kAbs( width - pageFormatInfo[i].width ) < 1.0
192 && kAbs( height - pageFormatInfo[i].height ) < 1.0 )
193 return static_cast<KoFormat>(i);
194 }
195 return PG_CUSTOM;
196}
197
198TQString KoPageFormat::formatString( KoFormat format )
199{
200 if ( format <= PG_LAST_FORMAT )
201 return TQString::fromLatin1( pageFormatInfo[ format ].shortName );
202 return TQString::fromLatin1( "A4" );
203}
204
205KoFormat KoPageFormat::formatFromString( const TQString & string )
206{
207 for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
208 {
209 if (string == TQString::fromLatin1( pageFormatInfo[ i ].shortName ))
210 return pageFormatInfo[ i ].format;
211 }
212 // We do not know the format name, so we have a custom format
213 return PG_CUSTOM;
214}
215
216KoFormat KoPageFormat::defaultFormat()
217{
218 int kprinter = TDEGlobal::locale()->pageSize();
219 for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
220 {
221 if ( pageFormatInfo[ i ].kprinter == kprinter )
222 return static_cast<KoFormat>(i);
223 }
224 return PG_DIN_A4;
225}
226
227TQString KoPageFormat::name( KoFormat format )
228{
229 if ( format <= PG_LAST_FORMAT )
230 return i18n( pageFormatInfo[ format ].descriptiveName );
231 return i18n( pageFormatInfo[ PG_DIN_A4 ].descriptiveName );
232}
233
234TQStringList KoPageFormat::allFormats()
235{
236 TQStringList lst;
237 for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
238 {
239 lst << i18n( pageFormatInfo[ i ].descriptiveName );
240 }
241 return lst;
242}
A generic style, i.e.
static double parseValue(TQString value, double defaultVal=0.0)
parse common KOffice and OO values, like "10cm", "5mm" to pt
Definition KoUnit.cpp:148
KOFFICECORE_EXPORT TQDomElement namedItemNS(const TQDomNode &node, const char *nsURI, const char *localName)
A namespace-aware version of TQDomNode::namedItem(), which also takes care of casting to a TQDomEleme...
Definition KoDom.cpp:22
This structure defines the page layout, including its size in pt, its format (e.g.
KoFormat format
Page format.
double ptRight
Right margin in pt.
double ptHeight
Page height in pt.
KOFFICECORE_EXPORT void loadOasis(const TQDomElement &style)
Load this page layout from OASIS.
KoOrientation orientation
Page orientation.
double ptLeft
Left margin in pt.
double ptBottom
Bottom margin in pt.
double ptWidth
Page width in pt.
static KOFFICECORE_EXPORT KoPageLayout standardLayout()
KOFFICECORE_EXPORT KoGenStyle saveOasis() const
Save this page layout to OASIS.
double ptTop
Top margin in pt.