summaryrefslogtreecommitdiff
path: root/contrib/japanese/gdevml6.c
blob: 60ab151fdd0b90686a90234703b0e37e6230cd81 (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
/* Copyright (C) 1991 Aladdin Enterprises.  All rights reserved.
   Distributed by Free Software Foundation, Inc.

This file is part of Ghostscript.

Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing.  Refer
to the Ghostscript General Public License for full details.

Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License.  A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities.  It should be in a file named COPYING.  Among other
things, the copyright notice and this notice must be preserved on all
copies.  */

/* gdevml6.c */
/* OKI MICROLINE 620CL IPL mode driver for Ghostscript */
/*
        Mar. 14, 1998	N.Tagawa

        $Id: gdevml6.c,v 1.4 2002/07/30 18:53:22 easysw Exp $
*/

#include "gdevprn.h"

/* The device descriptors */
static dev_proc_open_device(ml600_open);
static dev_proc_close_device(ml600_close);
static dev_proc_print_page(ml600_print_page);

static int
ml600_initialize(gx_device *dev)
{
    int code = gdev_prn_initialize_mono(dev);

    if (code < 0)
        return code;

    set_dev_proc(dev, open_device, ml600_open);
    set_dev_proc(dev, close_device, ml600_close);

    return code;
}

static gx_device_procs ml600_procs =
    devprocs_initialize(ml600_initialize);

gx_device_printer gs_ml600_device =
  prn_device(ml600_procs, "ml600",
        83,				/* width_10ths, 8.3" */
        117,				/* height_10ths, 11.7" */
        600, 600,
        0.20, 0.20, 0.20, 0.20,		/* margins, lbrt */
        1, ml600_print_page);

/* ------ prototype ------ */
static int
ml_finish(
        gx_device_printer *pdev,
        gp_file           *fp);

static int
ml_init(
        gx_device_printer *pdev,
        gp_file           *fp);

static int
move_pos(
        gp_file *fp,
        int      n,
        int      m);

static int
make_line_data(
        byte	*curr_data,
        byte	*last_data,
        int	line_size,
        byte	*buf);

static int
send_line(
        byte    *buf,
        int      cnt,
        gp_file *fp);

static int
page_header(
        gx_device_printer *pdev,
        gp_file           *fp);

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

#define ESC 0x1b
#define LINE_SIZE ((7016+7) / 8)	/* bytes per line (A4 600 DPI) */
#define ppdev	((gx_device_printer *)pdev)

static int
ml600_open(
        gx_device *pdev)
{
        int        code = gdev_prn_open(pdev);
        gp_file   *prn_stream;

        /* dprintf("gdevml6: ml600_open called\n");*/

        if (code < 0)
                return code;

        code = gdev_prn_open_printer(pdev, true);
        if (code < 0)
                return code;

        prn_stream = ppdev->file;
        return ml_init(ppdev, prn_stream);
}

static int
ml600_close(
        gx_device *pdev)
{
        int        code = gdev_prn_open_printer(pdev, true);
        gp_file   *prn_stream;

        /* dprintf("gdevml6: ml600_close called\n"); */
        if (code < 0)
                return code;

        prn_stream = ppdev->file;
        ml_finish(ppdev, prn_stream);

        return gdev_prn_close(pdev);
}

/* Send the page to the printer.  */
static int
ml600_print_page(
        gx_device_printer *pdev,
        gp_file           *prn_stream)
{
        int	ystep;
        byte	data[2][LINE_SIZE*2];
        byte	buf[LINE_SIZE*2];
        int	skip;
        int	current;
        int	c_size;
        int	lnum;
        int	line_size;
        byte	rmask;
        int	i;

#define LAST	((current ^ 1) & 1)

        /* initialize this page */
        ystep = page_header(pdev, prn_stream);
        ystep /= 300;

        /* clear last sent line buffer */
        skip = 0;
        current = 0;
        for (i = 0; i < LINE_SIZE*2; i++)
                data[LAST][i] = 0;

        /* Send each scan line */
        line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
        if (line_size > LINE_SIZE || line_size == 0)
                return 0;

        rmask = (byte)(0xff << (-pdev->width & 7));	/* right edge */

        for (lnum = 0; lnum < pdev->height; lnum++) {
                (void)gdev_prn_copy_scan_lines(pdev, lnum, data[current],
                                                                line_size);
                /* Mask right edge bits */
                *(data[current] + line_size - 1) &= rmask;

                /* blank line ? */
                for (i = 0; i < line_size; i++)
                        if (data[current][i] != 0)
                                break;

                if (i == line_size) {
                        skip = 1;
                        current = LAST;
                        continue;
                }

                /* move position, if previous lines are skipped */
                if (skip) {
                        move_pos(prn_stream, lnum / ystep, lnum % ystep);
                        skip = 0;
                }

                /* create one line data */
                c_size = make_line_data(data[current], data[LAST],
                                                                line_size, buf);

                /* send one line data */
                send_line(buf, c_size, prn_stream);

                /* swap line buffer */
                current = LAST;
        }

        /* eject page */
        gp_fprintf(prn_stream, "\014");

        return 0;
}

static int
move_pos(
        gp_file *fp,
        int      n,
        int      m)
{
        gp_fprintf(fp, "%c%c%c%c%c%c", ESC, 0x7c, 0xa6, 1, 0, 2);
        gp_fprintf(fp, "%c%c%c%c%c%c%c%c%c", ESC, 0x7c, 0xa4, 4, 0,
                                            (n >> 8) & 0xff, n & 0xff, 0, 0);
        if (m > 0) {
                int	i;

                gp_fprintf(fp, "%c%c%c%c%c%c", ESC, 0x7c, 0xa6, 1, 0, 0);
                for (i = 0; i < m; i++) {
                        gp_fprintf(fp, "%c%c%c%c%c%c", ESC, 0x7c, 0xa7, 0, 1, 0);
                }
        }
        gp_fprintf(fp, "%c%c%c%c%c%c", ESC, 0x7c, 0xa6, 1, 0, 3);

        return 0;
}

static int
make_line_data(
        byte	*curr_data,
        byte	*last_data,
        int	line_size,
        byte	*buf)
{
        int	i;	/* raster data index */
        int	n;	/* output data index */
        int	bytes;	/* num of bitimage bytes in one block */
        int	offs;

#define TOP_BYTE(bytes,offs)	((((bytes - 1) & 7) << 5) | (offs & 0x1f))

        i = 0;
        n = 0;

        while (i < line_size) {
                /* skip unchanged bytes */
                offs = 0;
                while (i < line_size && curr_data[i] == last_data[i]) {
                        i++;
                        offs++;
                }
                if (i >= line_size)
                        break;

                /* count changed bytes (max 8) */
                bytes = 1;
                while (i + bytes < line_size && bytes < 8 &&
                                curr_data[i + bytes] != last_data[i + bytes]) {
                        bytes++;
                }

                /* make data */
                if (offs < 0x1f) {
                        buf[n++] = TOP_BYTE(bytes, offs);
                }
                else {
                        offs -= 0x1f;
                        buf[n++] = TOP_BYTE(bytes, 0x1f);

                        while (offs >= 0xff) {
                                offs -= 0xff;
                                buf[n++] = 0xff;
                        }
                        buf[n++] = offs;
                }

                /* write bitimage */
                while (bytes > 0) {
                        buf[n++] = curr_data[i++];
                        --bytes;
                }
        }

        return n;
}

static int
send_line(
        byte    *buf,
        int      cnt,
        gp_file *fp)
{
        gp_fprintf(fp, "%c%c%c", ESC, 0x7c, 0xa7);
        gp_fprintf(fp, "%c%c", (cnt >> 8) & 0xff, cnt & 0xff);
        return gp_fwrite(buf, sizeof(byte), cnt, fp);
}

static int
ml_init(
        gx_device_printer *pdev,
        gp_file           *fp)
{
        /* dprintf("gdevml6: ml_init called\n"); */

        gp_fprintf(fp, "%c%c%c", ESC, 0x2f, 0xf2);

        return 0;
}

static int
page_header(
        gx_device_printer *pdev,
        gp_file           *fp)
{
        int	ydpi;

        gp_fprintf(fp, "%c%c%c%c%c%c", ESC, 0x7c, 0xa0, 1, 0, 1);
        gp_fprintf(fp, "%c%c%c%c%c%c", ESC, 0x7c, 0xa1, 1, 0, 1);
        gp_fprintf(fp, "%c%c%c%c%c%c", ESC, 0x7c, 0xa2, 1, 0, 1);
        if (pdev->y_pixels_per_inch > 600) {	/* 600 x 1200 dpi */
                gp_fprintf(fp, "%c%c%c%c%c%c%c%c%c",
                                ESC, 0x7c, 0xa5, 4, 0, 2, 0x58, 4, 0xb0);
                ydpi = 1200;
        }
        else if (pdev->y_pixels_per_inch > 300) {	/* 600 dpi */
                gp_fprintf(fp, "%c%c%c%c%c%c%c", ESC, 0x7c, 0xa5, 2, 0, 2, 0x58);
                ydpi = 600;
        }
        else {
                gp_fprintf(fp, "%c%c%c%c%c%c%c", ESC, 0x7c, 0xa5, 2, 0, 1, 0x2c);
                ydpi = 300;
        }
        gp_fprintf(fp, "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
                   ESC, 0x7c, 0xf1, 0x0c, 0, 0, 1 , 0, 2, 0, 0, 0, 0, 0, 0, 0, 0);
        gp_fprintf(fp, "%c%c%c%c%c%c", ESC, 0x7c, 0xa6, 1, 0, 3);

        return ydpi;
}

static int
ml_finish(
        gx_device_printer *pdev,
        gp_file           *fp)
{
        gp_fprintf(fp, "%c%c%c", ESC, 0x2f, 0xfe);

        return 0;
}