summaryrefslogtreecommitdiff
path: root/contrib/japanese/gdevp201.c
blob: 1d3f4607c96ba918a2addc4ee1d754989ec173e2 (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
/* Copyright (C) 1990, 1992 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.  */

/* gdevp201.c */
/* NEC PC-PR201 printer driver for Ghostscript */
/* modified by m.mori for printers other than pr-201 */
/* modified by OkI for hires-98 */
/* modified by ASAYAMA Kazunori for Ghostscript version 2.6.1 */
#include "gdevprn.h"

/* There are NEC PC-PR printer paramaters.
        MODEL		DEVICE_NAME		X_DPI,Y_DPI		HEAD_PINS	LF_PITCH
        PC-PR201	"nec160"			160				24			18
        PC-PR1000	"nec240"			240				40			20
        PC-PR150	"nec320"			320				48			18
        PC-PR1000/4	"nec400"			400				60			18
*/

#define WIDTH 80				/* width_10ths, 8" */
#define HEIGHT 110				/* height_10ths, 11" */

enum{PR201, PR1000, PR150, PR1K4};

static dev_proc_print_page(pr201_print_page);

/* The device descriptor */
gx_device_printer gs_pr201_device =
  prn_device(prn_std_procs, "pr201",
        WIDTH,
        HEIGHT,
        160,
        160,
        0,0,0,0,		/* margins */
        1, pr201_print_page);

gx_device_printer gs_pr1000_device =
  prn_device(prn_std_procs, "pr1000",
        WIDTH,
        HEIGHT,
        240,
        240,
        0,0,0,0,		/* margins */
        1, pr201_print_page);

gx_device_printer gs_pr150_device =
  prn_device(prn_std_procs, "pr150",
        WIDTH,
        HEIGHT,
        320,
        320,
        0,0,0,0,		/* margins */
        1, pr201_print_page);

gx_device_printer gs_pr1000_4_device =
  prn_device(prn_std_procs, "pr1000_4",
        WIDTH,
        HEIGHT,
        400,
        400,
        0,0,0,0,		/* margins */
        1, pr201_print_page);

/* Transpose a block of 8x8 bits */
static int
pr201_transpose_8x8(byte *src, int src_step, byte *dst, int dst_step)
{
        byte mask, s, d0, d1, d2, d3, d4, d5, d6, d7;
        int i;

        d0 = d1 = d2 = d3 = d4 = d5 = d6 = d7 = 0;

        for(i=0, mask=0x01; i<8; i++, mask <<= 1) {
                s = *src;
                if(s & 0x80) d0 |= mask;
                if(s & 0x40) d1 |= mask;
                if(s & 0x20) d2 |= mask;
                if(s & 0x10) d3 |= mask;
                if(s & 0x08) d4 |= mask;
                if(s & 0x04) d5 |= mask;
                if(s & 0x02) d6 |= mask;
                if(s & 0x01) d7 |= mask;
                src += src_step;
        }

        *dst = d0;
        *(dst += dst_step) = d1;
        *(dst += dst_step) = d2;
        *(dst += dst_step) = d3;
        *(dst += dst_step) = d4;
        *(dst += dst_step) = d5;
        *(dst += dst_step) = d6;
        *(dst += dst_step) = d7;

        return 0;
}

static int
check_mode(const char* modename)
{
        if (!strcmp(modename, "pr201"))
                return PR201;
        else if (!strcmp(modename, "pr1000"))
                return PR1000;
        else if (!strcmp(modename, "pr150"))
                return PR150;
        else
                return PR1K4;
}

/* Send the page to the printer. */
static int
pr201_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{	int line_size;
        int height;
        int bits_per_column;
        int bytes_per_column;
        int chunk_size;
        byte *in, *out;
        int lnum, skip;
        int head_pins, lr_pitch, x_dpi;
        int code = 0;
        byte mask;
        int endidx = pdev->width>>3;

        switch (check_mode(pdev->dname)){
                case PR201:
                        head_pins=24; lr_pitch=18; x_dpi=160; break;
                case PR1000:
                        head_pins=40; lr_pitch=20; x_dpi=240; break;
                case PR150:
                        head_pins=48; lr_pitch=18; x_dpi=320; break;
                case PR1K4:
                default:
                        head_pins=60; lr_pitch=18; x_dpi=400; break;
        }

        line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
        height = pdev->height;
        bits_per_column	 = head_pins;
        bytes_per_column = bits_per_column / 8;
        chunk_size = bits_per_column * line_size;

        in = (byte *)
                gs_malloc(pdev->memory->non_gc_memory, bits_per_column, line_size, "pr201_print_page(in)");
        out = (byte *)
                gs_malloc(pdev->memory->non_gc_memory, bits_per_column, line_size, "pr201_print_page(out)");
        if(in == 0 || out == 0)
                return -1;

        if (pdev->width & 7)
            mask = ~(255>>(pdev->width & 7));
        else
            mask = 255, endidx--;

        /* Initialize printer */
        gp_fputs("\033cl", pdev->file);	/* Software Reset */
        gp_fputs("\033P", pdev->file);	/* Proportional Mode */
        if (check_mode(pdev->dname)==PR150){
                gp_fprintf(pdev->file, "\034d%d.", x_dpi); /* 320 dpi mode. */
        }
        gp_fprintf(pdev->file, "\033T%d" , lr_pitch);
                                /* 18/120 inch per line */

        /* Send Data to printer */
        lnum = 0;
        skip = 0;
        while(lnum < height) {
                byte *inp, *outp, *out_beg, *out_end, *p;
                int x, y, num_lines, size, mod, i;

                /* The number of lines to process */
                if((num_lines = height - lnum) > bits_per_column)
                        num_lines = bits_per_column;

                /* Copy scan lines */
                for (i = 0, p = in; i < num_lines; i++, p += line_size) {
                    code = gdev_prn_get_bits(pdev, lnum + i, p, NULL);
                    if (code < 0)
                        goto error;
                    p[endidx] &= mask;
                }

                /* Ensure we have a full stripe of line data */
                for (; i < bits_per_column; i++, p += line_size) {
                    memset(p, 0, line_size);
                }

                /* Test for all zero */
                size = line_size * num_lines;
                if(in[0] == 0 &&
                   !memcmp((char *)in, (char *)in + 1, size - 1)) {
                        lnum += bits_per_column;
                        skip ++;
                        continue;
                }

                /* Fill zero */
                if(num_lines < bits_per_column) {
                        size = line_size * (bits_per_column - num_lines);
                        memset(in + line_size * num_lines, 0, size);
                }
                lnum += bits_per_column;

                /* Vertical tab to the appropriate position. */
                while(skip > 72) {
                        gp_fprintf(pdev->file, "\037%c", 16 + 72);
                        skip -= 72;
                }
                if(skip > 0) {
                        gp_fprintf(pdev->file, "\037%c", 16 + skip);
                }

                /* Transpose in blocks of 8 scan lines. */
                for(y = 0; y < bytes_per_column; y ++) {
                        inp = in + line_size * 8 * y;
                        outp = out + y;
                        for(x = 0; x < line_size; x ++) {
                                pr201_transpose_8x8(inp, line_size,
                                                    outp, bytes_per_column);
                                inp ++;
                                outp += bits_per_column;
                        }
                }

                /* Remove trailing 0s. */
                out_end = out + chunk_size - 1;
                while(out_end >= out) {
                        if(*out_end)
                                break;
                        out_end --;
                }
                size = (out_end - out) + 1;
                if((mod = size % bytes_per_column) != 0)
                        out_end += bytes_per_column - mod;

                /* Remove leading 0s. */
                out_beg = out;
                while(out_beg <= out_end) {
                        if(*out_beg)
                                break;
                        out_beg ++;
                }
                out_beg -= (out_beg - out) % bytes_per_column;

                /* Dot addressing */
                gp_fprintf(pdev->file, "\033F%04" PRIdSIZE,
                           (out_beg - out) / bytes_per_column);

                /* Dot graphics */
                size = out_end - out_beg + 1;
                if (check_mode(pdev->dname)==PR201){
                        gp_fprintf(pdev->file,"\033J%04d", size / bytes_per_column);
                }else{
                        gp_fprintf(pdev->file,"\034bP,48,%04d.",
                                   size / bytes_per_column);
                }
                gp_fwrite(out_beg, size, 1, pdev->file);

                /* Carriage Return */
                gp_fputc('\r', pdev->file);
                skip = 1;
        }

        /* Form Feed */
        gp_fputc('\f',pdev->file);
        gp_fflush(pdev->file);

error:
        gs_free(pdev->memory->non_gc_memory, (char *)out,
                bits_per_column, line_size, "pr201_print_page(out)");
        gs_free(pdev->memory->non_gc_memory, (char *)in,
                bits_per_column, line_size, "pr201_print_page(in)");

        return code;
}