1 /* Copyright (C) 1998, 1999 Norihito Ohmori.
2 
3    Ghostscript printer driver
4    for Canon LBP (LIPS IV)
5 
6    This software is distributed in the hope that it will be useful, but
7    WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
8    to anyone for the consequences of using it or for whether it serves any
9    particular purpose or works at all, unless he says so in writing.  Refer
10    to the GNU General Public License for full details.
11 
12    Everyone is granted permission to copy, modify and redistribute
13    this software, but only under the conditions described in the GNU
14    General Public License.  A copy of this license is supposed to have been
15    given to you along with this software so you can know your rights and
16    responsibilities.  It should be in a file named COPYING.  Among other
17    things, the copyright notice and this notice must be preserved on all
18    copies.
19  */
20 
21 /*$Id: gdevl4v.c $ */
22 /* Vector Version of LIPS driver */
23 
24 /*
25 
26    Vector driver �� Ghostscript 5.0 ���鿷���˲ä�ä��ɥ饤�ФǤ���
27    ��äƻ��ͤ����ꤷ�Ƥ��ޤ���Ghostscript 5.10 �� Ghostscript 5.50 �Ǥ�
28    Hi-level bitmap imaging �λ��ͤ��Ѥ�äƤ��ޤ���
29    Ghostscript 6.0 �ǤϹ��� text_begin �Ȥ��� API ���ɲä����褦�Ǥ���
30 
31    ��Ghostscript 5.10/5.50 �ΥХ��ˤĤ���
32 
33    Ghostscript 5.10/5.50 �� Vector driver �� setlinewidth �ؿ��ˤ�
34    �Х�������ޤ������襹�����뤬�ѹ������ˤ������ä������������ѹ�����
35    �ʤ���Фʤ�ޤ�����Ghostscript 5.10/5.50 �Ǥϥ���������θ����Τ�
36    ˺��Ƥ��ޤ���(�Х�����2��ۤ����ä��Τ������...)
37    ���Υɥ饤�ФϤ��ΥХ�������뤿��˥��������ʬ�ǽ������Ƥ��ޤ���
38 
39    Ghostscript 5.10 ����������ѥ��� Ghostscript 5.50 ����������ѥ�����
40    ���˸�Ψ�ΰ�����Τˤʤ뤳�Ȥ�����ޤ���LIPS IV �ν����Ǥ���³���Ķ����
41    ���Ȥ⤢��ޤ�������Ψ�ΰ����ѥ���®�٤��㲼�ˤ�Ĥʤ���Τ� Ghostscript
42    5.50 ��Ȥä��������Υɥ饤�ФǤϽ���®�٤�®���ʤ�ޤ���
43 
44    �ޤ� Ghostscript 5.10 �Ǥ�
45    fill_trapezoid��fill_parallelogram��fill_triangle
46    �δؿ��ε�ư���Ѥˤʤ뤳�Ȥ����ä��ΤǤ����ϻȤäƤ��ޤ���
47 
48    �� LIPS IV �ΥХ��ˤĤ���
49 
50    ���Υɥ饤�Фϳ�ȯ���ȯ���������� LIPS IV �ΥХ�������Ƥ��ޤ���
51    1. 1 dot �Υ��᡼�����ѷ������Ȥ��˺�ɸ�η׻�������
52    2. �����ʥ��᡼�������褹��������������ʤ�
53    3. ����åԥ�̿��ȥѥ�������åԥ�̿�᤬���줾����Ω��Ư���Ƥ��ޤ���
54 
55    1. �� 1 dot �Υ��᡼�������ˤ��Ʋ����ޤ�����
56    2. �ϥ��᡼���ΰ����̿���ʬ�����ꤹ�뤳�Ȥˤ������ޤ�����
57    3. �ϥ���åԥ�̿���Ȥ�ʤ����Ȥˤ��Ʋ����ޤ�����
58 
59    �� LIPS IV �λ��ͤˤĤ���
60 
61    1. ñ�����᡼�������顼����̿��ϥ��졼�������륤�᡼�����Ф��Ƥ�
62    Ư���Ƥ��ޤ��ޤ���
63    2. LIPS IV �Ǥϥ��졼��������ο�ɽ���� PostScript �ȵդǤ���
64    3. LIPS IV �ˤϥ��᡼���ΥӥåȤ�ȿž�����륳�ޥ�ɤϤ���ޤ���
65    4. LIPS IV �ˤ� CMYK �Υ��顼���ڡ����Ϥ���ޤ���
66 
67  */
68 
69 #include "string.h"
70 #include "math_.h"
71 #include "gx.h"
72 #include "gserrors.h"
73 #include "gsmatrix.h"
74 #include "gsparam.h"
75 #include "gxdevice.h"
76 #include "gscspace.h"
77 #include "gsutil.h"
78 #include "gdevvec.h"
79 #if 0
80 #include "gdevpstr.h"
81 #else
82 #include "spprint.h"
83 #endif
84 #include "ghost.h"
85 #include "gzstate.h"
86 #include "imemory.h"
87 #include "igstate.h"
88 #include "gdevlips.h"
89 
90 /* ---------------- Device definition ---------------- */
91 
92 /* Device procedures */
93 static dev_proc_open_device(lips4v_open);
94 static dev_proc_output_page(lips4v_output_page);
95 static dev_proc_close_device(lips4v_close);
96 static dev_proc_copy_mono(lips4v_copy_mono);
97 static dev_proc_copy_color(lips4v_copy_color);
98 static dev_proc_put_params(lips4v_put_params);
99 static dev_proc_get_params(lips4v_get_params);
100 static dev_proc_fill_mask(lips4v_fill_mask);
101 static dev_proc_begin_image(lips4v_begin_image);
102 
103 #define X_DPI 600
104 #define Y_DPI 600
105 
106 typedef struct gx_device_lips4v_s
107 {
108     gx_device_vector_common;
109     lips_params_common;
110     lips4_params_common;
111     bool first_page;
112     bool ManualFeed;
113     bool Duplex;
114     int Duplex_set;
115     bool Tumble;
116     bool OneBitMask;		/* for LIPS Bug */
117     int ncomp;
118     int MaskReverse;
119     int MaskState;
120     bool TextMode;
121     int prev_x;
122     int prev_y;
123     gx_color_index prev_color;
124     gx_color_index current_color;
125     int linecap;
126     /* for Font Downloading */
127 #define max_cached_chars 256	/* 128 * n */
128     bool FontDL;
129     int current_font;
130     int count;
131     gx_bitmap_id id_table[max_cached_chars + 1];
132     gx_bitmap_id id_cache[max_cached_chars + 1];
133 }
134 gx_device_lips4v;
135 
136 gs_public_st_suffix_add0_final(st_device_lips4v, gx_device_lips4v,
137                                "gx_device_lips4v", device_lips4v_enum_ptrs,
138                                device_lips4v_reloc_ptrs, gx_device_finalize,
139                                st_device_vector);
140 
141 #define lips_device_full_body(dtype, pprocs, dname, stype, w, h, xdpi, ydpi, ncomp, depth, mg, mc, dg, dc, lm, bm, rm, tm)\
142         std_device_part1_(dtype, pprocs, dname, stype, open_init_closed),\
143         dci_values(ncomp, depth, mg, mc, dg, dc),\
144         std_device_part2_(w, h, xdpi, ydpi),\
145         offset_margin_values(-lm * xdpi, -tm * ydpi, lm * 72.0, bm * 72.0, rm * 72.0, tm * 72.0),\
146         std_device_part3_()
147 
148 #define lips4v_device_body\
149   lips_device_full_body(gx_device_lips4v, 0, "lips4v",\
150                         &st_device_lips4v,\
151                         DEFAULT_WIDTH_10THS * X_DPI / 10,\
152                         DEFAULT_HEIGHT_10THS * Y_DPI / 10,\
153                         X_DPI, Y_DPI,\
154                         1, 8, 1000, 1000, 5, 2,\
155                         LIPS4_LEFT_MARGIN_DEFAULT,\
156                         LIPS4_BOTTOM_MARGIN_DEFAULT,\
157                         LIPS4_RIGHT_MARGIN_DEFAULT,\
158                         LIPS4_TOP_MARGIN_DEFAULT)
159 
160 #define lips4v_procs\
161         {	lips4v_open,\
162                 gx_upright_get_initial_matrix,\
163                 NULL,			/* sync_output */\
164                 lips4v_output_page,\
165                 lips4v_close,\
166                 gx_default_gray_map_rgb_color,\
167                 gx_default_gray_map_color_rgb,\
168                 gdev_vector_fill_rectangle,\
169                 NULL,			/* tile_rectangle */\
170                 lips4v_copy_mono,\
171                 lips4v_copy_color,\
172                 NULL,			/* draw_line */\
173                 NULL,			/* get_bits */\
174                 lips4v_get_params,\
175                 lips4v_put_params,\
176                 NULL,			/* map_cmyk_color */\
177                 NULL,			/* get_xfont_procs */\
178                 NULL,			/* get_xfont_device */\
179                 NULL,			/* map_rgb_alpha_color */\
180                 gx_page_device_get_page_device,\
181                 NULL,			/* get_alpha_bits */\
182                 NULL,			/* copy_alpha */\
183                 NULL,			/* get_band */\
184                 NULL,			/* copy_rop */\
185                 gdev_vector_fill_path,\
186                 gdev_vector_stroke_path,\
187                 lips4v_fill_mask,\
188                 gdev_vector_fill_trapezoid,\
189                 gdev_vector_fill_parallelogram,\
190                 gdev_vector_fill_triangle,\
191                 NULL /****** WRONG ******/,	/* draw_thin_line */\
192                 lips4v_begin_image,\
193                 NULL,\
194                 NULL,\
195                 NULL,			/* strip_tile_rectangle */\
196                 NULL/******strip_copy_rop******/\
197         }
198 
199 gx_device_lips4v far_data gs_lips4v_device = {
200     lips4v_device_body,
201     lips4v_procs,
202     vector_initial_values,
203     LIPS_CASSETFEED_DEFAULT,
204     LIPS_USERNAME_DEFAULT,
205     FALSE /* PJL */ ,
206     0 /* toner_density */ ,
207     FALSE /* toner_saving */ ,
208     0 /* toner_saving_set */ ,
209     0, 0, 0, 0, -1,
210     0 /* prev_duplex_mode */ ,
211     LIPS_NUP_DEFAULT,
212     LIPS_FACEUP_DEFAULT,
213     LIPS_MEDIATYPE_DEFAULT,
214     0 /* first_page */ ,
215     LIPS_MANUALFEED_DEFAULT,
216     0 /* Duplex */ , 0 /* Duplex_set */ , LIPS_TUMBLE_DEFAULT,
217     0 /* OneBitMask */ ,
218     0 /* ncomp */ , -1 /* MaskReverse */ , 0 /* MaskState */ ,
219     0 /* TextMode */ , 0 /* prev_x */ , 0 /* prev_y */ ,
220     0 /* prev_color */ , 0 /* current_color */ ,
221     0 /* linecap */ ,
222     0 /* FontDL */ ,
223     -1 /* current_font */ ,
224     0 /* count */ ,
225     {0},
226     {0}
227 };
228 
229 /* Vector device implementation */
230 #if GS_VERSION_MAJOR >= 8
231 static int lips4v_beginpage(gx_device_vector * vdev);
232 static int lips4v_setfillcolor(gx_device_vector * vdev, const gs_imager_state * pis,
233                                                                                 const gx_drawing_color * pdc);
234 static int lips4v_setstrokecolor(gx_device_vector * vdev, const gs_imager_state * pis,
235                                                                         const gx_drawing_color * pdc);
236 static int lips4v_setdash(gx_device_vector * vdev, const float *pattern,
237                               uint count, floatp offset);
238 static int lips4v_setflat(gx_device_vector * vdev, floatp flatness);
239 static int
240 lips4v_setlogop(gx_device_vector * vdev, gs_logical_operation_t lop,
241                  gs_logical_operation_t diff);
242 static int lips4v_can_handle_hl_color(gx_device_vector * vdev, const gs_imager_state * pis,
243                   const gx_drawing_color * pdc);
244 static int
245 
246 lips4v_beginpath(gx_device_vector * vdev, gx_path_type_t type);
247 static int
248 lips4v_moveto(gx_device_vector * vdev, floatp x0, floatp y0, floatp x,
249                floatp y, gx_path_type_t type);
250 static int
251 lips4v_lineto(gx_device_vector * vdev, floatp x0, floatp y0, floatp x,
252                floatp y, gx_path_type_t type);
253 static int
254 lips4v_curveto(gx_device_vector * vdev, floatp x0, floatp y0, floatp x1,
255                 floatp y1, floatp x2, floatp y2, floatp x3, floatp y3,
256                 gx_path_type_t type);
257 static int
258 lips4v_closepath(gx_device_vector * vdev, floatp x, floatp y, floatp x_start,
259                   floatp y_start, gx_path_type_t type);
260 
261 static int lips4v_endpath(gx_device_vector * vdev, gx_path_type_t type);
262 #else
263 static int lips4v_beginpage(P1(gx_device_vector * vdev));
264 static int lips4v_setfillcolor(P2(gx_device_vector * vdev,
265                                    const gx_drawing_color * pdc));
266 static int lips4v_setstrokecolor(P2(gx_device_vector * vdev,
267                                      const gx_drawing_color * pdc));
268 static int lips4v_setdash(P4(gx_device_vector * vdev, const float *pattern,
269                               uint count, floatp offset));
270 static int lips4v_setflat(P2(gx_device_vector * vdev, floatp flatness));
271 static int
272 lips4v_setlogop(P3
273 
274                 (gx_device_vector * vdev, gs_logical_operation_t lop,
275                  gs_logical_operation_t diff));
276 static int
277 
278 lips4v_beginpath(P2(gx_device_vector * vdev, gx_path_type_t type));
279 static int
280 lips4v_moveto(P6
281               (gx_device_vector * vdev, floatp x0, floatp y0, floatp x,
282                floatp y, gx_path_type_t type));
283 static int
284 lips4v_lineto(P6
285               (gx_device_vector * vdev, floatp x0, floatp y0, floatp x,
286                floatp y, gx_path_type_t type));
287 static int
288 lips4v_curveto(P10
289                (gx_device_vector * vdev, floatp x0, floatp y0, floatp x1,
290                 floatp y1, floatp x2, floatp y2, floatp x3, floatp y3,
291                 gx_path_type_t type));
292 static int
293 lips4v_closepath(P6
294                  (gx_device_vector * vdev, floatp x, floatp y, floatp x_start,
295                   floatp y_start, gx_path_type_t type));
296 
297 static int lips4v_endpath(P2(gx_device_vector * vdev, gx_path_type_t type));
298 #endif
299 static int lips4v_setlinewidth(gx_device_vector * vdev, floatp width);
300 static int lips4v_setlinecap(gx_device_vector * vdev, gs_line_cap cap);
301 static int lips4v_setlinejoin(gx_device_vector * vdev, gs_line_join join);
302 static int lips4v_setmiterlimit(gx_device_vector * vdev, floatp limit);
303 static const gx_device_vector_procs lips4v_vector_procs = {
304     /* Page management */
305     lips4v_beginpage,
306     /* Imager state */
307     lips4v_setlinewidth,
308     lips4v_setlinecap,
309     lips4v_setlinejoin,
310     lips4v_setmiterlimit,
311     lips4v_setdash,
312     lips4v_setflat,
313     lips4v_setlogop,
314     /* Other state */
315 #if GS_VERSION_MAJOR >= 8
316     lips4v_can_handle_hl_color,		/* can_handle_hl_color (dummy) */
317 #endif
318     lips4v_setfillcolor,	/* fill & stroke colors are the same */
319     lips4v_setstrokecolor,
320     /* Paths */
321     gdev_vector_dopath,
322     gdev_vector_dorect,
323     lips4v_beginpath,
324     lips4v_moveto,
325     lips4v_lineto,
326     lips4v_curveto,
327     lips4v_closepath,
328     lips4v_endpath
329 };
330 
331 /* ---------------- File header ---------------- */
332 
333 static const char *l4v_file_header1 =
334 
335     "\033%-12345X@PJL CJLMODE\n@PJL JOB\n\033%-12345X@PJL CJLMODE\n";
336 static const char *l4v_file_header2 = "@PJL SET LPARM : LIPS SW2 = ON\n";
337 static const char *l4v_file_header3 = "@PJL ENTER LANGUAGE = LIPS\n";
338 static const char *l4v_file_header4 = "\033%@\033P41;";
339 
340 static const char *l4vmono_file_header =
341 
342     ";1J" L4VMONO_STRING LIPS_VERSION "\033\\\033[0\"p\033<";
343 
344 static const char *l4vcolor_file_header =
345 
346     ";1J" L4VCOLOR_STRING LIPS_VERSION "\033\\\033[1\"p\033<";
347 
348 /* ---------------- Utilities ---------------- */
349 
350 static void
lips_param(int param,char * c)351 lips_param(int param, char *c)
352 {
353     int i, j;
354     bool bSign;
355 
356     bSign = TRUE;
357     if (param < 0) {
358         bSign = FALSE;
359         param = -param;
360     }
361     if (param < 16)
362         i = 1;
363     else if (param < 1024)
364         i = 2;
365     else if (param < 65536)
366         i = 3;
367     else
368         i = 4;
369 
370     c[i] = '\0';
371     c[i - 1] = (param & 0x0f) | 0x20 | (bSign ? 0x10 : 0x00);
372     param >>= 4;
373     for (j = i - 2; j >= 0; j--) {
374         c[j] = (param & 0x3f) | 0x40;
375         param >>= 6;
376     }
377 }
378 
379 static void
sput_lips_int(stream * s,int param)380 sput_lips_int(stream * s, int param)
381 {
382     int i;
383     char c[5];
384 
385     lips_param(param, c);
386     for (i = 0; i < strlen(c); i++)
387         sputc(s, c[i]);
388 }
389 
390 /* Put a string on a stream.
391    This function is copy of `pputs' in gdevpstr.c */
392 static int
lputs(stream * s,const char * str)393 lputs(stream * s, const char *str)
394 {
395     uint len = strlen(str);
396     uint used;
397     int status = sputs(s, (const byte *)str, len, &used);
398 
399     return (status >= 0 && used == len ? 0 : EOF);
400 }
401 
402 /* Write a string on a stream. */
403 static void
put_bytes(stream * s,const byte * data,uint count)404 put_bytes(stream * s, const byte * data, uint count)
405 {
406     uint used;
407 
408     sputs(s, data, count, &used);
409 }
410 
411 /* for Font Downloading */
412 static void
put_int(stream * s,uint number)413 put_int(stream * s, uint number)
414 {
415     sputc(s, number >> 8);
416     sputc(s, number & 0xff);
417 }
418 
419 static int
lips4v_range_check(gx_device * dev)420 lips4v_range_check(gx_device * dev)
421 {
422     int width = dev->MediaSize[0];
423     int height = dev->MediaSize[1];
424     int xdpi = dev->x_pixels_per_inch;
425     int ydpi = dev->y_pixels_per_inch;
426 
427     /* Paper Size Check */
428     if (width <= height) {	/* portrait */
429         if ((width < LIPS_WIDTH_MIN || width > LIPS_WIDTH_MAX ||
430              height < LIPS_HEIGHT_MIN || height > LIPS_HEIGHT_MAX) &&
431             !(width == LIPS_LEDGER_WIDTH && height == LIPS_LEDGER_HEIGHT))
432             return_error(gs_error_rangecheck);
433     } else {			/* landscape */
434         if ((width < LIPS_HEIGHT_MIN || width > LIPS_HEIGHT_MAX ||
435              height < LIPS_WIDTH_MIN || height > LIPS_WIDTH_MAX) &&
436             !(width == LIPS_LEDGER_HEIGHT && height == LIPS_LEDGER_WIDTH))
437             return_error(gs_error_rangecheck);
438     }
439 
440     /* Resolution Check */
441     if (xdpi != ydpi)
442         return_error(gs_error_rangecheck);
443     else {
444         if ((xdpi < LIPS_DPI_MIN || xdpi > LIPS4_DPI_MAX)
445             && xdpi != LIPS4_DPI_SUPERFINE) return_error(gs_error_rangecheck);
446     }
447 
448     return 0;
449 }
450 
451 static void
lips4v_set_cap(gx_device * dev,int x,int y)452 lips4v_set_cap(gx_device * dev, int x, int y)
453 {
454 
455     char cap[15];
456     stream *s = gdev_vector_stream((gx_device_vector *) dev);
457     gx_device_lips4v *const pdev = (gx_device_lips4v *) dev;
458     int dx = x - pdev->prev_x;
459     int dy = y - pdev->prev_y;
460 
461     if (dx > 0) {
462         sprintf(cap, "%c%da", LIPS_CSI, dx);
463         lputs(s, cap);
464     } else if (dx < 0) {
465         sprintf(cap, "%c%dj", LIPS_CSI, -dx);
466         lputs(s, cap);
467     }
468     if (dy > 0) {
469         sprintf(cap, "%c%dk", LIPS_CSI, dy);
470         lputs(s, cap);
471     } else if (dy < 0) {
472         sprintf(cap, "%c%de", LIPS_CSI, -dy);
473         lputs(s, cap);
474     }
475     pdev->prev_x = x;
476     pdev->prev_y = y;
477 }
478 
479 #define POINT 18
480 
481 /* Font Downloading Routine */
482 static int
lips4v_copy_text_char(gx_device * dev,const byte * data,int raster,gx_bitmap_id id,int x,int y,int w,int h)483 lips4v_copy_text_char(gx_device * dev, const byte * data,
484                       int raster, gx_bitmap_id id, int x, int y, int w, int h)
485 {
486     gx_device_lips4v *const pdev = (gx_device_lips4v *) dev;
487     stream *s = gdev_vector_stream((gx_device_vector *) dev);
488     uint width_bytes = (w + 7) >> 3;
489     uint size = width_bytes * h;
490     int i, j;
491     uint ccode;
492     char cset_sub[9], cset[64], cset_number[8], text_color[15];
493     int cell_length = (POINT * (int)dev->x_pixels_per_inch) / 72;
494     bool download = TRUE;
495 
496     if (w > cell_length || h > cell_length || !pdev->FontDL)
497         return -1;
498 
499     for (j = pdev->count - 1; j >= 0; j--) {
500         if (pdev->id_table[j] == id)
501             /* font is found */
502         {
503             download = FALSE;
504             ccode = j;
505             for (i = j; i < pdev->count - 1; i++) {
506                 pdev->id_cache[i] = pdev->id_cache[i + 1];
507             }
508             pdev->id_cache[pdev->count - 1] = id;
509             break;
510         }
511     }
512 
513     if (download) {
514         if (pdev->count > max_cached_chars - 1) {
515             gx_bitmap_id tmpid = pdev->id_cache[0];
516 
517             for (j = pdev->count - 1; j >= 0; j--) {
518                 if (pdev->id_table[j] == tmpid) {
519                     ccode = j;
520                     break;
521                 }
522             }
523             for (i = j; i < pdev->count - 1; i++) {
524                 pdev->id_cache[i] = pdev->id_cache[i + 1];
525             }
526             pdev->id_cache[pdev->count - 1] = tmpid;
527         } else {
528             ccode = pdev->count;
529             pdev->id_cache[pdev->count] = id;
530         }
531     }
532     if (pdev->TextMode == FALSE) {
533         /* Text mode */
534         lputs(s, "}p");
535         sput_lips_int(s, x);
536         sput_lips_int(s, y);
537         sputc(s, LIPS_IS2);
538         pdev->TextMode = TRUE;
539         pdev->prev_x = x;
540         pdev->prev_y = y;
541     } else
542         lips4v_set_cap(dev, x, y);
543 
544     if (download) {
545         if (ccode % 128 == 0 && ccode == pdev->count) {
546             /* ʸ�����å���Ͽ���̿�� */
547             sprintf(cset_sub, "%c%dx%c", LIPS_DCS, ccode / 128, LIPS_ST);
548             lputs(s, cset_sub);
549             /* ʸ�����å���Ͽ̿�� */
550             sprintf(cset,
551                     "%c%d;1;0;0;3840;8;400;100;0;0;200;%d;%d;0;0;;;;;%d.p",
552                     LIPS_CSI,
553                     size + 9, cell_length,	/* Cell Width */
554                     cell_length,	/* Cell Height */
555                     (int)dev->x_pixels_per_inch);
556             lputs(s, cset);
557         } else {
558             /* 1ʸ����Ͽ̿�� */
559             sprintf(cset,
560                     "%c%d;%d;8;%d.q", LIPS_CSI,
561                     size + 9, ccode / 128, (int)dev->x_pixels_per_inch);
562             lputs(s, cset);
563         }
564 
565         /* �桼��ʸ����Ͽ�ǡ��� �Υإå� */
566         sputc(s, ccode % 128);	/* charcter code */
567         put_int(s, w);
568         put_int(s, 0);
569         put_int(s, h);
570         put_int(s, 0);
571         for (i = h - 1; i >= 0; --i) {
572             put_bytes(s, data + i * raster, width_bytes);
573         }
574     }
575     /* ʸ�����åȡ����������ֹ�����̿��2 */
576     if (download) {
577         if (pdev->current_font != ccode / 128) {
578             sprintf(cset_number, "%c%d%%v", LIPS_CSI, ccode / 128);
579             lputs(s, cset_number);
580             pdev->current_font = ccode / 128;
581         }
582     } else {
583         if (pdev->current_font != ccode / 128) {
584             sprintf(cset_number, "%c%d%%v", LIPS_CSI, ccode / 128);
585             lputs(s, cset_number);
586             pdev->current_font = ccode / 128;
587         }
588     }
589 
590     /* ���顼 */
591     if (pdev->current_color != pdev->prev_color) {
592         if (pdev->color_info.depth == 8) {
593             sputc(s, LIPS_CSI);
594             lputs(s, "?10;2;");
595             sprintf(text_color, "%d",
596                     (int)(pdev->color_info.max_gray - pdev->current_color));
597         } else {
598             int r = (pdev->current_color >> 16) * 1000.0 / 255.0;
599             int g = ((pdev->current_color >> 8) & 0xff) * 1000.0 / 255.0;
600             int b = (pdev->current_color & 0xff) * 1000.0 / 255.0;
601 
602             sputc(s, LIPS_CSI);
603             lputs(s, "?10;;");
604             sprintf(text_color, "%d;%d;%d", r, g, b);
605         }
606         lputs(s, text_color);
607         lputs(s, "%p");
608         pdev->prev_color = pdev->current_color;
609     }
610     /* ����ʸ������̿�� */
611     if (ccode % 128 == 0x00 ||
612         (ccode % 128 >= 0x07 && ccode % 128 <= 0x0F) ||
613         ccode % 128 == 0x1B) {
614         sputc(s, LIPS_CSI);
615         lputs(s, "1.v");
616     }
617     sputc(s, ccode % 128);
618 
619     if (download) {
620         pdev->id_table[ccode] = id;
621         if (pdev->count < max_cached_chars - 1)
622             pdev->count++;
623     }
624     return 0;
625 }
626 
627 static void
reverse_buffer(byte * buf,int Len)628 reverse_buffer(byte * buf, int Len)
629 {
630     int i;
631 
632     for (i = 0; i < Len; i++)
633         *(buf + i) = ~*(buf + i);
634 }
635 
636 static void
lips4v_write_image_data(gx_device_vector * vdev,byte * buf,int tbyte,int reverse)637 lips4v_write_image_data(gx_device_vector * vdev, byte * buf, int tbyte,
638                         int reverse)
639 {
640     stream *s = gdev_vector_stream(vdev);
641     byte *cbuf = gs_alloc_bytes(vdev->memory, tbyte * 3 / 2,
642                                 "lips4v_write_image_data(cbuf)");
643     byte *cbuf_rle = gs_alloc_bytes(vdev->memory, tbyte * 3,
644                                     "lips4v_write_image_data(cbuf_rle)");
645     int Len, Len_rle;
646 
647     if (reverse)
648         reverse_buffer(buf, tbyte);
649 
650     Len = lips_packbits_encode(buf, cbuf, tbyte);
651     Len_rle = lips_rle_encode(buf, cbuf_rle, tbyte);
652 
653     if (Len > tbyte && Len_rle > tbyte) {
654         /* Not compress */
655         lputs(s, "0");
656         sput_lips_int(s, tbyte);
657         sputc(s, LIPS_IS2);
658 
659         put_bytes(s, buf, tbyte);
660     } else if (Len > Len_rle) {
661         /* Use RunLength encode */
662         lputs(s, ":");
663         sput_lips_int(s, Len_rle);
664         sputc(s, LIPS_IS2);
665 
666         put_bytes(s, cbuf_rle, Len_rle);
667     } else {
668         /* Use PackBits encode */
669         lputs(s, ";");
670         sput_lips_int(s, Len);
671         sputc(s, LIPS_IS2);
672 
673         put_bytes(s, cbuf, Len);
674     }
675 
676     gs_free_object(vdev->memory, cbuf, "lips4v_write_image_data(cbuf)");
677     gs_free_object(vdev->memory, cbuf_rle,
678                    "lips4v_write_image_data(cbuf_rle)");
679 }
680 
681 /* ---------------- Vector device implementation ---------------- */
682 
683 static int
lips4v_beginpage(gx_device_vector * vdev)684 lips4v_beginpage(gx_device_vector * vdev)
685 {				/*
686                                  * We can't use gdev_vector_stream here, because this may be called
687                                  * from there before in_page is set.
688                                  */
689     gx_device_lips4v *const pdev = (gx_device_lips4v *) vdev;
690     stream *s = vdev->strm;
691     int dpi = vdev->x_pixels_per_inch;
692     int width = pdev->MediaSize[0];
693     int height = pdev->MediaSize[1];
694     int paper_size, x0, y0;
695     char dpi_char[6], unit[14];
696     char page_header[8], l4vmono_page_header[7], l4vcolor_page_header[7];
697     char duplex_char[6], tumble_char[6], toner_d[26], toner_s[5],
698 
699         nup_char[10];
700     char username[6 + LIPS_USERNAME_MAX], feedmode[5], paper[16],
701 
702         faceup_char[256];
703     bool dup = pdev->Duplex;
704     int dupset = pdev->Duplex_set;
705     bool tum = pdev->Tumble;
706 
707     /* �٥������⡼�ɰܹ�̿�� CSI &} ���Ǥ��Ȥ�ȯ�Ԥ��� */
708 
709     if (pdev->first_page) {
710         if (pdev->pjl) {
711             lputs(s, l4v_file_header1);
712             if ((int)pdev->x_pixels_per_inch == 1200)
713                 lputs(s, "@PJL SET RESOLUTION = SUPERFINE\n");
714             else if ((int)pdev->x_pixels_per_inch == 600)
715                 lputs(s, "@PJL SET RESOLUTION = FINE\n");
716             else if ((int)pdev->x_pixels_per_inch == 300)
717                 lputs(s, "@PJL SET RESOLUTION = QUICK\n");
718             lputs(s, l4v_file_header2);
719             if (pdev->toner_density) {
720                 sprintf(toner_d, "@PJL SET TONER-DENSITY=%d\n",
721                         pdev->toner_density);
722                 lputs(s, toner_d);
723             }
724             if (pdev->toner_saving_set) {
725                 lputs(s, "@PJL SET TONER-SAVING=");
726                 if (pdev->toner_saving)
727                     sprintf(toner_s, "ON\n");
728                 else
729                     sprintf(toner_s, "OFF\n");
730                 lputs(s, toner_s);
731             }
732             lputs(s, l4v_file_header3);
733         }
734         lputs(s, l4v_file_header4);
735 
736         if (dpi > 9999)
737             return_error(gs_error_rangecheck);
738 
739         /* set reaolution (dpi) */
740         sprintf(dpi_char, "%d", dpi);
741         lputs(s, dpi_char);
742 
743         if (pdev->color_info.depth == 8)
744             lputs(s, l4vmono_file_header);
745         else
746             lputs(s, l4vcolor_file_header);
747 
748         /* username */
749         sprintf(username, "%c2y%s%c", LIPS_DCS, pdev->Username, LIPS_ST);
750         lputs(s, username);
751     }
752     if (strcmp(pdev->mediaType, "PlainPaper") == 0) {
753         sputc(s, LIPS_CSI);
754         lputs(s, "20\'t");
755     }
756     else if (strcmp(pdev->mediaType, "OHP") == 0 ||
757              strcmp(pdev->mediaType, "TransparencyFilm") == 0) {
758         sputc(s, LIPS_CSI);
759         lputs(s, "40\'t");	/* OHP mode (for LBP-2160) */
760     }
761     else if (strcmp(pdev->mediaType, "CardBoard") == 0) {
762         sputc(s, LIPS_CSI);
763         lputs(s, "30\'t");	/* CardBoard mode (for LBP-2160) */
764     }
765     else if (strcmp(pdev->mediaType, "GlossyFilm") == 0) {
766         sputc(s, LIPS_CSI);
767         lputs(s, "41\'t");	/* GlossyFilm mode (for LBP-2160) */
768     }
769 
770     /* ���⡼�� */
771     if (pdev->ManualFeed ||
772         (strcmp(pdev->mediaType, "PlainPaper") != 0
773          && strcmp(pdev->mediaType, LIPS_MEDIATYPE_DEFAULT) != 0)) {
774         /* Use ManualFeed */
775         if (pdev->prev_feed_mode != 10) {
776             sprintf(feedmode, "%c10q", LIPS_CSI);
777             lputs(s, feedmode);
778             pdev->prev_feed_mode = 10;
779         }
780     } else {
781         if (pdev->prev_feed_mode != pdev->cassetFeed) {
782             sprintf(feedmode, "%c%dq", LIPS_CSI, pdev->cassetFeed);
783             lputs(s, feedmode);
784             pdev->prev_feed_mode = pdev->cassetFeed;
785         }
786     }
787 
788     paper_size = lips_media_selection(width, height);
789 
790     /* �ѻ極���� */
791     if (pdev->prev_paper_size != paper_size) {
792         if (paper_size == USER_SIZE) {
793             /* modified by shige 06/27 2003
794             sprintf(paper, "%c80;%d;%dp", LIPS_CSI, width * 10, height * 10); */
795             /* modified by shige 11/09 2003
796             sprintf(paper, "%c80;%d;%dp", LIPS_CSI, height * 10, width * 10); */
797             sprintf(paper, "%c80;%d;%dp", LIPS_CSI,
798                     (height * 10 > LIPS_HEIGHT_MAX_720)?
799                     LIPS_HEIGHT_MAX_720 : (height * 10),
800                     (width * 10 > LIPS_WIDTH_MAX_720)?
801                     LIPS_WIDTH_MAX_720 : (width * 10));
802             lputs(s, paper);
803         } else if (paper_size == USER_SIZE + LANDSCAPE) {
804             /* modified by shige 06/27 2003
805             sprintf(paper, "%c81;%d;%dp", LIPS_CSI, height * 10, width * 10); */
806             /* modified by shige 11/09 2003
807             sprintf(paper, "%c81;%d;%dp", LIPS_CSI, width * 10, height * 10); */
808             sprintf(paper, "%c80;%d;%dp", LIPS_CSI,
809                     (width * 10 > LIPS_HEIGHT_MAX_720)?
810                     LIPS_HEIGHT_MAX_720 : (width * 10),
811                     (height * 10 > LIPS_WIDTH_MAX_720)?
812                     LIPS_WIDTH_MAX_720 : (height * 10));
813             lputs(s, paper);
814         } else {
815             sprintf(paper, "%c%dp", LIPS_CSI, paper_size);
816             lputs(s, paper);
817         }
818     } else if (paper_size == USER_SIZE) {
819         if (pdev->prev_paper_width != width ||
820             pdev->prev_paper_height != height)
821                 /* modified by shige 06/27 2003
822                 sprintf(paper, "%c80;%d;%dp", LIPS_CSI, width * 10, height * 10); */
823                 /* modified by shige 11/09 2003
824                 sprintf(paper, "%c80;%d;%dp", LIPS_CSI, height * 10, width * 10); */
825                 sprintf(paper, "%c80;%d;%dp", LIPS_CSI,
826                     (height * 10 > LIPS_HEIGHT_MAX_720)?
827                     LIPS_HEIGHT_MAX_720 : (height * 10),
828                     (width * 10 > LIPS_WIDTH_MAX_720)?
829                     LIPS_WIDTH_MAX_720 : (width * 10));
830         lputs(s, paper);
831     } else if (paper_size == USER_SIZE + LANDSCAPE) {
832         if (pdev->prev_paper_width != width ||
833             pdev->prev_paper_height != height)
834                 /* modified by shige 06/27 2003
835                 sprintf(paper, "%c81;%d;%dp", LIPS_CSI, height * 10, width * 10); */
836                 /* modified by shige 11/09 2003
837                 sprintf(paper, "%c81;%d;%dp", LIPS_CSI, width * 10, height * 10); */
838                 sprintf(paper, "%c80;%d;%dp", LIPS_CSI,
839                     (width * 10 > LIPS_HEIGHT_MAX_720)?
840                     LIPS_HEIGHT_MAX_720 : (width * 10),
841                     (height * 10 > LIPS_WIDTH_MAX_720)?
842                     LIPS_WIDTH_MAX_720 : (height * 10));
843         lputs(s, paper);
844     }
845     pdev->prev_paper_size = paper_size;
846     pdev->prev_paper_width = width;
847     pdev->prev_paper_height = height;
848 
849     if (pdev->faceup) {
850         sprintf(faceup_char, "%c11;12;12~", LIPS_CSI);
851         lputs(s, faceup_char);
852     }
853     /* N-up Printing Setting */
854     if (pdev->first_page) {
855         if (pdev->nup != 1) {
856             sprintf(nup_char, "%c%d1;;%do", LIPS_CSI, pdev->nup, paper_size);
857             lputs(s, nup_char);
858         }
859     }
860     /* Duplex Setting */
861     if (dupset && dup) {
862         if (pdev->prev_duplex_mode == 0 || pdev->prev_duplex_mode == 1) {
863             sprintf(duplex_char, "%c2;#x", LIPS_CSI);	/* duplex */
864             lputs(s, duplex_char);
865             if (!tum) {
866                 /* long edge binding */
867                 if (pdev->prev_duplex_mode != 2) {
868                     sprintf(tumble_char, "%c0;#w", LIPS_CSI);
869                     lputs(s, tumble_char);
870                 }
871                 pdev->prev_duplex_mode = 2;
872             } else {
873                 /* short edge binding */
874                 if (pdev->prev_duplex_mode != 3) {
875                     sprintf(tumble_char, "%c2;#w", LIPS_CSI);
876                     lputs(s, tumble_char);
877                 }
878                 pdev->prev_duplex_mode = 3;
879             }
880         }
881     } else if (dupset && !dup) {
882         if (pdev->prev_duplex_mode != 1) {
883             sprintf(duplex_char, "%c0;#x", LIPS_CSI);	/* simplex */
884             lputs(s, duplex_char);
885         }
886         pdev->prev_duplex_mode = 1;
887     }
888     sputc(s, LIPS_CSI);
889     lputs(s, "?1;4;5;6;14l");
890     sputc(s, LIPS_CSI);
891     lputs(s, "?2;3;h");
892 
893     /* size unit (dpi) */
894     sputc(s, LIPS_CSI);
895     lputs(s, "11h");
896     sprintf(unit, "%c?7;%d I", LIPS_CSI, (int)pdev->x_pixels_per_inch);
897     lputs(s, unit);
898     sprintf(page_header, "%c[0&}#%c", LIPS_ESC, LIPS_IS2);
899     lputs(s, page_header);	/* vector mode */
900 
901     lputs(s, "!0");		/* size unit (dpi) */
902     sput_lips_int(s, dpi);
903     lputs(s, "1");
904     sputc(s, LIPS_IS2);
905 
906     if (pdev->color_info.depth == 8) {
907         sprintf(l4vmono_page_header, "!13%c$%c", LIPS_IS2, LIPS_IS2);
908         lputs(s, l4vmono_page_header);
909     } else {
910         sprintf(l4vcolor_page_header, "!11%c$%c", LIPS_IS2, LIPS_IS2);
911         lputs(s, l4vcolor_page_header);
912     }
913 
914     lputs(s, "(00");
915     sput_lips_int(s,
916                   ((width - dev_l_margin(vdev) - dev_r_margin(vdev)) * dpi) /
917                   72);
918     sput_lips_int(s,
919                   ((height - dev_b_margin(vdev) - dev_t_margin(vdev)) * dpi) /
920                   72);
921     sputc(s, LIPS_IS2);
922 
923     /* ������ư̿�� */
924     x0 = (dev_l_margin(vdev) - 5. / MMETER_PER_INCH) * dpi;
925     y0 = (dev_b_margin(vdev) - 5. / MMETER_PER_INCH) * dpi;
926 
927     if (x0 != 0 && y0 != 0) {
928         lputs(s, "}\"");
929         sput_lips_int(s, x0);
930         sput_lips_int(s, y0);
931         sputc(s, LIPS_IS2);
932     }
933     lputs(s, "I00");
934     sputc(s, LIPS_IS2);
935     lputs(s, "}F2");
936     sputc(s, LIPS_IS2);
937     lputs(s, "}H1");
938     sputc(s, LIPS_IS2);
939     lputs(s, "*0");
940     sputc(s, LIPS_IS2);
941 
942     pdev->MaskState = 1;	/* �����: Ʃ�� */
943     pdev->linecap = 0;
944     lputs(s, "}M");
945     sput_lips_int(s, 3277);	/* 11 degree : 16383 * 2 / 10 */
946     sputc(s, LIPS_IS2);
947     lputs(s, "}I1");		/* non-zero winding rule is default */
948     sputc(s,  LIPS_IS2);
949 
950     return 0;
951 }
952 
953 static int
lips4v_setlinewidth(gx_device_vector * vdev,floatp width)954 lips4v_setlinewidth(gx_device_vector * vdev, floatp width)
955 {
956     stream *s = gdev_vector_stream(vdev);
957     gx_device_lips4v *const pdev = (gx_device_lips4v *) vdev;
958 
959 #if 0
960     /* Scale ��ݤ��Ƥ���Τ�, Ghostscript 5.10/5.50 �ΥХ��Τ��� */
961     floatp xscale, yscale;
962 
963     xscale = fabs(igs->ctm.xx);
964     yscale = fabs(igs->ctm.xy);
965 
966     if (xscale == 0 || yscale > xscale)	/* if portrait */
967         width = ceil(width * yscale);
968     else
969         width = ceil(width * xscale);
970 #endif
971 
972     if (pdev->TextMode) {
973         sputc(s, LIPS_CSI);
974         lputs(s, "&}");
975         pdev->TextMode = FALSE;
976     }
977     if (width < 1)
978         width = 1;
979 
980     lputs(s, "F1");
981     sput_lips_int(s, width);
982     sputc(s, LIPS_IS2);
983 
984     return 0;
985 }
986 
987 static int
lips4v_setlinecap(gx_device_vector * vdev,gs_line_cap cap)988 lips4v_setlinecap(gx_device_vector * vdev, gs_line_cap cap)
989 {
990     stream *s = gdev_vector_stream(vdev);
991     gx_device_lips4v *const pdev = (gx_device_lips4v *) vdev;
992     char c[6];
993     int line_cap = 0;
994 
995     if (pdev->TextMode) {
996         sputc(s, LIPS_CSI);
997         lputs(s, "&}");
998         pdev->TextMode = FALSE;
999     }
1000     switch (cap) {
1001         case 0:
1002         case 3:
1003         line_cap = 0;		/* butt */
1004         break;
1005         case 1:
1006         line_cap = 1;		/* round */
1007         break;
1008         case 2:
1009         line_cap = 2;		/* square */
1010         break;
1011     }
1012     /* ��ü��������̿�� */
1013     sprintf(c, "}E%d%c", line_cap, LIPS_IS2);
1014     lputs(s, c);
1015 
1016     pdev->linecap = cap;
1017 
1018     return 0;
1019 }
1020 
1021 static int
lips4v_setlinejoin(gx_device_vector * vdev,gs_line_join join)1022 lips4v_setlinejoin(gx_device_vector * vdev, gs_line_join join)
1023 {
1024     stream *s = gdev_vector_stream(vdev);
1025     gx_device_lips4v *const pdev = (gx_device_lips4v *) vdev;
1026 
1027 /* ����³����̿�� */
1028     char c[5];
1029     int lips_join = 0;
1030 
1031     if (pdev->TextMode) {
1032         sputc(s, LIPS_CSI);
1033         lputs(s, "&}");
1034         pdev->TextMode = FALSE;
1035     }
1036 
1037     switch (join) {
1038         case 0:
1039         lips_join = 2;		/* miter */
1040         break;
1041         case 1:
1042         lips_join = 1;		/* round */
1043         break;
1044         case 2:
1045         lips_join = 3;		/* bevel */
1046         break;
1047         case 3:
1048         case 4:
1049         lips_join = 0;		/* none */
1050         break;
1051     }
1052 
1053     sprintf(c, "}F%d%c", lips_join, LIPS_IS2);
1054     lputs(s, c);
1055 
1056     return 0;
1057 }
1058 
1059 static int
lips4v_setmiterlimit(gx_device_vector * vdev,floatp limit)1060 lips4v_setmiterlimit(gx_device_vector * vdev, floatp limit)
1061 {
1062     stream *s = gdev_vector_stream(vdev);
1063     gx_device_lips4v *const pdev = (gx_device_lips4v *) vdev;
1064     floatp lips_miterlimit;
1065 
1066     if (pdev->TextMode) {
1067         sputc(s, LIPS_CSI);
1068         lputs(s, "&}");
1069         pdev->TextMode = FALSE;
1070     }
1071     lips_miterlimit = (16383.0 * 2.0) / limit;
1072 
1073     lputs(s, "}M");
1074     sput_lips_int(s, lips_miterlimit);
1075     sputc(s, LIPS_IS2);
1076 
1077     return 0;
1078 }
1079 
1080 #if GS_VERSION_MAJOR >= 8
1081 static int
lips4v_setfillcolor(gx_device_vector * vdev,const gs_imager_state * pis,const gx_drawing_color * pdc)1082 lips4v_setfillcolor(gx_device_vector * vdev, const gs_imager_state * pis, const gx_drawing_color * pdc)
1083 #else
1084 static int
1085 lips4v_setfillcolor(gx_device_vector * vdev, const gx_drawing_color * pdc)
1086 #endif
1087 {
1088 
1089     if (!gx_dc_is_pure(pdc))
1090         return_error(gs_error_rangecheck);
1091     {
1092         stream *s = gdev_vector_stream(vdev);
1093         gx_device_lips4v *const pdev = (gx_device_lips4v *) vdev;
1094         gx_color_index color = gx_dc_pure_color(pdc);
1095         int drawing_color;
1096         float r, g, b;
1097 
1098         if (vdev->color_info.depth == 8) {
1099             drawing_color = vdev->color_info.max_gray - color;
1100         } else {
1101             r = (color >> 16) * 1000.0 / 255.0;
1102             g = ((color >> 8) & 0xff) * 1000.0 / 255.0;
1103             b = (color & 0xff) * 1000.0 / 255.0;
1104         }
1105 
1106         if (pdev->TextMode) {
1107             sputc(s, LIPS_CSI);
1108             lputs(s, "&}");
1109             pdev->TextMode = FALSE;
1110         }
1111         pdev->current_color = color;
1112 
1113         if (color == gx_no_color_index) {
1114             lputs(s, "I0");
1115             sputc(s, LIPS_IS2);
1116         } else {
1117             lputs(s, "I1");
1118             sputc(s, LIPS_IS2);
1119         }
1120 
1121         /* �ɤ�Ĥ֤����顼����̿�� */
1122         /* J {color} IS2 */
1123         lputs(s, "J");
1124         if (vdev->color_info.depth == 8) {
1125             sput_lips_int(s, drawing_color);
1126         } else {
1127             sput_lips_int(s, r);
1128             sput_lips_int(s, g);
1129             sput_lips_int(s, b);
1130         }
1131         sputc(s, LIPS_IS2);
1132 
1133         /* ñ�����᡼�������顼����̿�� */
1134         /* }T {color} IS2 */
1135         lputs(s, "}T");
1136         if (vdev->color_info.depth == 8) {
1137             sput_lips_int(s, drawing_color);
1138         } else {
1139             sput_lips_int(s, r);
1140             sput_lips_int(s, g);
1141             sput_lips_int(s, b);
1142         }
1143         sputc(s, LIPS_IS2);
1144     }
1145     return 0;
1146 }
1147 
1148 #if GS_VERSION_MAJOR >= 8
1149 static int
lips4v_setstrokecolor(gx_device_vector * vdev,const gs_imager_state * pis,const gx_drawing_color * pdc)1150 lips4v_setstrokecolor(gx_device_vector * vdev, const gs_imager_state * pis, const gx_drawing_color * pdc)
1151 #else
1152 static int
1153 lips4v_setstrokecolor(gx_device_vector * vdev, const gx_drawing_color * pdc)
1154 #endif
1155 {
1156     if (!gx_dc_is_pure(pdc))
1157         return_error(gs_error_rangecheck);
1158     {
1159         stream *s = gdev_vector_stream(vdev);
1160         gx_device_lips4v *const pdev = (gx_device_lips4v *) vdev;
1161         gx_color_index color = gx_dc_pure_color(pdc);
1162         float r, g, b;
1163 
1164         if (vdev->color_info.depth == 24) {
1165             r = (color >> 16) * 1000 / 255.0;
1166             g = ((color >> 8) & 0xff) * 1000 / 255.0;
1167             b = (color & 0xff) * 1000 / 255.0;
1168         }
1169 
1170         if (pdev->TextMode) {
1171             sputc(s, LIPS_CSI);
1172             lputs(s, "&}");
1173             pdev->TextMode = FALSE;
1174         }
1175         /* �饤���顼����̿�� */
1176         /* G {color} IS2 */
1177         lputs(s, "G");
1178         if (vdev->color_info.depth == 8) {
1179             sput_lips_int(s, vdev->color_info.max_gray - color);
1180         } else {
1181             sput_lips_int(s, r);
1182             sput_lips_int(s, g);
1183             sput_lips_int(s, b);
1184         }
1185         sputc(s, LIPS_IS2);
1186     }
1187     return 0;
1188 }
1189 
1190 /* �������̿�� */
1191 static int
lips4v_setdash(gx_device_vector * vdev,const float * pattern,uint count,floatp offset)1192 lips4v_setdash(gx_device_vector * vdev, const float *pattern, uint count,
1193                floatp offset)
1194 {
1195     stream *s = gdev_vector_stream(vdev);
1196     gx_device_lips4v *const pdev = (gx_device_lips4v *) vdev;
1197     int i;
1198     float scale, xscale, yscale;
1199 
1200     if (pdev->TextMode) {
1201         sputc(s, LIPS_CSI);
1202         lputs(s, "&}");
1203         pdev->TextMode = FALSE;
1204     }
1205 #if 0
1206     /* Scale ��ݤ��Ƥ���Τ�, Ghostscript 5.10/5.50 �ΥХ��Τ��� */
1207     xscale = fabs(igs->ctm.xx);
1208     yscale = fabs(igs->ctm.xy);
1209 
1210     if (xscale == 0)		/* if portrait */
1211         scale = yscale;
1212     else
1213         scale = xscale;
1214 #endif
1215 
1216     if (count == 0) {
1217         lputs(s, "E10");
1218         sputc(s, LIPS_IS2);
1219     } else {
1220         lputs(s, "}d");
1221         sputc(s, 0x2c);
1222         lputs(s, "1");
1223 #if 0
1224         sput_lips_int(s, offset * scale / vdev->x_pixels_per_inch + 0.5);
1225 #else
1226         sput_lips_int(s, offset);
1227 #endif
1228         for (i = 0; i < count; ++i) {
1229             if (pdev->linecap == 1 && count == 2 && pattern[0] == 0) {
1230                 if (i == 0) {
1231                     sput_lips_int(s, 1);
1232                 } else {
1233 #if 0
1234                     sput_lips_int(s,
1235                                   pattern[i] * scale /
1236                                   vdev->x_pixels_per_inch - 0.5);
1237 #else
1238                     sput_lips_int(s, pattern[i] - 1);
1239 #endif
1240                 }
1241             } else {
1242 #if 0
1243                 sput_lips_int(s,
1244                               pattern[i] * scale / vdev->x_pixels_per_inch +
1245                               0.5);
1246 #else
1247                 sput_lips_int(s, pattern[i]);
1248 #endif
1249             }
1250         }
1251         sputc(s, LIPS_IS2);
1252         lputs(s, "E1");
1253         sputc(s, 0x2c);
1254         lputs(s, "0");
1255         sputc(s, LIPS_IS2);
1256     }
1257 
1258     return 0;
1259 }
1260 
1261 /* �ѥ�ʿ���ٻ��� */
1262 static int
lips4v_setflat(gx_device_vector * vdev,floatp flatness)1263 lips4v_setflat(gx_device_vector * vdev, floatp flatness)
1264 {
1265     stream *s = gdev_vector_stream(vdev);
1266     gx_device_lips4v *const pdev = (gx_device_lips4v *) vdev;
1267 
1268     if (pdev->TextMode) {
1269         sputc(s, LIPS_CSI);
1270         lputs(s, "&}");
1271         pdev->TextMode = FALSE;
1272     }
1273     lputs(s, "Pf");
1274     sput_lips_int(s, flatness);
1275     sputc(s, LIPS_IS2);
1276 
1277     return 0;
1278 }
1279 
1280 static int
lips4v_setlogop(gx_device_vector * vdev,gs_logical_operation_t lop,gs_logical_operation_t diff)1281 lips4v_setlogop(gx_device_vector * vdev, gs_logical_operation_t lop,
1282                 gs_logical_operation_t diff)
1283 {
1284 /****** SHOULD AT LEAST DETECT SET-0 & SET-1 ******/
1285     return 0;
1286 }
1287 
1288 #if GS_VERSION_MAJOR >= 8
1289 /*--- added for Ghostscritp 8.15 ---*/
1290 static int
lips4v_can_handle_hl_color(gx_device_vector * vdev,const gs_imager_state * pis1,const gx_drawing_color * pdc)1291 lips4v_can_handle_hl_color(gx_device_vector * vdev, const gs_imager_state * pis1,
1292               const gx_drawing_color * pdc)
1293 {
1294     return false; /* High level color is not implemented yet. */
1295 }
1296 #endif
1297 
1298 static int
lips4v_beginpath(gx_device_vector * vdev,gx_path_type_t type)1299 lips4v_beginpath(gx_device_vector * vdev, gx_path_type_t type)
1300 {
1301     stream *s = gdev_vector_stream(vdev);
1302     gx_device_lips4v *const pdev = (gx_device_lips4v *) vdev;
1303 
1304     if (pdev->TextMode) {
1305         sputc(s, LIPS_CSI);
1306         lputs(s, "&}");
1307         pdev->TextMode = FALSE;
1308     }
1309     /* �ѥ����۳���̿�� */
1310     if (type & gx_path_type_clip) {
1311         lputs(s, "P(10");
1312         sputc(s, LIPS_IS2);
1313     } else
1314         lputs(s, "P(00");
1315         sputc(s, LIPS_IS2);
1316 
1317     return 0;
1318 }
1319 
1320 static int
lips4v_moveto(gx_device_vector * vdev,floatp x0,floatp y0,floatp x,floatp y,gx_path_type_t type)1321 lips4v_moveto(gx_device_vector * vdev, floatp x0, floatp y0, floatp x,
1322               floatp y, gx_path_type_t type)
1323 {
1324     stream *s = gdev_vector_stream(vdev);
1325 
1326     /* ���֥ѥ�����̿�� p1 */
1327     lputs(s, "p10");
1328     sput_lips_int(s, x);
1329     sput_lips_int(s, y);
1330     sputc(s, LIPS_IS2);
1331 
1332     return 0;
1333 }
1334 
1335 static int
lips4v_lineto(gx_device_vector * vdev,floatp x0,floatp y0,floatp x,floatp y,gx_path_type_t type)1336 lips4v_lineto(gx_device_vector * vdev, floatp x0, floatp y0, floatp x,
1337               floatp y, gx_path_type_t type)
1338 {
1339     stream *s = gdev_vector_stream(vdev);
1340     gx_device_lips4v *const pdev = (gx_device_lips4v *) vdev;
1341 
1342     /* if round cap */
1343     if (pdev->linecap == 1) {
1344         if ((x0 == x) && (y0 == y))
1345             x += 1;
1346     }
1347 
1348     /* �ѥ����ݥ�饤��̿�� */
1349     lputs(s, "p402");
1350     sput_lips_int(s, x);
1351     sput_lips_int(s, y);
1352     sputc(s, LIPS_IS2);
1353 
1354     return 0;
1355 }
1356 
1357 static int
lips4v_curveto(gx_device_vector * vdev,floatp x0,floatp y0,floatp x1,floatp y1,floatp x2,floatp y2,floatp x3,floatp y3,gx_path_type_t type)1358 lips4v_curveto(gx_device_vector * vdev, floatp x0, floatp y0,
1359                floatp x1, floatp y1, floatp x2, floatp y2, floatp x3,
1360                floatp y3, gx_path_type_t type)
1361 {
1362     stream *s = gdev_vector_stream(vdev);
1363 
1364     /* �ѥ����ݥ�饤��̿�� */
1365     lputs(s, "p404");
1366     sput_lips_int(s, x1);
1367     sput_lips_int(s, y1);
1368     sput_lips_int(s, x2);
1369     sput_lips_int(s, y2);
1370     sput_lips_int(s, x3);
1371     sput_lips_int(s, y3);
1372     sputc(s, LIPS_IS2);
1373 
1374     return 0;
1375 }
1376 
1377 static int
lips4v_closepath(gx_device_vector * vdev,floatp x,floatp y,floatp x_start,floatp y_start,gx_path_type_t type)1378 lips4v_closepath(gx_device_vector * vdev, floatp x, floatp y,
1379                  floatp x_start, floatp y_start, gx_path_type_t type)
1380 {
1381     stream *s = gdev_vector_stream(vdev);
1382 
1383     lputs(s, "p0");
1384     sputc(s, LIPS_IS2);
1385     return 0;
1386 }
1387 
1388 static int
lips4v_endpath(gx_device_vector * vdev,gx_path_type_t type)1389 lips4v_endpath(gx_device_vector * vdev, gx_path_type_t type)
1390 {
1391     stream *s = gdev_vector_stream(vdev);
1392 
1393     lputs(s, "P)");
1394     sputc(s, LIPS_IS2);
1395     if (type & gx_path_type_rule) {
1396         if (type & gx_path_type_winding_number) {
1397             lputs(s, "}I1");
1398             sputc(s, LIPS_IS2);
1399         } else {
1400             lputs(s, "}I0");
1401             sputc(s, LIPS_IS2);
1402         }
1403     }
1404     if (type & gx_path_type_fill) {
1405         if (type & gx_path_type_stroke) {
1406             lputs(s, "P&00");
1407             sputc(s, LIPS_IS2);
1408         } else {
1409             lputs(s, "PF00");
1410             sputc(s, LIPS_IS2);
1411         }
1412     }
1413     if (type & gx_path_type_stroke) {
1414         lputs(s, "PS00");
1415         sputc(s, LIPS_IS2);
1416     }
1417     if (type & gx_path_type_clip) {
1418         lputs(s, "PC10");
1419         sputc(s, LIPS_IS2);
1420     }
1421     return 0;
1422 }
1423 
1424 /* ---------------- Driver procedures ---------------- */
1425 
1426 /* ------ Open/close/page ------ */
1427 
1428 /* Open the device. */
1429 static int
lips4v_open(gx_device * dev)1430 lips4v_open(gx_device * dev)
1431 {
1432     gx_device_vector *const vdev = (gx_device_vector *) dev;
1433     gx_device_lips4v *const pdev = (gx_device_lips4v *) dev;
1434 
1435     int code;
1436 
1437     code = lips4v_range_check(dev);
1438     if (code < 0)
1439         return code;
1440 
1441     vdev->v_memory = dev->memory;
1442 /****** WRONG ******/
1443     vdev->vec_procs = &lips4v_vector_procs;
1444 
1445 #if GS_VERSION_MAJOR >= 8
1446       code = gdev_vector_open_file_options(vdev, 512,
1447                         (VECTOR_OPEN_FILE_SEQUENTIAL|VECTOR_OPEN_FILE_BBOX));
1448 #else
1449     code = gdev_vector_open_file_bbox(vdev, 512, true);
1450 #endif
1451     if (code < 0)
1452         return code;
1453 
1454 #if GS_VERSION_MAJOR >= 8
1455         if (pdev->bbox_device != NULL) {
1456                 if (pdev->bbox_device->memory == NULL)
1457                         pdev->bbox_device->memory = gs_memory_stable(dev->memory);
1458         }
1459 #endif
1460 
1461     gdev_vector_init(vdev);
1462     pdev->first_page = true;
1463 
1464     return 0;
1465 }
1466 
1467 /* Wrap up ("output") a page. */
1468 static int
lips4v_output_page(gx_device * dev,int num_copies,int flush)1469 lips4v_output_page(gx_device * dev, int num_copies, int flush)
1470 {
1471     gx_device_vector *const vdev = (gx_device_vector *) dev;
1472     gx_device_lips4v *const pdev = (gx_device_lips4v *) dev;
1473     stream *s = gdev_vector_stream(vdev);
1474     char str[6];
1475 
1476     if (pdev->TextMode) {
1477         sputc(s, LIPS_CSI);
1478         lputs(s, "&}");
1479         pdev->TextMode = FALSE;
1480     }
1481     lputs(s, "%");
1482     sputc(s, LIPS_IS2);
1483     lputs(s, "}p");
1484     sputc(s, LIPS_IS2);
1485 
1486     if (num_copies > 255)
1487         num_copies = 255;
1488     if (pdev->prev_num_copies != num_copies) {
1489         sprintf(str, "%c%dv", LIPS_CSI, num_copies);
1490         lputs(s, str);
1491         pdev->prev_num_copies = num_copies;
1492     }
1493     sputc(s, LIPS_FF);
1494     sflush(s);
1495     vdev->in_page = false;
1496     pdev->first_page = false;
1497     gdev_vector_reset(vdev);
1498     return 0;
1499 }
1500 
1501 static int
lips4v_close(gx_device * dev)1502 lips4v_close(gx_device * dev)
1503 {
1504     gx_device_vector *const vdev = (gx_device_vector *) dev;
1505     gx_device_lips4v *const pdev = (gx_device_lips4v *) dev;
1506     FILE *f = vdev->file;
1507 
1508     fprintf(f, "%c0J%c", LIPS_DCS, LIPS_ST);
1509     if (pdev->pjl) {
1510         fprintf(f, "%c%%-12345X@PJL SET LPARM : LIPS SW2 = OFF\n", LIPS_ESC);
1511         fprintf(f,
1512                 "%c%%-12345X%c%%-12345X@PJL EOJ\n"
1513                 "%c%%-12345X", LIPS_ESC, LIPS_ESC, LIPS_ESC);
1514     }
1515     gdev_vector_close_file(vdev);
1516 
1517     return 0;
1518 }
1519 
1520 /* Close the device. */
1521 /* Note that if this is being called as a result of finalization, */
1522 /* the stream may no longer exist; but the file will still be open. */
1523 
1524 /* ---------------- Get/put parameters ---------------- */
1525 
1526 /* Get parameters. */
1527 static int
lips4v_get_params(gx_device * dev,gs_param_list * plist)1528 lips4v_get_params(gx_device * dev, gs_param_list * plist)
1529 {
1530     gx_device_lips4v *const pdev = (gx_device_lips4v *) dev;
1531     int code = gdev_vector_get_params(dev, plist);
1532     int ncode;
1533     gs_param_string usern;
1534     gs_param_string pmedia;
1535 
1536     if (code < 0)
1537         return code;
1538 
1539     if ((ncode = param_write_bool(plist, LIPS_OPTION_MANUALFEED,
1540                                   &pdev->ManualFeed)) < 0)
1541         code = ncode;
1542 
1543     if ((ncode = param_write_int(plist, LIPS_OPTION_CASSETFEED,
1544                                  &pdev->cassetFeed)) < 0)
1545         code = ncode;
1546 
1547     if ((ncode = param_write_bool(plist, LIPS_OPTION_DUPLEX_TUMBLE,
1548                                   &pdev->Tumble)) < 0)
1549         code = ncode;
1550 
1551     if ((ncode = param_write_int(plist, LIPS_OPTION_NUP, &pdev->nup)) < 0)
1552         code = ncode;
1553 
1554     if ((ncode = param_write_bool(plist, LIPS_OPTION_PJL, &pdev->pjl)) < 0)
1555         code = ncode;
1556 
1557     if ((ncode = param_write_int(plist, LIPS_OPTION_TONERDENSITY,
1558                                  &pdev->toner_density)) < 0)
1559         code = ncode;
1560 
1561     if (pdev->toner_saving_set >= 0 &&
1562         (code = (pdev->toner_saving_set ?
1563                  param_write_bool(plist, LIPS_OPTION_TONERSAVING,
1564                                   &pdev->
1565                                   toner_saving) : param_write_null(plist,
1566                                                                    LIPS_OPTION_TONERSAVING)))
1567         < 0)
1568         code = ncode;
1569 
1570     if (pdev->Duplex_set >= 0 &&
1571         (ncode = (pdev->Duplex_set ?
1572                   param_write_bool(plist, "Duplex", &pdev->Duplex) :
1573                   param_write_null(plist, "Duplex"))) < 0)
1574         code = ncode;
1575 
1576     if ((ncode = param_write_bool(plist, LIPS_OPTION_FONTDOWNLOAD,
1577                                   &pdev->FontDL)) < 0)
1578         code = ncode;
1579 
1580     if ((ncode = param_write_bool(plist, LIPS_OPTION_FACEUP,
1581                                   &pdev->faceup)) < 0) code = ncode;
1582 
1583     pmedia.data = (const byte *)pdev->mediaType,
1584         pmedia.size = strlen(pdev->mediaType), pmedia.persistent = false;
1585 
1586     if ((ncode = param_write_string(plist, LIPS_OPTION_MEDIATYPE,
1587                                     &pmedia)) < 0) code = ncode;
1588 
1589     if (code < 0)
1590         return code;
1591 
1592     usern.data = (const byte *)pdev->Username,
1593         usern.size = strlen(pdev->Username), usern.persistent = false;
1594 
1595     return param_write_string(plist, LIPS_OPTION_USER_NAME, &usern);
1596 }
1597 
1598 /* Put parameters. */
1599 static int
lips4v_put_params(gx_device * dev,gs_param_list * plist)1600 lips4v_put_params(gx_device * dev, gs_param_list * plist)
1601 {
1602     gx_device_lips4v *const pdev = (gx_device_lips4v *) dev;
1603     int ecode = 0;
1604     int code;
1605     gs_param_name param_name;
1606     gs_param_string pmedia;
1607     bool mf = pdev->ManualFeed;
1608     int cass = pdev->cassetFeed;
1609     gs_param_string usern;
1610     bool tum = pdev->Tumble;
1611     int nup = pdev->nup;
1612     bool pjl = pdev->pjl;
1613     int toner_density = pdev->toner_density;
1614     bool toner_saving = pdev->toner_saving;
1615     bool toner_saving_set = pdev->toner_saving_set;
1616     bool fontdl = pdev->FontDL;
1617     bool faceup = pdev->faceup;
1618     bool duplex;
1619     int duplex_set = -1;
1620     int old_bpp = dev->color_info.depth;
1621     int bpp = 0;
1622 
1623     if ((code = param_read_bool(plist,
1624                                 (param_name = LIPS_OPTION_MANUALFEED),
1625                                 &mf)) < 0) {
1626         param_signal_error(plist, param_name, ecode = code);
1627     }
1628     switch (code = param_read_int(plist,
1629                                   (param_name = LIPS_OPTION_CASSETFEED),
1630                                   &cass)) {
1631         case 0:
1632         if (cass < -1 || cass > 17 || (cass > 3 && cass < 10))
1633             ecode = gs_error_limitcheck;
1634         else
1635             break;
1636         goto casse;
1637         default:
1638         ecode = code;
1639       casse:param_signal_error(plist, param_name, ecode);
1640         case 1:
1641         break;
1642     }
1643 
1644     switch (code = param_read_string(plist,
1645                                      (param_name = LIPS_OPTION_MEDIATYPE),
1646                                      &pmedia)) {
1647         case 0:
1648         if (pmedia.size > LIPS_MEDIACHAR_MAX) {
1649             ecode = gs_error_limitcheck;
1650             goto pmediae;
1651         } else {   /* Check the validity of ``MediaType'' characters */
1652             if (strcmp(pmedia.data, "PlainPaper") != 0 &&
1653                 strcmp(pmedia.data, "OHP") != 0 &&
1654                 strcmp(pmedia.data, "TransparencyFilm") != 0 &&	/* same as OHP */
1655                 strcmp(pmedia.data, "GlossyFilm") != 0 &&
1656                 strcmp(pmedia.data, "CardBoard") != 0) {
1657                 ecode = gs_error_rangecheck;
1658                 goto pmediae;
1659             }
1660         }
1661         break;
1662         default:
1663         ecode = code;
1664       pmediae:param_signal_error(plist, param_name, ecode);
1665         case 1:
1666         pmedia.data = 0;
1667         break;
1668     }
1669 
1670     switch (code = param_read_string(plist,
1671                                      (param_name = LIPS_OPTION_USER_NAME),
1672                                      &usern)) {
1673         case 0:
1674         if (usern.size > LIPS_USERNAME_MAX) {
1675             ecode = gs_error_limitcheck;
1676             goto userne;
1677         } else {    /* Check the validity of ``User Name'' characters */
1678             int i;
1679 
1680             for (i = 0; i < usern.size; i++)
1681                 if (usern.data[i] < 0x20 || usern.data[i] > 0x7e
1682                     /*
1683                        && usern.data[i] < 0xa0) ||
1684                        usern.data[i] > 0xfe
1685                      */
1686                     ) {
1687                     ecode = gs_error_rangecheck;
1688                     goto userne;
1689                 }
1690         }
1691         break;
1692         default:
1693         ecode = code;
1694       userne:param_signal_error(plist, param_name, ecode);
1695         case 1:
1696         usern.data = 0;
1697         break;
1698     }
1699 
1700     if ((code = param_read_bool(plist,
1701                                 (param_name = LIPS_OPTION_DUPLEX_TUMBLE),
1702                                 &tum)) < 0)
1703         param_signal_error(plist, param_name, ecode = code);
1704 
1705     switch (code = param_read_int(plist,
1706                                   (param_name = LIPS_OPTION_NUP), &nup)) {
1707         case 0:
1708         if (nup != 1 && nup != 2 && nup != 4)
1709             ecode = gs_error_rangecheck;
1710         else
1711             break;
1712         goto nupe;
1713         default:
1714         ecode = code;
1715       nupe:param_signal_error(plist, param_name, ecode);
1716         case 1:
1717         break;
1718     }
1719 
1720     if ((code = param_read_bool(plist,
1721                                 (param_name = LIPS_OPTION_PJL), &pjl)) < 0)
1722         param_signal_error(plist, param_name, ecode = code);
1723 
1724     switch (code = param_read_int(plist,
1725                                   (param_name = LIPS_OPTION_TONERDENSITY),
1726                                   &toner_density)) {
1727         case 0:
1728         if (toner_density < 0 || toner_density > 8)
1729             ecode = gs_error_rangecheck;
1730         else
1731             break;
1732         goto tden;
1733         default:
1734         ecode = code;
1735       tden:param_signal_error(plist, param_name, ecode);
1736         case 1:
1737         break;
1738     }
1739 
1740     if (pdev->toner_saving_set >= 0)
1741         switch (code =
1742                 param_read_bool(plist, (param_name = LIPS_OPTION_TONERSAVING),
1743                                 &toner_saving)) {
1744             case 0:
1745             toner_saving_set = 1;
1746             break;
1747             default:
1748             if ((code = param_read_null(plist, param_name)) == 0) {
1749                 toner_saving_set = 0;
1750                 break;
1751             }
1752             ecode = code;
1753             param_signal_error(plist, param_name, ecode);
1754             case 1:
1755             break;
1756         }
1757 
1758     if (pdev->Duplex_set >= 0)	/* i.e., Duplex is supported */
1759         switch (code = param_read_bool(plist, (param_name = "Duplex"),
1760                                        &duplex)) {
1761             case 0:
1762             duplex_set = 1;
1763             break;
1764             default:
1765             if ((code = param_read_null(plist, param_name)) == 0) {
1766                 duplex_set = 0;
1767                 break;
1768             }
1769             ecode = code;
1770             param_signal_error(plist, param_name, ecode);
1771             case 1:
1772             break;
1773         }
1774     if ((code = param_read_bool(plist,
1775                                 (param_name = LIPS_OPTION_FONTDOWNLOAD),
1776                                 &fontdl)) < 0)
1777         param_signal_error(plist, param_name, ecode = code);
1778 
1779     if ((code = param_read_bool(plist,
1780                                 (param_name = LIPS_OPTION_FACEUP),
1781                                 &faceup)) < 0) {
1782         param_signal_error(plist, param_name, ecode = code);
1783     }
1784 
1785     switch (code = param_read_int(plist, (param_name = "BitsPerPixel"), &bpp)) {
1786         case 0:
1787         if (bpp != 8 && bpp != 24)
1788             ecode = gs_error_rangecheck;
1789         else
1790             break;
1791         goto bppe;
1792         default:
1793         ecode = code;
1794       bppe:param_signal_error(plist, param_name, ecode);
1795         case 1:
1796         break;
1797     }
1798 
1799     if (bpp != 0) {
1800         dev->color_info.depth = bpp;
1801         dev->color_info.num_components = ((bpp == 8) ? 1 : 3);
1802         dev->color_info.max_gray = (bpp > 8 ? 255 : 1000);
1803         dev->color_info.max_color = (bpp > 8 ? 255 : 1000);
1804         dev->color_info.dither_grays = (bpp > 8 ? 256 : 5);
1805         dev->color_info.dither_colors = (bpp > 8 ? 256 : 2);
1806         dev_proc(pdev, map_rgb_color) =
1807             ((bpp == 8) ? gx_default_gray_map_rgb_color :
1808              gx_default_rgb_map_rgb_color);
1809         dev_proc(pdev, map_color_rgb) =
1810             ((bpp == 8) ? gx_default_gray_map_color_rgb :
1811              gx_default_rgb_map_color_rgb);
1812     }
1813 
1814     if (ecode < 0)
1815         return ecode;
1816     code = gdev_vector_put_params(dev, plist);
1817     if (code < 0)
1818         return code;
1819 
1820     pdev->ManualFeed = mf;
1821     pdev->cassetFeed = cass;
1822     pdev->Tumble = tum;
1823     pdev->nup = nup;
1824     pdev->pjl = pjl;
1825     pdev->toner_density = toner_density;
1826     pdev->toner_saving = toner_saving;
1827     pdev->toner_saving_set = toner_saving_set;
1828     pdev->FontDL = fontdl;
1829     pdev->faceup = faceup;
1830 
1831     if (duplex_set >= 0) {
1832         pdev->Duplex = duplex;
1833         pdev->Duplex_set = duplex_set;
1834     }
1835     if (pmedia.data != 0 &&
1836         bytes_compare(pmedia.data, pmedia.size,
1837                       (const byte *)pdev->mediaType, strlen(pdev->mediaType))
1838         ) {
1839         memcpy(pdev->mediaType, pmedia.data, pmedia.size);
1840         pdev->mediaType[pmedia.size] = 0;
1841     }
1842     if (usern.data != 0 &&
1843         bytes_compare(usern.data, usern.size,
1844                       (const byte *)pdev->Username, strlen(pdev->Username))
1845         ) {
1846         memcpy(pdev->Username, usern.data, usern.size);
1847         pdev->Username[usern.size] = 0;
1848     }
1849     if (bpp != 0 && bpp != old_bpp && pdev->is_open)
1850         return gs_closedevice(dev);
1851     return 0;
1852 }
1853 
1854 /* ---------------- Images ---------------- */
1855 
1856 static int
lips4v_copy_mono(gx_device * dev,const byte * data,int data_x,int raster,gx_bitmap_id id,int x,int y,int w,int h,gx_color_index zero,gx_color_index one)1857 lips4v_copy_mono(gx_device * dev, const byte * data,
1858                  int data_x, int raster, gx_bitmap_id id, int x, int y, int w,
1859                  int h, gx_color_index zero, gx_color_index one)
1860 {
1861     gx_device_lips4v *const pdev = (gx_device_lips4v *) dev;
1862     gx_device_vector *const vdev = (gx_device_vector *) dev;
1863     stream *s = gdev_vector_stream(vdev);
1864     int dpi = dev->x_pixels_per_inch;
1865     gx_drawing_color color;
1866     int code = 0;
1867     floatp r, g, b;
1868 
1869     if (id != gs_no_id && zero == gx_no_color_index &&
1870         one != gx_no_color_index && data_x == 0) {
1871         gx_drawing_color dcolor;
1872 
1873         color_set_pure(&dcolor, one);
1874 #if GS_VERSION_MAJOR >= 8
1875         lips4v_setfillcolor(vdev, NULL, &dcolor);
1876 #else
1877         lips4v_setfillcolor(vdev, &dcolor);
1878 #endif
1879 
1880         if (lips4v_copy_text_char(dev, data, raster, id, x, y, w, h) >= 0)
1881             return 0;
1882     }
1883     if (pdev->TextMode) {
1884         sputc(s, LIPS_CSI);
1885         lputs(s, "&}");
1886         pdev->TextMode = FALSE;
1887     }
1888     /*
1889        (*dev_proc(vdev->bbox_device, copy_mono))
1890        ((gx_device *)vdev->bbox_device, data, data_x, raster, id,
1891        x, y, w, h, zero, one);
1892      */
1893     if (zero == gx_no_color_index) {
1894         if (one == gx_no_color_index)
1895             return 0;
1896         /* one �������ᡢƩ��ˤ��� */
1897         if (pdev->MaskState != 1) {
1898             lputs(s, "}H1");
1899             sputc(s, LIPS_IS2);
1900             pdev->MaskState = 1;
1901         }
1902         if (pdev->color_info.depth == 8) {
1903             gx_color_index one_color = vdev->color_info.max_gray - one;
1904 
1905             lputs(s, "}T");
1906             sput_lips_int(s, one_color);
1907             sputc(s, LIPS_IS2);
1908         } else {
1909             r = (one >> 16) * 1000.0 / 255.0;
1910             g = ((one >> 8) & 0xff) * 1000.0 / 255.0;
1911             b = (one & 0xff) * 1000.0 / 255.0;
1912             lputs(s, "}T");
1913             sput_lips_int(s, r);
1914             sput_lips_int(s, g);
1915             sput_lips_int(s, b);
1916             sputc(s, LIPS_IS2);
1917         }
1918     } else if (one == gx_no_color_index)
1919         /* 1bit ��Ʃ�� �ӥå�ȿž��zero ��������� */
1920     {
1921         gx_color_index zero_color = vdev->color_info.max_gray - zero;
1922 
1923         if (pdev->MaskState != 1) {
1924             lputs(s, "}H1");
1925             sputc(s, LIPS_IS2);
1926             pdev->MaskState = 1;
1927         }
1928         if (pdev->color_info.depth == 8) {
1929             lputs(s, "}T");
1930             sput_lips_int(s, zero_color);
1931             sputc(s, LIPS_IS2);
1932         } else {
1933             r = (zero >> 16) * 1000.0 / 255.0;
1934             g = ((zero >> 8) & 0xff) * 1000.0 / 255.0;
1935             b = (zero & 0xff) * 1000.0 / 255.0;
1936             lputs(s, "}T");
1937             sput_lips_int(s, r);
1938             sput_lips_int(s, g);
1939             sput_lips_int(s, b);
1940             sputc(s, LIPS_IS2);
1941         }
1942     } else if (one == vdev->white) {
1943         /* �ӥå�ȿž ���ɤ�  zero ��������� */
1944         gx_color_index zero_color = vdev->color_info.max_gray - zero;
1945 
1946         if (pdev->MaskState != 0) {
1947             lputs(s, "}H0");
1948             sputc(s, LIPS_IS2);
1949             pdev->MaskState = 0;
1950         }
1951         if (pdev->color_info.depth == 8) {
1952             lputs(s, "}T");
1953             sput_lips_int(s, zero_color);
1954             sputc(s, LIPS_IS2);
1955         } else {
1956             r = (zero >> 16) * 1000.0 / 255.0;
1957             g = ((zero >> 8) & 0xff) * 1000.0 / 255.0;
1958             b = (zero & 0xff) * 1000.0 / 255.0;
1959             lputs(s, "}T");
1960             sput_lips_int(s, r);
1961             sput_lips_int(s, g);
1962             sput_lips_int(s, b);
1963             sputc(s, LIPS_IS2);
1964         }
1965     } else {
1966         if (zero != gx_no_color_index) {
1967             code = (*dev_proc(dev, fill_rectangle)) (dev, x, y, w, h, zero);
1968             if (code < 0)
1969                 return code;
1970         }
1971         if (pdev->MaskState != 1) {
1972             lputs(s, "}H1");
1973             sputc(s, LIPS_IS2);
1974             pdev->MaskState = 1;
1975         }
1976         color_set_pure(&color, one);
1977 
1978 #if GS_VERSION_MAJOR >= 8
1979         code = gdev_vector_update_fill_color((gx_device_vector *) pdev,
1980                                              NULL, &color);
1981 #else
1982         code = gdev_vector_update_fill_color((gx_device_vector *) pdev,
1983                                              &color);
1984 #endif
1985     }
1986     if (code < 0)
1987         return 0;
1988     lputs(s, "}P");
1989     sput_lips_int(s, x);	/* Position X */
1990     sput_lips_int(s, y);	/* Position Y */
1991     sput_lips_int(s, dpi * 100);
1992     sput_lips_int(s, dpi * 100);
1993     sput_lips_int(s, h);	/* Height */
1994     sput_lips_int(s, w);	/* Width */
1995     lputs(s, "100110");
1996     sputc(s, LIPS_IS2);
1997 
1998     lputs(s, "}Q11");
1999 
2000     {
2001         int i, j;
2002         uint width_bytes = (w + 7) >> 3;
2003         uint num_bytes = round_up(width_bytes, 4) * h;
2004         byte *buf = gs_alloc_bytes(vdev->memory, num_bytes,
2005                                    "lips4v_copy_mono(buf)");
2006 
2007         if (data_x % 8 == 0) {
2008             for (i = 0; i < h; ++i) {
2009                 memcpy(buf + i * width_bytes,
2010                        data + (data_x >> 3) + i * raster, width_bytes);
2011             }
2012         } else {
2013             for (i = 0; i < h; ++i) {
2014                 for (j = 0; j < width_bytes; j++) {
2015                     *(buf + i * width_bytes + j) =
2016                         *(data + (data_x >> 3) + i * raster +
2017                           j) << (data_x % 8) | *(data + (data_x >> 3) +
2018                                                  i * raster + j + 1) >> (8 -
2019                                                                          data_x
2020                                                                          % 8);
2021                 }
2022             }
2023         }
2024 
2025         if (one == gx_no_color_index
2026             || (one == vdev->white
2027                 && zero != gx_no_color_index)) lips4v_write_image_data(vdev,
2028                                                                        buf,
2029                                                                        num_bytes,
2030                                                                        TRUE);
2031         else
2032             lips4v_write_image_data(vdev, buf, num_bytes, FALSE);
2033 
2034         gs_free_object(vdev->memory, buf, "lips4v_copy_mono(buf)");
2035     }
2036 
2037     return 0;
2038 }
2039 
2040 /* Copy a color bitmap. */
2041 static int
lips4v_copy_color(gx_device * dev,const byte * data,int data_x,int raster,gx_bitmap_id id,int x,int y,int w,int h)2042 lips4v_copy_color(gx_device * dev,
2043                   const byte * data, int data_x, int raster, gx_bitmap_id id,
2044                   int x, int y, int w, int h)
2045 {
2046     gx_device_lips4v *const pdev = (gx_device_lips4v *) dev;
2047     gx_device_vector *const vdev = (gx_device_vector *) dev;
2048 
2049     stream *s = gdev_vector_stream(vdev);
2050     int depth = dev->color_info.depth;
2051     int dpi = dev->x_pixels_per_inch;
2052     int num_components = (depth < 24 ? 1 : 3);
2053     uint width_bytes = w * num_components;
2054 
2055     if (dev->color_info.depth == 8) {
2056         gx_drawing_color dcolor;
2057 
2058         /* LIPS IV �Ǥϥ��졼���������ñ�����᡼�������顼����̿���
2059            �ƶ������Τǹ�������ꤷ�ʤ���Фʤ�ʤ��� */
2060         color_set_pure(&dcolor, vdev->black);
2061 #if GS_VERSION_MAJOR >= 8
2062         lips4v_setfillcolor(vdev, NULL, &dcolor);
2063 #else
2064         lips4v_setfillcolor(vdev, &dcolor);
2065 #endif
2066     } else {
2067         if (pdev->TextMode) {
2068             sputc(s, LIPS_CSI);
2069             lputs(s, "&}");
2070             pdev->TextMode = FALSE;
2071         }
2072     }
2073 
2074     if (pdev->MaskState != 0) {
2075         lputs(s, "}H0");	/* ������������̿�� */
2076         sputc(s, LIPS_IS2);
2077         pdev->MaskState = 0;
2078     }
2079     lputs(s, "}P");
2080     sput_lips_int(s, x);
2081     sput_lips_int(s, y);
2082     sput_lips_int(s, dpi * 100);
2083     sput_lips_int(s, dpi * 100);
2084     sput_lips_int(s, h);
2085     sput_lips_int(s, w);
2086     sput_lips_int(s, depth / num_components);
2087     sputc(s, depth < 24 ? '0' : ':');	/* 24 bit �ΤȤ����缡 */
2088     lputs(s, "0110");
2089     sputc(s, LIPS_IS2);
2090 
2091     {
2092         int i;
2093         uint num_bytes = width_bytes * h;
2094         byte *buf = gs_alloc_bytes(vdev->memory, num_bytes,
2095                                    "lips4v_copy_color(buf)");
2096 
2097         lputs(s, "}Q11");
2098 
2099         for (i = 0; i < h; ++i) {
2100             memcpy(buf + i * width_bytes,
2101                    data + ((data_x * depth) >> 3) + i * raster, width_bytes);
2102         }
2103 
2104         if (dev->color_info.depth == 8)
2105             lips4v_write_image_data(vdev, buf, num_bytes, TRUE);
2106         else
2107             lips4v_write_image_data(vdev, buf, num_bytes, FALSE);
2108 
2109         gs_free_object(vdev->memory, buf, "lips4v_copy_color(buf)");
2110     }
2111 
2112     return 0;
2113 }
2114 
2115 /* Fill a mask. */
2116 static int
lips4v_fill_mask(gx_device * dev,const byte * data,int data_x,int raster,gx_bitmap_id id,int x,int y,int w,int h,const gx_drawing_color * pdcolor,int depth,gs_logical_operation_t lop,const gx_clip_path * pcpath)2117 lips4v_fill_mask(gx_device * dev,
2118                  const byte * data, int data_x, int raster, gx_bitmap_id id,
2119                  int x, int y, int w, int h,
2120                  const gx_drawing_color * pdcolor, int depth,
2121                  gs_logical_operation_t lop, const gx_clip_path * pcpath)
2122 {
2123     gx_device_vector *const vdev = (gx_device_vector *) dev;
2124     gx_device_lips4v *const pdev = (gx_device_lips4v *) dev;
2125     stream *s = gdev_vector_stream(vdev);
2126     int dpi = dev->x_pixels_per_inch;
2127 
2128     if (w <= 0 || h <= 0)
2129         return 0;
2130     if (depth > 1 ||
2131 #if GS_VERSION_MAJOR >= 8
2132         gdev_vector_update_fill_color(vdev, NULL, pdcolor) < 0 ||
2133 #else
2134         gdev_vector_update_fill_color(vdev, pdcolor) < 0 ||
2135 #endif
2136         gdev_vector_update_clip_path(vdev, pcpath) < 0 ||
2137         gdev_vector_update_log_op(vdev, lop) < 0)
2138         return gx_default_fill_mask(dev, data, data_x, raster, id,
2139                                     x, y, w, h, pdcolor, depth, lop, pcpath);
2140 #if 1
2141     (*dev_proc(vdev->bbox_device, fill_mask))
2142         ((gx_device *) vdev->bbox_device, data, data_x, raster, id,
2143          x, y, w, h, pdcolor, depth, lop, pcpath);
2144 #endif
2145     if (id != gs_no_id && data_x == 0) {
2146         if (lips4v_copy_text_char(dev, data, raster, id, x, y, w, h) >= 0)
2147             return 0;
2148     }
2149     if (pdev->TextMode) {
2150         sputc(s, LIPS_CSI);
2151         lputs(s, "&}");
2152         pdev->TextMode = FALSE;
2153     }
2154     /* ������ */
2155     if (pdev->MaskState != 1) {
2156         lputs(s, "}H1");	/* ������������̿�� */
2157         sputc(s, LIPS_IS2);
2158         pdev->MaskState = 1;
2159     }
2160     lputs(s, "}P");
2161     sput_lips_int(s, x);
2162     sput_lips_int(s, y);
2163     sput_lips_int(s, dpi * 100);
2164     sput_lips_int(s, dpi * 100);
2165     sput_lips_int(s, h);
2166     sput_lips_int(s, w);
2167     lputs(s, "100110");
2168     sputc(s, LIPS_IS2);
2169 
2170     lputs(s, "}Q11");
2171 
2172     {
2173         int i;
2174         uint width_bytes = (w + 7) >> 3;
2175         uint num_bytes = round_up(width_bytes, 4) * h;
2176         byte *buf = gs_alloc_bytes(vdev->memory, num_bytes,
2177                                    "lips4v_fill_mask(buf)");
2178 
2179         for (i = 0; i < h; ++i) {
2180             memcpy(buf + i * width_bytes, data + (data_x >> 3) + i * raster,
2181                    width_bytes);
2182         }
2183 
2184         lips4v_write_image_data(vdev, buf, num_bytes, FALSE);
2185 
2186         gs_free_object(vdev->memory, buf, "lips4v_fill_mask(buf)");
2187     }
2188 
2189     return 0;
2190 }
2191 
2192 /* ---------------- High-level images ---------------- */
2193 
2194 static image_enum_proc_plane_data(lips4v_image_plane_data);
2195 static image_enum_proc_end_image(lips4v_image_end_image);
2196 static const gx_image_enum_procs_t lips4v_image_enum_procs = {
2197     lips4v_image_plane_data, lips4v_image_end_image
2198 };
2199 
2200 /* Start processing an image. */
2201 static int
lips4v_begin_image(gx_device * dev,const gs_imager_state * pis,const gs_image_t * pim,gs_image_format_t format,const gs_int_rect * prect,const gx_drawing_color * pdcolor,const gx_clip_path * pcpath,gs_memory_t * mem,gx_image_enum_common_t ** pinfo)2202 lips4v_begin_image(gx_device * dev,
2203                    const gs_imager_state * pis, const gs_image_t * pim,
2204                    gs_image_format_t format, const gs_int_rect * prect,
2205                    const gx_drawing_color * pdcolor,
2206                    const gx_clip_path * pcpath, gs_memory_t * mem,
2207                    gx_image_enum_common_t ** pinfo)
2208 {
2209     gx_device_vector *const vdev = (gx_device_vector *) dev;
2210     gx_device_lips4v *const pdev = (gx_device_lips4v *) dev;
2211     gdev_vector_image_enum_t *pie =
2212         gs_alloc_struct(mem, gdev_vector_image_enum_t,
2213                         &st_vector_image_enum, "lips4v_begin_image");
2214     const gs_color_space *pcs = pim->ColorSpace;
2215     gs_color_space_index index;
2216     int num_components = 1;
2217     bool can_do = prect == 0 &&
2218         (pim->format == gs_image_format_chunky ||
2219 
2220          pim->format == gs_image_format_component_planar);
2221 
2222     int code;
2223 
2224     if (pie == 0)
2225         return_error(gs_error_VMerror);
2226     pie->memory = mem;
2227     code = gdev_vector_begin_image(vdev, pis, pim, format, prect,
2228                                    pdcolor, pcpath, mem,
2229                                    &lips4v_image_enum_procs, pie);
2230     if (code < 0)
2231         return code;
2232     *pinfo = (gx_image_enum_common_t *) pie;
2233 
2234     if (!pim->ImageMask) {
2235         index = gs_color_space_get_index(pcs);
2236         num_components = gs_color_space_num_components(pcs);
2237 
2238         if (pim->CombineWithColor)
2239             can_do = false;
2240         else {
2241             switch (index) {
2242                 case gs_color_space_index_DeviceGray:
2243                 if ((pim->Decode[0] != 0 || pim->Decode[1] != 1)
2244                     && (pim->Decode[0] != 1 || pim->Decode[1] != 0))
2245                     can_do = false;
2246                 break;
2247                 case gs_color_space_index_DeviceRGB:
2248                 /* LIPS �Ǥ� RGB ��ȿž���뤳�ȤϤǤ��ʤ� */
2249                 if (pim->Decode[0] != 0 || pim->Decode[1] != 1 ||
2250                     pim->Decode[2] != 0 || pim->Decode[3] != 1 ||
2251                     pim->Decode[4] != 0)
2252                     can_do = false;
2253                 break;
2254                 default:
2255                 /*
2256                    LIPS �Ǥ� L*a*b* �����Υ��顼���ڡ������Ȥ��ޤ���
2257                    CIEBasedABC ��Ȥä�ɽ���Ǥ��ʤ����Ȥ�ʤ��ΤǤ�����
2258                    ư���ǧ�Ǥ��ʤ��Τ����٥�δؿ��ˤޤ����뤳�Ȥ�
2259                    �����㤤�ޤ���
2260                    ������� CMYK �Υ��顼���ڡ������ߤ���...
2261                  */
2262                 can_do = false;
2263             }
2264         }
2265     }
2266     if (!can_do)
2267         return gx_default_begin_image(dev, pis, pim, format, prect,
2268                                       pdcolor, pcpath, mem,
2269                                       &pie->default_info);
2270     else if (index == gs_color_space_index_DeviceGray) {
2271         gx_drawing_color dcolor;
2272 
2273         /* LIPS IV �Ǥϥ��졼���������ñ�����᡼�������顼����̿���
2274            �ƶ������Τǹ���������Ū�˻��ꤷ�ʤ���Фʤ�ʤ��� */
2275         color_set_pure(&dcolor, vdev->black);
2276 #if GS_VERSION_MAJOR >= 8
2277         lips4v_setfillcolor(vdev, NULL, &dcolor);
2278 #else
2279         lips4v_setfillcolor(vdev, &dcolor);
2280 #endif
2281     }
2282     if (pim->ImageMask || (pim->BitsPerComponent == 1 && num_components == 1)) {
2283         if (pim->Decode[0] > pim->Decode[1])
2284             pdev->MaskReverse = 1;
2285         else
2286             pdev->MaskReverse = 0;
2287     }
2288     /* Write the image/colorimage/imagemask preamble. */
2289     {
2290         stream *s = gdev_vector_stream((gx_device_vector *) pdev);
2291         int ax, ay, bx, by, cx, cy, dx, dy;
2292         int tbyte;
2293         gs_matrix imat;
2294         int interpolate = 0;
2295 
2296         if (pdev->TextMode) {
2297             sputc(s, LIPS_CSI);
2298             lputs(s, "&}");
2299             pdev->TextMode = FALSE;
2300         }
2301         gs_matrix_invert(&pim->ImageMatrix, &imat);
2302         gs_matrix_multiply(&imat, &ctm_only(pis), &imat);
2303         /*
2304            [xx xy yx yy tx ty]
2305            LIPS �κ�ɸ�Ϥ��Ѵ���Ԥʤ���
2306 
2307            }U{Ax}{Ay}{Bx}{By}{Cx}{Cy}{pie->height}{pie->width}
2308            {pim->BitsPerComponent}{0}{0}{1} LIPS_IS2
2309          */
2310 
2311         /* }Q110{byte} LIPS_IS2 */
2312         ax = imat.tx;
2313         ay = imat.ty;
2314         bx = imat.xx * pim->Width + imat.yx * pim->Height + imat.tx;
2315         by = imat.xy * pim->Width + imat.yy * pim->Height + imat.ty;
2316         cx = imat.yx * pim->Height + imat.tx;
2317         cy = imat.yy * pim->Height + imat.ty;
2318         dx = imat.xx * pim->Width + imat.tx;
2319         dy = imat.xy * pim->Width + imat.ty;
2320 
2321         if (pim->ImageMask) {
2322             tbyte =
2323                 (pie->width * pim->BitsPerComponent +
2324                  7) / 8 * num_components * pie->height;
2325             pdev->ncomp = 1;
2326             if (tbyte == 1) {
2327                 /* LIPS IV �Ǥ� 1 dot �Υ��᡼�����ѷ�����Ⱥ�ɸ�������Х���
2328                    ���롣��ä� 1 dot �Υ��᡼���϶���Ȥ��ƽ������롣 */
2329                 pdev->OneBitMask = true;
2330                 /* Draw Rectangle */
2331                 lputs(s, "2");
2332                 sput_lips_int(s, ax);
2333                 sput_lips_int(s, ay);
2334                 sput_lips_int(s, cx - ax);
2335                 sput_lips_int(s, cy - ay);
2336                 sput_lips_int(s, bx - cx);
2337                 sput_lips_int(s, by - cy);
2338                 sput_lips_int(s, dx - bx);
2339                 sput_lips_int(s, dy - by);
2340                 sputc(s, LIPS_IS2);
2341                 return 0;
2342             } else {
2343                 /* ������������̿�� - Ʃ�� */
2344                 if (pdev->MaskState != 1) {
2345                     lputs(s, "}H1");
2346                     sputc(s, LIPS_IS2);
2347                     pdev->MaskState = 1;
2348                 }
2349             }
2350         } else {
2351             /* ������������̿�� - ���ɤ� */
2352             if (pdev->MaskState != 0) {
2353                 lputs(s, "}H0");
2354                 sputc(s, LIPS_IS2);
2355                 pdev->MaskState = 0;
2356             }
2357             pdev->ncomp = num_components;
2358         }
2359 
2360         lputs(s, "}U");
2361         sput_lips_int(s, ax);
2362         sput_lips_int(s, ay);
2363         sput_lips_int(s, bx);
2364         sput_lips_int(s, by);
2365         sput_lips_int(s, cx);
2366         sput_lips_int(s, cy);
2367         sput_lips_int(s, pie->height);
2368         sput_lips_int(s, pie->width);
2369         sput_lips_int(s, pim->BitsPerComponent);
2370 
2371         if (pim->Interpolate) {
2372             if (pim->BitsPerComponent * pie->num_planes == 1)
2373                 interpolate = 1;
2374             else
2375                 interpolate = 3;
2376         }
2377         if (pim->ImageMask) {	/* 1bit �ΤȤ� */
2378             lputs(s, "0");
2379         } else {
2380             if (index == gs_color_space_index_DeviceGray)
2381                 lputs(s, "0");
2382             else {
2383                 if (format == gs_image_format_chunky)	/* RGBRGBRGB... */
2384                     sputc(s, 0x3a);
2385                 else		/* RRR...GGG...BBB... */
2386                     sputc(s, 0x3b);
2387             }
2388         }
2389         if (interpolate > 0)
2390             sput_lips_int(s, interpolate);
2391         sputc(s, LIPS_IS2);
2392     }
2393     return 0;
2394 }
2395 
2396 /* Process the next piece of an image. */
2397 static int
lips4v_image_plane_data(gx_image_enum_common_t * info,const gx_image_plane_t * planes,int height,int * rows_used)2398 lips4v_image_plane_data(gx_image_enum_common_t * info,
2399                         const gx_image_plane_t * planes, int height,
2400                         int *rows_used)
2401 {
2402     gx_device *dev = info->dev;
2403     gx_device_vector *const vdev = (gx_device_vector *) dev;
2404     gx_device_lips4v *const pdev = (gx_device_lips4v *) dev;
2405     gdev_vector_image_enum_t *pie = (gdev_vector_image_enum_t *) info;
2406 
2407     stream *s = gdev_vector_stream((gx_device_vector *) pdev);
2408     int y;
2409 
2410     if (pdev->OneBitMask) {
2411         /*
2412            lputs(s, "\200");
2413          */
2414         pie->y += height;
2415         return 1;
2416     }
2417     if (pie->default_info)
2418         return gx_image_plane_data(pie->default_info, planes, height);
2419     gx_image_plane_data(pie->bbox_info, planes, height);
2420     {
2421         int plane;
2422         int width_bytes, tbyte;
2423         byte *buf;
2424 
2425         width_bytes =
2426             (pie->width * pie->bits_per_pixel / pdev->ncomp +
2427              7) / 8 * pdev->ncomp;
2428         tbyte = width_bytes * height;
2429         buf = gs_alloc_bytes(vdev->memory, tbyte, "lips4v_image_data(buf)");
2430 
2431         for (plane = 0; plane < pie->num_planes; ++plane)
2432             for (y = 0; y < height; ++y) {
2433                 memcpy(buf + y * width_bytes,
2434                        planes[plane].data +
2435                        ((planes[plane].data_x * pie->bits_per_pixel) >> 3)
2436                        + y * planes[plane].raster, width_bytes);
2437             }
2438 
2439         lputs(s, "}Q10");
2440 
2441         if ((pie->bits_per_pixel > 1 && pdev->ncomp == 1) ||
2442             pdev->MaskReverse == 0) {
2443             lips4v_write_image_data(vdev, buf, tbyte, TRUE);
2444         } else
2445             lips4v_write_image_data(vdev, buf, tbyte, FALSE);
2446 
2447         gs_free_object(vdev->memory, buf, "lips4v_image_data(buf)");
2448 
2449     }
2450 
2451     return (pie->y += height) >= pie->height;
2452 }
2453 
2454 static int
lips4v_image_end_image(gx_image_enum_common_t * info,bool draw_last)2455 lips4v_image_end_image(gx_image_enum_common_t * info, bool draw_last)
2456 {
2457     gx_device *dev = info->dev;
2458     gx_device_vector *const vdev = (gx_device_vector *) dev;
2459     gx_device_lips4v *const pdev = (gx_device_lips4v *) dev;
2460     gdev_vector_image_enum_t *pie = (gdev_vector_image_enum_t *) info;
2461     stream *s = gdev_vector_stream((gx_device_vector *) pdev);
2462     int code;
2463 
2464     if (pdev->OneBitMask)
2465         pdev->OneBitMask = false;
2466     else
2467         lputs(s, "}Q1100");
2468         sputc(s, LIPS_IS2);	/* End of Image */
2469 
2470     pdev->MaskReverse = -1;
2471 
2472     code = gdev_vector_end_image(vdev, (gdev_vector_image_enum_t *) pie,
2473                                  draw_last, pdev->white);
2474     return code;
2475 }
2476