1 /* Copyright (C) 1990, 1995, 1997 Aladdin Enterprises.  All rights reserved.
2 
3   This file is part of Aladdin Ghostscript.
4 
5   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
6   or distributor accepts any responsibility for the consequences of using it,
7   or for whether it serves any particular purpose or works at all, unless he
8   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
9   License (the "License") for full details.
10 
11   Every copy of Aladdin Ghostscript must include a copy of the License,
12   normally in a plain ASCII text file named PUBLIC.  The License grants you
13   the right to copy, modify and redistribute Aladdin Ghostscript, but only
14   under certain conditions described in the License.  Among other things, the
15   License requires that the copyright notice and this notice be preserved on
16   all copies.
17 */
18 
19 /*
20   This driver supports ALPS MD Series printer 600dpi monochrome mode only.
21 
22    Feb.  3 1999   Ver. 0.1      MD5000 monochrome mode support.
23    Mar. 19 1999   Ver. 0.2      old MD series monochrome compressed mode
24                                 support. contributed by Kousuke Ikeda.
25 
26   There is no printer command refernces, so the sequence of initializing,
27   inc cartridge selecting, paper feeding and many specs are not clear yet.
28 
29   ESC 0x2a 0x62 n2 n1 0x59          raster line skip command.
30                                     skip (n1 * 0x100 + n2) lines.
31   ESC 0x2a 0x62 n2 n1 0x54 s2 s1    raster line print command.
32                                     skip (s1 * 0x100 + s2) * 8 dots from
33                                     paper side, then (n1 * 0x100 + n2) bytes
34                                     of MSB first data streams following.
35   ESC 0x2a 0x62 n2 n1 0x57          raster line print command for MD1300.
36                                     (applicable to MD1xxx and MD2xxx ?)
37                                     (n1 * 0x100 + n2) bytes of MSB first data
38                                     streams following,
39                                     but the data must be compressed.
40 */
41 
42 /* gdevalps.c */
43 /* Alps Micro Dry 600dpi monochrome printer driver */
44 #include "gdevprn.h"
45 
46 #define MD_TOP_MARGIN		0.47
47 #define MD_BOTTOM_MARGIN	0.59
48 #define MD_SIDE_MARGIN		0.13
49 
50 #define X_DPI 600
51 #define Y_DPI 600
52 #define LINE_SIZE ((X_DPI * 84 / 10 + 7) / 8)	/* bytes per line for letter */
53 
54 static int md50_print_page(gx_device_printer *, FILE *, const char *, int);
55 static dev_proc_open_device(md_open);
56 static dev_proc_print_page(md50m_print_page);
57 static dev_proc_print_page(md50e_print_page);
58 static dev_proc_print_page(md1xm_print_page);
59 
60 static gx_device_procs prn_md_procs =
61   prn_procs(md_open, gdev_prn_output_page, gdev_prn_close);
62 
63 gx_device_printer far_data gs_md50Mono_device =
64   prn_device(prn_md_procs, "md50Mono",
65         DEFAULT_WIDTH_10THS,
66         DEFAULT_HEIGHT_10THS,
67         600,				/* x_dpi */
68         600,				/* y_dpi */
69         0, 0, 0, 0,			/* margins */
70         1, md50m_print_page);
71 
72 gx_device_printer far_data gs_md50Eco_device =
73   prn_device(prn_md_procs, "md50Eco",
74         DEFAULT_WIDTH_10THS,
75         DEFAULT_HEIGHT_10THS,
76         600,				/* x_dpi */
77         600,				/* y_dpi */
78         0, 0, 0, 0,			/* margins */
79         1, md50e_print_page);
80 
81 gx_device_printer far_data gs_md1xMono_device =
82   prn_device(prn_md_procs, "md1xMono",
83         DEFAULT_WIDTH_10THS,
84         DEFAULT_HEIGHT_10THS,
85         600,				/* x_dpi */
86         600,				/* y_dpi */
87         0, 0, 0, 0,			/* margins */
88         1, md1xm_print_page);
89 
90 /* Normal black 600 x 600 dpi mode. */
91 static const char init_50mono[] = {
92 0x1b, 0x65,
93 0x1b, 0x25, 0x80, 0x41,
94 0x1b, 0x1a, 0x00, 0x00, 0x4c,
95 0x1b, 0x26, 0x6c, 0x01, 0x00, 0x48,
96 0x1b, 0x26, 0x6c, 0x07, 0x00, 0x4d,
97 0x1b, 0x26, 0x6c, 0x04, 0x00, 0x41,
98 0x1b, 0x2a, 0x72, 0x00, 0x55,
99 0x1b, 0x2a, 0x74, 0x03, 0x52,
100 0x1b, 0x26, 0x6c, 0xe5, 0x18, 0x50,
101 0x1b, 0x1a, 0x00, 0x00, 0x41,
102 0x1b, 0x26, 0x6c, 0x01, 0x00, 0x43, 0x00,
103 0x1b, 0x1a, 0x00, 0x00, 0x55,
104 0x1b, 0x2a, 0x72, 0x01, 0x41,
105 0x1b, 0x2a, 0x62, 0x00, 0x00, 0x4d,
106 0x1b, 0x1a, 0x00, 0x80, 0x72,
107 };
108 
109 /* ECO black 600 x 600 dpi mode. */
110 /* If you wanto to use ECO black casette, use this sequence for initialize. */
111 static const char init_50eco[] = {
112 0x1b, 0x65,
113 0x1b, 0x25, 0x80, 0x41,
114 0x1b, 0x1a, 0x00, 0x00, 0x4c,
115 0x1b, 0x26, 0x6c, 0x01, 0x00, 0x48,
116 0x1b, 0x26, 0x6c, 0x07, 0x00, 0x4d,
117 0x1b, 0x26, 0x6c, 0x04, 0x00, 0x41,
118 0x1b, 0x2a, 0x72, 0x01, 0x55,
119 0x1b, 0x2a, 0x74, 0x03, 0x52,
120 0x1b, 0x26, 0x6c, 0xe5, 0x18, 0x50,
121 0x1b, 0x1a, 0x00, 0x00, 0x41,
122 0x1b, 0x1a, 0x01, 0x00, 0x43,
123 0x1b, 0x26, 0x6c, 0x01, 0x00, 0x43, 0x17,
124 0x1b, 0x1a, 0x00, 0x00, 0x55,
125 0x1b, 0x2a, 0x72, 0x01, 0x41,
126 0x1b, 0x2a, 0x62, 0x00, 0x00, 0x4d,
127 0x1b, 0x1a, 0x16, 0x80, 0x72,
128 };
129 
130 /* Mono Black 600x600 mode for MD1300 */
131 static const char init_md13[] = {
132 0x1b, 0x65,
133 0x1b, 0x25, 0x80, 0x41,
134 0x1b, 0x1a, 0x00, 0x00, 0x4c,
135 0x1b, 0x26, 0x6c, 0x01, 0x00, 0x48,
136 0x1b, 0x26, 0x6c, 0x00, 0x00, 0x4d,
137 0x1b, 0x26, 0x6c, 0x04, 0x00, 0x41,
138 0x1b, 0x2a, 0x72, 0x00, 0x55,
139 0x1b, 0x2a, 0x74, 0x03, 0x52,
140 0x1b, 0x26, 0x6c, 0xe5, 0x18, 0x50,
141 0x1b, 0x1a, 0x00, 0x00, 0x41,
142 0x1b, 0x2a, 0x72, 0x00, 0x41,
143 0x1b, 0x2a, 0x62, 0x02, 0x00, 0x4d,
144 0x1b, 0x1a, 0x00, 0x00, 0x72,
145 };
146 
147 static const char end_md[] = {
148 0x0c,
149 0x1b, 0x2a, 0x72, 0x43,
150 0x1b, 0x25, 0x00, 0x58
151 };
152 /* ------ Internal routines ------ */
153 
154 /* Open the printer, and set the margins. */
155 static int
md_open(gx_device * pdev)156 md_open(gx_device *pdev)
157 {
158         static const float md_margins[4] =
159          {	MD_SIDE_MARGIN, MD_BOTTOM_MARGIN,
160                 MD_SIDE_MARGIN, MD_TOP_MARGIN
161          };
162 
163         gx_device_set_margins(pdev, md_margins, true);
164         return gdev_prn_open(pdev);
165 }
166 
167 /* MD5000 monochrome mode entrance. */
168 static int
md50m_print_page(gx_device_printer * pdev,FILE * prn_stream)169 md50m_print_page(gx_device_printer *pdev, FILE *prn_stream)
170 {
171   return(md50_print_page(pdev, prn_stream, init_50mono, sizeof(init_50mono)));
172 }
173 
174 /* MD5000 Eco mode monochrome mode entrance. */
175 static int
md50e_print_page(gx_device_printer * pdev,FILE * prn_stream)176 md50e_print_page(gx_device_printer *pdev, FILE *prn_stream)
177 {
178   return(md50_print_page(pdev, prn_stream, init_50eco, sizeof(init_50eco)));
179 }
180 
181 /* MD5000 monochrome mode print. */
182 static int
md50_print_page(gx_device_printer * pdev,FILE * prn_stream,const char * init_str,int init_size)183 md50_print_page(gx_device_printer *pdev, FILE *prn_stream,
184               const char *init_str, int init_size)
185 {
186   int lnum;
187   int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
188   byte *data = (byte *)gs_malloc(gs_lib_ctx_get_non_gc_memory_t(), 8, line_size, "md50_print_page(data)" );
189   int skipping = 0;
190   int nbyte;
191   int nskip;
192   int n;
193 
194     /* Load Paper & Select Inc Cartridge */
195   fwrite(init_str, sizeof(char), init_size, prn_stream);
196   fflush(prn_stream);
197 
198   for ( lnum = 0; lnum <= pdev->height; lnum++ ) {
199     byte *end_data = data + line_size;
200     byte *start_data = data;
201     memset(data, 0, LINE_SIZE);
202     n = gdev_prn_copy_scan_lines(pdev, lnum,
203                              (byte *)data, line_size);
204 
205     /* Remove trailing 0s. */
206     while ( end_data > data && end_data[-1] == 0 )
207       end_data--;
208     /* Count pre print skip octets */
209     while ( start_data < end_data && *start_data == 0 )
210       start_data++;
211     nbyte = end_data - start_data;
212     nskip = start_data - data;
213 
214     if(nbyte == 0)
215       {
216         skipping++;
217         continue;
218       }
219     else
220       {
221         if(skipping)
222           {
223             fprintf(prn_stream, "%c%c%c%c%c%c", 0x1b, 0x2a, 0x62,
224                     skipping & 0xff, (skipping & 0xff00) / 0x100, 0x59);
225             skipping = 0;
226           }
227         fprintf(prn_stream, "%c%c%c%c%c%c%c%c", 0x1b, 0x2a, 0x62,
228                 nbyte & 0xff, (nbyte & 0xff00) / 0x100, 0x54,
229                 nskip & 0xff, (nskip & 0xff00) / 0x100);
230         fwrite(start_data, sizeof(char), nbyte, prn_stream);
231       }
232   }
233 
234   /* Eject Page */
235   fwrite(end_md, sizeof(char), sizeof(end_md), prn_stream);
236   fflush(prn_stream);
237 
238   return 0;
239 }
240 
241 /* all? MD series monochrome mode print with data compression. */
242 static int
md1xm_print_page(gx_device_printer * pdev,FILE * prn_stream)243 md1xm_print_page(gx_device_printer *pdev, FILE *prn_stream)
244 {
245   int lnum;
246   int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
247   byte *data = (byte *)gs_malloc(gs_lib_ctx_get_non_gc_memory_t(), 8, line_size, "md1xm_print_page(data)");
248   byte *out_start = (byte *)gs_malloc(gs_lib_ctx_get_non_gc_memory_t(), 8, line_size, "md1xm_print_page(data)");
249   int skipping = 0;
250   int nbyte;
251 
252   /* Load Paper & Select Inc Cartridge */
253   fwrite(&init_md13[0], sizeof(char), sizeof(init_md13), prn_stream);
254   fflush(prn_stream);
255 
256   for ( lnum = 0; lnum <= pdev->height; lnum++ ) {
257     byte *end_data = data + line_size;
258     byte *data_p = data;
259     byte *out_data = out_start;
260     byte *p, *q;
261     int count;
262 
263     gdev_prn_copy_scan_lines(pdev, lnum, data, line_size);
264     /* Remove trailing 0s. */
265     while ( end_data > data && end_data[-1] == 0 )
266       end_data--;
267 
268      nbyte = end_data - data_p;
269 
270     if(nbyte == 0)
271       {
272         skipping++;
273         continue;
274       }
275     else
276       {
277         if(skipping)
278           {
279             fprintf(prn_stream, "%c%c%c%c%c%c", 0x1b, 0x2a, 0x62,
280                     skipping & 0xff, (skipping & 0xff00) / 0x100, 0x59);
281             skipping = 0;
282           }
283 
284         /* Following codes are borrowed from gdevescp.c */
285 
286         for ( p = data_p, q = data_p + 1; q < end_data; ){
287 
288           if( *p != *q ) {
289 
290             p += 2;
291             q += 2;
292 
293           } else {
294             /*
295             ** Check behind us, just in case:
296             */
297 
298             if( p > data_p && *p == *(p-1) )
299               p--;
300 
301             /*
302             ** walk forward, looking for matches:
303             */
304 
305             for( q++ ; *q == *p && q < end_data ; q++ ) {
306               if( (q-p) >= 128 ) {
307                 if( p > data_p ) {
308                   count = p - data_p;
309                   while( count > 128 ) {
310                     *out_data++ = '\177';
311                     memcpy(out_data, data_p, 128);	/* data */
312                     data_p += 128;
313                     out_data += 128;
314                     count -= 128;
315                   }
316                   *out_data++ = (char) (count - 1); /* count */
317                   memcpy(out_data, data_p, count);	/* data */
318                   out_data += count;
319                 }
320                 *out_data++ = '\201';	/* Repeat 128 times */
321                 *out_data++ = *p;
322                 p += 128;
323                 data_p = p;
324               }
325             }
326 
327             if( (q - p) > 2 ) {	/* output this sequence */
328               if( p > data_p ) {
329                 count = p - data_p;
330                 while( count > 128 ) {
331                   *out_data++ = '\177';
332                   memcpy(out_data, data_p, 128);	/* data */
333                   data_p += 128;
334                   out_data += 128;
335                   count -= 128;
336                 }
337                 *out_data++ = (char) (count - 1);	/* byte count */
338                 memcpy(out_data, data_p, count);	/* data */
339                 out_data += count;
340               }
341               count = q - p;
342               *out_data++ = (char) (256 - count + 1);
343               *out_data++ = *p;
344               p += count;
345               data_p = p;
346             } else	/* add to non-repeating data list */
347               p = q;
348             if( q < end_data )
349               q++;
350           }
351         }
352         /*
353         ** copy remaining part of line:
354         */
355 
356         if( data_p < end_data ) {
357 
358           count = end_data - data_p;
359 
360           /*
361           ** If we've had a long run of varying data followed by a
362           ** sequence of repeated data and then hit the end of line,
363           ** it's possible to get data counts > 128.
364           */
365 
366           while( count > 128 ) {
367             *out_data++ = '\177';
368             memcpy(out_data, data_p, 128);	/* data */
369             data_p += 128;
370             out_data += 128;
371             count -= 128;
372           }
373 
374           *out_data++ = (char) (count - 1);	/* byte count */
375           memcpy(out_data, data_p, count);	/* data */
376           out_data += count;
377         }
378 
379         nbyte = out_data - out_start;
380 
381         fprintf(prn_stream, "%c%c%c%c%c%c", 0x1b, 0x2a, 0x62,
382                 nbyte & 0xff, (nbyte & 0xff00) / 0x100, 0x57);
383         fwrite(out_start, sizeof(char), nbyte, prn_stream);
384       }
385   }
386 
387   /* Eject Page */
388   fwrite(end_md, sizeof(char), sizeof(end_md), prn_stream);
389   fflush(prn_stream);
390 
391   return 0;
392 }
393