1 /* Copyright (C) 1996 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: gdevlp8k.c,v 1.5 2004/08/10 13:02:36 stefan Exp $*/
18
19 /* EPSON LP-8000 ESC-sequence Laser Printer driver for Ghostscript.
20
21 This driver structure is most close to that of the Epson 'ESC/P 2' language
22 printer driver "gdevescp.c" contributed by Richard Brown, but all the control
23 sequences and data formats are totally different.
24
25 The main driver strategy is as follows. The driver scans lines, skips empty
26 ones, removes leading and trailing zeros for other lines, compresses the
27 non-zero rest of each line and finally outputs the data.
28
29 At the moment the driver supports only 300x300 DPI resolution. If somebody
30 needs 240x240, another valid value for LP-8000 printer, he or she can try to
31 play with the corresponding values in initialization and termination
32 strings. Or I shall spend some extra time for hacking, if enough people
33 encourage me to do it. (The only available in our laboratory "Operation
34 guide" in Japanese does not contain any information about it. And LP-8000
35 driver for Japanese Windows does not support this mode either.)
36
37
38 The output data format is the following.
39
40 1. Initialization string, pretty long and sophisticated, I don't know why it
41 was necessary.
42
43 2. Data bits for each line. The most general format includes both starting X
44 and Y values as well as data type (simple or compressed).
45
46 3. Termination string.
47
48
49 DATA FORMATS
50
51 1. A simple (non-compressed) data format. By evident reasons it is NOT
52 SUPPORTED by the driver and is discussed here just as a starting point for
53 the future explanations. "\035" here is an alias for 0x1d ESC-character :
54
55 "\035" "Starting X point in ASCII format" "X"
56 "\035" "Starting Y point in ASCII format" "Y"
57 "\035" "Number of data BYTES for this printer line in ASCII format" ";"
58 "Number of POINTS to print in this line (equals to the
59 (Number of BYTES)*8)" ";"
60 "1;obi{I" "data BYTES for this line in BINARY format"
61
62 Both X and Y printer coordinates are 60 pixels shifted from the corresponding
63 coordinates of the Ghostscript display, that is X = x - 60, Y = y - 60. For
64 example, 1 inch left margin requires the value of 300 - 60 = 240 for
65 starting X printer coordinate. Similar, 1.5 inch top margin requires Y
66 values to start from 300*1.5 - 60 = 390.
67
68 The shortest possible abbreviation for the simple data format string is
69
70 "\035" "Starting Y point in ASCII format" "Y"
71 "\035" "Number of data BYTES for this printer line in ASCII format" ";"
72 "Number of POINTS to print in this line (equals to the
73 (Number of BYTES)*8)" ";"
74 "1;obi{I" "data BYTES for this line in BINARY format"
75
76 In this case the value of the starting X point is assumed to be equal to
77 that for the previous line.
78
79 An example of the data output for 2 printer lines
80
81 "\035"315X"\035"240Y"\035"2;16;1;obi{I"0ff0""\035"241Y"\035"3;24;1;obi{I"0f000f"
82
83 Here "0ff0" is an alias for 0x0f 0xf0 binary data, etc. The first line of the
84 above example starts from X=315, Y=240 and consists of 2 data bytes
85 resulting in 4 blank (white) points followed by 8 black points followed by 4
86 white points on the paper. The second line starts from X=315, Y=241 and
87 contains 3 data bytes resulting in output of 4 white, 4 black, 12 white and
88 finally 4 black points.
89
90 2. Compressed data format (SUPPORTED BY THE DRIVER).
91
92 General description is as follows.
93
94 "\035" "Starting X point in ASCII format" "X"
95 "\035" "Starting Y point in ASCII format" "Y"
96 "\035" "3bcI"
97 "\035" "Total number of compressed BYTES in ASCII format" ";"
98 "Number of POINTS to print in this line" ";"
99 "1;obi{I" "compressed data BYTES for this line in BINARY format"
100 "\035" "0bcI"
101
102 Additional ESC-sequences "\035" "3bcI" and "\035" "0bcI" mean start and end
103 of the compressed data format, respectively. As in the discussed above case
104 of a non-compressed data format, the shortest abbreviation has the form of
105
106 "\035" "Starting Y point in ASCII format" "Y"
107 "\035" "Total number of compressed BYTES in ASCII format" ";"
108 "Number of POINTS to print in this line" ";"
109 "1;obi{I" "compressed data BYTES for this line in BINARY format"
110
111 COMPRESSED DATA BYTES FORMAT has the form of
112
113 "d1 d2 d3 d4 d4 count_d4 d5 d6 d6 count_d6 ... d(n-1) d(n-1) count_d(n-1) dn"
114
115 Here dx (x = 1 ... n) means data in a BINARY format. Any 2 repeated bytes
116 MUST follow by the count, otherwise the printer will interpret the next
117 data byte as a counter. The count value indicates how many bytes of the
118 same value should be INSERTED after the repeated ones. So, the total number of
119 repeated bytes is (count + 2), not count. If there are only 2 equal data
120 bytes somewhere in the data stream, they MUST follow by zero.
121
122 Example of 2 compressed data strings.
123
124 "\035"105X"\035"320Y"\035"3bcI"\035"3;2048;1;obi{I"0000fe"
125 "\035"105X"\035"321Y"\035"11;2048;1;obi{I"0000021fffffe5fc000011"
126
127 The first one containing 3 bytes of compressed data will result in empty
128 (zero) line of 2048 blank points started from X=105, Y=320. The second one
129 containing 11 compressed data bytes will produce the picture of 4*8 + 3 = 35
130 white points followed by 5 + 16 + 0xe5*8 + 6 = 1859 black points followed by
131 2 + 8*19 = 154 white points (total 2048 points) started from X=105, Y=321.
132
133 Strictly speaking, it was not necessary to adjust the number of points to
134 the byte boundary. I did it for the sake of simplicity. One more argument in
135 favor of this step is that the error of positioning does not exceed (7 /
136 300) inches or (7 / 118) cm, that is 0.6 mm, which is negligible, I guess.
137
138
139 ADDITIONAL INFORMATION
140
141 It is also possible to use LP-8000 printer with 180x180 DPI resolution as an
142 "ibmpro" device from gdevepsn.c The only thing which should be corrected, is
143 the value 0x30 in static const char ibmpro_init_string[]. Decimal 36
144 fixes the 1,5 times elongation along the vertical axis. It is also
145 recommended to choose the appropriate values for all margins. In my case it
146 was 0.2, 0.6, 0, 0.3 in the device descriptor instead of the 0.2, 0.95, 0,
147 1.0
148
149 Nevertheless, typical Latex file looked so ugly after printing in this mode,
150 that I preferred to spend several days for hacking the format of the Japanese
151 Windows printer output for 300 DPI resolution and create my own driver.
152
153 Any suggestions, corrections, critical comments, etc. are welcome!
154
155 Oleg Fat'yanov <faty1@rlem.titech.ac.jp>
156
157 */
158
159
160 #include "gdevprn.h"
161
162 #ifndef X_DPI
163 #define X_DPI 300
164 #endif
165
166 #ifndef Y_DPI
167 #define Y_DPI 300
168 #endif
169
170 #define L_MARGIN 0.25
171 #define B_MARGIN 0.25
172 #define R_MARGIN 0.25
173 #define T_MARGIN 0.25
174
175 private dev_proc_print_page(lp8000_print_page);
176
177 gx_device_printer far_data gs_lp8000_device =
178 prn_device(prn_std_procs, "lp8000",
179 DEFAULT_WIDTH_10THS,
180 DEFAULT_HEIGHT_10THS,
181 X_DPI, Y_DPI,
182 L_MARGIN, B_MARGIN, R_MARGIN, T_MARGIN,
183 1, lp8000_print_page);
184
185
186 private int
lp8000_print_page(gx_device_printer * pdev,FILE * prn_stream)187 lp8000_print_page(gx_device_printer *pdev, FILE *prn_stream)
188 {
189
190 int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
191 int in_size = line_size;
192
193 byte *buf1 = (byte *)gs_malloc(pdev->memory, in_size, 1, "lp8000_print_page(buf1)");
194 byte *buf2 = (byte *)gs_malloc(pdev->memory, in_size, 1, "lp8000_print_page(buf2)");
195 byte *in = buf1;
196 byte *out = buf2;
197
198 int lnum, top, bottom, left, width;
199 int count, i, left1, left2, left0;
200
201 /* Check memory allocations */
202
203 if ( buf1 == 0 || buf2 == 0 )
204 { if ( buf1 )
205 gs_free(pdev->memory, (char *)buf1, in_size, 1, "lp8000_print_page(buf1)");
206
207 if ( buf2 )
208 gs_free(pdev->memory, (char *)buf2, in_size, 1, "lp8000_print_page(buf2)");
209
210 return_error(gs_error_VMerror);
211 }
212
213 /* Initialize the printer */
214
215 fwrite("\033\001@EJL \n",1,8,prn_stream);
216 fwrite("@EJL EN LA=ESC/PAGE\n",1,20,prn_stream);
217 fwrite("\035rhE\033\001@EJL \n",1,12,prn_stream);
218 fwrite("@EJL SE LA=ESC/PAGE\n",1,20,prn_stream);
219 fwrite("@EJL SET PU=1 PS=A4 ZO=OFF\n",1,27,prn_stream);
220 fwrite("@EJL EN LA=ESC/PAGE\n",1,20,prn_stream);
221 fwrite("\0350;0.24muE\0352;300;300drE",1,23,prn_stream);
222 fwrite("\0350;300;300drE\0351tsE\0351mmE",1,23,prn_stream);
223 fwrite("\0357isE\0355iaF\0355ipP\03514psE\0350poE",1,26,prn_stream);
224 fwrite("\03560;60loE\0350X\0350Y",1,15,prn_stream);
225 fwrite("\0350;0;2360;3388caE",1,17,prn_stream);
226 fwrite("\0351cmE\0350alfP",1,11,prn_stream);
227 fwrite("\0350affP\0350boP\0350abP",1,16,prn_stream);
228 fwrite("\0354ilG\0350bcI\0350sarG",1,16,prn_stream);
229 fwrite("\0351;0;100spE\0352owE",1,16,prn_stream);
230
231 /* Here the common part of the initialization string ends */
232
233
234 /* Calculate the PRINTER_LEFT_MARGIN = device_left_margin - 60 adjusted to
235 the byte boundary. Save this value for future comparison and set the
236 starting X value of the printer line.
237 */
238 left1 = (int) (L_MARGIN * pdev->x_pixels_per_inch) - 60;
239 left1 = (left1 >> 3) << 3;
240 left0 = left1;
241
242 fwrite("\035",1,1,prn_stream);
243 fprintf(prn_stream,"%d",left1);
244 fwrite("X",1,1,prn_stream);
245
246 /* Set the compressed data format */
247 fwrite("\0353bcI",1,5,prn_stream);
248
249 top = T_MARGIN * pdev->y_pixels_per_inch;
250 bottom = pdev->height - B_MARGIN * pdev->y_pixels_per_inch;
251
252 left = ( (int) (L_MARGIN * pdev->x_pixels_per_inch) ) >> 3 ;
253 width = ((pdev->width - (int)(R_MARGIN * pdev->x_pixels_per_inch)) >> 3) - left;
254
255 /*
256 ** Print the page:
257 */
258
259 for ( lnum = top; lnum < bottom ; )
260
261
262 {
263 byte *in_data;
264 byte *inp;
265 byte *in_end;
266 byte *outp;
267 register byte *p, *q;
268 int lcnt;
269
270 /*
271 ** Check buffer for 0 data.
272 */
273
274 gdev_prn_get_bits(pdev, lnum, in, &in_data);
275 while ( in_data[0] == 0 &&
276 !memcmp((char *)in_data, (char *)in_data + 1, line_size - 1) &&
277 lnum < bottom )
278 {
279 lnum++;
280 gdev_prn_get_bits(pdev, lnum, in, &in_data);
281 }
282
283 if(lnum == bottom ) break;
284 /* finished with this page */
285
286
287 lcnt = gdev_prn_copy_scan_lines(pdev, lnum, in, in_size);
288
289 inp = in + left;
290 in_end = inp + width;
291
292 /* Remove trailing 0s form the scan line data */
293
294 while (in_end > inp && in_end[-1] == 0)
295 {
296 in_end--;
297 }
298
299 /* Remove leading 0s form the scan line data */
300
301 for(left2 = 0; inp < in_end && inp[0] == 0; inp++,left2++);
302
303 /* Recalculate starting X value */
304
305 left2 = left1 + (left2 << 3);
306
307
308
309 /* Compress non-zero data for this line*/
310
311 outp = out;
312
313 for( p = inp, q = inp + 1 ; q < in_end ; )
314 {
315 if( *p != *q++ )
316 {
317 /*
318 Copy non-repeated bytes
319 to the output buffer
320 */
321 *outp++ = *p++;
322 }
323 else
324 {
325 for (count = 2; ( *p == *q ) && (q < in_end); q++, count++);
326
327 /*
328 Copy repeated bytes and counts to the output buffer.
329 As long as count is <= 255, additional step is necessary
330 for a long repeated sequence
331 */
332
333 while (count > 257)
334 {
335 *outp++ = *p;
336 *outp++ = *p;
337 *outp++ = 255;
338 p += 257;
339 count -=257;
340 }
341 *outp++ = *p;
342 *outp++ = *p;
343 *outp++ = count - 2;
344 p += count;
345 q = p+1;
346 }
347 }
348
349 /* The next line is necessary just in case of a single non-repeated byte at
350 the end of the input buffer */
351
352 if (p == (in_end - 1)) *outp++ = *p;
353
354 /* End of the compression procedure */
355
356
357 /* Set a new value of the starting X point, if necessary */
358
359 if (left2 != left0)
360 {
361 left0 = left2;
362 fwrite("\035",1,1,prn_stream);
363 fprintf(prn_stream,"%d",left2);
364 fwrite("X",1,1,prn_stream);
365 }
366
367 /* Output the data string to the printer.
368 Y coordinate of the printer equals (lnum - 60)
369 */
370
371 fwrite("\035",1,1,prn_stream);
372 fprintf(prn_stream,"%d",lnum-60);
373 fwrite("Y\035",1,2,prn_stream);
374 fprintf(prn_stream,"%d;",(outp - out));
375 fprintf(prn_stream,"%d;",(in_end - inp) << 3);
376 fwrite("1;0bi{I",1,7,prn_stream);
377 fwrite(out,1,(outp - out),prn_stream);
378
379 lnum++;
380
381 }
382
383 /* Send the termination string */
384
385 fwrite("\0350bcI",1,5,prn_stream);
386 fwrite("\0351coO",1,5,prn_stream);
387 fwrite("\035rhE",1,4,prn_stream);
388
389 fwrite("\033\001@EJL \n",1,8,prn_stream);
390 fwrite("@EJL SE LA=ESC/PAGE\n",1,20,prn_stream);
391 fwrite("@EJL SET PU=1 PS=A4 ZO=OFF\n",1,27,prn_stream);
392 fwrite("@EJL EN LA=ESC/PAGE\n",1,20,prn_stream);
393 fwrite("\0350;0.24muE\0352;300;300drE",1,23,prn_stream);
394 fwrite("\0350;300;300drE\0351tsE\0351mmE",1,23,prn_stream);
395 fwrite("\0357isE\0355iaF\0355ipP\03514psE\0350poE",1,26,prn_stream);
396 fwrite("\03560;60loE\0350X\0350Y",1,15,prn_stream);
397 fwrite("\0350;0;2360;3388caE",1,17,prn_stream);
398 fwrite("\0351cmE\0350alfP",1,11,prn_stream);
399 fwrite("\0350affP\0350boP\0350abP",1,16,prn_stream);
400 fwrite("\0354ilG\0350bcI\0350sarG",1,16,prn_stream);
401 fwrite("\035rhE",1,4,prn_stream);
402 fwrite("\033\001@EJL \n",1,8,prn_stream);
403 fwrite("\033\001@EJL \n",1,8,prn_stream);
404
405 fflush(prn_stream);
406
407 gs_free(pdev->memory, (char *)buf2, in_size, 1, "lp8000_print_page(buf2)");
408 gs_free(pdev->memory, (char *)buf1, in_size, 1, "lp8000_print_page(buf1)");
409 return 0;
410 }
411