Update ooo320-m1
[ooovba.git] / i18npool / source / paper / paper.cxx
blob88ccdcc0cee74820a671fcf59f0c0e5111f1b74a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile$
10 * $Revision$
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_i18npool.hxx"
33 #include <sal/config.h>
34 #include <rtl/ustring.hxx>
35 #include <rtl/string.hxx>
36 #include <comphelper/processfactory.hxx>
37 #include <com/sun/star/i18n/ScriptType.hpp>
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 #include <com/sun/star/container/XNameAccess.hpp>
41 #include "i18npool/mslangid.hxx"
42 #include "i18npool/paper.hxx"
44 #include <utility>
45 #include <cstdlib>
47 #ifdef UNX
48 #include <stdio.h>
49 #include <string.h>
50 #include <locale.h>
51 #include <langinfo.h>
52 #endif
54 struct PageDesc
56 long m_nWidth;
57 long m_nHeight;
58 const char *m_pPSName;
59 const char *m_pAltPSName;
62 #define PT2MM100( v ) \
63 (long)(((v) * 35.27777778) + 0.5)
65 #define IN2MM100( v ) \
66 ((long)(((v) * 2540) + 0.5))
68 #define MM2MM100( v ) \
69 ((long)((v) * 100))
71 //PostScript Printer Description File Format Specification
72 //http://partners.adobe.com/public/developer/en/ps/5003.PPD_Spec_v4.3.pdf
73 //http://www.y-adagio.com/public/committees/docsii/doc_00-49/symp_ulaan/china_ppr.pdf (Kai)
74 //http://www.sls.psi.ch/controls/help/howto/Howto_Print_a_A0_Poster_at_WSLA_012_2.pdf (Dia)
75 static PageDesc aDinTab[] =
77 { MM2MM100( 841 ), MM2MM100( 1189 ), "A0", NULL },
78 { MM2MM100( 594 ), MM2MM100( 841 ), "A1", NULL },
79 { MM2MM100( 420 ), MM2MM100( 594 ), "A2", NULL },
80 { MM2MM100( 297 ), MM2MM100( 420 ), "A3", NULL },
81 { MM2MM100( 210 ), MM2MM100( 297 ), "A4", NULL },
82 { MM2MM100( 148 ), MM2MM100( 210 ), "A5", NULL },
83 { MM2MM100( 250 ), MM2MM100( 353 ), "ISOB4", NULL },
84 { MM2MM100( 176 ), MM2MM100( 250 ), "ISOB5", NULL },
85 { IN2MM100( 8.5 ), IN2MM100( 11 ), "Letter", "Note" },
86 { IN2MM100( 8.5 ), IN2MM100( 14 ), "Legal", NULL },
87 { IN2MM100( 11 ), IN2MM100( 17 ), "Tabloid", "11x17" },
88 { 0, 0, NULL, NULL }, //User
89 { MM2MM100( 125 ), MM2MM100( 176 ), "ISOB6", NULL },
90 { MM2MM100( 229 ), MM2MM100( 324 ), "EnvC4", "C4" },
91 { MM2MM100( 162 ), MM2MM100( 229 ), "EnvC5", "C5" },
92 { MM2MM100( 114 ), MM2MM100( 162 ), "EnvC6", "C6" },
93 { MM2MM100( 114 ), MM2MM100( 229 ), "EnvC65", NULL },
94 { MM2MM100( 110 ), MM2MM100( 220 ), "EnvDL", "DL" },
95 { MM2MM100( 180), MM2MM100( 270 ), NULL, NULL }, //Dia
96 { MM2MM100( 210), MM2MM100( 280 ), NULL, NULL }, //Screen
97 { IN2MM100( 17 ), IN2MM100( 22 ), "AnsiC", "CSheet" },
98 { IN2MM100( 22 ), IN2MM100( 34 ), "AnsiD", "DSheet" },
99 { IN2MM100( 34 ), IN2MM100( 44 ), "AnsiE", "ESheet" },
100 { IN2MM100( 7.25 ), IN2MM100( 10.5 ), "Executive", NULL },
101 //"Folio" is a different size in the PPD documentation than 8.5x11
102 //This "FanFoldGermanLegal" is known in the Philippines as
103 //"Legal" paper or "Long Bond Paper". The "Legal" name causing untold
104 //misery, given the differently sized US "Legal" paper
105 { IN2MM100( 8.5 ), IN2MM100( 13 ), "FanFoldGermanLegal", NULL },
106 { IN2MM100( 3.875 ), IN2MM100( 7.5 ), "EnvMonarch", "Monarch" },
107 { IN2MM100( 3.625 ), IN2MM100( 6.5 ), "EnvPersonal", "Personal" },
108 { IN2MM100( 3.875 ), IN2MM100( 8.875 ), "Env9", NULL },
109 { IN2MM100( 4.125 ), IN2MM100( 9.5 ), "Env10", "Comm10" },
110 { IN2MM100( 4.5 ), IN2MM100( 10.375 ), "Env11", NULL },
111 { IN2MM100( 4.75 ), IN2MM100( 11 ), "Env12", NULL },
112 { MM2MM100( 184 ), MM2MM100( 260 ), NULL, NULL }, //Kai16
113 { MM2MM100( 130 ), MM2MM100( 184 ), NULL, NULL }, //Kai32
114 { MM2MM100( 140 ), MM2MM100( 203 ), NULL, NULL }, //BigKai32
115 { MM2MM100( 257 ), MM2MM100( 364 ), "B4", NULL }, //JIS
116 { MM2MM100( 182 ), MM2MM100( 257 ), "B5", NULL }, //JIS
117 { MM2MM100( 128 ), MM2MM100( 182 ), "B6", NULL }, //JIS
118 { IN2MM100( 17 ), IN2MM100( 11 ), "Ledger", NULL },
119 { IN2MM100( 5.5 ), IN2MM100( 8.5 ), "Statement", NULL },
120 { PT2MM100( 610 ), PT2MM100( 780 ), "Quarto", NULL },
121 { IN2MM100( 10 ), IN2MM100( 14 ), "10x14", NULL },
122 { IN2MM100( 5.5 ), IN2MM100( 11.5 ), "Env14", NULL },
123 { MM2MM100( 324 ), MM2MM100( 458 ), "EnvC3", "C3" },
124 { MM2MM100( 110 ), MM2MM100( 230 ), "EnvItalian", NULL },
125 { IN2MM100( 14.875 ),IN2MM100( 11 ), "FanFoldUS", NULL },
126 { IN2MM100( 8.5 ), IN2MM100( 13 ), "FanFoldGerman", NULL },
127 { MM2MM100( 100 ), MM2MM100( 148 ), "Postcard", NULL },
128 { IN2MM100( 9 ), IN2MM100( 11 ), "9x11", NULL },
129 { IN2MM100( 10 ), IN2MM100( 11 ), "10x11", NULL },
130 { IN2MM100( 15 ), IN2MM100( 11 ), "15x11", NULL },
131 { MM2MM100( 220 ), MM2MM100( 220 ), "EnvInvite", NULL },
132 { MM2MM100( 227 ), MM2MM100( 356 ), "SuperA", NULL },
133 { MM2MM100( 305 ), MM2MM100( 487 ), "SuperB", NULL },
134 { IN2MM100( 8.5 ), IN2MM100( 12.69 ), "LetterPlus", NULL },
135 { IN2MM100( 8.5 ), IN2MM100( 12.69 ), "LetterPlus", NULL },
136 { MM2MM100( 210 ), MM2MM100( 330 ), "A4Plus", NULL },
137 { MM2MM100( 200 ), MM2MM100( 148 ), "DoublePostcard", NULL },
138 { MM2MM100( 105 ), MM2MM100( 148 ), "A6", NULL },
139 { IN2MM100( 12 ), IN2MM100( 11 ), "12x11", NULL },
140 { MM2MM100( 74 ), MM2MM100( 105 ), "A7", NULL },
141 { MM2MM100( 52 ), MM2MM100( 74 ), "A8", NULL },
142 { MM2MM100( 37 ), MM2MM100( 52 ), "A9", NULL },
143 { MM2MM100( 26 ), MM2MM100( 37 ), "A10", NULL },
144 { MM2MM100( 1000 ), MM2MM100( 1414 ), "ISOB0", NULL },
145 { MM2MM100( 707 ), MM2MM100( 1000 ), "ISOB1", NULL },
146 { MM2MM100( 500 ), MM2MM100( 707 ), "ISOB2", NULL },
147 { MM2MM100( 353 ), MM2MM100( 500 ), "ISOB3", NULL },
148 { MM2MM100( 88 ), MM2MM100( 125 ), "ISOB7", NULL },
149 { MM2MM100( 62 ), MM2MM100( 88 ), "ISOB8", NULL },
150 { MM2MM100( 44 ), MM2MM100( 62 ), "ISOB9", NULL },
151 { MM2MM100( 31 ), MM2MM100( 44 ), "ISOB10", NULL },
152 { MM2MM100( 458 ), MM2MM100( 648 ), "EnvC2", "C2" },
153 { MM2MM100( 81 ), MM2MM100( 114 ), "EnvC7", "C7" },
154 { MM2MM100( 57 ), MM2MM100( 81 ), "EnvC8", "C8" },
155 { IN2MM100( 9 ), IN2MM100( 12 ), "ARCHA", NULL },
156 { IN2MM100( 12 ), IN2MM100( 18 ), "ARCHB", NULL },
157 { IN2MM100( 18 ), IN2MM100( 24 ), "ARCHC", NULL },
158 { IN2MM100( 24 ), IN2MM100( 36 ), "ARCHD", NULL },
159 { IN2MM100( 36 ), IN2MM100( 48 ), "ARCHE", NULL }
162 static const size_t nTabSize = sizeof(aDinTab) / sizeof(aDinTab[0]);
164 #define MAXSLOPPY 11
166 bool PaperInfo::doSloppyFit()
168 if (m_eType != PAPER_USER)
169 return true;
171 for ( size_t i = 0; i < nTabSize; ++i )
173 if (i == PAPER_USER) continue;
175 long lDiffW = labs(aDinTab[i].m_nWidth - m_nPaperWidth);
176 long lDiffH = labs(aDinTab[i].m_nHeight - m_nPaperHeight);
178 if ( lDiffW < MAXSLOPPY && lDiffH < MAXSLOPPY )
180 m_nPaperWidth = aDinTab[i].m_nWidth;
181 m_nPaperHeight = aDinTab[i].m_nHeight;
182 m_eType = (Paper)i;
183 return true;
187 return false;
190 bool PaperInfo::sloppyEqual(const PaperInfo &rOther) const
192 return
194 (labs(m_nPaperWidth - rOther.m_nPaperWidth) < MAXSLOPPY) &&
195 (labs(m_nPaperHeight - rOther.m_nPaperHeight) < MAXSLOPPY)
199 long PaperInfo::sloppyFitPageDimension(long nDimension)
201 for ( size_t i = 0; i < nTabSize; ++i )
203 if (i == PAPER_USER) continue;
204 long lDiff;
206 lDiff = labs(aDinTab[i].m_nWidth - nDimension);
207 if ( lDiff < MAXSLOPPY )
208 return aDinTab[i].m_nWidth;
210 lDiff = labs(aDinTab[i].m_nHeight - nDimension);
211 if ( lDiff < MAXSLOPPY )
212 return aDinTab[i].m_nHeight;
214 return nDimension;
217 PaperInfo PaperInfo::getSystemDefaultPaper()
219 using ::com::sun::star::uno::Reference;
220 using ::com::sun::star::lang::XMultiServiceFactory;
221 using ::com::sun::star::uno::UNO_QUERY_THROW;
222 using ::com::sun::star::uno::Sequence;
223 using ::com::sun::star::uno::Any;
224 using ::com::sun::star::container::XNameAccess;
225 using ::com::sun::star::uno::Exception;
226 # define CREATE_OUSTRING( ascii ) \
227 rtl::OUString::intern( RTL_CONSTASCII_USTRINGPARAM( ascii ) )
229 rtl::OUString aLocaleStr;
231 Reference< XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
232 Reference< XMultiServiceFactory > xConfigProv(
233 xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.configuration.ConfigurationProvider" ) ),
234 UNO_QUERY_THROW );
236 Sequence< Any > aArgs( 1 );
237 aArgs[ 0 ] <<= CREATE_OUSTRING( "org.openoffice.Setup/L10N/" );
238 Reference< XNameAccess > xConfigNA( xConfigProv->createInstanceWithArguments(
239 CREATE_OUSTRING( "com.sun.star.configuration.ConfigurationAccess" ), aArgs ), UNO_QUERY_THROW );
242 // try user-defined locale setting
243 xConfigNA->getByName( CREATE_OUSTRING( "ooSetupSystemLocale" ) ) >>= aLocaleStr;
245 catch( Exception& ) {}
247 #ifdef UNX
248 // if set to "use system", get papersize from system
249 if (aLocaleStr.getLength() == 0)
251 static bool bInitialized = false;
252 static PaperInfo aInstance(PAPER_A4);
254 if (bInitialized)
255 return aInstance;
257 // try libpaper
258 // #i78617# workaround missing paperconf command
259 FILE* pPipe = popen( "sh -c paperconf 2>/dev/null", "r" );
260 if( pPipe )
262 Paper ePaper = PAPER_USER;
264 char aBuffer[ 1024 ];
265 aBuffer[0] = 0;
266 char *pBuffer = fgets( aBuffer, sizeof(aBuffer), pPipe );
267 pclose( pPipe );
269 if (pBuffer && *pBuffer != 0)
271 rtl::OString aPaper(pBuffer);
272 aPaper = aPaper.trim();
273 static struct { const char *pName; Paper ePaper; } aCustoms [] =
275 { "B0", PAPER_B0_ISO },
276 { "B1", PAPER_B1_ISO },
277 { "B2", PAPER_B2_ISO },
278 { "B3", PAPER_B3_ISO },
279 { "B4", PAPER_B4_ISO },
280 { "B5", PAPER_B5_ISO },
281 { "B6", PAPER_B6_ISO },
282 { "B7", PAPER_B7_ISO },
283 { "B8", PAPER_B8_ISO },
284 { "B9", PAPER_B9_ISO },
285 { "B10", PAPER_B10_ISO },
286 { "folio", PAPER_FANFOLD_LEGAL_DE },
287 { "flsa", PAPER_FANFOLD_LEGAL_DE },
288 { "flse", PAPER_FANFOLD_LEGAL_DE }
291 bool bHalve = false;
293 size_t nExtraTabSize = sizeof(aCustoms) / sizeof(aCustoms[0]);
294 for (size_t i = 0; i < nExtraTabSize; ++i)
296 if (rtl_str_compareIgnoreAsciiCase(aCustoms[i].pName, aPaper.getStr()) == 0)
298 ePaper = aCustoms[i].ePaper;
299 break;
303 if (ePaper == PAPER_USER)
305 bHalve = !rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
306 aPaper.getStr(), aPaper.getLength(), "half", 4, 4);
307 if (bHalve)
308 aPaper = aPaper.copy(4);
309 ePaper = PaperInfo::fromPSName(aPaper);
312 if (ePaper != PAPER_USER)
314 aInstance = PaperInfo(ePaper);
315 if (bHalve)
316 aInstance = PaperInfo(aInstance.getHeight()/2, aInstance.getWidth());
317 bInitialized = true;
318 return aInstance;
323 #if defined(LC_PAPER) && defined(_GNU_SOURCE)
325 union paperword { char *string; int word; };
327 // try LC_PAPER
328 paperword w, h;
329 w.string = nl_langinfo(_NL_PAPER_WIDTH);
330 h.string = nl_langinfo(_NL_PAPER_HEIGHT);
332 //glibc stores sizes as integer mm units
333 w.word *= 100;
334 h.word *= 100;
336 for ( size_t i = 0; i < nTabSize; ++i )
338 if (i == PAPER_USER) continue;
340 //glibc stores sizes as integer mm units, and so is inaccurate. To
341 //find a standard paper size we calculate the standard paper sizes
342 //into equally inaccurate mm and compare
343 long width = (aDinTab[i].m_nWidth + 50) / 100;
344 long height = (aDinTab[i].m_nHeight + 50) / 100;
346 if (width == w.word/100 && height == h.word/100)
348 w.word = aDinTab[i].m_nWidth;
349 h.word = aDinTab[i].m_nHeight;
350 break;
354 aInstance = PaperInfo(w.word, h.word);
355 bInitialized = true;
356 return aInstance;
357 #endif
359 #endif
363 // if set to "use system", try to get locale from system
364 if( aLocaleStr.getLength() == 0 )
366 aArgs[ 0 ] <<= CREATE_OUSTRING( "org.openoffice.System/L10N/" );
367 xConfigNA.set( xConfigProv->createInstanceWithArguments(
368 CREATE_OUSTRING( "com.sun.star.configuration.ConfigurationAccess" ), aArgs ),
369 UNO_QUERY_THROW );
370 xConfigNA->getByName( CREATE_OUSTRING( "Locale" ) ) >>= aLocaleStr;
373 catch( Exception& ) {}
375 if (aLocaleStr.getLength() == 0)
376 aLocaleStr = CREATE_OUSTRING("en-US");
378 // convert locale string to locale struct
379 ::com::sun::star::lang::Locale aSysLocale;
380 sal_Int32 nDashPos = aLocaleStr.indexOf( '-' );
381 if( nDashPos < 0 ) nDashPos = aLocaleStr.getLength();
382 aSysLocale.Language = aLocaleStr.copy( 0, nDashPos );
383 if( nDashPos + 1 < aLocaleStr.getLength() )
384 aSysLocale.Country = aLocaleStr.copy( nDashPos + 1 );
386 return PaperInfo::getDefaultPaperForLocale(aSysLocale);
389 PaperInfo::PaperInfo(Paper eType) : m_eType(eType)
391 m_nPaperWidth = aDinTab[m_eType].m_nWidth;
392 m_nPaperHeight = aDinTab[m_eType].m_nHeight;
395 PaperInfo::PaperInfo(long nPaperWidth, long nPaperHeight)
396 : m_eType(PAPER_USER),
397 m_nPaperWidth(nPaperWidth),
398 m_nPaperHeight(nPaperHeight)
400 for ( size_t i = 0; i < nTabSize; ++i )
402 if (
403 (nPaperWidth == aDinTab[i].m_nWidth) &&
404 (nPaperHeight == aDinTab[i].m_nHeight)
407 m_eType = static_cast<Paper>(i);
408 break;
413 rtl::OString PaperInfo::toPSName(Paper ePaper)
415 return static_cast<size_t>(ePaper) < nTabSize ?
416 rtl::OString(aDinTab[ePaper].m_pPSName) : rtl::OString();
419 Paper PaperInfo::fromPSName(const rtl::OString &rName)
421 if (!rName.getLength())
422 return PAPER_USER;
424 for ( size_t i = 0; i < nTabSize; ++i )
426 if (aDinTab[i].m_pPSName &&
427 !rtl_str_compareIgnoreAsciiCase(aDinTab[i].m_pPSName, rName.getStr()))
429 return static_cast<Paper>(i);
431 else if (aDinTab[i].m_pAltPSName &&
432 !rtl_str_compareIgnoreAsciiCase(aDinTab[i].m_pAltPSName, rName.getStr()))
434 return static_cast<Paper>(i);
438 return PAPER_USER;
441 //http://wiki.services.openoffice.org/wiki/DefaultPaperSize
442 //http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/localedata/locales/?cvsroot=glibc
443 //http://www.unicode.org/cldr/data/charts/supplemental/territory_language_information.html
444 //http://en.wikipedia.org/wiki/Paper_size
445 //http://msdn.microsoft.com/en-us/library/cc195164.aspx
446 PaperInfo PaperInfo::getDefaultPaperForLocale(
447 const ::com::sun::star::lang::Locale & rLocale)
449 Paper eType = PAPER_A4;
451 if (
452 //United States, Letter
453 !rLocale.Country.compareToAscii("US") ||
454 //Puerto Rico, http://sources.redhat.com/ml/libc-hacker/2001-07/msg00046.html
455 !rLocale.Country.compareToAscii("PR") ||
456 //Canada, http://sources.redhat.com/ml/libc-hacker/2001-07/msg00053.html
457 !rLocale.Country.compareToAscii("CA") ||
458 //Venuzuela, https://www.redhat.com/archives/fedora-devel-list/2008-August/msg00019.html
459 !rLocale.Country.compareToAscii("VE") ||
460 //Chile, https://www.redhat.com/archives/fedora-devel-list/2008-August/msg00240.html
461 !rLocale.Country.compareToAscii("CL") ||
462 //Mexico, http://qa.openoffice.org/issues/show_bug.cgi?id=49739
463 !rLocale.Country.compareToAscii("MX") ||
464 //Colombia, http://qa.openoffice.org/issues/show_bug.cgi?id=69703
465 !rLocale.Country.compareToAscii("CO") ||
466 //Philippines,
467 // http://ubuntuliving.blogspot.com/2008/07/default-paper-size-in-evince.html
468 // http://www.gov.ph/faqs/driverslicense.asp
469 !rLocale.Country.compareToAscii("PH")
472 eType = PAPER_LETTER;
475 return eType;
478 /* vi:set tabstop=4 shiftwidth=4 expandtab: */