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