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