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