00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "KoPageLayout.h"
00022 #include <KoUnit.h>
00023 #include <klocale.h>
00024 #include <kprinter.h>
00025 #include <kdebug.h>
00026 #include <kglobal.h>
00027 #include <KoDom.h>
00028 #include <KoXmlNS.h>
00029 #include <qdom.h>
00030
00031
00032 #define PG_A3_WIDTH 297.0
00033 #define PG_A3_HEIGHT 420.0
00034 #define PG_A4_WIDTH 210.0
00035 #define PG_A4_HEIGHT 297.0
00036 #define PG_A5_WIDTH 148.0
00037 #define PG_A5_HEIGHT 210.0
00038 #define PG_B5_WIDTH 182.0
00039 #define PG_B5_HEIGHT 257.0
00040 #define PG_US_LETTER_WIDTH 216.0
00041 #define PG_US_LETTER_HEIGHT 279.0
00042 #define PG_US_LEGAL_WIDTH 216.0
00043 #define PG_US_LEGAL_HEIGHT 356.0
00044 #define PG_US_EXECUTIVE_WIDTH 191.0
00045 #define PG_US_EXECUTIVE_HEIGHT 254.0
00046
00047 KoGenStyle KoPageLayout::saveOasis() const
00048 {
00049 KoGenStyle style(KoGenStyle::STYLE_PAGELAYOUT);
00050 style.addPropertyPt("fo:page-width", ptWidth);
00051 style.addPropertyPt("fo:page-height", ptHeight);
00052 style.addPropertyPt("fo:margin-left", ptLeft);
00053 style.addPropertyPt("fo:margin-right", ptRight);
00054 style.addPropertyPt("fo:margin-top", ptTop);
00055 style.addPropertyPt("fo:margin-bottom", ptBottom);
00056 style.addProperty("style:print-orientation", (orientation == PG_LANDSCAPE ? "landscape" : "portrait"));
00057 return style;
00058 }
00059
00060 void KoPageLayout::loadOasis(const KoXmlElement &style)
00061 {
00062 KoXmlElement properties( KoDom::namedItemNS( style, KoXmlNS::style, "page-layout-properties" ) );
00063 if ( !properties.isNull() )
00064 {
00065 ptWidth = KoUnit::parseValue(properties.attributeNS( KoXmlNS::fo, "page-width", QString::null ) );
00066 ptHeight = KoUnit::parseValue(properties.attributeNS( KoXmlNS::fo, "page-height", QString::null ) );
00067 if (properties.attributeNS( KoXmlNS::style, "print-orientation", QString::null)=="portrait")
00068 orientation=PG_PORTRAIT;
00069 else
00070 orientation=PG_LANDSCAPE;
00071 ptRight = KoUnit::parseValue( properties.attributeNS( KoXmlNS::fo, "margin-right", QString::null ) );
00072 ptBottom = KoUnit::parseValue( properties.attributeNS( KoXmlNS::fo, "margin-bottom", QString::null ) );
00073 ptLeft = KoUnit::parseValue( properties.attributeNS( KoXmlNS::fo, "margin-left", QString::null ) );
00074 ptTop = KoUnit::parseValue( properties.attributeNS( KoXmlNS::fo, "margin-top", QString::null ) );
00075
00076 if ( orientation == PG_LANDSCAPE )
00077 format = KoPageFormat::guessFormat( POINT_TO_MM(ptHeight), POINT_TO_MM(ptWidth) );
00078 else
00079 format = KoPageFormat::guessFormat( POINT_TO_MM(ptWidth), POINT_TO_MM(ptHeight) );
00080 }
00081 }
00082
00083 KoPageLayout KoPageLayout::standardLayout()
00084 {
00085 KoPageLayout layout;
00086 layout.format = KoPageFormat::defaultFormat();
00087 layout.orientation = PG_PORTRAIT;
00088 layout.ptWidth = MM_TO_POINT( KoPageFormat::width( layout.format, layout.orientation ) );
00089 layout.ptHeight = MM_TO_POINT( KoPageFormat::height( layout.format, layout.orientation ) );
00090 layout.ptLeft = MM_TO_POINT( 20.0 );
00091 layout.ptRight = MM_TO_POINT( 20.0 );
00092 layout.ptTop = MM_TO_POINT( 20.0 );
00093 layout.ptBottom = MM_TO_POINT( 20.0 );
00094 layout.ptPageEdge = -1;
00095 layout.ptBindingSide = -1;
00096 return layout;
00097 }
00098
00099 struct PageFormatInfo
00100 {
00101 KoFormat format;
00102 KPrinter::PageSize kprinter;
00103 const char* shortName;
00104 const char* descriptiveName;
00105 double width;
00106 double height;
00107 };
00108
00109
00110
00111
00112
00113
00114 const PageFormatInfo pageFormatInfo[]=
00115 {
00116 { PG_DIN_A3, KPrinter::A3, "A3", I18N_NOOP("ISO A3"), 297.0, 420.0 },
00117 { PG_DIN_A4, KPrinter::A4, "A4", I18N_NOOP("ISO A4"), 210.0, 297.0 },
00118 { PG_DIN_A5, KPrinter::A5, "A5", I18N_NOOP("ISO A5"), 148.0, 210.0 },
00119 { PG_US_LETTER, KPrinter::Letter, "Letter", I18N_NOOP("US Letter"), 215.9, 279.4 },
00120 { PG_US_LEGAL, KPrinter::Legal, "Legal", I18N_NOOP("US Legal"), 215.9, 355.6 },
00121 { PG_SCREEN, KPrinter::A4, "Screen", I18N_NOOP("Screen"), PG_A4_HEIGHT, PG_A4_WIDTH },
00122 { PG_CUSTOM, KPrinter::A4, "Custom", I18N_NOOP2("Custom size", "Custom"), PG_A4_WIDTH, PG_A4_HEIGHT },
00123 { PG_DIN_B5, KPrinter::B5, "B5", I18N_NOOP("ISO B5"), 182.0, 257.0 },
00124
00125 { PG_US_EXECUTIVE, KPrinter::Executive, "Executive", I18N_NOOP("US Executive"), 191.0, 254.0 },
00126 { PG_DIN_A0, KPrinter::A0, "A0", I18N_NOOP("ISO A0"), 841.0, 1189.0 },
00127 { PG_DIN_A1, KPrinter::A1, "A1", I18N_NOOP("ISO A1"), 594.0, 841.0 },
00128 { PG_DIN_A2, KPrinter::A2, "A2", I18N_NOOP("ISO A2"), 420.0, 594.0 },
00129 { PG_DIN_A6, KPrinter::A6, "A6", I18N_NOOP("ISO A6"), 105.0, 148.0 },
00130 { PG_DIN_A7, KPrinter::A7, "A7", I18N_NOOP("ISO A7"), 74.0, 105.0 },
00131 { PG_DIN_A8, KPrinter::A8, "A8", I18N_NOOP("ISO A8"), 52.0, 74.0 },
00132 { PG_DIN_A9, KPrinter::A9, "A9", I18N_NOOP("ISO A9"), 37.0, 52.0 },
00133 { PG_DIN_B0, KPrinter::B0, "B0", I18N_NOOP("ISO B0"), 1030.0, 1456.0 },
00134 { PG_DIN_B1, KPrinter::B1, "B1", I18N_NOOP("ISO B1"), 728.0, 1030.0 },
00135 { PG_DIN_B10, KPrinter::B10, "B10", I18N_NOOP("ISO B10"), 32.0, 45.0 },
00136 { PG_DIN_B2, KPrinter::B2, "B2", I18N_NOOP("ISO B2"), 515.0, 728.0 },
00137 { PG_DIN_B3, KPrinter::B3, "B3", I18N_NOOP("ISO B3"), 364.0, 515.0 },
00138 { PG_DIN_B4, KPrinter::B4, "B4", I18N_NOOP("ISO B4"), 257.0, 364.0 },
00139 { PG_DIN_B6, KPrinter::B6, "B6", I18N_NOOP("ISO B6"), 128.0, 182.0 },
00140 { PG_ISO_C5, KPrinter::C5E, "C5", I18N_NOOP("ISO C5"), 163.0, 229.0 },
00141 { PG_US_COMM10, KPrinter::Comm10E, "Comm10", I18N_NOOP("US Common 10"), 105.0, 241.0 },
00142 { PG_ISO_DL, KPrinter::DLE, "DL", I18N_NOOP("ISO DL"), 110.0, 220.0 },
00143 { PG_US_FOLIO, KPrinter::Folio, "Folio", I18N_NOOP("US Folio"), 210.0, 330.0 },
00144 { PG_US_LEDGER, KPrinter::Ledger, "Ledger", I18N_NOOP("US Ledger"), 432.0, 279.0 },
00145 { PG_US_TABLOID, KPrinter::Tabloid, "Tabloid", I18N_NOOP("US Tabloid"), 279.0, 432.0 }
00146 };
00147
00148 int KoPageFormat::printerPageSize( KoFormat format )
00149 {
00150 if ( format == PG_SCREEN )
00151 {
00152 kWarning() << "You use the page layout SCREEN. Printing in DIN A4 LANDSCAPE." << endl;
00153 return KPrinter::A4;
00154 }
00155 else if ( format == PG_CUSTOM )
00156 {
00157 kWarning() << "The used page layout (CUSTOM) is not supported by KPrinter. Printing in A4." << endl;
00158 return KPrinter::A4;
00159 }
00160 else if ( format <= PG_LAST_FORMAT )
00161 return pageFormatInfo[ format ].kprinter;
00162 else
00163 return KPrinter::A4;
00164 }
00165
00166 double KoPageFormat::width( KoFormat format, KoOrientation orientation )
00167 {
00168 if ( orientation == PG_LANDSCAPE )
00169 return height( format, PG_PORTRAIT );
00170 if ( format <= PG_LAST_FORMAT )
00171 return pageFormatInfo[ format ].width;
00172 return PG_A4_WIDTH;
00173 }
00174
00175 double KoPageFormat::height( KoFormat format, KoOrientation orientation )
00176 {
00177 if ( orientation == PG_LANDSCAPE )
00178 return width( format, PG_PORTRAIT );
00179 if ( format <= PG_LAST_FORMAT )
00180 return pageFormatInfo[ format ].height;
00181 return PG_A4_HEIGHT;
00182 }
00183
00184 KoFormat KoPageFormat::guessFormat( double width, double height )
00185 {
00186 for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
00187 {
00188
00189
00190 if ( i != PG_CUSTOM
00191 && qAbs( width - pageFormatInfo[i].width ) < 1.0
00192 && qAbs( height - pageFormatInfo[i].height ) < 1.0 )
00193 return static_cast<KoFormat>(i);
00194 }
00195 return PG_CUSTOM;
00196 }
00197
00198 QString KoPageFormat::formatString( KoFormat format )
00199 {
00200 if ( format <= PG_LAST_FORMAT )
00201 return QString::fromLatin1( pageFormatInfo[ format ].shortName );
00202 return QString::fromLatin1( "A4" );
00203 }
00204
00205 KoFormat KoPageFormat::formatFromString( const QString & string )
00206 {
00207 for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
00208 {
00209 if (string == QString::fromLatin1( pageFormatInfo[ i ].shortName ))
00210 return pageFormatInfo[ i ].format;
00211 }
00212
00213 return PG_CUSTOM;
00214 }
00215
00216 KoFormat KoPageFormat::defaultFormat()
00217 {
00218 int kprinter = KGlobal::locale()->pageSize();
00219 for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
00220 {
00221 if ( pageFormatInfo[ i ].kprinter == kprinter )
00222 return static_cast<KoFormat>(i);
00223 }
00224 return PG_DIN_A4;
00225 }
00226
00227 QString KoPageFormat::name( KoFormat format )
00228 {
00229 if ( format <= PG_LAST_FORMAT )
00230 return i18n( pageFormatInfo[ format ].descriptiveName );
00231 return i18n( pageFormatInfo[ PG_DIN_A4 ].descriptiveName );
00232 }
00233
00234 QStringList KoPageFormat::allFormats()
00235 {
00236 QStringList lst;
00237 for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
00238 {
00239 lst << i18n( pageFormatInfo[ i ].descriptiveName );
00240 }
00241 return lst;
00242 }