Coverage Report

Created: 2026-12-01 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cups/cups/ppd-page.c
Line
Count
Source
1
/*
2
 * Page size functions for CUPS.
3
 *
4
 * Copyright © 2020-2025 by OpenPrinting.
5
 * Copyright © 2007-2015 by Apple Inc.
6
 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
7
 *
8
 * Licensed under Apache License v2.0.  See the file "LICENSE" for more
9
 * information.
10
 *
11
 * PostScript is a trademark of Adobe Systems, Inc.
12
 */
13
14
#include "string-private.h"
15
#include "debug-internal.h"
16
#include "ppd.h"
17
18
19
/*
20
 * 'ppdPageSize()' - Get the page size record for the named size.
21
 */
22
23
ppd_size_t *        /* O - Size record for page or NULL */
24
ppdPageSize(ppd_file_t *ppd,    /* I - PPD file record */
25
            const char *name)   /* I - Size name */
26
21.9k
{
27
21.9k
  int   i;      /* Looping var */
28
21.9k
  ppd_size_t  *size;      /* Current page size */
29
21.9k
  double  w, l;     /* Width and length of page */
30
21.9k
  char    *nameptr;   /* Pointer into name */
31
21.9k
  struct lconv  *loc;     /* Locale data */
32
21.9k
  ppd_coption_t *coption;   /* Custom option for page size */
33
21.9k
  ppd_cparam_t  *cparam;    /* Custom option parameter */
34
35
36
21.9k
  DEBUG_printf("2ppdPageSize(ppd=%p, name=\"%s\")", (void *)ppd, name);
37
38
21.9k
  if (!ppd)
39
0
  {
40
0
    DEBUG_puts("3ppdPageSize: Bad PPD pointer, returning NULL...");
41
0
    return (NULL);
42
0
  }
43
44
21.9k
  if (name)
45
14.2k
  {
46
14.2k
    if (!strncmp(name, "Custom.", 7) && ppd->variable_sizes)
47
2.22k
    {
48
     /*
49
      * Find the custom page size...
50
      */
51
52
6.18k
      for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++)
53
6.18k
  if (!strcmp("Custom", size->name))
54
2.22k
          break;
55
56
2.22k
      if (!i)
57
0
      {
58
0
  DEBUG_puts("3ppdPageSize: No custom sizes, returning NULL...");
59
0
        return (NULL);
60
0
      }
61
62
     /*
63
      * Variable size; size name can be one of the following:
64
      *
65
      *    Custom.WIDTHxLENGTHin    - Size in inches
66
      *    Custom.WIDTHxLENGTHft    - Size in feet
67
      *    Custom.WIDTHxLENGTHcm    - Size in centimeters
68
      *    Custom.WIDTHxLENGTHmm    - Size in millimeters
69
      *    Custom.WIDTHxLENGTHm     - Size in meters
70
      *    Custom.WIDTHxLENGTH[pt]  - Size in points
71
      */
72
73
2.22k
      loc = localeconv();
74
2.22k
      w   = _cupsStrScand(name + 7, &nameptr, loc);
75
2.22k
      if (!nameptr || *nameptr != 'x')
76
709
        return (NULL);
77
78
1.51k
      l = _cupsStrScand(nameptr + 1, &nameptr, loc);
79
1.51k
      if (!nameptr)
80
0
        return (NULL);
81
82
1.51k
      if (!_cups_strcasecmp(nameptr, "in"))
83
197
      {
84
197
        w *= 72.0;
85
197
  l *= 72.0;
86
197
      }
87
1.31k
      else if (!_cups_strcasecmp(nameptr, "ft"))
88
324
      {
89
324
        w *= 12.0 * 72.0;
90
324
  l *= 12.0 * 72.0;
91
324
      }
92
991
      else if (!_cups_strcasecmp(nameptr, "mm"))
93
248
      {
94
248
        w *= 72.0 / 25.4;
95
248
        l *= 72.0 / 25.4;
96
248
      }
97
743
      else if (!_cups_strcasecmp(nameptr, "cm"))
98
196
      {
99
196
        w *= 72.0 / 2.54;
100
196
        l *= 72.0 / 2.54;
101
196
      }
102
547
      else if (!_cups_strcasecmp(nameptr, "m"))
103
199
      {
104
199
        w *= 72.0 / 0.0254;
105
199
        l *= 72.0 / 0.0254;
106
199
      }
107
108
1.51k
      size->width  = (float)w;
109
1.51k
      size->length = (float)l;
110
1.51k
      size->left   = ppd->custom_margins[0];
111
1.51k
      size->bottom = ppd->custom_margins[1];
112
1.51k
      size->right  = (float)(w - ppd->custom_margins[2]);
113
1.51k
      size->top    = (float)(l - ppd->custom_margins[3]);
114
115
     /*
116
      * Update the custom option records for the page size, too...
117
      */
118
119
1.51k
      if ((coption = ppdFindCustomOption(ppd, "PageSize")) != NULL)
120
1.51k
      {
121
1.51k
        if ((cparam = ppdFindCustomParam(coption, "Width")) != NULL)
122
194
    cparam->current.custom_points = (float)w;
123
124
1.51k
        if ((cparam = ppdFindCustomParam(coption, "Height")) != NULL)
125
324
    cparam->current.custom_points = (float)l;
126
1.51k
      }
127
128
     /*
129
      * Return the page size...
130
      */
131
132
1.51k
      DEBUG_printf("3ppdPageSize: Returning %p (\"%s\", %gx%g)", (void *)size, size->name, size->width, size->length);
133
134
1.51k
      return (size);
135
1.51k
    }
136
12.0k
    else
137
12.0k
    {
138
     /*
139
      * Lookup by name...
140
      */
141
142
2.29M
      for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++)
143
2.28M
  if (!_cups_strcasecmp(name, size->name))
144
9.68k
  {
145
9.68k
    DEBUG_printf("3ppdPageSize: Returning %p (\"%s\", %gx%g)", (void *)size, size->name, size->width, size->length);
146
147
9.68k
          return (size);
148
9.68k
  }
149
12.0k
    }
150
14.2k
  }
151
7.64k
  else
152
7.64k
  {
153
   /*
154
    * Find default...
155
    */
156
157
96.4k
    for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++)
158
93.8k
      if (size->marked)
159
5.09k
      {
160
5.09k
  DEBUG_printf("3ppdPageSize: Returning %p (\"%s\", %gx%g)", (void *)size, size->name, size->width, size->length);
161
162
5.09k
        return (size);
163
5.09k
      }
164
7.64k
  }
165
166
4.90k
  DEBUG_puts("3ppdPageSize: Size not found, returning NULL");
167
168
4.90k
  return (NULL);
169
21.9k
}
170
171
172
/*
173
 * 'ppdPageSizeLimits()' - Return the custom page size limits.
174
 *
175
 * This function returns the minimum and maximum custom page sizes and printable
176
 * areas based on the currently-marked (selected) options.
177
 *
178
 * If the specified PPD file does not support custom page sizes, both
179
 * "minimum" and "maximum" are filled with zeroes.
180
 *
181
 * @since CUPS 1.4@
182
 */
183
184
int         /* O - 1 if custom sizes are supported, 0 otherwise */
185
ppdPageSizeLimits(ppd_file_t *ppd,  /* I - PPD file record */
186
                  ppd_size_t *minimum,  /* O - Minimum custom size */
187
      ppd_size_t *maximum)  /* O - Maximum custom size */
188
0
{
189
0
  ppd_choice_t  *qualifier2,    /* Second media qualifier */
190
0
    *qualifier3;    /* Third media qualifier */
191
0
  ppd_attr_t  *attr;      /* Attribute */
192
0
  float   width,      /* Min/max width */
193
0
    length;     /* Min/max length */
194
0
  char    spec[PPD_MAX_NAME]; /* Selector for min/max */
195
196
197
 /*
198
  * Range check input...
199
  */
200
201
0
  if (!ppd || !ppd->variable_sizes || !minimum || !maximum)
202
0
  {
203
0
    if (minimum)
204
0
      memset(minimum, 0, sizeof(ppd_size_t));
205
206
0
    if (maximum)
207
0
      memset(maximum, 0, sizeof(ppd_size_t));
208
209
0
    return (0);
210
0
  }
211
212
 /*
213
  * See if we have the cupsMediaQualifier2 and cupsMediaQualifier3 attributes...
214
  */
215
216
0
  cupsArraySave(ppd->sorted_attrs);
217
218
0
  if ((attr = ppdFindAttr(ppd, "cupsMediaQualifier2", NULL)) != NULL &&
219
0
      attr->value)
220
0
    qualifier2 = ppdFindMarkedChoice(ppd, attr->value);
221
0
  else
222
0
    qualifier2 = NULL;
223
224
0
  if ((attr = ppdFindAttr(ppd, "cupsMediaQualifier3", NULL)) != NULL &&
225
0
      attr->value)
226
0
    qualifier3 = ppdFindMarkedChoice(ppd, attr->value);
227
0
  else
228
0
    qualifier3 = NULL;
229
230
 /*
231
  * Figure out the current minimum width and length...
232
  */
233
234
0
  width  = ppd->custom_min[0];
235
0
  length = ppd->custom_min[1];
236
237
0
  if (qualifier2)
238
0
  {
239
   /*
240
    * Try getting cupsMinSize...
241
    */
242
243
0
    if (qualifier3)
244
0
    {
245
0
      snprintf(spec, sizeof(spec), ".%s.%s", qualifier2->choice,
246
0
         qualifier3->choice);
247
0
      attr = ppdFindAttr(ppd, "cupsMinSize", spec);
248
0
    }
249
0
    else
250
0
      attr = NULL;
251
252
0
    if (!attr)
253
0
    {
254
0
      snprintf(spec, sizeof(spec), ".%s.", qualifier2->choice);
255
0
      attr = ppdFindAttr(ppd, "cupsMinSize", spec);
256
0
    }
257
258
0
    if (!attr && qualifier3)
259
0
    {
260
0
      snprintf(spec, sizeof(spec), "..%s", qualifier3->choice);
261
0
      attr = ppdFindAttr(ppd, "cupsMinSize", spec);
262
0
    }
263
264
0
    if ((attr && attr->value &&
265
0
         sscanf(attr->value, "%f%f", &width, &length) != 2) || !attr)
266
0
    {
267
0
      width  = ppd->custom_min[0];
268
0
      length = ppd->custom_min[1];
269
0
    }
270
0
  }
271
272
0
  minimum->width  = width;
273
0
  minimum->length = length;
274
0
  minimum->left   = ppd->custom_margins[0];
275
0
  minimum->bottom = ppd->custom_margins[1];
276
0
  minimum->right  = width - ppd->custom_margins[2];
277
0
  minimum->top    = length - ppd->custom_margins[3];
278
279
 /*
280
  * Figure out the current maximum width and length...
281
  */
282
283
0
  width  = ppd->custom_max[0];
284
0
  length = ppd->custom_max[1];
285
286
0
  if (qualifier2)
287
0
  {
288
   /*
289
    * Try getting cupsMaxSize...
290
    */
291
292
0
    if (qualifier3)
293
0
    {
294
0
      snprintf(spec, sizeof(spec), ".%s.%s", qualifier2->choice,
295
0
         qualifier3->choice);
296
0
      attr = ppdFindAttr(ppd, "cupsMaxSize", spec);
297
0
    }
298
0
    else
299
0
      attr = NULL;
300
301
0
    if (!attr)
302
0
    {
303
0
      snprintf(spec, sizeof(spec), ".%s.", qualifier2->choice);
304
0
      attr = ppdFindAttr(ppd, "cupsMaxSize", spec);
305
0
    }
306
307
0
    if (!attr && qualifier3)
308
0
    {
309
0
      snprintf(spec, sizeof(spec), "..%s", qualifier3->choice);
310
0
      attr = ppdFindAttr(ppd, "cupsMaxSize", spec);
311
0
    }
312
313
0
    if (!attr ||
314
0
        (attr->value && sscanf(attr->value, "%f%f", &width, &length) != 2))
315
0
    {
316
0
      width  = ppd->custom_max[0];
317
0
      length = ppd->custom_max[1];
318
0
    }
319
0
  }
320
321
0
  maximum->width  = width;
322
0
  maximum->length = length;
323
0
  maximum->left   = ppd->custom_margins[0];
324
0
  maximum->bottom = ppd->custom_margins[1];
325
0
  maximum->right  = width - ppd->custom_margins[2];
326
0
  maximum->top    = length - ppd->custom_margins[3];
327
328
 /*
329
  * Return the min and max...
330
  */
331
332
0
  cupsArrayRestore(ppd->sorted_attrs);
333
334
0
  return (1);
335
0
}
336
337
338
/*
339
 * 'ppdPageWidth()' - Get the page width for the given size.
340
 */
341
342
float       /* O - Width of page in points or 0.0 */
343
ppdPageWidth(ppd_file_t *ppd, /* I - PPD file record */
344
             const char *name)  /* I - Size name */
345
0
{
346
0
  ppd_size_t  *size;    /* Page size */
347
348
349
0
  if ((size = ppdPageSize(ppd, name)) == NULL)
350
0
    return (0.0);
351
0
  else
352
0
    return (size->width);
353
0
}
354
355
356
/*
357
 * 'ppdPageLength()' - Get the page length for the given size.
358
 */
359
360
float       /* O - Length of page in points or 0.0 */
361
ppdPageLength(ppd_file_t *ppd,  /* I - PPD file */
362
              const char *name) /* I - Size name */
363
0
{
364
0
  ppd_size_t  *size;    /* Page size */
365
366
367
0
  if ((size = ppdPageSize(ppd, name)) == NULL)
368
0
    return (0.0);
369
0
  else
370
0
    return (size->length);
371
0
}