summaryrefslogtreecommitdiff
path: root/gs/src/gdevokii.c
blob: 7b0fc55987472647923cf6d4d6c2df882abea64f (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
/* Copyright (C) 1989, 1992, 1995 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$*/
/*
 * Okidata IBM compatible dot-matrix printer driver for Ghostscript.
 *
 * This device is for the Okidata Microline IBM compatible 9 pin dot
 * matrix printers.  It is derived from the Epson 9 pin printer driver
 * using the standard 1/72" vertical pin spacing and the 60/120/240
 * dpi horizontal resolutions.  The vertical feed resolution however
 * is 1/144" and the Okidata implements the standard 1/216" requests
 * through "scaling":
 *
 *   (power on)
 *   "\033J\001" (vertical feed 1/216")  => Nothing happens
 *   "\033J\001" (vertical feed 1/216")  => Advance 1/144"
 *   "\033J\001" (vertical feed 1/216")  => Advance 1/144"
 *   "\033J\001" (vertical feed 1/216")  => Nothing happens
 *   (and so on)
 *
 * The simple minded accounting used here keep track of when the
 * page actually advances assumes the printer starts in a "power on"
 * state.
 * 
 * Supported resolutions are:
 *
 *    60x72      60x144
 *   120x72     120x144
 *   240x72     240x144
 *
 */
#include "gdevprn.h"

/*
 * Valid values for X_DPI:
 *
 *     60, 120, 240
 *
 * The value specified at compile time is the default value used if the
 * user does not specify a resolution at runtime.
 */

#ifndef X_DPI
#  define X_DPI 120
#endif

/*
 * Valid values for Y_DPI:
 *
 *     72, 144
 *
 * The value specified at compile time is the default value used if the
 * user does not specify a resolution at runtime.
 */

#ifndef Y_DPI
#  define Y_DPI 72
#endif

/* The device descriptor */
private dev_proc_print_page(okiibm_print_page);

/* Okidata IBM device */
gx_device_printer far_data gs_okiibm_device =
  prn_device(prn_std_procs, "okiibm",
	DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
	X_DPI, Y_DPI,
	0.25, 0.0, 0.25, 0.0,			/* margins */
	1, okiibm_print_page);

/* ------ Internal routines ------ */

/* Forward references */
private void okiibm_output_run(P6(byte *, int, int, char, FILE *, int));

/* Send the page to the printer. */
private int
okiibm_print_page1(gx_device_printer *pdev, FILE *prn_stream, int y_9pin_high,
  const char *init_string, int init_length,
  const char *end_string, int end_length)
{	
	static const char graphics_modes_9[5] =
	{	
	-1, 0 /*60*/, 1 /*120*/, -1, 3 /*240*/
	};

	int in_y_mult = (y_9pin_high ? 2 : 1);
	int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
	/* Note that in_size is a multiple of 8. */
	int in_size = line_size * (8 * in_y_mult);
	byte *buf1 = (byte *)gs_malloc(in_size, 1, "okiibm_print_page(buf1)");
	byte *buf2 = (byte *)gs_malloc(in_size, 1, "okiibm_print_page(buf2)");
	byte *in = buf1;
	byte *out = buf2;
	int out_y_mult = 1;
	int x_dpi = pdev->x_pixels_per_inch;
	char start_graphics = graphics_modes_9[x_dpi / 60];
	int first_pass = (start_graphics == 3 ? 1 : 0);
	int last_pass = first_pass * 2;
	int y_passes = (y_9pin_high ? 2 : 1);
	int skip = 0, lnum = 0, pass, ypass;
	int y_step = 0;

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

	/* Initialize the printer. */
	fwrite(init_string, 1, init_length, prn_stream);

	/* Print lines of graphics */
	while ( lnum < pdev->height )
	{	
		byte *in_data;
		byte *inp;
		byte *in_end;
		byte *out_end;
		int lcnt;

		/* Copy 1 scan line and test for all zero. */
		gdev_prn_get_bits(pdev, lnum, in, &in_data);
		if ( in_data[0] == 0 &&
		     !memcmp((char *)in_data, (char *)in_data + 1, line_size - 1)
		   )
	    	{	
			lnum++;
			skip += 2 / in_y_mult;
			continue;
		}

		/*
		 * Vertical tab to the appropriate position.
		 * The skip count is in 1/144" steps.  If total
		 * vertical request is not a multiple od 1/72"
		 * we need to make sure the page is actually
		 * going to advance.
		 */
		if ( skip & 1 )
		{
			int n = 1 + (y_step == 0 ? 1 : 0);
			fprintf(prn_stream, "\033J%c", n);
			y_step = (y_step + n) % 3;
			skip -= 1;
		}
		skip = skip / 2 * 3;
		while ( skip > 255 )
		{	
			fputs("\033J\377", prn_stream);
			skip -= 255;
		}
		if ( skip )
		{
			fprintf(prn_stream, "\033J%c", skip);
		}

		/* Copy the the scan lines. */
	    	lcnt = gdev_prn_copy_scan_lines(pdev, lnum, in, in_size);
		if ( lcnt < 8 * in_y_mult )
		{	/* Pad with lines of zeros. */
			memset(in + lcnt * line_size, 0,
			       in_size - lcnt * line_size);
		}

		if ( y_9pin_high )
		{	/* Shuffle the scan lines */
			byte *p;
			int i;
			static const char index[] =
			{  0, 2, 4, 6, 8, 10, 12, 14,
			   1, 3, 5, 7, 9, 11, 13, 15
			};
			for ( i = 0; i < 16; i++ )
			{
				memcpy( out + (i * line_size),
				        in + (index[i] * line_size),
				        line_size);
			}
			p = in;
			in = out;
			out = p;
		}

	for ( ypass = 0; ypass < y_passes; ypass++ )
	{
	    for ( pass = first_pass; pass <= last_pass; pass++ )
	    {
		/* We have to 'transpose' blocks of 8 pixels x 8 lines, */
		/* because that's how the printer wants the data. */

	        if ( pass == first_pass )
	        {
		    out_end = out;
		    inp = in;
		    in_end = inp + line_size;
    
    	            for ( ; inp < in_end; inp++, out_end += 8 )
    	            { 
    		        gdev_prn_transpose_8x8(inp + (ypass * 8 * line_size), 
					       line_size, out_end, 1);
		    }
		    /* Remove trailing 0s. */
		    while ( out_end > out && out_end[-1] == 0 )
	            {
		       	out_end--;
		    }
		}

		/* Transfer whatever is left and print. */
		if ( out_end > out )
	        {
		    okiibm_output_run(out, (int)(out_end - out),
			           out_y_mult, start_graphics,
				   prn_stream, pass);
	        }
	    	fputc('\r', prn_stream);
	    }
	    if ( ypass < y_passes - 1 )
	    {
		int n = 1 + (y_step == 0 ? 1 : 0);
		fprintf(prn_stream, "\033J%c", n);
		y_step = (y_step + n) % 3;
	    }
	}
	skip = 16 - y_passes + 1;		/* no skip on last Y pass */
	lnum += 8 * in_y_mult;
	}

	/* Reinitialize the printer. */
	fwrite(end_string, 1, end_length, prn_stream);
	fflush(prn_stream);

	gs_free((char *)buf2, in_size, 1, "okiibm_print_page(buf2)");
	gs_free((char *)buf1, in_size, 1, "okiibm_print_page(buf1)");
	return 0;
}

/* Output a single graphics command. */
/* pass=0 for all columns, 1 for even columns, 2 for odd columns. */
private void
okiibm_output_run(byte *data, int count, int y_mult,
  char start_graphics, FILE *prn_stream, int pass)
{	
	int xcount = count / y_mult;

	fputc(033, prn_stream);
	fputc("KLYZ"[start_graphics], prn_stream);
	fputc(xcount & 0xff, prn_stream);
	fputc(xcount >> 8, prn_stream);
	if ( !pass )
	{
		fwrite(data, 1, count, prn_stream);
	}
	else
	{	
		/* Only write every other column of y_mult bytes. */
		int which = pass;
		register byte *dp = data;
		register int i, j;

		for ( i = 0; i < xcount; i++, which++ )
		{
			for ( j = 0; j < y_mult; j++, dp++ )
			{
				putc(((which & 1) ? *dp : 0), prn_stream);
			}
		}
	}
}

/* The print_page procedures are here, to avoid a forward reference. */

private const char okiibm_init_string[]	= { 0x18 };
private const char okiibm_end_string[]	= { 0x0c };
private const char okiibm_one_direct[]	= { 0x1b, 0x55, 0x01 };
private const char okiibm_two_direct[]	= { 0x1b, 0x55, 0x00 };

private int
okiibm_print_page(gx_device_printer *pdev, FILE *prn_stream)
{
	char init_string[16], end_string[16];
	int init_length, end_length;

	init_length = sizeof(okiibm_init_string);
	memcpy(init_string, okiibm_init_string, init_length);

	end_length = sizeof(okiibm_end_string);
	memcpy(end_string, okiibm_end_string, end_length);

	if ( pdev->y_pixels_per_inch > 72 &&
	     pdev->x_pixels_per_inch > 60 )
	{
		/* Unidirectional printing for the higher resolutions. */
		memcpy( init_string + init_length, okiibm_one_direct,
		        sizeof(okiibm_one_direct) );
		init_length += sizeof(okiibm_one_direct);

		memcpy( end_string + end_length, okiibm_two_direct,
		        sizeof(okiibm_two_direct) );
		end_length += sizeof(okiibm_two_direct);
	}
	
	return okiibm_print_page1( pdev, prn_stream, 
				   pdev->y_pixels_per_inch > 72 ? 1 : 0,
				   init_string, init_length,
				   end_string, end_length );
}