Coverage Report

Created: 2026-09-02 06:27

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