xref: /plan9/sys/src/cmd/gs/src/gdevdljm.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 2000 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: gdevdljm.c,v 1.11 2004/01/29 18:19:41 ray Exp $ */
18 /* Generic monochrome H-P DeskJet/LaserJet driver */
19 #include "gdevprn.h"
20 #include "gdevdljm.h"
21 
22 /*
23  * Thanks for various improvements to:
24  *      Jim Mayer (mayer@wrc.xerox.com)
25  *      Jan-Mark Wams (jms@cs.vu.nl)
26  *      Frans van Hoesel (hoesel@chem.rug.nl)
27  *      George Cameron (g.cameron@biomed.abdn.ac.uk)
28  *      Nick Duffek (nsd@bbc.com)
29  * Thanks for the FS-600 driver to:
30  *	Peter Schildmann (peter.schildmann@etechnik.uni-rostock.de)
31  * Thanks for the LJIIID duplex capability to:
32  *      PDP (Philip) Brown (phil@3soft-uk.com)
33  * Thanks for the OCE 9050 driver to:
34  *      William Bader (wbader@EECS.Lehigh.Edu)
35  * Thanks for the LJ4D duplex capability to:
36  *	Les Johnson <les@infolabs.com>
37  */
38 
39 /* See gdevdljm.h for the definitions of the PCL_ features. */
40 
41 /* The number of blank lines that make it worthwhile to reposition */
42 /* the cursor. */
43 #define MIN_SKIP_LINES 7
44 
45 /* We round up the LINE_SIZE to a multiple of a ulong for faster scanning. */
46 #define W sizeof(word)
47 
48 /* Send a page to the printer. */
49 int
dljet_mono_print_page(gx_device_printer * pdev,FILE * prn_stream,int dots_per_inch,int features,const char * page_init)50 dljet_mono_print_page(gx_device_printer * pdev, FILE * prn_stream,
51 		      int dots_per_inch, int features, const char *page_init)
52 {
53     return dljet_mono_print_page_copies(pdev, prn_stream, 1, dots_per_inch,
54 					features, page_init);
55 }
56 int
dljet_mono_print_page_copies(gx_device_printer * pdev,FILE * prn_stream,int num_copies,int dots_per_inch,int features,const char * page_init)57 dljet_mono_print_page_copies(gx_device_printer * pdev, FILE * prn_stream,
58 			     int num_copies, int dots_per_inch, int features,
59 			     const char *page_init)
60 {
61     int line_size = gdev_mem_bytes_per_scan_line((gx_device *) pdev);
62     int line_size_words = (line_size + W - 1) / W;
63     uint storage_size_words = line_size_words * 8;	/* data, out_row, out_row_alt, prev_row */
64     word *storage;
65     word
66 	*data_words,
67 	*out_row_words,
68 	*out_row_alt_words,
69 	*prev_row_words;
70 #define data ((byte *)data_words)
71 #define out_row ((byte *)out_row_words)
72 #define out_row_alt ((byte *)out_row_alt_words)
73 #define prev_row ((byte *)prev_row_words)
74     byte *out_data;
75     int x_dpi = (int)pdev->x_pixels_per_inch;
76     int y_dpi = (int)pdev->y_pixels_per_inch;
77     int y_dots_per_pixel = dots_per_inch / y_dpi;
78     int num_rows = dev_print_scan_lines(pdev);
79 
80     int out_count;
81     int compression = -1;
82     static const char *const from2to3 = "\033*b3M";
83     static const char *const from3to2 = "\033*b2M";
84     int penalty_from2to3 = strlen(from2to3);
85     int penalty_from3to2 = strlen(from3to2);
86     int paper_size = gdev_pcl_paper_size((gx_device *) pdev);
87     int code = 0;
88     bool dup = pdev->Duplex;
89     bool dupset = pdev->Duplex_set >= 0;
90 
91     if (num_copies != 1 && !(features & PCL_CAN_PRINT_COPIES))
92 	return gx_default_print_page_copies(pdev, prn_stream, num_copies);
93     storage =
94 	(ulong *)gs_alloc_byte_array(pdev->memory, storage_size_words, W,
95 				     "hpjet_print_page");
96     if (storage == 0)		/* can't allocate working area */
97 	return_error(gs_error_VMerror);
98     data_words = storage;
99     out_row_words = data_words + (line_size_words * 2);
100     out_row_alt_words = out_row_words + (line_size_words * 2);
101     prev_row_words = out_row_alt_words + (line_size_words * 2);
102     /* Clear temp storage */
103     memset(data, 0, storage_size_words * W);
104 
105     /* Initialize printer. */
106     if (pdev->PageCount == 0) {
107 	fputs("\033E", prn_stream);	/* reset printer */
108 	/* If the printer supports it, set the paper size */
109 	/* based on the actual requested size. */
110 	if (features & PCL_CAN_SET_PAPER_SIZE) {
111 	    fprintf(prn_stream, "\033&l%dA", paper_size);
112 	}
113 	/* If printer can duplex, set duplex mode appropriately. */
114 	if (features & PCL_HAS_DUPLEX) {
115 	    if (dupset && dup)
116 		fputs("\033&l1S", prn_stream);
117 	    else if (dupset && !dup)
118 		fputs("\033&l0S", prn_stream);
119 	    else		/* default to duplex for this printer */
120 		fputs("\033&l1S", prn_stream);
121 	}
122     }
123     /* Put out per-page initialization. */
124     if (features & PCL_CAN_SET_PAPER_SIZE){
125         fprintf(prn_stream, "\033&l%dA", paper_size);
126     }
127     fputs("\033&l0o0l0E", prn_stream);
128     fputs(page_init, prn_stream);
129     fprintf(prn_stream, "\033&l%dX", num_copies);	/* # of copies */
130 
131     /* End raster graphics, position cursor at top. */
132     fputs("\033*rB\033*p0x0Y", prn_stream);
133 
134     /* The DeskJet and DeskJet Plus reset everything upon */
135     /* receiving \033*rB, so we must reinitialize graphics mode. */
136     if (features & PCL_END_GRAPHICS_DOES_RESET) {
137 	fputs(page_init, prn_stream);
138 	fprintf(prn_stream, "\033&l%dX", num_copies);	/* # of copies */
139     }
140 
141     /* Set resolution. */
142     fprintf(prn_stream, "\033*t%dR", x_dpi);
143 
144     /* Send each scan line in turn */
145     {
146 	int lnum;
147 	int num_blank_lines = 0;
148 	word rmask = ~(word) 0 << (-pdev->width & (W * 8 - 1));
149 
150 	/* Transfer raster graphics. */
151 	for (lnum = 0; lnum < num_rows; lnum++) {
152 	    register word *end_data =
153 	    data_words + line_size_words;
154 
155 	    code = gdev_prn_copy_scan_lines(pdev, lnum,
156 					    (byte *) data, line_size);
157 	    if (code < 0)
158 		break;
159 	    /* Mask off 1-bits beyond the line width. */
160 	    end_data[-1] &= rmask;
161 	    /* Remove trailing 0s. */
162 	    while (end_data > data_words && end_data[-1] == 0)
163 		end_data--;
164 	    if (end_data == data_words) {	/* Blank line */
165 		num_blank_lines++;
166 		continue;
167 	    }
168 	    /* We've reached a non-blank line. */
169 	    /* Put out a spacing command if necessary. */
170 	    if (num_blank_lines == lnum) {
171 		/* We're at the top of a page. */
172 		if (features & PCL_ANY_SPACING) {
173 		    if (num_blank_lines > 0)
174 			fprintf(prn_stream, "\033*p+%dY",
175 				num_blank_lines * y_dots_per_pixel);
176 		    /* Start raster graphics. */
177 		    fputs("\033*r1A", prn_stream);
178 		} else if (features & PCL_MODE_3_COMPRESSION) {
179 		    /* Start raster graphics. */
180 		    fputs("\033*r1A", prn_stream);
181 #if 1				/* don't waste paper */
182 		    if (num_blank_lines > 0)
183 			fputs("\033*b0W", prn_stream);
184 		    num_blank_lines = 0;
185 #else
186 		    for (; num_blank_lines; num_blank_lines--)
187 			fputs("\033*b0W", prn_stream);
188 #endif
189 		} else {
190 		    /* Start raster graphics. */
191 		    fputs("\033*r1A", prn_stream);
192 		    for (; num_blank_lines; num_blank_lines--)
193 			fputs("\033*bW", prn_stream);
194 		}
195 	    }
196 	    /* Skip blank lines if any */
197 	    else if (num_blank_lines != 0) {
198 		/*
199 		 * Moving down from current position causes head motion
200 		 * on the DeskJet, so if the number of lines is small,
201 		 * we're better off printing blanks.
202 		 */
203 		/*
204 		 * For Canon LBP4i and some others, <ESC>*b<n>Y doesn't
205 		 * properly clear the seed row if we are in compression mode
206 		 * 3.
207 		 */
208 		if ((num_blank_lines < MIN_SKIP_LINES && compression != 3) ||
209 		    !(features & PCL_ANY_SPACING)
210 		    ) {
211 		    bool mode_3ns =
212 			(features & PCL_MODE_3_COMPRESSION) &&
213 			!(features & PCL_ANY_SPACING);
214 
215 		    if (mode_3ns && compression != 2) {
216 			/* Switch to mode 2 */
217 			fputs(from3to2, prn_stream);
218 			compression = 2;
219 		    }
220 		    if (features & PCL_MODE_3_COMPRESSION) {
221 			/* Must clear the seed row. */
222 			fputs("\033*b1Y", prn_stream);
223 			num_blank_lines--;
224 		    }
225 		    if (mode_3ns) {
226 			for (; num_blank_lines; num_blank_lines--)
227 			    fputs("\033*b0W", prn_stream);
228 		    } else {
229 			for (; num_blank_lines; num_blank_lines--)
230 			    fputs("\033*bW", prn_stream);
231 		    }
232 		} else if (features & PCL3_SPACING) {
233 		    fprintf(prn_stream, "\033*p+%dY",
234 			    num_blank_lines * y_dots_per_pixel);
235 		} else {
236 		    fprintf(prn_stream, "\033*b%dY",
237 			    num_blank_lines);
238 		}
239 		/* Clear the seed row (only matters for */
240 		/* mode 3 compression). */
241 		memset(prev_row, 0, line_size);
242 	    }
243 	    num_blank_lines = 0;
244 
245 	    /* Choose the best compression mode */
246 	    /* for this particular line. */
247 	    if (features & PCL_MODE_3_COMPRESSION) {
248 		/* Compression modes 2 and 3 are both */
249 		/* available.  Try both and see which one */
250 		/* produces the least output data. */
251 		int count3 = gdev_pcl_mode3compress(line_size, data,
252 						    prev_row, out_row);
253 		int count2 = gdev_pcl_mode2compress(data_words, end_data,
254 						    out_row_alt);
255 		int penalty3 =
256 		    (compression == 3 ? 0 : penalty_from2to3);
257 		int penalty2 =
258 		    (compression == 2 ? 0 : penalty_from3to2);
259 
260 		if (count3 + penalty3 < count2 + penalty2) {
261 		    if (compression != 3)
262 			fputs(from2to3, prn_stream);
263 		    compression = 3;
264 		    out_data = out_row;
265 		    out_count = count3;
266 		} else {
267 		    if (compression != 2)
268 			fputs(from3to2, prn_stream);
269 		    compression = 2;
270 		    out_data = out_row_alt;
271 		    out_count = count2;
272 		}
273 	    } else if (features & PCL_MODE_2_COMPRESSION) {
274 		out_data = out_row;
275 		out_count = gdev_pcl_mode2compress(data_words, end_data,
276 						   out_row);
277 	    } else {
278 		out_data = data;
279 		out_count = (byte *) end_data - data;
280 	    }
281 
282 	    /* Transfer the data */
283 	    fprintf(prn_stream, "\033*b%dW", out_count);
284 	    fwrite(out_data, sizeof(byte), out_count,
285 		   prn_stream);
286 	}
287     }
288 
289     /* end raster graphics and eject page */
290     fputs("\033*rB\f", prn_stream);
291 
292     /* free temporary storage */
293     gs_free_object(pdev->memory, storage, "hpjet_print_page");
294 
295     return code;
296 }
297