summaryrefslogtreecommitdiff
path: root/gs/src/gdevlp8k.c
blob: 38188810f3bd7f7f30bfd264640f89dee690bd47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* Copyright (C) 1996 Aladdin Enterprises.  All rights reserved.
  
  This file is part of Aladdin Ghostscript.
  
  Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  or distributor accepts any responsibility for the consequences of using it,
  or for whether it serves any particular purpose or works at all, unless he
  or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  License (the "License") for full details.
  
  Every copy of Aladdin Ghostscript must include a copy of the License,
  normally in a plain ASCII text file named PUBLIC.  The License grants you
  the right to copy, modify and redistribute Aladdin Ghostscript, but only
  under certain conditions described in the License.  Among other things, the
  License requires that the copyright notice and this notice be preserved on
  all copies.
*/

/*$Id$*/

/* EPSON LP-8000 ESC-sequence Laser Printer driver for Ghostscript.

This driver structure is most close to that of the Epson 'ESC/P 2' language
printer driver "gdevescp.c" contributed by Richard Brown, but all the control
sequences and data formats are totally different.

The main driver strategy is as follows. The driver scans lines, skips empty
ones, removes leading and trailing zeros for other lines, compresses the
non-zero rest of each line and finally outputs the data.

At the moment the driver supports only 300x300 DPI resolution.  If somebody
needs 240x240, another valid value for LP-8000 printer, he or she can try to
play with the corresponding values in initialization and termination
strings. Or I shall spend some extra time for hacking, if enough people
encourage me to do it. (The only available in our laboratory "Operation
guide" in Japanese does not contain any information about it. And LP-8000
driver for Japanese Windows does not support this mode either.)


The output data format is the following. 

1. Initialization string, pretty long and sophisticated, I don't know why it
was necessary.  

2. Data bits for each line. The most general format includes both starting X
and Y values as well as data type (simple or compressed).

3. Termination string.


	DATA FORMATS

1. A simple (non-compressed) data format. By evident reasons it is NOT
SUPPORTED by the driver and is discussed here just as a starting point for
the future explanations. "\035" here is an alias for 0x1d ESC-character :

"\035" "Starting X point in ASCII format" "X" 
"\035" "Starting Y point in ASCII format" "Y"
"\035" "Number of data BYTES for this printer line in ASCII format" ";" 
"Number of POINTS to print in this line (equals to the 
(Number of BYTES)*8)" ";" 
"1;obi{I" "data BYTES for this line in BINARY format"

Both X and Y printer coordinates are 60 pixels shifted from the corresponding
coordinates of the Ghostscript display, that is X = x - 60, Y = y - 60. For
example, 1 inch left margin requires the value of 300 - 60 = 240 for
starting X printer coordinate. Similar, 1.5 inch top margin requires Y
values to start from 300*1.5 - 60 = 390.

The shortest possible abbreviation for the simple data format string is 

"\035" "Starting Y point in ASCII format" "Y" 
"\035" "Number of data BYTES for this printer line in ASCII format" ";"
"Number of POINTS to print in this line (equals to the 
(Number of BYTES)*8)" ";" 
"1;obi{I" "data BYTES for this line in BINARY format"

In this case the value of the starting X point is assumed to be equal to
that for the previous line.
 
An example of the data output for 2 printer lines

"\035"315X"\035"240Y"\035"2;16;1;obi{I"0ff0""\035"241Y"\035"3;24;1;obi{I"0f000f"

Here "0ff0" is an alias for 0x0f 0xf0 binary data, etc. The first line of the
above example starts from X=315, Y=240 and consists of 2 data bytes
resulting in 4 blank (white) points followed by 8 black points followed by 4
white points on the paper. The second line starts from X=315, Y=241 and
contains 3 data bytes resulting in output of 4 white, 4 black, 12 white and
finally 4 black points.

2. Compressed data format (SUPPORTED BY THE DRIVER).

General description is as follows.

"\035" "Starting X point in ASCII format" "X" 
"\035" "Starting Y point in ASCII format" "Y"
"\035" "3bcI" 
"\035" "Total number of compressed BYTES in ASCII format" ";" 
"Number of POINTS to print in this line" ";"
"1;obi{I" "compressed data BYTES for this line in BINARY format"
"\035" "0bcI"

Additional ESC-sequences "\035" "3bcI" and "\035" "0bcI" mean start and end
of the compressed data format, respectively.  As in the discussed above case
of a non-compressed data format, the shortest abbreviation has the form of

"\035" "Starting Y point in ASCII format" "Y"
"\035" "Total number of compressed BYTES in ASCII format" ";" 
"Number of POINTS to print in this line" ";"
"1;obi{I" "compressed data BYTES for this line in BINARY format"

COMPRESSED DATA BYTES FORMAT has the form of 

"d1 d2 d3 d4 d4 count_d4 d5 d6 d6 count_d6 ... d(n-1) d(n-1) count_d(n-1) dn"

Here dx (x = 1 ... n) means data in a BINARY format. Any 2 repeated bytes
MUST follow by the count, otherwise the printer will interpret the next
data byte as a counter. The count value  indicates how many bytes of the
same value should be INSERTED after the repeated ones. So, the total number of
repeated bytes is (count + 2), not count. If there are only 2 equal data
bytes somewhere in the data stream, they MUST follow by zero.

Example of 2 compressed data strings.

"\035"105X"\035"320Y"\035"3bcI"\035"3;2048;1;obi{I"0000fe"
"\035"105X"\035"321Y"\035"11;2048;1;obi{I"0000021fffffe5fc000011" 

The first one containing 3 bytes of compressed data will result in empty
(zero) line of 2048 blank points started from X=105, Y=320. The second one
containing 11 compressed data bytes will produce the picture of 4*8 + 3 = 35
white points followed by 5 + 16 + 0xe5*8 + 6 = 1859 black points followed by
2 + 8*19 = 154 white points (total 2048 points) started from X=105, Y=321.

Strictly speaking, it was not necessary to adjust the number of points to
the byte boundary. I did it for the sake of simplicity. One more argument in
favor of this step is that the error of positioning does not exceed (7 /
300) inches or (7 / 118) cm, that is 0.6 mm, which is negligible, I guess.


ADDITIONAL INFORMATION

It is also possible to use LP-8000 printer with 180x180 DPI resolution as an
"ibmpro" device from gdevepsn.c The only thing which should be corrected, is
the value 0x30 in static const char ibmpro_init_string[]. Decimal 36
fixes the 1,5 times elongation along the vertical axis. It is also
recommended to choose the appropriate values for all margins. In my case it
was 0.2, 0.6, 0, 0.3 in the device descriptor instead of the 0.2, 0.95, 0,
1.0 

Nevertheless, typical Latex file looked so ugly after printing in this mode,
that I preferred to spend several days for hacking the format of the Japanese
Windows printer output for 300 DPI resolution and create my own driver.

Any suggestions, corrections, critical comments, etc. are welcome!

Oleg Fat'yanov  <faty1@rlem.titech.ac.jp> 

*/


#include "gdevprn.h"

#ifndef X_DPI
#define X_DPI 300
#endif
     
#ifndef Y_DPI
#define Y_DPI 300
#endif
     
#define L_MARGIN 0.25
#define B_MARGIN 0.25
#define R_MARGIN 0.25
#define T_MARGIN 0.25

private dev_proc_print_page(lp8000_print_page);

gx_device_printer far_data gs_lp8000_device =
  prn_device(prn_std_procs, "lp8000",
	DEFAULT_WIDTH_10THS,
	DEFAULT_HEIGHT_10THS,
	X_DPI, Y_DPI,
	L_MARGIN, B_MARGIN, R_MARGIN, T_MARGIN,
	1, lp8000_print_page);
                                          
       
private int
lp8000_print_page(gx_device_printer *pdev, FILE *prn_stream)
{

        int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
        int in_size = line_size;
                              
	byte *buf1 = (byte *)gs_malloc(in_size, 1, "lp8000_print_page(buf1)");
        byte *buf2 = (byte *)gs_malloc(in_size, 1, "lp8000_print_page(buf2)");
        byte *in = buf1;
        byte *out = buf2;
                        
        int lnum, top, bottom, left, width;
        int count, i, left1, left2, left0;

/* Check  memory allocations  */         
        
        if ( buf1 == 0 || buf2 == 0 )
        {       if ( buf1 )
        	gs_free((char *)buf1, in_size, 1, "lp8000_print_page(buf1)");
                
                if ( buf2 )
                gs_free((char *)buf2, in_size, 1, "lp8000_print_page(buf2)");
          
          return_error(gs_error_VMerror);
      	}

/* Initialize the printer */
       
	fwrite("\033\001@EJL \n",1,8,prn_stream);
	fwrite("@EJL EN LA=ESC/PAGE\n",1,20,prn_stream);
	fwrite("\035rhE\033\001@EJL \n",1,12,prn_stream);
	fwrite("@EJL SE LA=ESC/PAGE\n",1,20,prn_stream);
	fwrite("@EJL SET PU=1 PS=A4 ZO=OFF\n",1,27,prn_stream);
	fwrite("@EJL EN LA=ESC/PAGE\n",1,20,prn_stream);
	fwrite("\0350;0.24muE\0352;300;300drE",1,23,prn_stream);
	fwrite("\0350;300;300drE\0351tsE\0351mmE",1,23,prn_stream);
	fwrite("\0357isE\0355iaF\0355ipP\03514psE\0350poE",1,26,prn_stream);
	fwrite("\03560;60loE\0350X\0350Y",1,15,prn_stream);
	fwrite("\0350;0;2360;3388caE",1,17,prn_stream);
	fwrite("\0351cmE\0350alfP",1,11,prn_stream);
	fwrite("\0350affP\0350boP\0350abP",1,16,prn_stream);
	fwrite("\0354ilG\0350bcI\0350sarG",1,16,prn_stream);
	fwrite("\0351;0;100spE\0352owE",1,16,prn_stream);
	
/* Here the common part of the initialization string ends */


/* Calculate the PRINTER_LEFT_MARGIN = device_left_margin - 60 adjusted to
the byte boundary. Save this value for future comparison and set the
starting X value of the printer line.
*/
 	left1  =  (int) (L_MARGIN * pdev->x_pixels_per_inch) - 60;
 	left1 = (left1 >> 3) << 3;
	left0 = left1;

	fwrite("\035",1,1,prn_stream);
        fprintf(prn_stream,"%d",left1);
        fwrite("X",1,1,prn_stream);
        
	/* Set the compressed data format */
        fwrite("\0353bcI",1,5,prn_stream);
      
      	top = T_MARGIN * pdev->y_pixels_per_inch;
        bottom = pdev->height - B_MARGIN * pdev->y_pixels_per_inch;
        
	left  = ( (int) (L_MARGIN * pdev->x_pixels_per_inch) ) >> 3 ;
 	width = ((pdev->width - (int)(R_MARGIN * pdev->x_pixels_per_inch)) >> 3) - left;
 
	/*
	** Print the page:
	*/

	for ( lnum = top; lnum < bottom ; )


	{	
		byte *in_data;
		byte *inp;
		byte *in_end;
		byte *outp;
		register byte *p, *q;
		int lcnt;

		/*
		** Check buffer for 0 data.
		*/

		gdev_prn_get_bits(pdev, lnum, in, &in_data);
		while ( in_data[0] == 0 &&
		        !memcmp((char *)in_data, (char *)in_data + 1, line_size - 1) &&
		        lnum < bottom )
	    	{	
			lnum++;
			gdev_prn_get_bits(pdev, lnum, in, &in_data);
		}

		if(lnum == bottom ) break;	
		/* finished with this page */


		lcnt = gdev_prn_copy_scan_lines(pdev, lnum, in, in_size);

		inp = in  + left;
		in_end = inp + width;

/* Remove trailing 0s form the scan line data */

		while (in_end > inp &&  in_end[-1] == 0) 
		{
		in_end--;
		}
		
/* Remove leading 0s form the scan line data */
	
	for(left2 = 0; inp < in_end && inp[0] == 0; inp++,left2++);

/* Recalculate starting X value */
 
	left2 = left1 + (left2 << 3);



/* Compress  non-zero data for this line*/

		outp = out;

 for( p = inp, q = inp + 1 ; q < in_end ; )
 	{
	if( *p != *q++ ) 
	  { 
	  /* 
	  Copy non-repeated bytes 
	  to the output buffer  
	  */
	  *outp++ = *p++;
	  } 
	  else 
	    {
	    for (count = 2; ( *p == *q ) && (q < in_end); q++, count++);

		/* 
		Copy repeated bytes and counts to the output buffer. 
		As long as count is <= 255, additional step is necessary 
		for a long repeated sequence
		*/
	    	
	    	while (count > 257)
	    	{
	    	*outp++ = *p;
	    	*outp++ = *p;
	    	*outp++ = 255;
	    	p += 257;
	    	count -=257;
		}
		*outp++ = *p;
	    	*outp++ = *p;
	    	*outp++ = count - 2;
	    	p += count;
		q = p+1;
	    }		
	} 

/* The next line is necessary just in case of a single non-repeated byte at
the end of the input buffer */

if (p == (in_end - 1)) *outp++ = *p;

/* End of the compression procedure */
	

/* Set a new value of the starting X point, if necessary  */

if (left2 != left0)
	{
	left0 = left2;
	fwrite("\035",1,1,prn_stream);
        fprintf(prn_stream,"%d",left2);
        fwrite("X",1,1,prn_stream);
        }

/* Output the data string to the printer. 
Y coordinate of the printer equals (lnum - 60)
*/        	

        fwrite("\035",1,1,prn_stream);
        fprintf(prn_stream,"%d",lnum-60);
        fwrite("Y\035",1,2,prn_stream);
        fprintf(prn_stream,"%d;",(outp - out));
	fprintf(prn_stream,"%d;",(in_end - inp) << 3); 
	fwrite("1;0bi{I",1,7,prn_stream);       
        fwrite(out,1,(outp - out),prn_stream);                 
        
        lnum++;
        
        }

/* Send the termination string */

        fwrite("\0350bcI",1,5,prn_stream);                        
	fwrite("\0351coO",1,5,prn_stream);
    	fwrite("\035rhE",1,4,prn_stream); 
        
        fwrite("\033\001@EJL \n",1,8,prn_stream);
	fwrite("@EJL SE LA=ESC/PAGE\n",1,20,prn_stream);
	fwrite("@EJL SET PU=1 PS=A4 ZO=OFF\n",1,27,prn_stream);
	fwrite("@EJL EN LA=ESC/PAGE\n",1,20,prn_stream);
	fwrite("\0350;0.24muE\0352;300;300drE",1,23,prn_stream);
	fwrite("\0350;300;300drE\0351tsE\0351mmE",1,23,prn_stream);
	fwrite("\0357isE\0355iaF\0355ipP\03514psE\0350poE",1,26,prn_stream);
	fwrite("\03560;60loE\0350X\0350Y",1,15,prn_stream);
	fwrite("\0350;0;2360;3388caE",1,17,prn_stream);
	fwrite("\0351cmE\0350alfP",1,11,prn_stream);
	fwrite("\0350affP\0350boP\0350abP",1,16,prn_stream);
	fwrite("\0354ilG\0350bcI\0350sarG",1,16,prn_stream);
	fwrite("\035rhE",1,4,prn_stream);
	fwrite("\033\001@EJL \n",1,8,prn_stream);
	fwrite("\033\001@EJL \n",1,8,prn_stream);
	
	fflush(prn_stream);
	
	gs_free((char *)buf2, in_size, 1, "lp8000_print_page(buf2)");
	gs_free((char *)buf1, in_size, 1, "lp8000_print_page(buf1)");
	return 0;
}