merged tag ooo/OOO330_m14
[LibreOffice.git] / i18npool / source / paper / paper.cxx
blobe94efa6e06c57d6c5d5f5c6c8a521a8bebc7f1ea
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_i18npool.hxx"
30 #include <sal/config.h>
31 #include <rtl/ustring.hxx>
32 #include <rtl/string.hxx>
33 #include <comphelper/processfactory.hxx>
34 #include <com/sun/star/i18n/ScriptType.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/container/XNameAccess.hpp>
38 #include "i18npool/mslangid.hxx"
39 #include "i18npool/paper.hxx"
41 #include <utility>
42 #include <cstdlib>
44 #ifdef UNX
45 #include <stdio.h>
46 #include <string.h>
47 #include <locale.h>
48 #include <langinfo.h>
49 #endif
51 struct PageDesc
53 long m_nWidth;
54 long m_nHeight;
55 const char *m_pPSName;
56 const char *m_pAltPSName;
59 #define PT2MM100( v ) \
60 (long)(((v) * 35.27777778) + 0.5)
62 #define IN2MM100( v ) \
63 ((long)(((v) * 2540) + 0.5))
65 #define MM2MM100( v ) \
66 ((long)((v) * 100))
68 //PostScript Printer Description File Format Specification
69 //http://partners.adobe.com/public/developer/en/ps/5003.PPD_Spec_v4.3.pdf
70 //http://www.y-adagio.com/public/committees/docsii/doc_00-49/symp_ulaan/china_ppr.pdf (Kai)
71 //http://www.sls.psi.ch/controls/help/howto/Howto_Print_a_A0_Poster_at_WSLA_012_2.pdf (Dia)
72 static PageDesc aDinTab[] =
74 { MM2MM100( 841 ), MM2MM100( 1189 ), "A0", NULL },
75 { MM2MM100( 594 ), MM2MM100( 841 ), "A1", NULL },
76 { MM2MM100( 420 ), MM2MM100( 594 ), "A2", NULL },
77 { MM2MM100( 297 ), MM2MM100( 420 ), "A3", NULL },
78 { MM2MM100( 210 ), MM2MM100( 297 ), "A4", NULL },
79 { MM2MM100( 148 ), MM2MM100( 210 ), "A5", NULL },
80 { MM2MM100( 250 ), MM2MM100( 353 ), "ISOB4", NULL },
81 { MM2MM100( 176 ), MM2MM100( 250 ), "ISOB5", NULL },
82 { IN2MM100( 8.5 ), IN2MM100( 11 ), "Letter", "Note" },
83 { IN2MM100( 8.5 ), IN2MM100( 14 ), "Legal", NULL },
84 { IN2MM100( 11 ), IN2MM100( 17 ), "Tabloid", "11x17" },
85 { 0, 0, NULL, NULL }, //User
86 { MM2MM100( 125 ), MM2MM100( 176 ), "ISOB6", NULL },
87 { MM2MM100( 229 ), MM2MM100( 324 ), "EnvC4", "C4" },
88 { MM2MM100( 162 ), MM2MM100( 229 ), "EnvC5", "C5" },
89 { MM2MM100( 114 ), MM2MM100( 162 ), "EnvC6", "C6" },
90 { MM2MM100( 114 ), MM2MM100( 229 ), "EnvC65", NULL },
91 { MM2MM100( 110 ), MM2MM100( 220 ), "EnvDL", "DL" },
92 { MM2MM100( 180), MM2MM100( 270 ), NULL, NULL }, //Dia
93 { MM2MM100( 210), MM2MM100( 280 ), NULL, NULL }, //Screen
94 { IN2MM100( 17 ), IN2MM100( 22 ), "AnsiC", "CSheet" },
95 { IN2MM100( 22 ), IN2MM100( 34 ), "AnsiD", "DSheet" },
96 { IN2MM100( 34 ), IN2MM100( 44 ), "AnsiE", "ESheet" },
97 { IN2MM100( 7.25 ), IN2MM100( 10.5 ), "Executive", NULL },
98 //"Folio" is a different size in the PPD documentation than 8.5x11
99 //This "FanFoldGermanLegal" is known in the Philippines as
100 //"Legal" paper or "Long Bond Paper". The "Legal" name causing untold
101 //misery, given the differently sized US "Legal" paper
102 { IN2MM100( 8.5 ), IN2MM100( 13 ), "FanFoldGermanLegal", NULL },
103 { IN2MM100( 3.875 ), IN2MM100( 7.5 ), "EnvMonarch", "Monarch" },
104 { IN2MM100( 3.625 ), IN2MM100( 6.5 ), "EnvPersonal", "Personal" },
105 { IN2MM100( 3.875 ), IN2MM100( 8.875 ), "Env9", NULL },
106 { IN2MM100( 4.125 ), IN2MM100( 9.5 ), "Env10", "Comm10" },
107 { IN2MM100( 4.5 ), IN2MM100( 10.375 ), "Env11", NULL },
108 { IN2MM100( 4.75 ), IN2MM100( 11 ), "Env12", NULL },
109 { MM2MM100( 184 ), MM2MM100( 260 ), NULL, NULL }, //Kai16
110 { MM2MM100( 130 ), MM2MM100( 184 ), NULL, NULL }, //Kai32
111 { MM2MM100( 140 ), MM2MM100( 203 ), NULL, NULL }, //BigKai32
112 { MM2MM100( 257 ), MM2MM100( 364 ), "B4", NULL }, //JIS
113 { MM2MM100( 182 ), MM2MM100( 257 ), "B5", NULL }, //JIS
114 { MM2MM100( 128 ), MM2MM100( 182 ), "B6", NULL }, //JIS
115 { IN2MM100( 17 ), IN2MM100( 11 ), "Ledger", NULL },
116 { IN2MM100( 5.5 ), IN2MM100( 8.5 ), "Statement", NULL },
117 { PT2MM100( 610 ), PT2MM100( 780 ), "Quarto", NULL },
118 { IN2MM100( 10 ), IN2MM100( 14 ), "10x14", NULL },
119 { IN2MM100( 5.5 ), IN2MM100( 11.5 ), "Env14", NULL },
120 { MM2MM100( 324 ), MM2MM100( 458 ), "EnvC3", "C3" },
121 { MM2MM100( 110 ), MM2MM100( 230 ), "EnvItalian", NULL },
122 { IN2MM100( 14.875 ),IN2MM100( 11 ), "FanFoldUS", NULL },
123 { IN2MM100( 8.5 ), IN2MM100( 13 ), "FanFoldGerman", NULL },
124 { MM2MM100( 100 ), MM2MM100( 148 ), "Postcard", NULL },
125 { IN2MM100( 9 ), IN2MM100( 11 ), "9x11", NULL },
126 { IN2MM100( 10 ), IN2MM100( 11 ), "10x11", NULL },
127 { IN2MM100( 15 ), IN2MM100( 11 ), "15x11", NULL },
128 { MM2MM100( 220 ), MM2MM100( 220 ), "EnvInvite", NULL },
129 { MM2MM100( 227 ), MM2MM100( 356 ), "SuperA", NULL },
130 { MM2MM100( 305 ), MM2MM100( 487 ), "SuperB", NULL },
131 { IN2MM100( 8.5 ), IN2MM100( 12.69 ), "LetterPlus", NULL },
132 { IN2MM100( 8.5 ), IN2MM100( 12.69 ), "LetterPlus", NULL },
133 { MM2MM100( 210 ), MM2MM100( 330 ), "A4Plus", NULL },
134 { MM2MM100( 200 ), MM2MM100( 148 ), "DoublePostcard", NULL },
135 { MM2MM100( 105 ), MM2MM100( 148 ), "A6", NULL },
136 { IN2MM100( 12 ), IN2MM100( 11 ), "12x11", NULL },
137 { MM2MM100( 74 ), MM2MM100( 105 ), "A7", NULL },
138 { MM2MM100( 52 ), MM2MM100( 74 ), "A8", NULL },
139 { MM2MM100( 37 ), MM2MM100( 52 ), "A9", NULL },
140 { MM2MM100( 26 ), MM2MM100( 37 ), "A10", NULL },
141 { MM2MM100( 1000 ), MM2MM100( 1414 ), "ISOB0", NULL },
142 { MM2MM100( 707 ), MM2MM100( 1000 ), "ISOB1", NULL },
143 { MM2MM100( 500 ), MM2MM100( 707 ), "ISOB2", NULL },
144 { MM2MM100( 353 ), MM2MM100( 500 ), "ISOB3", NULL },
145 { MM2MM100( 88 ), MM2MM100( 125 ), "ISOB7", NULL },
146 { MM2MM100( 62 ), MM2MM100( 88 ), "ISOB8", NULL },
147 { MM2MM100( 44 ), MM2MM100( 62 ), "ISOB9", NULL },
148 { MM2MM100( 31 ), MM2MM100( 44 ), "ISOB10", NULL },
149 { MM2MM100( 458 ), MM2MM100( 648 ), "EnvC2", "C2" },
150 { MM2MM100( 81 ), MM2MM100( 114 ), "EnvC7", "C7" },
151 { MM2MM100( 57 ), MM2MM100( 81 ), "EnvC8", "C8" },
152 { IN2MM100( 9 ), IN2MM100( 12 ), "ARCHA", NULL },
153 { IN2MM100( 12 ), IN2MM100( 18 ), "ARCHB", NULL },
154 { IN2MM100( 18 ), IN2MM100( 24 ), "ARCHC", NULL },
155 { IN2MM100( 24 ), IN2MM100( 36 ), "ARCHD", NULL },
156 { IN2MM100( 36 ), IN2MM100( 48 ), "ARCHE", NULL }
159 static const size_t nTabSize = sizeof(aDinTab) / sizeof(aDinTab[0]);
161 #define MAXSLOPPY 11
163 bool PaperInfo::doSloppyFit()
165 if (m_eType != PAPER_USER)
166 return true;
168 for ( size_t i = 0; i < nTabSize; ++i )
170 if (i == PAPER_USER) continue;
172 long lDiffW = labs(aDinTab[i].m_nWidth - m_nPaperWidth);
173 long lDiffH = labs(aDinTab[i].m_nHeight - m_nPaperHeight);
175 if ( lDiffW < MAXSLOPPY && lDiffH < MAXSLOPPY )
177 m_nPaperWidth = aDinTab[i].m_nWidth;
178 m_nPaperHeight = aDinTab[i].m_nHeight;
179 m_eType = (Paper)i;
180 return true;
184 return false;
187 bool PaperInfo::sloppyEqual(const PaperInfo &rOther) const
189 return
191 (labs(m_nPaperWidth - rOther.m_nPaperWidth) < MAXSLOPPY) &&
192 (labs(m_nPaperHeight - rOther.m_nPaperHeight) < MAXSLOPPY)
196 long PaperInfo::sloppyFitPageDimension(long nDimension)
198 for ( size_t i = 0; i < nTabSize; ++i )
200 if (i == PAPER_USER) continue;
201 long lDiff;
203 lDiff = labs(aDinTab[i].m_nWidth - nDimension);
204 if ( lDiff < MAXSLOPPY )
205 return aDinTab[i].m_nWidth;
207 lDiff = labs(aDinTab[i].m_nHeight - nDimension);
208 if ( lDiff < MAXSLOPPY )
209 return aDinTab[i].m_nHeight;
211 return nDimension;
214 PaperInfo PaperInfo::getSystemDefaultPaper()
216 using ::com::sun::star::uno::Reference;
217 using ::com::sun::star::lang::XMultiServiceFactory;
218 using ::com::sun::star::uno::UNO_QUERY_THROW;
219 using ::com::sun::star::uno::Sequence;
220 using ::com::sun::star::uno::Any;
221 using ::com::sun::star::container::XNameAccess;
222 using ::com::sun::star::uno::Exception;
223 # define CREATE_OUSTRING( ascii ) \
224 rtl::OUString::intern( RTL_CONSTASCII_USTRINGPARAM( ascii ) )
226 rtl::OUString aLocaleStr;
228 Reference< XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
229 Reference< XMultiServiceFactory > xConfigProv(
230 xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.configuration.ConfigurationProvider" ) ),
231 UNO_QUERY_THROW );
233 Sequence< Any > aArgs( 1 );
234 aArgs[ 0 ] <<= CREATE_OUSTRING( "org.openoffice.Setup/L10N/" );
235 Reference< XNameAccess > xConfigNA( xConfigProv->createInstanceWithArguments(
236 CREATE_OUSTRING( "com.sun.star.configuration.ConfigurationAccess" ), aArgs ), UNO_QUERY_THROW );
239 // try user-defined locale setting
240 xConfigNA->getByName( CREATE_OUSTRING( "ooSetupSystemLocale" ) ) >>= aLocaleStr;
242 catch( Exception& ) {}
244 #ifdef UNX
245 // if set to "use system", get papersize from system
246 if (aLocaleStr.getLength() == 0)
248 static bool bInitialized = false;
249 static PaperInfo aInstance(PAPER_A4);
251 if (bInitialized)
252 return aInstance;
254 // try libpaper
255 // #i78617# workaround missing paperconf command
256 FILE* pPipe = popen( "sh -c paperconf 2>/dev/null", "r" );
257 if( pPipe )
259 Paper ePaper = PAPER_USER;
261 char aBuffer[ 1024 ];
262 aBuffer[0] = 0;
263 char *pBuffer = fgets( aBuffer, sizeof(aBuffer), pPipe );
264 pclose( pPipe );
266 if (pBuffer && *pBuffer != 0)
268 rtl::OString aPaper(pBuffer);
269 aPaper = aPaper.trim();
270 static struct { const char *pName; Paper ePaper; } aCustoms [] =
272 { "B0", PAPER_B0_ISO },
273 { "B1", PAPER_B1_ISO },
274 { "B2", PAPER_B2_ISO },
275 { "B3", PAPER_B3_ISO },
276 { "B4", PAPER_B4_ISO },
277 { "B5", PAPER_B5_ISO },
278 { "B6", PAPER_B6_ISO },
279 { "B7", PAPER_B7_ISO },
280 { "B8", PAPER_B8_ISO },
281 { "B9", PAPER_B9_ISO },
282 { "B10", PAPER_B10_ISO },
283 { "folio", PAPER_FANFOLD_LEGAL_DE },
284 { "flsa", PAPER_FANFOLD_LEGAL_DE },
285 { "flse", PAPER_FANFOLD_LEGAL_DE }
288 bool bHalve = false;
290 size_t nExtraTabSize = sizeof(aCustoms) / sizeof(aCustoms[0]);
291 for (size_t i = 0; i < nExtraTabSize; ++i)
293 if (rtl_str_compareIgnoreAsciiCase(aCustoms[i].pName, aPaper.getStr()) == 0)
295 ePaper = aCustoms[i].ePaper;
296 break;
300 if (ePaper == PAPER_USER)
302 bHalve = !rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
303 aPaper.getStr(), aPaper.getLength(), "half", 4, 4);
304 if (bHalve)
305 aPaper = aPaper.copy(4);
306 ePaper = PaperInfo::fromPSName(aPaper);
309 if (ePaper != PAPER_USER)
311 aInstance = PaperInfo(ePaper);
312 if (bHalve)
313 aInstance = PaperInfo(aInstance.getHeight()/2, aInstance.getWidth());
314 bInitialized = true;
315 return aInstance;
320 #if defined(LC_PAPER) && defined(_GNU_SOURCE)
322 union paperword { char *string; int word; };
324 // try LC_PAPER
325 paperword w, h;
326 w.string = nl_langinfo(_NL_PAPER_WIDTH);
327 h.string = nl_langinfo(_NL_PAPER_HEIGHT);
329 //glibc stores sizes as integer mm units
330 w.word *= 100;
331 h.word *= 100;
333 for ( size_t i = 0; i < nTabSize; ++i )
335 if (i == PAPER_USER) continue;
337 //glibc stores sizes as integer mm units, and so is inaccurate. To
338 //find a standard paper size we calculate the standard paper sizes
339 //into equally inaccurate mm and compare
340 long width = (aDinTab[i].m_nWidth + 50) / 100;
341 long height = (aDinTab[i].m_nHeight + 50) / 100;
343 if (width == w.word/100 && height == h.word/100)
345 w.word = aDinTab[i].m_nWidth;
346 h.word = aDinTab[i].m_nHeight;
347 break;
351 aInstance = PaperInfo(w.word, h.word);
352 bInitialized = true;
353 return aInstance;
354 #endif
356 #endif
360 // if set to "use system", try to get locale from system
361 if( aLocaleStr.getLength() == 0 )
363 aArgs[ 0 ] <<= CREATE_OUSTRING( "org.openoffice.System/L10N/" );
364 xConfigNA.set( xConfigProv->createInstanceWithArguments(
365 CREATE_OUSTRING( "com.sun.star.configuration.ConfigurationAccess" ), aArgs ),
366 UNO_QUERY_THROW );
367 xConfigNA->getByName( CREATE_OUSTRING( "Locale" ) ) >>= aLocaleStr;
370 catch( Exception& ) {}
372 if (aLocaleStr.getLength() == 0)
373 aLocaleStr = CREATE_OUSTRING("en-US");
375 // convert locale string to locale struct
376 ::com::sun::star::lang::Locale aSysLocale;
377 sal_Int32 nDashPos = aLocaleStr.indexOf( '-' );
378 if( nDashPos < 0 ) nDashPos = aLocaleStr.getLength();
379 aSysLocale.Language = aLocaleStr.copy( 0, nDashPos );
380 if( nDashPos + 1 < aLocaleStr.getLength() )
381 aSysLocale.Country = aLocaleStr.copy( nDashPos + 1 );
383 return PaperInfo::getDefaultPaperForLocale(aSysLocale);
386 PaperInfo::PaperInfo(Paper eType) : m_eType(eType)
388 m_nPaperWidth = aDinTab[m_eType].m_nWidth;
389 m_nPaperHeight = aDinTab[m_eType].m_nHeight;
392 PaperInfo::PaperInfo(long nPaperWidth, long nPaperHeight)
393 : m_eType(PAPER_USER),
394 m_nPaperWidth(nPaperWidth),
395 m_nPaperHeight(nPaperHeight)
397 for ( size_t i = 0; i < nTabSize; ++i )
399 if (
400 (nPaperWidth == aDinTab[i].m_nWidth) &&
401 (nPaperHeight == aDinTab[i].m_nHeight)
404 m_eType = static_cast<Paper>(i);
405 break;
410 rtl::OString PaperInfo::toPSName(Paper ePaper)
412 return static_cast<size_t>(ePaper) < nTabSize ?
413 rtl::OString(aDinTab[ePaper].m_pPSName) : rtl::OString();
416 Paper PaperInfo::fromPSName(const rtl::OString &rName)
418 if (!rName.getLength())
419 return PAPER_USER;
421 for ( size_t i = 0; i < nTabSize; ++i )
423 if (aDinTab[i].m_pPSName &&
424 !rtl_str_compareIgnoreAsciiCase(aDinTab[i].m_pPSName, rName.getStr()))
426 return static_cast<Paper>(i);
428 else if (aDinTab[i].m_pAltPSName &&
429 !rtl_str_compareIgnoreAsciiCase(aDinTab[i].m_pAltPSName, rName.getStr()))
431 return static_cast<Paper>(i);
435 return PAPER_USER;
438 //http://wiki.services.openoffice.org/wiki/DefaultPaperSize
439 //http://www.unicode.org/cldr/data/charts/supplemental/territory_language_information.html
440 //http://sourceware.org/git/?p=glibc.git;a=tree;f=localedata/locales
441 //http://en.wikipedia.org/wiki/Paper_size
442 //http://msdn.microsoft.com/en-us/library/cc195164.aspx
443 PaperInfo PaperInfo::getDefaultPaperForLocale(
444 const ::com::sun::star::lang::Locale & rLocale)
446 Paper eType = PAPER_A4;
448 if (
449 //United States, Letter
450 !rLocale.Country.compareToAscii("US") ||
451 //Puerto Rico:
452 // http://unicode.org/cldr/trac/ticket/1710
453 // http://sources.redhat.com/ml/libc-hacker/2001-07/msg00046.html
454 !rLocale.Country.compareToAscii("PR") ||
455 //Canada:
456 // http://sources.redhat.com/ml/libc-hacker/2001-07/msg00053.html
457 !rLocale.Country.compareToAscii("CA") ||
458 //Venuzuela:
459 // http://unicode.org/cldr/trac/ticket/1710
460 // https://www.redhat.com/archives/fedora-devel-list/2008-August/msg00019.html
461 !rLocale.Country.compareToAscii("VE") ||
462 //Chile:
463 // http://unicode.org/cldr/trac/ticket/1710
464 // https://www.redhat.com/archives/fedora-devel-list/2008-August/msg00240.html
465 !rLocale.Country.compareToAscii("CL") ||
466 //Mexico:
467 // http://unicode.org/cldr/trac/ticket/1710
468 // http://qa.openoffice.org/issues/show_bug.cgi?id=49739
469 !rLocale.Country.compareToAscii("MX") ||
470 //Colombia:
471 // http://unicode.org/cldr/trac/ticket/1710
472 // http://qa.openoffice.org/issues/show_bug.cgi?id=69703
473 !rLocale.Country.compareToAscii("CO") ||
474 //Philippines:
475 // http://unicode.org/cldr/trac/ticket/1710
476 // http://ubuntuliving.blogspot.com/2008/07/default-paper-size-in-evince.html
477 // http://www.gov.ph/faqs/driverslicense.asp
478 !rLocale.Country.compareToAscii("PH") ||
479 //Belize:
480 // http://unicode.org/cldr/trac/ticket/2585
481 // http://www.belize.gov.bz/ct.asp?xItem=1666&ctNode=486&mp=27
482 !rLocale.Country.compareToAscii("BZ") ||
483 //Costa Rica:
484 // http://unicode.org/cldr/trac/ticket/2585
485 // http://sources.redhat.com/bugzilla/show_bug.cgi?id=11258
486 !rLocale.Country.compareToAscii("CR") ||
487 //Guatemala:
488 // http://unicode.org/cldr/trac/ticket/2585
489 // http://sources.redhat.com/bugzilla/show_bug.cgi?id=10936
490 !rLocale.Country.compareToAscii("GT") ||
491 //Nicaragua:
492 // http://unicode.org/cldr/trac/ticket/2585
493 !rLocale.Country.compareToAscii("NI") ||
494 //Panama:
495 // http://unicode.org/cldr/trac/ticket/2585
496 // http://www.minsa.gob.pa/minsa/tl_files/documents/baner_informativo/INSTRUMENTO%20DE%20INVESTIGACION%20DE%20RAAV%202009.pdf
497 !rLocale.Country.compareToAscii("PA") ||
498 //El Salvador:
499 // http://unicode.org/cldr/trac/ticket/2585
500 // http://www.tse.gob.sv
501 !rLocale.Country.compareToAscii("SV")
504 eType = PAPER_LETTER;
507 return eType;
510 /* vi:set tabstop=4 shiftwidth=4 expandtab: */