xref: /aoo42x/main/i18npool/source/paper/paper.cxx (revision 635e0213)
1449ab281SAndrew Rist /**************************************************************
2*635e0213SMatthias Seidel  *
3449ab281SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4449ab281SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5449ab281SAndrew Rist  * distributed with this work for additional information
6449ab281SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7449ab281SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8449ab281SAndrew Rist  * "License"); you may not use this file except in compliance
9449ab281SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*635e0213SMatthias Seidel  *
11449ab281SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*635e0213SMatthias Seidel  *
13449ab281SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14449ab281SAndrew Rist  * software distributed under the License is distributed on an
15449ab281SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16449ab281SAndrew Rist  * KIND, either express or implied.  See the License for the
17449ab281SAndrew Rist  * specific language governing permissions and limitations
18449ab281SAndrew Rist  * under the License.
19*635e0213SMatthias Seidel  *
20449ab281SAndrew Rist  *************************************************************/
21449ab281SAndrew Rist 
22449ab281SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_i18npool.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <osl/diagnose.h>
28cdf0e10cSrcweir #include <sal/config.h>
29cdf0e10cSrcweir #include <rtl/ustring.hxx>
30cdf0e10cSrcweir #include <rtl/string.hxx>
31cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
32cdf0e10cSrcweir #include <com/sun/star/i18n/ScriptType.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include "i18npool/mslangid.hxx"
37cdf0e10cSrcweir #include "i18npool/paper.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <utility>
40cdf0e10cSrcweir #include <cstdlib>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #ifdef UNX
43cdf0e10cSrcweir #include <stdio.h>
44cdf0e10cSrcweir #include <string.h>
45cdf0e10cSrcweir #include <locale.h>
46cdf0e10cSrcweir #include <langinfo.h>
47cdf0e10cSrcweir #endif
48cdf0e10cSrcweir 
49cdf0e10cSrcweir struct PageDesc
50cdf0e10cSrcweir {
51cdf0e10cSrcweir     long m_nWidth;
52cdf0e10cSrcweir     long m_nHeight;
53cdf0e10cSrcweir     const char *m_pPSName;
54cdf0e10cSrcweir     const char *m_pAltPSName;
55cdf0e10cSrcweir };
56cdf0e10cSrcweir 
57cdf0e10cSrcweir #define PT2MM100( v ) \
58cdf0e10cSrcweir     (long)(((v) * 35.27777778) + 0.5)
59cdf0e10cSrcweir 
60cdf0e10cSrcweir #define IN2MM100( v ) \
61cdf0e10cSrcweir     ((long)(((v) * 2540) + 0.5))
62cdf0e10cSrcweir 
63cdf0e10cSrcweir #define MM2MM100( v ) \
64cdf0e10cSrcweir     ((long)((v) * 100))
65cdf0e10cSrcweir 
66cdf0e10cSrcweir //PostScript Printer Description File Format Specification
67cdf0e10cSrcweir //http://partners.adobe.com/public/developer/en/ps/5003.PPD_Spec_v4.3.pdf
68cdf0e10cSrcweir //http://www.y-adagio.com/public/committees/docsii/doc_00-49/symp_ulaan/china_ppr.pdf (Kai)
69cdf0e10cSrcweir //http://www.sls.psi.ch/controls/help/howto/Howto_Print_a_A0_Poster_at_WSLA_012_2.pdf (Dia)
70cdf0e10cSrcweir static PageDesc aDinTab[] =
71cdf0e10cSrcweir {
72cdf0e10cSrcweir     { MM2MM100( 841 ),   MM2MM100( 1189 ),   "A0",  NULL },
73cdf0e10cSrcweir     { MM2MM100( 594 ),   MM2MM100( 841 ),    "A1",  NULL },
74cdf0e10cSrcweir     { MM2MM100( 420 ),   MM2MM100( 594 ),    "A2",  NULL },
75cdf0e10cSrcweir     { MM2MM100( 297 ),   MM2MM100( 420 ),    "A3",  NULL },
76cdf0e10cSrcweir     { MM2MM100( 210 ),   MM2MM100( 297 ),    "A4",  NULL },
77cdf0e10cSrcweir     { MM2MM100( 148 ),   MM2MM100( 210 ),    "A5",  NULL },
78cdf0e10cSrcweir     { MM2MM100( 250 ),   MM2MM100( 353 ),    "ISOB4",  NULL },
79cdf0e10cSrcweir     { MM2MM100( 176 ),   MM2MM100( 250 ),    "ISOB5",  NULL },
80cdf0e10cSrcweir     { IN2MM100( 8.5 ),   IN2MM100( 11 ),     "Letter",  "Note" },
81cdf0e10cSrcweir     { IN2MM100( 8.5 ),   IN2MM100( 14 ),     "Legal",  NULL },
82cdf0e10cSrcweir     { IN2MM100( 11 ),    IN2MM100( 17 ),     "Tabloid",  "11x17" },
83*635e0213SMatthias Seidel     { 0,                 0,                  NULL, NULL }, // User
84cdf0e10cSrcweir     { MM2MM100( 125 ),   MM2MM100( 176 ),    "ISOB6",  NULL },
85cdf0e10cSrcweir     { MM2MM100( 229 ),   MM2MM100( 324 ),    "EnvC4",  "C4" },
86cdf0e10cSrcweir     { MM2MM100( 162 ),   MM2MM100( 229 ),    "EnvC5",  "C5" },
87cdf0e10cSrcweir     { MM2MM100( 114 ),   MM2MM100( 162 ),    "EnvC6",  "C6" },
88cdf0e10cSrcweir     { MM2MM100( 114 ),   MM2MM100( 229 ),    "EnvC65", NULL },
89cdf0e10cSrcweir     { MM2MM100( 110 ),   MM2MM100( 220 ),    "EnvDL",  "DL" },
90*635e0213SMatthias Seidel     { MM2MM100( 180),    MM2MM100( 270 ),    NULL,  NULL }, // Dia Slide
91*635e0213SMatthias Seidel     { MM2MM100( 210),    MM2MM100( 280 ),    NULL,  NULL }, // Screen (4:3)
92cdf0e10cSrcweir     { IN2MM100( 17 ),    IN2MM100( 22 ),     "AnsiC",  "CSheet" },
93cdf0e10cSrcweir     { IN2MM100( 22 ),    IN2MM100( 34 ),     "AnsiD",  "DSheet" },
94cdf0e10cSrcweir     { IN2MM100( 34 ),    IN2MM100( 44 ),     "AnsiE",  "ESheet" },
95cdf0e10cSrcweir     { IN2MM100( 7.25 ),  IN2MM100( 10.5 ),   "Executive",  NULL },
96cdf0e10cSrcweir     //"Folio" is a different size in the PPD documentation than 8.5x11
97cdf0e10cSrcweir     //This "FanFoldGermanLegal" is known in the Philippines as
98*635e0213SMatthias Seidel     //"Legal" paper or "Long Bond Paper". The "Legal" name causing untold
99cdf0e10cSrcweir     //misery, given the differently sized US "Legal" paper
100cdf0e10cSrcweir     { IN2MM100( 8.5 ),   IN2MM100( 13 ),     "FanFoldGermanLegal",  NULL },
101cdf0e10cSrcweir     { IN2MM100( 3.875 ), IN2MM100( 7.5 ),    "EnvMonarch", "Monarch" },
102cdf0e10cSrcweir     { IN2MM100( 3.625 ), IN2MM100( 6.5 ),    "EnvPersonal",  "Personal" },
103cdf0e10cSrcweir     { IN2MM100( 3.875 ), IN2MM100( 8.875 ),  "Env9",  NULL },
104cdf0e10cSrcweir     { IN2MM100( 4.125 ), IN2MM100( 9.5 ),    "Env10",  "Comm10" },
105cdf0e10cSrcweir     { IN2MM100( 4.5 ),   IN2MM100( 10.375 ), "Env11",  NULL },
106cdf0e10cSrcweir     { IN2MM100( 4.75 ),  IN2MM100( 11 ),     "Env12",  NULL },
107*635e0213SMatthias Seidel     { MM2MM100( 184 ),   MM2MM100( 260 ),    NULL,  NULL }, // Kai16
108*635e0213SMatthias Seidel     { MM2MM100( 130 ),   MM2MM100( 184 ),    NULL,  NULL }, // Kai32
109*635e0213SMatthias Seidel     { MM2MM100( 140 ),   MM2MM100( 203 ),    NULL,  NULL }, // BigKai32
110*635e0213SMatthias Seidel     { MM2MM100( 257 ),   MM2MM100( 364 ),    "B4",  NULL }, // JIS
111*635e0213SMatthias Seidel     { MM2MM100( 182 ),   MM2MM100( 257 ),    "B5",  NULL }, // JIS
112*635e0213SMatthias Seidel     { MM2MM100( 128 ),   MM2MM100( 182 ),    "B6",  NULL }, // JIS
113cdf0e10cSrcweir     { IN2MM100( 17 ),    IN2MM100( 11 ),     "Ledger",  NULL },
114cdf0e10cSrcweir     { IN2MM100( 5.5 ),   IN2MM100( 8.5 ),    "Statement",  NULL },
115cdf0e10cSrcweir     { PT2MM100( 610 ),   PT2MM100( 780 ),    "Quarto",  NULL },
116cdf0e10cSrcweir     { IN2MM100( 10 ),    IN2MM100( 14 ),     "10x14",  NULL },
117cdf0e10cSrcweir     { IN2MM100( 5.5 ),   IN2MM100( 11.5 ),   "Env14",  NULL },
118cdf0e10cSrcweir     { MM2MM100( 324 ),   MM2MM100( 458 ),    "EnvC3",  "C3" },
119cdf0e10cSrcweir     { MM2MM100( 110 ),   MM2MM100( 230 ),    "EnvItalian",  NULL },
120cdf0e10cSrcweir     { IN2MM100( 14.875 ),IN2MM100( 11 ),     "FanFoldUS",  NULL },
121cdf0e10cSrcweir     { IN2MM100( 8.5 ),   IN2MM100( 13 ),     "FanFoldGerman",  NULL },
122cdf0e10cSrcweir     { MM2MM100( 100 ),   MM2MM100( 148 ),    "Postcard",  NULL },
123cdf0e10cSrcweir     { IN2MM100( 9 ),     IN2MM100( 11 ),     "9x11",  NULL },
124cdf0e10cSrcweir     { IN2MM100( 10 ),    IN2MM100( 11 ),     "10x11",  NULL },
125cdf0e10cSrcweir     { IN2MM100( 15 ),    IN2MM100( 11 ),     "15x11",  NULL },
126cdf0e10cSrcweir     { MM2MM100( 220 ),   MM2MM100( 220 ),    "EnvInvite",  NULL },
127cdf0e10cSrcweir     { MM2MM100( 227 ),   MM2MM100( 356 ),    "SuperA",  NULL },
128cdf0e10cSrcweir     { MM2MM100( 305 ),   MM2MM100( 487 ),    "SuperB",  NULL },
129cdf0e10cSrcweir     { IN2MM100( 8.5 ),   IN2MM100( 12.69 ),  "LetterPlus",  NULL },
130cdf0e10cSrcweir     { MM2MM100( 210 ),   MM2MM100( 330 ),    "A4Plus",  NULL },
131cdf0e10cSrcweir     { MM2MM100( 200 ),   MM2MM100( 148 ),    "DoublePostcard",  NULL },
132cdf0e10cSrcweir     { MM2MM100( 105 ),   MM2MM100( 148 ),    "A6",  NULL },
133cdf0e10cSrcweir     { IN2MM100( 12 ),    IN2MM100( 11 ),     "12x11",  NULL },
134cdf0e10cSrcweir     { MM2MM100( 74 ),    MM2MM100( 105 ),    "A7",  NULL },
135cdf0e10cSrcweir     { MM2MM100( 52 ),    MM2MM100( 74 ),     "A8",  NULL },
136cdf0e10cSrcweir     { MM2MM100( 37 ),    MM2MM100( 52 ),     "A9",  NULL },
137cdf0e10cSrcweir     { MM2MM100( 26 ),    MM2MM100( 37 ),     "A10",  NULL },
138cdf0e10cSrcweir     { MM2MM100( 1000 ),  MM2MM100( 1414 ),   "ISOB0",  NULL },
139cdf0e10cSrcweir     { MM2MM100( 707 ),   MM2MM100( 1000 ),   "ISOB1",  NULL },
140cdf0e10cSrcweir     { MM2MM100( 500 ),   MM2MM100( 707 ),    "ISOB2",  NULL },
141cdf0e10cSrcweir     { MM2MM100( 353 ),   MM2MM100( 500 ),    "ISOB3",  NULL },
142cdf0e10cSrcweir     { MM2MM100( 88 ),    MM2MM100( 125 ),    "ISOB7",  NULL },
143cdf0e10cSrcweir     { MM2MM100( 62 ),    MM2MM100( 88 ),     "ISOB8",  NULL },
144cdf0e10cSrcweir     { MM2MM100( 44 ),    MM2MM100( 62 ),     "ISOB9",  NULL },
145cdf0e10cSrcweir     { MM2MM100( 31 ),    MM2MM100( 44 ),     "ISOB10", NULL },
146cdf0e10cSrcweir     { MM2MM100( 458 ),   MM2MM100( 648 ),    "EnvC2",  "C2" },
147cdf0e10cSrcweir     { MM2MM100( 81 ),    MM2MM100( 114 ),    "EnvC7",  "C7" },
148cdf0e10cSrcweir     { MM2MM100( 57 ),    MM2MM100( 81 ),     "EnvC8",  "C8" },
149cdf0e10cSrcweir     { IN2MM100( 9 ),     IN2MM100( 12 ),     "ARCHA",  NULL },
150cdf0e10cSrcweir     { IN2MM100( 12 ),    IN2MM100( 18 ),     "ARCHB",  NULL },
151cdf0e10cSrcweir     { IN2MM100( 18 ),    IN2MM100( 24 ),     "ARCHC",  NULL },
152cdf0e10cSrcweir     { IN2MM100( 24 ),    IN2MM100( 36 ),     "ARCHD",  NULL },
153*635e0213SMatthias Seidel     { IN2MM100( 36 ),    IN2MM100( 48 ),     "ARCHE",  NULL },
154*635e0213SMatthias Seidel     { MM2MM100( 157.5),  MM2MM100( 280 ),    NULL,  NULL }, // Screen (16:9)
155*635e0213SMatthias Seidel     { MM2MM100( 175),    MM2MM100( 280 ),    NULL,  NULL }  // Screen (16:10)
156cdf0e10cSrcweir };
157cdf0e10cSrcweir 
158cdf0e10cSrcweir static const size_t nTabSize = sizeof(aDinTab) / sizeof(aDinTab[0]);
159cdf0e10cSrcweir 
160cdf0e10cSrcweir #define MAXSLOPPY 21
161cdf0e10cSrcweir 
162cdf0e10cSrcweir bool PaperInfo::doSloppyFit()
163cdf0e10cSrcweir {
164cdf0e10cSrcweir     if (m_eType != PAPER_USER)
165cdf0e10cSrcweir         return true;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     for ( size_t i = 0; i < nTabSize; ++i )
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         if (i == PAPER_USER) continue;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir         long lDiffW = labs(aDinTab[i].m_nWidth - m_nPaperWidth);
172cdf0e10cSrcweir         long lDiffH = labs(aDinTab[i].m_nHeight - m_nPaperHeight);
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         if ( lDiffW < MAXSLOPPY && lDiffH < MAXSLOPPY )
175cdf0e10cSrcweir         {
176cdf0e10cSrcweir             m_nPaperWidth = aDinTab[i].m_nWidth;
177cdf0e10cSrcweir             m_nPaperHeight = aDinTab[i].m_nHeight;
178cdf0e10cSrcweir             m_eType = (Paper)i;
179cdf0e10cSrcweir             return true;
180cdf0e10cSrcweir         }
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     return false;
184cdf0e10cSrcweir }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir bool PaperInfo::sloppyEqual(const PaperInfo &rOther) const
187cdf0e10cSrcweir {
188*635e0213SMatthias Seidel     return
189cdf0e10cSrcweir     (
190cdf0e10cSrcweir       (labs(m_nPaperWidth - rOther.m_nPaperWidth) < MAXSLOPPY) &&
191cdf0e10cSrcweir       (labs(m_nPaperHeight - rOther.m_nPaperHeight) < MAXSLOPPY)
192cdf0e10cSrcweir     );
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir long PaperInfo::sloppyFitPageDimension(long nDimension)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir     for ( size_t i = 0; i < nTabSize; ++i )
198cdf0e10cSrcweir     {
199cdf0e10cSrcweir         if (i == PAPER_USER) continue;
200cdf0e10cSrcweir         long lDiff;
201cdf0e10cSrcweir 
202cdf0e10cSrcweir         lDiff = labs(aDinTab[i].m_nWidth - nDimension);
203cdf0e10cSrcweir         if ( lDiff < MAXSLOPPY )
204cdf0e10cSrcweir             return aDinTab[i].m_nWidth;
205cdf0e10cSrcweir 
206cdf0e10cSrcweir         lDiff = labs(aDinTab[i].m_nHeight - nDimension);
207cdf0e10cSrcweir         if ( lDiff < MAXSLOPPY )
208cdf0e10cSrcweir             return aDinTab[i].m_nHeight;
209cdf0e10cSrcweir     }
210cdf0e10cSrcweir     return nDimension;
211cdf0e10cSrcweir }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir PaperInfo PaperInfo::getSystemDefaultPaper()
214cdf0e10cSrcweir {
215cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
216cdf0e10cSrcweir     using ::com::sun::star::lang::XMultiServiceFactory;
217cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
218cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
219cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
220cdf0e10cSrcweir     using ::com::sun::star::container::XNameAccess;
221cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
222cdf0e10cSrcweir #   define CREATE_OUSTRING( ascii ) \
223cdf0e10cSrcweir         rtl::OUString::intern( RTL_CONSTASCII_USTRINGPARAM( ascii ) )
224cdf0e10cSrcweir 
225cdf0e10cSrcweir     rtl::OUString aLocaleStr;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir     Reference< XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
228cdf0e10cSrcweir     Reference< XMultiServiceFactory > xConfigProv(
229cdf0e10cSrcweir         xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.configuration.ConfigurationProvider" ) ),
230cdf0e10cSrcweir         UNO_QUERY_THROW );
231cdf0e10cSrcweir 
232cdf0e10cSrcweir     Sequence< Any > aArgs( 1 );
233cdf0e10cSrcweir     aArgs[ 0 ] <<= CREATE_OUSTRING( "org.openoffice.Setup/L10N/" );
234cdf0e10cSrcweir     Reference< XNameAccess > xConfigNA( xConfigProv->createInstanceWithArguments(
235cdf0e10cSrcweir         CREATE_OUSTRING( "com.sun.star.configuration.ConfigurationAccess" ), aArgs ), UNO_QUERY_THROW );
236cdf0e10cSrcweir     try
237cdf0e10cSrcweir     {
238cdf0e10cSrcweir         // try user-defined locale setting
239cdf0e10cSrcweir         xConfigNA->getByName( CREATE_OUSTRING( "ooSetupSystemLocale" ) ) >>= aLocaleStr;
240cdf0e10cSrcweir     }
241cdf0e10cSrcweir     catch( Exception& ) {}
242cdf0e10cSrcweir 
243cdf0e10cSrcweir #ifdef UNX
244cdf0e10cSrcweir     // if set to "use system", get papersize from system
245cdf0e10cSrcweir     if (aLocaleStr.getLength() == 0)
246cdf0e10cSrcweir     {
247cdf0e10cSrcweir         static bool bInitialized = false;
248cdf0e10cSrcweir         static PaperInfo aInstance(PAPER_A4);
249cdf0e10cSrcweir 
250cdf0e10cSrcweir         if (bInitialized)
251cdf0e10cSrcweir             return aInstance;
252cdf0e10cSrcweir 
253cdf0e10cSrcweir         // try libpaper
254cdf0e10cSrcweir         // #i78617# workaround missing paperconf command
255cdf0e10cSrcweir         FILE* pPipe = popen( "sh -c paperconf 2>/dev/null", "r" );
256cdf0e10cSrcweir         if( pPipe )
257cdf0e10cSrcweir         {
258cdf0e10cSrcweir             Paper ePaper = PAPER_USER;
259cdf0e10cSrcweir 
260cdf0e10cSrcweir             char aBuffer[ 1024 ];
261cdf0e10cSrcweir             aBuffer[0] = 0;
262cdf0e10cSrcweir             char *pBuffer = fgets( aBuffer, sizeof(aBuffer), pPipe );
263cdf0e10cSrcweir             pclose( pPipe );
264cdf0e10cSrcweir 
265cdf0e10cSrcweir             if (pBuffer && *pBuffer != 0)
266cdf0e10cSrcweir             {
267cdf0e10cSrcweir                 rtl::OString aPaper(pBuffer);
268cdf0e10cSrcweir                 aPaper = aPaper.trim();
269cdf0e10cSrcweir                 static struct { const char *pName; Paper ePaper; } aCustoms [] =
270cdf0e10cSrcweir                 {
271*635e0213SMatthias Seidel                     { "B0",    PAPER_B0_ISO },
272*635e0213SMatthias Seidel                     { "B1",    PAPER_B1_ISO },
273*635e0213SMatthias Seidel                     { "B2",    PAPER_B2_ISO },
274*635e0213SMatthias Seidel                     { "B3",    PAPER_B3_ISO },
275*635e0213SMatthias Seidel                     { "B4",    PAPER_B4_ISO },
276*635e0213SMatthias Seidel                     { "B5",    PAPER_B5_ISO },
277*635e0213SMatthias Seidel                     { "B6",    PAPER_B6_ISO },
278*635e0213SMatthias Seidel                     { "B7",    PAPER_B7_ISO },
279*635e0213SMatthias Seidel                     { "B8",    PAPER_B8_ISO },
280*635e0213SMatthias Seidel                     { "B9",    PAPER_B9_ISO },
281*635e0213SMatthias Seidel                     { "B10",   PAPER_B10_ISO },
282cdf0e10cSrcweir                     { "folio", PAPER_FANFOLD_LEGAL_DE },
283cdf0e10cSrcweir                     { "flsa",  PAPER_FANFOLD_LEGAL_DE },
284cdf0e10cSrcweir                     { "flse",  PAPER_FANFOLD_LEGAL_DE }
285cdf0e10cSrcweir                 };
286cdf0e10cSrcweir 
287cdf0e10cSrcweir                 bool bHalve = false;
288cdf0e10cSrcweir 
289cdf0e10cSrcweir                 size_t nExtraTabSize = sizeof(aCustoms) / sizeof(aCustoms[0]);
290cdf0e10cSrcweir                 for (size_t i = 0; i < nExtraTabSize; ++i)
291cdf0e10cSrcweir                 {
292cdf0e10cSrcweir                     if (rtl_str_compareIgnoreAsciiCase(aCustoms[i].pName, aPaper.getStr()) == 0)
293cdf0e10cSrcweir                     {
294cdf0e10cSrcweir                         ePaper = aCustoms[i].ePaper;
295cdf0e10cSrcweir                         break;
296cdf0e10cSrcweir                     }
297cdf0e10cSrcweir                 }
298cdf0e10cSrcweir 
299cdf0e10cSrcweir                 if (ePaper == PAPER_USER)
300cdf0e10cSrcweir                 {
301cdf0e10cSrcweir                     bHalve = !rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
302*635e0213SMatthias Seidel                         aPaper.getStr(), aPaper.getLength(), "half", 4, 4);
303cdf0e10cSrcweir                     if (bHalve)
304cdf0e10cSrcweir                         aPaper = aPaper.copy(4);
305cdf0e10cSrcweir                     ePaper = PaperInfo::fromPSName(aPaper);
306cdf0e10cSrcweir                 }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir                 if (ePaper != PAPER_USER)
309cdf0e10cSrcweir                 {
310cdf0e10cSrcweir                     aInstance = PaperInfo(ePaper);
311cdf0e10cSrcweir                     if (bHalve)
312cdf0e10cSrcweir                         aInstance = PaperInfo(aInstance.getHeight()/2, aInstance.getWidth());
313cdf0e10cSrcweir                     bInitialized = true;
314cdf0e10cSrcweir                     return aInstance;
315cdf0e10cSrcweir                 }
316cdf0e10cSrcweir             }
317cdf0e10cSrcweir         }
318cdf0e10cSrcweir 
319cdf0e10cSrcweir #if defined(LC_PAPER) && defined(_GNU_SOURCE)
320cdf0e10cSrcweir 
321cdf0e10cSrcweir         union paperword { char *string; int word; };
322cdf0e10cSrcweir 
323cdf0e10cSrcweir         // try LC_PAPER
324cdf0e10cSrcweir         paperword w, h;
325cdf0e10cSrcweir         w.string = nl_langinfo(_NL_PAPER_WIDTH);
326cdf0e10cSrcweir         h.string = nl_langinfo(_NL_PAPER_HEIGHT);
327cdf0e10cSrcweir 
328*635e0213SMatthias Seidel         // glibc stores sizes as integer mm units
329cdf0e10cSrcweir         w.word *= 100;
330cdf0e10cSrcweir         h.word *= 100;
331cdf0e10cSrcweir 
332cdf0e10cSrcweir         for ( size_t i = 0; i < nTabSize; ++i )
333cdf0e10cSrcweir         {
334cdf0e10cSrcweir             if (i == PAPER_USER) continue;
335*635e0213SMatthias Seidel 
336*635e0213SMatthias Seidel             // glibc stores sizes as integer mm units, and so is inaccurate. To
337*635e0213SMatthias Seidel             // find a standard paper size we calculate the standard paper sizes
338*635e0213SMatthias Seidel             // into equally inaccurate mm and compare
339cdf0e10cSrcweir             long width = (aDinTab[i].m_nWidth + 50) / 100;
340cdf0e10cSrcweir             long height = (aDinTab[i].m_nHeight + 50) / 100;
341*635e0213SMatthias Seidel 
342cdf0e10cSrcweir             if (width == w.word/100 && height == h.word/100)
343cdf0e10cSrcweir             {
344cdf0e10cSrcweir                 w.word = aDinTab[i].m_nWidth;
345cdf0e10cSrcweir                 h.word = aDinTab[i].m_nHeight;
346cdf0e10cSrcweir                 break;
347cdf0e10cSrcweir             }
348cdf0e10cSrcweir         }
349cdf0e10cSrcweir 
350cdf0e10cSrcweir         aInstance = PaperInfo(w.word, h.word);
351cdf0e10cSrcweir         bInitialized = true;
352cdf0e10cSrcweir         return aInstance;
353cdf0e10cSrcweir #endif
354cdf0e10cSrcweir     }
355cdf0e10cSrcweir #endif
356cdf0e10cSrcweir 
357cdf0e10cSrcweir     try
358cdf0e10cSrcweir     {
359cdf0e10cSrcweir         // if set to "use system", try to get locale from system
360cdf0e10cSrcweir         if( aLocaleStr.getLength() == 0 )
361cdf0e10cSrcweir         {
362cdf0e10cSrcweir             aArgs[ 0 ] <<= CREATE_OUSTRING( "org.openoffice.System/L10N/" );
363cdf0e10cSrcweir             xConfigNA.set( xConfigProv->createInstanceWithArguments(
364cdf0e10cSrcweir                 CREATE_OUSTRING( "com.sun.star.configuration.ConfigurationAccess" ), aArgs ),
365cdf0e10cSrcweir                 UNO_QUERY_THROW );
366cdf0e10cSrcweir             xConfigNA->getByName( CREATE_OUSTRING( "Locale" ) ) >>= aLocaleStr;
367cdf0e10cSrcweir         }
368cdf0e10cSrcweir     }
369cdf0e10cSrcweir     catch( Exception& ) {}
370cdf0e10cSrcweir 
371cdf0e10cSrcweir     if (aLocaleStr.getLength() == 0)
372cdf0e10cSrcweir         aLocaleStr = CREATE_OUSTRING("en-US");
373cdf0e10cSrcweir 
374cdf0e10cSrcweir     // convert locale string to locale struct
375cdf0e10cSrcweir     ::com::sun::star::lang::Locale aSysLocale;
376cdf0e10cSrcweir     sal_Int32 nDashPos = aLocaleStr.indexOf( '-' );
377cdf0e10cSrcweir     if( nDashPos < 0 ) nDashPos = aLocaleStr.getLength();
378cdf0e10cSrcweir     aSysLocale.Language = aLocaleStr.copy( 0, nDashPos );
379cdf0e10cSrcweir     if( nDashPos + 1 < aLocaleStr.getLength() )
380cdf0e10cSrcweir         aSysLocale.Country = aLocaleStr.copy( nDashPos + 1 );
381cdf0e10cSrcweir 
382cdf0e10cSrcweir     return PaperInfo::getDefaultPaperForLocale(aSysLocale);
383cdf0e10cSrcweir }
384cdf0e10cSrcweir 
385cdf0e10cSrcweir PaperInfo::PaperInfo(Paper eType) : m_eType(eType)
386cdf0e10cSrcweir {
387cdf0e10cSrcweir     OSL_ENSURE( sizeof(aDinTab) / sizeof(aDinTab[0]) == NUM_PAPER_ENTRIES,
388cdf0e10cSrcweir             "mismatch between array entries and enum values" );
389cdf0e10cSrcweir 
390cdf0e10cSrcweir     m_nPaperWidth = aDinTab[m_eType].m_nWidth;
391cdf0e10cSrcweir     m_nPaperHeight = aDinTab[m_eType].m_nHeight;
392cdf0e10cSrcweir }
393cdf0e10cSrcweir 
394cdf0e10cSrcweir PaperInfo::PaperInfo(long nPaperWidth, long nPaperHeight)
395cdf0e10cSrcweir     : m_eType(PAPER_USER),
396cdf0e10cSrcweir       m_nPaperWidth(nPaperWidth),
397cdf0e10cSrcweir       m_nPaperHeight(nPaperHeight)
398cdf0e10cSrcweir {
399cdf0e10cSrcweir     for ( size_t i = 0; i < nTabSize; ++i )
400cdf0e10cSrcweir     {
401*635e0213SMatthias Seidel         if (
402cdf0e10cSrcweir              (nPaperWidth == aDinTab[i].m_nWidth) &&
403cdf0e10cSrcweir              (nPaperHeight == aDinTab[i].m_nHeight)
404cdf0e10cSrcweir            )
405cdf0e10cSrcweir         {
406cdf0e10cSrcweir             m_eType = static_cast<Paper>(i);
407cdf0e10cSrcweir             break;
408cdf0e10cSrcweir         }
409cdf0e10cSrcweir     }
410cdf0e10cSrcweir }
411cdf0e10cSrcweir 
412cdf0e10cSrcweir rtl::OString PaperInfo::toPSName(Paper ePaper)
413cdf0e10cSrcweir {
414cdf0e10cSrcweir     return static_cast<size_t>(ePaper) < nTabSize ?
415cdf0e10cSrcweir         rtl::OString(aDinTab[ePaper].m_pPSName) : rtl::OString();
416cdf0e10cSrcweir }
417cdf0e10cSrcweir 
418cdf0e10cSrcweir Paper PaperInfo::fromPSName(const rtl::OString &rName)
419cdf0e10cSrcweir {
420cdf0e10cSrcweir     if (!rName.getLength())
421cdf0e10cSrcweir         return PAPER_USER;
422cdf0e10cSrcweir 
423cdf0e10cSrcweir     for ( size_t i = 0; i < nTabSize; ++i )
424cdf0e10cSrcweir     {
425*635e0213SMatthias Seidel         if (aDinTab[i].m_pPSName &&
426cdf0e10cSrcweir           !rtl_str_compareIgnoreAsciiCase(aDinTab[i].m_pPSName, rName.getStr()))
427cdf0e10cSrcweir         {
428cdf0e10cSrcweir             return static_cast<Paper>(i);
429cdf0e10cSrcweir         }
430cdf0e10cSrcweir         else if (aDinTab[i].m_pAltPSName &&
431cdf0e10cSrcweir           !rtl_str_compareIgnoreAsciiCase(aDinTab[i].m_pAltPSName, rName.getStr()))
432cdf0e10cSrcweir         {
433cdf0e10cSrcweir             return static_cast<Paper>(i);
434cdf0e10cSrcweir         }
435cdf0e10cSrcweir     }
436cdf0e10cSrcweir 
437cdf0e10cSrcweir     return PAPER_USER;
438cdf0e10cSrcweir }
439cdf0e10cSrcweir 
440cdf0e10cSrcweir //http://wiki.services.openoffice.org/wiki/DefaultPaperSize
441cdf0e10cSrcweir //http://www.unicode.org/cldr/data/charts/supplemental/territory_language_information.html
442cdf0e10cSrcweir //http://sourceware.org/git/?p=glibc.git;a=tree;f=localedata/locales
443cdf0e10cSrcweir //http://en.wikipedia.org/wiki/Paper_size
444cdf0e10cSrcweir //http://msdn.microsoft.com/en-us/library/cc195164.aspx
445cdf0e10cSrcweir PaperInfo PaperInfo::getDefaultPaperForLocale(
446cdf0e10cSrcweir     const ::com::sun::star::lang::Locale & rLocale)
447cdf0e10cSrcweir {
448cdf0e10cSrcweir     Paper eType = PAPER_A4;
449cdf0e10cSrcweir 
450cdf0e10cSrcweir     if (
451cdf0e10cSrcweir         //United States, Letter
452cdf0e10cSrcweir         !rLocale.Country.compareToAscii("US") ||
453cdf0e10cSrcweir         //Puerto Rico:
454cdf0e10cSrcweir         //    http://unicode.org/cldr/trac/ticket/1710
455cdf0e10cSrcweir         //    http://sources.redhat.com/ml/libc-hacker/2001-07/msg00046.html
456cdf0e10cSrcweir         !rLocale.Country.compareToAscii("PR") ||
457cdf0e10cSrcweir         //Canada:
458cdf0e10cSrcweir         //    http://sources.redhat.com/ml/libc-hacker/2001-07/msg00053.html
459cdf0e10cSrcweir         !rLocale.Country.compareToAscii("CA") ||
460cdf0e10cSrcweir         //Venuzuela:
461cdf0e10cSrcweir         //    http://unicode.org/cldr/trac/ticket/1710
462cdf0e10cSrcweir         //    https://www.redhat.com/archives/fedora-devel-list/2008-August/msg00019.html
463cdf0e10cSrcweir         !rLocale.Country.compareToAscii("VE") ||
464cdf0e10cSrcweir         //Chile:
465cdf0e10cSrcweir         //    http://unicode.org/cldr/trac/ticket/1710
466cdf0e10cSrcweir         //    https://www.redhat.com/archives/fedora-devel-list/2008-August/msg00240.html
467cdf0e10cSrcweir         !rLocale.Country.compareToAscii("CL") ||
468cdf0e10cSrcweir         //Mexico:
469cdf0e10cSrcweir         //    http://unicode.org/cldr/trac/ticket/1710
470cdf0e10cSrcweir         //    http://qa.openoffice.org/issues/show_bug.cgi?id=49739
471cdf0e10cSrcweir         !rLocale.Country.compareToAscii("MX") ||
472cdf0e10cSrcweir         //Colombia:
473cdf0e10cSrcweir         //    http://unicode.org/cldr/trac/ticket/1710
474cdf0e10cSrcweir         //    http://qa.openoffice.org/issues/show_bug.cgi?id=69703
475cdf0e10cSrcweir         !rLocale.Country.compareToAscii("CO") ||
476cdf0e10cSrcweir         //Philippines:
477cdf0e10cSrcweir         //    http://unicode.org/cldr/trac/ticket/1710
478cdf0e10cSrcweir         //    http://ubuntuliving.blogspot.com/2008/07/default-paper-size-in-evince.html
479cdf0e10cSrcweir         //    http://www.gov.ph/faqs/driverslicense.asp
480cdf0e10cSrcweir         !rLocale.Country.compareToAscii("PH") ||
481cdf0e10cSrcweir         //Belize:
482cdf0e10cSrcweir         //    http://unicode.org/cldr/trac/ticket/2585
483cdf0e10cSrcweir         //    http://www.belize.gov.bz/ct.asp?xItem=1666&ctNode=486&mp=27
484cdf0e10cSrcweir         !rLocale.Country.compareToAscii("BZ") ||
485cdf0e10cSrcweir         //Costa Rica:
486cdf0e10cSrcweir         //    http://unicode.org/cldr/trac/ticket/2585
487cdf0e10cSrcweir         //    http://sources.redhat.com/bugzilla/show_bug.cgi?id=11258
488cdf0e10cSrcweir         !rLocale.Country.compareToAscii("CR") ||
489cdf0e10cSrcweir         //Guatemala:
490cdf0e10cSrcweir         //    http://unicode.org/cldr/trac/ticket/2585
491cdf0e10cSrcweir         //    http://sources.redhat.com/bugzilla/show_bug.cgi?id=10936
492cdf0e10cSrcweir         !rLocale.Country.compareToAscii("GT") ||
493cdf0e10cSrcweir         //Nicaragua:
494cdf0e10cSrcweir         //    http://unicode.org/cldr/trac/ticket/2585
495cdf0e10cSrcweir         !rLocale.Country.compareToAscii("NI") ||
496cdf0e10cSrcweir         //Panama:
497cdf0e10cSrcweir         //    http://unicode.org/cldr/trac/ticket/2585
498cdf0e10cSrcweir         //    http://www.minsa.gob.pa/minsa/tl_files/documents/baner_informativo/INSTRUMENTO%20DE%20INVESTIGACION%20DE%20RAAV%202009.pdf
499cdf0e10cSrcweir         !rLocale.Country.compareToAscii("PA") ||
500cdf0e10cSrcweir         //El Salvador:
501cdf0e10cSrcweir         //    http://unicode.org/cldr/trac/ticket/2585
502cdf0e10cSrcweir         //    http://www.tse.gob.sv
503cdf0e10cSrcweir         !rLocale.Country.compareToAscii("SV")
504cdf0e10cSrcweir        )
505cdf0e10cSrcweir     {
506cdf0e10cSrcweir         eType = PAPER_LETTER;
507cdf0e10cSrcweir     }
508cdf0e10cSrcweir 
509cdf0e10cSrcweir     return eType;
510cdf0e10cSrcweir }
511cdf0e10cSrcweir 
512*635e0213SMatthias Seidel /* vim: set noet sw=4 ts=4: */
513