1 /* Copyright (C) EPSON SOFTWARE DEVELOPMENT LABORATORY 1999, 2000, 2001, 2002, 2003, 2004
2                             Maintenance by EPSON KOWA
3 
4    Ghostscript printer driver for EPSON ESC/Page-Color
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: gdevescv.c,v 1.1 2004/05/17 05:25:19 ishii Exp $ */
22 /* Vector Version of ESC/Page-Color driver */
23 
24 /*
25 ���ռ�
26     �ܥɥ饤�Фκ����ˤ����ꡢgdevlips(��� �翹���ͤ���)���ͤˤ�����
27     ĺ���ޤ�����
28 */
29 /*
30    ��Ghostscript 5.10/5.50 �ΥХ��ˤĤ���
31 
32    Ghostscript 5.10/5.50 �� Vector driver �� setlinewidth �ؿ��ˤ�
33    �Х�������ޤ������襹�����뤬�ѹ������ˤ������ä������������ѹ�����
34    �ʤ���Фʤ�ޤ�����Ghostscript 5.10/5.50 �Ǥϥ���������θ����Τ�
35    ˺��Ƥ��ޤ���
36    ���Υɥ饤�ФϤ��ΥХ�������뤿��˥��������ʬ�ǽ������Ƥ��ޤ���
37 
38  */
39 
40 #include "math_.h"
41 #include "gx.h"
42 #include "gserrors.h"
43 #include "gsmatrix.h"
44 #include "gsparam.h"
45 #include "gxdevice.h"
46 #include "gscspace.h"
47 #include "gsutil.h"
48 #include "gdevvec.h"
49 #include "spprint.h"
50 #if GS_VERSION_MAJOR == 5
51 #include "gdevpstr.h"
52 #endif
53 #include "ghost.h"
54 #include "gzstate.h"
55 #include "imemory.h"
56 #include "igstate.h"
57 #include "gdevescv.h"
58 #include "gspath.h"
59 #include "gzpath.h"
60 
61 #include <stdlib.h>
62 #include <unistd.h>
63 #include <string.h>
64 
65 /* ---------------- Device definition ---------------- */
66 
67 /* Device procedures */
68 private dev_proc_open_device(escv_open);
69 private dev_proc_output_page(escv_output_page);
70 private dev_proc_close_device(escv_close);
71 private dev_proc_copy_mono(escv_copy_mono);
72 private dev_proc_copy_color(escv_copy_color);
73 private dev_proc_put_params(escv_put_params);
74 private dev_proc_get_params(escv_get_params);
75 private dev_proc_fill_mask(escv_fill_mask);
76 private dev_proc_begin_image(escv_begin_image);
77 
78 
79 #define	X_DPI		600
80 #define	Y_DPI		600
81 #define	VCACHE		0x3FF
82 
83 typedef struct gx_device_escv_s {
84     gx_device_vector_common;
85 
86     bool		manualFeed;			/* Use manual feed */
87     int			cassetFeed;			/* Input Casset */
88     bool		RITOff;				/* RIT Control */
89     bool		Collate;			/* �������� */
90     int			toner_density;			/* �ȥʡ�ǻ�� */
91     bool		toner_saving;			/* �ȥʡ������� */
92     int			prev_paper_size;
93     int			prev_paper_width;
94     int			prev_paper_height;
95     int			prev_num_copies;		/* */
96     int			prev_feed_mode;
97     int			orientation;			/* ���� */
98     bool		faceup;				/* �ե��������� */
99     int	                MediaType;			/* ��� */
100 
101     bool		first_page;
102     bool		Duplex;				/* ξ�̰��� */
103     bool		Tumble;				/* �Ȥ����� */
104     int			ncomp;				/* */
105     int			MaskReverse;			/* ȿž���� */
106     int			MaskState;			/* */
107     bool		c4map;				/* 4bit ColorMap */
108     bool		c8map;				/* 8bit ColorMap */
109     int			prev_x;
110     int			prev_y;
111     gx_color_index	prev_color;
112     gx_color_index	current_color;			/* Current Color */
113 
114     /* for Font Downloading */
115     gx_bitmap_id	id_cache[VCACHE + 1];
116 
117     floatp		lwidth;
118     long		cap;
119     long		join;
120     long		reverse_x;
121     long		reverse_y;
122     gs_matrix		xmat;
123     int			bx, by;
124     int			w, h;
125     int			roll;
126     float		sx, sy;
127     long		dd;
128 } gx_device_escv;
129 
130 
131 gs_public_st_suffix_add0_final(st_device_escv, gx_device_escv,
132       "gx_device_escv", device_escv_enum_ptrs, device_escv_reloc_ptrs,
133 			       gx_device_finalize, st_device_vector);
134 
135 /*
136 ** �������ͤ� 0 �Ȥ������,�׻����������꤫�����襨�ꥢ����������
137 ** ������ 0.001 �Ȥ��Ƥ�����
138 */
139 #define escv_device_full_body(dtype, pprocs, dname, stype, w, h, xdpi, ydpi, \
140 			      ncomp, depth, mg, mc, dg, dc, lm, bm, rm, tm)\
141         std_device_part1_(dtype, pprocs, dname, stype, open_init_closed),\
142         dci_values(ncomp, depth, mg, mc, dg, dc),\
143         std_device_part2_(w, h, xdpi, ydpi),\
144 	offset_margin_values(0.001, 0.001, lm, 0, 0, tm), \
145         std_device_part3_()
146 
147 
148 #define escv_device_body(name)	\
149   escv_device_full_body(gx_device_escv, 0, name, \
150 				&st_device_escv,\
151 /* width & height */		ESCPAGE_DEFAULT_WIDTH, ESCPAGE_DEFAULT_HEIGHT,\
152 /* default resolution */	X_DPI, Y_DPI,\
153 /* color info */		3, 24, 255, 255, 256, 256,\
154 				ESCPAGE_LEFT_MARGIN_DEFAULT,\
155 				ESCPAGE_BOTTOM_MARGIN_DEFAULT,\
156 				ESCPAGE_RIGHT_MARGIN_DEFAULT,\
157 				ESCPAGE_TOP_MARGIN_DEFAULT)
158 
159 
160 #define escv_procs	\
161 {\
162 	escv_open,				/* open_device */\
163 	gx_default_get_initial_matrix,		/* get_initial_matrix */\
164 	NULL,					/* sync_output */\
165 	escv_output_page,			/* output_page */\
166 	escv_close,				/* close_device */\
167 	gx_default_rgb_map_rgb_color,		/* map_rgb_color */\
168         gx_default_rgb_map_color_rgb,		/* map_color_rgb */\
169 	gdev_vector_fill_rectangle,		/* fill_rectangle */\
170 	NULL,					/* tile_rectangle */\
171 	escv_copy_mono,				/* dev_t_proc_copy_mono */\
172 	escv_copy_color,			/* dev_t_proc_copy_color */\
173 	NULL,					/* draw_line */\
174 	NULL,					/* get_bits */\
175 	escv_get_params,			/* dev_t_proc_get_params */\
176 	escv_put_params,			/* dev_t_proc_put_params */\
177 	NULL,					/* map_cmyk_color */\
178 	NULL,					/* get_xfont_procs */\
179 	NULL,					/* get_xfont_device */\
180 	NULL,					/* map_rgb_alpha_color */\
181 	gx_page_device_get_page_device,		/* dev_t_proc_get_page_device */\
182 	NULL,					/* get_alpha_bits */\
183 	NULL,					/* copy_alpha */\
184 	NULL,					/* get_band */\
185 	NULL,					/* copy_rop */\
186 	gdev_vector_fill_path,			/* fill_path */\
187 	gdev_vector_stroke_path,		/* stroke_path */\
188 	escv_fill_mask,				/* fill_mask */\
189 	gdev_vector_fill_trapezoid,		/* fill_trapezoid */\
190 	gdev_vector_fill_parallelogram,		/* fill_parallelogram */\
191 	gdev_vector_fill_triangle,		/* fill_triangle */\
192 	NULL,	/****** WRONG ******/		/* draw_thin_line */\
193 	escv_begin_image,			/* begin_image */\
194 	NULL,					/* image_data */\
195 	NULL,					/* end_image */\
196 	NULL,					/* strip_tile_rectangle */\
197 	NULL					/******strip_copy_rop******/\
198 }
199 
200 
201 #define	escv_init_code			\
202     vector_initial_values,\
203     ESCPAGE_MANUALFEED_DEFAULT,\
204     ESCPAGE_CASSETFEED_DEFAULT,\
205     ESCPAGE_RIT_DEFAULT,		/* RIT */\
206     FALSE,				/* Collate */\
207     0,					/* toner_density */\
208     FALSE,				/* toner_saving */\
209     0, 0, 0, 0, -1,\
210     0,					/* orientation */\
211     ESCPAGE_FACEUP_DEFAULT,\
212     ESCPAGE_MEDIATYPE_DEFAULT,\
213     0,					/* first_page */\
214     0,					/* Duplex */\
215     ESCPAGE_TUMBLE_DEFAULT,\
216     0,					/* ncomp */\
217     0,					/* MaskReverse */\
218     0,					/* MaskState */\
219     TRUE,				/* 4bits Color Map */\
220     TRUE,				/* 8bits Color Map */\
221     0,					/* prev_x */\
222     0,					/* prev_y */\
223     0,					/* prev_color */\
224     0,					/* current_color */\
225     {},					/* id_cache */\
226     3,					/* lwidth */\
227     0,					/* cap */\
228     3,					/* join */\
229     0,0,				/* reverse x, y */\
230     {}, 				/* matrix */\
231     0,0,				/* x, y */\
232     0,0,				/* width, hight */\
233     0,					/* roll */\
234     0,0,				/* scale x, y */\
235     0					/* */
236 
237 
238 gx_device_escv far_data gs_lp8000c_device =
239 {
240     escv_device_body("lp8000c"),
241     escv_procs,
242     escv_init_code
243 };
244 
245 
246 gx_device_escv far_data gs_lp8200c_device =
247 {
248     escv_device_body("lp8200c"),
249     escv_procs,
250     escv_init_code
251 };
252 
253 gx_device_escv far_data gs_lp8300c_device =
254 {
255     escv_device_body("lp8300c"),
256     escv_procs,
257     escv_init_code
258 };
259 
260 gx_device_escv far_data gs_lp8500c_device =
261 {
262     escv_device_body("lp8500c"),
263     escv_procs,
264     escv_init_code
265 };
266 
267 gx_device_escv far_data gs_lp8800c_device =
268 {
269     escv_device_body("lp8800c"),
270     escv_procs,
271     escv_init_code
272 };
273 
274 gx_device_escv far_data gs_lp9000c_device =
275 {
276     escv_device_body("lp9000c"),
277     escv_procs,
278     escv_init_code
279 };
280 
281 gx_device_escv far_data gs_lp9500c_device =
282 {
283     escv_device_body("lp9500c"),
284     escv_procs,
285     escv_init_code
286 };
287 
288 gx_device_escv far_data gs_lp9800c_device =
289 {
290     escv_device_body("lp9800c"),
291     escv_procs,
292     escv_init_code
293 };
294 
295 gx_device_escv far_data gs_lp3000c_device =
296 {
297     escv_device_body("lp3000c"),
298     escv_procs,
299     escv_init_code
300 };
301 
302 gx_device_escv far_data gs_alc8600_device =
303 {
304     escv_device_body("alc8600"),
305     escv_procs,
306     escv_init_code
307 };
308 
309 gx_device_escv far_data gs_alc8500_device =
310 {
311     escv_device_body("alc8500"),
312     escv_procs,
313     escv_init_code
314 };
315 
316 gx_device_escv far_data gs_alc2000_device =
317 {
318     escv_device_body("alc2000"),
319     escv_procs,
320     escv_init_code
321 };
322 
323 gx_device_escv far_data gs_alc4000_device =
324 {
325     escv_device_body("alc4000"),
326     escv_procs,
327     escv_init_code
328 };
329 
330 gx_device_escv far_data gs_alc4100_device =
331 {
332     escv_device_body("alc4100"),
333     escv_procs,
334     escv_init_code
335 };
336 
337 gx_device_escv far_data gs_alc1900_device =
338 {
339     escv_device_body("alc1900"),
340     escv_procs,
341     escv_init_code
342 };
343 
344 /* Vector device implementation */
345 #if GS_VERSION_MAJOR >= 8
346 private int escv_beginpage(gx_device_vector * vdev);
347 private int escv_setfillcolor(gx_device_vector * vdev, const gx_drawing_color * pdc);
348 private int escv_setstrokecolor(gx_device_vector * vdev, const gx_drawing_color * pdc);
349 private int escv_setdash(gx_device_vector * vdev, const float *pattern,
350 			      uint count, floatp offset);
351 private int escv_setflat(gx_device_vector * vdev, floatp flatness);
352 private int escv_setlogop(gx_device_vector * vdev, gs_logical_operation_t lop,
353 			       gs_logical_operation_t diff);
354 #else
355 private int escv_beginpage(P1(gx_device_vector * vdev));
356 private int escv_setfillcolor(P2(gx_device_vector * vdev, const gx_drawing_color * pdc));
357 private int escv_setstrokecolor(P2(gx_device_vector * vdev, const gx_drawing_color * pdc));
358 private int escv_setdash(P4(gx_device_vector * vdev, const float *pattern,
359 			      uint count, floatp offset));
360 private int escv_setflat(P2(gx_device_vector * vdev, floatp flatness));
361 private int escv_setlogop(P3(gx_device_vector * vdev, gs_logical_operation_t lop,
362 			       gs_logical_operation_t diff));
363 #endif
364 private int escv_vector_dorect(gx_device_vector * vdev, fixed x0, fixed y0, fixed x1,
365 			       fixed y1, gx_path_type_t type);
366 private int escv_vector_dopath(gx_device_vector * vdev, const gx_path * ppath,
367 			       gx_path_type_t type);
368 #if GS_VERSION_MAJOR >= 8
369 private int escv_beginpath(gx_device_vector * vdev, gx_path_type_t type);
370 private int escv_moveto(gx_device_vector * vdev, floatp x0, floatp y0,
371 			   floatp x, floatp y, gx_path_type_t type);
372 private int escv_lineto(gx_device_vector * vdev, floatp x0, floatp y0,
373 			   floatp x, floatp y, gx_path_type_t type);
374 private int escv_curveto(gx_device_vector * vdev, floatp x0, floatp y0,
375 			       floatp x1, floatp y1, floatp x2, floatp y2,
376 			       floatp x3, floatp y3, gx_path_type_t type);
377 private int escv_closepath(gx_device_vector * vdev, floatp x, floatp y,
378 				floatp x_start, floatp y_start, gx_path_type_t type);
379 
380 private int escv_endpath(gx_device_vector * vdev, gx_path_type_t type);
381 #else
382 private int escv_beginpath(P2(gx_device_vector * vdev, gx_path_type_t type));
383 private int escv_moveto(P6(gx_device_vector * vdev, floatp x0, floatp y0,
384 			   floatp x, floatp y, gx_path_type_t type));
385 private int escv_lineto(P6(gx_device_vector * vdev, floatp x0, floatp y0,
386 			   floatp x, floatp y, gx_path_type_t type));
387 private int escv_curveto(P10(gx_device_vector * vdev, floatp x0, floatp y0,
388 			       floatp x1, floatp y1, floatp x2, floatp y2,
389 			       floatp x3, floatp y3, gx_path_type_t type));
390 private int escv_closepath(P6(gx_device_vector * vdev, floatp x, floatp y,
391 				floatp x_start, floatp y_start, gx_path_type_t type));
392 
393 private int escv_endpath(P2(gx_device_vector * vdev, gx_path_type_t type));
394 #endif
395 private int escv_setlinewidth(gx_device_vector * vdev, floatp width);
396 private int escv_setlinecap(gx_device_vector * vdev, gs_line_cap cap);
397 private int escv_setlinejoin(gx_device_vector * vdev, gs_line_join join);
398 private int escv_setmiterlimit(gx_device_vector * vdev, floatp limit);
399 
400 private void escv_write_begin(gx_device *, int, int, int, int, int, int, int, int);
401 private void escv_write_data(gx_device *, int, char *, int, int, int);
402 private void escv_write_end(gx_device *, int);
403 
404 
405 private const gx_device_vector_procs escv_vector_procs =
406 {
407 	/* Page management */
408     escv_beginpage,
409 	/* Imager state */
410     escv_setlinewidth,
411     escv_setlinecap,
412     escv_setlinejoin,
413     escv_setmiterlimit,
414     escv_setdash,
415     escv_setflat,
416     escv_setlogop,
417 	/* Other state */
418     escv_setfillcolor,	/* fill & stroke colors are the same */
419     escv_setstrokecolor,
420 	/* Paths */
421     escv_vector_dopath,
422     escv_vector_dorect,
423     escv_beginpath,
424     escv_moveto,
425     escv_lineto,
426     escv_curveto,
427     escv_closepath,
428     escv_endpath
429 };
430 
431 /* ---------------- Utilities ---------------- */
432 
433 /* Put a string on a stream.
434    This function is copy of `pputs' in gdevpstr.c */
435 private int
lputs(stream * s,const char * str)436 lputs(stream * s, const char *str)
437 {
438     uint	len = strlen(str);
439     uint	used;
440     int		status;
441 
442     status = sputs(s, (const byte *)str, len, &used);
443 
444     return (status >= 0 && used == len ? 0 : EOF);
445 }
446 
447 /* Write a string on a stream. */
448 private void
put_bytes(stream * s,const byte * data,uint count)449 put_bytes(stream * s, const byte * data, uint count)
450 {
451     uint used;
452 
453     sputs(s, data, count, &used);
454 }
455 
456 
457 private int
escv_range_check(gx_device * dev)458 escv_range_check(gx_device * dev)
459 {
460     int width = dev->MediaSize[0];
461     int height = dev->MediaSize[1];
462     int xdpi = dev->x_pixels_per_inch;
463     int ydpi = dev->y_pixels_per_inch;
464 
465     /* Paper Size Check */
466     if (width <= height) {	/* portrait */
467 	if ((width < ESCPAGE_WIDTH_MIN || width > ESCPAGE_WIDTH_MAX ||
468 	     height < ESCPAGE_HEIGHT_MIN || height > ESCPAGE_HEIGHT_MAX) &&
469 	    !(width == ESCPAGE_LEDGER_WIDTH && height == ESCPAGE_LEDGER_HEIGHT))
470 	    return_error(gs_error_rangecheck);
471     } else {			/* landscape */
472 	if ((width < ESCPAGE_HEIGHT_MIN || width > ESCPAGE_HEIGHT_MAX ||
473 	     height < ESCPAGE_WIDTH_MIN || height > ESCPAGE_WIDTH_MAX) &&
474 	    !(width == ESCPAGE_LEDGER_HEIGHT && height == ESCPAGE_LEDGER_WIDTH))
475 	    return_error(gs_error_rangecheck);
476     }
477 
478     /* Resolution Check */
479     if (xdpi != ydpi)
480 	return_error(gs_error_rangecheck);
481     else {
482 	if ((xdpi < ESCPAGE_DPI_MIN || xdpi > ESCPAGE_DPI_MAX) && xdpi != ESCPAGE_DPI_SUPERFINE)
483 	    return_error(gs_error_rangecheck);
484     }
485 
486     return 0;
487 }
488 
489 
490 /* ---------------- Vector device implementation ---------------- */
491 
492 int
escv_vector_dopath(gx_device_vector * vdev,const gx_path * ppath,gx_path_type_t type)493 escv_vector_dopath(gx_device_vector * vdev, const gx_path * ppath,
494 		   gx_path_type_t type)
495 {
496     bool do_close = (type & gx_path_type_stroke) != 0;
497     gs_fixed_rect rect;
498     gs_point scale;
499     double x_start = 0, y_start = 0, x_prev = 0, y_prev = 0;
500     bool first = true;
501     gs_path_enum cenum;
502     int code;
503 
504     stream	*s = gdev_vector_stream(vdev);
505     char	obuf[128];
506 
507     if (gx_path_is_rectangle(ppath, &rect))
508 	return (*vdev_proc(vdev, dorect)) (vdev, rect.p.x, rect.p.y, rect.q.x, rect.q.y, type);
509     scale = vdev->scale;
510     code = (*vdev_proc(vdev, beginpath)) (vdev, type);
511     gx_path_enum_init(&cenum, ppath);
512 
513     for (;;) {
514 	double	x, y;
515 	fixed	vs[6];
516 	int	pe_op, cnt;
517 	segment *pseg;
518 
519 
520 	pe_op = gx_path_enum_next(&cenum, (gs_fixed_point *) vs);
521 
522       sw:switch (pe_op) {
523 	    case 0:		/* done */
524 		return (*vdev_proc(vdev, endpath)) (vdev, type);
525 
526 	    case gs_pe_moveto:
527 		x = fixed2float(vs[0]) / scale.x;
528 		y = fixed2float(vs[1]) / scale.y;
529 
530 		/* ���֥ѥ�����̿�� p1 */
531 		(void)sprintf(obuf, ESC_GS "0;%d;%dmvpG", (int)x, (int)y);
532 		lputs(s, obuf);
533 
534 		if (first)
535 		    x_start = x, y_start = y, first = false;
536 		break;
537 
538 	    case gs_pe_lineto:
539 		cnt = 1;
540 		for (pseg = cenum.pseg; pseg != 0 && pseg->type == s_line; cnt++, pseg = pseg->next);
541 
542 		(void)sprintf(obuf, ESC_GS "0;%d", cnt);
543 		lputs(s, obuf);
544 
545 		do {
546 		    (void)sprintf(obuf, ";%d;%d", (int)(fixed2float(vs[0]) / scale.x), (int)(fixed2float(vs[1]) / scale.y));
547 		    lputs(s, obuf);
548 
549 		    pe_op = gx_path_enum_next(&cenum, (gs_fixed_point *) vs);
550 		} while (pe_op == gs_pe_lineto);
551 
552 		/* �ѥ����ݥ�饤��̿�� */
553 		lputs(s, "lnpG");
554 
555 		goto sw;
556 
557 	    case gs_pe_curveto:
558 		cnt = 1;
559 		for (pseg = cenum.pseg; pseg != 0 && pseg->type == s_curve; cnt++, pseg = pseg->next);
560 		(void)sprintf(obuf, ESC_GS "0;%d", cnt * 3);
561 		lputs(s, obuf);
562 
563 		do {
564 		    (void)sprintf(obuf, ";%d;%d;%d;%d;%d;%d",
565 				  (int)(fixed2float(vs[0]) / scale.x), (int)(fixed2float(vs[1]) / scale.y),
566 				  (int)(fixed2float(vs[2]) / scale.x), (int)(fixed2float(vs[3]) / scale.y),
567 				  (int)(fixed2float(vs[4]) / scale.x), (int)(fixed2float(vs[5]) / scale.y));
568 		    lputs(s, obuf);
569 
570 		    pe_op = gx_path_enum_next(&cenum, (gs_fixed_point *) vs);
571 		} while (pe_op == gs_pe_curveto);
572 
573 		/* �٥������� */
574 		lputs(s, "bzpG");
575 
576 		goto sw;
577 
578 	    case gs_pe_closepath:
579 		x = x_start, y = y_start;
580 		if (do_close) {
581 		    lputs(s, ESC_GS "clpG");
582 		    break;
583 		}
584 
585 		pe_op = gx_path_enum_next(&cenum, (gs_fixed_point *) vs);
586 		if (pe_op != 0) {
587 		    lputs(s, ESC_GS "clpG");
588 
589 		    if (code < 0)
590 			return code;
591 		    goto sw;
592 		}
593 		return (*vdev_proc(vdev, endpath)) (vdev, type);
594 	    default:		/* can't happen */
595 		return_error(gs_error_unknownerror);
596 	}
597 	if (code < 0)
598 	    return code;
599 	x_prev = x, y_prev = y;
600     }
601 }
602 
603 
604 int
escv_vector_dorect(gx_device_vector * vdev,fixed x0,fixed y0,fixed x1,fixed y1,gx_path_type_t type)605 escv_vector_dorect(gx_device_vector * vdev, fixed x0, fixed y0, fixed x1,
606 		   fixed y1, gx_path_type_t type)
607 {
608     int		code;
609     char	obuf[128];
610     gs_point	scale;
611     stream	*s = gdev_vector_stream(vdev);
612 
613     code = (*vdev_proc(vdev, beginpath))(vdev, type);
614     if (code < 0)
615 	return code;
616 
617     scale = vdev->scale;
618 
619     (void)sprintf(obuf, ESC_GS "0;%d;%d;%d;%d;0;0rrpG", (int)(fixed2float(x0) / scale.x), (int)(fixed2float(y0) / scale.y), (int)(fixed2float(x1) / scale.x), (int)(fixed2float(y1) / scale.y));
620     lputs(s, obuf);
621 
622 #if 0
623     /* Ghostscript ¦�ΥХ��� closepath ��Ƥ�Ǥ��ʤ��Τǽ������碌�롣 */
624 
625     /* ����� (*vdev_proc(vdev, closepath))() ��Ƥ֤٤� */
626     lputs(s, ESC_GS "clpG");
627 #endif
628 
629     return (*vdev_proc(vdev, endpath))(vdev, type);
630 }
631 
632 
633 /* ---------- */
634 
635 
636 typedef struct {
637     int		width;			/* paper width (unit: point) */
638     int		height;			/* paper height (unit: point) */
639     int		escpage;		/* number of papersize in ESC/PAGE */
640     char	*name;			/* Paper Name */
641 } EPaperTable;
642 
643 private const EPaperTable ePaperTable[24] =
644 {
645     {933, 1369, 72, "A3PLUS"},		/* A3 NOBI */
646     {842, 1190, 13, "A3"},		/* A3 */
647     {595, 842, 14, "A4"},		/* A4 */
648     {421, 595, 15, "A5"},		/* A5 */
649     {709, 1002, 24, "B4"},		/* B4 */
650     {729, 1032, 24, "B4"},		/* B4 JIS */
651     {516, 729, 25, "B5"},		/* B5 JIS */
652     {612, 792, 30, "LT"},		/* Letter */
653     {396, 612, 31, "HLT"},		/* Half Letter */
654     {612, 1008, 32, "LGL"},		/* Legal */
655     {522, 756, 33, "EXE"},		/* Executive */
656     {612, 936, 34, "GLG"},		/* Government Letter */ /* LPD.1. */
657     {576, 756, 35, "GLT"},		/* Government Legal */ /* LPD.1. */
658     {792, 1224, 36, "B"},		/* Ledger */
659     {593, 935, 37, "F4"},		/* F4 */
660     {284, 419, 38, "POSTCARD"},		/* PostCard */
661     {298, 666, 64, "YOU4"},             /* Japanese Envelope You4 */
662     {279, 540, 80, "MON"},		/* Monarch */
663     {297, 684, 81, "C10"},		/* Commercial 10 */
664     {312, 624, 90, "DL"},		/* DL */
665     {459, 649, 91, "C5"},               /* Envelope C5 */
666     {501, 709, 99, "IB5"},              /* Envelope ISO B5 */
667     {0, 0, -1, NULL}			/* Undefined */
668 };
669 
670 private int
671 
escv_beginpage(gx_device_vector * vdev)672 escv_beginpage(gx_device_vector * vdev)
673 {
674     gx_device_escv	*const pdev = (gx_device_escv *)vdev;
675     /* gdev_vector_stream �ϻ��Ѷػ� */
676     stream		*s = vdev->strm;
677     char		ebuf[1024];
678     int			w, h;
679     EPaperTable		*pt;
680     int                 MaxRes;
681     int                 Local;
682     int                 Duplex;
683     int                 FaceUp;
684 
685     if (pdev -> first_page) {
686 
687 	if (strcmp(pdev->dname, "lp8000c") == 0 || \
688 	    strcmp(pdev->dname, "lp8200c")  == 0 ) {
689 	    MaxRes = RES600;
690 	    Local  = JPN;
691 	    Duplex = FALSE;
692 	    FaceUp = TRUE;
693 	} else if (strcmp(pdev->dname, "lp8300c") == 0 || \
694 	    strcmp(pdev->dname, "lp8500c")  == 0 || \
695 	    strcmp(pdev->dname, "lp8800c")  == 0 || \
696 	    strcmp(pdev->dname, "lp9500c")  == 0 || \
697 	    strcmp(pdev->dname, "lp9800c")  == 0 ) {
698 	    MaxRes = RES600;
699 	    Local  = JPN;
700 	    Duplex = TRUE;
701 	    FaceUp = TRUE;
702 	} else if (strcmp(pdev->dname, "lp3000c") == 0 || \
703 	    strcmp(pdev->dname, "lp9000c")  == 0 ) {
704 	    MaxRes = RES600;
705 	    Local  = JPN;
706 	    Duplex = TRUE;
707 	    FaceUp = FALSE;
708 	} else if (strcmp(pdev->dname, "alc8500") == 0 || \
709 	    strcmp(pdev->dname, "alc8600")  == 0 ) {
710 	    MaxRes = RES600;
711 	    Local  = ENG;
712 	    Duplex = TRUE;
713 	    FaceUp = TRUE;
714 	} else if (strcmp(pdev->dname, "alc2000") == 0 || \
715 	    strcmp(pdev->dname, "alc4100")  == 0 ) {
716 	    MaxRes = RES600;
717 	    Local  = ENG;
718 	    Duplex = TRUE;
719 	    FaceUp = FALSE;
720 	} else if (strcmp(pdev->dname, "alc4000") == 0) {
721 	    MaxRes = RES1200;
722 	    Local  = ENG;
723 	    Duplex = TRUE;
724 	    FaceUp = FALSE;
725 	} else if (strcmp(pdev->dname, "alc1900") == 0) {
726 	    MaxRes = RES600;
727 	    Local  = ENG;
728 	    Duplex = TRUE;
729 	    FaceUp = FALSE;
730 	} else {
731 	    MaxRes = RES600;
732 	    Local  = JPN;
733 	    Duplex = FALSE;
734 	    FaceUp = FALSE;
735 	}
736 
737 	lputs(s, "\033\001@EJL \012@EJL SJ ID=\"Ghostscript\"\012");
738 	lputs(s, "@EJL SE LA=ESC/PAGE\012");
739 	lputs(s, "@EJL SET");
740 	/* Resolusion */
741 	if (vdev->x_pixels_per_inch == 1200){
742 	    if (MaxRes == 1200){
743 	    	lputs(s, " RS=1200");
744 	    } else {
745 	    	lputs(s, " RS=FN");
746 	    }
747 	} else if (vdev->x_pixels_per_inch == 600) {
748 	    lputs(s, " RS=FN");
749 	} else {
750 	    lputs(s, " RS=QK");
751 	}
752 
753 	/* Output Unit */
754 	if ((pdev->faceup && FaceUp) || (pdev->MediaType && FaceUp)) {
755 	    lputs(s, " OU=FU");
756 	} else {
757 	    lputs(s, " OU=FD");
758 	}
759 
760 	/* Paper unit */
761         if (pdev->MediaType){
762 	    if (Local == ENG){
763 		lputs(s, " PU=1");
764 	    } else {
765 		lputs(s, " PU=15");
766 	    }
767 	}else{
768 	    if (pdev->manualFeed) {
769 		if (Local == ENG){
770 		    lputs(s, " PU=1");
771 		} else {
772 	            lputs(s, " PU=15");
773 		}
774 	    } else if (pdev->cassetFeed) {
775 	        (void)sprintf(ebuf, " PU=%d", pdev->cassetFeed);
776 	        lputs(s, ebuf);
777 	    } else {
778 	        lputs(s, " PU=AU");
779 	    }
780 	}
781 
782 	if (Duplex && pdev->Duplex) {
783 	    /* ξ�̰������� */
784 	    lputs(s, " DX=ON");
785 
786 	    /* �Ȥ����� */
787 	    if (pdev->Tumble) {
788 		lputs(s, " BD=SE");
789 	    } else {
790 		lputs(s, " BD=LE");
791 	    }
792         } else {
793 	    /* ξ�̰���off */
794 	    lputs(s, " DX=OFF");
795 	}
796 
797 	/* �������� */
798 	if (pdev->NumCopies) {
799 	    if (pdev->NumCopies >= 1000) {
800 		pdev->NumCopies = 999;
801 	    }
802 
803 	    /* lp8000c �Ǥ� QT ���Ȥ��ʤ� */
804 	    if (strcmp(pdev->dname, "lp8000c") == 0) {
805 		(void)sprintf(ebuf, " QT=1 CO=%d", pdev->NumCopies);
806 	    } else {
807                 if (pdev->Collate) {
808 		    /* QT ����ꤷ����� CO �� 1 */
809 		    (void)sprintf(ebuf, " QT=%d CO=1", pdev->NumCopies);
810 	        } else {
811 		    /* QT ����ꤷ�ʤ��ä���� QT �� 1 */
812 		    (void)sprintf(ebuf, " QT=1 CO=%d", pdev->NumCopies);
813 		}
814 	    }
815 	    lputs(s, ebuf);
816 	} else {
817 	    lputs(s, " QT=1 CO=1");
818 	}
819 
820 	if (pdev->toner_density) {
821 	    (void)sprintf(ebuf, " DL=%d", pdev->toner_density);
822 	    lputs(s, ebuf);
823 	}
824 
825 	if (pdev->orientation) {
826 	    lputs(s, " OR=LA");
827 	}
828 
829 	if (pdev->toner_saving) {
830 	    lputs(s, " SN=ON");
831 	}
832 
833 	if (pdev->RITOff) {
834 	    lputs(s, " RI=OFF");
835 	} else {
836 	    lputs(s, " RI=ON");
837 	}
838 
839 	if (pdev->MediaType == 1) {
840 	    lputs(s, " PK=TH");
841 	} else if (pdev->MediaType == 2) {
842 	    lputs(s, " PK=TR");
843 	} else {
844 	    lputs(s, " PK=NM");
845 	}
846 
847 	if (pdev->MediaSize[0] < pdev->MediaSize[1]) {
848 	    w = pdev->MediaSize[0];
849 	    h = pdev->MediaSize[1];
850 	} else {
851 	    w = pdev->MediaSize[1];
852 	    h = pdev->MediaSize[0];
853 	}
854 
855 	for (pt = ePaperTable; pt->escpage > 0; pt++)
856 	    if (pt->width == w && pt->height == h)
857 		break;
858 	if (pt->name == NULL) {
859 	    lputs(s, " PS=A4");
860 	} else {
861 	    (void)sprintf(ebuf, " PS=%s", pt->name);
862 	    lputs(s, ebuf);
863 	}
864 
865 	lputs(s, " ZO=OFF EC=ON SZ=OFF SL=YES TO=0 LO=0\012");
866 	lputs(s, "@EJL EN LA=ESC/PAGE-COLOR\012");
867 
868 #define	START_CODE1	ESC_GS "1tsE" ESC_GS "0alfP" ESC_GS "0affP" ESC_GS "0;0;0clfP" ESC_GS "0pmP" ESC_GS "1024ibI" ESC_GS "2cmE" ESC_GS "0bcI" ESC_GS "1;10mlG"
869 
870 #define	LP8000_CODE	ESC_GS "0pddO" ESC_GS "0;0mmE" ESC_GS "2csE" ESC_GS "0;1;3cmmE" ESC_GS "0;1raE" ESC_GS "0;2;4ccmE"
871 
872 #define	LP8200_CODE	ESC_GS "0pddO" ESC_GS "0;0cmmE" ESC_GS "1;2;3ccmE" ESC_GS "2;2;3ccmE" ESC_GS "3;2;4ccmE" ESC_GS "1;1raE" ESC_GS "2;1raE" ESC_GS "3;1raE"
873 
874 	lputs(s, ESC_GS "rhE");
875 
876 	if (strcmp(vdev -> dname, "lp8000c") == 0) {
877 	    lputs(s, LP8000_CODE);
878 	} else {
879 	    lputs(s, LP8200_CODE);
880 	}
881 
882 #if 1	/* default */
883 	put_bytes(s, ESC_GS "7;0;2;0cam{E\012\000\000\000\000\000\000", 20);
884 	lputs(s, ESC_GS "0;0cmmE" ESC_GS "0;0loE");
885 #else
886 	put_bytes(s, ESC_GS "7;0;2;3cam{E\012\000\000\000\000\000\000", 20);
887 	lputs(s, ESC_GS "1cmmE" ESC_GS "0;0loE");
888 #endif
889         if (vdev->x_pixels_per_inch == 1200) {
890             /* 1200 dpi */
891             lputs(s, ESC_GS "0;0.06muE");
892             lputs(s, ESC_GS "3;1200;1200drE" ESC_GS "2;1200;1200drE" ESC_GS "1;1200;1200drE" ESC_GS "0;1200;1200drE");
893         } else if (vdev->x_pixels_per_inch == 600) {
894 	    /* 600 dpi */
895 	    lputs(s, ESC_GS "0;0.12muE");
896 	    lputs(s, ESC_GS "3;600;600drE" ESC_GS "2;600;600drE" ESC_GS "1;600;600drE" ESC_GS "0;600;600drE");
897 	} else {
898 	    /* 300 dpi */
899 	    lputs(s, ESC_GS "0;0.24muE");
900 	    lputs(s, ESC_GS "3;300;300drE" ESC_GS "2;300;300drE" ESC_GS "1;300;300drE" ESC_GS "0;300;300drE");
901 	}
902 
903 	lputs(s, START_CODE1);
904 	lputs(s, ESC_GS "8;1;2;2;2plr{E");
905 	put_bytes(s, "\377\377\377\377\000\000\000\000", 8);
906 
907 	lputs(s, ESC_GS "0sarG");		/* ���к�ɸ���� */
908 	lputs(s, ESC_GS "2;204wfE");		/* rop ���� */
909     }
910 
911     return 0;
912 }
913 
914 
915 private int
escv_setlinewidth(gx_device_vector * vdev,floatp width)916 escv_setlinewidth(gx_device_vector * vdev, floatp width)
917 {
918     stream			*s = gdev_vector_stream(vdev);
919     gx_device_escv *const	pdev = (gx_device_escv *) vdev;
920     char			obuf[64];
921 
922 #if GS_VERSION_MAJOR == 5
923     /* Scale ��ݤ��Ƥ���Τ�, Ghostscript 5.10/5.50 �ΥХ��Τ��� */
924     floatp xscale, yscale;
925 
926     xscale = fabs(igs->ctm.xx);
927     yscale = fabs(igs->ctm.xy);
928 
929     if (xscale == 0 || yscale > xscale)		/* if portrait */
930 	width = ceil(width * yscale);
931     else
932 	width = ceil(width * xscale);
933 #endif
934 
935     if (width < 1) width = 1;
936 
937     /* ESC/Page �Ǥ���������ü���ܹ���������ϣ��ĤΥ��ޥ�ɤˤʤäƤ��뤿���ݻ����Ƥ����� */
938     pdev -> lwidth = width;
939 
940     (void)sprintf(obuf, ESC_GS "%d;%d;%dlwG", (int)(pdev -> lwidth), (int)(pdev -> cap), (int)(pdev -> join));
941     lputs(s, obuf);
942 
943     return 0;
944 }
945 
946 
947 private int
escv_setlinecap(gx_device_vector * vdev,gs_line_cap cap)948 escv_setlinecap(gx_device_vector * vdev, gs_line_cap cap)
949 {
950     stream			*s = gdev_vector_stream(vdev);
951     gx_device_escv *const	pdev = (gx_device_escv *) vdev;
952     char			obuf[64];
953 
954     /* ESC/Page �Ǥ���������ü���ܹ���������ϣ��ĤΥ��ޥ�ɤˤʤäƤ��뤿���ݻ����Ƥ����� */
955     pdev -> cap = cap;
956 
957     if (pdev -> cap >= 3) return -1;
958 
959     (void)sprintf(obuf, ESC_GS "%d;%d;%dlwG", (int)(pdev -> lwidth), (int)(pdev -> cap), (int)(pdev -> join));
960     lputs(s, obuf);
961 
962     return 0;
963 }
964 
965 
966 private int
escv_setlinejoin(gx_device_vector * vdev,gs_line_join join)967 escv_setlinejoin(gx_device_vector * vdev, gs_line_join join)
968 {
969     stream			*s = gdev_vector_stream(vdev);
970     gx_device_escv *const	pdev = (gx_device_escv *) vdev;
971     char			obuf[64];
972 
973     /* ESC/Page �Ǥ���������ü���ܹ���������ϣ��ĤΥ��ޥ�ɤˤʤäƤ��뤿���ݻ����Ƥ����� */
974     switch (join) {
975     case 0:
976 	pdev -> join = 3;		/* miter */
977 	break;
978     case 1:
979 	pdev -> join = 1;		/* round */
980 	break;
981     case 2:
982 	pdev -> join = 2;		/* bevel */
983 	break;
984     default:
985 	return -1;
986     }
987 
988     (void)sprintf(obuf, ESC_GS "%d;%d;%dlwG", (int)(pdev -> lwidth), (int)(pdev -> cap), (int)(pdev -> join));
989     lputs(s, obuf);
990 
991     return 0;
992 }
993 
994 
995 private int
escv_setmiterlimit(gx_device_vector * vdev,floatp limit)996 escv_setmiterlimit(gx_device_vector * vdev, floatp limit)
997 {
998     stream			*s = gdev_vector_stream(vdev);
999     gx_device_escv *const	pdev = (gx_device_escv *) vdev;
1000     char			obuf[128];
1001 
1002     /* �ޥ�������ߥå��ͤ����ꤹ��ˤ� lwG �ˤ� �ܹ�������(n3) �� 3 �ˤʤäƤ���
1003     ** ɬ�פ����롣
1004     */
1005     if (pdev -> join != 3) {
1006 	/* ����Ū���ܹ��������Ԥ� */
1007 	pdev -> join = 3;
1008 	(void)sprintf(obuf, ESC_GS "%d;%d;%dlwG", (int)(pdev -> lwidth), (int)(pdev -> cap), (int)(pdev -> join));
1009 	lputs(s, obuf);
1010     }
1011 
1012     (void)sprintf(obuf, ESC_GS "1;%dmlG", (int)limit);
1013     lputs(s, obuf);
1014 
1015     return 0;
1016 }
1017 
1018 
1019 private int
escv_setfillcolor(gx_device_vector * vdev,const gx_drawing_color * pdc)1020 escv_setfillcolor(gx_device_vector * vdev, const gx_drawing_color * pdc)
1021 {
1022     stream			*s = gdev_vector_stream(vdev);
1023     gx_device_escv *const	pdev = (gx_device_escv *) vdev;
1024     gx_color_index		color = gx_dc_pure_color(pdc);
1025     char			obuf[64];
1026 
1027     if (!gx_dc_is_pure(pdc)) return_error(gs_error_rangecheck);
1028     pdev->current_color = color;
1029 
1030     /* �ѥ�����ϣλ��꡿����åɥѥ�������� */
1031     (void)sprintf(obuf, ESC_GS "1;2;3;%d;%d;%dfpE",
1032 		  (unsigned char)(color >> 16 & 0xff),
1033 		  (unsigned char)(color >> 8 & 0xff),
1034 		  (unsigned char)(color & 0xff));
1035     lputs(s, obuf);
1036     lputs(s, ESC_GS "3;2;1;0;0cpE" ESC_GS "1;2;1;0;0cpE" ESC_GS "5;2;1;0;0cpE");
1037 
1038     return 0;
1039 }
1040 
1041 
1042 private int
escv_setstrokecolor(gx_device_vector * vdev,const gx_drawing_color * pdc)1043 escv_setstrokecolor(gx_device_vector * vdev, const gx_drawing_color * pdc)
1044 {
1045     stream			*s = gdev_vector_stream(vdev);
1046     gx_device_escv *const	pdev = (gx_device_escv *) vdev;
1047     gx_color_index		color = gx_dc_pure_color(pdc);
1048     char			obuf[64];
1049 
1050     if (!gx_dc_is_pure(pdc)) return_error(gs_error_rangecheck);
1051 
1052     if (vdev->color_info.depth == 24) {
1053 
1054 	pdev->current_color = color;
1055 	/* �ѥ�����ϣο����꡿����åɥѥ�������� */
1056 	(void)sprintf(obuf, ESC_GS "1;2;3;%d;%d;%dfpE" ESC_GS "2;2;1;0;0cpE",
1057 		      (unsigned char)(color >> 16 & 0xff),
1058 		      (unsigned char)(color >> 8 & 0xff),
1059 		      (unsigned char)(color & 0xff));
1060 	lputs(s, obuf);
1061 
1062     }
1063     return 0;
1064 }
1065 
1066 
1067 /* �������̿�� */
1068 private int
escv_setdash(gx_device_vector * vdev,const float * pattern,uint count,floatp offset)1069 escv_setdash(gx_device_vector * vdev, const float *pattern, uint count, floatp offset)
1070 {
1071     stream			*s = gdev_vector_stream(vdev);
1072     int				i;
1073     char			obuf[64];
1074 
1075 #if GS_VERSION_MAJOR == 5
1076     float			scale, xscale, yscale;
1077     /* Scale ��ݤ��Ƥ���Τ�, Ghostscript 5.10/5.50 �ΥХ��Τ��� */
1078     xscale = fabs(igs->ctm.xx);
1079     yscale = fabs(igs->ctm.xy);
1080 
1081     if (xscale == 0)		/* if portrait */
1082 	scale = yscale;
1083     else
1084 	scale = xscale;
1085 #endif
1086 
1087     if (count == 0){
1088 	/* ���� */
1089 	lputs(s, ESC_GS "0;0lpG");
1090 	return 0;
1091     }
1092 
1093     /* offset �����ʳ��ξ��������ԲĤȤ����ֵ� */
1094     if (offset != 0) return -1;
1095 
1096     if (count) {
1097 	if (count == 1) {
1098 #if GS_VERSION_MAJOR == 5
1099 	    (void)sprintf(obuf, ESC_GS "1;%d;%ddlG",
1100 			  (int)(pattern[0] * scale / vdev->x_pixels_per_inch + 0.5),
1101 			  (int)(pattern[0] * scale / vdev->x_pixels_per_inch + 0.5));
1102 #else
1103 	    (void)sprintf(obuf, ESC_GS "1;%d;%ddlG", pattern[0], pattern[0]);
1104 #endif
1105 	    lputs(s, obuf);
1106 	} else {
1107 	    /* pattern �ˣ������ä����������ԲĤȤ����ֵ� */
1108 	    for (i = 0; i < count; ++i) {
1109 		if (pattern[i] == 0) return -1;
1110 	    }
1111 
1112 	    lputs(s, ESC_GS "1");
1113 	    for (i = 0; i < count; ++i) {
1114 #if GS_VERSION_MAJOR == 5
1115 		(void)sprintf(obuf, ";%d", (int)(pattern[i] * scale / vdev->x_pixels_per_inch + 0.5));
1116 
1117 #else
1118 		(void)sprintf(obuf, ";%d", pattern[i]);
1119 #endif
1120 		lputs(s, obuf);
1121 	    }
1122 	    lputs(s, "dlG");
1123 	}
1124 	lputs(s, ESC_GS "1;1lpG");
1125     }
1126     return 0;
1127 }
1128 
1129 
1130 /* �ѥ�ʿ���ٻ��� */
1131 private int
escv_setflat(gx_device_vector * vdev,floatp flatness)1132 escv_setflat(gx_device_vector * vdev, floatp flatness)
1133 {
1134     return 0;
1135 }
1136 
1137 
1138 private int
escv_setlogop(gx_device_vector * vdev,gs_logical_operation_t lop,gs_logical_operation_t diff)1139 escv_setlogop(gx_device_vector * vdev, gs_logical_operation_t lop,
1140 		gs_logical_operation_t diff)
1141 {
1142 /****** SHOULD AT LEAST DETECT SET-0 & SET-1 ******/
1143     return 0;
1144 }
1145 
1146 
1147 private int
escv_beginpath(gx_device_vector * vdev,gx_path_type_t type)1148 escv_beginpath(gx_device_vector * vdev, gx_path_type_t type)
1149 {
1150     stream		*s = gdev_vector_stream(vdev);
1151 
1152     /* �ѥ����۳���̿�� */
1153     if (type & gx_path_type_clip) {
1154 	lputs(s, ESC_GS "1bgpG");		/* ����å���Ͽ */
1155     } else {
1156 	lputs(s, ESC_GS "0bgpG");		/* ������Ͽ */
1157     }
1158     return 0;
1159 }
1160 
1161 
1162 private int
escv_moveto(gx_device_vector * vdev,floatp x0,floatp y0,floatp x1,floatp y1,gx_path_type_t type)1163 escv_moveto(gx_device_vector * vdev,
1164 	    floatp x0, floatp y0, floatp x1, floatp y1, gx_path_type_t type)
1165 {
1166     stream	*s = gdev_vector_stream(vdev);
1167     char	obuf[64];
1168 
1169     /* ���֥ѥ�����̿�� */
1170     (void)sprintf(obuf, ESC_GS "0;%d;%dmvpG", (int)x1, (int)y1);
1171     lputs(s, obuf);
1172 
1173     return 0;
1174 }
1175 
1176 
1177 private int
escv_lineto(gx_device_vector * vdev,floatp x0,floatp y0,floatp x1,floatp y1,gx_path_type_t type)1178 escv_lineto(gx_device_vector * vdev,
1179 	    floatp x0, floatp y0, floatp x1, floatp y1, gx_path_type_t type)
1180 {
1181     stream	*s = gdev_vector_stream(vdev);
1182     char	obuf[64];
1183 
1184     (void)sprintf(obuf, ESC_GS "0;1;%d;%dlnpG", (int)x1, (int)y1);
1185     lputs(s, obuf);
1186 
1187     return 0;
1188 }
1189 
1190 
1191 private int
escv_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)1192 escv_curveto(gx_device_vector * vdev, floatp x0, floatp y0,
1193 	   floatp x1, floatp y1, floatp x2, floatp y2, floatp x3, floatp y3,
1194 	       gx_path_type_t type)
1195 {
1196     stream	*s = gdev_vector_stream(vdev);
1197     char	obuf[128];
1198 
1199     /* �٥������� */
1200     (void)sprintf(obuf, ESC_GS "0;3;%d;%d;%d;%d;%d;%dbzpG",
1201 		  (int)x1, (int)y1, (int)x2, (int)y2, (int)x3, (int)y3);
1202     lputs(s, obuf);
1203 
1204     return 0;
1205 }
1206 
1207 
1208 private int
escv_closepath(gx_device_vector * vdev,floatp x,floatp y,floatp x_start,floatp y_start,gx_path_type_t type)1209 escv_closepath(gx_device_vector * vdev, floatp x, floatp y,
1210 		 floatp x_start, floatp y_start, gx_path_type_t type)
1211 {
1212     stream	*s = gdev_vector_stream(vdev);
1213 
1214     lputs(s, ESC_GS "clpG");
1215     return 0;
1216 }
1217 
1218 
1219 private int
escv_endpath(gx_device_vector * vdev,gx_path_type_t type)1220 escv_endpath(gx_device_vector * vdev, gx_path_type_t type)
1221 {
1222     stream	*s = gdev_vector_stream(vdev);
1223 
1224     if (type & gx_path_type_fill || type & gx_path_type_clip) {
1225 	/* default �ǽ�������뤬���Ϥ��Ƥ��� */
1226 	lputs(s, ESC_GS "clpG");
1227     }
1228 
1229     /* �ѥ������� */
1230     lputs(s, ESC_GS "enpG");
1231 
1232     /* �ѥ����� */
1233     if (type & gx_path_type_clip) {
1234 
1235 	/* ����å׻���
1236 	** ����åפˤ� gx_path_type_winding_number, gx_path_type_even_odd ��Ƚ�Ǥ�
1237 	** ɬ�פ��Ȼפ��� gs ¦���ղä��Ƥ��ʤ���
1238 	** �Ȥꤢ���� gx_path_type_even_odd ��ǥե���Ȥˤ��롣
1239 	*/
1240 	lputs(s, ESC_GS "1;2capG");
1241     } else if (type & gx_path_type_fill) {
1242 
1243 	/* �ɤ�Ĥ֤���§���� */
1244 	if (type & gx_path_type_even_odd) {
1245 	    lputs(s, ESC_GS "0;2drpG");		/* �ɤ�Ĥ֤����� */
1246 	} else {
1247 	    lputs(s, ESC_GS "0;1drpG");		/* �ɤ�Ĥ֤����� */
1248 	}
1249     } else {
1250 	lputs(s, ESC_GS "0;0drpG");		/* �س������� */
1251     }
1252 
1253     return 0;
1254 }
1255 
1256 /* ---------------- Driver procedures ---------------- */
1257 
1258 /* ------ Open/close/page ------ */
1259 
1260 /* Open the device. */
1261 private int
escv_open(gx_device * dev)1262 escv_open(gx_device * dev)
1263 {
1264     gx_device_vector	*const vdev = (gx_device_vector *) dev;
1265     gx_device_escv	*const pdev = (gx_device_escv *) dev;
1266     int			code;
1267     /*    char		*error, *path;*/
1268     float               width, height;
1269 
1270     code = escv_range_check(dev);
1271     if (code < 0) return code;
1272 
1273     vdev->v_memory = dev->memory;
1274     /****** VERY WRONG ******/
1275     vdev->vec_procs = &escv_vector_procs;
1276 
1277     code = gdev_vector_open_file_bbox(vdev, 512, true);
1278     if (code < 0) return code;
1279 
1280     gdev_vector_init(vdev);
1281     pdev->first_page = true;
1282 
1283     if(pdev->orientation){
1284 /*    pdev->Margins[1] = pdev->width - pdev->height + dev->HWMargins[0];
1285  */
1286       pdev->Margins[1] = (pdev->width - pdev->height) * \
1287       X_DPI / vdev->x_pixels_per_inch;
1288 
1289       width = dev->MediaSize[0];
1290       height  = dev->MediaSize[1];
1291       dev->MediaSize[0] = height;
1292       dev->MediaSize[1] = width;
1293     }
1294 
1295     return 0;
1296 }
1297 
1298 
1299 /* Wrap up ("output") a page. */
1300 private int
escv_output_page(gx_device * dev,int num_copies,int flush)1301 escv_output_page(gx_device * dev, int num_copies, int flush)
1302 {
1303     gx_device_vector *const vdev = (gx_device_vector *) dev;
1304     gx_device_escv *const pdev = (gx_device_escv *) dev;
1305     stream *s = gdev_vector_stream(vdev);
1306 
1307     /* ����,��ü����,�ܹ����������������Ƥ��� */
1308     lputs(s, ESC_GS "3;0;0lwG" ESC_GS "1;10mlG" ESC_FF);
1309 
1310     sflush(s);
1311     vdev->in_page = false;
1312     pdev->first_page = false;
1313 
1314     gdev_vector_reset(vdev);
1315 
1316     return 0;
1317 }
1318 
1319 
1320 private int
escv_close(gx_device * dev)1321 escv_close(gx_device *dev)
1322 {
1323     gx_device_vector	*const vdev = (gx_device_vector *) dev;
1324     FILE		*f = vdev->file;
1325 
1326     /* ��λ���������ɤϷ���Ǥ� */
1327     (void)fprintf(f, ESC_GS "rhE" "\033\001@EJL \012@EJL EJ \012\033\001@EJL \012");
1328 
1329     gdev_vector_close_file(vdev);
1330 
1331     return 0;
1332 }
1333 
1334 /* Close the device. */
1335 /* Note that if this is being called as a result of finalization, */
1336 /* the stream may no longer exist; but the file will still be open. */
1337 
1338 /* ---------------- Get/put parameters ---------------- */
1339 
1340 /* Get parameters. */
1341 private int
escv_get_params(gx_device * dev,gs_param_list * plist)1342 escv_get_params(gx_device * dev, gs_param_list * plist)
1343 {
1344     gx_device_escv	*const pdev = (gx_device_escv *) dev;
1345     int			code;
1346     int			ncode;
1347 
1348     code = gdev_vector_get_params(dev, plist);
1349     if (code < 0) return code;
1350 
1351     if ((ncode = param_write_bool(plist, ESCPAGE_OPTION_MANUALFEED, &pdev->manualFeed)) < 0)
1352 	code = ncode;
1353 
1354     if ((ncode = param_write_int(plist, ESCPAGE_OPTION_CASSETFEED, &pdev->cassetFeed)) < 0)
1355 	code = ncode;
1356 
1357     if ((ncode = param_write_bool(plist, ESCPAGE_OPTION_RIT, &pdev->RITOff)) < 0)
1358 	code = ncode;
1359 
1360     if ((ncode = param_write_bool(plist, ESCPAGE_OPTION_COLLATE, &pdev->Collate)) < 0)
1361         code = ncode;
1362 
1363     if ((ncode = param_write_int(plist, ESCPAGE_OPTION_TONERDENSITY, &pdev->toner_density)) < 0)
1364         code = ncode;
1365 
1366     if ((ncode = param_write_bool(plist, ESCPAGE_OPTION_LANDSCAPE, &pdev->orientation)) < 0)
1367         code = ncode;
1368 
1369     if ( param_write_bool(plist, ESCPAGE_OPTION_TONERSAVING, &pdev->toner_saving)< 0)
1370         code = ncode;
1371 
1372     if ((ncode = param_write_bool(plist, ESCPAGE_OPTION_DUPLEX, &pdev->Duplex)) < 0)
1373 	code = ncode;
1374 
1375     if ((ncode = param_write_bool(plist, ESCPAGE_OPTION_DUPLEX_TUMBLE, &pdev->Tumble)) < 0)
1376 	code = ncode;
1377 
1378     if ((ncode = param_write_bool(plist, ESCPAGE_OPTION_FACEUP, &pdev->faceup)) < 0)
1379 	code = ncode;
1380 
1381     if ((ncode = param_write_int(plist, ESCPAGE_OPTION_MEDIATYPE, &pdev->MediaType)) < 0)
1382 	code = ncode;
1383 
1384     return code;
1385 }
1386 
1387 
1388 /* Put parameters. */
1389 private int
escv_put_params(gx_device * dev,gs_param_list * plist)1390 escv_put_params(gx_device * dev, gs_param_list * plist)
1391 {
1392     gx_device_escv	*const pdev = (gx_device_escv *) dev;
1393     int			ecode = 0;
1394     int			code;
1395     gs_param_name	param_name;
1396     gs_param_string	pmedia;
1397     bool		mf = pdev->manualFeed;
1398     int			cass = pdev->cassetFeed;
1399     bool		tum = pdev->Tumble;
1400     bool		collate = pdev->Collate;
1401     int			toner_density = pdev->toner_density;
1402     bool		toner_saving = pdev->toner_saving;
1403     bool		landscape = pdev->orientation;
1404     bool		faceup = pdev->faceup;
1405     bool		duplex = pdev->Duplex;
1406     bool		RITOff = pdev->RITOff;
1407     int			old_bpp = dev->color_info.depth;
1408     int			bpp = 0;
1409 
1410     if ((code = param_read_bool(plist, (param_name = ESCPAGE_OPTION_MANUALFEED), &mf)) < 0) {
1411 	param_signal_error(plist, param_name, ecode = code);
1412     }
1413     switch (code = param_read_int(plist, (param_name = ESCPAGE_OPTION_CASSETFEED), &cass)) {
1414 	case 0:
1415 	    if (cass < -1 || cass > 15)
1416 		ecode = gs_error_limitcheck;
1417 	    else
1418 		break;
1419 	    goto casse;
1420 	default:
1421 	    ecode = code;
1422 	  casse:param_signal_error(plist, param_name, ecode);
1423 	case 1:
1424 	    break;
1425     }
1426 
1427     if((code = param_read_bool(plist, (param_name = ESCPAGE_OPTION_COLLATE), &collate)) < 0) {
1428 	param_signal_error(plist, param_name, ecode = code);
1429     }
1430 
1431     if ((code = param_read_bool(plist, (param_name = ESCPAGE_OPTION_RIT), &RITOff)) < 0) {
1432 	param_signal_error(plist, param_name, ecode = code);
1433     }
1434 
1435     switch (code = param_read_string(plist, (param_name = ESCPAGE_OPTION_MEDIATYPE), &pmedia)) {
1436 	case 0:
1437 	    if (pmedia.size > ESCPAGE_MEDIACHAR_MAX)
1438 		ecode = gs_error_limitcheck;
1439 	    else {		/* Check the validity of ``MediaType'' characters */
1440 
1441 		if (strcmp(pmedia.data, "TRANS") == 0) {
1442 		    pdev->MediaType = 2;
1443 		} else if (strcmp(pmedia.data, "THICK") == 0) {
1444 		    pdev->MediaType = 1;
1445 		} else {
1446 		    ecode = gs_error_rangecheck;
1447 		    goto pmediae;
1448 		}
1449 	    }
1450 	    break;
1451 	    goto pmediae;
1452 	default:
1453 	    ecode = code;
1454 	  pmediae:param_signal_error(plist, param_name, ecode);
1455 	case 1:
1456 	  if(!pdev->MediaType){
1457 	    pdev->MediaType = 0;
1458 	    pmedia.data = 0;
1459 	  }
1460 	  break;
1461     }
1462 
1463     switch (code = param_read_int(plist,
1464                                   (param_name = ESCPAGE_OPTION_TONERDENSITY), &toner_density)) {
1465     case 0:
1466             if (toner_density < 0 || toner_density > 5)
1467                 ecode = gs_error_rangecheck;
1468             else
1469                 break;
1470             goto tden;
1471     default:
1472 	ecode = code;
1473 tden:
1474 	param_signal_error(plist, param_name, ecode);
1475     case 1:
1476 	break;
1477     }
1478 
1479     switch (code = param_read_bool(plist, (param_name = ESCPAGE_OPTION_TONERSAVING), &toner_saving)) {
1480     case 0:
1481 	break;
1482     default:
1483 	if ((code = param_read_null(plist, param_name)) == 0) {
1484 	    break;
1485 	}
1486 	ecode = code;
1487 	param_signal_error(plist, param_name, ecode);
1488     case 1:
1489 	break;
1490     }
1491 
1492     if ((code = param_read_bool(plist, (param_name = ESCPAGE_OPTION_DUPLEX), &duplex)) < 0)
1493 	param_signal_error(plist, param_name, ecode = code);
1494 
1495     if ((code = param_read_bool(plist, (param_name = ESCPAGE_OPTION_DUPLEX_TUMBLE), &tum)) < 0)
1496 	param_signal_error(plist, param_name, ecode = code);
1497 
1498     if ((code = param_read_bool(plist, (param_name = ESCPAGE_OPTION_LANDSCAPE), &landscape)) < 0)
1499 	param_signal_error(plist, param_name, ecode = code);
1500 
1501     if ((code = param_read_bool(plist, (param_name = ESCPAGE_OPTION_FACEUP), &faceup)) < 0) {
1502 	param_signal_error(plist, param_name, ecode = code);
1503     }
1504 
1505 
1506     switch (code = param_read_int(plist, (param_name = "BitsPerPixel"), &bpp)) {
1507 	case 0:
1508 	    if (bpp != 8 && bpp != 24)
1509 		ecode = gs_error_rangecheck;
1510 	    else
1511 		break;
1512 	    goto bppe;
1513 	default:
1514 	    ecode = code;
1515 	  bppe:param_signal_error(plist, param_name, ecode);
1516 	case 1:
1517 	    break;
1518     }
1519 
1520     if (bpp != 0) {
1521 	dev->color_info.depth = bpp;
1522 	dev->color_info.num_components = ((bpp == 8) ? 1 : 3);
1523 	dev->color_info.max_gray = (bpp > 8 ? 255 : 1000);
1524 	dev->color_info.max_color = (bpp > 8 ? 255 : 1000);
1525 	dev->color_info.dither_grays = (bpp > 8 ? 256 : 5);
1526 	dev->color_info.dither_colors = (bpp > 8 ? 256 : 2);
1527 	dev_proc(pdev, map_rgb_color) = ((bpp == 8) ? gx_default_gray_map_rgb_color : gx_default_rgb_map_rgb_color);
1528 	dev_proc(pdev, map_color_rgb) = ((bpp == 8) ? gx_default_gray_map_color_rgb : gx_default_rgb_map_color_rgb);
1529     }
1530 
1531     if (ecode < 0) return ecode;
1532     code = gdev_vector_put_params(dev, plist);
1533     if (code < 0) return code;
1534 
1535     pdev->manualFeed = mf;
1536     pdev->cassetFeed = cass;
1537     pdev->faceup = faceup;
1538     pdev->RITOff = RITOff;
1539     pdev->orientation = landscape;
1540     pdev->toner_density = toner_density;
1541     pdev->toner_saving = toner_saving;
1542     pdev->Collate = collate;
1543     pdev->Duplex = duplex;
1544     pdev->Tumble = tum;
1545 
1546     if (bpp != 0 && bpp != old_bpp && pdev->is_open)
1547 	return gs_closedevice(dev);
1548 
1549     return 0;
1550 }
1551 
1552 /* ---------------- Images ---------------- */
1553 
1554 
1555 private int
escv_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)1556 escv_copy_mono(gx_device * dev, const byte * data,
1557 	int data_x, int raster, gx_bitmap_id id, int x, int y, int w, int h,
1558 	   gx_color_index zero, gx_color_index one)
1559 {
1560     gx_device_escv *const	pdev = (gx_device_escv *) dev;
1561     gx_device_vector *const	vdev = (gx_device_vector *) dev;
1562     stream			*s = gdev_vector_stream(vdev);
1563     gx_drawing_color		color;
1564     int				code = 0;
1565     gx_color_index		c_color = 0;
1566     char			obuf[128];
1567     int				depth = 1;
1568 
1569     if (id != gs_no_id && zero == gx_no_color_index && one != gx_no_color_index && data_x == 0) {
1570 	gx_drawing_color dcolor;
1571 
1572 	color_set_pure(&dcolor, one);
1573 	escv_setfillcolor(vdev, &dcolor);
1574     }
1575 
1576     if (zero == gx_no_color_index) {
1577 
1578         if (one == gx_no_color_index) return 0;
1579 	if (pdev->MaskState != 1) {
1580 	    lputs(s, ESC_GS "2;184wfE" ESC_GS "3;184wfE" ESC_GS "5;184wfE");
1581             pdev->MaskState = 1;
1582         }
1583 	c_color = one;
1584 
1585     } else if (one == gx_no_color_index)
1586 	/* 1bit ��Ʃ�� �ӥå�ȿž��zero ��������� */
1587     {
1588 	if (pdev->MaskState != 1) {
1589 	    lputs(s, ESC_GS "3;184wfE" ESC_GS "5;184wfE");
1590 	    pdev->MaskState = 1;
1591 	}
1592 	c_color = zero;
1593     } else if (one == vdev->white) {
1594 
1595 	if (pdev->MaskState != 0) {
1596 	    lputs(s, ESC_GS "3;204wfE" ESC_GS "5;204wfE");
1597 	    pdev->MaskState = 0;
1598 	}
1599 	c_color = zero;
1600     } else {
1601 
1602         if (pdev->MaskState != 1) {
1603 	    lputs(s, ESC_GS "3;184wfE" ESC_GS "5;184wfE");
1604             pdev->MaskState = 1;
1605         }
1606 	color_set_pure(&color, one);
1607 	code = gdev_vector_update_fill_color((gx_device_vector *) pdev, &color);
1608 
1609 	/* �������̲ᤷ����ʲ��ο������̵��̣�� */
1610     }
1611     if (code < 0) return 0;
1612 
1613     /* �ѥ�����ϣλ��꡿����åɥѥ�������� */
1614     (void)sprintf(obuf, ESC_GS "1;2;3;%d;%d;%dfpE",
1615 		  (unsigned char)(c_color >> 16 & 0xff),
1616 		  (unsigned char)(c_color >> 8 & 0xff),
1617 		  (unsigned char)(c_color & 0xff));
1618     lputs(s, obuf);
1619 
1620     lputs(s, ESC_GS "5;2;1;0;0cpE");
1621 
1622     escv_write_begin(dev, depth, (int)x, (int)y, w, h, w, h, 0);
1623     {
1624 	int		i, j;
1625 	uint		width_bytes = (w + 7) >> 3;
1626 	uint		num_bytes = width_bytes * h;
1627 
1628 	byte *buf = gs_alloc_bytes(vdev->memory, num_bytes, "escv_copy_mono(buf)");
1629 
1630 	if (data_x % 8 == 0) {
1631 	    for (i = 0; i < h; ++i) {
1632 		memcpy(buf + i * width_bytes, data + (data_x >> 3) + i * raster, width_bytes);
1633 	    }
1634 	} else {
1635 	    for (i = 0; i < h; ++i) {
1636 		for (j = 0; j < width_bytes; j++) {
1637 		    *(buf + i * width_bytes + j) =
1638 			*(data + (data_x >> 3) + i * raster + j) << (data_x % 8) |
1639 			*(data + (data_x >> 3) + i * raster + j + 1) >> (8 - data_x % 8);
1640 		}
1641 	    }
1642 	}
1643 
1644 	escv_write_data(dev, depth, buf, num_bytes, w, h);
1645 	gs_free_object(vdev->memory, buf, "escv_copy_mono(buf)");
1646     }
1647     escv_write_end(dev, depth);
1648     return 0;
1649 }
1650 
1651 
1652 /* Copy a color bitmap. */
1653 private int
escv_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)1654 escv_copy_color(gx_device * dev,
1655 		  const byte * data, int data_x, int raster, gx_bitmap_id id,
1656 		  int x, int y, int w, int h)
1657 {
1658     gx_device_escv	*const pdev = (gx_device_escv *) dev;
1659     gx_device_vector	*const vdev = (gx_device_vector *) dev;
1660 
1661     stream		*s = gdev_vector_stream(vdev);
1662     int			depth = dev->color_info.depth;
1663     int			num_components = (depth < 24 ? 1 : 3);
1664     uint		width_bytes = w * num_components;
1665 
1666     if (pdev->MaskState != 0) {
1667 	lputs(s, ESC_GS "3;204wfE" ESC_GS "5;204wfE");
1668 	pdev->MaskState = 0;
1669     }
1670 
1671     escv_write_begin(dev, depth, (int)x, (int)y, w, h, w, h, 0);
1672 
1673     {
1674 	int		i;
1675 	uint		num_bytes = width_bytes * h;
1676 	byte		*buf = gs_alloc_bytes(vdev->memory, num_bytes, "escv_copy_color(buf)");
1677 
1678 	for (i = 0; i < h; ++i) {
1679 	    memcpy(buf + i * width_bytes, data + ((data_x * depth) >> 3) + i * raster, width_bytes);
1680 	}
1681 
1682 	escv_write_data(dev, depth, buf, num_bytes, w, h);
1683 	gs_free_object(vdev->memory, buf, "escv_copy_color(buf)");
1684     }
1685 
1686     escv_write_end(dev, depth);
1687     return 0;
1688 }
1689 
1690 
1691 /* Fill a mask. */
1692 private int
escv_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)1693 escv_fill_mask(gx_device * dev,
1694 		 const byte * data, int data_x, int raster, gx_bitmap_id id,
1695 		 int x, int y, int w, int h,
1696 		 const gx_drawing_color * pdcolor, int depth,
1697 		 gs_logical_operation_t lop, const gx_clip_path * pcpath)
1698 {
1699     gx_device_vector const	*vdev = (gx_device_vector *) dev;
1700     gx_device_escv		*pdev = (gx_device_escv *) dev;
1701     stream			*s = gdev_vector_stream(vdev);
1702 
1703     if (w <= 0 || h <= 0) return 0;
1704 
1705     if (depth > 1 ||
1706 	gdev_vector_update_fill_color(vdev, pdcolor) < 0 ||
1707 	gdev_vector_update_clip_path(vdev, pcpath) < 0 ||
1708 	gdev_vector_update_log_op(vdev, lop) < 0
1709 	)
1710 	return gx_default_fill_mask(dev, data, data_x, raster, id,
1711 				    x, y, w, h, pdcolor, depth, lop, pcpath);
1712 
1713 #if 0
1714     (*dev_proc(vdev->bbox_device, fill_mask))
1715 	((gx_device *) vdev->bbox_device, data, data_x, raster, id,
1716 	 x, y, w, h, pdcolor, depth, lop, pcpath);
1717 #endif
1718 
1719     if (pdev->MaskState != 1) {
1720 
1721 	lputs(s, ESC_GS "3;184wfE" ESC_GS "5;184wfE");
1722 	pdev->MaskState = 1;
1723     }
1724 
1725     if (id != gs_no_id && data_x == 0 && depth == 1) {
1726 	char		obuf[128];
1727 	int		i;
1728 	uint		width_bytes = (w + 7) >> 3;
1729 	uint		num_bytes = width_bytes * h;
1730 	byte		*buf;
1731 
1732 	if (pdev -> id_cache[id & VCACHE] != id) {
1733 
1734 	    buf = gs_alloc_bytes(vdev->memory, num_bytes, "escv_fill_mask(buf)");
1735 
1736 	    /* cache entry */
1737 	    for (i = 0; i < h; ++i) {
1738 		memcpy(buf + i * width_bytes, data + (data_x >> 3) + i * raster, width_bytes);
1739 	    }
1740 
1741 	    (void)sprintf(obuf, ESC_GS "%d;%d;%d;%d;0db{F", num_bytes, (int)(id & VCACHE), w, h);
1742 	    lputs(s, obuf);
1743 	    put_bytes(s, buf, num_bytes);
1744 
1745 	    gs_free_object(vdev->memory, buf, "escv_fill_mask(buf)");
1746 	    pdev -> id_cache[id & VCACHE] = id;
1747 	}
1748 
1749 	(void)sprintf(obuf, ESC_GS "%dX" ESC_GS "%dY", x, y);
1750 	lputs(s, obuf);
1751 	(void)sprintf(obuf, ESC_GS "%ddbF", id & VCACHE);
1752 	lputs(s, obuf);
1753 
1754 	return 0;
1755     }
1756 
1757     escv_write_begin(dev, depth, (int)x, (int)y, w, h, w, h, 0);
1758     {
1759 	int		i;
1760 	uint		width_bytes = (w + 7) >> 3;
1761 	uint		num_bytes = width_bytes * h;
1762 	byte		*buf = gs_alloc_bytes(vdev->memory, num_bytes, "escv_fill_mask(buf)");
1763 
1764 	for (i = 0; i < h; ++i) {
1765 	    memcpy(buf + i * width_bytes, data + (data_x >> 3) + i * raster, width_bytes);
1766 	}
1767 
1768 	escv_write_data(dev, depth, buf, num_bytes, w, h);
1769 	escv_write_end(dev, depth);
1770 	gs_free_object(vdev->memory, buf, "escv_fill_mask(buf)");
1771     }
1772 
1773     return 0;
1774 }
1775 
1776 /* ---------------- High-level images ---------------- */
1777 
1778 private image_enum_proc_plane_data(escv_image_plane_data);
1779 private image_enum_proc_end_image(escv_image_end_image);
1780 private const gx_image_enum_procs_t escv_image_enum_procs =
1781 {
1782     escv_image_plane_data, escv_image_end_image
1783 };
1784 
1785 
1786 /* Start processing an image. */
1787 private int
escv_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)1788 escv_begin_image(gx_device * dev,
1789 		   const gs_imager_state * pis, const gs_image_t * pim,
1790 		   gs_image_format_t format, const gs_int_rect * prect,
1791 	      const gx_drawing_color * pdcolor, const gx_clip_path * pcpath,
1792 		   gs_memory_t * mem, gx_image_enum_common_t **pinfo)
1793 {
1794     gx_device_vector *const	vdev = (gx_device_vector *) dev;
1795     gx_device_escv *const	pdev = (gx_device_escv *) dev;
1796     stream			*s = gdev_vector_stream((gx_device_vector *) pdev);
1797     gdev_vector_image_enum_t	*pie =
1798 	gs_alloc_struct(mem, gdev_vector_image_enum_t, &st_vector_image_enum, "escv_begin_image");
1799     const gs_color_space	*pcs = pim->ColorSpace;
1800     gs_color_space_index	index;
1801     int				num_components = 1;
1802     bool can_do = prect == 0 &&
1803       (pim->format == gs_image_format_chunky ||
1804        pim->format == gs_image_format_component_planar);
1805 
1806     gs_matrix			imat;
1807     int				code;
1808     int				ty, bx, by, cx, cy, dx, dy, sx, sy;
1809 
1810     if (pie == 0) return_error(gs_error_VMerror);
1811     pie->memory = mem;
1812     code = gdev_vector_begin_image(vdev, pis, pim, format, prect,
1813 				   pdcolor, pcpath, mem, &escv_image_enum_procs, pie);
1814     if (code < 0) return code;
1815 
1816     *pinfo = (gx_image_enum_common_t *) pie;
1817 
1818     if (!pim->ImageMask) {
1819 	index = gs_color_space_get_index(pcs);
1820 	num_components = gs_color_space_num_components(pcs);
1821 
1822 	if (pim->CombineWithColor) {
1823 	    can_do = false;
1824 	} else {
1825 	    switch (index) {
1826 		case gs_color_space_index_DeviceGray:
1827 		    if ((pim->Decode[0] != 0 || pim->Decode[1] != 1)
1828 			&& (pim->Decode[0] != 1 || pim->Decode[1] != 0))
1829 			can_do = false;
1830 		    break;
1831 		case gs_color_space_index_DeviceRGB:
1832 		    if (pim->Decode[0] != 0 || pim->Decode[1] != 1 ||
1833 			pim->Decode[2] != 0 || pim->Decode[3] != 1 ||
1834 			pim->Decode[4] != 0)
1835 			can_do = false;
1836 		    break;
1837 		default:
1838 		    can_do = false;
1839 	    }
1840 	}
1841     }
1842     if (!can_do) {
1843 	return gx_default_begin_image(dev, pis, pim, format, prect,
1844 				      pdcolor, pcpath, mem, &pie->default_info);
1845     }
1846 
1847     if (pim->ImageMask || (pim->BitsPerComponent == 1 && num_components == 1)) {
1848 	if (pim->Decode[0] > pim->Decode[1]) {
1849 	    pdev->MaskReverse = 1;
1850 	} else {
1851 	    lputs(s, ESC_GS "8;1;2;2;2plr{E");
1852 	    put_bytes(s, "\000\000\000\000\377\377\377\377", 8);
1853 	    pdev->MaskReverse = 0;
1854 	}
1855     }
1856 
1857     /* Write the image/colorimage/imagemask preamble. */
1858     gs_matrix_invert(&pim->ImageMatrix, &imat);
1859     gs_matrix_multiply(&imat, &ctm_only(pis), &imat);
1860 
1861     ty = imat.ty;
1862     bx = imat.xx * pim->Width + imat.yx * pim->Height + imat.tx;
1863     by = imat.xy * pim->Width + imat.yy * pim->Height + imat.ty;
1864     cx = imat.yx * pim->Height + imat.tx;
1865     cy = imat.yy * pim->Height + imat.ty;
1866     dx = imat.xx * pim->Width + imat.tx;
1867     dy = imat.xy * pim->Width + imat.ty;
1868 
1869     sx = bx - (int)imat.tx;
1870     sy = by - (int)imat.ty;
1871 
1872     /* �Ȥꤢ�������ΰ��֤˼��ޤ�褦�˶���Ū�˺�ɸ���ѹ����롣 */
1873     pdev -> roll = 0;
1874     pdev -> reverse_x = pdev -> reverse_y = 0;
1875     if (imat.tx > bx) {
1876 	pdev -> reverse_x = 1;
1877 	sx = -sx;
1878 	imat.tx = bx;
1879     }
1880 
1881     if (imat.ty > by) {
1882 	pdev -> reverse_y = 1;
1883 	sy = -sy;
1884 	imat.ty = by;
1885     }
1886 
1887     (void)memcpy(&pdev -> xmat, &imat, sizeof(gs_matrix));
1888     pdev -> sx = sx;
1889     pdev -> sy = sy;
1890     pdev -> h = pim->Height;
1891     pdev -> w = pim->Width;
1892     pdev -> dd = 0;
1893     pdev -> bx = 0;
1894     pdev -> by = 0;
1895 
1896     if (ty == cy) {
1897 	/* ��ž��������ˤĤ��Ƥϸ���̤������GS ¦�ε�ǽ����Ѥ��� */
1898 	return -1;
1899     }
1900 
1901     if (pim->ImageMask) {
1902 	pdev->ncomp = 1;
1903 
1904 	/* ������������̿�� - Ʃ�� */
1905 	if (pdev->MaskState != 1) {
1906 
1907 	    lputs(s, ESC_GS "3;184wfE" ESC_GS "5;184wfE");
1908 	    pdev->MaskState = 1;
1909 	}
1910 
1911     } else {
1912 
1913 	/* ������������̿�� - ���ɤ� */
1914 	if (pdev->MaskState != 0) {
1915 
1916 	    lputs(s, ESC_GS "3;204wfE" ESC_GS "5;204wfE");
1917 	    pdev->MaskState = 0;
1918 	}
1919 	pdev->ncomp = num_components;
1920     }
1921 
1922     if (pdev -> reverse_y) return 0;
1923 
1924     escv_write_begin(dev, pie->bits_per_pixel, (int)imat.tx, (int)imat.ty, pie->width, pie->height, sx, sy, pdev -> roll);
1925 
1926     return 0;
1927 }
1928 
1929 
1930 /* Process the next piece of an image. */
1931 private int
1932 #if GS_VERSION_MAJOR >= 6
escv_image_plane_data(gx_image_enum_common_t * info,const gx_image_plane_t * planes,int height,int * rows_used)1933 escv_image_plane_data(gx_image_enum_common_t *info, const gx_image_plane_t *planes, int height, int *rows_used)
1934 #else
1935 escv_image_plane_data(gx_device *dev, gx_image_enum_common_t *info, const gx_image_plane_t *planes, int height)
1936 #endif
1937 {
1938 #if GS_VERSION_MAJOR >= 6
1939     gx_device *dev = info->dev;
1940 #endif
1941     gx_device_vector *const	vdev = (gx_device_vector *) dev;
1942     gx_device_escv *const	pdev = (gx_device_escv *) dev;
1943     gdev_vector_image_enum_t	*pie = (gdev_vector_image_enum_t *) info;
1944 
1945     int				y;
1946     int				plane;
1947     int				width_bytes, tbyte;
1948     byte			*buf;
1949 
1950     if (pie->default_info) return gx_image_plane_data(pie->default_info, planes, height);
1951 
1952     gx_image_plane_data(pie->bbox_info, planes, height);
1953 
1954     {
1955 
1956 #if GS_VERSION_MAJOR >= 6
1957 	if (height == 260)
1958 	    height = 1;
1959 #endif
1960 	width_bytes = (pie->width * pie->bits_per_pixel / pdev->ncomp + 7) / 8 * pdev->ncomp;
1961 	tbyte = width_bytes * height;
1962 	buf = gs_alloc_bytes(vdev->memory, tbyte, "escv_image_data(buf)");
1963 
1964 	if (pdev -> reverse_y) {
1965 
1966 	    if (pdev -> h == height) {
1967 #if 1
1968 		if(tbyte == 1){
1969 		      pdev->w += pdev->sx / 2048;
1970 		      height  += pdev->sy / 2048;
1971 	        }
1972 #endif
1973 
1974 
1975 		escv_write_begin(dev, pie->bits_per_pixel, (int)pdev -> xmat.tx, (int)pdev -> xmat.ty, pdev -> w, height, (int)pdev -> sx, (int)pdev -> sy, pdev -> roll);
1976 
1977 	    } else {
1978 		float	yy, sy;
1979 
1980 		yy = (pdev -> h * pdev->xmat.yy) - (pdev -> dd * pdev -> xmat.yy) - (height * pdev -> xmat.yy);
1981 		if (yy == 0) {
1982 		    yy = (pdev -> h * pdev->xmat.yx) - (pdev -> dd * pdev -> xmat.yx) - (height * pdev -> xmat.yx);
1983 		}
1984 
1985 		if (pdev -> by) {
1986 		    sy = (int)pdev -> xmat.ty - (int)yy;
1987 		    sy = pdev -> by - (int)sy;
1988 		} else {
1989 		    sy = height * pdev -> xmat.yy + 0.5;
1990 		}
1991 		if (sy < 0) {
1992 		    sy = -sy;
1993 		}
1994 
1995 		escv_write_begin(dev, pie->bits_per_pixel, (int)pdev -> xmat.tx, (int)pdev -> xmat.ty - (int)yy, pdev -> w, height, (int)pdev -> sx, (int)sy, pdev -> roll);
1996 
1997 		pdev -> by = (int)pdev -> xmat.ty - (int)yy;
1998 	    }
1999 	}
2000 	pdev -> dd += height;
2001 
2002 	for (plane = 0; plane < pie->num_planes; ++plane) {
2003 
2004 	    for (y = 0; y < height; ++y) {
2005 
2006 		int     bit, w;
2007 		char    *p, *d, c;
2008 
2009                 p = planes[plane].data + ((planes[plane].data_x * pie->bits_per_pixel) >> 3) + y * planes[plane].raster;
2010 		if (pdev -> reverse_y) {
2011 
2012 		    d = buf + (height - y) * width_bytes;
2013 
2014 		    if (!pdev -> reverse_x) {
2015 			(void)memcpy(buf + (height - y - 1) * width_bytes,
2016 				     planes[plane].data + ((planes[plane].data_x * pie->bits_per_pixel) >> 3)
2017 				     + y * planes[plane].raster, width_bytes);
2018 
2019 		    }
2020 
2021 		} else {
2022 
2023 		    d = buf + (y + 1) * width_bytes;
2024 
2025 		    if (!pdev -> reverse_x) {
2026 
2027 			(void)memcpy(buf + y * width_bytes,
2028 				     planes[plane].data + ((planes[plane].data_x * pie->bits_per_pixel) >> 3)
2029 				     + y * planes[plane].raster, width_bytes);
2030 
2031 		    }
2032 		}
2033 		if (pdev -> reverse_x) {
2034 		    if (pie->bits_per_pixel == 1) {
2035 			for (w = 0; w < width_bytes; w++) {
2036 			    c = 0;
2037 			    for (bit = 0; bit < 8; bit++) {
2038 				if (*p & 1 << (7 - bit)) {
2039 				    c |= 1 << bit;
2040 				}
2041 			    }
2042 			    p++;
2043 			    *--d = c;
2044 			}
2045 		    } else if (pie->bits_per_pixel == 8){
2046 			for (w = 0; w < width_bytes; w++) {
2047 			    *--d = *p++;
2048 			}
2049 		    } else {
2050 			for (w = 0; w < width_bytes / 3; w++) {
2051 			    *--d = *(p + 2);
2052 			    *--d = *(p + 1);
2053 			    *--d = *p;
2054 			    p += 3;
2055 			}
2056 		    }
2057 		}
2058 	    }
2059 	}
2060 
2061 #if 1
2062 	if(tbyte == 1){
2063 	    int t;
2064 	    gs_free_object(vdev->memory, buf, "esmv_image_data(buf)");
2065 	    if(pdev->sx > pdev->sy){
2066 	        tbyte = 1;
2067 	    } else {
2068 		if(pdev->sx < pdev->sy){
2069 	            tbyte = tbyte * height;
2070   	        } else {
2071 	            tbyte = 1;
2072 	        }
2073 	    }
2074 	    buf = gs_alloc_bytes(vdev->memory, tbyte, "esmv_image_data(buf)");
2075 	    for(t = 0; t < tbyte; t++){
2076 	        buf[t] = 0x00;
2077 	    }
2078 	}
2079 #endif
2080 
2081 	escv_write_data(dev, pie->bits_per_pixel, buf, tbyte, pdev -> w, height);
2082 
2083 	if (pdev -> reverse_y) {
2084 	    escv_write_end(dev, pie->bits_per_pixel);
2085 	}
2086 
2087 	gs_free_object(vdev->memory, buf, "escv_image_data(buf)");
2088     }
2089     return (pie->y += height) >= pie->height;
2090 }
2091 
2092 
2093 private int
2094 #if GS_VERSION_MAJOR >= 6
escv_image_end_image(gx_image_enum_common_t * info,bool draw_last)2095 escv_image_end_image(gx_image_enum_common_t * info, bool draw_last)
2096 #else
2097 escv_image_end_image(gx_device *dev, gx_image_enum_common_t * info, bool draw_last)
2098 #endif
2099 {
2100 #if GS_VERSION_MAJOR >= 6
2101     gx_device *dev = info->dev;
2102 #endif
2103     gx_device_vector		*const vdev = (gx_device_vector *) dev;
2104     gx_device_escv		*const pdev = (gx_device_escv *) dev;
2105     gdev_vector_image_enum_t	*pie = (gdev_vector_image_enum_t *) info;
2106     stream			*s = gdev_vector_stream((gx_device_vector *)pdev);
2107     int				code;
2108 
2109     if (!(pdev -> reverse_y)) {
2110 	escv_write_end(dev, pie->bits_per_pixel);
2111     }
2112 
2113     pdev->reverse_x = pdev->reverse_y = 0;
2114     if (pdev->MaskReverse == 0) {
2115 	lputs(s, ESC_GS "8;1;2;2;2plr{E");
2116 	put_bytes(s, "\377\377\377\377\000\000\000\000", 8);
2117     }
2118     pdev->MaskReverse = -1;
2119 
2120     code = gdev_vector_end_image(vdev, (gdev_vector_image_enum_t *) pie, draw_last, pdev->white);
2121     return code;
2122 }
2123 
2124 
escv_write_begin(gx_device * dev,int bits,int x,int y,int sw,int sh,int dw,int dh,int roll)2125 private void escv_write_begin(gx_device *dev, int bits, int x, int y, int sw, int sh, int dw, int dh, int roll)
2126 {
2127     gx_device_vector *const     vdev = (gx_device_vector *) dev;
2128     gx_device_escv *const       pdev = (gx_device_escv *)dev;
2129     stream			*s = gdev_vector_stream((gx_device_vector *)pdev);
2130     char                        obuf[128], *tmp, *p;
2131     int				i, comp;
2132 
2133     (void)sprintf(obuf, ESC_GS "%dX" ESC_GS "%dY", x, y);
2134     lputs(s, obuf);
2135 
2136     comp = 0;
2137 
2138     if (bits == 1) {
2139 	(void)sprintf(obuf, ESC_GS "2;201;1;%d;%d;%d;%d;%d;%dscrI", comp, sw, sh, dw, dh, roll);
2140     } else if (bits == 4) {
2141 	if (pdev -> c4map) {
2142 	    /* ���顼�ޥå���Ͽ */
2143 	    lputs(s, ESC_GS "64;2;2;16;16plr{E");
2144 	    p = tmp = gs_alloc_bytes(vdev->memory, 64, "escv_write_begin(tmp4)");
2145 	    for (i = 0; i < 16; i++) {
2146 		*p++ = i << 4;
2147 		*p++ = i << 4;
2148 		*p++ = i << 4;
2149 		*p++ = i << 4;
2150 	    }
2151 	    put_bytes(s, tmp, 64);
2152 	    gs_free_object(vdev->memory, tmp, "escv_write_begin(tmp4)");
2153 	    pdev -> c4map = FALSE;
2154 	}
2155 	(void)sprintf(obuf, ESC_GS "2;203;2;%d;%d;%d;%d;%d;%dscrI", comp, sw, sh, dw, dh, roll);
2156     } else if (bits == 8) {
2157 	if (pdev -> c8map) {
2158 	    /* ���顼�ޥå���Ͽ */
2159 	    lputs(s, ESC_GS "1024;4;2;256;256plr{E");
2160 	    p = tmp = gs_alloc_bytes(vdev->memory, 1024, "escv_write_begin(tmp)");
2161 	    for (i = 0; i < 256; i++) {
2162 		*p++ = i;
2163 		*p++ = i;
2164 		*p++ = i;
2165 		*p++ = i;
2166 	    }
2167 	    put_bytes(s, tmp, 1024);
2168 	    gs_free_object(vdev->memory, tmp, "escv_write_begin(tmp)");
2169 	    pdev -> c8map = FALSE;
2170 	}
2171 	(void)sprintf(obuf, ESC_GS "2;204;4;%d;%d;%d;%d;%d;%dscrI", comp, sw, sh, dw, dh, roll);
2172     } else {
2173 	/* 24 bit */
2174 	(void)sprintf(obuf, ESC_GS "2;102;0;%d;%d;%d;%d;%d;%dscrI", comp, sw, sh, dw, dh, roll);
2175     }
2176     lputs(s, obuf);
2177 
2178     return;
2179 }
2180 
2181 
escv_write_data(gx_device * dev,int bits,char * buf,int bsize,int w,int ras)2182 private void escv_write_data(gx_device *dev, int bits, char *buf, int bsize, int w, int ras)
2183 {
2184     gx_device_vector *const     vdev = (gx_device_vector *) dev;
2185     gx_device_escv *const       pdev = (gx_device_escv *) dev;
2186     stream			*s = gdev_vector_stream((gx_device_vector *)pdev);
2187     char                        obuf[128];
2188     int				size;
2189     char			/**tmp,*/ *tmps, *p, tmp;
2190 
2191     if (bits == 12) {
2192 	p = tmps = gs_alloc_bytes(vdev->memory, bsize * 2, "escv_write_data(tmp)");
2193 #if 0
2194 	for (size = 0; size < bsize; size++) {
2195 	    if((buf[size] & 0xF0) == 0xF0){
2196 	        *p++ = 0xFF;
2197 	    }else{
2198 	        *p++ = buf[size] & 0xF0;
2199 	    }
2200 	    if((buf[size] << 4 & 0xF0) == 0xF0){
2201 	        *p++ = 0xFF;
2202 	    }else{
2203 	        *p++ = buf[size] << 4;
2204 	    }
2205 	}
2206 #else
2207 	for (size = 0; size < bsize; size++) {
2208 	        tmp = buf[size] & 0xF0;
2209 	        *p++ = (tmp + (tmp >> 4 & 0x0F));
2210 	        tmp = buf[size] << 4;
2211 	        *p++ = (tmp + (tmp >> 4 & 0x0F));
2212 	}
2213 
2214 #endif
2215 	bsize = bsize * 2;
2216 	buf = tmps;
2217     }
2218 
2219     (void)sprintf(obuf, ESC_GS "%d;%dcu{I", bsize, ras);
2220     lputs(s, obuf);
2221     put_bytes(s, buf, bsize);
2222 
2223     if (bits == 12) {
2224       gs_free_object(vdev->memory, tmps, "escv_write_data(tmp)");
2225     }
2226     return;
2227 }
2228 
2229 
escv_write_end(gx_device * dev,int bits)2230 private void escv_write_end(gx_device *dev, int bits)
2231 {
2232     gx_device_escv *const       pdev = (gx_device_escv *) dev;
2233     stream			*s = gdev_vector_stream((gx_device_vector *)pdev);
2234 
2235     lputs(s, ESC_GS "ecrI");
2236     return;
2237 }
2238 
2239