home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 5
/
FreshFish_July-August1994.bin
/
bbs
/
gnu
/
gs-2.6.1.4-src.lha
/
src
/
amiga
/
gs-2.6.1.4
/
gdevprn.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-01-27
|
12KB
|
383 lines
/* Copyright (C) 1990, 1992 Aladdin Enterprises. All rights reserved.
This file is part of Ghostscript.
Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the Ghostscript General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License. A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
copies. */
/* gdevprn.c */
/* Generic printer support for Ghostscript */
#include "gdevprn.h"
#include "gp.h"
#include "gsprops.h"
/* Define the scratch file name prefix for mktemp */
#define SCRATCH_TEMPLATE gp_scratch_file_name_prefix
/* Internal routine for opening a scratch file */
private int
open_scratch(char *fname, FILE **pfile)
{ char fmode[4];
strcpy(fmode, "w+");
strcat(fmode, gp_fmode_binary_suffix);
*pfile = gp_open_scratch_file(SCRATCH_TEMPLATE, fname, fmode);
if ( *pfile == NULL )
{ eprintf1("Could not open the scratch file %s.\n", fname);
return_error(gs_error_invalidfileaccess);
}
return 0;
}
/* ------ Standard device procedures ------ */
/* Macros for casting the pdev argument */
#define ppdev ((gx_device_printer *)pdev)
#define pmemdev ((gx_device_memory *)pdev)
#define pcldev ((gx_device_clist *)pdev)
gx_device_procs prn_std_procs =
prn_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close);
/* Generic initialization for the printer device. */
/* Specific devices may wish to extend this. */
int
gdev_prn_open(gx_device *pdev)
{ const gx_device_memory *mdev =
gdev_mem_device_for_bits(pdev->color_info.depth);
gx_device_procs *pprocs = pdev->procs;
ulong mem_space;
byte *base = 0;
char *left = 0;
if ( mdev == 0 )
return_error(gs_error_rangecheck);
memset(ppdev->skip, 0, sizeof(ppdev->skip));
ppdev->orig_procs = pprocs;
/*
* We should initialize the page count to 0, but since resetting
* some properties like page size closes and reopens the driver,
* we just have to rely on the linker initializing uninitialized
* fields to 0. When we make page_count a property of all devices
* (which is a big deal, because many drivers currently initialize
* their structures statically and therefore can't handle changes
* in the structure), this problem will go away. Meanwhile:
*/
/* ppdev->page_count = 0; */
ppdev->file = ppdev->ccfile = ppdev->cbfile = NULL;
mem_space = gdev_mem_bitmap_size(pmemdev);
if ( mem_space >= ppdev->max_bitmap ||
mem_space != (uint)mem_space || /* too big to allocate */
(base = (byte *)gs_malloc((uint)mem_space, 1, "printer buffer(open)")) == 0 || /* can't allocate */
(PRN_MIN_MEMORY_LEFT != 0 &&
(left = gs_malloc(PRN_MIN_MEMORY_LEFT, 1, "printer memory left")) == 0) /* not enough left */
)
{ /* Buffer the image in a command list. */
uint space;
int code;
/* Release the buffer if we allocated it. */
if ( base != 0 )
gs_free((char *)base, (uint)mem_space, 1,
"printer buffer(open)");
for ( space = ppdev->use_buffer_space; ; )
{ base = (byte *)gs_malloc(space, 1,
"command list buffer(open)");
if ( base != 0 ) break;
if ( (space >>= 1) < PRN_MIN_BUFFER_SPACE )
return_error(gs_error_VMerror); /* no hope */
}
open_c: pcldev->data = base;
pcldev->data_size = space;
pcldev->target = pdev;
pcldev->mdev = *mdev;
pcldev->mdev.target = pdev;
ppdev->buf = base;
ppdev->buffer_space = space;
/* Try opening the command list, to see if we allocated */
/* enough buffer space. */
code = (*gs_clist_device_procs.open_device)((gx_device *)pcldev);
if ( code < 0 )
{ /* If there wasn't enough room, and we haven't */
/* already shrunk the buffer, try enlarging it. */
if ( code == gs_error_limitcheck &&
space >= ppdev->use_buffer_space
)
{ gs_free((char *)base, space, 1,
"command list buffer(retry open)");
space <<= 1;
base = (byte *)gs_malloc(space, 1,
"command list buffer(retry open)");
ppdev->buf = base;
if ( base != 0 ) goto open_c;
}
/* Fall through with code < 0 */
}
if ( code < 0 ||
(code = open_scratch(ppdev->ccfname, &ppdev->ccfile)) < 0 ||
(code = open_scratch(ppdev->cbfname, &ppdev->cbfile)) < 0
)
{ /* Clean up before exiting */
gdev_prn_close(pdev);
return code;
}
pcldev->cfile = ppdev->ccfile;
pcldev->bfile = ppdev->cbfile;
pcldev->bfile_end_pos = 0;
ppdev->mod_procs = gs_clist_device_procs;
}
else
{ /* Render entirely in memory. */
/* Release the leftover memory. */
gs_free(left, PRN_MIN_MEMORY_LEFT, 1,
"printer memory left");
ppdev->buffer_space = 0;
pmemdev->base = base;
ppdev->mod_procs = *mdev->procs;
}
/* Synthesize the procedure vector. */
/* Rendering operations come from the memory or clist device, */
/* non-rendering come from the printer device. */
pdev->procs = &ppdev->mod_procs;
#define copy_proc(p) ppdev->mod_procs.p = pprocs->p
copy_proc(get_initial_matrix);
copy_proc(output_page);
copy_proc(close_device);
copy_proc(map_rgb_color);
copy_proc(map_color_rgb);
copy_proc(get_props);
copy_proc(put_props);
copy_proc(map_cmyk_color);
copy_proc(get_xfont_procs);
copy_proc(get_xfont_device);
#undef copy_proc
return (*pdev->procs->open_device)(pdev);
}
/* Added properties for printers */
private const gs_prop_item props_prn[] = {
/* Read-write properties. */
prop_def("BufferSpace", prt_int),
prop_def("MaxBitmap", prt_int),
prop_def("OutputFile", prt_string),
/* Read-only properties. */
prop_def("PageCount", prt_int)
};
/* Get properties. In addition to the standard properties, */
/* we supply the max bitmap size, buffer size, and output name. */
int
gdev_prn_get_props(gx_device *pdev, gs_prop_item *plist)
{ int start = gx_default_get_props(pdev, plist);
if ( plist != 0 )
{ register gs_prop_item *pi = plist + start;
memcpy(pi, props_prn, sizeof(props_prn));
pi[0].value.i = ppdev->use_buffer_space;
pi[1].value.i = ppdev->max_bitmap;
pi[2].value.a.p.s = ppdev->fname;
pi[2].value.a.size = -1;
pi[3].value.i = ppdev->page_count;
}
return start + sizeof(props_prn) / sizeof(gs_prop_item);
}
/* Put properties. */
/* Note that setting the buffer sizes closes the device. */
int
gdev_prn_put_props(gx_device *pdev, gs_prop_item *plist, int count)
{ gs_prop_item *known[3];
int code = 0;
props_extract(plist, count, props_prn, 3, known, 0);
code = gx_default_put_props(pdev, plist, count);
if ( code < 0 ) return code;
if ( known[0] != 0 )
{ gs_prop_item *pi = known[0];
if ( pi->value.i < 10000 )
pi->status = pv_rangecheck,
code = gs_error_rangecheck;
else
{ ppdev->use_buffer_space = known[0]->value.i;
if ( code == 0 ) code = 1;
}
}
if ( known[1] != 0 )
ppdev->max_bitmap = known[1]->value.i;
if ( known[2] != 0 )
{ gs_prop_item *pn = known[2];
int size = pn->value.a.size;
if ( size >= prn_fname_sizeof )
pn->status = pv_limitcheck,
code = gs_error_limitcheck;
else
{ /* Close the file if it's open. */
if ( ppdev->file != NULL && ppdev->file != stdout )
gp_close_printer(ppdev->file, ppdev->fname);
ppdev->file = NULL;
memcpy(ppdev->fname, pn->value.a.p.s, size);
ppdev->fname[size] = 0;
if ( code == 0 ) code = 1;
}
}
if ( code < 0 )
return_error(code);
/* If we're changing the buffer sizes, close the device; */
/* gs_putdeviceprops will reopen it. */
if ( pdev->is_open && code )
{ int ccode = gs_closedevice(pdev);
if ( ccode < 0 ) return ccode;
}
return code;
}
/* Generic routine to send the page to the printer. */
/****** Note that num_copies is currently ignored: this is wrong. ******/
int
gdev_prn_output_page(gx_device *pdev, int num_copies, int flush)
{ int code, outcode, closeco