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