#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gimp-print/gimp-print.h>
#include "gimp-print-internal.h"
#include <gimp-print/gimp-print-intl-internal.h>
#include <time.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#ifdef _MSC_VER
#define strncasecmp(s,t,n) _strnicmp(s,t,n)
#define strcasecmp(s,t) _stricmp(s,t)
#endif
static FILE *ps_ppd = NULL;
static const char *ps_ppd_file = NULL;
static void ps_hex(const stp_vars_t, unsigned short *, int);
static void ps_ascii85(const stp_vars_t, unsigned short *, int, int);
static char *ppd_find(const char *, const char *, const char *, int *);
static char *
c_strdup(const char *s)
{
char *ret = stp_malloc(strlen(s) + 1);
strcpy(ret, s);
return ret;
}
static stp_param_t *
ps_parameters(const stp_printer_t printer,
const char *ppd_file,
const char *name,
int *count)
{
int i;
char line[1024],
lname[255],
loption[255],
*ltext;
stp_param_t *valptrs;
if (count == NULL)
return (NULL);
*count = 0;
if (ppd_file == NULL || name == NULL)
return (NULL);
if (ps_ppd_file == NULL || strcmp(ps_ppd_file, ppd_file) != 0)
{
if (ps_ppd != NULL)
fclose(ps_ppd);
ps_ppd = fopen(ppd_file, "r");
if (ps_ppd == NULL)
ps_ppd_file = NULL;
else
ps_ppd_file = ppd_file;
}
if (ps_ppd == NULL)
{
if (strcmp(name, "PageSize") == 0)
{
int papersizes = stp_known_papersizes();
valptrs = stp_zalloc(sizeof(stp_param_t) * papersizes);
*count = 0;
for (i = 0; i < papersizes; i++)
{
const stp_papersize_t pt = stp_get_papersize_by_index(i);
if (strlen(stp_papersize_get_name(pt)) > 0)
{
valptrs[*count].name = c_strdup(stp_papersize_get_name(pt));
valptrs[*count].text = c_strdup(stp_papersize_get_text(pt));
(*count)++;
}
}
return (valptrs);
}
else
return (NULL);
}
rewind(ps_ppd);
*count = 0;
valptrs = stp_zalloc(100 * sizeof(stp_param_t));
while (fgets(line, sizeof(line), ps_ppd) != NULL)
{
if (line[0] != '*')
continue;
if (sscanf(line, "*%s %[^:]", lname, loption) != 2)
continue;
if (strcasecmp(lname, name) == 0)
{
if ((ltext = strchr(loption, '/')) != NULL)
*ltext++ = '\0';
else
ltext = loption;
valptrs[(*count)].name = c_strdup(loption);
valptrs[(*count)].text = c_strdup(ltext);
(*count) ++;
}
}
if (*count == 0)
{
stp_free(valptrs);
return (NULL);
}
else
return (valptrs);
}
static const char *
ps_default_parameters(const stp_printer_t printer,
const char *ppd_file,
const char *name)
{
int i;
char line[1024],
lname[255],
loption[255],
defname[255];
if (ppd_file == NULL || name == NULL)
return (NULL);
sprintf(defname, "Default%s", name);
if (ps_ppd_file == NULL || strcmp(ps_ppd_file, ppd_file) != 0)
{
if (ps_ppd != NULL)
fclose(ps_ppd);
ps_ppd = fopen(ppd_file, "r");
if (ps_ppd == NULL)
ps_ppd_file = NULL;
else
ps_ppd_file = ppd_file;
}
if (ps_ppd == NULL)
{
if (strcmp(name, "PageSize") == 0)
{
int papersizes = stp_known_papersizes();
for (i = 0; i < papersizes; i++)
{
const stp_papersize_t pt = stp_get_papersize_by_index(i);
if (strlen(stp_papersize_get_name(pt)) > 0)
{
return stp_papersize_get_name(pt);
}
}
return NULL;
}
else
return (NULL);
}
rewind(ps_ppd);
while (fgets(line, sizeof(line), ps_ppd) != NULL)
{
if (line[0] != '*')
continue;
if (sscanf(line, "*%[^:]:%s", lname, loption) != 2)
continue;
if (strcasecmp(lname, defname) == 0)
{
return c_strdup(loption);
}
}
if (strcmp(name, "Resolution") == 0)
{
return "default";
}
return NULL;
}
static void
ps_media_size(const stp_printer_t printer,
const stp_vars_t v,
int *width,
int *height)
{
char *dimensions;
stp_dprintf(STP_DBG_PS, v,
"ps_media_size(%d, \'%s\', \'%s\', %08x, %08x)\n",
stp_printer_get_model(printer), stp_get_ppd_file(v),
stp_get_media_size(v),
width, height);
if ((dimensions = ppd_find(stp_get_ppd_file(v), "PaperDimension",
stp_get_media_size(v), NULL))
!= NULL)
sscanf(dimensions, "%d%d", width, height);
else
stp_default_media_size(printer, v, width, height);
}
static void
ps_imageable_area(const stp_printer_t printer,
const stp_vars_t v,
int *left,
int *right,
int *bottom,
int *top)
{
char *area;
float fleft,
fright,
fbottom,
ftop;
if ((area = ppd_find(stp_get_ppd_file(v), "ImageableArea",
stp_get_media_size(v), NULL))
!= NULL)
{
stp_dprintf(STP_DBG_PS, v, "area = \'%s\'\n", area);
if (sscanf(area, "%f%f%f%f", &fleft, &fbottom, &fright, &ftop) == 4)
{
*left = (int)fleft;
*right = (int)fright;
*bottom = (int)fbottom;
*top = (int)ftop;
}
else
*left = *right = *bottom = *top = 0;
}
else
{
stp_default_media_size(printer, v, right, top);
*left = 18;
*right -= 18;
*top -= 36;
*bottom = 36;
}
}
static void
ps_limit(const stp_printer_t printer,
const stp_vars_t v,
int *width,
int *height,
int *min_width,
int *min_height)
{
*width = INT_MAX;
*height = INT_MAX;
*min_width = 1;
*min_height = 1;
}
static void
ps_describe_resolution(const stp_printer_t printer,
const char *resolution, int *x, int *y)
{
*x = -1;
*y = -1;
sscanf(resolution, "%dx%d", x, y);
return;
}
static void
ps_print(const stp_printer_t printer,
stp_image_t *image,
const stp_vars_t v)
{
unsigned char *cmap = stp_get_cmap(v);
int model = stp_printer_get_model(printer);
const char *ppd_file = stp_get_ppd_file(v);
const char *resolution = stp_get_resolution(v);
const char *media_size = stp_get_media_size(v);
const char *media_type = stp_get_media_type(v);
const char *media_source = stp_get_media_source(v);
int output_type = stp_get_output_type(v);
int orientation = stp_get_orientation(v);
double scaling = stp_get_scaling(v);
int top = stp_get_top(v);
int left = stp_get_left(v);
int i, j;
int y;
unsigned char *in;
unsigned short *out;
int page_left,
page_right,
page_top,
page_bottom,
page_width,
page_height,
out_width,
out_height,
out_bpp,
out_ps_height,
out_offset;
time_t curtime;
stp_convert_t colorfunc;
int zero_mask;
char *command;
const char *temp;
int order,
num_commands;
struct
{
const char *keyword, *choice;
char *command;
int order;
} commands[4];
int image_height,
image_width,
image_bpp;
stp_vars_t nv = stp_allocate_copy(v);
if (!stp_get_verified(nv))
{
stp_eprintf(nv, "Print options not verified; cannot print.\n");
return;
}
image->init(image);
image_height = image->height(image);
image_width = image->width(image);
image_bpp = image->bpp(image);
colorfunc = stp_choose_colorfunc(output_type, image_bpp, cmap, &out_bpp, nv);
ps_imageable_area(printer, nv, &page_left, &page_right,
&page_bottom, &page_top);
stp_compute_page_parameters(page_right, page_left, page_top, page_bottom,
scaling, image_width, image_height, image,
&orientation, &page_width, &page_height,
&out_width, &out_height, &left, &top);
image_height = image->height(image);
image_width = image->width(image);
image->progress_init(image);
curtime = time(NULL);
if (left < 0)
left = (page_width - out_width) / 2 + page_left;
else
left += page_left;
if (top < 0)
top = (page_height + out_height) / 2 + page_bottom;
else
top = page_height - top + page_bottom;
stp_dprintf(STP_DBG_PS, v,
"out_width = %d, out_height = %d\n", out_width, out_height);
stp_dprintf(STP_DBG_PS, v,
"page_left = %d, page_right = %d, page_bottom = %d, page_top = %d\n",
page_left, page_right, page_bottom, page_top);
stp_dprintf(STP_DBG_PS, v, "left = %d, top = %d\n", left, top);
stp_puts("%!PS-Adobe-3.0\n", v);
#ifdef HAVE_CONFIG_H
stp_zprintf(v, "%%%%Creator: %s/Gimp-Print %s (%s)\n",
image->get_appname(image), VERSION, RELEASE_DATE);
#else
stp_zprintf(v, "%%%%Creator: %s/Gimp-Print\n", image->get_appname(image));
#endif
stp_zprintf(v, "%%%%CreationDate: %s", ctime(&curtime));
stp_puts("%Copyright: 1997-2002 by Michael Sweet (mike@easysw.com) and Robert Krawitz (rlk@alum.mit.edu)\n", v);
stp_zprintf(v, "%%%%BoundingBox: %d %d %d %d\n",
left, top - out_height, left + out_width, top);
stp_puts("%%DocumentData: Clean7Bit\n", v);
stp_zprintf(v, "%%%%LanguageLevel: %d\n", model + 1);
stp_puts("%%Pages: 1\n", v);
stp_puts("%%Orientation: Portrait\n", v);
stp_puts("%%EndComments\n", v);
num_commands = 0;
if ((command = ppd_find(ppd_file, "PageSize", media_size, &order)) != NULL)
{
commands[num_commands].keyword = "PageSize";
commands[num_commands].choice = media_size;
commands[num_commands].command = stp_malloc(strlen(command) + 1);
strcpy(commands[num_commands].command, command);
commands[num_commands].order = order;
num_commands ++;
}
if ((command = ppd_find(ppd_file, "InputSlot", media_source, &order)) != NULL)
{
commands[num_commands].keyword = "InputSlot";
commands[num_commands].choice = media_source;
commands[num_commands].command = stp_malloc(strlen(command) + 1);
strcpy(commands[num_commands].command, command);
commands[num_commands].order = order;
num_commands ++;
}
if ((command = ppd_find(ppd_file, "MediaType", media_type, &order)) != NULL)
{
commands[num_commands].keyword = "MediaType";
commands[num_commands].choice = media_type;
commands[num_commands].command = stp_malloc(strlen(command) + 1);
strcpy(commands[num_commands].command, command);
commands[num_commands].order = order;
num_commands ++;
}
if ((command = ppd_find(ppd_file, "Resolution", resolution, &order)) != NULL)
{
commands[num_commands].keyword = "Resolution";
commands[num_commands].choice = resolution;
commands[num_commands].command = stp_malloc(strlen(command) + 1);
strcpy(commands[num_commands].command, command);
commands[num_commands].order = order;
num_commands ++;
}
for (i = 0; i < (num_commands - 1); i ++)
for (j = i + 1; j < num_commands; j ++)
if (commands[j].order < commands[i].order)
{
temp = commands[i].keyword;
commands[i].keyword = commands[j].keyword;
commands[j].keyword = temp;
temp = commands[i].choice;
commands[i].choice = commands[j].choice;
commands[j].choice = temp;
order = commands[i].order;
commands[i].order = commands[j].order;
commands[j].order = order;
command = commands[i].command;
commands[i].command = commands[j].command;
commands[j].command = command;
}
if (num_commands > 0)
{
stp_puts("%%BeginSetup\n", v);
for (i = 0; i < num_commands; i ++)
{
stp_puts("[{\n", v);
stp_zprintf(v, "%%%%BeginFeature: *%s %s\n", commands[i].keyword,
commands[i].choice);
if (commands[i].command[0])
{
stp_puts(commands[i].command, v);
if (commands[i].command[strlen(commands[i].command) - 1] != '\n')
stp_puts("\n", v);
}
stp_puts("%%EndFeature\n", v);
stp_puts("} stopped cleartomark\n", v);
stp_free(commands[i].command);
}
stp_puts("%%EndSetup\n", v);
}
stp_puts("%%Page: 1 1\n", v);
stp_puts("gsave\n", v);
stp_zprintf(v, "%d %d translate\n", left, top);
stp_zprintf(v, "%.3f %.3f scale\n",
(double)out_width / ((double)image_width),
(double)out_height / ((double)image_height));
in = stp_zalloc(image_width * image_bpp);
out = stp_zalloc((image_width * out_bpp + 3) * 2);
stp_compute_lut(nv, 256);
if (model == 0)
{
stp_zprintf(v, "/picture %d string def\n", image_width * out_bpp);
stp_zprintf(v, "%d %d 8\n", image_width, image_height);
stp_puts("[ 1 0 0 -1 0 1 ]\n", v);
if (output_type == OUTPUT_GRAY || output_type == OUTPUT_MONOCHROME)
stp_puts("{currentfile picture readhexstring pop} image\n", v);
else
stp_puts("{currentfile picture readhexstring pop} false 3 colorimage\n", v);
for (y = 0; y < image_height; y ++)
{
if ((y & 15) == 0)
image->note_progress(image, y, image_height);
if (image->get_row(image, in, y) != STP_IMAGE_OK)
break;
(*colorfunc)(nv, in, out, &zero_mask, image_width, image_bpp, cmap,
NULL, NULL, NULL);
ps_hex(v, out, image_width * out_bpp);
}
}
else
{
if (output_type == OUTPUT_GRAY || output_type == OUTPUT_MONOCHROME)
stp_puts("/DeviceGray setcolorspace\n", v);
else
stp_puts("/DeviceRGB setcolorspace\n", v);
stp_puts("<<\n", v);
stp_puts("\t/ImageType 1\n", v);
stp_zprintf(v, "\t/Width %d\n", image_width);
stp_zprintf(v, "\t/Height %d\n", image_height);
stp_puts("\t/BitsPerComponent 8\n", v);
if (output_type == OUTPUT_GRAY || output_type == OUTPUT_MONOCHROME)
stp_puts("\t/Decode [ 0 1 ]\n", v);
else
stp_puts("\t/Decode [ 0 1 0 1 0 1 ]\n", v);
stp_puts("\t/DataSource currentfile /ASCII85Decode filter\n", v);
if ((image_width * 72 / out_width) < 100)
stp_puts("\t/Interpolate true\n", v);
stp_puts("\t/ImageMatrix [ 1 0 0 -1 0 1 ]\n", v);
stp_puts(">>\n", v);
stp_puts("image\n", v);
for (y = 0, out_offset = 0; y < image_height; y ++)
{
if ((y & 15) == 0)
image->note_progress(image, y, image_height);
if (image->get_row(image, in, y) != STP_IMAGE_OK)
break;
(*colorfunc)(nv, in, out + out_offset, &zero_mask, image_width,
image_bpp, cmap, NULL, NULL, NULL);
out_ps_height = out_offset + image_width * out_bpp;
if (y < (image_height - 1))
{
ps_ascii85(v, out, out_ps_height & ~3, 0);
out_offset = out_ps_height & 3;
}
else
{
ps_ascii85(v, out, out_ps_height, 1);
out_offset = 0;
}
if (out_offset > 0)
memcpy(out, out + out_ps_height - out_offset, out_offset);
}
}
image->progress_conclude(image);
stp_free_lut(nv);
stp_free(in);
stp_free(out);
stp_puts("grestore\n", v);
stp_puts("showpage\n", v);
stp_puts("%%Trailer\n", v);
stp_puts("%%EOF\n", v);
stp_free_vars(nv);
}
static void
ps_hex(const stp_vars_t v,
unsigned short *data,
int length)
{
int col;
static const char *hex = "0123456789ABCDEF";
col = 0;
while (length > 0)
{
unsigned char pixel = (*data & 0xff00) >> 8;
stp_putc(hex[pixel >> 4], v);
stp_putc(hex[pixel & 15], v);
data ++;
length --;
col += 2;
if (col >= 72)
{
col = 0;
stp_putc('\n', v);
}
}
if (col > 0)
stp_putc('\n', v);
}
static void
ps_ascii85(const stp_vars_t v,
unsigned short *data,
int length,
int last_line)
{
int i;
unsigned b;
unsigned char c[5];
static int column = 0;
while (length > 3)
{
unsigned char d0 = (data[0] & 0xff00) >> 8;
unsigned char d1 = (data[1] & 0xff00) >> 8;
unsigned char d2 = (data[2] & 0xff00) >> 8;
unsigned char d3 = (data[3] & 0xff00) >> 8;
b = (((((d0 << 8) | d1) << 8) | d2) << 8) | d3;
if (b == 0)
{
stp_putc('z', v);
column ++;
}
else
{
c[4] = (b % 85) + '!';
b /= 85;
c[3] = (b % 85) + '!';
b /= 85;
c[2] = (b % 85) + '!';
b /= 85;
c[1] = (b % 85) + '!';
b /= 85;
c[0] = b + '!';
stp_zfwrite((const char *)c, 5, 1, v);
column += 5;
}
if (column > 72)
{
stp_putc('\n', v);
column = 0;
}
data += 4;
length -= 4;
}
if (last_line)
{
if (length > 0)
{
for (b = 0, i = length; i > 0; b = (b << 8) | data[0], data ++, i --);
c[4] = (b % 85) + '!';
b /= 85;
c[3] = (b % 85) + '!';
b /= 85;
c[2] = (b % 85) + '!';
b /= 85;
c[1] = (b % 85) + '!';
b /= 85;
c[0] = b + '!';
stp_zfwrite((const char *)c, length + 1, 1, v);
}
stp_puts("~>\n", v);
column = 0;
}
}
static char *
ppd_find(const char *ppd_file,
const char *name,
const char *option,
int *order)
{
char line[1024],
lname[255],
loption[255],
*opt;
static char *value = NULL;
if (ppd_file == NULL || name == NULL || option == NULL)
return (NULL);
if (!value)
value = stp_zalloc(32768);
if (ps_ppd_file == NULL || strcmp(ps_ppd_file, ppd_file) != 0)
{
if (ps_ppd != NULL)
fclose(ps_ppd);
ps_ppd = fopen(ppd_file, "r");
if (ps_ppd == NULL)
ps_ppd_file = NULL;
else
ps_ppd_file = ppd_file;
}
if (ps_ppd == NULL)
return (NULL);
if (order != NULL)
*order = 1000;
rewind(ps_ppd);
while (fgets(line, sizeof(line), ps_ppd) != NULL)
{
if (line[0] != '*')
continue;
if (strncasecmp(line, "*OrderDependency:", 17) == 0 && order != NULL)
{
sscanf(line, "%*s%d", order);
continue;
}
else if (sscanf(line, "*%s %[^/:]", lname, loption) != 2)
continue;
if (strcasecmp(lname, name) == 0 &&
strcasecmp(loption, option) == 0)
{
opt = strchr(line, ':') + 1;
while (*opt == ' ' || *opt == '\t')
opt ++;
if (*opt != '\"')
continue;
strcpy(value, opt + 1);
if ((opt = strchr(value, '\"')) == NULL)
{
while (fgets(line, sizeof(line), ps_ppd) != NULL)
{
strcat(value, line);
if (strchr(line, '\"') != NULL)
{
strcpy(strchr(value, '\"'), "\n");
break;
}
}
}
else
*opt = '\0';
return (value);
}
}
return (NULL);
}
const stp_printfuncs_t stp_ps_printfuncs =
{
ps_parameters,
ps_media_size,
ps_imageable_area,
ps_limit,
ps_print,
ps_default_parameters,
ps_describe_resolution,
stp_verify_printer_params,
stp_start_job,
stp_end_job
};