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