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