1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
33 #include <unotools/configmgr.hxx>
39 #if defined(LC_PAPER) && defined(_GNU_SOURCE)
48 const char *m_pPSName
;
49 const char *m_pAltPSName
;
52 #define PT2MM100( v ) \
53 (long)(((v) * 35.27777778) + 0.5)
55 #define IN2MM100( v ) \
56 ((long)(((v) * 2540) + 0.5))
58 #define MM2MM100( v ) \
61 //PostScript Printer Description File Format Specification
62 //http://partners.adobe.com/public/developer/en/ps/5003.PPD_Spec_v4.3.pdf
63 //https://web.archive.org/web/20040912070512/http://www.y-adagio.com/public/committees/docsii/doc_00-49/symp_ulaan/china_ppr.pdf (Kai)
64 //http://www.sls.psi.ch/controls/help/howto/Howto_Print_a_A0_Poster_at_WSLA_012_2.pdf (Dia)
65 static const PageDesc aDinTab
[] =
67 { MM2MM100( 841 ), MM2MM100( 1189 ), "A0", nullptr },
68 { MM2MM100( 594 ), MM2MM100( 841 ), "A1", nullptr },
69 { MM2MM100( 420 ), MM2MM100( 594 ), "A2", nullptr },
70 { MM2MM100( 297 ), MM2MM100( 420 ), "A3", nullptr },
71 { MM2MM100( 210 ), MM2MM100( 297 ), "A4", nullptr },
72 { MM2MM100( 148 ), MM2MM100( 210 ), "A5", nullptr },
73 { MM2MM100( 250 ), MM2MM100( 353 ), "ISOB4", nullptr },
74 { MM2MM100( 176 ), MM2MM100( 250 ), "ISOB5", nullptr },
75 { IN2MM100( 8.5 ), IN2MM100( 11 ), "Letter", "Note" },
76 { IN2MM100( 8.5 ), IN2MM100( 14 ), "Legal", nullptr },
77 { IN2MM100( 11 ), IN2MM100( 17 ), "Tabloid", "11x17" },
78 { 0, 0, nullptr, nullptr }, //User
79 { MM2MM100( 125 ), MM2MM100( 176 ), "ISOB6", nullptr },
80 { MM2MM100( 229 ), MM2MM100( 324 ), "EnvC4", "C4" },
81 { MM2MM100( 162 ), MM2MM100( 229 ), "EnvC5", "C5" },
82 { MM2MM100( 114 ), MM2MM100( 162 ), "EnvC6", "C6" },
83 { MM2MM100( 114 ), MM2MM100( 229 ), "EnvC65", nullptr },
84 { MM2MM100( 110 ), MM2MM100( 220 ), "EnvDL", "DL" },
85 { MM2MM100( 180), MM2MM100( 270 ), nullptr, nullptr }, //Dia
86 { MM2MM100( 210), MM2MM100( 280 ), nullptr, nullptr }, //Screen 4:3
87 { IN2MM100( 17 ), IN2MM100( 22 ), "AnsiC", "CSheet" },
88 { IN2MM100( 22 ), IN2MM100( 34 ), "AnsiD", "DSheet" },
89 { IN2MM100( 34 ), IN2MM100( 44 ), "AnsiE", "ESheet" },
90 { IN2MM100( 7.25 ), IN2MM100( 10.5 ), "Executive", nullptr },
91 //"Folio" is a different size in the PPD documentation than 8.5x11
92 //This "FanFoldGermanLegal" is known in the Philippines as
93 //"Legal" paper or "Long Bond Paper". The "Legal" name causing untold
94 //misery, given the differently sized US "Legal" paper
95 { IN2MM100( 8.5 ), IN2MM100( 13 ), "FanFoldGermanLegal", nullptr },
96 { IN2MM100( 3.875 ), IN2MM100( 7.5 ), "EnvMonarch", "Monarch" },
97 { IN2MM100( 3.625 ), IN2MM100( 6.5 ), "EnvPersonal", "Personal" },
98 { IN2MM100( 3.875 ), IN2MM100( 8.875 ), "Env9", nullptr },
99 { IN2MM100( 4.125 ), IN2MM100( 9.5 ), "Env10", "Comm10" },
100 { IN2MM100( 4.5 ), IN2MM100( 10.375 ), "Env11", nullptr },
101 { IN2MM100( 4.75 ), IN2MM100( 11 ), "Env12", nullptr },
102 { MM2MM100( 184 ), MM2MM100( 260 ), nullptr, nullptr }, //Kai16
103 { MM2MM100( 130 ), MM2MM100( 184 ), nullptr, nullptr }, //Kai32
104 { MM2MM100( 140 ), MM2MM100( 203 ), nullptr, nullptr }, //BigKai32
105 { MM2MM100( 257 ), MM2MM100( 364 ), "B4", nullptr }, //JIS
106 { MM2MM100( 182 ), MM2MM100( 257 ), "B5", nullptr }, //JIS
107 { MM2MM100( 128 ), MM2MM100( 182 ), "B6", nullptr }, //JIS
108 { IN2MM100( 17 ), IN2MM100( 11 ), "Ledger", nullptr },
109 { IN2MM100( 5.5 ), IN2MM100( 8.5 ), "Statement", nullptr },
110 { PT2MM100( 610 ), PT2MM100( 780 ), "Quarto", nullptr },
111 { IN2MM100( 10 ), IN2MM100( 14 ), "10x14", nullptr },
112 { IN2MM100( 5.5 ), IN2MM100( 11.5 ), "Env14", nullptr },
113 { MM2MM100( 324 ), MM2MM100( 458 ), "EnvC3", "C3" },
114 { MM2MM100( 110 ), MM2MM100( 230 ), "EnvItalian", nullptr },
115 { IN2MM100( 14.875 ),IN2MM100( 11 ), "FanFoldUS", nullptr },
116 { IN2MM100( 8.5 ), IN2MM100( 13 ), "FanFoldGerman", nullptr },
117 { MM2MM100( 100 ), MM2MM100( 148 ), "Postcard", nullptr },
118 { IN2MM100( 9 ), IN2MM100( 11 ), "9x11", nullptr },
119 { IN2MM100( 10 ), IN2MM100( 11 ), "10x11", nullptr },
120 { IN2MM100( 15 ), IN2MM100( 11 ), "15x11", nullptr },
121 { MM2MM100( 220 ), MM2MM100( 220 ), "EnvInvite", nullptr },
122 { MM2MM100( 227 ), MM2MM100( 356 ), "SuperA", nullptr },
123 { MM2MM100( 305 ), MM2MM100( 487 ), "SuperB", nullptr },
124 { IN2MM100( 8.5 ), IN2MM100( 12.69 ), "LetterPlus", nullptr },
125 { MM2MM100( 210 ), MM2MM100( 330 ), "A4Plus", nullptr },
126 { MM2MM100( 200 ), MM2MM100( 148 ), "DoublePostcard", nullptr },
127 { MM2MM100( 105 ), MM2MM100( 148 ), "A6", nullptr },
128 { IN2MM100( 12 ), IN2MM100( 11 ), "12x11", nullptr },
129 { MM2MM100( 74 ), MM2MM100( 105 ), "A7", nullptr },
130 { MM2MM100( 52 ), MM2MM100( 74 ), "A8", nullptr },
131 { MM2MM100( 37 ), MM2MM100( 52 ), "A9", nullptr },
132 { MM2MM100( 26 ), MM2MM100( 37 ), "A10", nullptr },
133 { MM2MM100( 1000 ), MM2MM100( 1414 ), "ISOB0", nullptr },
134 { MM2MM100( 707 ), MM2MM100( 1000 ), "ISOB1", nullptr },
135 { MM2MM100( 500 ), MM2MM100( 707 ), "ISOB2", nullptr },
136 { MM2MM100( 353 ), MM2MM100( 500 ), "ISOB3", nullptr },
137 { MM2MM100( 88 ), MM2MM100( 125 ), "ISOB7", nullptr },
138 { MM2MM100( 62 ), MM2MM100( 88 ), "ISOB8", nullptr },
139 { MM2MM100( 44 ), MM2MM100( 62 ), "ISOB9", nullptr },
140 { MM2MM100( 31 ), MM2MM100( 44 ), "ISOB10", nullptr },
141 { MM2MM100( 458 ), MM2MM100( 648 ), "EnvC2", "C2" },
142 { MM2MM100( 81 ), MM2MM100( 114 ), "EnvC7", "C7" },
143 { MM2MM100( 57 ), MM2MM100( 81 ), "EnvC8", "C8" },
144 { IN2MM100( 9 ), IN2MM100( 12 ), "ARCHA", nullptr },
145 { IN2MM100( 12 ), IN2MM100( 18 ), "ARCHB", nullptr },
146 { IN2MM100( 18 ), IN2MM100( 24 ), "ARCHC", nullptr },
147 { IN2MM100( 24 ), IN2MM100( 36 ), "ARCHD", nullptr },
148 { IN2MM100( 36 ), IN2MM100( 48 ), "ARCHE", nullptr },
149 { MM2MM100( 157.5), MM2MM100( 280 ), nullptr, nullptr }, //Screen 16:9
150 { MM2MM100( 175 ), MM2MM100( 280 ), nullptr, nullptr } //Screen 16:10
154 static const size_t nTabSize
= SAL_N_ELEMENTS(aDinTab
);
158 bool PaperInfo::doSloppyFit()
160 if (m_eType
!= PAPER_USER
)
163 for ( size_t i
= 0; i
< nTabSize
; ++i
)
165 if (i
== PAPER_USER
) continue;
167 long lDiffW
= labs(aDinTab
[i
].m_nWidth
- m_nPaperWidth
);
168 long lDiffH
= labs(aDinTab
[i
].m_nHeight
- m_nPaperHeight
);
170 if ( lDiffW
< MAXSLOPPY
&& lDiffH
< MAXSLOPPY
)
172 m_nPaperWidth
= aDinTab
[i
].m_nWidth
;
173 m_nPaperHeight
= aDinTab
[i
].m_nHeight
;
182 bool PaperInfo::sloppyEqual(const PaperInfo
&rOther
) const
186 (labs(m_nPaperWidth
- rOther
.m_nPaperWidth
) < MAXSLOPPY
) &&
187 (labs(m_nPaperHeight
- rOther
.m_nPaperHeight
) < MAXSLOPPY
)
191 long PaperInfo::sloppyFitPageDimension(long nDimension
)
193 for ( size_t i
= 0; i
< nTabSize
; ++i
)
195 if (i
== PAPER_USER
) continue;
198 lDiff
= labs(aDinTab
[i
].m_nWidth
- nDimension
);
199 if ( lDiff
< MAXSLOPPY
)
200 return aDinTab
[i
].m_nWidth
;
202 lDiff
= labs(aDinTab
[i
].m_nHeight
- nDimension
);
203 if ( lDiff
< MAXSLOPPY
)
204 return aDinTab
[i
].m_nHeight
;
209 PaperInfo
PaperInfo::getSystemDefaultPaper()
211 if (utl::ConfigManager::IsAvoidConfig())
212 return PaperInfo(PAPER_A4
);
214 OUString aLocaleStr
= officecfg::Setup::L10N::ooSetupSystemLocale::get();
217 // if set to "use system", get papersize from system
218 if (aLocaleStr
.isEmpty())
220 static bool bInitialized
= false;
221 static PaperInfo
aInstance(PAPER_A4
);
227 // #i78617# workaround missing paperconf command
228 FILE* pPipe
= popen( "paperconf 2>/dev/null", "r" );
231 Paper ePaper
= PAPER_USER
;
233 char aBuffer
[ 1024 ];
235 char *pBuffer
= fgets( aBuffer
, sizeof(aBuffer
), pPipe
);
236 bool bOk
= pclose(pPipe
) == 0;
238 if (bOk
&& pBuffer
&& *pBuffer
!= 0)
240 OString
aPaper(pBuffer
);
241 aPaper
= aPaper
.trim();
242 static const struct { const char *pName
; Paper ePaper
; } aCustoms
[] =
244 { "B0", PAPER_B0_ISO
},
245 { "B1", PAPER_B1_ISO
},
246 { "B2", PAPER_B2_ISO
},
247 { "B3", PAPER_B3_ISO
},
248 { "B4", PAPER_B4_ISO
},
249 { "B5", PAPER_B5_ISO
},
250 { "B6", PAPER_B6_ISO
},
251 { "B7", PAPER_B7_ISO
},
252 { "B8", PAPER_B8_ISO
},
253 { "B9", PAPER_B9_ISO
},
254 { "B10", PAPER_B10_ISO
},
255 { "folio", PAPER_FANFOLD_LEGAL_DE
},
256 { "flsa", PAPER_FANFOLD_LEGAL_DE
},
257 { "flse", PAPER_FANFOLD_LEGAL_DE
}
262 size_t nExtraTabSize
= SAL_N_ELEMENTS(aCustoms
);
263 for (size_t i
= 0; i
< nExtraTabSize
; ++i
)
265 if (rtl_str_compareIgnoreAsciiCase(aCustoms
[i
].pName
, aPaper
.getStr()) == 0)
267 ePaper
= aCustoms
[i
].ePaper
;
272 if (ePaper
== PAPER_USER
)
274 bHalve
= !rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
275 aPaper
.getStr(), aPaper
.getLength(), "half", 4, 4);
277 aPaper
= aPaper
.copy(4);
278 ePaper
= PaperInfo::fromPSName(aPaper
);
281 if (ePaper
!= PAPER_USER
)
283 aInstance
= PaperInfo(ePaper
);
285 aInstance
= PaperInfo(aInstance
.getHeight()/2, aInstance
.getWidth());
292 #if defined(LC_PAPER) && defined(_GNU_SOURCE)
294 union paperword
{ char *string
; int word
; };
298 w
.string
= nl_langinfo(_NL_PAPER_WIDTH
);
299 h
.string
= nl_langinfo(_NL_PAPER_HEIGHT
);
301 //glibc stores sizes as integer mm units
305 for ( size_t i
= 0; i
< nTabSize
; ++i
)
307 if (i
== PAPER_USER
) continue;
309 //glibc stores sizes as integer mm units, and so is inaccurate. To
310 //find a standard paper size we calculate the standard paper sizes
311 //into equally inaccurate mm and compare
312 long width
= (aDinTab
[i
].m_nWidth
+ 50) / 100;
313 long height
= (aDinTab
[i
].m_nHeight
+ 50) / 100;
315 if (width
== w
.word
/100 && height
== h
.word
/100)
317 w
.word
= aDinTab
[i
].m_nWidth
;
318 h
.word
= aDinTab
[i
].m_nHeight
;
323 aInstance
= PaperInfo(w
.word
, h
.word
);
330 // if set to "use system", try to get locale from system
331 if (aLocaleStr
.isEmpty())
332 aLocaleStr
= officecfg::System::L10N::Locale::get();
334 if (aLocaleStr
.isEmpty())
335 aLocaleStr
= OUString::intern(RTL_CONSTASCII_USTRINGPARAM("en-US"));
337 // convert locale string to locale struct
338 css::lang::Locale aSysLocale
;
339 sal_Int32 nDashPos
= aLocaleStr
.indexOf( '-' );
340 if( nDashPos
< 0 ) nDashPos
= aLocaleStr
.getLength();
341 aSysLocale
.Language
= aLocaleStr
.copy( 0, nDashPos
);
342 if( nDashPos
+ 1 < aLocaleStr
.getLength() )
343 aSysLocale
.Country
= aLocaleStr
.copy( nDashPos
+ 1 );
345 return PaperInfo::getDefaultPaperForLocale(aSysLocale
);
348 PaperInfo::PaperInfo(Paper eType
) : m_eType(eType
)
350 OSL_ENSURE( SAL_N_ELEMENTS(aDinTab
) == NUM_PAPER_ENTRIES
,
351 "mismatch between array entries and enum values" );
353 m_nPaperWidth
= aDinTab
[m_eType
].m_nWidth
;
354 m_nPaperHeight
= aDinTab
[m_eType
].m_nHeight
;
357 PaperInfo::PaperInfo(long nPaperWidth
, long nPaperHeight
)
358 : m_eType(PAPER_USER
),
359 m_nPaperWidth(nPaperWidth
),
360 m_nPaperHeight(nPaperHeight
)
362 for ( size_t i
= 0; i
< nTabSize
; ++i
)
365 (nPaperWidth
== aDinTab
[i
].m_nWidth
) &&
366 (nPaperHeight
== aDinTab
[i
].m_nHeight
)
369 m_eType
= static_cast<Paper
>(i
);
375 OString
PaperInfo::toPSName(Paper ePaper
)
377 return static_cast<size_t>(ePaper
) < nTabSize
?
378 OString(aDinTab
[ePaper
].m_pPSName
) : OString();
381 Paper
PaperInfo::fromPSName(const OString
&rName
)
386 for ( size_t i
= 0; i
< nTabSize
; ++i
)
388 if (aDinTab
[i
].m_pPSName
&&
389 !rtl_str_compareIgnoreAsciiCase(aDinTab
[i
].m_pPSName
, rName
.getStr()))
391 return static_cast<Paper
>(i
);
393 else if (aDinTab
[i
].m_pAltPSName
&&
394 !rtl_str_compareIgnoreAsciiCase(aDinTab
[i
].m_pAltPSName
, rName
.getStr()))
396 return static_cast<Paper
>(i
);
403 //http://wiki.openoffice.org/wiki/DefaultPaperSize
404 //http://www.unicode.org/cldr/data/charts/supplemental/territory_language_information.html
405 //http://sourceware.org/git/?p=glibc.git;a=tree;f=localedata/locales
406 //http://en.wikipedia.org/wiki/Paper_size
407 //http://msdn.microsoft.com/en-us/library/cc195164.aspx
408 PaperInfo
PaperInfo::getDefaultPaperForLocale( const css::lang::Locale
& rLocale
)
410 Paper eType
= PAPER_A4
;
413 //United States, Letter
414 rLocale
.Country
== "US" ||
416 // http://unicode.org/cldr/trac/ticket/1710
417 // http://sources.redhat.com/ml/libc-hacker/2001-07/msg00046.html
418 rLocale
.Country
== "PR" ||
420 // http://sources.redhat.com/ml/libc-hacker/2001-07/msg00053.html
421 rLocale
.Country
== "CA" ||
423 // http://unicode.org/cldr/trac/ticket/1710
424 // https://www.redhat.com/archives/fedora-devel-list/2008-August/msg00019.html
425 rLocale
.Country
== "VE" ||
427 // http://unicode.org/cldr/trac/ticket/1710
428 // https://www.redhat.com/archives/fedora-devel-list/2008-August/msg00240.html
429 rLocale
.Country
== "CL" ||
431 // http://unicode.org/cldr/trac/ticket/1710
432 // http://qa.openoffice.org/issues/show_bug.cgi?id=49739
433 rLocale
.Country
== "MX" ||
435 // http://unicode.org/cldr/trac/ticket/1710
436 // http://qa.openoffice.org/issues/show_bug.cgi?id=69703
437 rLocale
.Country
== "CO" ||
439 // http://unicode.org/cldr/trac/ticket/1710
440 // http://ubuntuliving.blogspot.com/2008/07/default-paper-size-in-evince.html
441 // http://www.gov.ph/faqs/driverslicense.asp
442 rLocale
.Country
== "PH" ||
444 // http://unicode.org/cldr/trac/ticket/2585
445 // http://www.belize.gov.bz/ct.asp?xItem=1666&ctNode=486&mp=27
446 rLocale
.Country
== "BZ" ||
448 // http://unicode.org/cldr/trac/ticket/2585
449 // http://sources.redhat.com/bugzilla/show_bug.cgi?id=11258
450 rLocale
.Country
== "CR" ||
452 // http://unicode.org/cldr/trac/ticket/2585
453 // http://sources.redhat.com/bugzilla/show_bug.cgi?id=10936
454 rLocale
.Country
== "GT" ||
456 // http://unicode.org/cldr/trac/ticket/2585
457 rLocale
.Country
== "NI" ||
459 // http://unicode.org/cldr/trac/ticket/2585
460 // http://www.minsa.gob.pa/minsa/tl_files/documents/baner_informativo/INSTRUMENTO%20DE%20INVESTIGACION%20DE%20RAAV%202009.pdf
461 rLocale
.Country
== "PA" ||
463 // http://unicode.org/cldr/trac/ticket/2585
464 // http://www.tse.gob.sv
465 rLocale
.Country
== "SV"
468 eType
= PAPER_LETTER
;
474 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */