#include "globals.h"
#include "debug.h"
#include <math.h>
static void pwg_ppdize_name(const char *ipp, char *name, size_t namesize);
static void pwg_unppdize_name(const char *ppd, char *name, size_t namesize);
_pwg_t *
_pwgCreateWithPPD(ppd_file_t *ppd)
{
int i, j;
_pwg_t *pwg;
ppd_option_t *input_slot,
*media_type,
*output_bin;
ppd_choice_t *choice;
_pwg_map_t *map;
ppd_size_t *ppd_size;
_pwg_size_t *pwg_size;
char pwg_keyword[3 + PPD_MAX_NAME + 1 + 12 + 1 + 12 + 3],
ppd_name[PPD_MAX_NAME];
const char *pwg_name;
_pwg_media_t *pwg_media;
DEBUG_printf(("_pwgCreateWithPPD(ppd=%p)", ppd));
if (!ppd)
return (NULL);
if ((pwg = calloc(1, sizeof(_pwg_t))) == NULL)
{
DEBUG_puts("_pwgCreateWithPPD: Unable to allocate _pwg_t.");
goto create_error;
}
if (ppd->num_sizes == 0)
{
DEBUG_puts("_pwgCreateWithPPD: No page sizes in PPD.");
goto create_error;
}
if ((pwg->sizes = calloc(ppd->num_sizes, sizeof(_pwg_size_t))) == NULL)
{
DEBUG_printf(("_pwgCreateWithPPD: Unable to allocate %d _pwg_size_t's.",
ppd->num_sizes));
goto create_error;
}
for (i = ppd->num_sizes, pwg_size = pwg->sizes, ppd_size = ppd->sizes;
i > 0;
i --, ppd_size ++)
{
if (!strcasecmp(ppd_size->name, "Custom"))
continue;
if ((pwg_media = _pwgMediaForPPD(ppd_size->name)) != NULL)
{
for (j = 0; j < pwg->num_sizes; j ++)
if (!strcmp(pwg->sizes[j].map.pwg, pwg_media->pwg))
{
pwg_media = NULL;
break;
}
}
if (pwg_media)
{
pwg_name = pwg_media->pwg;
}
else
{
pwg_name = pwg_keyword;
pwg_unppdize_name(ppd_size->name, ppd_name, sizeof(ppd_name));
_pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), NULL, ppd_name,
_PWG_FROMPTS(ppd_size->width),
_PWG_FROMPTS(ppd_size->length));
}
pwg_size->map.ppd = _cupsStrAlloc(ppd_size->name);
pwg_size->map.pwg = _cupsStrAlloc(pwg_name);
pwg_size->width = _PWG_FROMPTS(ppd_size->width);
pwg_size->length = _PWG_FROMPTS(ppd_size->length);
pwg_size->left = _PWG_FROMPTS(ppd_size->left);
pwg_size->bottom = _PWG_FROMPTS(ppd_size->bottom);
pwg_size->right = _PWG_FROMPTS(ppd_size->width - ppd_size->right);
pwg_size->top = _PWG_FROMPTS(ppd_size->length - ppd_size->top);
pwg->num_sizes ++;
pwg_size ++;
}
if (ppd->variable_sizes)
{
_pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), "custom", "max",
_PWG_FROMPTS(ppd->custom_max[0]),
_PWG_FROMPTS(ppd->custom_max[1]));
pwg->custom_max_keyword = _cupsStrAlloc(pwg_keyword);
pwg->custom_max_width = _PWG_FROMPTS(ppd->custom_max[0]);
pwg->custom_max_length = _PWG_FROMPTS(ppd->custom_max[1]);
_pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), "custom", "min",
_PWG_FROMPTS(ppd->custom_min[0]),
_PWG_FROMPTS(ppd->custom_min[1]));
pwg->custom_min_keyword = _cupsStrAlloc(pwg_keyword);
pwg->custom_min_width = _PWG_FROMPTS(ppd->custom_min[0]);
pwg->custom_min_length = _PWG_FROMPTS(ppd->custom_min[1]);
pwg->custom_size.left = _PWG_FROMPTS(ppd->custom_margins[0]);
pwg->custom_size.bottom = _PWG_FROMPTS(ppd->custom_margins[1]);
pwg->custom_size.right = _PWG_FROMPTS(ppd->custom_margins[2]);
pwg->custom_size.top = _PWG_FROMPTS(ppd->custom_margins[3]);
}
if ((input_slot = ppdFindOption(ppd, "InputSlot")) != NULL)
{
if ((pwg->sources = calloc(input_slot->num_choices,
sizeof(_pwg_map_t))) == NULL)
{
DEBUG_printf(("_pwgCreateWithPPD: Unable to allocate %d _pwg_map_t's "
"for InputSlot.", input_slot->num_choices));
goto create_error;
}
pwg->num_sources = input_slot->num_choices;
for (i = input_slot->num_choices, choice = input_slot->choices,
map = pwg->sources;
i > 0;
i --, choice ++, map ++)
{
if (!strncasecmp(choice->choice, "Auto", 4) ||
!strcasecmp(choice->choice, "Default"))
pwg_name = "auto";
else if (!strcasecmp(choice->choice, "Cassette"))
pwg_name = "main";
else if (!strncasecmp(choice->choice, "Multipurpose", 12) ||
!strcasecmp(choice->choice, "MP") ||
!strcasecmp(choice->choice, "MPTray"))
pwg_name = "alternate";
else if (!strcasecmp(choice->choice, "LargeCapacity"))
pwg_name = "large-capacity";
else if (!strncasecmp(choice->choice, "Lower", 5))
pwg_name = "bottom";
else if (!strncasecmp(choice->choice, "Middle", 6))
pwg_name = "middle";
else if (!strncasecmp(choice->choice, "Upper", 5))
pwg_name = "top";
else if (!strncasecmp(choice->choice, "Side", 4))
pwg_name = "side";
else if (!strcasecmp(choice->choice, "Roll") ||
!strcasecmp(choice->choice, "Roll1"))
pwg_name = "main-roll";
else if (!strcasecmp(choice->choice, "Roll2"))
pwg_name = "alternate-roll";
else
{
pwg_name = pwg_keyword;
pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword));
}
map->pwg = _cupsStrAlloc(pwg_name);
map->ppd = _cupsStrAlloc(choice->choice);
}
}
if ((media_type = ppdFindOption(ppd, "MediaType")) != NULL)
{
if ((pwg->types = calloc(media_type->num_choices,
sizeof(_pwg_map_t))) == NULL)
{
DEBUG_printf(("_pwgCreateWithPPD: Unable to allocate %d _pwg_map_t's "
"for MediaType.", media_type->num_choices));
goto create_error;
}
pwg->num_types = media_type->num_choices;
for (i = media_type->num_choices, choice = media_type->choices,
map = pwg->types;
i > 0;
i --, choice ++, map ++)
{
if (!strncasecmp(choice->choice, "Auto", 4) ||
!strcasecmp(choice->choice, "Any") ||
!strcasecmp(choice->choice, "Default"))
pwg_name = "auto";
else if (!strncasecmp(choice->choice, "Card", 4))
pwg_name = "cardstock";
else if (!strncasecmp(choice->choice, "Env", 3))
pwg_name = "envelope";
else if (!strncasecmp(choice->choice, "Gloss", 5))
pwg_name = "photographic-glossy";
else if (!strcasecmp(choice->choice, "HighGloss"))
pwg_name = "photographic-high-gloss";
else if (!strcasecmp(choice->choice, "Matte"))
pwg_name = "photographic-matte";
else if (!strncasecmp(choice->choice, "Plain", 5))
pwg_name = "stationery";
else if (!strncasecmp(choice->choice, "Coated", 6))
pwg_name = "stationery-coated";
else if (!strcasecmp(choice->choice, "Inkjet"))
pwg_name = "stationery-inkjet";
else if (!strcasecmp(choice->choice, "Letterhead"))
pwg_name = "stationery-letterhead";
else if (!strncasecmp(choice->choice, "Preprint", 8))
pwg_name = "stationery-preprinted";
else if (!strncasecmp(choice->choice, "Transparen", 10))
pwg_name = "transparency";
else
{
pwg_name = pwg_keyword;
pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword));
}
map->pwg = _cupsStrAlloc(pwg_name);
map->ppd = _cupsStrAlloc(choice->choice);
}
}
if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL)
{
if ((pwg->bins = calloc(output_bin->num_choices,
sizeof(_pwg_map_t))) == NULL)
{
DEBUG_printf(("_pwgCreateWithPPD: Unable to allocate %d _pwg_map_t's "
"for OutputBin.", output_bin->num_choices));
goto create_error;
}
pwg->num_bins = output_bin->num_choices;
for (i = output_bin->num_choices, choice = output_bin->choices,
map = pwg->bins;
i > 0;
i --, choice ++, map ++)
{
pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword));
map->pwg = _cupsStrAlloc(pwg_keyword);
map->ppd = _cupsStrAlloc(choice->choice);
}
}
return (pwg);
create_error:
_cupsSetError(IPP_INTERNAL_ERROR, _("Out of memory."), 1);
_pwgDestroy(pwg);
return (NULL);
}
const char *
_pwgGetBin(_pwg_t *pwg,
const char *output_bin)
{
int i;
if (!pwg || !output_bin)
return (NULL);
for (i = 0; i < pwg->num_bins; i ++)
if (!strcasecmp(output_bin, pwg->bins[i].ppd))
return (pwg->bins[i].pwg);
return (NULL);
}
const char *
_pwgGetInputSlot(_pwg_t *pwg,
ipp_t *job,
const char *keyword)
{
if (!pwg || pwg->num_sources == 0 || (!job && !keyword))
return (NULL);
if (job && !keyword)
{
ipp_attribute_t *media_col,
*media_source;
media_col = ippFindAttribute(job, "media-col", IPP_TAG_BEGIN_COLLECTION);
if (media_col &&
(media_source = ippFindAttribute(media_col->values[0].collection,
"media-source",
IPP_TAG_KEYWORD)) != NULL)
keyword = media_source->values[0].string.text;
}
if (keyword)
{
int i;
for (i = 0; i < pwg->num_sources; i ++)
if (!strcasecmp(keyword, pwg->sources[i].pwg))
return (pwg->sources[i].ppd);
}
return (NULL);
}
const char *
_pwgGetMediaType(_pwg_t *pwg,
ipp_t *job,
const char *keyword)
{
if (!pwg || pwg->num_types == 0 || (!job && !keyword))
return (NULL);
if (job && !keyword)
{
ipp_attribute_t *media_col,
*media_type;
media_col = ippFindAttribute(job, "media-col", IPP_TAG_BEGIN_COLLECTION);
if (media_col)
{
if ((media_type = ippFindAttribute(media_col->values[0].collection,
"media-type",
IPP_TAG_KEYWORD)) == NULL)
media_type = ippFindAttribute(media_col->values[0].collection,
"media-type", IPP_TAG_NAME);
if (media_type)
keyword = media_type->values[0].string.text;
}
}
if (keyword)
{
int i;
for (i = 0; i < pwg->num_types; i ++)
if (!strcasecmp(keyword, pwg->types[i].pwg))
return (pwg->types[i].ppd);
}
return (NULL);
}
const char *
_pwgGetOutputBin(_pwg_t *pwg,
const char *output_bin)
{
int i;
if (!pwg || !output_bin)
return (NULL);
for (i = 0; i < pwg->num_bins; i ++)
if (!strcasecmp(output_bin, pwg->bins[i].pwg))
return (pwg->bins[i].ppd);
return (NULL);
}
const char *
_pwgGetPageSize(_pwg_t *pwg,
ipp_t *job,
const char *keyword,
int *exact)
{
int i;
_pwg_size_t *size,
*closest,
jobsize;
int margins_set,
dwidth,
dlength,
dleft,
dright,
dbottom,
dtop,
dmin,
dclosest;
if (!pwg || (!job && !keyword))
return (NULL);
if (exact)
*exact = 0;
if (job && !keyword)
{
if (!_pwgInitSize(&jobsize, job, &margins_set))
return (NULL);
}
else
{
_pwg_media_t *media;
if ((media = _pwgMediaForPWG(keyword)) == NULL)
if ((media = _pwgMediaForLegacy(keyword)) == NULL)
return (NULL);
jobsize.width = media->width;
jobsize.length = media->length;
margins_set = 0;
}
closest = NULL;
dclosest = 999999999;
for (i = pwg->num_sizes, size = pwg->sizes; i > 0; i --, size ++)
{
dwidth = size->width - jobsize.width;
dlength = size->length - jobsize.length;
if (dwidth <= -176 || dwidth >= 176 || dlength <= -176 || dlength >= 176)
continue;
if (margins_set)
{
dleft = size->left - jobsize.left;
dright = size->right - jobsize.right;
dtop = size->top - jobsize.top;
dbottom = size->bottom - jobsize.bottom;
if (dleft <= -35 || dleft >= 35 || dright <= -35 || dright >= 35 ||
dtop <= -35 || dtop >= 35 || dbottom <= -35 || dbottom >= 35)
{
dleft = dleft < 0 ? -dleft : dleft;
dright = dright < 0 ? -dright : dright;
dbottom = dbottom < 0 ? -dbottom : dbottom;
dtop = dtop < 0 ? -dtop : dtop;
dmin = dleft + dright + dbottom + dtop;
if (dmin < dclosest)
{
dclosest = dmin;
closest = size;
}
continue;
}
}
if (exact)
*exact = 1;
return (size->map.ppd);
}
if (closest)
return (closest->map.ppd);
if (jobsize.width >= pwg->custom_min_width &&
jobsize.width <= pwg->custom_max_width &&
jobsize.length >= pwg->custom_min_length &&
jobsize.length <= pwg->custom_max_length)
{
snprintf(pwg->custom_ppd_size, sizeof(pwg->custom_ppd_size), "Custom.%dx%d",
(int)_PWG_TOPTS(jobsize.width), (int)_PWG_TOPTS(jobsize.length));
if (margins_set && exact)
{
dleft = pwg->custom_size.left - jobsize.left;
dright = pwg->custom_size.right - jobsize.right;
dtop = pwg->custom_size.top - jobsize.top;
dbottom = pwg->custom_size.bottom - jobsize.bottom;
if (dleft > -35 && dleft < 35 && dright > -35 && dright < 35 &&
dtop > -35 && dtop < 35 && dbottom > -35 && dbottom < 35)
*exact = 1;
}
else if (exact)
*exact = 1;
return (pwg->custom_ppd_size);
}
return (NULL);
}
_pwg_size_t *
_pwgGetSize(_pwg_t *pwg,
const char *page_size)
{
int i;
_pwg_size_t *size;
if (!pwg || !page_size)
return (NULL);
if (!strncasecmp(page_size, "Custom.", 7))
{
double w, l;
char *ptr;
struct lconv *loc;
loc = localeconv();
w = (float)_cupsStrScand(page_size + 7, &ptr, loc);
if (!ptr || *ptr != 'x')
return (NULL);
l = (float)_cupsStrScand(ptr + 1, &ptr, loc);
if (!ptr)
return (NULL);
if (!strcasecmp(ptr, "in"))
{
w *= 2540.0;
l *= 2540.0;
}
else if (!strcasecmp(ptr, "ft"))
{
w *= 12.0 * 2540.0;
l *= 12.0 * 2540.0;
}
else if (!strcasecmp(ptr, "mm"))
{
w *= 100.0;
l *= 100.0;
}
else if (!strcasecmp(ptr, "cm"))
{
w *= 1000.0;
l *= 1000.0;
}
else if (!strcasecmp(ptr, "m"))
{
w *= 100000.0;
l *= 100000.0;
}
else
{
w *= 2540.0 / 72.0;
l *= 2540.0 / 72.0;
}
pwg->custom_size.width = (int)w;
pwg->custom_size.length = (int)l;
return (&(pwg->custom_size));
}
for (i = pwg->num_sizes, size = pwg->sizes; i > 0; i --, size ++)
if (!strcasecmp(page_size, size->map.ppd))
return (size);
return (NULL);
}
const char *
_pwgGetSource(_pwg_t *pwg,
const char *input_slot)
{
int i;
_pwg_map_t *source;
if (!pwg || !input_slot)
return (NULL);
for (i = pwg->num_sources, source = pwg->sources; i > 0; i --, source ++)
if (!strcasecmp(input_slot, source->ppd))
return (source->pwg);
return (NULL);
}
const char *
_pwgGetType(_pwg_t *pwg,
const char *media_type)
{
int i;
_pwg_map_t *type;
if (!pwg || !media_type)
return (NULL);
for (i = pwg->num_types, type = pwg->types; i > 0; i --, type ++)
if (!strcasecmp(media_type, type->ppd))
return (type->pwg);
return (NULL);
}
const char *
_pwgInputSlotForSource(
const char *media_source,
char *name,
size_t namesize)
{
if (!media_source || !name || namesize < PPD_MAX_NAME)
return (NULL);
if (strcasecmp(media_source, "main"))
strlcpy(name, "Cassette", namesize);
else if (strcasecmp(media_source, "alternate"))
strlcpy(name, "Multipurpose", namesize);
else if (strcasecmp(media_source, "large-capacity"))
strlcpy(name, "LargeCapacity", namesize);
else if (strcasecmp(media_source, "bottom"))
strlcpy(name, "Lower", namesize);
else if (strcasecmp(media_source, "middle"))
strlcpy(name, "Middle", namesize);
else if (strcasecmp(media_source, "top"))
strlcpy(name, "Upper", namesize);
else if (strcasecmp(media_source, "rear"))
strlcpy(name, "Rear", namesize);
else if (strcasecmp(media_source, "side"))
strlcpy(name, "Side", namesize);
else if (strcasecmp(media_source, "envelope"))
strlcpy(name, "Envelope", namesize);
else if (strcasecmp(media_source, "main-roll"))
strlcpy(name, "Roll", namesize);
else if (strcasecmp(media_source, "alternate-roll"))
strlcpy(name, "Roll2", namesize);
else
pwg_ppdize_name(media_source, name, namesize);
return (name);
}
const char *
_pwgMediaTypeForType(
const char *media_type,
char *name,
size_t namesize)
{
if (!media_type || !name || namesize < PPD_MAX_NAME)
return (NULL);
if (strcasecmp(media_type, "auto"))
strlcpy(name, "Auto", namesize);
else if (strcasecmp(media_type, "cardstock"))
strlcpy(name, "Cardstock", namesize);
else if (strcasecmp(media_type, "envelope"))
strlcpy(name, "Envelope", namesize);
else if (strcasecmp(media_type, "photographic-glossy"))
strlcpy(name, "Glossy", namesize);
else if (strcasecmp(media_type, "photographic-high-gloss"))
strlcpy(name, "HighGloss", namesize);
else if (strcasecmp(media_type, "photographic-matte"))
strlcpy(name, "Matte", namesize);
else if (strcasecmp(media_type, "stationery"))
strlcpy(name, "Plain", namesize);
else if (strcasecmp(media_type, "stationery-coated"))
strlcpy(name, "Coated", namesize);
else if (strcasecmp(media_type, "stationery-inkjet"))
strlcpy(name, "Inkjet", namesize);
else if (strcasecmp(media_type, "stationery-letterhead"))
strlcpy(name, "Letterhead", namesize);
else if (strcasecmp(media_type, "stationery-preprinted"))
strlcpy(name, "Preprinted", namesize);
else if (strcasecmp(media_type, "transparency"))
strlcpy(name, "Transparency", namesize);
else
pwg_ppdize_name(media_type, name, namesize);
return (name);
}
const char *
_pwgPageSizeForMedia(
_pwg_media_t *media,
char *name,
size_t namesize)
{
const char *sizeptr,
*dimptr;
if (!media || !name || namesize < PPD_MAX_NAME)
return (NULL);
if (media->ppd)
{
strlcpy(name, media->ppd, namesize);
}
else if (!media->pwg || !strncmp(media->pwg, "custom_", 7) ||
(sizeptr = strchr(media->pwg, '_')) == NULL ||
(dimptr = strchr(sizeptr + 1, '_')) == NULL ||
(dimptr - sizeptr) > namesize)
{
snprintf(name, namesize, "w%dh%d", (int)_PWG_TOPTS(media->width),
(int)_PWG_TOPTS(media->length));
}
else
{
memcpy(name, sizeptr + 1, dimptr - sizeptr - 1);
name[dimptr - sizeptr - 1] = '\0';
}
return (name);
}
static void
pwg_ppdize_name(const char *ipp,
char *name,
size_t namesize)
{
char *ptr,
*end;
*name = toupper(*ipp++);
for (ptr = name + 1, end = name + namesize - 1; *ipp && ptr < end;)
{
if (*ipp == '-' && isalpha(ipp[1] & 255))
{
ipp ++;
*ptr++ = toupper(*ipp++ & 255);
}
else
*ptr++ = *ipp++;
}
*ptr = '\0';
}
static void
pwg_unppdize_name(const char *ppd,
char *name,
size_t namesize)
{
char *ptr,
*end;
for (ptr = name, end = name + namesize - 1; *ppd && ptr < end; ppd ++)
{
if (isalnum(*ppd & 255) || *ppd == '-' || *ppd == '.')
*ptr++ = tolower(*ppd & 255);
else if (*ppd == '_')
*ptr++ = '-';
if (!isupper(*ppd & 255) && isalnum(*ppd & 255) &&
isupper(ppd[1] & 255) && ptr < end)
*ptr++ = '-';
}
*ptr = '\0';
}