#include "string-private.h"
#include "debug-private.h"
#include "ppd.h"
ppd_size_t *
ppdPageSize(ppd_file_t *ppd,
const char *name)
{
int i;
ppd_size_t *size;
double w, l;
char *nameptr;
struct lconv *loc;
ppd_coption_t *coption;
ppd_cparam_t *cparam;
DEBUG_printf(("2ppdPageSize(ppd=%p, name=\"%s\")", ppd, name));
if (!ppd)
{
DEBUG_puts("3ppdPageSize: Bad PPD pointer, returning NULL...");
return (NULL);
}
if (name)
{
if (!strncmp(name, "Custom.", 7) && ppd->variable_sizes)
{
for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++)
if (!strcmp("Custom", size->name))
break;
if (!i)
{
DEBUG_puts("3ppdPageSize: No custom sizes, returning NULL...");
return (NULL);
}
loc = localeconv();
w = _cupsStrScand(name + 7, &nameptr, loc);
if (!nameptr || *nameptr != 'x')
return (NULL);
l = _cupsStrScand(nameptr + 1, &nameptr, loc);
if (!nameptr)
return (NULL);
if (!_cups_strcasecmp(nameptr, "in"))
{
w *= 72.0;
l *= 72.0;
}
else if (!_cups_strcasecmp(nameptr, "ft"))
{
w *= 12.0 * 72.0;
l *= 12.0 * 72.0;
}
else if (!_cups_strcasecmp(nameptr, "mm"))
{
w *= 72.0 / 25.4;
l *= 72.0 / 25.4;
}
else if (!_cups_strcasecmp(nameptr, "cm"))
{
w *= 72.0 / 2.54;
l *= 72.0 / 2.54;
}
else if (!_cups_strcasecmp(nameptr, "m"))
{
w *= 72.0 / 0.0254;
l *= 72.0 / 0.0254;
}
size->width = (float)w;
size->length = (float)l;
size->left = ppd->custom_margins[0];
size->bottom = ppd->custom_margins[1];
size->right = (float)(w - ppd->custom_margins[2]);
size->top = (float)(l - ppd->custom_margins[3]);
if ((coption = ppdFindCustomOption(ppd, "PageSize")) != NULL)
{
if ((cparam = ppdFindCustomParam(coption, "Width")) != NULL)
cparam->current.custom_points = (float)w;
if ((cparam = ppdFindCustomParam(coption, "Height")) != NULL)
cparam->current.custom_points = (float)l;
}
DEBUG_printf(("3ppdPageSize: Returning %p (\"%s\", %gx%g)", size,
size->name, size->width, size->length));
return (size);
}
else
{
for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++)
if (!_cups_strcasecmp(name, size->name))
{
DEBUG_printf(("3ppdPageSize: Returning %p (\"%s\", %gx%g)", size,
size->name, size->width, size->length));
return (size);
}
}
}
else
{
for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++)
if (size->marked)
{
DEBUG_printf(("3ppdPageSize: Returning %p (\"%s\", %gx%g)", size,
size->name, size->width, size->length));
return (size);
}
}
DEBUG_puts("3ppdPageSize: Size not found, returning NULL");
return (NULL);
}
int
ppdPageSizeLimits(ppd_file_t *ppd,
ppd_size_t *minimum,
ppd_size_t *maximum)
{
ppd_choice_t *qualifier2,
*qualifier3;
ppd_attr_t *attr;
float width,
length;
char spec[PPD_MAX_NAME];
if (!ppd || !ppd->variable_sizes || !minimum || !maximum)
{
if (minimum)
memset(minimum, 0, sizeof(ppd_size_t));
if (maximum)
memset(maximum, 0, sizeof(ppd_size_t));
return (0);
}
cupsArraySave(ppd->sorted_attrs);
if ((attr = ppdFindAttr(ppd, "cupsMediaQualifier2", NULL)) != NULL &&
attr->value)
qualifier2 = ppdFindMarkedChoice(ppd, attr->value);
else
qualifier2 = NULL;
if ((attr = ppdFindAttr(ppd, "cupsMediaQualifier3", NULL)) != NULL &&
attr->value)
qualifier3 = ppdFindMarkedChoice(ppd, attr->value);
else
qualifier3 = NULL;
width = ppd->custom_min[0];
length = ppd->custom_min[1];
if (qualifier2)
{
if (qualifier3)
{
snprintf(spec, sizeof(spec), ".%s.%s", qualifier2->choice,
qualifier3->choice);
attr = ppdFindAttr(ppd, "cupsMinSize", spec);
}
else
attr = NULL;
if (!attr)
{
snprintf(spec, sizeof(spec), ".%s.", qualifier2->choice);
attr = ppdFindAttr(ppd, "cupsMinSize", spec);
}
if (!attr && qualifier3)
{
snprintf(spec, sizeof(spec), "..%s", qualifier3->choice);
attr = ppdFindAttr(ppd, "cupsMinSize", spec);
}
if ((attr && attr->value &&
sscanf(attr->value, "%f%f", &width, &length) != 2) || !attr)
{
width = ppd->custom_min[0];
length = ppd->custom_min[1];
}
}
minimum->width = width;
minimum->length = length;
minimum->left = ppd->custom_margins[0];
minimum->bottom = ppd->custom_margins[1];
minimum->right = width - ppd->custom_margins[2];
minimum->top = length - ppd->custom_margins[3];
width = ppd->custom_max[0];
length = ppd->custom_max[1];
if (qualifier2)
{
if (qualifier3)
{
snprintf(spec, sizeof(spec), ".%s.%s", qualifier2->choice,
qualifier3->choice);
attr = ppdFindAttr(ppd, "cupsMaxSize", spec);
}
else
attr = NULL;
if (!attr)
{
snprintf(spec, sizeof(spec), ".%s.", qualifier2->choice);
attr = ppdFindAttr(ppd, "cupsMaxSize", spec);
}
if (!attr && qualifier3)
{
snprintf(spec, sizeof(spec), "..%s", qualifier3->choice);
attr = ppdFindAttr(ppd, "cupsMaxSize", spec);
}
if (!attr ||
(attr->value && sscanf(attr->value, "%f%f", &width, &length) != 2))
{
width = ppd->custom_max[0];
length = ppd->custom_max[1];
}
}
maximum->width = width;
maximum->length = length;
maximum->left = ppd->custom_margins[0];
maximum->bottom = ppd->custom_margins[1];
maximum->right = width - ppd->custom_margins[2];
maximum->top = length - ppd->custom_margins[3];
cupsArrayRestore(ppd->sorted_attrs);
return (1);
}
float
ppdPageWidth(ppd_file_t *ppd,
const char *name)
{
ppd_size_t *size;
if ((size = ppdPageSize(ppd, name)) == NULL)
return (0.0);
else
return (size->width);
}
float
ppdPageLength(ppd_file_t *ppd,
const char *name)
{
ppd_size_t *size;
if ((size = ppdPageSize(ppd, name)) == NULL)
return (0.0);
else
return (size->length);
}