2 * "$Id: page.c 9793 2011-05-20 03:49:49Z mike $"
4 * Page size functions for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * PostScript is a trademark of Adobe Systems, Inc.
17 * This file is subject to the Apple OS-Developed Software exception.
21 * ppdPageSize() - Get the page size record for the given size.
22 * ppdPageSizeLimits() - Return the custom page size limits.
23 * ppdPageWidth() - Get the page width for the given size.
24 * ppdPageLength() - Get the page length for the given size.
28 * Include necessary headers...
31 #include "string-private.h"
32 #include "debug-private.h"
37 * 'ppdPageSize()' - Get the page size record for the given size.
40 ppd_size_t * /* O - Size record for page or NULL */
41 ppdPageSize(ppd_file_t *ppd, /* I - PPD file record */
42 const char *name) /* I - Size name */
44 int i; /* Looping var */
45 ppd_size_t *size; /* Current page size */
46 double w, l; /* Width and length of page */
47 char *nameptr; /* Pointer into name */
48 struct lconv *loc; /* Locale data */
49 ppd_coption_t *coption; /* Custom option for page size */
50 ppd_cparam_t *cparam; /* Custom option parameter */
53 DEBUG_printf(("2ppdPageSize(ppd=%p, name=\"%s\")", ppd, name));
57 DEBUG_puts("3ppdPageSize: Bad PPD pointer, returning NULL...");
63 if (!strncmp(name, "Custom.", 7) && ppd->variable_sizes)
66 * Find the custom page size...
69 for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++)
70 if (!strcmp("Custom", size->name))
75 DEBUG_puts("3ppdPageSize: No custom sizes, returning NULL...");
80 * Variable size; size name can be one of the following:
82 * Custom.WIDTHxLENGTHin - Size in inches
83 * Custom.WIDTHxLENGTHft - Size in feet
84 * Custom.WIDTHxLENGTHcm - Size in centimeters
85 * Custom.WIDTHxLENGTHmm - Size in millimeters
86 * Custom.WIDTHxLENGTHm - Size in meters
87 * Custom.WIDTHxLENGTH[pt] - Size in points
91 w = _cupsStrScand(name + 7, &nameptr, loc);
92 if (!nameptr || *nameptr != 'x')
95 l = _cupsStrScand(nameptr + 1, &nameptr, loc);
99 if (!_cups_strcasecmp(nameptr, "in"))
104 else if (!_cups_strcasecmp(nameptr, "ft"))
109 else if (!_cups_strcasecmp(nameptr, "mm"))
114 else if (!_cups_strcasecmp(nameptr, "cm"))
119 else if (!_cups_strcasecmp(nameptr, "m"))
125 size->width = (float)w;
126 size->length = (float)l;
127 size->left = ppd->custom_margins[0];
128 size->bottom = ppd->custom_margins[1];
129 size->right = (float)(w - ppd->custom_margins[2]);
130 size->top = (float)(l - ppd->custom_margins[3]);
133 * Update the custom option records for the page size, too...
136 if ((coption = ppdFindCustomOption(ppd, "PageSize")) != NULL)
138 if ((cparam = ppdFindCustomParam(coption, "Width")) != NULL)
139 cparam->current.custom_points = (float)w;
141 if ((cparam = ppdFindCustomParam(coption, "Height")) != NULL)
142 cparam->current.custom_points = (float)l;
146 * Return the page size...
149 DEBUG_printf(("3ppdPageSize: Returning %p (\"%s\", %gx%g)", size,
150 size->name, size->width, size->length));
160 for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++)
161 if (!_cups_strcasecmp(name, size->name))
163 DEBUG_printf(("3ppdPageSize: Returning %p (\"%s\", %gx%g)", size,
164 size->name, size->width, size->length));
176 for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++)
179 DEBUG_printf(("3ppdPageSize: Returning %p (\"%s\", %gx%g)", size,
180 size->name, size->width, size->length));
186 DEBUG_puts("3ppdPageSize: Size not found, returning NULL");
193 * 'ppdPageSizeLimits()' - Return the custom page size limits.
195 * This function returns the minimum and maximum custom page sizes and printable
196 * areas based on the currently-marked (selected) options.
198 * If the specified PPD file does not support custom page sizes, both
199 * "minimum" and "maximum" are filled with zeroes.
201 * @since CUPS 1.4/Mac OS X 10.6@
204 int /* O - 1 if custom sizes are supported, 0 otherwise */
205 ppdPageSizeLimits(ppd_file_t *ppd, /* I - PPD file record */
206 ppd_size_t *minimum, /* O - Minimum custom size */
207 ppd_size_t *maximum) /* O - Maximum custom size */
209 ppd_choice_t *qualifier2, /* Second media qualifier */
210 *qualifier3; /* Third media qualifier */
211 ppd_attr_t *attr; /* Attribute */
212 float width, /* Min/max width */
213 length; /* Min/max length */
214 char spec[PPD_MAX_NAME]; /* Selector for min/max */
218 * Range check input...
221 if (!ppd || !ppd->variable_sizes || !minimum || !maximum)
224 memset(minimum, 0, sizeof(ppd_size_t));
227 memset(maximum, 0, sizeof(ppd_size_t));
233 * See if we have the cupsMediaQualifier2 and cupsMediaQualifier3 attributes...
236 cupsArraySave(ppd->sorted_attrs);
238 if ((attr = ppdFindAttr(ppd, "cupsMediaQualifier2", NULL)) != NULL &&
240 qualifier2 = ppdFindMarkedChoice(ppd, attr->value);
244 if ((attr = ppdFindAttr(ppd, "cupsMediaQualifier3", NULL)) != NULL &&
246 qualifier3 = ppdFindMarkedChoice(ppd, attr->value);
251 * Figure out the current minimum width and length...
254 width = ppd->custom_min[0];
255 length = ppd->custom_min[1];
260 * Try getting cupsMinSize...
265 snprintf(spec, sizeof(spec), ".%s.%s", qualifier2->choice,
267 attr = ppdFindAttr(ppd, "cupsMinSize", spec);
274 snprintf(spec, sizeof(spec), ".%s.", qualifier2->choice);
275 attr = ppdFindAttr(ppd, "cupsMinSize", spec);
278 if (!attr && qualifier3)
280 snprintf(spec, sizeof(spec), "..%s", qualifier3->choice);
281 attr = ppdFindAttr(ppd, "cupsMinSize", spec);
284 if ((attr && attr->value &&
285 sscanf(attr->value, "%f%f", &width, &length) != 2) || !attr)
287 width = ppd->custom_min[0];
288 length = ppd->custom_min[1];
292 minimum->width = width;
293 minimum->length = length;
294 minimum->left = ppd->custom_margins[0];
295 minimum->bottom = ppd->custom_margins[1];
296 minimum->right = width - ppd->custom_margins[2];
297 minimum->top = length - ppd->custom_margins[3];
300 * Figure out the current maximum width and length...
303 width = ppd->custom_max[0];
304 length = ppd->custom_max[1];
309 * Try getting cupsMaxSize...
314 snprintf(spec, sizeof(spec), ".%s.%s", qualifier2->choice,
316 attr = ppdFindAttr(ppd, "cupsMaxSize", spec);
323 snprintf(spec, sizeof(spec), ".%s.", qualifier2->choice);
324 attr = ppdFindAttr(ppd, "cupsMaxSize", spec);
327 if (!attr && qualifier3)
329 snprintf(spec, sizeof(spec), "..%s", qualifier3->choice);
330 attr = ppdFindAttr(ppd, "cupsMaxSize", spec);
334 (attr->value && sscanf(attr->value, "%f%f", &width, &length) != 2))
336 width = ppd->custom_max[0];
337 length = ppd->custom_max[1];
341 maximum->width = width;
342 maximum->length = length;
343 maximum->left = ppd->custom_margins[0];
344 maximum->bottom = ppd->custom_margins[1];
345 maximum->right = width - ppd->custom_margins[2];
346 maximum->top = length - ppd->custom_margins[3];
349 * Return the min and max...
352 cupsArrayRestore(ppd->sorted_attrs);
359 * 'ppdPageWidth()' - Get the page width for the given size.
362 float /* O - Width of page in points or 0.0 */
363 ppdPageWidth(ppd_file_t *ppd, /* I - PPD file record */
364 const char *name) /* I - Size name */
366 ppd_size_t *size; /* Page size */
369 if ((size = ppdPageSize(ppd, name)) == NULL)
372 return (size->width);
377 * 'ppdPageLength()' - Get the page length for the given size.
380 float /* O - Length of page in points or 0.0 */
381 ppdPageLength(ppd_file_t *ppd, /* I - PPD file */
382 const char *name) /* I - Size name */
384 ppd_size_t *size; /* Page size */
387 if ((size = ppdPageSize(ppd, name)) == NULL)
390 return (size->length);
395 * End of "$Id: page.c 9793 2011-05-20 03:49:49Z mike $".