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