lib Library API Documentation

koPageLayout.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
00003    Copyright 2002, 2003 David Faure <faure@kde.org>
00004    Copyright 2003 Nicolas GOUTTE <goutte@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019    Boston, MA 02111-1307, USA.
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 // paper formats ( mm )
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 QDomElement &style)
00061 {
00062     QDomElement 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         // guessFormat takes millimeters
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     return layout;
00095 }
00096 
00097 struct PageFormatInfo
00098 {
00099     KoFormat format;
00100     KPrinter::PageSize kprinter;
00101     const char* shortName; // Short name
00102     const char* descriptiveName; // Full name, which will be translated
00103     double width; // in mm
00104     double height; // in mm
00105 };
00106 
00107 // NOTES:
00108 // - the width and height of non-ISO formats are rounded
00109 // http://en.wikipedia.org/wiki/Paper_size can help
00110 // - the comments "should be..." indicates the exact values if the inch sizes would be multiplied by 25.4 mm/inch
00111 
00112 const PageFormatInfo pageFormatInfo[]=
00113 {
00114     { PG_DIN_A3,        KPrinter::A3,           "A3",           I18N_NOOP("ISO A3"),       297.0,  420.0 },
00115     { PG_DIN_A4,        KPrinter::A4,           "A4",           I18N_NOOP("ISO A4"),       210.0,  297.0 },
00116     { PG_DIN_A5,        KPrinter::A5,           "A5",           I18N_NOOP("ISO A5"),       148.0,  210.0 },
00117     { PG_US_LETTER,     KPrinter::Letter,       "Letter",       I18N_NOOP("US Letter"),    215.9,  279.4 },
00118     { PG_US_LEGAL,      KPrinter::Legal,        "Legal",        I18N_NOOP("US Legal"),     215.9,  355.6 },
00119     { PG_SCREEN,        KPrinter::A4,           "Screen",       I18N_NOOP("Screen"), PG_A4_HEIGHT, PG_A4_WIDTH }, // Custom, so fall back to A4
00120     { PG_CUSTOM,        KPrinter::A4,           "Custom",       I18N_NOOP("Custom"), PG_A4_WIDTH, PG_A4_HEIGHT }, // Custom, so fall back to A4
00121     { PG_DIN_B5,        KPrinter::B5,           "B5",           I18N_NOOP("ISO B5"),       182.0,  257.0 },
00122     // Hmm, wikipedia says 184.15 * 266.7 for executive !
00123     { PG_US_EXECUTIVE,  KPrinter::Executive,    "Executive",    I18N_NOOP("US Executive"), 191.0,  254.0 }, // should be 190.5 mm x 254.0 mm
00124     { PG_DIN_A0,        KPrinter::A0,           "A0",           I18N_NOOP("ISO A0"),       841.0, 1189.0 },
00125     { PG_DIN_A1,        KPrinter::A1,           "A1",           I18N_NOOP("ISO A1"),       594.0,  841.0 },
00126     { PG_DIN_A2,        KPrinter::A2,           "A2",           I18N_NOOP("ISO A2"),       420.0,  594.0 },
00127     { PG_DIN_A6,        KPrinter::A6,           "A6",           I18N_NOOP("ISO A6"),       105.0,  148.0 },
00128     { PG_DIN_A7,        KPrinter::A7,           "A7",           I18N_NOOP("ISO A7"),        74.0,  105.0 },
00129     { PG_DIN_A8,        KPrinter::A8,           "A8",           I18N_NOOP("ISO A8"),        52.0,   74.0 },
00130     { PG_DIN_A9,        KPrinter::A9,           "A9",           I18N_NOOP("ISO A9"),        37.0,   52.0 },
00131     { PG_DIN_B0,        KPrinter::B0,           "B0",           I18N_NOOP("ISO B0"),      1030.0, 1456.0 },
00132     { PG_DIN_B1,        KPrinter::B1,           "B1",           I18N_NOOP("ISO B1"),       728.0, 1030.0 },
00133     { PG_DIN_B10,       KPrinter::B10,          "B10",          I18N_NOOP("ISO B10"),       32.0,   45.0 },
00134     { PG_DIN_B2,        KPrinter::B2,           "B2",           I18N_NOOP("ISO B2"),       515.0,  728.0 },
00135     { PG_DIN_B3,        KPrinter::B3,           "B3",           I18N_NOOP("ISO B3"),       364.0,  515.0 },
00136     { PG_DIN_B4,        KPrinter::B4,           "B4",           I18N_NOOP("ISO B4"),       257.0,  364.0 },
00137     { PG_DIN_B6,        KPrinter::B6,           "B6",           I18N_NOOP("ISO B6"),       128.0,  182.0 },
00138     { PG_ISO_C5,        KPrinter::C5E,          "C5",           I18N_NOOP("ISO C5"),       163.0,  229.0 }, // Some sources tells: 162 mm x 228 mm
00139     { PG_US_COMM10,     KPrinter::Comm10E,      "Comm10",       I18N_NOOP("US Common 10"), 105.0,  241.0 }, // should be 104.775 mm x 241.3 mm
00140     { PG_ISO_DL,        KPrinter::DLE,          "DL",           I18N_NOOP("ISO DL"),       110.0,  220.0 },
00141     { PG_US_FOLIO,      KPrinter::Folio,        "Folio",        I18N_NOOP("US Folio"),     210.0,  330.0 }, // should be 209.54 mm x 330.2 mm
00142     { PG_US_LEDGER,     KPrinter::Ledger,       "Ledger",       I18N_NOOP("US Ledger"),    432.0,  279.0 }, // should be 431.8 mm x 297.4 mm
00143     { PG_US_TABLOID,    KPrinter::Tabloid,      "Tabloid",      I18N_NOOP("US Tabloid"),   279.0,  432.0 }  // should be 297.4 mm x 431.8 mm
00144 };
00145 
00146 int KoPageFormat::printerPageSize( KoFormat format )
00147 {
00148     if ( format == PG_SCREEN )
00149     {
00150             kdWarning() << "You use the page layout SCREEN. Printing in DIN A4 LANDSCAPE." << endl;
00151             return KPrinter::A4;
00152     }
00153     else if ( format == PG_CUSTOM )
00154     {
00155             kdWarning() << "The used page layout (CUSTOM) is not supported by KPrinter. Printing in A4." << endl;
00156             return KPrinter::A4;
00157     }
00158     else if ( format <= PG_LAST_FORMAT )
00159         return pageFormatInfo[ format ].kprinter;
00160     else
00161         return KPrinter::A4;
00162 }
00163 
00164 double KoPageFormat::width( KoFormat format, KoOrientation orientation )
00165 {
00166     if ( orientation == PG_LANDSCAPE )
00167         return height( format, PG_PORTRAIT );
00168     if ( format <= PG_LAST_FORMAT )
00169         return pageFormatInfo[ format ].width;
00170     return PG_A4_WIDTH;   // should never happen
00171 }
00172 
00173 double KoPageFormat::height( KoFormat format, KoOrientation orientation )
00174 {
00175     if ( orientation == PG_LANDSCAPE )
00176         return width( format, PG_PORTRAIT );
00177     if ( format <= PG_LAST_FORMAT )
00178         return pageFormatInfo[ format ].height;
00179     return PG_A4_HEIGHT;
00180 }
00181 
00182 KoFormat KoPageFormat::guessFormat( double width, double height )
00183 {
00184     for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
00185     {
00186         // We have some tolerance. 1pt is a third of a mm, this is
00187         // barely noticeable for a page size.
00188         if ( i != PG_CUSTOM
00189              && kAbs( width - pageFormatInfo[i].width ) < 1.0
00190              && kAbs( height - pageFormatInfo[i].height ) < 1.0 )
00191             return static_cast<KoFormat>(i);
00192     }
00193     return PG_CUSTOM;
00194 }
00195 
00196 QString KoPageFormat::formatString( KoFormat format )
00197 {
00198     if ( format <= PG_LAST_FORMAT )
00199         return QString::fromLatin1( pageFormatInfo[ format ].shortName );
00200     return QString::fromLatin1( "A4" );
00201 }
00202 
00203 KoFormat KoPageFormat::formatFromString( const QString & string )
00204 {
00205     for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
00206     {
00207         if (string == QString::fromLatin1( pageFormatInfo[ i ].shortName ))
00208             return pageFormatInfo[ i ].format;
00209     }
00210     // We do not know the format name, so we have a custom format
00211     return PG_CUSTOM;
00212 }
00213 
00214 KoFormat KoPageFormat::defaultFormat()
00215 {
00216     int kprinter = KGlobal::locale()->pageSize();
00217     for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
00218     {
00219         if ( pageFormatInfo[ i ].kprinter == kprinter )
00220             return static_cast<KoFormat>(i);
00221     }
00222     return PG_DIN_A4;
00223 }
00224 
00225 QString KoPageFormat::name( KoFormat format )
00226 {
00227     if ( format <= PG_LAST_FORMAT )
00228         return i18n( pageFormatInfo[ format ].descriptiveName );
00229     return i18n( pageFormatInfo[ PG_DIN_A4 ].descriptiveName );
00230 }
00231 
00232 QStringList KoPageFormat::allFormats()
00233 {
00234     QStringList lst;
00235     for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
00236     {
00237         lst << i18n( pageFormatInfo[ i ].descriptiveName );
00238     }
00239     return lst;
00240 }
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:02 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003