1 /* Copyright (C) 2001-2019 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,
8    modified or distributed except as expressly authorized under the terms
9    of the license contained in the file LICENSE in this distribution.
10 
11    Refer to licensing information at http://www.artifex.com or contact
12    Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
13    CA 94945, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 /*
17  * Lexmark 5700 ink-jet printer driver for Ghostscript
18  *
19  * defines the lxm5700m device for printing in black-and-white at 1200 dpi
20  * doesn't handle color or any other resolution.
21  * Native resolution appears to be 600 x 1200, but print bands are overlapped.
22  *
23  * I use the command
24  * gs -sOutputFile=/dev/lp0 -sDevice=lxm5700m -dHeadSeparation=15 file.ps
25  *
26  * where HeadSeparation varies from print-cartridge to print-cartridge and
27  * 16 (the default) usually works fine.
28  *
29  *   Stephen Taylor  setaylor@ma.ultranet.com  staylor@cs.wpi.edu
30  */
31 
32 #include "gdevprn.h"
33 #include "gsparams.h"
34 
35 /* The procedure descriptors */
36 /* declare functions */
37 static dev_proc_print_page(lxm5700m_print_page);
38 static dev_proc_get_params(lxm_get_params);
39 static dev_proc_put_params(lxm_put_params);
40 
41 /* set up dispatch table.  I follow gdevdjet in using gdev_prn_output_page */
42 /* Since the print_page doesn't alter the device, this device can print in the background */
43 static const gx_device_procs lxm5700m_procs =
44     prn_params_procs(gdev_prn_open, gdev_prn_bg_output_page, gdev_prn_close,
45                      lxm_get_params, lxm_put_params);
46 
47 /* The device descriptors */
48 
49 /* define a subclass with useful state in it. */
50 typedef struct lxm_device_s { /* a sub-class of gx_device_printer */
51     gx_device_common;
52     gx_prn_device_common;
53     int headSeparation;
54 } lxm_device;
55 
56 /* Standard lxm5700m device */
57 lxm_device far_data gs_lxm5700m_device = {
58     prn_device_std_body(lxm_device, lxm5700m_procs, "lxm5700m",
59         DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
60         600, 600,	/* x dpi, y dpi */
61         0.2, 0.0, 0.0, 0.0,			/* margins */
62         1, lxm5700m_print_page),
63     16   /* default headSeparation value */
64 };
65 
66 /* I don't know the whole protocol for the printer, but let me tell
67  * you about the fraction I use.
68  * Each page begins with a header, which I describe as init1, init2, init3 --
69  *  (I separate them, because I've seen output in which those sections
70  *   seemed to appear independently, but I've deleted the only code I
71  *   had that actually used them separately.)
72  * Then there are a number of swipe commands, each of which describes one
73  * swipe of the printhead.  Each swipe command begins with a fixed
74  * header, which gives the number of bytes in the command,
75  * left and right margins,
76  * a vertical offset, some noise characters I don't know the meaning of,
77  * and the table of bits.  The bits are given one column at a time, using a
78  * simple compression scheme: a directory, consisting of two bytes, tells
79  * which sixteen-bit intervals of the 208-bit column contain 1-bits; then
80  * the appropriate number of sixteen-bit bit-maps follow.  (A zero-bit in the
81  * directory indicates that a bitmap for that sector follows.) In the worst case,
82  * this scheme would be bigger than the uncompressed bitmap, but it seems to
83  * usually save 80-90%.  The biggest complication of the bitmap scheme is this:
84  * There are two print-heads on the black cartridge, and they are 16 pixels
85  * (or headSeparation) apart.  Odd columns of the swipe address one printhead,
86  * and even columns the other.  On the following swipe, the printheads
87  * addressed by even and odd columns are reversed.  I think the printheads might be
88  * staggered, but the output I've seen staggers things in software;
89  * adjacent vertical bits on the same head are not addressed; the missing
90  * bits are written in by the second head when it passes (16 columns later.)
91  * In my code, I call the state of addressing one head or the other "direction".
92  * Originally I thought that the printhead was writing on both leftward and
93  * rightward motion, and that which head was addressed by odd columns changed
94  * accordingly.  I'm no longer sure this is true, but the head addressed by the
95  * even columns does alternate with each swipe.
96  */
97 /*
98  * various output shorthands
99  */
100 
101 #define init1() \
102         top(), \
103         0xA5,0, 3, 0x40,4,5, \
104         0xA5,0, 3, 0x40,4,6, \
105         0xA5,0, 3, 0x40,4,7, \
106         0xA5,0, 3, 0x40,4,8, \
107         0xA5,0, 4, 0x40,0xe0,0x0b, 3
108 
109 #define init2() \
110         0xA5,0, 11, 0x40,0xe0,0x41, 0,0,0,0,0,0,0, 2, \
111         0xA5,0, 6, 0x40, 5, 0,0,0x80,0 \
112 
113 #define init3()  \
114         0x1b,'*', 7,0x73,0x30, \
115         0x1b,'*', 'm', 0, 0x14, 3, 0x84, 2, 0, 1, 0xf4, \
116         0x1b,'*', 7,0x63, \
117         0x1b,'*', 'm', 0, 0x42,  0, 0, \
118         0xA5,0, 5, 0x40,0xe0,0x80, 8, 7, \
119         0x1b,'*', 'm', 0, 0x40, 0x15, 7, 0x0f, 0x0f  \
120 
121 #define top()  \
122         0xA5,0, 6, 0x40, 3,3,0xc0,0x0f,0x0f \
123 
124 #define fin()  \
125         0x1b,'*', 7, 0x65 \
126 
127 #define outByte(b) gp_fputc(b, prn_stream)
128 
129 #define RIGHTWARD 0
130 #define LEFTWARD 1
131 /* number of pixels between even columns in output and odd ones*/
132 /* #define headSeparation 16 */
133 /* overlap between successive swipes of the print head */
134 #define overLap 104
135 /* height of printhead in pixels */
136 #define swipeHeight 208
137 /* number of shorts described by each column directory */
138 #define directorySize 13
139 
140 /* ------ Driver procedures ------ */
141 
142 /* Send the page to the printer. */
143 static int
lxm5700m_print_page(gx_device_printer * pdev,gp_file * prn_stream)144 lxm5700m_print_page(gx_device_printer *pdev, gp_file *prn_stream)
145 {
146     int lnum,minX, maxX, i, l, highestX, leastX, extent;
147     int direction = RIGHTWARD;
148     int lastY = 0;
149     int code = 0;
150 
151     int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
152     /* Note that in_size is a multiple of 8. */
153     int in_size = line_size * (swipeHeight);
154     int swipeBuf_size = in_size;
155     byte *buf1 = (byte *)gs_malloc(pdev->memory, in_size, 1, "lxm_print_page(buf1)");
156     byte *swipeBuf =
157         (byte *)gs_malloc(pdev->memory, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");
158     byte *in = buf1;
159 
160     /* Check allocations */
161     if ( buf1 == 0 || swipeBuf == 0 ) {
162         code = gs_error_VMerror;
163         goto xit;
164     }
165 
166     {	/* Initialize the printer and reset the margins. */
167         static const char init_string[] = {
168             init1(),
169             init2(),
170             init3()
171         };
172         gp_fwrite(init_string, 1, sizeof(init_string), prn_stream);
173     }
174     /* Print lines of graphics */
175     for (lnum=0; lnum < pdev->height-swipeHeight ; ) { /* increment in body */
176         byte *in_data;
177         register byte *outp;
178         int lcnt;
179 
180         {	/* test for blank scan lines.  We  maintain the */
181             /* loop invariant lnum <pdev->height, but modify lnum */
182             int l;
183 
184             for (l=lnum; l<pdev->height; l++) {
185                 /* Copy 1 scan line and test for all zero. */
186                 code = gdev_prn_get_bits(pdev, l, in, &in_data);
187                 if (code < 0)
188                     goto xit;
189                 if ( in_data[0] != 0 ||
190                      memcmp((char *)in_data, (char *)in_data + 1, line_size - 1)
191                      ) {
192                     break;
193                 }
194             }/* end for l */
195 
196             /* now l is the next non-blank scan line */
197             if (l >= pdev->height) {/* if there are no more bits on this page */
198                 lnum = l;
199                 break;			/* end the loop and eject the page*/
200             }
201 
202             /* leave room for following swipe to reinforce these bits */
203             if (l-lnum > overLap) lnum = l - overLap;
204 
205             /* if the first non-blank near bottom of page */
206             if (lnum >=pdev->height - swipeHeight) {
207                 /* don't move the printhead over empty air*/
208                 lnum = pdev->height - swipeHeight;
209             }
210         }
211 
212         /* Copy the the scan lines. */
213         code = lcnt = gdev_prn_copy_scan_lines(pdev, lnum, in, in_size);
214         if (code < 0)
215             goto xit;
216         if ( lcnt < swipeHeight ) {
217             /* Pad with lines of zeros. */
218             memset(in + lcnt * line_size, 0,
219                    in_size - lcnt * line_size);
220         }
221 
222         /* compute right and left margin for this swipe */
223         minX = line_size;
224         maxX = 0;
225         for (l=0; l<swipeHeight; l++) {/* for each line of swipe */
226             for (i=0; i<minX; i++) {/* look for left-most non-zero byte*/
227                 if (in[l*line_size+i] !=0) {
228                     minX = i;
229                     break;
230                 }
231             }
232             for (i=line_size-1; i>=maxX; i--) {/* look for right-most */
233                 if (in[l*line_size+i] !=0) {
234                     maxX = i;
235                     break;
236                 }
237             }
238         }
239         minX = (minX&(-2)); /* truncate to even */
240         maxX = (maxX+3)&-2; /* raise to even */
241 
242         highestX = maxX*8-1;
243         leastX = minX*8;
244         extent = highestX -leastX +1;
245 
246         outp = swipeBuf;
247 
248         /* macro, not fcn call.  Space penalty is modest, speed helps */
249 #define buffer_store(x)\
250         {\
251             if (outp-swipeBuf>=swipeBuf_size) {\
252                 size_t  outp_offset = outp - swipeBuf;\
253                 size_t  swipeBuf_size_new = swipeBuf_size * 2;\
254                 byte*   swipeBuf_new = gs_malloc(pdev->memory, swipeBuf_size_new, 1, "lxm_print_page(swipeBuf_new)");\
255                 if (!swipeBuf_new) { code = gs_error_VMerror; goto xit; }\
256                 memcpy(swipeBuf_new, swipeBuf, swipeBuf_size);\
257                 gs_free(pdev->memory, swipeBuf, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");\
258                 swipeBuf_size = swipeBuf_size_new;\
259                 swipeBuf = swipeBuf_new;\
260                 outp = swipeBuf + outp_offset;\
261             }\
262             *outp++ = (x);\
263         }
264 
265             {/* work out the bytes to store for this swipe*/
266 
267                 int sx, sxBy8, sxMask;
268                 int words[directorySize];
269                 bool f, sum;
270                 int retval=0;
271                 int j,c,y;
272                 int j1,c1;
273                 int i,b,x, directory ;
274 
275                 /* want to set up pointers for (upto two) stripes covered by the output*/
276 
277                 /* now for each column covered by output: */
278                 for (x=leastX; x<=highestX; x++) {
279                     for (i=0; i<directorySize; i++) {
280                         words[i] = 0;
281                     }
282                     directory = 0x2000;	/* empty directory != 0 */
283 
284                     /* prime loops: make comparisons here */
285                     switch (direction) {
286                     case(RIGHTWARD):
287                         sx = (x&1)==1 ? x : x-(((lxm_device*)pdev)->headSeparation);
288                         j1 = (x&1);	/* even if x even, odd if x odd */
289                         break;
290                     default:	/* shouldn't happen ... but compilation checks */
291                     case(LEFTWARD):
292                         sx = (x&1)==0 ? x : x-((lxm_device*)pdev)->headSeparation;
293                         j1 = 1-(x&1);	/* odd if x even, even if x odd */
294                     }
295                     c1 = 0x8000 >> j1;
296 
297                     sxBy8 = sx/8;
298                     sxMask = 0x80>>(sx%8);
299 
300                     /* loop through all the swipeHeight bits of this column.
301 
302                     Note that <sx> looks like it can get out of range, so we
303                     check for this here. This fixes bug 701842.
304 
305                     [An alternative might be to change above code from 'maxX
306                     = (maxX+3)&-2' to 'maxX = (maxX+1)&-2', but that might be
307                     risky. */
308                     if (sx < pdev->width) {
309                         for (i = 0, b=1, y= sxBy8+j1*line_size; i < directorySize; i++,b<<=1) {
310                             sum = false;
311                             for (j=j1,c=c1 /*,y=i*16*line_size+sxBy8*/; j<16; j+=2, y+=2*line_size, c>>=2) {
312                                 f = (in[y]&sxMask);
313                                 if (f) {
314                                     words[i] |= c;
315                                     sum |= f;
316                                 }
317                             }
318                             if (!sum) directory |=b;
319                         }
320                     }
321                     retval+=2;
322                     buffer_store(directory>>8); buffer_store(directory&0xff);
323                     if (directory != 0x3fff) {
324                         for (i=0; i<directorySize; i++) {
325                             if (words[i] !=0) {
326                                 buffer_store(words[i]>>8) ; buffer_store(words[i]&0xff);
327                                 retval += 2;
328                             }
329                         }
330                     }
331                 }
332 #undef buffer_store
333             }
334         {/* now write out header, then buffered bits */
335             int leastY = lnum;
336 
337             /* compute size of swipe, needed for header */
338             int sz = 0x1a + outp - swipeBuf;
339 
340             /* put out header*/
341             int deltaY = 2*(leastY - lastY);  /* vert coordinates here are 1200 dpi */
342             lastY = leastY;
343             outByte(0x1b); outByte('*'); outByte(3);
344             outByte(deltaY>>8); outByte(deltaY&0xff);
345             outByte(0x1b); outByte('*'); outByte(4); outByte(0); outByte(0);
346             outByte(sz>>8); outByte(sz&0xff); outByte(0); outByte(3);
347             outByte(1); outByte(1); outByte(0x1a);
348             outByte(0);
349             outByte(extent>>8); outByte(extent&0xff);
350             outByte(leastX>>8); outByte(leastX&0xff);
351             outByte(highestX>>8); outByte(highestX&0xff);
352             outByte(0); outByte(0);
353             outByte(0x22); outByte(0x33); outByte(0x44);
354             outByte(0x55); outByte(1);
355             /* put out bytes */
356             gp_fwrite(swipeBuf,1,outp-swipeBuf,prn_stream);
357         }
358             lnum += overLap;
359             direction ^= 1;
360     }/* ends the loop for swipes of the print head.*/
361 
362     /* Eject the page and reinitialize the printer */
363     {
364         static const char bottom[] = {
365             fin() /*,  looks like I can get away with only this much ...
366             init1(),
367             init3(),
368             fin()   ,
369             top(),
370             fin()  */
371         };
372         gp_fwrite(bottom, 1, sizeof(bottom), prn_stream);
373     }
374     gp_fflush(prn_stream);
375 
376 xit:
377     if ( buf1 )
378         gs_free(pdev->memory, (char *)buf1, in_size, 1, "lxm_print_page(buf1)");
379     if ( swipeBuf )
380         gs_free(pdev->memory, (char *)swipeBuf, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");
381     if (code < 0)
382         return_error(code);
383     return code;
384 }
385 
386 /*
387  * There are a number of parameters which can differ between ink cartridges.
388  * The Windows driver asks you to recalibrate every time you load a new
389  * cartridge.
390  * most of the parameters adjusted there relate to color, and so this
391  * monotone driver doesn't need them.  However, the Lexmark 5700 black
392  * cartridge has two columns of dots, separated by about 16 pixels.
393  * This `head separation' distance can vary between cartridges, so
394  * we provide a parameter to set it.  In my small experience I've not
395  * set the corresponding parameter in windows to anything greater than 17
396  * or smaller than 15, but it would seem that it can vary from 1 to 32,
397  * based on the calibration choices offered.
398  *
399  * As I understand the rules laid out in gsparams.h,
400  * lxm_get_params is supposed to return the current values of parameters
401  * and lxm_put_params is supposed to set up values in the lxm_device
402  * structure which can be used by the lxm5700m_print_page routine.
403  * I've copied my routines from gdevcdj.c
404  */
405 
406 static int
lxm_get_params(gx_device * pdev,gs_param_list * plist)407 lxm_get_params(gx_device *pdev, gs_param_list *plist)
408 {
409     lxm_device* const ldev = (lxm_device*)pdev;
410     int code = gdev_prn_get_params(pdev, plist);
411 
412     if ( code < 0 ) return code;
413     code = param_write_int(plist,
414                            "HeadSeparation",
415                            (int *)&(ldev->headSeparation));
416 
417     return code;
418 }
419 
420 /* put_params is supposed to check all the parameters before setting any. */
421 static int
lxm_put_params(gx_device * pdev,gs_param_list * plist)422 lxm_put_params(gx_device *pdev, gs_param_list *plist)
423 {
424     int ecode;
425     lxm_device* const ldev = (lxm_device*)pdev;
426     int trialHeadSeparation=ldev->headSeparation;
427     int code = param_read_int(plist, "HeadSeparation", &trialHeadSeparation);
428 
429     if ( trialHeadSeparation < 1 || trialHeadSeparation > 32 )
430         param_signal_error(plist, "HeadSeparation", gs_error_rangecheck);
431     /* looks like param_signal_error is not expected to return */
432     ecode = gdev_prn_put_params(pdev, plist);	/* call super class put_params */
433     if ( code < 0 ) return code;
434     if (ecode < 0) return ecode;
435 
436     /* looks like everything okay; go ahead and set headSeparation */
437     ldev->headSeparation = trialHeadSeparation;
438     if ( code == 1) return ecode; /* I guess this means there is no "HeadSeparation" parameter */
439     return 0;
440 }
441