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 <officecfg/Setup.hxx>
22 #include <officecfg/System.hxx>
23 #include <sal/config.h>
24 #include <sal/macros.h>
25 #include <rtl/ustring.hxx>
26 #include <rtl/string.hxx>
27 #include <tools/long.hxx>
29 #include <i18nutil/paper.hxx>
31 #include <unotools/configmgr.hxx>
32 #include <com/sun/star/lang/Locale.hpp>
37 #if defined(LC_PAPER) && defined(_GNU_SOURCE)
47 tools::Long m_nHeight
;
48 const char *m_pPSName
;
49 const char *m_pAltPSName
;
54 #define PT2MM100( v ) \
55 tools::Long(((v) * 35.27777778) + 0.5)
57 #define IN2MM100( v ) \
58 (tools::Long(((v) * 2540) + 0.5))
60 #define MM2MM100( v ) \
61 (tools::Long((v) * 100))
63 //PostScript Printer Description File Format Specification
64 //http://partners.adobe.com/public/developer/en/ps/5003.PPD_Spec_v4.3.pdf
65 //https://web.archive.org/web/20040912070512/http://www.y-adagio.com/public/committees/docsii/doc_00-49/symp_ulaan/china_ppr.pdf (Kai)
66 //http://www.sls.psi.ch/controls/help/howto/Howto_Print_a_A0_Poster_at_WSLA_012_2.pdf (Dia)
68 //!! The order of these entries must correspond to enum Paper in <i18nutil/paper.hxx>
70 // see XclPaperSize pPaperSizeTable in calc and ApiPaperSize in filter
71 const PageDesc aDinTab
[] =
73 { MM2MM100( 841 ), MM2MM100( 1189 ), "A0", nullptr },
74 { MM2MM100( 594 ), MM2MM100( 841 ), "A1", nullptr },
75 { MM2MM100( 420 ), MM2MM100( 594 ), "A2", nullptr },
76 { MM2MM100( 297 ), MM2MM100( 420 ), "A3", nullptr },
77 { MM2MM100( 210 ), MM2MM100( 297 ), "A4", nullptr },
78 { MM2MM100( 148 ), MM2MM100( 210 ), "A5", nullptr },
79 { MM2MM100( 250 ), MM2MM100( 353 ), "ISOB4", nullptr },
80 { MM2MM100( 176 ), MM2MM100( 250 ), "ISOB5", nullptr },
81 { IN2MM100( 8.5 ), IN2MM100( 11 ), "Letter", "Note" },
82 { IN2MM100( 8.5 ), IN2MM100( 14 ), "Legal", nullptr },
83 { IN2MM100( 11 ), IN2MM100( 17 ), "Tabloid", "11x17" },
84 { 0, 0, nullptr, nullptr }, //User
85 { MM2MM100( 125 ), MM2MM100( 176 ), "ISOB6", nullptr },
86 { MM2MM100( 229 ), MM2MM100( 324 ), "EnvC4", "C4" },
87 { MM2MM100( 162 ), MM2MM100( 229 ), "EnvC5", "C5" },
88 { MM2MM100( 114 ), MM2MM100( 162 ), "EnvC6", "C6" },
89 { MM2MM100( 114 ), MM2MM100( 229 ), "EnvC65", nullptr },
90 { MM2MM100( 110 ), MM2MM100( 220 ), "EnvDL", "DL" },
91 { MM2MM100( 180), MM2MM100( 270 ), nullptr, nullptr }, //Dia
92 { MM2MM100( 210), MM2MM100( 280 ), nullptr, nullptr }, //Screen 4:3
93 { IN2MM100( 17 ), IN2MM100( 22 ), "AnsiC", "CSheet" },
94 { IN2MM100( 22 ), IN2MM100( 34 ), "AnsiD", "DSheet" },
95 { IN2MM100( 34 ), IN2MM100( 44 ), "AnsiE", "ESheet" },
96 { IN2MM100( 7.25 ), IN2MM100( 10.5 ), "Executive", nullptr },
97 //"Folio" is a different size in the PPD documentation than 8.5x11
98 //This "FanFoldGermanLegal" is known in the Philippines as
99 //"Legal" paper or "Long Bond Paper". The "Legal" name causing untold
100 //misery, given the differently sized US "Legal" paper
101 { IN2MM100( 8.5 ), IN2MM100( 13 ), "FanFoldGermanLegal", nullptr },
102 { IN2MM100( 3.875 ), IN2MM100( 7.5 ), "EnvMonarch", "Monarch" },
103 { IN2MM100( 3.625 ), IN2MM100( 6.5 ), "EnvPersonal", "Personal" },
104 { IN2MM100( 3.875 ), IN2MM100( 8.875 ), "Env9", nullptr },
105 { IN2MM100( 4.125 ), IN2MM100( 9.5 ), "Env10", "Comm10" },
106 { IN2MM100( 4.5 ), IN2MM100( 10.375 ), "Env11", nullptr },
107 { IN2MM100( 4.75 ), IN2MM100( 11 ), "Env12", nullptr },
108 { MM2MM100( 184 ), MM2MM100( 260 ), nullptr, nullptr }, //Kai16 / 16k
109 { MM2MM100( 130 ), MM2MM100( 184 ), nullptr, nullptr }, //Kai32
110 { MM2MM100( 140 ), MM2MM100( 203 ), nullptr, nullptr }, //BigKai32
111 { MM2MM100( 257 ), MM2MM100( 364 ), "B4", nullptr }, //JIS
112 { MM2MM100( 182 ), MM2MM100( 257 ), "B5", nullptr }, //JIS
113 { MM2MM100( 128 ), MM2MM100( 182 ), "B6", nullptr }, //JIS
114 { IN2MM100( 17 ), IN2MM100( 11 ), "Ledger", nullptr },
115 { IN2MM100( 5.5 ), IN2MM100( 8.5 ), "Statement", nullptr },
116 { PT2MM100( 610 ), PT2MM100( 780 ), "Quarto", nullptr },
117 { IN2MM100( 10 ), IN2MM100( 14 ), "10x14", nullptr },
118 { IN2MM100( 5.5 ), IN2MM100( 11.5 ), "Env14", nullptr },
119 { MM2MM100( 324 ), MM2MM100( 458 ), "EnvC3", "C3" },
120 { MM2MM100( 110 ), MM2MM100( 230 ), "EnvItalian", nullptr },
121 { IN2MM100( 14.875 ),IN2MM100( 11 ), "FanFoldUS", nullptr },
122 { IN2MM100( 8.5 ), IN2MM100( 13 ), "FanFoldGerman", nullptr },
123 { MM2MM100( 100 ), MM2MM100( 148 ), "Postcard", nullptr },
124 { IN2MM100( 9 ), IN2MM100( 11 ), "9x11", nullptr },
125 { IN2MM100( 10 ), IN2MM100( 11 ), "10x11", nullptr },
126 { IN2MM100( 15 ), IN2MM100( 11 ), "15x11", nullptr },
127 { MM2MM100( 220 ), MM2MM100( 220 ), "EnvInvite", nullptr },
128 { MM2MM100( 227 ), MM2MM100( 356 ), "SuperA", nullptr },
129 { MM2MM100( 305 ), MM2MM100( 487 ), "SuperB", nullptr },
130 { IN2MM100( 8.5 ), IN2MM100( 12.69 ), "LetterPlus", nullptr },
131 { MM2MM100( 210 ), MM2MM100( 330 ), "A4Plus", nullptr },
132 { MM2MM100( 200 ), MM2MM100( 148 ), "DoublePostcard", nullptr },
133 { MM2MM100( 105 ), MM2MM100( 148 ), "A6", nullptr },
134 { IN2MM100( 12 ), IN2MM100( 11 ), "12x11", nullptr },
135 { MM2MM100( 74 ), MM2MM100( 105 ), "A7", nullptr },
136 { MM2MM100( 52 ), MM2MM100( 74 ), "A8", nullptr },
137 { MM2MM100( 37 ), MM2MM100( 52 ), "A9", nullptr },
138 { MM2MM100( 26 ), MM2MM100( 37 ), "A10", nullptr },
139 { MM2MM100( 1000 ), MM2MM100( 1414 ), "ISOB0", nullptr },
140 { MM2MM100( 707 ), MM2MM100( 1000 ), "ISOB1", nullptr },
141 { MM2MM100( 500 ), MM2MM100( 707 ), "ISOB2", nullptr },
142 { MM2MM100( 353 ), MM2MM100( 500 ), "ISOB3", nullptr },
143 { MM2MM100( 88 ), MM2MM100( 125 ), "ISOB7", nullptr },
144 { MM2MM100( 62 ), MM2MM100( 88 ), "ISOB8", nullptr },
145 { MM2MM100( 44 ), MM2MM100( 62 ), "ISOB9", nullptr },
146 { MM2MM100( 31 ), MM2MM100( 44 ), "ISOB10", nullptr },
147 { MM2MM100( 458 ), MM2MM100( 648 ), "EnvC2", "C2" },
148 { MM2MM100( 81 ), MM2MM100( 114 ), "EnvC7", "C7" },
149 { MM2MM100( 57 ), MM2MM100( 81 ), "EnvC8", "C8" },
150 { IN2MM100( 9 ), IN2MM100( 12 ), "ARCHA", nullptr },
151 { IN2MM100( 12 ), IN2MM100( 18 ), "ARCHB", nullptr },
152 { IN2MM100( 18 ), IN2MM100( 24 ), "ARCHC", nullptr },
153 { IN2MM100( 24 ), IN2MM100( 36 ), "ARCHD", nullptr },
154 { IN2MM100( 36 ), IN2MM100( 48 ), "ARCHE", nullptr },
155 { MM2MM100( 157.5), MM2MM100( 280 ), nullptr, nullptr }, //Screen 16:9
156 { MM2MM100( 175 ), MM2MM100( 280 ), nullptr, nullptr }, //Screen 16:10
157 { MM2MM100( 195 ), MM2MM100( 270 ), nullptr, nullptr }, // 16k
158 { MM2MM100( 197 ), MM2MM100( 273 ), nullptr, nullptr }, // 16k
159 { IN2MM100( 7.5 ), IN2MM100( 13.333 ), nullptr, nullptr }, //PowerPoint Widescreen
160 { IN2MM100( 7.5 ), IN2MM100( 10 ), nullptr, nullptr }, //PowerPoint On-screen Show (4:3)
161 { IN2MM100( 5.625 ), IN2MM100( 10 ), nullptr, nullptr }, //PowerPoint On-screen Show (16:9)
162 { IN2MM100( 6.25 ), IN2MM100( 10 ), nullptr, nullptr } //PowerPoint On-screen Show (16:10)
166 const size_t nTabSize
= SAL_N_ELEMENTS(aDinTab
);
168 // tdf#151107 make this not just large enough for rounding errors on our side
169 // but also large enough to match where the ppd side rounded up to the next
170 // point. Seen with C5/C6/C65 envelopes which can be found over 1 point away
171 // from the values in aDinTab
172 #define MAXSLOPPY PT2MM100(1.25)
174 void PaperInfo::doSloppyFit(bool bAlsoTryRotated
)
176 if (m_eType
!= PAPER_USER
)
179 for ( size_t i
= 0; i
< nTabSize
; ++i
)
181 if (i
== PAPER_USER
) continue;
183 tools::Long lDiffW
= std::abs(aDinTab
[i
].m_nWidth
- m_nPaperWidth
);
184 tools::Long lDiffH
= std::abs(aDinTab
[i
].m_nHeight
- m_nPaperHeight
);
186 if (lDiffW
< MAXSLOPPY
&& lDiffH
< MAXSLOPPY
)
188 m_nPaperWidth
= aDinTab
[i
].m_nWidth
;
189 m_nPaperHeight
= aDinTab
[i
].m_nHeight
;
190 m_eType
= static_cast<Paper
>(i
);
197 std::swap(m_nPaperWidth
, m_nPaperHeight
);
199 std::swap(m_nPaperWidth
, m_nPaperHeight
);
203 bool PaperInfo::sloppyEqual(const PaperInfo
&rOther
) const
207 (std::abs(m_nPaperWidth
- rOther
.m_nPaperWidth
) < MAXSLOPPY
) &&
208 (std::abs(m_nPaperHeight
- rOther
.m_nPaperHeight
) < MAXSLOPPY
)
212 tools::Long
PaperInfo::sloppyFitPageDimension(tools::Long nDimension
)
214 for ( size_t i
= 0; i
< nTabSize
; ++i
)
216 if (i
== PAPER_USER
) continue;
219 lDiff
= std::abs(aDinTab
[i
].m_nWidth
- nDimension
);
220 if ( lDiff
< MAXSLOPPY
)
221 return aDinTab
[i
].m_nWidth
;
223 lDiff
= std::abs(aDinTab
[i
].m_nHeight
- nDimension
);
224 if ( lDiff
< MAXSLOPPY
)
225 return aDinTab
[i
].m_nHeight
;
230 PaperInfo
PaperInfo::getSystemDefaultPaper()
232 if (utl::ConfigManager::IsFuzzing())
233 return PaperInfo(PAPER_A4
);
235 OUString aLocaleStr
= officecfg::Setup::L10N::ooSetupSystemLocale::get();
238 // if set to "use system", get papersize from system
239 if (aLocaleStr
.isEmpty())
241 static bool bInitialized
= false;
242 static PaperInfo
aInstance(PAPER_A4
);
247 #if ! defined(MACOSX) && ! defined(EMSCRIPTEN)
249 // #i78617# workaround missing paperconf command
250 FILE* pPipe
= popen( "paperconf 2>/dev/null", "r" );
253 Paper ePaper
= PAPER_USER
;
255 char aBuffer
[ 1024 ];
257 char *pBuffer
= fgets( aBuffer
, sizeof(aBuffer
), pPipe
);
258 bool bOk
= pclose(pPipe
) == 0;
260 if (bOk
&& pBuffer
&& *pBuffer
!= 0)
262 OString
aPaper(pBuffer
);
263 aPaper
= aPaper
.trim();
264 static const struct { const char *pName
; Paper ePaper
; } aCustoms
[] =
266 { "B0", PAPER_B0_ISO
},
267 { "B1", PAPER_B1_ISO
},
268 { "B2", PAPER_B2_ISO
},
269 { "B3", PAPER_B3_ISO
},
270 { "B4", PAPER_B4_ISO
},
271 { "B5", PAPER_B5_ISO
},
272 { "B6", PAPER_B6_ISO
},
273 { "B7", PAPER_B7_ISO
},
274 { "B8", PAPER_B8_ISO
},
275 { "B9", PAPER_B9_ISO
},
276 { "B10", PAPER_B10_ISO
},
277 { "folio", PAPER_FANFOLD_LEGAL_DE
},
278 { "flsa", PAPER_FANFOLD_LEGAL_DE
},
279 { "flse", PAPER_FANFOLD_LEGAL_DE
}
284 size_t const nExtraTabSize
= SAL_N_ELEMENTS(aCustoms
);
285 for (size_t i
= 0; i
< nExtraTabSize
; ++i
)
287 if (rtl_str_compareIgnoreAsciiCase(aCustoms
[i
].pName
, aPaper
.getStr()) == 0)
289 ePaper
= aCustoms
[i
].ePaper
;
294 if (ePaper
== PAPER_USER
)
296 bHalve
= aPaper
.startsWith("half", &aPaper
);
297 ePaper
= PaperInfo::fromPSName(aPaper
);
300 if (ePaper
!= PAPER_USER
)
302 aInstance
= PaperInfo(ePaper
);
304 aInstance
= PaperInfo(aInstance
.getHeight()/2, aInstance
.getWidth());
312 // _NL_PAPER_WIDTH / HEIGHT not available with android unified headers
313 #if defined(LC_PAPER) && defined(_GNU_SOURCE) && !defined(ANDROID)
315 locale_t loc
= newlocale(LC_PAPER_MASK
, "", static_cast<locale_t
>(0));
316 if (loc
!= static_cast<locale_t
>(0))
318 union paperword
{ char *string
; int word
; };
320 w
.string
= nl_langinfo_l(_NL_PAPER_WIDTH
, loc
);
321 h
.string
= nl_langinfo_l(_NL_PAPER_HEIGHT
, loc
);
325 //glibc stores sizes as integer mm units
329 for ( size_t i
= 0; i
< nTabSize
; ++i
)
331 if (i
== PAPER_USER
) continue;
333 //glibc stores sizes as integer mm units, and so is inaccurate.
334 //To find a standard paper size we calculate the standard paper
335 //sizes into equally inaccurate mm and compare
336 tools::Long width
= (aDinTab
[i
].m_nWidth
+ 50) / 100;
337 tools::Long height
= (aDinTab
[i
].m_nHeight
+ 50) / 100;
339 if (width
== w
.word
/100 && height
== h
.word
/100)
341 w
.word
= aDinTab
[i
].m_nWidth
;
342 h
.word
= aDinTab
[i
].m_nHeight
;
347 aInstance
= PaperInfo(w
.word
, h
.word
);
355 // if set to "use system", try to get locale from system
356 if (aLocaleStr
.isEmpty())
357 aLocaleStr
= officecfg::System::L10N::Locale::get();
359 static constexpr OUStringLiteral EN_US
= u
"en-US";
360 if (aLocaleStr
.isEmpty())
363 // convert locale string to locale struct
364 css::lang::Locale aSysLocale
;
365 sal_Int32 nDashPos
= aLocaleStr
.indexOf( '-' );
366 if( nDashPos
< 0 ) nDashPos
= aLocaleStr
.getLength();
367 aSysLocale
.Language
= aLocaleStr
.copy( 0, nDashPos
);
368 if( nDashPos
+ 1 < aLocaleStr
.getLength() )
369 aSysLocale
.Country
= aLocaleStr
.copy( nDashPos
+ 1 );
371 return PaperInfo::getDefaultPaperForLocale(aSysLocale
);
374 PaperInfo::PaperInfo(Paper eType
) : m_eType(eType
)
376 static_assert( SAL_N_ELEMENTS(aDinTab
) == NUM_PAPER_ENTRIES
,
377 "mismatch between array entries and enum values" );
379 m_nPaperWidth
= aDinTab
[m_eType
].m_nWidth
;
380 m_nPaperHeight
= aDinTab
[m_eType
].m_nHeight
;
383 PaperInfo::PaperInfo(tools::Long nPaperWidth
, tools::Long nPaperHeight
)
384 : m_eType(PAPER_USER
),
385 m_nPaperWidth(nPaperWidth
),
386 m_nPaperHeight(nPaperHeight
)
388 for ( size_t i
= 0; i
< nTabSize
; ++i
)
391 (nPaperWidth
== aDinTab
[i
].m_nWidth
) &&
392 (nPaperHeight
== aDinTab
[i
].m_nHeight
)
395 m_eType
= static_cast<Paper
>(i
);
401 OString
PaperInfo::toPSName(Paper ePaper
)
403 return static_cast<size_t>(ePaper
) < nTabSize
&& aDinTab
[ePaper
].m_pPSName
? OString(aDinTab
[ePaper
].m_pPSName
) : OString();
406 Paper
PaperInfo::fromPSName(const OString
&rName
)
411 for ( size_t i
= 0; i
< nTabSize
; ++i
)
413 if (aDinTab
[i
].m_pPSName
&&
414 !rtl_str_compareIgnoreAsciiCase(aDinTab
[i
].m_pPSName
, rName
.getStr()))
416 return static_cast<Paper
>(i
);
418 else if (aDinTab
[i
].m_pAltPSName
&&
419 !rtl_str_compareIgnoreAsciiCase(aDinTab
[i
].m_pAltPSName
, rName
.getStr()))
421 return static_cast<Paper
>(i
);
428 //http://wiki.openoffice.org/wiki/DefaultPaperSize
429 //http://www.unicode.org/cldr/data/charts/supplemental/territory_language_information.html
430 //http://sourceware.org/git/?p=glibc.git;a=tree;f=localedata/locales
431 //http://en.wikipedia.org/wiki/Paper_size
432 //http://msdn.microsoft.com/en-us/library/cc195164.aspx
433 PaperInfo
PaperInfo::getDefaultPaperForLocale( const css::lang::Locale
& rLocale
)
435 Paper eType
= PAPER_A4
;
438 //United States, Letter
439 rLocale
.Country
== "US" ||
441 // http://unicode.org/cldr/trac/ticket/1710
442 // http://sources.redhat.com/ml/libc-hacker/2001-07/msg00046.html
443 rLocale
.Country
== "PR" ||
445 // http://sources.redhat.com/ml/libc-hacker/2001-07/msg00053.html
446 rLocale
.Country
== "CA" ||
448 // http://unicode.org/cldr/trac/ticket/1710
449 // https://www.redhat.com/archives/fedora-devel-list/2008-August/msg00019.html
450 rLocale
.Country
== "VE" ||
452 // http://unicode.org/cldr/trac/ticket/1710
453 // https://www.redhat.com/archives/fedora-devel-list/2008-August/msg00240.html
454 rLocale
.Country
== "CL" ||
456 // http://unicode.org/cldr/trac/ticket/1710
457 // http://qa.openoffice.org/issues/show_bug.cgi?id=49739
458 rLocale
.Country
== "MX" ||
460 // http://unicode.org/cldr/trac/ticket/1710
461 // http://qa.openoffice.org/issues/show_bug.cgi?id=69703
462 rLocale
.Country
== "CO" ||
464 // http://unicode.org/cldr/trac/ticket/1710
465 // http://ubuntuliving.blogspot.com/2008/07/default-paper-size-in-evince.html
466 // http://www.gov.ph/faqs/driverslicense.asp
467 rLocale
.Country
== "PH" ||
469 // http://unicode.org/cldr/trac/ticket/2585
470 // http://www.belize.gov.bz/ct.asp?xItem=1666&ctNode=486&mp=27
471 rLocale
.Country
== "BZ" ||
473 // http://unicode.org/cldr/trac/ticket/2585
474 // http://sources.redhat.com/bugzilla/show_bug.cgi?id=11258
475 rLocale
.Country
== "CR" ||
477 // http://unicode.org/cldr/trac/ticket/2585
478 // http://sources.redhat.com/bugzilla/show_bug.cgi?id=10936
479 rLocale
.Country
== "GT" ||
481 // http://unicode.org/cldr/trac/ticket/2585
482 rLocale
.Country
== "NI" ||
484 // http://unicode.org/cldr/trac/ticket/2585
485 // http://www.minsa.gob.pa/minsa/tl_files/documents/baner_informativo/INSTRUMENTO%20DE%20INVESTIGACION%20DE%20RAAV%202009.pdf
486 rLocale
.Country
== "PA" ||
488 // http://unicode.org/cldr/trac/ticket/2585
489 // http://www.tse.gob.sv
490 rLocale
.Country
== "SV"
493 eType
= PAPER_LETTER
;
499 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */