#include "cups-private.h"
#include <math.h>
#define _PWG_EQUIVALENT(x, y) (abs((x)-(y)) < 2)
static int pwg_compare_finishings(_pwg_finishings_t *a,
_pwg_finishings_t *b);
static void pwg_free_finishings(_pwg_finishings_t *f);
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);
_ppd_cache_t *
_ppdCacheCreateWithFile(
const char *filename,
ipp_t **attrs)
{
cups_file_t *fp;
_ppd_cache_t *pc;
_pwg_size_t *size;
_pwg_map_t *map;
_pwg_finishings_t *finishings;
int linenum,
num_bins,
num_sizes,
num_sources,
num_types;
char line[2048],
*value,
*valueptr,
pwg_keyword[128],
ppd_keyword[PPD_MAX_NAME];
_pwg_print_color_mode_t print_color_mode;
_pwg_print_quality_t print_quality;
DEBUG_printf(("_ppdCacheCreateWithFile(filename=\"%s\")", filename));
if (attrs)
*attrs = NULL;
if (!filename)
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
return (NULL);
}
if ((fp = cupsFileOpen(filename, "r")) == NULL)
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
return (NULL);
}
if (!cupsFileGets(fp, line, sizeof(line)))
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
DEBUG_puts("_ppdCacheCreateWithFile: Unable to read first line.");
cupsFileClose(fp);
return (NULL);
}
if (strncmp(line, "#CUPS-PPD-CACHE-", 16))
{
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
DEBUG_printf(("_ppdCacheCreateWithFile: Wrong first line \"%s\".", line));
cupsFileClose(fp);
return (NULL);
}
if (atoi(line + 16) != _PPD_CACHE_VERSION)
{
_cupsSetError(IPP_INTERNAL_ERROR, _("Out of date PPD cache file."), 1);
DEBUG_printf(("_ppdCacheCreateWithFile: Cache file has version %s, "
"expected %d.", line + 16, _PPD_CACHE_VERSION));
cupsFileClose(fp);
return (NULL);
}
if ((pc = calloc(1, sizeof(_ppd_cache_t))) == NULL)
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
DEBUG_puts("_ppdCacheCreateWithFile: Unable to allocate _ppd_cache_t.");
goto create_error;
}
pc->max_copies = 9999;
linenum = 0;
num_bins = 0;
num_sizes = 0;
num_sources = 0;
num_types = 0;
while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
{
DEBUG_printf(("_ppdCacheCreateWithFile: line=\"%s\", value=\"%s\", "
"linenum=%d", line, value, linenum));
if (!value)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Missing value on line %d.",
linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
else if (!_cups_strcasecmp(line, "Filter"))
{
if (!pc->filters)
pc->filters = cupsArrayNew3(NULL, NULL, NULL, 0,
(cups_acopy_func_t)_cupsStrAlloc,
(cups_afree_func_t)_cupsStrFree);
cupsArrayAdd(pc->filters, value);
}
else if (!_cups_strcasecmp(line, "PreFilter"))
{
if (!pc->prefilters)
pc->prefilters = cupsArrayNew3(NULL, NULL, NULL, 0,
(cups_acopy_func_t)_cupsStrAlloc,
(cups_afree_func_t)_cupsStrFree);
cupsArrayAdd(pc->prefilters, value);
}
else if (!_cups_strcasecmp(line, "Product"))
{
pc->product = _cupsStrAlloc(value);
}
else if (!_cups_strcasecmp(line, "SingleFile"))
{
pc->single_file = !_cups_strcasecmp(value, "true");
}
else if (!_cups_strcasecmp(line, "IPP"))
{
off_t pos = cupsFileTell(fp),
length = strtol(value, NULL, 10);
if (attrs && *attrs)
{
DEBUG_puts("_ppdCacheCreateWithFile: IPP listed multiple times.");
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
else if (length <= 0)
{
DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP length.");
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if (attrs)
{
*attrs = ippNew();
if (ippReadIO(fp, (ipp_iocb_t)cupsFileRead, 1, NULL,
*attrs) != IPP_DATA)
{
DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
}
else
{
cupsFileSeek(fp, pos + length);
}
if (cupsFileTell(fp) != (pos + length))
{
DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
}
else if (!_cups_strcasecmp(line, "NumBins"))
{
if (num_bins > 0)
{
DEBUG_puts("_ppdCacheCreateWithFile: NumBins listed multiple times.");
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if ((num_bins = atoi(value)) <= 0 || num_bins > 65536)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumBins value %d on line "
"%d.", num_sizes, linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if ((pc->bins = calloc(num_bins, sizeof(_pwg_map_t))) == NULL)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d bins.",
num_sizes));
_cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
goto create_error;
}
}
else if (!_cups_strcasecmp(line, "Bin"))
{
if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Bad Bin on line %d.", linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if (pc->num_bins >= num_bins)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Too many Bin's on line %d.",
linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
map = pc->bins + pc->num_bins;
map->pwg = _cupsStrAlloc(pwg_keyword);
map->ppd = _cupsStrAlloc(ppd_keyword);
pc->num_bins ++;
}
else if (!_cups_strcasecmp(line, "NumSizes"))
{
if (num_sizes > 0)
{
DEBUG_puts("_ppdCacheCreateWithFile: NumSizes listed multiple times.");
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if ((num_sizes = atoi(value)) <= 0 || num_sizes > 65536)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumSizes value %d on line "
"%d.", num_sizes, linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if ((pc->sizes = calloc(num_sizes, sizeof(_pwg_size_t))) == NULL)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sizes.",
num_sizes));
_cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
goto create_error;
}
}
else if (!_cups_strcasecmp(line, "Size"))
{
if (pc->num_sizes >= num_sizes)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Too many Size's on line %d.",
linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
size = pc->sizes + pc->num_sizes;
if (sscanf(value, "%127s%40s%d%d%d%d%d%d", pwg_keyword, ppd_keyword,
&(size->width), &(size->length), &(size->left),
&(size->bottom), &(size->right), &(size->top)) != 8)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Bad Size on line %d.",
linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
size->map.pwg = _cupsStrAlloc(pwg_keyword);
size->map.ppd = _cupsStrAlloc(ppd_keyword);
pc->num_sizes ++;
}
else if (!_cups_strcasecmp(line, "CustomSize"))
{
if (pc->custom_max_width > 0)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Too many CustomSize's on line "
"%d.", linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if (sscanf(value, "%d%d%d%d%d%d%d%d", &(pc->custom_max_width),
&(pc->custom_max_length), &(pc->custom_min_width),
&(pc->custom_min_length), &(pc->custom_size.left),
&(pc->custom_size.bottom), &(pc->custom_size.right),
&(pc->custom_size.top)) != 8)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Bad CustomSize on line %d.",
linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
_pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), "custom", "max",
pc->custom_max_width, pc->custom_max_length);
pc->custom_max_keyword = _cupsStrAlloc(pwg_keyword);
_pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), "custom", "min",
pc->custom_min_width, pc->custom_min_length);
pc->custom_min_keyword = _cupsStrAlloc(pwg_keyword);
}
else if (!_cups_strcasecmp(line, "SourceOption"))
{
pc->source_option = _cupsStrAlloc(value);
}
else if (!_cups_strcasecmp(line, "NumSources"))
{
if (num_sources > 0)
{
DEBUG_puts("_ppdCacheCreateWithFile: NumSources listed multiple "
"times.");
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if ((num_sources = atoi(value)) <= 0 || num_sources > 65536)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumSources value %d on "
"line %d.", num_sources, linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if ((pc->sources = calloc(num_sources, sizeof(_pwg_map_t))) == NULL)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sources.",
num_sources));
_cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
goto create_error;
}
}
else if (!_cups_strcasecmp(line, "Source"))
{
if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Bad Source on line %d.",
linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if (pc->num_sources >= num_sources)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Too many Source's on line %d.",
linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
map = pc->sources + pc->num_sources;
map->pwg = _cupsStrAlloc(pwg_keyword);
map->ppd = _cupsStrAlloc(ppd_keyword);
pc->num_sources ++;
}
else if (!_cups_strcasecmp(line, "NumTypes"))
{
if (num_types > 0)
{
DEBUG_puts("_ppdCacheCreateWithFile: NumTypes listed multiple times.");
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if ((num_types = atoi(value)) <= 0 || num_types > 65536)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumTypes value %d on "
"line %d.", num_types, linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if ((pc->types = calloc(num_types, sizeof(_pwg_map_t))) == NULL)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d types.",
num_types));
_cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
goto create_error;
}
}
else if (!_cups_strcasecmp(line, "Type"))
{
if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Bad Type on line %d.",
linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if (pc->num_types >= num_types)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Too many Type's on line %d.",
linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
map = pc->types + pc->num_types;
map->pwg = _cupsStrAlloc(pwg_keyword);
map->ppd = _cupsStrAlloc(ppd_keyword);
pc->num_types ++;
}
else if (!_cups_strcasecmp(line, "Preset"))
{
print_color_mode = (_pwg_print_color_mode_t)strtol(value, &valueptr, 10);
print_quality = (_pwg_print_quality_t)strtol(valueptr, &valueptr, 10);
if (print_color_mode < _PWG_PRINT_COLOR_MODE_MONOCHROME ||
print_color_mode >= _PWG_PRINT_COLOR_MODE_MAX ||
print_quality < _PWG_PRINT_QUALITY_DRAFT ||
print_quality >= _PWG_PRINT_QUALITY_MAX ||
valueptr == value || !*valueptr)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Bad Preset on line %d.",
linenum));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
pc->num_presets[print_color_mode][print_quality] =
cupsParseOptions(valueptr, 0,
pc->presets[print_color_mode] + print_quality);
}
else if (!_cups_strcasecmp(line, "SidesOption"))
pc->sides_option = _cupsStrAlloc(value);
else if (!_cups_strcasecmp(line, "Sides1Sided"))
pc->sides_1sided = _cupsStrAlloc(value);
else if (!_cups_strcasecmp(line, "Sides2SidedLong"))
pc->sides_2sided_long = _cupsStrAlloc(value);
else if (!_cups_strcasecmp(line, "Sides2SidedShort"))
pc->sides_2sided_short = _cupsStrAlloc(value);
else if (!_cups_strcasecmp(line, "Finishings"))
{
if (!pc->finishings)
pc->finishings =
cupsArrayNew3((cups_array_func_t)pwg_compare_finishings,
NULL, NULL, 0, NULL,
(cups_afree_func_t)pwg_free_finishings);
if ((finishings = calloc(1, sizeof(_pwg_finishings_t))) == NULL)
goto create_error;
finishings->value = strtol(value, &valueptr, 10);
finishings->num_options = cupsParseOptions(valueptr, 0,
&(finishings->options));
cupsArrayAdd(pc->finishings, finishings);
}
else if (!_cups_strcasecmp(line, "MaxCopies"))
pc->max_copies = atoi(value);
else
{
DEBUG_printf(("_ppdCacheCreateWithFile: Unknown %s on line %d.", line,
linenum));
}
}
if (pc->num_sizes < num_sizes)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Not enough sizes (%d < %d).",
pc->num_sizes, num_sizes));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if (pc->num_sources < num_sources)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Not enough sources (%d < %d).",
pc->num_sources, num_sources));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
if (pc->num_types < num_types)
{
DEBUG_printf(("_ppdCacheCreateWithFile: Not enough types (%d < %d).",
pc->num_types, num_types));
_cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
goto create_error;
}
cupsFileClose(fp);
return (pc);
create_error:
cupsFileClose(fp);
_ppdCacheDestroy(pc);
if (attrs)
{
ippDelete(*attrs);
*attrs = NULL;
}
return (NULL);
}
_ppd_cache_t *
_ppdCacheCreateWithPPD(ppd_file_t *ppd)
{
int i, j, k;
_ppd_cache_t *pc;
ppd_option_t *input_slot,
*media_type,
*output_bin,
*color_model,
*duplex;
ppd_choice_t *choice;
_pwg_map_t *map;
ppd_attr_t *ppd_attr;
int num_options;
cups_option_t *options;
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;
_pwg_print_color_mode_t pwg_print_color_mode;
_pwg_print_quality_t pwg_print_quality;
int similar;
_pwg_size_t *old_size;
int old_imageable,
old_borderless,
old_known_pwg;
int new_width,
new_length,
new_left,
new_bottom,
new_right,
new_top,
new_imageable,
new_borderless,
new_known_pwg;
_pwg_size_t *new_size;
const char *filter;
_pwg_finishings_t *finishings;
DEBUG_printf(("_ppdCacheCreateWithPPD(ppd=%p)", ppd));
if (!ppd)
return (NULL);
if ((pc = calloc(1, sizeof(_ppd_cache_t))) == NULL)
{
DEBUG_puts("_ppdCacheCreateWithPPD: Unable to allocate _ppd_cache_t.");
goto create_error;
}
if (ppd->num_sizes == 0)
{
DEBUG_puts("_ppdCacheCreateWithPPD: No page sizes in PPD.");
goto create_error;
}
if ((pc->sizes = calloc(ppd->num_sizes, sizeof(_pwg_size_t))) == NULL)
{
DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
"_pwg_size_t's.", ppd->num_sizes));
goto create_error;
}
for (i = ppd->num_sizes, pwg_size = pc->sizes, ppd_size = ppd->sizes;
i > 0;
i --, ppd_size ++)
{
if (!_cups_strcasecmp(ppd_size->name, "Custom"))
continue;
if ((pwg_media = _pwgMediaForPPD(ppd_size->name)) != NULL)
{
for (j = 0; j < pc->num_sizes; j ++)
if (!strcmp(pc->sizes[j].map.pwg, pwg_media->pwg))
{
pwg_media = NULL;
break;
}
}
if (pwg_media)
{
pwg_name = pwg_media->pwg;
new_known_pwg = 1;
}
else
{
pwg_name = pwg_keyword;
new_known_pwg = 0;
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));
}
if ((pwg_media = _pwgMediaForSize(_PWG_FROMPTS(ppd_size->width),
_PWG_FROMPTS(ppd_size->length))) == NULL)
continue;
new_width = pwg_media->width;
new_length = pwg_media->length;
new_left = _PWG_FROMPTS(ppd_size->left);
new_bottom = _PWG_FROMPTS(ppd_size->bottom);
new_right = _PWG_FROMPTS(ppd_size->width - ppd_size->right);
new_top = _PWG_FROMPTS(ppd_size->length - ppd_size->top);
new_imageable = new_length - new_top - new_bottom;
new_borderless = new_bottom == 0 && new_top == 0 &&
new_left == 0 && new_right == 0;
for (k = pc->num_sizes, similar = 0, old_size = pc->sizes, new_size = NULL;
k > 0 && !similar;
k --, old_size ++)
{
old_imageable = old_size->length - old_size->top - old_size->bottom;
old_borderless = old_size->left == 0 && old_size->bottom == 0 &&
old_size->right == 0 && old_size->top == 0;
old_known_pwg = strncmp(old_size->map.pwg, "oe_", 3) &&
strncmp(old_size->map.pwg, "om_", 3);
similar = old_borderless == new_borderless &&
_PWG_EQUIVALENT(old_size->width, new_width) &&
_PWG_EQUIVALENT(old_size->length, new_length);
if (similar &&
(new_known_pwg || (!old_known_pwg && new_imageable > old_imageable)))
{
new_size = old_size;
_cupsStrFree(old_size->map.ppd);
_cupsStrFree(old_size->map.pwg);
}
}
if (!similar)
{
new_size = pwg_size ++;
pc->num_sizes ++;
}
if (new_size)
{
new_size->map.ppd = _cupsStrAlloc(ppd_size->name);
new_size->map.pwg = _cupsStrAlloc(pwg_name);
new_size->width = new_width;
new_size->length = new_length;
new_size->left = new_left;
new_size->bottom = new_bottom;
new_size->right = new_right;
new_size->top = new_top;
}
}
if (ppd->variable_sizes)
{
_pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), "custom", "max",
_PWG_FROMPTS(ppd->custom_max[0]),
_PWG_FROMPTS(ppd->custom_max[1]));
pc->custom_max_keyword = _cupsStrAlloc(pwg_keyword);
pc->custom_max_width = _PWG_FROMPTS(ppd->custom_max[0]);
pc->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]));
pc->custom_min_keyword = _cupsStrAlloc(pwg_keyword);
pc->custom_min_width = _PWG_FROMPTS(ppd->custom_min[0]);
pc->custom_min_length = _PWG_FROMPTS(ppd->custom_min[1]);
pc->custom_size.left = _PWG_FROMPTS(ppd->custom_margins[0]);
pc->custom_size.bottom = _PWG_FROMPTS(ppd->custom_margins[1]);
pc->custom_size.right = _PWG_FROMPTS(ppd->custom_margins[2]);
pc->custom_size.top = _PWG_FROMPTS(ppd->custom_margins[3]);
}
if ((input_slot = ppdFindOption(ppd, "InputSlot")) == NULL)
input_slot = ppdFindOption(ppd, "HPPaperSource");
if (input_slot)
{
pc->source_option = _cupsStrAlloc(input_slot->keyword);
if ((pc->sources = calloc(input_slot->num_choices,
sizeof(_pwg_map_t))) == NULL)
{
DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
"_pwg_map_t's for InputSlot.", input_slot->num_choices));
goto create_error;
}
pc->num_sources = input_slot->num_choices;
for (i = input_slot->num_choices, choice = input_slot->choices,
map = pc->sources;
i > 0;
i --, choice ++, map ++)
{
if (!_cups_strncasecmp(choice->choice, "Auto", 4) ||
!_cups_strcasecmp(choice->choice, "Default"))
pwg_name = "auto";
else if (!_cups_strcasecmp(choice->choice, "Cassette"))
pwg_name = "main";
else if (!_cups_strcasecmp(choice->choice, "PhotoTray"))
pwg_name = "photo";
else if (!_cups_strcasecmp(choice->choice, "CDTray"))
pwg_name = "disc";
else if (!_cups_strncasecmp(choice->choice, "Multipurpose", 12) ||
!_cups_strcasecmp(choice->choice, "MP") ||
!_cups_strcasecmp(choice->choice, "MPTray"))
pwg_name = "by-pass-tray";
else if (!_cups_strcasecmp(choice->choice, "LargeCapacity"))
pwg_name = "large-capacity";
else if (!_cups_strncasecmp(choice->choice, "Lower", 5))
pwg_name = "bottom";
else if (!_cups_strncasecmp(choice->choice, "Middle", 6))
pwg_name = "middle";
else if (!_cups_strncasecmp(choice->choice, "Upper", 5))
pwg_name = "top";
else if (!_cups_strncasecmp(choice->choice, "Side", 4))
pwg_name = "side";
else if (!_cups_strcasecmp(choice->choice, "Roll"))
pwg_name = "main-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 ((pc->types = calloc(media_type->num_choices,
sizeof(_pwg_map_t))) == NULL)
{
DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
"_pwg_map_t's for MediaType.", media_type->num_choices));
goto create_error;
}
pc->num_types = media_type->num_choices;
for (i = media_type->num_choices, choice = media_type->choices,
map = pc->types;
i > 0;
i --, choice ++, map ++)
{
if (!_cups_strncasecmp(choice->choice, "Auto", 4) ||
!_cups_strcasecmp(choice->choice, "Any") ||
!_cups_strcasecmp(choice->choice, "Default"))
pwg_name = "auto";
else if (!_cups_strncasecmp(choice->choice, "Card", 4))
pwg_name = "cardstock";
else if (!_cups_strncasecmp(choice->choice, "Env", 3))
pwg_name = "envelope";
else if (!_cups_strncasecmp(choice->choice, "Gloss", 5))
pwg_name = "photographic-glossy";
else if (!_cups_strcasecmp(choice->choice, "HighGloss"))
pwg_name = "photographic-high-gloss";
else if (!_cups_strcasecmp(choice->choice, "Matte"))
pwg_name = "photographic-matte";
else if (!_cups_strncasecmp(choice->choice, "Plain", 5))
pwg_name = "stationery";
else if (!_cups_strncasecmp(choice->choice, "Coated", 6))
pwg_name = "stationery-coated";
else if (!_cups_strcasecmp(choice->choice, "Inkjet"))
pwg_name = "stationery-inkjet";
else if (!_cups_strcasecmp(choice->choice, "Letterhead"))
pwg_name = "stationery-letterhead";
else if (!_cups_strncasecmp(choice->choice, "Preprint", 8))
pwg_name = "stationery-preprinted";
else if (!_cups_strcasecmp(choice->choice, "Recycled"))
pwg_name = "stationery-recycled";
else if (!_cups_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 ((pc->bins = calloc(output_bin->num_choices,
sizeof(_pwg_map_t))) == NULL)
{
DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
"_pwg_map_t's for OutputBin.", output_bin->num_choices));
goto create_error;
}
pc->num_bins = output_bin->num_choices;
for (i = output_bin->num_choices, choice = output_bin->choices,
map = pc->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);
}
}
if ((ppd_attr = ppdFindAttr(ppd, "APPrinterPreset", NULL)) != NULL)
{
const char *quality,
*output_mode,
*color_model_val,
*graphicsType,
*media_front_coating;
do
{
num_options = _ppdParseOptions(ppd_attr->value, 0, &options,
_PPD_PARSE_ALL);
if ((quality = cupsGetOption("com.apple.print.preset.quality",
num_options, options)) != NULL)
{
if (!strcmp(quality, "low"))
pwg_print_quality = _PWG_PRINT_QUALITY_DRAFT;
else if (!strcmp(quality, "high"))
pwg_print_quality = _PWG_PRINT_QUALITY_HIGH;
else
pwg_print_quality = _PWG_PRINT_QUALITY_NORMAL;
graphicsType = cupsGetOption("com.apple.print.preset.graphicsType",
num_options, options);
if (pwg_print_quality != _PWG_PRINT_QUALITY_HIGH && graphicsType &&
!strcmp(graphicsType, "Photo"))
continue;
media_front_coating = cupsGetOption(
"com.apple.print.preset.media-front-coating",
num_options, options);
if (pwg_print_quality != _PWG_PRINT_QUALITY_HIGH &&
media_front_coating &&
strcmp(media_front_coating, "none") &&
strcmp(media_front_coating, "autodetect"))
continue;
output_mode = cupsGetOption("com.apple.print.preset.output-mode",
num_options, options);
color_model_val = cupsGetOption("ColorModel", num_options, options);
if (output_mode)
{
if (!strcmp(output_mode, "monochrome"))
pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME;
else
pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
}
else if (color_model_val)
{
if (!_cups_strcasecmp(color_model_val, "Gray"))
pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME;
else
pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
}
else
pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
if (!pc->num_presets[pwg_print_color_mode][pwg_print_quality])
pc->num_presets[pwg_print_color_mode][pwg_print_quality] =
_ppdParseOptions(ppd_attr->value, 0,
pc->presets[pwg_print_color_mode] +
pwg_print_quality, _PPD_PARSE_OPTIONS);
}
cupsFreeOptions(num_options, options);
}
while ((ppd_attr = ppdFindNextAttr(ppd, "APPrinterPreset", NULL)) != NULL);
}
if (!pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_DRAFT] &&
!pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_NORMAL] &&
!pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_HIGH])
{
const char *color_option = NULL,
*gray_choice = NULL;
if ((color_model = ppdFindOption(ppd, "ColorModel")) != NULL &&
ppdFindChoice(color_model, "Gray"))
{
color_option = "ColorModel";
gray_choice = "Gray";
}
else if ((color_model = ppdFindOption(ppd, "HPColorMode")) != NULL &&
ppdFindChoice(color_model, "grayscale"))
{
color_option = "HPColorMode";
gray_choice = "grayscale";
}
else if ((color_model = ppdFindOption(ppd, "BRMonoColor")) != NULL &&
ppdFindChoice(color_model, "Mono"))
{
color_option = "BRMonoColor";
gray_choice = "Mono";
}
else if ((color_model = ppdFindOption(ppd, "CNIJSGrayScale")) != NULL &&
ppdFindChoice(color_model, "1"))
{
color_option = "CNIJSGrayScale";
gray_choice = "1";
}
else if ((color_model = ppdFindOption(ppd, "HPColorAsGray")) != NULL &&
ppdFindChoice(color_model, "True"))
{
color_option = "HPColorAsGray";
gray_choice = "True";
}
if (color_option && gray_choice)
{
cups_option_t *coption,
*moption;
for (pwg_print_quality = _PWG_PRINT_QUALITY_DRAFT;
pwg_print_quality < _PWG_PRINT_QUALITY_MAX;
pwg_print_quality ++)
{
if (pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR][pwg_print_quality])
{
num_options = pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR]
[pwg_print_quality];
options = calloc(sizeof(cups_option_t), num_options);
if (options)
{
for (i = num_options, moption = options,
coption = pc->presets[_PWG_PRINT_COLOR_MODE_COLOR]
[pwg_print_quality];
i > 0;
i --, moption ++, coption ++)
{
moption->name = _cupsStrRetain(coption->name);
moption->value = _cupsStrRetain(coption->value);
}
pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] =
num_options;
pc->presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] =
options;
}
}
else if (pwg_print_quality != _PWG_PRINT_QUALITY_NORMAL)
continue;
pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] =
cupsAddOption(color_option, gray_choice,
pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME]
[pwg_print_quality],
pc->presets[_PWG_PRINT_COLOR_MODE_MONOCHROME] +
pwg_print_quality);
}
}
}
if ((duplex = ppdFindOption(ppd, "Duplex")) == NULL)
if ((duplex = ppdFindOption(ppd, "JCLDuplex")) == NULL)
if ((duplex = ppdFindOption(ppd, "EFDuplex")) == NULL)
if ((duplex = ppdFindOption(ppd, "EFDuplexing")) == NULL)
duplex = ppdFindOption(ppd, "KD03Duplex");
if (duplex)
{
pc->sides_option = _cupsStrAlloc(duplex->keyword);
for (i = duplex->num_choices, choice = duplex->choices;
i > 0;
i --, choice ++)
{
if ((!_cups_strcasecmp(choice->choice, "None") ||
!_cups_strcasecmp(choice->choice, "False")) && !pc->sides_1sided)
pc->sides_1sided = _cupsStrAlloc(choice->choice);
else if ((!_cups_strcasecmp(choice->choice, "DuplexNoTumble") ||
!_cups_strcasecmp(choice->choice, "LongEdge") ||
!_cups_strcasecmp(choice->choice, "Top")) && !pc->sides_2sided_long)
pc->sides_2sided_long = _cupsStrAlloc(choice->choice);
else if ((!_cups_strcasecmp(choice->choice, "DuplexTumble") ||
!_cups_strcasecmp(choice->choice, "ShortEdge") ||
!_cups_strcasecmp(choice->choice, "Bottom")) &&
!pc->sides_2sided_short)
pc->sides_2sided_short = _cupsStrAlloc(choice->choice);
}
}
pc->filters = cupsArrayNew3(NULL, NULL, NULL, 0,
(cups_acopy_func_t)_cupsStrAlloc,
(cups_afree_func_t)_cupsStrFree);
cupsArrayAdd(pc->filters,
"application/vnd.cups-raw application/octet-stream 0 -");
if ((ppd_attr = ppdFindAttr(ppd, "cupsFilter2", NULL)) != NULL)
{
do
{
cupsArrayAdd(pc->filters, ppd_attr->value);
}
while ((ppd_attr = ppdFindNextAttr(ppd, "cupsFilter2", NULL)) != NULL);
}
else if (ppd->num_filters > 0)
{
for (i = 0; i < ppd->num_filters; i ++)
cupsArrayAdd(pc->filters, ppd->filters[i]);
}
else
cupsArrayAdd(pc->filters, "application/vnd.cups-postscript 0 -");
for (filter = (const char *)cupsArrayFirst(pc->filters);
filter;
filter = (const char *)cupsArrayNext(pc->filters))
if (!_cups_strncasecmp(filter, "application/vnd.cups-command", 28) &&
_cups_isspace(filter[28]))
break;
if (!filter &&
((ppd_attr = ppdFindAttr(ppd, "cupsCommands", NULL)) == NULL ||
_cups_strcasecmp(ppd_attr->value, "none")))
{
for (filter = (const char *)cupsArrayFirst(pc->filters);
filter;
filter = (const char *)cupsArrayNext(pc->filters))
if (!_cups_strncasecmp(filter, "application/vnd.cups-postscript", 31) &&
_cups_isspace(filter[31]))
break;
if (filter)
cupsArrayAdd(pc->filters,
"application/vnd.cups-command application/postscript 100 "
"commandtops");
}
if ((ppd_attr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
{
pc->prefilters = cupsArrayNew3(NULL, NULL, NULL, 0,
(cups_acopy_func_t)_cupsStrAlloc,
(cups_afree_func_t)_cupsStrFree);
do
{
cupsArrayAdd(pc->prefilters, ppd_attr->value);
}
while ((ppd_attr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL)) != NULL);
}
if ((ppd_attr = ppdFindAttr(ppd, "cupsSingleFile", NULL)) != NULL)
pc->single_file = !_cups_strcasecmp(ppd_attr->value, "true");
if (ppd->product)
pc->product = _cupsStrAlloc(ppd->product);
if ((ppd_attr = ppdFindAttr(ppd, "cupsIPPFinishings", NULL)) != NULL)
{
pc->finishings = cupsArrayNew3((cups_array_func_t)pwg_compare_finishings,
NULL, NULL, 0, NULL,
(cups_afree_func_t)pwg_free_finishings);
do
{
if ((finishings = calloc(1, sizeof(_pwg_finishings_t))) == NULL)
goto create_error;
finishings->value = atoi(ppd_attr->spec);
finishings->num_options = _ppdParseOptions(ppd_attr->value, 0,
&(finishings->options),
_PPD_PARSE_OPTIONS);
cupsArrayAdd(pc->finishings, finishings);
}
while ((ppd_attr = ppdFindNextAttr(ppd, "cupsIPPFinishings",
NULL)) != NULL);
}
if ((ppd_attr = ppdFindAttr(ppd, "cupsMaxCopies", NULL)) != NULL)
pc->max_copies = atoi(ppd_attr->value);
else if (ppd->manual_copies)
pc->max_copies = 1;
else
pc->max_copies = 9999;
return (pc);
create_error:
_cupsSetError(IPP_INTERNAL_ERROR, _("Out of memory."), 1);
_ppdCacheDestroy(pc);
return (NULL);
}
void
_ppdCacheDestroy(_ppd_cache_t *pc)
{
int i;
_pwg_map_t *map;
_pwg_size_t *size;
if (!pc)
return;
if (pc->bins)
{
for (i = pc->num_bins, map = pc->bins; i > 0; i --, map ++)
{
_cupsStrFree(map->pwg);
_cupsStrFree(map->ppd);
}
free(pc->bins);
}
if (pc->sizes)
{
for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
{
_cupsStrFree(size->map.pwg);
_cupsStrFree(size->map.ppd);
}
free(pc->sizes);
}
if (pc->source_option)
_cupsStrFree(pc->source_option);
if (pc->sources)
{
for (i = pc->num_sources, map = pc->sources; i > 0; i --, map ++)
{
_cupsStrFree(map->pwg);
_cupsStrFree(map->ppd);
}
free(pc->sources);
}
if (pc->types)
{
for (i = pc->num_types, map = pc->types; i > 0; i --, map ++)
{
_cupsStrFree(map->pwg);
_cupsStrFree(map->ppd);
}
free(pc->types);
}
if (pc->custom_max_keyword)
_cupsStrFree(pc->custom_max_keyword);
if (pc->custom_min_keyword)
_cupsStrFree(pc->custom_min_keyword);
_cupsStrFree(pc->product);
cupsArrayDelete(pc->filters);
cupsArrayDelete(pc->prefilters);
cupsArrayDelete(pc->finishings);
free(pc);
}
const char *
_ppdCacheGetBin(
_ppd_cache_t *pc,
const char *output_bin)
{
int i;
if (!pc || !output_bin)
return (NULL);
for (i = 0; i < pc->num_bins; i ++)
if (!_cups_strcasecmp(output_bin, pc->bins[i].ppd))
return (pc->bins[i].pwg);
return (NULL);
}
int
_ppdCacheGetFinishingOptions(
_ppd_cache_t *pc,
ipp_t *job,
ipp_finish_t value,
int num_options,
cups_option_t **options)
{
int i;
_pwg_finishings_t *f,
key;
ipp_attribute_t *attr;
cups_option_t *option;
if (!pc || cupsArrayCount(pc->finishings) == 0 || !options ||
(!job && value == IPP_FINISHINGS_NONE))
return (num_options);
if (job && (attr = ippFindAttribute(job, "finishings", IPP_TAG_ENUM)) != NULL)
{
int num_values = ippGetCount(attr);
for (i = 0; i < num_values; i ++)
{
key.value = ippGetInteger(attr, i);
if ((f = cupsArrayFind(pc->finishings, &key)) != NULL)
{
int j;
for (j = f->num_options, option = f->options; j > 0; j --, option ++)
num_options = cupsAddOption(option->name, option->value,
num_options, options);
}
}
}
else if (value != IPP_FINISHINGS_NONE)
{
key.value = value;
if ((f = cupsArrayFind(pc->finishings, &key)) != NULL)
{
int j;
for (j = f->num_options, option = f->options; j > 0; j --, option ++)
num_options = cupsAddOption(option->name, option->value,
num_options, options);
}
}
return (num_options);
}
int
_ppdCacheGetFinishingValues(
_ppd_cache_t *pc,
int num_options,
cups_option_t *options,
int max_values,
int *values)
{
int i,
num_values = 0;
_pwg_finishings_t *f;
cups_option_t *option;
const char *val;
if (!pc || !pc->finishings || num_options < 1 || max_values < 1 || !values)
return (0);
for (f = (_pwg_finishings_t *)cupsArrayFirst(pc->finishings);
f;
f = (_pwg_finishings_t *)cupsArrayNext(pc->finishings))
{
for (i = f->num_options, option = f->options; i > 0; i --, option ++)
if ((val = cupsGetOption(option->name, num_options, options)) == NULL ||
_cups_strcasecmp(option->value, val))
break;
if (i == 0)
{
values[num_values ++] = f->value;
if (num_values >= max_values)
break;
}
}
return (num_values);
}
const char *
_ppdCacheGetInputSlot(
_ppd_cache_t *pc,
ipp_t *job,
const char *keyword)
{
if (!pc || pc->num_sources == 0 || (!job && !keyword))
return (NULL);
if (job && !keyword)
{
ipp_attribute_t *media_col,
*media_source;
_pwg_size_t size;
int margins_set;
media_col = ippFindAttribute(job, "media-col", IPP_TAG_BEGIN_COLLECTION);
if (media_col &&
(media_source = ippFindAttribute(ippGetCollection(media_col, 0),
"media-source",
IPP_TAG_KEYWORD)) != NULL)
{
keyword = ippGetString(media_source, 0, NULL);
}
else if (_pwgInitSize(&size, job, &margins_set))
{
if (size.width <= (5 * 2540) && size.length <= (7 * 2540))
keyword = "photo";
}
}
if (keyword)
{
int i;
for (i = 0; i < pc->num_sources; i ++)
if (!_cups_strcasecmp(keyword, pc->sources[i].pwg))
return (pc->sources[i].ppd);
}
return (NULL);
}
const char *
_ppdCacheGetMediaType(
_ppd_cache_t *pc,
ipp_t *job,
const char *keyword)
{
if (!pc || pc->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 < pc->num_types; i ++)
if (!_cups_strcasecmp(keyword, pc->types[i].pwg))
return (pc->types[i].ppd);
}
return (NULL);
}
const char *
_ppdCacheGetOutputBin(
_ppd_cache_t *pc,
const char *output_bin)
{
int i;
if (!pc || !output_bin)
return (NULL);
for (i = 0; i < pc->num_bins; i ++)
if (!_cups_strcasecmp(output_bin, pc->bins[i].pwg))
return (pc->bins[i].ppd);
return (NULL);
}
const char *
_ppdCacheGetPageSize(
_ppd_cache_t *pc,
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;
const char *ppd_name;
DEBUG_printf(("_ppdCacheGetPageSize(pc=%p, job=%p, keyword=\"%s\", exact=%p)",
pc, job, keyword, exact));
if (!pc || (!job && !keyword))
return (NULL);
if (exact)
*exact = 0;
ppd_name = keyword;
if (job)
{
ipp_attribute_t *attr;
if ((attr = ippFindAttribute(job, "PageSize", IPP_TAG_ZERO)) == NULL)
if ((attr = ippFindAttribute(job, "PageRegion", IPP_TAG_ZERO)) == NULL)
attr = ippFindAttribute(job, "media", IPP_TAG_ZERO);
#ifdef DEBUG
if (attr)
DEBUG_printf(("1_ppdCacheGetPageSize: Found attribute %s (%s)",
attr->name, ippTagString(attr->value_tag)));
else
DEBUG_puts("1_ppdCacheGetPageSize: Did not find media attribute.");
#endif
if (attr && (attr->value_tag == IPP_TAG_NAME ||
attr->value_tag == IPP_TAG_KEYWORD))
ppd_name = attr->values[0].string.text;
}
DEBUG_printf(("1_ppdCacheGetPageSize: ppd_name=\"%s\"", ppd_name));
if (ppd_name)
{
for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
{
DEBUG_printf(("2_ppdCacheGetPageSize: size[%d]=[\"%s\" \"%s\"]",
(int)(size - pc->sizes), size->map.pwg, size->map.ppd));
if (!_cups_strcasecmp(ppd_name, size->map.ppd) ||
!_cups_strcasecmp(ppd_name, size->map.pwg))
{
if (exact)
*exact = 1;
DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", ppd_name));
return (size->map.ppd);
}
}
}
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)
if ((media = _pwgMediaForPPD(keyword)) == NULL)
return (NULL);
jobsize.width = media->width;
jobsize.length = media->length;
margins_set = 0;
}
closest = NULL;
dclosest = 999999999;
if (!ppd_name || _cups_strncasecmp(ppd_name, "Custom.", 7) ||
_cups_strncasecmp(ppd_name, "custom_", 7))
{
for (i = pc->num_sizes, size = pc->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;
DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", size->map.ppd));
return (size->map.ppd);
}
}
if (closest)
{
DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (closest)",
closest->map.ppd));
return (closest->map.ppd);
}
if (jobsize.width >= pc->custom_min_width &&
jobsize.width <= pc->custom_max_width &&
jobsize.length >= pc->custom_min_length &&
jobsize.length <= pc->custom_max_length)
{
snprintf(pc->custom_ppd_size, sizeof(pc->custom_ppd_size), "Custom.%dx%d",
(int)_PWG_TOPTS(jobsize.width), (int)_PWG_TOPTS(jobsize.length));
if (margins_set && exact)
{
dleft = pc->custom_size.left - jobsize.left;
dright = pc->custom_size.right - jobsize.right;
dtop = pc->custom_size.top - jobsize.top;
dbottom = pc->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;
DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (custom)",
pc->custom_ppd_size));
return (pc->custom_ppd_size);
}
DEBUG_puts("1_ppdCacheGetPageSize: Returning NULL");
return (NULL);
}
_pwg_size_t *
_ppdCacheGetSize(
_ppd_cache_t *pc,
const char *page_size)
{
int i;
_pwg_media_t *media;
_pwg_size_t *size;
if (!pc || !page_size)
return (NULL);
if (!_cups_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 (!_cups_strcasecmp(ptr, "in"))
{
w *= 2540.0;
l *= 2540.0;
}
else if (!_cups_strcasecmp(ptr, "ft"))
{
w *= 12.0 * 2540.0;
l *= 12.0 * 2540.0;
}
else if (!_cups_strcasecmp(ptr, "mm"))
{
w *= 100.0;
l *= 100.0;
}
else if (!_cups_strcasecmp(ptr, "cm"))
{
w *= 1000.0;
l *= 1000.0;
}
else if (!_cups_strcasecmp(ptr, "m"))
{
w *= 100000.0;
l *= 100000.0;
}
else
{
w *= 2540.0 / 72.0;
l *= 2540.0 / 72.0;
}
pc->custom_size.width = (int)w;
pc->custom_size.length = (int)l;
return (&(pc->custom_size));
}
for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
if (!_cups_strcasecmp(page_size, size->map.ppd) ||
!_cups_strcasecmp(page_size, size->map.pwg))
return (size);
if ((media = _pwgMediaForPPD(page_size)) == NULL)
if ((media = _pwgMediaForLegacy(page_size)) == NULL)
media = _pwgMediaForPWG(page_size);
if (media)
{
pc->custom_size.width = media->width;
pc->custom_size.length = media->length;
return (&(pc->custom_size));
}
return (NULL);
}
const char *
_ppdCacheGetSource(
_ppd_cache_t *pc,
const char *input_slot)
{
int i;
_pwg_map_t *source;
if (!pc || !input_slot)
return (NULL);
for (i = pc->num_sources, source = pc->sources; i > 0; i --, source ++)
if (!_cups_strcasecmp(input_slot, source->ppd))
return (source->pwg);
return (NULL);
}
const char *
_ppdCacheGetType(
_ppd_cache_t *pc,
const char *media_type)
{
int i;
_pwg_map_t *type;
if (!pc || !media_type)
return (NULL);
for (i = pc->num_types, type = pc->types; i > 0; i --, type ++)
if (!_cups_strcasecmp(media_type, type->ppd))
return (type->pwg);
return (NULL);
}
int
_ppdCacheWriteFile(
_ppd_cache_t *pc,
const char *filename,
ipp_t *attrs)
{
int i, j, k;
cups_file_t *fp;
_pwg_size_t *size;
_pwg_map_t *map;
_pwg_finishings_t *f;
cups_option_t *option;
const char *value;
char newfile[1024];
if (!pc || !filename)
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
return (0);
}
snprintf(newfile, sizeof(newfile), "%s.N", filename);
if ((fp = cupsFileOpen(newfile, "w9")) == NULL)
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
return (0);
}
cupsFilePrintf(fp, "#CUPS-PPD-CACHE-%d\n", _PPD_CACHE_VERSION);
if (pc->num_bins > 0)
{
cupsFilePrintf(fp, "NumBins %d\n", pc->num_bins);
for (i = pc->num_bins, map = pc->bins; i > 0; i --, map ++)
cupsFilePrintf(fp, "Bin %s %s\n", map->pwg, map->ppd);
}
cupsFilePrintf(fp, "NumSizes %d\n", pc->num_sizes);
for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
cupsFilePrintf(fp, "Size %s %s %d %d %d %d %d %d\n", size->map.pwg,
size->map.ppd, size->width, size->length, size->left,
size->bottom, size->right, size->top);
if (pc->custom_max_width > 0)
cupsFilePrintf(fp, "CustomSize %d %d %d %d %d %d %d %d\n",
pc->custom_max_width, pc->custom_max_length,
pc->custom_min_width, pc->custom_min_length,
pc->custom_size.left, pc->custom_size.bottom,
pc->custom_size.right, pc->custom_size.top);
if (pc->source_option)
cupsFilePrintf(fp, "SourceOption %s\n", pc->source_option);
if (pc->num_sources > 0)
{
cupsFilePrintf(fp, "NumSources %d\n", pc->num_sources);
for (i = pc->num_sources, map = pc->sources; i > 0; i --, map ++)
cupsFilePrintf(fp, "Source %s %s\n", map->pwg, map->ppd);
}
if (pc->num_types > 0)
{
cupsFilePrintf(fp, "NumTypes %d\n", pc->num_types);
for (i = pc->num_types, map = pc->types; i > 0; i --, map ++)
cupsFilePrintf(fp, "Type %s %s\n", map->pwg, map->ppd);
}
for (i = _PWG_PRINT_COLOR_MODE_MONOCHROME; i < _PWG_PRINT_COLOR_MODE_MAX; i ++)
for (j = _PWG_PRINT_QUALITY_DRAFT; j < _PWG_PRINT_QUALITY_MAX; j ++)
if (pc->num_presets[i][j])
{
cupsFilePrintf(fp, "Preset %d %d", i, j);
for (k = pc->num_presets[i][j], option = pc->presets[i][j];
k > 0;
k --, option ++)
cupsFilePrintf(fp, " %s=%s", option->name, option->value);
cupsFilePutChar(fp, '\n');
}
if (pc->sides_option)
cupsFilePrintf(fp, "SidesOption %s\n", pc->sides_option);
if (pc->sides_1sided)
cupsFilePrintf(fp, "Sides1Sided %s\n", pc->sides_1sided);
if (pc->sides_2sided_long)
cupsFilePrintf(fp, "Sides2SidedLong %s\n", pc->sides_2sided_long);
if (pc->sides_2sided_short)
cupsFilePrintf(fp, "Sides2SidedShort %s\n", pc->sides_2sided_short);
if (pc->product)
cupsFilePutConf(fp, "Product", pc->product);
for (value = (const char *)cupsArrayFirst(pc->filters);
value;
value = (const char *)cupsArrayNext(pc->filters))
cupsFilePutConf(fp, "Filter", value);
for (value = (const char *)cupsArrayFirst(pc->prefilters);
value;
value = (const char *)cupsArrayNext(pc->prefilters))
cupsFilePutConf(fp, "PreFilter", value);
cupsFilePrintf(fp, "SingleFile %s\n", pc->single_file ? "true" : "false");
for (f = (_pwg_finishings_t *)cupsArrayFirst(pc->finishings);
f;
f = (_pwg_finishings_t *)cupsArrayNext(pc->finishings))
{
cupsFilePrintf(fp, "Finishings %d", f->value);
for (i = f->num_options, option = f->options; i > 0; i --, option ++)
cupsFilePrintf(fp, " %s=%s", option->name, option->value);
cupsFilePutChar(fp, '\n');
}
cupsFilePrintf(fp, "MaxCopies %d\n", pc->max_copies);
if (attrs)
{
cupsFilePrintf(fp, "IPP " CUPS_LLFMT "\n", CUPS_LLCAST ippLength(attrs));
attrs->state = IPP_IDLE;
ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL, attrs);
}
if (cupsFileClose(fp))
{
unlink(newfile);
return (0);
}
unlink(filename);
return (!rename(newfile, filename));
}
const char *
_pwgInputSlotForSource(
const char *media_source,
char *name,
size_t namesize)
{
if (!media_source || !name || namesize < PPD_MAX_NAME)
return (NULL);
if (_cups_strcasecmp(media_source, "main"))
strlcpy(name, "Cassette", namesize);
else if (_cups_strcasecmp(media_source, "alternate"))
strlcpy(name, "Multipurpose", namesize);
else if (_cups_strcasecmp(media_source, "large-capacity"))
strlcpy(name, "LargeCapacity", namesize);
else if (_cups_strcasecmp(media_source, "bottom"))
strlcpy(name, "Lower", namesize);
else if (_cups_strcasecmp(media_source, "middle"))
strlcpy(name, "Middle", namesize);
else if (_cups_strcasecmp(media_source, "top"))
strlcpy(name, "Upper", namesize);
else if (_cups_strcasecmp(media_source, "rear"))
strlcpy(name, "Rear", namesize);
else if (_cups_strcasecmp(media_source, "side"))
strlcpy(name, "Side", namesize);
else if (_cups_strcasecmp(media_source, "envelope"))
strlcpy(name, "Envelope", namesize);
else if (_cups_strcasecmp(media_source, "main-roll"))
strlcpy(name, "Roll", namesize);
else if (_cups_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 (_cups_strcasecmp(media_type, "auto"))
strlcpy(name, "Auto", namesize);
else if (_cups_strcasecmp(media_type, "cardstock"))
strlcpy(name, "Cardstock", namesize);
else if (_cups_strcasecmp(media_type, "envelope"))
strlcpy(name, "Envelope", namesize);
else if (_cups_strcasecmp(media_type, "photographic-glossy"))
strlcpy(name, "Glossy", namesize);
else if (_cups_strcasecmp(media_type, "photographic-high-gloss"))
strlcpy(name, "HighGloss", namesize);
else if (_cups_strcasecmp(media_type, "photographic-matte"))
strlcpy(name, "Matte", namesize);
else if (_cups_strcasecmp(media_type, "stationery"))
strlcpy(name, "Plain", namesize);
else if (_cups_strcasecmp(media_type, "stationery-coated"))
strlcpy(name, "Coated", namesize);
else if (_cups_strcasecmp(media_type, "stationery-inkjet"))
strlcpy(name, "Inkjet", namesize);
else if (_cups_strcasecmp(media_type, "stationery-letterhead"))
strlcpy(name, "Letterhead", namesize);
else if (_cups_strcasecmp(media_type, "stationery-preprinted"))
strlcpy(name, "Preprinted", namesize);
else if (_cups_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 ||
(size_t)(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 int
pwg_compare_finishings(
_pwg_finishings_t *a,
_pwg_finishings_t *b)
{
return (b->value - a->value);
}
static void
pwg_free_finishings(
_pwg_finishings_t *f)
{
cupsFreeOptions(f->num_options, f->options);
free(f);
}
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 == '-' && _cups_isalpha(ipp[1]))
{
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 (_cups_isalnum(*ppd) || *ppd == '-')
*ptr++ = tolower(*ppd & 255);
else if (*ppd == '_' || *ppd == '.')
*ptr++ = '-';
if (!_cups_isupper(*ppd) && _cups_isalnum(*ppd) &&
_cups_isupper(ppd[1]) && ptr < end)
*ptr++ = '-';
}
*ptr = '\0';
}