summaryrefslogtreecommitdiff
path: root/contrib/japanese/gdevlbp3.c
blob: 8cbc2ce8bd1b27d9773250fc36f9877ed71ea50b (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
/* gdevlbp3.c */
/* Canon LASER SHOT LBP-310/320 printer driver for Ghostscript
 * working on standerd/fine image mode.
 *
 * If any comment, write to naoya@mahoroba.ne.jp
 */

#include "gdevprn.h"

#define	mm_to_inch(x)	(x)/25.4

/* The device descriptor */

static dev_proc_print_page(lbp310PrintPage);
static dev_proc_print_page(lbp320PrintPage);

gx_device_printer far_data gs_lbp310_device =
        prn_device(prn_std_procs,
        "lbp310",
        DEFAULT_WIDTH_10THS,
        DEFAULT_HEIGHT_10THS,
        600, 600,
        mm_to_inch(5.0),
        mm_to_inch(5.0),
        mm_to_inch(5.0),
        mm_to_inch(5.0),
        1, lbp310PrintPage);

gx_device_printer far_data gs_lbp320_device =
        prn_device(prn_std_procs,
        "lbp320",
        DEFAULT_WIDTH_10THS,
        DEFAULT_HEIGHT_10THS,
        600, 600,
        mm_to_inch(5.0),
        mm_to_inch(5.0),
        mm_to_inch(5.0),
        mm_to_inch(5.0),
        1, lbp320PrintPage);

enum	Paper
        {
                a4, a5, postcard, b5, letter
        };

struct	bounding
        {
                enum Paper	paper;
                int		Top, Bottom, Left, Right;
        };

struct	ppi
        {
                int	w;
                int	h;
                int	id;
        };

static	const struct	ppi PaperInfo[] =
        {
                {2100, 2960, 14}, /* A4 */
                {1485, 2098, 16}, /* A5 */
                {1000, 1480, 18}, /* Postcard */
                {1820, 2570, 26}, /* B5 */
                {2100, 2790, 14}  /* Letter */
        };

static void BoundImage(gx_device_printer *, struct bounding *);
static long CompressImage(gx_device_printer *, struct bounding *, gp_file *, const char *);

static int
lbp310PrintPage(gx_device_printer *pDev, gp_file *fp)
{
        int	i;
        char	Buf[10];
        long	DataSize;
        struct	bounding	Box;

        BoundImage(pDev, &Box);

        DataSize = CompressImage(pDev, &Box, fp, "\x1b[1;%d;%d;11;%d;.r");

        /* ----==== Set size ====---- */
        gs_sprintf(Buf, "0%ld", DataSize);
        i = (DataSize+strlen(Buf)+1)&1;
        /* ----==== escape to LIPS ====---- */
        gp_fprintf(fp, "\x80%s\x80\x80\x80\x80\x0c",Buf+i);
        gp_fprintf(fp, "\x1bP0J\x1b\\");

        return(0);
}

static int
lbp320PrintPage(gx_device_printer *pDev, gp_file *fp)
{
        int	i;
        char	Buf[16];
        long	DataSize;
        struct	bounding	Box;

        BoundImage(pDev, &Box);

        /* ----==== fix bounding box 4-byte align ====---- */
        Box.Left &= ~1;
        Box.Right |= 1;

        /* ----==== JOB start ??? ====---- */
        gp_fprintf(fp, "\x1b%%-12345X@PJL CJLMODE\n@PJL JOB\n");

        DataSize = CompressImage(pDev, &Box, fp, "\x1b[1;%d;%d;11;%d;.&r");

        /* ----==== Set size ====---- */
        gs_sprintf(Buf, "000%ld", DataSize);
        i = (DataSize+strlen(Buf)+1)&3;
        /* ----==== escape to LIPS ====---- */
        gp_fprintf(fp, "\x80%s\x80\x80\x80\x80\x0c",Buf+i);
        gp_fprintf(fp, "\x1bP0J\x1b\\");
        gp_fprintf(fp, "\x1b%%-12345X@PJL CJLMODE\n@PJL EOJ\n\x1b%%-12345X");

        return(0);
}

static void
BoundImage(gx_device_printer *pDev, struct bounding *pBox)
{
        int	x, y, flag;
        int	LineSize = gdev_mem_bytes_per_scan_line((gx_device *)pDev);
        int	Xsize, Ysize, Pt, Pb, Pl, Pr;
        int	Xres = (int)pDev->x_pixels_per_inch,
                Yres = (int)pDev->y_pixels_per_inch,
                height = pDev->height;
        byte	*Buf;
        enum	Paper	paper;

        /* ----==== Check parameters ====---- */
        paper = height*10/Yres < 82 ? postcard :\
                height*10/Yres < 98 ? a5 :\
                height*10/Yres < 109 ? b5 :\
                height*10/Yres < 116 ? letter : a4;
        Xsize = (int)(Xres * mm_to_inch(PaperInfo[paper].w-100) / 160);
        Ysize = (int)(Yres * mm_to_inch(PaperInfo[paper].h-100) / 10);
        /* ----==== Allocate momory ====---- */
        if (LineSize < Xsize*2+1) {
                LineSize = Xsize*2+1;
        }
        Buf = (byte *)gs_malloc(pDev->memory->non_gc_memory, 1, LineSize, "LineBuffer");
        /* ----==== bounding image ====---- */
        Pt = Pb = Pl = Pr = -1;
        for(y=0 ; y<height && y<Ysize ; y++){
                flag = 0;
                gdev_prn_copy_scan_lines(pDev, y, Buf, LineSize);
                for (x=0 ; x<min(LineSize/2, Xsize) ; x++) {
                        if (*(Buf+x*2) || *(Buf+x*2+1)) {
                                if (Pl == -1 || Pl > x) {
                                        Pl = x;
                                }
                                if (Pr < x) {
                                        Pr = x;
                                }
                                flag = 1;
                        }
                }
                if (flag) {
                        if (Pt == -1) {
                                Pt = y;
                        }
                        Pb = y;
                }
        }
        pBox->paper = paper;
        pBox->Top = Pt;
        pBox->Bottom = Pb;
        pBox->Left = Pl;
        pBox->Right = Pr;
        gs_free(pDev->memory->non_gc_memory, Buf, 1, LineSize, "LineBuffer");
}

static long
CompressImage(gx_device_printer *pDev, struct bounding *pBox, gp_file *fp, const char *format)
{
        int	x, y, i, count = 255;
        int	Xres = (int)pDev->x_pixels_per_inch;
        int	LineSize = gdev_mem_bytes_per_scan_line((gx_device *)pDev);
        byte	*Buf, oBuf[128], c_prev = 0, c_cur, c_tmp;
        long	DataSize = 0;

        /* ----==== Printer initialize ====---- */
        /* ----==== start TEXT mode ====---- */
        gp_fprintf(fp, "\x1b%%@");
        /* ----==== job start ====---- */
        gp_fprintf(fp, "\x1bP35;%d;1J;GhostScript\x1b\\", Xres);
        /* ----==== soft reset ====---- */
        gp_fprintf(fp, "\x1b<");
        /* ----==== select size as dot ====---- */
        gp_fprintf(fp, "\x1b[7 I");
        /* ----==== ??? ====---- */
        gp_fprintf(fp, "\x1b[;1;'v");
        /* ----==== set paper size ====---- */
        gp_fprintf(fp, "\x1b[%d;;p", PaperInfo[pBox->paper].id);
        /* ----==== select sheet feeder ====---- */
        gp_fprintf(fp, "\x1b[1q");
        /* ----==== disable automatic FF ====---- */
        gp_fprintf(fp, "\x1b[?2h");
        /* ----==== set number of copies ====---- */
        gp_fprintf(fp, "\x1b[%dv", 1);
        /* ----==== move CAP location ====---- */
        gp_fprintf(fp, "\x1b[%d;%df", pBox->Top, pBox->Left*16);
        /* ----==== draw raster image ====---- */
        gp_fprintf(fp, format, pBox->Right-pBox->Left+1,
                   Xres, pBox->Bottom-pBox->Top+1);

        /* ----==== Allocate momory ====---- */
        Buf = (byte *)gs_malloc(pDev->memory->non_gc_memory, 1, LineSize, "LineBuffer");
        /* ----==== transfer raster image ====---- */
        for (y=pBox->Top ; y<=pBox->Bottom ; y++) {
                gdev_prn_copy_scan_lines(pDev, y, Buf, LineSize);
                for (x=pBox->Left*2 ; x<=pBox->Right*2+1 ; x++) {
                /* ----==== check pointer & Reverse bit order ====---- */
                        c_cur = 0;
                        if (x<LineSize) {
                                c_tmp = *(Buf+x);
                                for (i=0 ; i<8 ; i++) {
                                        c_cur = (c_cur << 1) | (c_tmp & 1);
                                        c_tmp = c_tmp >> 1;
                                }
                        }
                        /* ----==== Compress data ====---- */
                        if (count < 0) {
                                if (c_prev == c_cur && count > -127) {
                                        count--;
                                        continue;
                                } else {
                                        gp_fprintf(fp, "%c%c", count, c_prev);
                                        DataSize += 2;
                                }
                        } else if (count == 0) {
                                if (c_prev == c_cur) {
                                        count--;
                                } else {
                                        count++;
                                        c_prev = *(oBuf+count) = c_cur;
                                }
                        continue;
                        } else if (count < 127) {
                                if (c_prev == c_cur) {
                                        gp_fprintf(fp, "%c", count-1);
                                        gp_fwrite(oBuf, 1, count, fp);
                                        DataSize += (count+1);
                                        count = -1;
                                } else {
                                        count++;
                                        c_prev = *(oBuf+count) = c_cur;
                                }
                        continue;
                        } else if (count == 127) {
                                gp_fprintf(fp, "%c", count);
                                gp_fwrite(oBuf, 1, count+1, fp);
                                DataSize += (count+2);
                        }
                        c_prev = *oBuf = c_cur;
                        count = 0;
                }
        }

        /* ----==== flush data ====---- */
        if (count < 0) {
                gp_fprintf(fp, "%c%c", count, c_prev);
                DataSize += 2;
        } else {
                gp_fprintf(fp, "%c", count);
                gp_fwrite(oBuf, 1, count+1, fp);
                DataSize += (count+2);
        }

        gs_free(pDev->memory->non_gc_memory, Buf, 1, LineSize, "LineBuffer");
        return(DataSize);
}