1 /* 2 * PWG media name API implementation for CUPS. 3 * 4 * Copyright 2009-2017 by Apple Inc. 5 * 6 * These coded instructions, statements, and computer programs are the 7 * property of Apple Inc. and are protected by Federal copyright 8 * law. Distribution and use rights are outlined in the file "LICENSE.txt" 9 * which should have been included with this file. If this file is 10 * missing or damaged, see the license at "http://www.cups.org/". 11 * 12 * This file is subject to the Apple OS-Developed Software exception. 13 */ 14 15 /* 16 * Include necessary headers... 17 */ 18 19 #include "cups-private.h" 20 #include <math.h> 21 22 23 /* 24 * Local macros... 25 */ 26 27 #define _PWG_MEDIA_IN(p,l,a,x,y) {p, l, a, (int)(x * 2540), (int)(y * 2540)} 28 #define _PWG_MEDIA_MM(p,l,a,x,y) {p, l, a, (int)(x * 100), (int)(y * 100)} 29 30 31 /* 32 * Local functions... 33 */ 34 35 static int pwg_compare_legacy(pwg_media_t *a, pwg_media_t *b); 36 static int pwg_compare_pwg(pwg_media_t *a, pwg_media_t *b); 37 static int pwg_compare_ppd(pwg_media_t *a, pwg_media_t *b); 38 static char *pwg_format_inches(char *buf, size_t bufsize, int val); 39 static char *pwg_format_millimeters(char *buf, size_t bufsize, int val); 40 static int pwg_scan_measurement(const char *buf, char **bufptr, int numer, int denom); 41 42 43 /* 44 * Local globals... 45 */ 46 47 static pwg_media_t const cups_pwg_media[] = 48 { /* Media size lookup table */ 49 /* North American Standard Sheet Media Sizes */ 50 _PWG_MEDIA_IN("na_index-3x5_3x5in", NULL, "3x5", 3, 5), 51 _PWG_MEDIA_IN("na_personal_3.625x6.5in", NULL, "EnvPersonal", 3.625, 6.5), 52 _PWG_MEDIA_IN("na_monarch_3.875x7.5in", "monarch-envelope", "EnvMonarch", 3.875, 7.5), 53 _PWG_MEDIA_IN("na_number-9_3.875x8.875in", "na-number-9-envelope", "Env9", 3.875, 8.875), 54 _PWG_MEDIA_IN("na_index-4x6_4x6in", NULL, "4x6", 4, 6), 55 _PWG_MEDIA_IN("na_number-10_4.125x9.5in", "na-number-10-envelope", "Env10", 4.125, 9.5), 56 _PWG_MEDIA_IN("na_a2_4.375x5.75in", NULL, "EnvA2", 4.375, 5.75), 57 _PWG_MEDIA_IN("na_number-11_4.5x10.375in", NULL, "Env11", 4.5, 10.375), 58 _PWG_MEDIA_IN("na_number-12_4.75x11in", NULL, "Env12", 4.75, 11), 59 _PWG_MEDIA_IN("na_5x7_5x7in", NULL, "5x7", 5, 7), 60 _PWG_MEDIA_IN("na_index-5x8_5x8in", NULL, "5x8", 5, 8), 61 _PWG_MEDIA_IN("na_number-14_5x11.5in", NULL, "Env14", 5, 11.5), 62 _PWG_MEDIA_IN("na_invoice_5.5x8.5in", "invoice", "Statement", 5.5, 8.5), 63 _PWG_MEDIA_IN("na_index-4x6-ext_6x8in", NULL, "6x8", 6, 8), 64 _PWG_MEDIA_IN("na_6x9_6x9in", "na-6x9-envelope", "6x9", 6, 9), 65 _PWG_MEDIA_IN("na_c5_6.5x9.5in", NULL, "6.5x9.5", 6.5, 9.5), 66 _PWG_MEDIA_IN("na_7x9_7x9in", "na-7x9-envelope", "7x9", 7, 9), 67 _PWG_MEDIA_IN("na_executive_7.25x10.5in", "executive", "Executive", 7.25, 10.5), 68 _PWG_MEDIA_IN("na_govt-letter_8x10in", "na-8x10", "8x10", 8, 10), 69 _PWG_MEDIA_IN("na_govt-legal_8x13in", NULL, "8x13", 8, 13), 70 _PWG_MEDIA_IN("na_quarto_8.5x10.83in", "quarto", "Quarto", 8.5, 10.83), 71 _PWG_MEDIA_IN("na_letter_8.5x11in", "na-letter", "Letter", 8.5, 11), 72 _PWG_MEDIA_IN("na_fanfold-eur_8.5x12in", NULL, "FanFoldGerman", 8.5, 12), 73 _PWG_MEDIA_IN("na_letter-plus_8.5x12.69in", NULL, "LetterPlus", 8.5, 12.69), 74 _PWG_MEDIA_IN("na_foolscap_8.5x13in", NULL, "FanFoldGermanLegal", 8.5, 13), 75 _PWG_MEDIA_IN("na_oficio_8.5x13.4in", NULL, "Oficio", 8.5, 13.4), 76 _PWG_MEDIA_IN("na_legal_8.5x14in", "na-legal", "Legal", 8.5, 14), 77 _PWG_MEDIA_IN("na_super-a_8.94x14in", NULL, "SuperA", 8.94, 14), 78 _PWG_MEDIA_IN("na_9x11_9x11in", "na-9x11-envelope", "9x11", 9, 11), 79 _PWG_MEDIA_IN("na_arch-a_9x12in", "arch-a", "ARCHA", 9, 12), 80 _PWG_MEDIA_IN("na_letter-extra_9.5x12in", NULL, "LetterExtra", 9.5, 12), 81 _PWG_MEDIA_IN("na_legal-extra_9.5x15in", NULL, "LegalExtra", 9.5, 15), 82 _PWG_MEDIA_IN("na_10x11_10x11in", NULL, "10x11", 10, 11), 83 _PWG_MEDIA_IN("na_10x13_10x13in", "na-10x13-envelope", "10x13", 10, 13), 84 _PWG_MEDIA_IN("na_10x14_10x14in", "na-10x14-envelope", "10x14", 10, 14), 85 _PWG_MEDIA_IN("na_10x15_10x15in", "na-10x15-envelope", "10x15", 10, 15), 86 _PWG_MEDIA_IN("na_11x12_11x12in", NULL, "11x12", 11, 12), 87 _PWG_MEDIA_IN("na_edp_11x14in", NULL, "11x14", 11, 14), 88 _PWG_MEDIA_IN("na_fanfold-us_11x14.875in", NULL, "11x14.875", 11, 14.875), 89 _PWG_MEDIA_IN("na_11x15_11x15in", NULL, "11x15", 11, 15), 90 _PWG_MEDIA_IN("na_ledger_11x17in", "tabloid", "Tabloid", 11, 17), 91 _PWG_MEDIA_IN("na_eur-edp_12x14in", NULL, NULL, 12, 14), 92 _PWG_MEDIA_IN("na_arch-b_12x18in", "arch-b", "ARCHB", 12, 18), 93 _PWG_MEDIA_IN("na_12x19_12x19in", NULL, "12x19", 12, 19), 94 _PWG_MEDIA_IN("na_b-plus_12x19.17in", NULL, "SuperB", 12, 19.17), 95 _PWG_MEDIA_IN("na_super-b_13x19in", "super-b", "13x19", 13, 19), 96 _PWG_MEDIA_IN("na_c_17x22in", "c", "AnsiC", 17, 22), 97 _PWG_MEDIA_IN("na_arch-c_18x24in", "arch-c", "ARCHC", 18, 24), 98 _PWG_MEDIA_IN("na_d_22x34in", "d", "AnsiD", 22, 34), 99 _PWG_MEDIA_IN("na_arch-d_24x36in", "arch-d", "ARCHD", 24, 36), 100 _PWG_MEDIA_IN("asme_f_28x40in", "f", "28x40", 28, 40), 101 _PWG_MEDIA_IN("na_wide-format_30x42in", NULL, "30x42", 30, 42), 102 _PWG_MEDIA_IN("na_e_34x44in", "e", "AnsiE", 34, 44), 103 _PWG_MEDIA_IN("na_arch-e_36x48in", "arch-e", "ARCHE", 36, 48), 104 _PWG_MEDIA_IN("na_f_44x68in", NULL, "AnsiF", 44, 68), 105 106 /* ISO Standard Sheet Media Sizes */ 107 _PWG_MEDIA_MM("iso_a10_26x37mm", "iso-a10", "A10", 26, 37), 108 _PWG_MEDIA_MM("iso_a9_37x52mm", "iso-a9", "A9", 37, 52), 109 _PWG_MEDIA_MM("iso_a8_52x74mm", "iso-a8", "A8", 52, 74), 110 _PWG_MEDIA_MM("iso_a7_74x105mm", "iso-a7", "A7", 74, 105), 111 _PWG_MEDIA_MM("iso_a6_105x148mm", "iso-a6", "A6", 105, 148), 112 _PWG_MEDIA_MM("iso_a5_148x210mm", "iso-a5", "A5", 148, 210), 113 _PWG_MEDIA_MM("iso_a5-extra_174x235mm", NULL, "A5Extra", 174, 235), 114 _PWG_MEDIA_MM("iso_a4_210x297mm", "iso-a4", "A4", 210, 297), 115 _PWG_MEDIA_MM("iso_a4-tab_225x297mm", NULL, "A4Tab", 225, 297), 116 _PWG_MEDIA_MM("iso_a4-extra_235.5x322.3mm", NULL, "A4Extra", 235.5, 322.3), 117 _PWG_MEDIA_MM("iso_a3_297x420mm", "iso-a3", "A3", 297, 420), 118 _PWG_MEDIA_MM("iso_a4x3_297x630mm", "iso-a4x3", "A4x3", 297, 630), 119 _PWG_MEDIA_MM("iso_a4x4_297x841mm", "iso-a4x4", "A4x4", 297, 841), 120 _PWG_MEDIA_MM("iso_a4x5_297x1051mm", "iso-a4x5", "A4x5", 297, 1051), 121 _PWG_MEDIA_MM("iso_a4x6_297x1261mm", "iso-a4x6", "A4x6", 297, 1261), 122 _PWG_MEDIA_MM("iso_a4x7_297x1471mm", "iso-a4x7", "A4x7", 297, 1471), 123 _PWG_MEDIA_MM("iso_a4x8_297x1682mm", "iso-a4x8", "A4x8", 297, 1682), 124 _PWG_MEDIA_MM("iso_a4x9_297x1892mm", "iso-a4x9", "A4x9", 297, 1892), 125 _PWG_MEDIA_MM("iso_a3-extra_322x445mm", "iso-a3-extra", "A3Extra", 322, 445), 126 _PWG_MEDIA_MM("iso_a2_420x594mm", "iso-a2", "A2", 420, 594), 127 _PWG_MEDIA_MM("iso_a3x3_420x891mm", "iso-a3x3", "A3x3", 420, 891), 128 _PWG_MEDIA_MM("iso_a3x4_420x1189mm", "iso-a3x4", "A3x4", 420, 1189), 129 _PWG_MEDIA_MM("iso_a3x5_420x1486mm", "iso-a3x5", "A3x6", 420, 1486), 130 _PWG_MEDIA_MM("iso_a3x6_420x1783mm", "iso-a3x6", "A3x6", 420, 1783), 131 _PWG_MEDIA_MM("iso_a3x7_420x2080mm", "iso-a3x7", "A3x7", 420, 2080), 132 _PWG_MEDIA_MM("iso_a1_594x841mm", "iso-a1", "A1", 594, 841), 133 _PWG_MEDIA_MM("iso_a2x3_594x1261mm", "iso-a2x3", "A2x3", 594, 1261), 134 _PWG_MEDIA_MM("iso_a2x4_594x1682mm", "iso-a2x4", "A2x4", 594, 1682), 135 _PWG_MEDIA_MM("iso_a2x5_594x2102mm", "iso-a2x5", "A2x5", 594, 2102), 136 _PWG_MEDIA_MM("iso_a0_841x1189mm", "iso-a0", "A0", 841, 1189), 137 _PWG_MEDIA_MM("iso_a1x3_841x1783mm", "iso-a1x3", "A1x3", 841, 1783), 138 _PWG_MEDIA_MM("iso_a1x4_841x2378mm", "iso-a1x4", "A1x4", 841, 2378), 139 _PWG_MEDIA_MM("iso_2a0_1189x1682mm", NULL, "1189x1682mm", 1189, 1682), 140 _PWG_MEDIA_MM("iso_a0x3_1189x2523mm", NULL, "A0x3", 1189, 2523), 141 _PWG_MEDIA_MM("iso_b10_31x44mm", "iso-b10", "ISOB10", 31, 44), 142 _PWG_MEDIA_MM("iso_b9_44x62mm", "iso-b9", "ISOB9", 44, 62), 143 _PWG_MEDIA_MM("iso_b8_62x88mm", "iso-b8", "ISOB8", 62, 88), 144 _PWG_MEDIA_MM("iso_b7_88x125mm", "iso-b7", "ISOB7", 88, 125), 145 _PWG_MEDIA_MM("iso_b6_125x176mm", "iso-b6", "ISOB6", 125, 176), 146 _PWG_MEDIA_MM("iso_b6c4_125x324mm", NULL, "125x324mm", 125, 324), 147 _PWG_MEDIA_MM("iso_b5_176x250mm", "iso-b5", "ISOB5", 176, 250), 148 _PWG_MEDIA_MM("iso_b5-extra_201x276mm", NULL, "ISOB5Extra", 201, 276), 149 _PWG_MEDIA_MM("iso_b4_250x353mm", "iso-b4", "ISOB4", 250, 353), 150 _PWG_MEDIA_MM("iso_b3_353x500mm", "iso-b3", "ISOB3", 353, 500), 151 _PWG_MEDIA_MM("iso_b2_500x707mm", "iso-b2", "ISOB2", 500, 707), 152 _PWG_MEDIA_MM("iso_b1_707x1000mm", "iso-b1", "ISOB1", 707, 1000), 153 _PWG_MEDIA_MM("iso_b0_1000x1414mm", "iso-b0", "ISOB0", 1000, 1414), 154 _PWG_MEDIA_MM("iso_c10_28x40mm", "iso-c10", "EnvC10", 28, 40), 155 _PWG_MEDIA_MM("iso_c9_40x57mm", "iso-c9", "EnvC9", 40, 57), 156 _PWG_MEDIA_MM("iso_c8_57x81mm", "iso-c8", "EnvC8", 57, 81), 157 _PWG_MEDIA_MM("iso_c7_81x114mm", "iso-c7", "EnvC7", 81, 114), 158 _PWG_MEDIA_MM("iso_c7c6_81x162mm", NULL, "EnvC76", 81, 162), 159 _PWG_MEDIA_MM("iso_c6_114x162mm", "iso-c6", "EnvC6", 114, 162), 160 _PWG_MEDIA_MM("iso_c6c5_114x229mm", NULL, "EnvC65", 114, 229), 161 _PWG_MEDIA_MM("iso_c5_162x229mm", "iso-c5", "EnvC5", 162, 229), 162 _PWG_MEDIA_MM("iso_c4_229x324mm", "iso-c4", "EnvC4", 229, 324), 163 _PWG_MEDIA_MM("iso_c3_324x458mm", "iso-c3", "EnvC3", 324, 458), 164 _PWG_MEDIA_MM("iso_c2_458x648mm", "iso-c2", "EnvC2", 458, 648), 165 _PWG_MEDIA_MM("iso_c1_648x917mm", "iso-c1", "EnvC1", 648, 917), 166 _PWG_MEDIA_MM("iso_c0_917x1297mm", "iso-c0", "EnvC0", 917, 1297), 167 _PWG_MEDIA_MM("iso_dl_110x220mm", "iso-designated", "EnvDL", 110, 220), 168 _PWG_MEDIA_MM("iso_ra4_215x305mm", "iso-ra4", "RA4", 215, 305), 169 _PWG_MEDIA_MM("iso_sra4_225x320mm", "iso-sra4", "SRA4", 225, 320), 170 _PWG_MEDIA_MM("iso_ra3_305x430mm", "iso-ra3", "RA3", 305, 430), 171 _PWG_MEDIA_MM("iso_sra3_320x450mm", "iso-sra3", "SRA3", 320, 450), 172 _PWG_MEDIA_MM("iso_ra2_430x610mm", "iso-ra2", "RA2", 430, 610), 173 _PWG_MEDIA_MM("iso_sra2_450x640mm", "iso-sra2", "SRA2", 450, 640), 174 _PWG_MEDIA_MM("iso_ra1_610x860mm", "iso-ra1", "RA1", 610, 860), 175 _PWG_MEDIA_MM("iso_sra1_640x900mm", "iso-sra1", "SRA1", 640, 900), 176 _PWG_MEDIA_MM("iso_ra0_860x1220mm", "iso-ra0", "RA0", 860, 1220), 177 _PWG_MEDIA_MM("iso_sra0_900x1280mm", "iso-sra0", "SRA0", 900, 1280), 178 179 /* Japanese Standard Sheet Media Sizes */ 180 _PWG_MEDIA_MM("jis_b10_32x45mm", "jis-b10", "B10", 32, 45), 181 _PWG_MEDIA_MM("jis_b9_45x64mm", "jis-b9", "B9", 45, 64), 182 _PWG_MEDIA_MM("jis_b8_64x91mm", "jis-b8", "B8", 64, 91), 183 _PWG_MEDIA_MM("jis_b7_91x128mm", "jis-b7", "B7", 91, 128), 184 _PWG_MEDIA_MM("jis_b6_128x182mm", "jis-b6", "B6", 128, 182), 185 _PWG_MEDIA_MM("jis_b5_182x257mm", "jis-b5", "B5", 182, 257), 186 _PWG_MEDIA_MM("jis_b4_257x364mm", "jis-b4", "B4", 257, 364), 187 _PWG_MEDIA_MM("jis_b3_364x515mm", "jis-b3", "B3", 364, 515), 188 _PWG_MEDIA_MM("jis_b2_515x728mm", "jis-b2", "B2", 515, 728), 189 _PWG_MEDIA_MM("jis_b1_728x1030mm", "jis-b1", "B1", 728, 1030), 190 _PWG_MEDIA_MM("jis_b0_1030x1456mm", "jis-b0", "B0", 1030, 1456), 191 _PWG_MEDIA_MM("jis_exec_216x330mm", NULL, "216x330mm", 216, 330), 192 _PWG_MEDIA_MM("jpn_kaku1_270x382mm", NULL, "EnvKaku1", 270, 382), 193 _PWG_MEDIA_MM("jpn_kaku2_240x332mm", NULL, "EnvKaku2", 240, 332), 194 _PWG_MEDIA_MM("jpn_kaku3_216x277mm", NULL, "EnvKaku3", 216, 277), 195 _PWG_MEDIA_MM("jpn_kaku4_197x267mm", NULL, "EnvKaku4", 197, 267), 196 _PWG_MEDIA_MM("jpn_kaku5_190x240mm", NULL, "EnvKaku5", 190, 240), 197 _PWG_MEDIA_MM("jpn_kaku7_142x205mm", NULL, "EnvKaku7", 142, 205), 198 _PWG_MEDIA_MM("jpn_kaku8_119x197mm", NULL, "EnvKaku8", 119, 197), 199 _PWG_MEDIA_MM("jpn_chou4_90x205mm", NULL, "EnvChou4", 90, 205), 200 _PWG_MEDIA_MM("jpn_hagaki_100x148mm", NULL, "Postcard", 100, 148), 201 _PWG_MEDIA_MM("jpn_you4_105x235mm", NULL, "EnvYou4", 105, 235), 202 _PWG_MEDIA_MM("jpn_you6_98x190mm", NULL, "EnvYou6", 98, 190), 203 _PWG_MEDIA_MM("jpn_chou2_111.1x146mm", NULL, NULL, 111.1, 146), 204 _PWG_MEDIA_MM("jpn_chou3_120x235mm", NULL, "EnvChou3", 120, 235), 205 _PWG_MEDIA_MM("jpn_chou40_90x225mm", NULL, "EnvChou40", 90, 225), 206 _PWG_MEDIA_MM("jpn_oufuku_148x200mm", NULL, "DoublePostcardRotated", 148, 200), 207 _PWG_MEDIA_MM("jpn_kahu_240x322.1mm", NULL, "240x322mm", 240, 322.1), 208 209 /* Chinese Standard Sheet Media Sizes */ 210 _PWG_MEDIA_MM("prc_32k_97x151mm", NULL, "PRC32K", 97, 151), 211 _PWG_MEDIA_MM("prc_1_102x165mm", NULL, "EnvPRC1", 102, 165), 212 _PWG_MEDIA_MM("prc_2_102x176mm", NULL, "EnvPRC2", 102, 176), 213 _PWG_MEDIA_MM("prc_4_110x208mm", NULL, "EnvPRC4", 110, 208), 214 _PWG_MEDIA_MM("prc_8_120x309mm", NULL, "EnvPRC8", 120, 309), 215 _PWG_MEDIA_MM("prc_6_120x320mm", NULL, NULL, 120, 320), 216 _PWG_MEDIA_MM("prc_16k_146x215mm", NULL, "PRC16K", 146, 215), 217 _PWG_MEDIA_MM("prc_7_160x230mm", NULL, "EnvPRC7", 160, 230), 218 _PWG_MEDIA_MM("om_juuro-ku-kai_198x275mm", NULL, "198x275mm", 198, 275), 219 _PWG_MEDIA_MM("om_pa-kai_267x389mm", NULL, "267x389mm", 267, 389), 220 _PWG_MEDIA_MM("om_dai-pa-kai_275x395mm", NULL, "275x395mm", 275, 395), 221 222 /* Chinese Standard Sheet Media Inch Sizes */ 223 _PWG_MEDIA_IN("roc_16k_7.75x10.75in", NULL, "roc16k", 7.75, 10.75), 224 _PWG_MEDIA_IN("roc_8k_10.75x15.5in", NULL, "roc8k", 10.75, 15.5), 225 226 /* Other English Standard Sheet Media Sizes */ 227 _PWG_MEDIA_IN("oe_photo-l_3.5x5in", NULL, "3.5x5", 3.5, 5), 228 229 /* Other Metric Standard Sheet Media Sizes */ 230 _PWG_MEDIA_MM("om_small-photo_100x150mm", NULL, "100x150mm", 100, 150), 231 _PWG_MEDIA_MM("om_italian_110x230mm", NULL, "EnvItalian", 110, 230), 232 _PWG_MEDIA_MM("om_large-photo_200x300", NULL, "200x300mm", 200, 300), 233 _PWG_MEDIA_MM("om_folio_210x330mm", "folio", "Folio", 210, 330), 234 _PWG_MEDIA_MM("om_folio-sp_215x315mm", NULL, "FolioSP", 215, 315), 235 _PWG_MEDIA_MM("om_invite_220x220mm", NULL, "EnvInvite", 220, 220), 236 _PWG_MEDIA_MM("om_small-photo_100x200mm", NULL, "100x200mm", 100, 200), 237 238 /* Disc Sizes */ 239 _PWG_MEDIA_MM("disc_standard_40x118mm", NULL, "Disc", 118, 118) 240 }; 241 242 243 /* 244 * 'pwgFormatSizeName()' - Generate a PWG self-describing media size name. 245 * 246 * This function generates a PWG self-describing media size name of the form 247 * "prefix_name_WIDTHxLENGTHunits". The prefix is typically "custom" or "roll" 248 * for user-supplied sizes but can also be "disc", "iso", "jis", "jpn", "na", 249 * "oe", "om", "prc", or "roc". A value of @code NULL@ automatically chooses 250 * "oe" or "om" depending on the units. 251 * 252 * The size name may only contain lowercase letters, numbers, "-", and ".". If 253 * @code NULL@ is passed, the size name will contain the formatted dimensions. 254 * 255 * The width and length are specified in hundredths of millimeters, equivalent 256 * to 1/100000th of a meter or 1/2540th of an inch. The width, length, and 257 * units used for the generated size name are calculated automatically if the 258 * units string is @code NULL@, otherwise inches ("in") or millimeters ("mm") 259 * are used. 260 * 261 * @since CUPS 1.7/macOS 10.9@ 262 */ 263 264 int /* O - 1 on success, 0 on failure */ 265 pwgFormatSizeName(char *keyword, /* I - Keyword buffer */ 266 size_t keysize, /* I - Size of keyword buffer */ 267 const char *prefix, /* I - Prefix for PWG size or @code NULL@ for automatic */ 268 const char *name, /* I - Size name or @code NULL@ */ 269 int width, /* I - Width of page in 2540ths */ 270 int length, /* I - Length of page in 2540ths */ 271 const char *units) /* I - Units - "in", "mm", or @code NULL@ for automatic */ 272 { 273 char usize[12 + 1 + 12 + 3], /* Unit size: NNNNNNNNNNNNxNNNNNNNNNNNNuu */ 274 *uptr; /* Pointer into unit size */ 275 char *(*format)(char *, size_t, int); 276 /* Formatting function */ 277 278 279 /* 280 * Range check input... 281 */ 282 283 DEBUG_printf(("pwgFormatSize(keyword=%p, keysize=" CUPS_LLFMT ", prefix=\"%s\", name=\"%s\", width=%d, length=%d, units=\"%s\")", (void *)keyword, CUPS_LLCAST keysize, prefix, name, width, length, units)); 284 285 if (keyword) 286 *keyword = '\0'; 287 288 if (!keyword || keysize < 32 || width < 0 || length < 0 || 289 (units && strcmp(units, "in") && strcmp(units, "mm"))) 290 { 291 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Invalid media name arguments."), 292 1); 293 return (0); 294 } 295 296 if (name) 297 { 298 /* 299 * Validate name... 300 */ 301 302 const char *nameptr; /* Pointer into name */ 303 304 for (nameptr = name; *nameptr; nameptr ++) 305 if (!(*nameptr >= 'a' && *nameptr <= 'z') && 306 !(*nameptr >= '0' && *nameptr <= '9') && 307 *nameptr != '.' && *nameptr != '-') 308 { 309 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, 310 _("Invalid media name arguments."), 1); 311 return (0); 312 } 313 } 314 else 315 name = usize; 316 317 if (prefix && !strcmp(prefix, "disc")) 318 width = 4000; /* Disc sizes use hardcoded 40mm inner diameter */ 319 320 if (!units) 321 { 322 if ((width % 635) == 0 && (length % 635) == 0) 323 { 324 /* 325 * Use inches since the size is a multiple of 1/4 inch. 326 */ 327 328 units = "in"; 329 } 330 else 331 { 332 /* 333 * Use millimeters since the size is not a multiple of 1/4 inch. 334 */ 335 336 units = "mm"; 337 } 338 } 339 340 if (!strcmp(units, "in")) 341 { 342 format = pwg_format_inches; 343 344 if (!prefix) 345 prefix = "oe"; 346 } 347 else 348 { 349 format = pwg_format_millimeters; 350 351 if (!prefix) 352 prefix = "om"; 353 } 354 355 /* 356 * Format the size string... 357 */ 358 359 uptr = usize; 360 (*format)(uptr, sizeof(usize) - (size_t)(uptr - usize), width); 361 uptr += strlen(uptr); 362 *uptr++ = 'x'; 363 (*format)(uptr, sizeof(usize) - (size_t)(uptr - usize), length); 364 uptr += strlen(uptr); 365 366 /* 367 * Safe because usize can hold up to 12 + 1 + 12 + 4 bytes. 368 */ 369 370 memcpy(uptr, units, 3); 371 372 /* 373 * Format the name... 374 */ 375 376 snprintf(keyword, keysize, "%s_%s_%s", prefix, name, usize); 377 378 return (1); 379 } 380 381 382 /* 383 * 'pwgInitSize()' - Initialize a pwg_size_t structure using IPP Job Template 384 * attributes. 385 * 386 * This function initializes a pwg_size_t structure from an IPP "media" or 387 * "media-col" attribute in the specified IPP message. 0 is returned if neither 388 * attribute is found in the message or the values are not valid. 389 * 390 * The "margins_set" variable is initialized to 1 if any "media-xxx-margin" 391 * member attribute was specified in the "media-col" Job Template attribute, 392 * otherwise it is initialized to 0. 393 * 394 * @since CUPS 1.7/macOS 10.9@ 395 */ 396 397 int /* O - 1 if size was initialized, 0 otherwise */ 398 pwgInitSize(pwg_size_t *size, /* I - Size to initialize */ 399 ipp_t *job, /* I - Job template attributes */ 400 int *margins_set) /* O - 1 if margins were set, 0 otherwise */ 401 { 402 ipp_attribute_t *media, /* media attribute */ 403 *media_bottom_margin, /* media-bottom-margin member attribute */ 404 *media_col, /* media-col attribute */ 405 *media_left_margin, /* media-left-margin member attribute */ 406 *media_right_margin, /* media-right-margin member attribute */ 407 *media_size, /* media-size member attribute */ 408 *media_top_margin, /* media-top-margin member attribute */ 409 *x_dimension, /* x-dimension member attribute */ 410 *y_dimension; /* y-dimension member attribute */ 411 pwg_media_t *pwg; /* PWG media value */ 412 413 414 /* 415 * Range check input... 416 */ 417 418 if (!size || !job || !margins_set) 419 return (0); 420 421 /* 422 * Look for media-col and then media... 423 */ 424 425 memset(size, 0, sizeof(pwg_size_t)); 426 *margins_set = 0; 427 428 if ((media_col = ippFindAttribute(job, "media-col", 429 IPP_TAG_BEGIN_COLLECTION)) != NULL) 430 { 431 /* 432 * Got media-col, look for media-size member attribute... 433 */ 434 435 if ((media_size = ippFindAttribute(media_col->values[0].collection, 436 "media-size", 437 IPP_TAG_BEGIN_COLLECTION)) != NULL) 438 { 439 /* 440 * Got media-size, look for x-dimension and y-dimension member 441 * attributes... 442 */ 443 444 x_dimension = ippFindAttribute(media_size->values[0].collection, 445 "x-dimension", IPP_TAG_INTEGER); 446 y_dimension = ippFindAttribute(media_size->values[0].collection, 447 "y-dimension", IPP_TAG_INTEGER); 448 449 if (x_dimension && y_dimension) 450 { 451 size->width = x_dimension->values[0].integer; 452 size->length = y_dimension->values[0].integer; 453 } 454 else if (!x_dimension) 455 { 456 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, 457 _("Missing x-dimension in media-size."), 1); 458 return (0); 459 } 460 else if (!y_dimension) 461 { 462 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, 463 _("Missing y-dimension in media-size."), 1); 464 return (0); 465 } 466 } 467 else 468 { 469 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Missing media-size in media-col."), 470 1); 471 return (0); 472 } 473 474 /* media-*-margin */ 475 media_bottom_margin = ippFindAttribute(media_col->values[0].collection, 476 "media-bottom-margin", 477 IPP_TAG_INTEGER); 478 media_left_margin = ippFindAttribute(media_col->values[0].collection, 479 "media-left-margin", 480 IPP_TAG_INTEGER); 481 media_right_margin = ippFindAttribute(media_col->values[0].collection, 482 "media-right-margin", 483 IPP_TAG_INTEGER); 484 media_top_margin = ippFindAttribute(media_col->values[0].collection, 485 "media-top-margin", 486 IPP_TAG_INTEGER); 487 if (media_bottom_margin && media_left_margin && media_right_margin && 488 media_top_margin) 489 { 490 *margins_set = 1; 491 size->bottom = media_bottom_margin->values[0].integer; 492 size->left = media_left_margin->values[0].integer; 493 size->right = media_right_margin->values[0].integer; 494 size->top = media_top_margin->values[0].integer; 495 } 496 } 497 else 498 { 499 if ((media = ippFindAttribute(job, "media", IPP_TAG_NAME)) == NULL) 500 if ((media = ippFindAttribute(job, "media", IPP_TAG_KEYWORD)) == NULL) 501 if ((media = ippFindAttribute(job, "PageSize", IPP_TAG_NAME)) == NULL) 502 media = ippFindAttribute(job, "PageRegion", IPP_TAG_NAME); 503 504 if (media && media->values[0].string.text) 505 { 506 const char *name = media->values[0].string.text; 507 /* Name string */ 508 509 if ((pwg = pwgMediaForPWG(name)) == NULL) 510 { 511 /* 512 * Not a PWG name, try a legacy name... 513 */ 514 515 if ((pwg = pwgMediaForLegacy(name)) == NULL) 516 { 517 /* 518 * Not a legacy name, try a PPD name... 519 */ 520 521 const char *suffix; /* Suffix on media string */ 522 523 pwg = pwgMediaForPPD(name); 524 if (pwg && 525 (suffix = name + strlen(name) - 10 /* .FullBleed */) > name && 526 !_cups_strcasecmp(suffix, ".FullBleed")) 527 { 528 /* 529 * Indicate that margins are set with the default values of 0. 530 */ 531 532 *margins_set = 1; 533 } 534 } 535 } 536 537 if (pwg) 538 { 539 size->width = pwg->width; 540 size->length = pwg->length; 541 } 542 else 543 { 544 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unsupported media value."), 1); 545 return (0); 546 } 547 } 548 else 549 { 550 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Missing media or media-col."), 1); 551 return (0); 552 } 553 } 554 555 return (1); 556 } 557 558 559 /* 560 * 'pwgMediaForLegacy()' - Find a PWG media size by ISO/IPP legacy name. 561 * 562 * The "name" argument specifies the legacy ISO media size name, for example 563 * "iso-a4" or "na-letter". 564 * 565 * @since CUPS 1.7/macOS 10.9@ 566 */ 567 568 pwg_media_t * /* O - Matching size or NULL */ 569 pwgMediaForLegacy(const char *legacy) /* I - Legacy size name */ 570 { 571 pwg_media_t key; /* Search key */ 572 _cups_globals_t *cg = _cupsGlobals(); /* Global data */ 573 574 575 /* 576 * Range check input... 577 */ 578 579 if (!legacy) 580 return (NULL); 581 582 /* 583 * Build the lookup table for PWG names as needed... 584 */ 585 586 if (!cg->leg_size_lut) 587 { 588 int i; /* Looping var */ 589 pwg_media_t *size; /* Current size */ 590 591 cg->leg_size_lut = cupsArrayNew((cups_array_func_t)pwg_compare_legacy, 592 NULL); 593 594 for (i = (int)(sizeof(cups_pwg_media) / sizeof(cups_pwg_media[0])), 595 size = (pwg_media_t *)cups_pwg_media; 596 i > 0; 597 i --, size ++) 598 if (size->legacy) 599 cupsArrayAdd(cg->leg_size_lut, size); 600 } 601 602 /* 603 * Lookup the name... 604 */ 605 606 key.legacy = legacy; 607 return ((pwg_media_t *)cupsArrayFind(cg->leg_size_lut, &key)); 608 } 609 610 611 /* 612 * 'pwgMediaForPPD()' - Find a PWG media size by Adobe PPD name. 613 * 614 * The "ppd" argument specifies an Adobe page size name as defined in Table B.1 615 * of the Adobe PostScript Printer Description File Format Specification Version 616 * 4.3. 617 * 618 * If the name is non-standard, the returned PWG media size is stored in 619 * thread-local storage and is overwritten by each call to the function in the 620 * thread. Custom names can be of the form "Custom.WIDTHxLENGTH[units]" or 621 * "WIDTHxLENGTH[units]". 622 * 623 * @since CUPS 1.7/macOS 10.9@ 624 */ 625 626 pwg_media_t * /* O - Matching size or NULL */ 627 pwgMediaForPPD(const char *ppd) /* I - PPD size name */ 628 { 629 pwg_media_t key, /* Search key */ 630 *size; /* Matching size */ 631 _cups_globals_t *cg = _cupsGlobals(); /* Global data */ 632 633 634 /* 635 * Range check input... 636 */ 637 638 if (!ppd) 639 return (NULL); 640 641 /* 642 * Build the lookup table for PWG names as needed... 643 */ 644 645 if (!cg->ppd_size_lut) 646 { 647 int i; /* Looping var */ 648 649 cg->ppd_size_lut = cupsArrayNew((cups_array_func_t)pwg_compare_ppd, NULL); 650 651 for (i = (int)(sizeof(cups_pwg_media) / sizeof(cups_pwg_media[0])), 652 size = (pwg_media_t *)cups_pwg_media; 653 i > 0; 654 i --, size ++) 655 if (size->ppd) 656 cupsArrayAdd(cg->ppd_size_lut, size); 657 } 658 659 /* 660 * Lookup the name... 661 */ 662 663 key.ppd = ppd; 664 if ((size = (pwg_media_t *)cupsArrayFind(cg->ppd_size_lut, &key)) == NULL) 665 { 666 /* 667 * See if the name is of the form: 668 * 669 * [Custom.]WIDTHxLENGTH[.FullBleed] - Size in points/inches [borderless] 670 * [Custom.]WIDTHxLENGTHcm[.FullBleed] - Size in centimeters [borderless] 671 * [Custom.]WIDTHxLENGTHft[.FullBleed] - Size in feet [borderless] 672 * [Custom.]WIDTHxLENGTHin[.FullBleed] - Size in inches [borderless] 673 * [Custom.]WIDTHxLENGTHm[.FullBleed] - Size in meters [borderless] 674 * [Custom.]WIDTHxLENGTHmm[.FullBleed] - Size in millimeters [borderless] 675 * [Custom.]WIDTHxLENGTHpt[.FullBleed] - Size in points [borderless] 676 */ 677 678 int w, l, /* Width and length of page */ 679 numer, /* Unit scaling factor */ 680 denom; /* ... */ 681 char *ptr; /* Pointer into name */ 682 const char *units; /* Pointer to units */ 683 int custom; /* Custom page size? */ 684 685 686 if (!_cups_strncasecmp(ppd, "Custom.", 7)) 687 { 688 custom = 1; 689 numer = 2540; 690 denom = 72; 691 ptr = (char *)ppd + 7; 692 } 693 else 694 { 695 custom = 0; 696 numer = 2540; 697 denom = 1; 698 ptr = (char *)ppd; 699 } 700 701 /* 702 * Find any units in the size... 703 */ 704 705 units = strchr(ptr, '.'); 706 while (units && isdigit(units[1] & 255)) 707 units = strchr(units + 1, '.'); 708 709 if (units) 710 units -= 2; 711 else 712 units = ptr + strlen(ptr) - 2; 713 714 if (units > ptr) 715 { 716 if (isdigit(*units & 255) || *units == '.') 717 units ++; 718 719 if (!_cups_strncasecmp(units, "cm", 2)) 720 { 721 numer = 1000; 722 denom = 1; 723 } 724 else if (!_cups_strncasecmp(units, "ft", 2)) 725 { 726 numer = 2540 * 12; 727 denom = 1; 728 } 729 else if (!_cups_strncasecmp(units, "in", 2)) 730 { 731 numer = 2540; 732 denom = 1; 733 } 734 else if (!_cups_strncasecmp(units, "mm", 2)) 735 { 736 numer = 100; 737 denom = 1; 738 } 739 else if (*units == 'm' || *units == 'M') 740 { 741 numer = 100000; 742 denom = 1; 743 } 744 else if (!_cups_strncasecmp(units, "pt", 2)) 745 { 746 numer = 2540; 747 denom = 72; 748 } 749 } 750 751 w = pwg_scan_measurement(ptr, &ptr, numer, denom); 752 753 if (ptr && ptr > ppd && *ptr == 'x') 754 { 755 l = pwg_scan_measurement(ptr + 1, &ptr, numer, denom); 756 757 if (ptr) 758 { 759 /* 760 * Not a standard size; convert it to a PWG custom name of the form: 761 * 762 * [oe|om]_WIDTHxHEIGHTuu_WIDTHxHEIGHTuu 763 */ 764 765 char wstr[32], lstr[32]; /* Width and length as strings */ 766 767 size = &(cg->pwg_media); 768 size->width = w; 769 size->length = l; 770 size->pwg = cg->pwg_name; 771 772 pwgFormatSizeName(cg->pwg_name, sizeof(cg->pwg_name), 773 custom ? "custom" : NULL, custom ? ppd + 7 : NULL, 774 size->width, size->length, NULL); 775 776 if ((w % 635) == 0 && (l % 635) == 0) 777 snprintf(cg->ppd_name, sizeof(cg->ppd_name), "%sx%s", pwg_format_inches(wstr, sizeof(wstr), w), pwg_format_inches(lstr, sizeof(lstr), l)); 778 else 779 snprintf(cg->ppd_name, sizeof(cg->ppd_name), "%sx%smm", pwg_format_millimeters(wstr, sizeof(wstr), w), pwg_format_millimeters(lstr, sizeof(lstr), l)); 780 size->ppd = cg->ppd_name; 781 } 782 } 783 } 784 785 return (size); 786 } 787 788 789 /* 790 * 'pwgMediaForPWG()' - Find a PWG media size by 5101.1 self-describing name. 791 * 792 * The "pwg" argument specifies a self-describing media size name of the form 793 * "prefix_name_WIDTHxLENGTHunits" as defined in PWG 5101.1. 794 * 795 * If the name is non-standard, the returned PWG media size is stored in 796 * thread-local storage and is overwritten by each call to the function in the 797 * thread. 798 * 799 * @since CUPS 1.7/macOS 10.9@ 800 */ 801 802 pwg_media_t * /* O - Matching size or NULL */ 803 pwgMediaForPWG(const char *pwg) /* I - PWG size name */ 804 { 805 char *ptr; /* Pointer into name */ 806 pwg_media_t key, /* Search key */ 807 *size; /* Matching size */ 808 _cups_globals_t *cg = _cupsGlobals(); /* Global data */ 809 810 811 /* 812 * Range check input... 813 */ 814 815 if (!pwg) 816 return (NULL); 817 818 /* 819 * Build the lookup table for PWG names as needed... 820 */ 821 822 if (!cg->pwg_size_lut) 823 { 824 int i; /* Looping var */ 825 826 cg->pwg_size_lut = cupsArrayNew((cups_array_func_t)pwg_compare_pwg, NULL); 827 828 for (i = (int)(sizeof(cups_pwg_media) / sizeof(cups_pwg_media[0])), 829 size = (pwg_media_t *)cups_pwg_media; 830 i > 0; 831 i --, size ++) 832 cupsArrayAdd(cg->pwg_size_lut, size); 833 } 834 835 /* 836 * Lookup the name... 837 */ 838 839 key.pwg = pwg; 840 if ((size = (pwg_media_t *)cupsArrayFind(cg->pwg_size_lut, &key)) == NULL && 841 (ptr = (char *)strchr(pwg, '_')) != NULL && 842 (ptr = (char *)strchr(ptr + 1, '_')) != NULL) 843 { 844 /* 845 * Try decoding the self-describing name of the form: 846 * 847 * class_name_WWWxHHHin 848 * class_name_WWWxHHHmm 849 */ 850 851 int w, l; /* Width and length of page */ 852 int numer; /* Scale factor for units */ 853 const char *units = ptr + strlen(ptr) - 2; 854 /* Units from size */ 855 856 ptr ++; 857 858 if (units >= ptr && !strcmp(units, "in")) 859 numer = 2540; 860 else 861 numer = 100; 862 863 w = pwg_scan_measurement(ptr, &ptr, numer, 1); 864 865 if (ptr && *ptr == 'x') 866 { 867 l = pwg_scan_measurement(ptr + 1, &ptr, numer, 1); 868 869 if (ptr) 870 { 871 char wstr[32], lstr[32]; /* Width and length strings */ 872 873 if (!strncmp(pwg, "disc_", 5)) 874 w = l; /* Make the media size OUTERxOUTER */ 875 876 size = &(cg->pwg_media); 877 size->width = w; 878 size->length = l; 879 880 strlcpy(cg->pwg_name, pwg, sizeof(cg->pwg_name)); 881 size->pwg = cg->pwg_name; 882 883 if (numer == 100) 884 snprintf(cg->ppd_name, sizeof(cg->ppd_name), "%sx%smm", pwg_format_millimeters(wstr, sizeof(wstr), w), pwg_format_millimeters(lstr, sizeof(lstr), l)); 885 else 886 snprintf(cg->ppd_name, sizeof(cg->ppd_name), "%sx%s", pwg_format_inches(wstr, sizeof(wstr), w), pwg_format_inches(lstr, sizeof(lstr), l)); 887 size->ppd = cg->ppd_name; 888 } 889 } 890 } 891 892 return (size); 893 } 894 895 896 /* 897 * 'pwgMediaForSize()' - Get the PWG media size for the given dimensions. 898 * 899 * The "width" and "length" are in hundredths of millimeters, equivalent to 900 * 1/100000th of a meter or 1/2540th of an inch. 901 * 902 * If the dimensions are non-standard, the returned PWG media size is stored in 903 * thread-local storage and is overwritten by each call to the function in the 904 * thread. 905 * 906 * @since CUPS 1.7/macOS 10.9@ 907 */ 908 909 pwg_media_t * /* O - PWG media name */ 910 pwgMediaForSize(int width, /* I - Width in hundredths of millimeters */ 911 int length) /* I - Length in hundredths of millimeters */ 912 { 913 /* 914 * Adobe uses a size matching algorithm with an epsilon of 5 points, which 915 * is just about 176/2540ths... 916 */ 917 918 return (_pwgMediaNearSize(width, length, 176)); 919 } 920 921 922 /* 923 * '_pwgMediaNearSize()' - Get the PWG media size within the given tolerance. 924 */ 925 926 pwg_media_t * /* O - PWG media name */ 927 _pwgMediaNearSize(int width, /* I - Width in hundredths of millimeters */ 928 int length, /* I - Length in hundredths of millimeters */ 929 int epsilon) /* I - Match within this tolernace. PWG units */ 930 { 931 int i; /* Looping var */ 932 pwg_media_t *media, /* Current media */ 933 *best_media = NULL; /* Best match */ 934 int dw, dl, /* Difference in width and length */ 935 best_dw = 999, /* Best difference in width and length */ 936 best_dl = 999; 937 char wstr[32], lstr[32]; /* Width and length as strings */ 938 _cups_globals_t *cg = _cupsGlobals(); /* Global data */ 939 940 941 /* 942 * Range check input... 943 */ 944 945 if (width <= 0 || length <= 0) 946 return (NULL); 947 948 /* 949 * Look for a standard size... 950 */ 951 952 for (i = (int)(sizeof(cups_pwg_media) / sizeof(cups_pwg_media[0])), 953 media = (pwg_media_t *)cups_pwg_media; 954 i > 0; 955 i --, media ++) 956 { 957 958 dw = abs(media->width - width); 959 dl = abs(media->length - length); 960 961 if (!dw && !dl) 962 return (media); 963 else if (dw <= epsilon && dl <= epsilon) 964 { 965 if (dw <= best_dw && dl <= best_dl) 966 { 967 best_media = media; 968 best_dw = dw; 969 best_dl = dl; 970 } 971 } 972 } 973 974 if (best_media) 975 return (best_media); 976 977 /* 978 * Not a standard size; convert it to a PWG custom name of the form: 979 * 980 * custom_WIDTHxHEIGHTuu_WIDTHxHEIGHTuu 981 */ 982 983 pwgFormatSizeName(cg->pwg_name, sizeof(cg->pwg_name), "custom", NULL, width, 984 length, NULL); 985 986 cg->pwg_media.pwg = cg->pwg_name; 987 cg->pwg_media.width = width; 988 cg->pwg_media.length = length; 989 990 if ((width % 635) == 0 && (length % 635) == 0) 991 snprintf(cg->ppd_name, sizeof(cg->ppd_name), "%sx%s", pwg_format_inches(wstr, sizeof(wstr), width), pwg_format_inches(lstr, sizeof(lstr), length)); 992 else 993 snprintf(cg->ppd_name, sizeof(cg->ppd_name), "%sx%smm", pwg_format_millimeters(wstr, sizeof(wstr), width), pwg_format_millimeters(lstr, sizeof(lstr), length)); 994 cg->pwg_media.ppd = cg->ppd_name; 995 996 return (&(cg->pwg_media)); 997 } 998 999 1000 /* 1001 * '_pwgMediaTable()' - Return the internal media size table. 1002 */ 1003 1004 const pwg_media_t * /* O - Pointer to first entry */ 1005 _pwgMediaTable(size_t *num_media) /* O - Number of entries */ 1006 { 1007 *num_media = sizeof(cups_pwg_media) / sizeof(cups_pwg_media[0]); 1008 1009 return (cups_pwg_media); 1010 } 1011 1012 1013 /* 1014 * 'pwg_compare_legacy()' - Compare two sizes using the legacy names. 1015 */ 1016 1017 static int /* O - Result of comparison */ 1018 pwg_compare_legacy(pwg_media_t *a, /* I - First size */ 1019 pwg_media_t *b) /* I - Second size */ 1020 { 1021 return (strcmp(a->legacy, b->legacy)); 1022 } 1023 1024 1025 /* 1026 * 'pwg_compare_ppd()' - Compare two sizes using the PPD names. 1027 */ 1028 1029 static int /* O - Result of comparison */ 1030 pwg_compare_ppd(pwg_media_t *a, /* I - First size */ 1031 pwg_media_t *b) /* I - Second size */ 1032 { 1033 return (strcmp(a->ppd, b->ppd)); 1034 } 1035 1036 1037 /* 1038 * 'pwg_compare_pwg()' - Compare two sizes using the PWG names. 1039 */ 1040 1041 static int /* O - Result of comparison */ 1042 pwg_compare_pwg(pwg_media_t *a, /* I - First size */ 1043 pwg_media_t *b) /* I - Second size */ 1044 { 1045 return (strcmp(a->pwg, b->pwg)); 1046 } 1047 1048 1049 /* 1050 * 'pwg_format_inches()' - Convert and format PWG units as inches. 1051 */ 1052 1053 static char * /* O - String */ 1054 pwg_format_inches(char *buf, /* I - Buffer */ 1055 size_t bufsize, /* I - Size of buffer */ 1056 int val) /* I - Value in hundredths of millimeters */ 1057 { 1058 int thousandths, /* Thousandths of inches */ 1059 integer, /* Integer portion */ 1060 fraction; /* Fractional portion */ 1061 1062 1063 /* 1064 * Convert hundredths of millimeters to thousandths of inches and round to 1065 * the nearest thousandth. 1066 */ 1067 1068 thousandths = (val * 1000 + 1270) / 2540; 1069 integer = thousandths / 1000; 1070 fraction = thousandths % 1000; 1071 1072 /* 1073 * Format as a pair of integers (avoids locale stuff), avoiding trailing 1074 * zeros... 1075 */ 1076 1077 if (fraction == 0) 1078 snprintf(buf, bufsize, "%d", integer); 1079 else if (fraction % 10) 1080 snprintf(buf, bufsize, "%d.%03d", integer, fraction); 1081 else if (fraction % 100) 1082 snprintf(buf, bufsize, "%d.%02d", integer, fraction / 10); 1083 else 1084 snprintf(buf, bufsize, "%d.%01d", integer, fraction / 100); 1085 1086 return (buf); 1087 } 1088 1089 1090 /* 1091 * 'pwg_format_millimeters()' - Convert and format PWG units as millimeters. 1092 */ 1093 1094 static char * /* O - String */ 1095 pwg_format_millimeters(char *buf, /* I - Buffer */ 1096 size_t bufsize, /* I - Size of buffer */ 1097 int val) /* I - Value in hundredths of millimeters */ 1098 { 1099 int integer, /* Integer portion */ 1100 fraction; /* Fractional portion */ 1101 1102 1103 /* 1104 * Convert hundredths of millimeters to integer and fractional portions. 1105 */ 1106 1107 integer = val / 100; 1108 fraction = val % 100; 1109 1110 /* 1111 * Format as a pair of integers (avoids locale stuff), avoiding trailing 1112 * zeros... 1113 */ 1114 1115 if (fraction == 0) 1116 snprintf(buf, bufsize, "%d", integer); 1117 else if (fraction % 10) 1118 snprintf(buf, bufsize, "%d.%02d", integer, fraction); 1119 else 1120 snprintf(buf, bufsize, "%d.%01d", integer, fraction / 10); 1121 1122 return (buf); 1123 } 1124 1125 1126 /* 1127 * 'pwg_scan_measurement()' - Scan a measurement in inches or millimeters. 1128 * 1129 * The "factor" argument specifies the scale factor for the units to convert to 1130 * hundredths of millimeters. The returned value is NOT rounded but is an 1131 * exact conversion of the fraction value (no floating point is used). 1132 */ 1133 1134 static int /* O - Hundredths of millimeters */ 1135 pwg_scan_measurement( 1136 const char *buf, /* I - Number string */ 1137 char **bufptr, /* O - First byte after the number */ 1138 int numer, /* I - Numerator from units */ 1139 int denom) /* I - Denominator from units */ 1140 { 1141 int value = 0, /* Measurement value */ 1142 fractional = 0, /* Fractional value */ 1143 divisor = 1, /* Fractional divisor */ 1144 digits = 10 * numer * denom; /* Maximum fractional value to read */ 1145 1146 1147 /* 1148 * Scan integer portion... 1149 */ 1150 1151 while (*buf >= '0' && *buf <= '9') 1152 value = value * 10 + (*buf++) - '0'; 1153 1154 if (*buf == '.') 1155 { 1156 /* 1157 * Scan fractional portion... 1158 */ 1159 1160 buf ++; 1161 1162 while (divisor < digits && *buf >= '0' && *buf <= '9') 1163 { 1164 fractional = fractional * 10 + (*buf++) - '0'; 1165 divisor *= 10; 1166 } 1167 1168 /* 1169 * Skip trailing digits that won't contribute... 1170 */ 1171 1172 while (*buf >= '0' && *buf <= '9') 1173 buf ++; 1174 } 1175 1176 if (bufptr) 1177 *bufptr = (char *)buf; 1178 1179 return (value * numer / denom + fractional * numer / denom / divisor); 1180 } 1181