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