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