summaryrefslogtreecommitdiff
path: root/devices/gdevsj48.c
blob: b220f7845c6b3d1881e91efa69b12254aca84e5f (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
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/*
 * StarJet SJ48 printer driver.
 *
 * --- derived from gdevbj10.c 1993-10-07
 *                by Mats kerblom (f86ma@dd.chalmers.se).
 */

#include "gdevprn.h"

/*
 * The only available resolutions (in the program) are (180,360)x(180,360).
 *
 * Used control codes:
 *   <Esc>@	Printer reset
 *   <Esc>J<n>	Make a n/180 inch linefeed
 *   <Esc>\<a><b>	Move the print position (a+256b)/180 inch to the right
 *   <Esc>*<m><a><b>... Print graphics; m=39: 180*180 dpi
 *					m=40: 360*180 dpi
 *					m=71: 180*360 dpi
 *					m=72: 360*360 dpi
 *			a+256b columns is printed.
 */

/* The device descriptor */
static dev_proc_print_page(sj48_print_page);
/* The print_page proc is compatible with allowing bg printing */
gx_device_printer far_data gs_sj48_device =
  prn_device(gdev_prn_initialize_device_procs_mono_bg, "sj48",
        80,				/* width_10ths, 8" */
        105,				/* height_10ths, 10.5" */
        360,				/* x_dpi */
        360,				/* y_dpi */
        0,0,0,0,			/* margins */
        1, sj48_print_page);

/*   This comes from the bj10/bj200 source. I don't know how it applies
 *   for a StarJet.  --- Mats kerblom.
 *
 *
 * The following is taken from the BJ200 Programmer's manual.  The top
 * margin is 3mm (0.12"), and the bottom margin is 6.4mm (0.25").  The
 * left and right margin depend on the type of paper -- US letter or
 * A4 -- but ultimately rest on a print width of 203.2mm (8").  For letter
 * paper, the left margin (and hence the right) is 6.4mm (0.25"), while
 * for A4 paper, both are 3.4mm (0.13").
 *
 * The bottom margin requires a bit of care.  The image is printed
 * as strips, each about 3.4mm wide.  We can only attain the bottom
 * margin if the final strip coincides with it.  Note that each strip
 * is generated using only 48 of the available 64 jets, and the absence
 * of those bottom 16 jets makes our bottom margin, in effect, about
 * 1.1mm (0.04") larger.
 *
 * The bj200 behaves, in effect, as though the origin were at the first
 * printable position, rather than the top left corner of the page, so
 * we add a translation to the initial matrix to compensate for this.
 *
 * Except for the details of getting the margins correct, the bj200 is
 * no different from the bj10e, and uses the same routine to print each
 * page.
 *
 */

/* Send the page to the printer. */
static int
sj48_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{	int line_size = gx_device_raster((gx_device *)pdev, 0);
        int xres = (int)pdev->x_pixels_per_inch;
        int yres = (int)pdev->y_pixels_per_inch;
        int mode = (yres == 180 ?
                        (xres == 180 ? 39 : 40) :
                        (xres == 180 ? 71 : 72));
        int bytes_per_column = (yres == 180) ? 3 : 6;
        int bits_per_column = bytes_per_column * 8;
        int skip_unit = bytes_per_column * (xres == 180 ? 1 : 2); /* Skips in step of 1/180" */
        byte *in = (byte *)gs_malloc(pdev->memory, 8, line_size, "sj48_print_page(in)");
        byte *out = (byte *)gs_malloc(pdev->memory, bits_per_column, line_size, "sj48_print_page(out)");
        int lnum = 0;
        int skip = 0;
        int skips;
        int code = 0;
        int last_row = dev_print_scan_lines(pdev);
        int limit = last_row - bits_per_column;

        if ( in == 0 || out == 0 )
        {	code = gs_note_error(gs_error_VMerror);
                goto fin;
        }

        /* Abort if the requested resolution is unsupported. */
        if ((xres !=180 && xres != 360) || (yres !=180 && yres != 360))
        {	code = gs_note_error(gs_error_rangecheck);
                goto fin;
        }

        /* Initialize the printer. */
        gp_fwrite("\033@\000\000", 1, 4, prn_stream);  /* <Printer reset>, <0>, <0>. */

        /* Transfer pixels to printer.  The last row we can print is defined
           by "last_row".  Only the bottom of the print head can print at the
           bottom margin, and so we align the final printing pass.  The print
           head is kept from moving below "limit", which is exactly one pass
           above the bottom margin.  Once it reaches this limit, we make our
           final printing pass of a full "bits_per_column" rows. */
        while ( lnum < last_row )
           {
                byte *in_data;
                byte *in_end = in + line_size;
                byte *out_beg = out;
                byte *out_end = out + bytes_per_column * pdev->width;
                byte *outl = out;
                int bnum;

                /* Copy 1 scan line and test for all zero. */
                code = gdev_prn_get_bits(pdev, lnum, in, &in_data);
                if ( code < 0 ) goto xit;
                /* The mem... or str... functions should be faster than */
                /* the following code, but all systems seem to implement */
                /* them so badly that this code is faster. */
                   {	register const long *zip = (const long *)in_data;
                        register int zcnt = line_size;
                        register const byte *zipb;
                        for ( ; zcnt >= 4 * sizeof(long); zip += 4, zcnt -= 4 * sizeof(long) )
                           {	if ( zip[0] | zip[1] | zip[2] | zip[3] )
                                        goto notz;
                           }
                        zipb = (const byte *)zip;
                        while ( --zcnt >= 0 )
                           {
                                if ( *zipb++ )
                                        goto notz;
                           }
                        /* Line is all zero, skip */
                        lnum++;
                        skip++;
                        continue;
notz:			;
                   }

                /* Vertical tab to the appropriate position.  Note here that
                   we make sure we don't move below limit. */
                if ( lnum > limit )
                    {	skip -= (limit - lnum);
                        lnum = limit;
                    }

                /* The SJ48 can only skip in steps of 1/180" */
                if (yres == 180) {
                  skips = skip;
                } else {
                  if (skip & 1) {
                    skip--; /* Makes skip even. */
                    lnum--;
                  }
                  skips = skip/2;
                }

                while ( skips > 255 )
                   {	gp_fputs("\033J\377", prn_stream);
                        skips -= 255;
                   }
                if ( skips )
                        gp_fprintf(prn_stream, "\033J%c", skips);

                /* If we've printed as far as "limit", then reset "limit"
                   to "last_row" for the final printing pass. */
                if ( lnum == limit )
                        limit = last_row;
                skip = 0;

                /* Transpose in blocks of 8 scan lines. */
                for ( bnum = 0; bnum < bits_per_column; bnum += 8 )
                   {	int lcnt = min(8, limit - lnum);
                        byte *inp = in;
                        byte *outp = outl;
                        lcnt = gdev_prn_copy_scan_lines(pdev,
                                lnum, in, lcnt * line_size);
                        if ( lcnt < 0 )
                           {	code = lcnt;
                                goto xit;
                           }
                        if ( lcnt < 8 )
                                memset(in + lcnt * line_size, 0,
                                       (8 - lcnt) * line_size);
                        for ( ; inp < in_end; inp++, outp += bits_per_column )
                           {	gdev_prn_transpose_8x8(inp, line_size,
                                        outp, bytes_per_column);
                           }
                        outl++;
                        lnum += lcnt;
                        skip += lcnt;
                   }

                /* Send the bits to the printer.  We alternate horizontal
                   skips with the data.  The horizontal skips are in units
                   of 1/180 inches, so we look at the data in groups of
                   1 or 2 columns depending on resolution (controlled
                   by skip_unit).  */
                outl = out;
                do
                   {	int count;
                        int n;
                        byte *out_ptr;

                        /* First look for blank groups of columns. */
                        while(outl < out_end)
                           {	n = count = min(out_end - outl, skip_unit);
                                out_ptr = outl;
                                while ( --count >= 0 )
                                   {	if ( *out_ptr++ )
                                                break;
                                   }
                                if ( count >= 0 )
                                        break;
                                else
                                        outl = out_ptr;
                           }
                        if (outl >= out_end)
                                break;
                        if (outl > out_beg)
                           {	count = (outl - out_beg) / skip_unit;
                                gp_fprintf(prn_stream, "\033\\%c%c",
                                        count & 0xff, count >> 8);
                           }

                        /* Next look for non-blank groups of columns. */
                        out_beg = outl;
                        outl += n;
                        while(outl < out_end)
                           {	n = count = min(out_end - outl, skip_unit);
                                out_ptr = outl;
                                while ( --count >= 0 )
                                   {	if ( *out_ptr++ )
                                                break;
                                   }
                                if ( count < 0 )
                                        break;
                                else
                                        outl += n;
                           }
                        count = outl - out_beg;
                        {
                          /* What to transmit is the number of columns in the row.
                             Compare this with the <Esc>|*-command wich expects the
                             total number of bytes in the graphic row! */
                          int count1 = count/bytes_per_column;
                          gp_fprintf(prn_stream, "\033*%c%c%c",
                                  mode, count1 & 0xff, count1 >> 8);
                        }
                        gp_fwrite(out_beg, 1, count, prn_stream);
                        out_beg = outl;
                        outl += n;
                   }
                while ( out_beg < out_end );

                gp_fputc('\r', prn_stream);
                skip = bits_per_column;  /* <CR> only moves to the beginning of the row. */
           }

        /* Eject the page */
xit:	gp_fputc(014, prn_stream);	/* form feed */
        gp_fflush(prn_stream);
fin:	if ( out != 0 )
                gs_free(pdev->memory, (char *)out, bits_per_column, line_size,
                        "sj48_print_page(out)");
        if ( in != 0 )
                gs_free(pdev->memory, (char *)in, 8, line_size, "sj48_print_page(in)");
        return code;
}