Bump version to 5.0-14
[LibreOffice.git] / i18nutil / source / utility / paper.cxx
blob73993bffddf65429385620bbc3f39d2a8c699756
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <osl/diagnose.h>
22 #include <officecfg/Setup.hxx>
23 #include <officecfg/System.hxx>
24 #include <sal/config.h>
25 #include <sal/macros.h>
26 #include <rtl/ustring.hxx>
27 #include <rtl/string.hxx>
29 #include "i18nutil/paper.hxx"
31 #include <utility>
32 #include <cstdlib>
34 #ifdef UNX
35 #include <stdio.h>
36 #include <string.h>
37 #include <locale.h>
38 #if defined(LC_PAPER) && defined(_GNU_SOURCE)
39 #include <langinfo.h>
40 #endif
41 #endif
43 struct PageDesc
45 long m_nWidth;
46 long m_nHeight;
47 const char *m_pPSName;
48 const char *m_pAltPSName;
51 #define PT2MM100( v ) \
52 (long)(((v) * 35.27777778) + 0.5)
54 #define IN2MM100( v ) \
55 ((long)(((v) * 2540) + 0.5))
57 #define MM2MM100( v ) \
58 ((long)((v) * 100))
60 //PostScript Printer Description File Format Specification
61 //http://partners.adobe.com/public/developer/en/ps/5003.PPD_Spec_v4.3.pdf
62 //http://www.y-adagio.com/public/committees/docsii/doc_00-49/symp_ulaan/china_ppr.pdf (Kai)
63 //http://www.sls.psi.ch/controls/help/howto/Howto_Print_a_A0_Poster_at_WSLA_012_2.pdf (Dia)
64 static const PageDesc aDinTab[] =
66 { MM2MM100( 841 ), MM2MM100( 1189 ), "A0", NULL },
67 { MM2MM100( 594 ), MM2MM100( 841 ), "A1", NULL },
68 { MM2MM100( 420 ), MM2MM100( 594 ), "A2", NULL },
69 { MM2MM100( 297 ), MM2MM100( 420 ), "A3", NULL },
70 { MM2MM100( 210 ), MM2MM100( 297 ), "A4", NULL },
71 { MM2MM100( 148 ), MM2MM100( 210 ), "A5", NULL },
72 { MM2MM100( 250 ), MM2MM100( 353 ), "ISOB4", NULL },
73 { MM2MM100( 176 ), MM2MM100( 250 ), "ISOB5", NULL },
74 { IN2MM100( 8.5 ), IN2MM100( 11 ), "Letter", "Note" },
75 { IN2MM100( 8.5 ), IN2MM100( 14 ), "Legal", NULL },
76 { IN2MM100( 11 ), IN2MM100( 17 ), "Tabloid", "11x17" },
77 { 0, 0, NULL, NULL }, //User
78 { MM2MM100( 125 ), MM2MM100( 176 ), "ISOB6", NULL },
79 { MM2MM100( 229 ), MM2MM100( 324 ), "EnvC4", "C4" },
80 { MM2MM100( 162 ), MM2MM100( 229 ), "EnvC5", "C5" },
81 { MM2MM100( 114 ), MM2MM100( 162 ), "EnvC6", "C6" },
82 { MM2MM100( 114 ), MM2MM100( 229 ), "EnvC65", NULL },
83 { MM2MM100( 110 ), MM2MM100( 220 ), "EnvDL", "DL" },
84 { MM2MM100( 180), MM2MM100( 270 ), NULL, NULL }, //Dia
85 { MM2MM100( 210), MM2MM100( 280 ), NULL, NULL }, //Screen 4:3
86 { IN2MM100( 17 ), IN2MM100( 22 ), "AnsiC", "CSheet" },
87 { IN2MM100( 22 ), IN2MM100( 34 ), "AnsiD", "DSheet" },
88 { IN2MM100( 34 ), IN2MM100( 44 ), "AnsiE", "ESheet" },
89 { IN2MM100( 7.25 ), IN2MM100( 10.5 ), "Executive", NULL },
90 //"Folio" is a different size in the PPD documentation than 8.5x11
91 //This "FanFoldGermanLegal" is known in the Philippines as
92 //"Legal" paper or "Long Bond Paper". The "Legal" name causing untold
93 //misery, given the differently sized US "Legal" paper
94 { IN2MM100( 8.5 ), IN2MM100( 13 ), "FanFoldGermanLegal", NULL },
95 { IN2MM100( 3.875 ), IN2MM100( 7.5 ), "EnvMonarch", "Monarch" },
96 { IN2MM100( 3.625 ), IN2MM100( 6.5 ), "EnvPersonal", "Personal" },
97 { IN2MM100( 3.875 ), IN2MM100( 8.875 ), "Env9", NULL },
98 { IN2MM100( 4.125 ), IN2MM100( 9.5 ), "Env10", "Comm10" },
99 { IN2MM100( 4.5 ), IN2MM100( 10.375 ), "Env11", NULL },
100 { IN2MM100( 4.75 ), IN2MM100( 11 ), "Env12", NULL },
101 { MM2MM100( 184 ), MM2MM100( 260 ), NULL, NULL }, //Kai16
102 { MM2MM100( 130 ), MM2MM100( 184 ), NULL, NULL }, //Kai32
103 { MM2MM100( 140 ), MM2MM100( 203 ), NULL, NULL }, //BigKai32
104 { MM2MM100( 257 ), MM2MM100( 364 ), "B4", NULL }, //JIS
105 { MM2MM100( 182 ), MM2MM100( 257 ), "B5", NULL }, //JIS
106 { MM2MM100( 128 ), MM2MM100( 182 ), "B6", NULL }, //JIS
107 { IN2MM100( 17 ), IN2MM100( 11 ), "Ledger", NULL },
108 { IN2MM100( 5.5 ), IN2MM100( 8.5 ), "Statement", NULL },
109 { PT2MM100( 610 ), PT2MM100( 780 ), "Quarto", NULL },
110 { IN2MM100( 10 ), IN2MM100( 14 ), "10x14", NULL },
111 { IN2MM100( 5.5 ), IN2MM100( 11.5 ), "Env14", NULL },
112 { MM2MM100( 324 ), MM2MM100( 458 ), "EnvC3", "C3" },
113 { MM2MM100( 110 ), MM2MM100( 230 ), "EnvItalian", NULL },
114 { IN2MM100( 14.875 ),IN2MM100( 11 ), "FanFoldUS", NULL },
115 { IN2MM100( 8.5 ), IN2MM100( 13 ), "FanFoldGerman", NULL },
116 { MM2MM100( 100 ), MM2MM100( 148 ), "Postcard", NULL },
117 { IN2MM100( 9 ), IN2MM100( 11 ), "9x11", NULL },
118 { IN2MM100( 10 ), IN2MM100( 11 ), "10x11", NULL },
119 { IN2MM100( 15 ), IN2MM100( 11 ), "15x11", NULL },
120 { MM2MM100( 220 ), MM2MM100( 220 ), "EnvInvite", NULL },
121 { MM2MM100( 227 ), MM2MM100( 356 ), "SuperA", NULL },
122 { MM2MM100( 305 ), MM2MM100( 487 ), "SuperB", NULL },
123 { IN2MM100( 8.5 ), IN2MM100( 12.69 ), "LetterPlus", NULL },
124 { MM2MM100( 210 ), MM2MM100( 330 ), "A4Plus", NULL },
125 { MM2MM100( 200 ), MM2MM100( 148 ), "DoublePostcard", NULL },
126 { MM2MM100( 105 ), MM2MM100( 148 ), "A6", NULL },
127 { IN2MM100( 12 ), IN2MM100( 11 ), "12x11", NULL },
128 { MM2MM100( 74 ), MM2MM100( 105 ), "A7", NULL },
129 { MM2MM100( 52 ), MM2MM100( 74 ), "A8", NULL },
130 { MM2MM100( 37 ), MM2MM100( 52 ), "A9", NULL },
131 { MM2MM100( 26 ), MM2MM100( 37 ), "A10", NULL },
132 { MM2MM100( 1000 ), MM2MM100( 1414 ), "ISOB0", NULL },
133 { MM2MM100( 707 ), MM2MM100( 1000 ), "ISOB1", NULL },
134 { MM2MM100( 500 ), MM2MM100( 707 ), "ISOB2", NULL },
135 { MM2MM100( 353 ), MM2MM100( 500 ), "ISOB3", NULL },
136 { MM2MM100( 88 ), MM2MM100( 125 ), "ISOB7", NULL },
137 { MM2MM100( 62 ), MM2MM100( 88 ), "ISOB8", NULL },
138 { MM2MM100( 44 ), MM2MM100( 62 ), "ISOB9", NULL },
139 { MM2MM100( 31 ), MM2MM100( 44 ), "ISOB10", NULL },
140 { MM2MM100( 458 ), MM2MM100( 648 ), "EnvC2", "C2" },
141 { MM2MM100( 81 ), MM2MM100( 114 ), "EnvC7", "C7" },
142 { MM2MM100( 57 ), MM2MM100( 81 ), "EnvC8", "C8" },
143 { IN2MM100( 9 ), IN2MM100( 12 ), "ARCHA", NULL },
144 { IN2MM100( 12 ), IN2MM100( 18 ), "ARCHB", NULL },
145 { IN2MM100( 18 ), IN2MM100( 24 ), "ARCHC", NULL },
146 { IN2MM100( 24 ), IN2MM100( 36 ), "ARCHD", NULL },
147 { IN2MM100( 36 ), IN2MM100( 48 ), "ARCHE", NULL },
148 { MM2MM100( 157.5), MM2MM100( 280 ), NULL, NULL }, //Screen 16:9
149 { MM2MM100( 175 ), MM2MM100( 280 ), NULL, NULL } //Screen 16:10
153 static const size_t nTabSize = SAL_N_ELEMENTS(aDinTab);
155 #define MAXSLOPPY 21
157 bool PaperInfo::doSloppyFit()
159 if (m_eType != PAPER_USER)
160 return true;
162 for ( size_t i = 0; i < nTabSize; ++i )
164 if (i == PAPER_USER) continue;
166 long lDiffW = labs(aDinTab[i].m_nWidth - m_nPaperWidth);
167 long lDiffH = labs(aDinTab[i].m_nHeight - m_nPaperHeight);
169 if ( lDiffW < MAXSLOPPY && lDiffH < MAXSLOPPY )
171 m_nPaperWidth = aDinTab[i].m_nWidth;
172 m_nPaperHeight = aDinTab[i].m_nHeight;
173 m_eType = (Paper)i;
174 return true;
178 return false;
181 bool PaperInfo::sloppyEqual(const PaperInfo &rOther) const
183 return
185 (labs(m_nPaperWidth - rOther.m_nPaperWidth) < MAXSLOPPY) &&
186 (labs(m_nPaperHeight - rOther.m_nPaperHeight) < MAXSLOPPY)
190 long PaperInfo::sloppyFitPageDimension(long nDimension)
192 for ( size_t i = 0; i < nTabSize; ++i )
194 if (i == PAPER_USER) continue;
195 long lDiff;
197 lDiff = labs(aDinTab[i].m_nWidth - nDimension);
198 if ( lDiff < MAXSLOPPY )
199 return aDinTab[i].m_nWidth;
201 lDiff = labs(aDinTab[i].m_nHeight - nDimension);
202 if ( lDiff < MAXSLOPPY )
203 return aDinTab[i].m_nHeight;
205 return nDimension;
208 PaperInfo PaperInfo::getSystemDefaultPaper()
210 OUString aLocaleStr = officecfg::Setup::L10N::ooSetupSystemLocale::get();
212 #ifdef UNX
213 // if set to "use system", get papersize from system
214 if (aLocaleStr.isEmpty())
216 static bool bInitialized = false;
217 static PaperInfo aInstance(PAPER_A4);
219 if (bInitialized)
220 return aInstance;
222 // try libpaper
223 // #i78617# workaround missing paperconf command
224 FILE* pPipe = popen( "paperconf 2>/dev/null", "r" );
225 if( pPipe )
227 Paper ePaper = PAPER_USER;
229 char aBuffer[ 1024 ];
230 aBuffer[0] = 0;
231 char *pBuffer = fgets( aBuffer, sizeof(aBuffer), pPipe );
232 pclose( pPipe );
234 if (pBuffer && *pBuffer != 0)
236 OString aPaper(pBuffer);
237 aPaper = aPaper.trim();
238 static const struct { const char *pName; Paper ePaper; } aCustoms [] =
240 { "B0", PAPER_B0_ISO },
241 { "B1", PAPER_B1_ISO },
242 { "B2", PAPER_B2_ISO },
243 { "B3", PAPER_B3_ISO },
244 { "B4", PAPER_B4_ISO },
245 { "B5", PAPER_B5_ISO },
246 { "B6", PAPER_B6_ISO },
247 { "B7", PAPER_B7_ISO },
248 { "B8", PAPER_B8_ISO },
249 { "B9", PAPER_B9_ISO },
250 { "B10", PAPER_B10_ISO },
251 { "folio", PAPER_FANFOLD_LEGAL_DE },
252 { "flsa", PAPER_FANFOLD_LEGAL_DE },
253 { "flse", PAPER_FANFOLD_LEGAL_DE }
256 bool bHalve = false;
258 size_t nExtraTabSize = SAL_N_ELEMENTS(aCustoms);
259 for (size_t i = 0; i < nExtraTabSize; ++i)
261 if (rtl_str_compareIgnoreAsciiCase(aCustoms[i].pName, aPaper.getStr()) == 0)
263 ePaper = aCustoms[i].ePaper;
264 break;
268 if (ePaper == PAPER_USER)
270 bHalve = !rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
271 aPaper.getStr(), aPaper.getLength(), "half", 4, 4);
272 if (bHalve)
273 aPaper = aPaper.copy(4);
274 ePaper = PaperInfo::fromPSName(aPaper);
277 if (ePaper != PAPER_USER)
279 aInstance = PaperInfo(ePaper);
280 if (bHalve)
281 aInstance = PaperInfo(aInstance.getHeight()/2, aInstance.getWidth());
282 bInitialized = true;
283 return aInstance;
288 #if defined(LC_PAPER) && defined(_GNU_SOURCE)
290 union paperword { char *string; int word; };
292 // try LC_PAPER
293 paperword w, h;
294 w.string = nl_langinfo(_NL_PAPER_WIDTH);
295 h.string = nl_langinfo(_NL_PAPER_HEIGHT);
297 //glibc stores sizes as integer mm units
298 w.word *= 100;
299 h.word *= 100;
301 for ( size_t i = 0; i < nTabSize; ++i )
303 if (i == PAPER_USER) continue;
305 //glibc stores sizes as integer mm units, and so is inaccurate. To
306 //find a standard paper size we calculate the standard paper sizes
307 //into equally inaccurate mm and compare
308 long width = (aDinTab[i].m_nWidth + 50) / 100;
309 long height = (aDinTab[i].m_nHeight + 50) / 100;
311 if (width == w.word/100 && height == h.word/100)
313 w.word = aDinTab[i].m_nWidth;
314 h.word = aDinTab[i].m_nHeight;
315 break;
319 aInstance = PaperInfo(w.word, h.word);
320 bInitialized = true;
321 return aInstance;
322 #endif
324 #endif
326 // if set to "use system", try to get locale from system
327 if (aLocaleStr.isEmpty())
328 aLocaleStr = officecfg::System::L10N::Locale::get();
330 if (aLocaleStr.isEmpty())
331 aLocaleStr = OUString::intern(RTL_CONSTASCII_USTRINGPARAM("en-US"));
333 // convert locale string to locale struct
334 ::com::sun::star::lang::Locale aSysLocale;
335 sal_Int32 nDashPos = aLocaleStr.indexOf( '-' );
336 if( nDashPos < 0 ) nDashPos = aLocaleStr.getLength();
337 aSysLocale.Language = aLocaleStr.copy( 0, nDashPos );
338 if( nDashPos + 1 < aLocaleStr.getLength() )
339 aSysLocale.Country = aLocaleStr.copy( nDashPos + 1 );
341 return PaperInfo::getDefaultPaperForLocale(aSysLocale);
344 PaperInfo::PaperInfo(Paper eType) : m_eType(eType)
346 OSL_ENSURE( sizeof(aDinTab) / sizeof(aDinTab[0]) == NUM_PAPER_ENTRIES,
347 "mismatch between array entries and enum values" );
349 m_nPaperWidth = aDinTab[m_eType].m_nWidth;
350 m_nPaperHeight = aDinTab[m_eType].m_nHeight;
353 PaperInfo::PaperInfo(long nPaperWidth, long nPaperHeight)
354 : m_eType(PAPER_USER),
355 m_nPaperWidth(nPaperWidth),
356 m_nPaperHeight(nPaperHeight)
358 for ( size_t i = 0; i < nTabSize; ++i )
360 if (
361 (nPaperWidth == aDinTab[i].m_nWidth) &&
362 (nPaperHeight == aDinTab[i].m_nHeight)
365 m_eType = static_cast<Paper>(i);
366 break;
371 OString PaperInfo::toPSName(Paper ePaper)
373 return static_cast<size_t>(ePaper) < nTabSize ?
374 OString(aDinTab[ePaper].m_pPSName) : OString();
377 Paper PaperInfo::fromPSName(const OString &rName)
379 if (rName.isEmpty())
380 return PAPER_USER;
382 for ( size_t i = 0; i < nTabSize; ++i )
384 if (aDinTab[i].m_pPSName &&
385 !rtl_str_compareIgnoreAsciiCase(aDinTab[i].m_pPSName, rName.getStr()))
387 return static_cast<Paper>(i);
389 else if (aDinTab[i].m_pAltPSName &&
390 !rtl_str_compareIgnoreAsciiCase(aDinTab[i].m_pAltPSName, rName.getStr()))
392 return static_cast<Paper>(i);
396 return PAPER_USER;
399 //http://wiki.openoffice.org/wiki/DefaultPaperSize
400 //http://www.unicode.org/cldr/data/charts/supplemental/territory_language_information.html
401 //http://sourceware.org/git/?p=glibc.git;a=tree;f=localedata/locales
402 //http://en.wikipedia.org/wiki/Paper_size
403 //http://msdn.microsoft.com/en-us/library/cc195164.aspx
404 PaperInfo PaperInfo::getDefaultPaperForLocale(
405 const ::com::sun::star::lang::Locale & rLocale)
407 Paper eType = PAPER_A4;
409 if (
410 //United States, Letter
411 rLocale.Country == "US" ||
412 //Puerto Rico:
413 // http://unicode.org/cldr/trac/ticket/1710
414 // http://sources.redhat.com/ml/libc-hacker/2001-07/msg00046.html
415 rLocale.Country == "PR" ||
416 //Canada:
417 // http://sources.redhat.com/ml/libc-hacker/2001-07/msg00053.html
418 rLocale.Country == "CA" ||
419 //Venuzuela:
420 // http://unicode.org/cldr/trac/ticket/1710
421 // https://www.redhat.com/archives/fedora-devel-list/2008-August/msg00019.html
422 rLocale.Country == "VE" ||
423 //Chile:
424 // http://unicode.org/cldr/trac/ticket/1710
425 // https://www.redhat.com/archives/fedora-devel-list/2008-August/msg00240.html
426 rLocale.Country == "CL" ||
427 //Mexico:
428 // http://unicode.org/cldr/trac/ticket/1710
429 // http://qa.openoffice.org/issues/show_bug.cgi?id=49739
430 rLocale.Country == "MX" ||
431 //Colombia:
432 // http://unicode.org/cldr/trac/ticket/1710
433 // http://qa.openoffice.org/issues/show_bug.cgi?id=69703
434 rLocale.Country == "CO" ||
435 //Philippines:
436 // http://unicode.org/cldr/trac/ticket/1710
437 // http://ubuntuliving.blogspot.com/2008/07/default-paper-size-in-evince.html
438 // http://www.gov.ph/faqs/driverslicense.asp
439 rLocale.Country == "PH" ||
440 //Belize:
441 // http://unicode.org/cldr/trac/ticket/2585
442 // http://www.belize.gov.bz/ct.asp?xItem=1666&ctNode=486&mp=27
443 rLocale.Country == "BZ" ||
444 //Costa Rica:
445 // http://unicode.org/cldr/trac/ticket/2585
446 // http://sources.redhat.com/bugzilla/show_bug.cgi?id=11258
447 rLocale.Country == "CR" ||
448 //Guatemala:
449 // http://unicode.org/cldr/trac/ticket/2585
450 // http://sources.redhat.com/bugzilla/show_bug.cgi?id=10936
451 rLocale.Country == "GT" ||
452 //Nicaragua:
453 // http://unicode.org/cldr/trac/ticket/2585
454 rLocale.Country == "NI" ||
455 //Panama:
456 // http://unicode.org/cldr/trac/ticket/2585
457 // http://www.minsa.gob.pa/minsa/tl_files/documents/baner_informativo/INSTRUMENTO%20DE%20INVESTIGACION%20DE%20RAAV%202009.pdf
458 rLocale.Country == "PA" ||
459 //El Salvador:
460 // http://unicode.org/cldr/trac/ticket/2585
461 // http://www.tse.gob.sv
462 rLocale.Country == "SV"
465 eType = PAPER_LETTER;
468 return eType;
471 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */