#include "ppd.h"
#include "string.h"
#include <ctype.h>
ppd_size_t *
ppdPageSize(ppd_file_t *ppd,
const char *name)
{
int i;
float w, l;
char units[255];
if (ppd == NULL)
return (NULL);
if (name != NULL)
{
if (strncmp(name, "Custom.", 7) == 0 && ppd->variable_sizes)
{
for (i = 0; i < ppd->num_sizes; i ++)
if (strcmp("Custom", ppd->sizes[i].name) == 0)
break;
if (i == ppd->num_sizes)
return (NULL);
units[0] = '\0';
if (sscanf(name + 7, "%fx%f%254s", &w, &l, units) < 2)
return (NULL);
if (strcasecmp(units, "in") == 0)
{
ppd->sizes[i].width = w * 72.0f;
ppd->sizes[i].length = l * 72.0f;
ppd->sizes[i].left = ppd->custom_margins[0];
ppd->sizes[i].bottom = ppd->custom_margins[1];
ppd->sizes[i].right = w * 72.0f - ppd->custom_margins[2];
ppd->sizes[i].top = l * 72.0f - ppd->custom_margins[3];
}
else if (strcasecmp(units, "cm") == 0)
{
ppd->sizes[i].width = w / 2.54f * 72.0f;
ppd->sizes[i].length = l / 2.54f * 72.0f;
ppd->sizes[i].left = ppd->custom_margins[0];
ppd->sizes[i].bottom = ppd->custom_margins[1];
ppd->sizes[i].right = w / 2.54f * 72.0f - ppd->custom_margins[2];
ppd->sizes[i].top = l / 2.54f * 72.0f - ppd->custom_margins[3];
}
else if (strcasecmp(units, "mm") == 0)
{
ppd->sizes[i].width = w / 25.4f * 72.0f;
ppd->sizes[i].length = l / 25.4f * 72.0f;
ppd->sizes[i].left = ppd->custom_margins[0];
ppd->sizes[i].bottom = ppd->custom_margins[1];
ppd->sizes[i].right = w / 25.4f * 72.0f - ppd->custom_margins[2];
ppd->sizes[i].top = l / 25.4f * 72.0f - ppd->custom_margins[3];
}
else
{
ppd->sizes[i].width = w;
ppd->sizes[i].length = l;
ppd->sizes[i].left = ppd->custom_margins[0];
ppd->sizes[i].bottom = ppd->custom_margins[1];
ppd->sizes[i].right = w - ppd->custom_margins[2];
ppd->sizes[i].top = l - ppd->custom_margins[3];
}
return (ppd->sizes + i);
}
else
{
for (i = 0; i < ppd->num_sizes; i ++)
if (strcmp(name, ppd->sizes[i].name) == 0)
return (ppd->sizes + i);
}
}
else
{
for (i = 0; i < ppd->num_sizes; i ++)
if (ppd->sizes[i].marked)
return (ppd->sizes + i);
}
return (NULL);
}
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);
}