1 /* Copyright (C) 2001-2006 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied, modified
8 or distributed except as expressly authorized under the terms of that
9 license. Refer to licensing information at http://www.artifex.com/
10 or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11 San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
12 */
13 /* $Id: gdevimgn.c 8250 2007-09-25 13:31:24Z giles $*/
14 /*
15 * Imagen ImPRESS printer driver - version 1.4
16 *
17 * This driver uses the Impress bitmap operation to print the page image.
18 */
19
20 /* Written by Alan Millar (AMillar@bolis.sf-bay.org) August 4 1992.
21 Basic bitmap dump. */
22 /* Updated by Alan Millar Sept 21 1992. Added resolution handling
23 for 75, 150, and 300 dpi. */
24 /* Updated by Alan Millar June 05 1993. General cleanup for
25 beta test release. */
26 /* Updated by Alan Millar June 21 1993. v1.3. Combined multipage
27 output into single imPress document. Quote fewer special
28 chars in byte stream mode. imPress document header options
29 can be set from environment variable IMPRESSHEADER */
30 /* Updated by Alan Millar July 04 1993. v1.4.
31 New makefile option USE_BYTE_STREAM instead of changing source.
32 Swatch output redone to eliminate ALL blank swatches (swatchMap).
33 Buffer copying changed to multi-byte operations (BIGTYPE).
34 Page margins and A4 paper settings fixed, at least for Canon CX.
35 */
36
37 /* -------------------------------------------------------- */
38 /* Instructions:
39
40 - Add "imagen.dev" to DEVICE_DEVS in the makefile. For example:
41 DEVICE_DEVS2=laserjet.dev imagen.dev
42
43 - Include or exclude USE_BYTE_STREAM in makefile as appropriate
44 If you are compiling on Unix, re-run "tar_cat" to update the makefile
45 from devs.mak
46
47 - At run time, specify the resolution on the GS command line
48 by using -r300 or -r150 or -r75
49 - At run time, specify any imPress document options in
50 the IMPRESSHEADER environment variable.
51 */
52
53 /* -------------------------------------------------------- */
54 /* Hardware/software combinations tested:
55 - ImageStation IP3 8/300 with parallel byte-stream interface,
56 using GS 2.6.1 on Linux with GCC 2.3.3;
57 earlier using GS 2.5.1 on MS-Dos with Turbo C++ 1.0
58 - Sequenced-packet-protocol interface untested.
59 */
60 /* -------------------------------------------------------- */
61 /* Bugs/Enhancements:
62 - Driver does not use any Impress language features for
63 drawing lines/arcs
64 - Driver does not use resident or downloadable fonts.
65 - Buffer output instead of system call for each byte?
66 */
67
68 /* -------------------------------------------------------- */
69 #include "gdevprn.h"
70 /* #include <stdio.h> should not be used in drivers */
71 #include <stdlib.h>
72
73 /* -------------------------------------------------------- */
74 /* Working Constants */
75
76 /* Byte stream quoting: convert special characters to hex.
77 Specify by including/excluding -DUSE_BYTE_STREAM in makefile.
78 This should match printer's hardware interface configuration.
79 If printer interface is serial with sequenced-packet-protocol
80 spooler software (ImageStation config# 11 = 01), then don't use it.
81 Imagen "ipr" spooler software should not use byte stream.
82 If printer interface is Centronics parallel byte stream,
83 (ImageStation config# 11 = 03), then use byte stream. */
84
85 #ifdef USE_BYTE_STREAM
86 # define BYTE_STREAM 1
87 #else
88 # define BYTE_STREAM 0
89 #endif
90
91 /* Byte stream quote character (ImageStation config# 15).
92 Only needed when using byte stream */
93 #define QUOTE_CHAR (char) 0x02
94 /* Byte stream end-of-file character (ImageStation config# 14). */
95 #define EOF_CHAR (char) 0x04
96 /* Other special characters to quote. Put them here if spooler or
97 hardware uses flow control, etc. If not needed, set to
98 a redundant value such as EOF_CHAR */
99 #define EXTRA_QUOTE1 (char) 0x11 /* ^Q */
100 #define EXTRA_QUOTE2 (char) 0x13 /* ^S */
101 #define EXTRA_QUOTE3 EOF_CHAR
102 #define EXTRA_QUOTE4 EOF_CHAR
103
104 /* -------------------------------------------------------- */
105 /* imPress header default options.
106 Can be overridden at run-time with IMPRESSHEADER env variable */
107
108 #define IMPRESSHEADER "jobheader onerror, prerasterization off"
109
110 /* -------------------------------------------------------- */
111
112 #define CANON_CX
113
114 /* Printer engine max resolution. 300 for Canon CX models such as
115 ImageStation IP3. Others (240?) unverified */
116 #ifdef CANON_CX
117 # define MAX_DPI 300
118 #endif
119 #ifndef MAX_DPI
120 # define MAX_DPI 300
121 #endif
122
123 /* Determine imPress scaling factor from GS resolution.
124 Magnify can be 0, 1, or 2.
125 0 = MAX_DPI, 1 = MAX_DPI / 2, 2 = MAX_DPI / 4
126 Assuming MAX_DPI is 300, you can specify -r75 or -r150
127 or -r300 on the GS command line */
128 #define getMagnification ( \
129 ( pdev->x_pixels_per_inch > (MAX_DPI >> 1) ) ? 0 : \
130 ( pdev->x_pixels_per_inch > (MAX_DPI >> 2) ) ? 1 : \
131 2 )
132
133 /* Page dimensions from gdevprn.h - specify -DA4 in makefile for A4 paper */
134 #define WIDTH_10THS DEFAULT_WIDTH_10THS
135 #define HEIGHT_10THS DEFAULT_HEIGHT_10THS
136
137 /* Width in inches of unprintable edge of paper. May need fine tuning.
138 Canon CX engine in ImageStation IP3 8/300 will only print 8 inches
139 wide on any paper size. May vary for other engines */
140
141 #ifdef CANON_CX
142 # define MARG_L 0.15
143 # define MARG_R ( (float)WIDTH_10THS / 10.0 - 8.0 - MARG_L)
144 #endif
145 #ifndef MARG_L
146 # define MARG_L 0.2
147 #endif
148 #ifndef MARG_R
149 # define MARG_R 0.2
150 #endif
151 #define MARG_T 0.1
152 #define MARG_B 0.2
153
154
155 /* Flag for displaying debug messages at run-time. Higher
156 number = higher detail */
157 #define IM_DEBUG 0
158 #define DebugMsg(Level,P1,P2) if (Level<=IM_DEBUG) {errprintf(P1,P2 );}
159
160 /*-------------------------------------------*/
161 /* Impress bitmaps are made up of 32x32 bit swatches.
162 A swatch is four bytes (32 bits) wide by 32 bytes high,
163 totalling 128 bytes. */
164 #define HorzBytesPerSw 4
165 #define HorzBitsPerSw (HorzBytesPerSw * 8)
166 #define VertBytesPerSw 32
167 #define TotalBytesPerSw (HorzBytesPerSw * VertBytesPerSw)
168
169 /*-------------------------------------------*/
170 /* Attempt at optimization to something faster than byte-by-byte copying.
171 imPress swatches are 4 bytes wide, so type must align on a 4-byte
172 boundary. Swatch interleaving restricts the copy to 4 bytes in a row.
173 Type must be numeric where value is zero when all bytes in it are zero. */
174 #if arch_sizeof_long == 4
175 # define BIGTYPE unsigned long int
176 #else
177 # if arch_sizeof_short == 4
178 # define BIGTYPE unsigned short int
179 # else
180 # if arch_sizeof_short == 2
181 # define BIGTYPE unsigned short
182 # endif
183 # endif
184 #endif
185 #ifndef BIGTYPE
186 #define BIGTYPE byte
187 #endif
188
189 #define BIGSIZE ( sizeof( BIGTYPE ) )
190
191 /*-------------------------------------------*/
192 /* IMAGEN imPress Command opcodes */
193 /* from DVIIMP.C */
194 #define iSP 128 /* advance one space */
195 #define iSP1 129 /* advance one space + 1 pixel */
196 #define iMPLUS 131 /* Move one pixel forward */
197 #define iMMINUS 132 /* Move one pixel back */
198 #define iMMOVE 133 /* Move in main advance direction */
199 #define iSMOVE 134 /* Move in secondary advance direction */
200
201 #define iABS_H 135 /* Move to H position */
202 #define iREL_H 136 /* Move in H direction */
203 #define iABS_V 137 /* Move to V position */
204 #define iREL_V 138 /* Move in V direction */
205
206 #define iCRLF 197 /* move to beginning of next line */
207
208 #define iSET_HV_SYSTEM 205 /* Define new coordinate system */
209 #define iSET_ADV_DIRS 206 /* Define advance directions */
210
211 #define iPAGE 213 /* Set H and V to 0 */
212 #define iENDPAGE 219 /* print the current page */
213
214 #define iBITMAP 235 /* Print a full bitmap */
215 #define iSET_MAGNIFICATION 236
216 /* magnify the page by 1, 2, 4 */
217 #define iNOOP 254 /* no operation */
218 #define iEOF 255 /* end of impress document */
219
220 /*-------------------------------------------*/
221 /*-------------------------------------------*/
222 /* The device descriptor */
223
224 static dev_proc_print_page(imagen_print_page);
225 static dev_proc_open_device(imagen_prn_open);
226 static dev_proc_close_device(imagen_prn_close);
227
228 gx_device_procs imagen_procs =
229 prn_procs(imagen_prn_open, gdev_prn_output_page, imagen_prn_close);
230
231 #define ppdev ((gx_device_printer *)pdev)
232
233 /*-------------------------------------------*/
234 const gx_device_printer far_data gs_imagen_device =
235 prn_device(/*prn_std_procs*/ imagen_procs,
236 "imagen",
237 WIDTH_10THS,
238 HEIGHT_10THS,
239 MAX_DPI, /* x_dpi */
240 MAX_DPI, /* y_dpi */
241 MARG_L,MARG_R,MARG_T,MARG_B, /* margins */
242 1, imagen_print_page);
243
244 /*-------------------------------------------*/
245
246 /*-------------------------------------------*/
247 static void
iWrite(FILE * Out,byte Val)248 iWrite(FILE *Out, byte Val)
249 { /* iWrite */
250 char *hexList = "0123456789ABCDEF";
251
252 /* if we are doing byte-stream, quote characters that would otherwise
253 match EOF and QUOTE itself, or other special chars */
254 /* Imagen quoting takes one character and writes out the QUOTE
255 character followed by the hex digits of the quoted character */
256 if (BYTE_STREAM &&
257 ( Val == QUOTE_CHAR || Val == EOF_CHAR
258 || Val == EXTRA_QUOTE1 || Val == EXTRA_QUOTE2
259 || Val == EXTRA_QUOTE3 || Val == EXTRA_QUOTE4 ) ) {
260 fputc (QUOTE_CHAR, Out);
261 fputc ((char) hexList[Val / 0x10], Out);
262 fputc ((char) hexList[Val % 0x10], Out);
263 } else { /* quoted char */
264 /* Not doing quoting, just send it out */
265 fputc(Val, Out);
266 } /* quoted char */
267 } /* iWrite */
268
269 /* Write out 16bit, high byte first */
270 void
iWrite2(FILE * Out,int Val)271 iWrite2(FILE *Out, int Val)
272 { /* iWrite2 */
273 iWrite(Out,(byte) (Val >> 8) & 0x00FF );
274 iWrite(Out,(byte) Val & 0x00FF );
275 } /* iWrite2 */
276
277 /* --------------------------------------------------------- */
278
279 static int
imagen_prn_open(gx_device * pdev)280 imagen_prn_open(gx_device *pdev)
281 { /* imagen_prn_open */
282 int code;
283
284 char *impHeader;
285
286 /* ----------------------------------------- */
287 DebugMsg(1,"%s\n","Start of imagen_prn_open");
288 DebugMsg(2,"BIGSIZE = %ld \n",BIGSIZE);
289
290 code = gdev_prn_open(pdev);
291 if ( code < 0 ) return code;
292
293 /* ----------------------------------------- */
294
295 DebugMsg(2,"opening file: %s\n",ppdev->fname);
296 code = gdev_prn_open_printer(pdev, 1);
297 if ( code < 0 ) return code;
298
299 impHeader = getenv("IMPRESSHEADER");
300 if (impHeader == NULL ) {
301 impHeader = IMPRESSHEADER ;
302 } /* if impHeader */
303
304 fprintf(ppdev->file,"@document(language impress, %s)",impHeader);
305
306 code = gdev_prn_close_printer(pdev);
307 if ( code < 0 ) return code;
308
309 /* ----------------------------------------- */
310 DebugMsg(1,"%s\n","End of imagen_prn_open");
311
312 return code;
313 } /* imagen_prn_open */
314
315 static int
imagen_prn_close(gx_device * pdev)316 imagen_prn_close(gx_device *pdev)
317 { /* imagen_prn_close */
318 int code;
319
320 /* ----------------------------------------- */
321 DebugMsg(1,"%s\n","Start of imagen_prn_close");
322
323 code = gdev_prn_open_printer(pdev, 1);
324 if ( code < 0 ) return code;
325
326 /* Write imPress end of document marker */
327 iWrite(ppdev->file,iEOF);
328
329 /* And byte stream end of file */
330 if (BYTE_STREAM) {
331 /* DON'T use iWrite because actual EOF should not be quoted! */
332 fputc(EOF_CHAR,ppdev->file);
333 } /* if byte stream */
334
335 fflush(ppdev->file);
336
337 code = gdev_prn_close_printer(pdev);
338 if ( code < 0 ) return code;
339
340 code = gdev_prn_close(pdev);
341
342 DebugMsg(1,"%s\n","End of imagen_prn_close");
343
344 return(code);
345 } /* imagen_prn_close */
346
347 /*-------------------------------------------*/
348 /* Send the page to the printer. */
349 static int
imagen_print_page(gx_device_printer * pdev,FILE * prn_stream)350 imagen_print_page(gx_device_printer *pdev, FILE *prn_stream)
351 {
352 int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
353 /* input buffer: one line of bytes rasterized by gs */
354 byte *in = (byte *)gs_malloc(pdev->memory, BIGSIZE, line_size / BIGSIZE + 1,
355 "imagen_print_page(in)");
356 /* output buffer: 32 lines, interleaved into imPress swatches */
357 byte *out;
358 /* working pointer into output buffer */
359 byte *swatch;
360 byte *temp;
361 /* map of which swatches in a row are completely blank, or are non-blank */
362 byte *swatchMap;
363 /* starting line number on page of a row of swatches */
364 int lnum ;
365 /* line number within a row of swatches */
366 int swatchLine;
367 /* ending line number of row of swatches */
368 int lastLine;
369 /* how many swatches can fit on a row */
370 int swatchCount;
371 /* index into row of non-blank swatch */
372 int startSwatch;
373 int endSwatch;
374 /* Scaling factor for resolution */
375 int Magnify;
376 /* page totals */
377 int totalBlankSwatches;
378 int totalGreySwatches;
379
380 /* ----------------------------------------- */
381 /* Start of routine */
382 /* ----------------------------------------- */
383
384 DebugMsg(1,"%s\n","Start of imagen_print_page");
385
386 /* ----------------------------------------- */
387 Magnify = getMagnification ;
388
389 /* Impress bitmaps are made up of 32x32 bit swatches.
390 A swatch is four bytes wide by 32 bytes high.
391 See how many swatches will fit horizontally. */
392
393 swatchCount = (line_size + HorzBytesPerSw - 1) / HorzBytesPerSw;
394
395 totalBlankSwatches = 0 ;
396 totalGreySwatches = 0 ;
397 DebugMsg(2,"Swatch count = %d\n",swatchCount);
398 DebugMsg(2,"Line size = %d\n",line_size );
399
400 out = (byte *)gs_malloc(pdev->memory, TotalBytesPerSw , swatchCount + 1,
401 "imagen_print_page(out)");
402
403 swatchMap = (byte *)gs_malloc(pdev->memory, BIGSIZE,swatchCount / BIGSIZE + 1,
404 "imagen_print_page(swatchMap)" );
405
406 if ( in == 0 || out == 0 )
407 return -1;
408
409 /* Initialize the page */
410 iWrite(prn_stream,iPAGE);
411
412 /* Tell ImPress what resolution we will be using */
413 iWrite(prn_stream,iSET_MAGNIFICATION);
414 iWrite(prn_stream,Magnify);
415
416 /*------------------------------------------------------*/
417 /* main loop down page */
418 lnum = 0;
419 while (lnum <= pdev->height) {
420
421 /* erase swatch map. */
422 for (swatch = swatchMap; swatch < swatchMap + swatchCount ;
423 swatch += BIGSIZE ) {
424 * (BIGTYPE *)swatch = (BIGTYPE) 0;
425 } /* for */
426
427 /* get scan lines to fill swatches */
428 swatchLine = 0;
429 lastLine = VertBytesPerSw - 1;
430
431 /* Check if we don't have a full-height row of swatches at end of page */
432 if (lnum + lastLine > pdev->height ) {
433 /* back up last row so it overlaps with previous. Not a problem
434 on a laser printer, because the overlapping part will be identical */
435 lnum = pdev->height - lastLine ;
436 }; /* not full height */
437
438 DebugMsg (3,"lnum = %d \n",lnum);
439
440 /* ------------------------------------------------------- */
441 /* get 32 lines and interleave into a row of swatches */
442 for (swatchLine = 0 ; swatchLine <= lastLine; swatchLine++) {
443 /* blank out end of buffer for BIGSIZE overlap */
444 for (temp = in + line_size; temp < in + line_size + BIGSIZE;temp++){
445 *temp = 0;
446 } /* for temp */
447
448 /* get one line */
449 gdev_prn_copy_scan_lines(pdev, lnum + swatchLine, in, line_size);
450 DebugMsg(5,"Got scan line %d ", lnum + swatchLine);
451 DebugMsg(5,"line %d \n", swatchLine);
452
453 /* interleave scan line into swatch buffer */
454 /* a swatch is a 4 byte * 32 byte square. Swatches are placed
455 next to each other. The first scan line maps into the first
456 four bytes of the first swatch, then the first four of the second
457 swatch, etc.
458 To get this on the page:
459 A1 A1 A1 A1 B1 B1 B1 B1 C1 C1 C1 C1
460 A2 A2 A2 A2 B2 B2 B2 B2 C2 C2 C2 C2
461 ...
462 A32 A32 A32 A32 B32 B32 B32 B32 C32 C32 C32 C32
463 You have to send it as:
464 A1 A1 A1 A1 A2 ... A32 B1 B1 .. B32 C1 C1 ... C32 */
465
466 /* set initial offset into swatch buffer based on which
467 line in the swatch we are processing */
468 swatch = out + swatchLine * HorzBytesPerSw;
469 DebugMsg(5,"offset: swatch = %d \n",(int) (swatch - out) );
470 temp = in;
471 while ( temp < in + line_size ) {
472 /* copy multi-byte to swatch buffer */
473 * (BIGTYPE *)swatch = * (BIGTYPE *)temp;
474 if ( * (BIGTYPE *)temp ) {
475 /* mark map if not blank */
476 swatchMap[(swatch - out)/TotalBytesPerSw] = (byte) 1 ;
477 } /* if not zero */
478
479 temp += (BIGSIZE > HorzBytesPerSw) ? HorzBytesPerSw : BIGSIZE ;
480 swatch += (BIGSIZE > HorzBytesPerSw) ? HorzBytesPerSw : BIGSIZE ;
481
482 /* if we copied four bytes, skip to next swatch */
483 if ( ((temp - in) % HorzBytesPerSw ) == 0 ) {
484 swatch += (TotalBytesPerSw - HorzBytesPerSw) ;
485 } /* if need to skip */
486 } /* while < line_size */
487
488 } /* for swatchLine */
489
490 /* ------------------------------------------------- */
491 /* we now have full swatches. */
492 /* Send to printer */
493
494 /* go through swatch map to find non-blank swatches.
495 Skip over completely blank swatches */
496 startSwatch = 0;
497 while (startSwatch < swatchCount ) {
498 if (swatchMap[startSwatch] == 0 ) {
499 /* skip blank swatch */
500 DebugMsg(6,"Skip blank %d \n",startSwatch);
501 totalBlankSwatches++;
502 startSwatch++;
503 } else { /* if swatch == 0 */
504 /* we hit a non-blank swatch. */
505 totalGreySwatches++;
506
507 /* See how many there are in a row */
508 endSwatch = startSwatch;
509 while ( (endSwatch < swatchCount) && swatchMap[endSwatch] ) {
510 endSwatch++;
511 totalGreySwatches++;
512 } /* while */
513 /* endSwatch is one past last non-blank swatch */
514 DebugMsg(6,"Grey swatches %d ",startSwatch);
515 DebugMsg(6,"until %d \n",endSwatch);
516
517 /* vertical position: scan line, shifted for magnification */
518 iWrite(prn_stream, iABS_V);
519 iWrite2(prn_stream, lnum << Magnify);
520
521 /* horizontal position = swatch number * 32 bits/swatch */
522 iWrite(prn_stream,iABS_H);
523 iWrite2(prn_stream, startSwatch * HorzBitsPerSw << Magnify );
524 iWrite(prn_stream,iBITMAP); /* start bitmap */
525 iWrite(prn_stream,0x07); /* bit OR with page */
526 iWrite(prn_stream,(endSwatch - startSwatch)); /* horizontal
527 number of swatches */
528 iWrite(prn_stream, 1) ; /* vertical number of swatches */
529 /* write out swatch buffer */
530 for (swatch = out + startSwatch * TotalBytesPerSw;
531 swatch < out + endSwatch * TotalBytesPerSw; swatch++) {
532 iWrite(prn_stream,*swatch);
533 } /* for swatch */
534
535 /* swatches have been printed, see if there are still
536 more in this row */
537 startSwatch = endSwatch;
538 } /* if swatch == 0 */
539
540 } /* while startSwatch */
541
542 /* Whole row of swatches is done. Go on to next row of swatches */
543 lnum += lastLine + 1;
544
545 } /* while lnum */
546
547 /* Eject the page */
548 iWrite(prn_stream,iENDPAGE);
549
550 fflush(prn_stream);
551
552 gs_free(pdev->memory, (char *)swatchMap, BIGSIZE, swatchCount / BIGSIZE + 1,
553 "imagen_print_page(swatchMap)" );
554 gs_free(pdev->memory, (char *)out, TotalBytesPerSw, swatchCount+1, "imagen_print_page(out)");
555 gs_free(pdev->memory, (char *)in, BIGSIZE, line_size / BIGSIZE + 1, "imagen_print_page(in)");
556 /* ----------------------------------------- */
557
558 DebugMsg(1,"Debug: Grey: %d \n",totalGreySwatches);
559 DebugMsg(1,"Debug: Blank: %d \n",totalBlankSwatches );
560 DebugMsg(1,"%s\n","End of imagen_print_page");
561
562 /* ----------------------------------------- */
563 return 0;
564
565 } /* imagen_print_page */
566