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