1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
|
/* Copyright (C) 1997 Aladdin Enterprises. All rights reserved.
This file is part of Aladdin Ghostscript.
Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author
or distributor accepts any responsibility for the consequences of using it,
or for whether it serves any particular purpose or works at all, unless he
or she says so in writing. Refer to the Aladdin Ghostscript Free Public
License (the "License") for full details.
Every copy of Aladdin Ghostscript must include a copy of the License,
normally in a plain ASCII text file named PUBLIC. The License grants you
the right to copy, modify and redistribute Aladdin Ghostscript, but only
under certain conditions described in the License. Among other things, the
License requires that the copyright notice and this notice be preserved on
all copies.
*/
/* gdevvec.c */
/* Utilities for "vector" devices */
#include "math_.h"
#include "memory_.h"
#include "string_.h"
#include "gx.h"
#include "gp.h"
#include "gserrors.h"
#include "gsparam.h"
#include "gsutil.h"
#include "gxfixed.h"
#include "gdevvec.h"
#include "gscspace.h"
#include "gxdcolor.h"
#include "gxpaint.h" /* requires gx_path, ... */
#include "gzpath.h"
#include "gzcpath.h"
/******
****** NOTE: EVERYTHING IN THIS FILE IS SUBJECT TO CHANGE WITHOUT NOTICE.
****** USE AT YOUR OWN RISK.
******/
/* Structure descriptors */
public_st_device_vector();
public_st_vector_image_enum();
/* ================ Default implementations of vector procs ================ */
int
gdev_vector_setflat(gx_device_vector * vdev, floatp flatness)
{
return 0;
}
int
gdev_vector_dopath(gx_device_vector * vdev, const gx_path * ppath,
gx_path_type_t type)
{
bool do_close = (type & gx_path_type_stroke) != 0;
gs_fixed_rect rect;
gs_point scale;
double x_start = 0, y_start = 0, x_prev, y_prev;
bool first = true;
gs_path_enum cenum;
int code;
if (gx_path_is_rectangle(ppath, &rect))
return (*vdev_proc(vdev, dorect)) (vdev, rect.p.x, rect.p.y, rect.q.x,
rect.q.y, type);
scale = vdev->scale;
code = (*vdev_proc(vdev, beginpath)) (vdev, type);
gx_path_enum_init(&cenum, ppath);
for (;;) {
fixed vs[6];
int pe_op = gx_path_enum_next(&cenum, (gs_fixed_point *) vs);
double x, y;
sw:switch (pe_op) {
case 0: /* done */
return (*vdev_proc(vdev, endpath)) (vdev, type);
case gs_pe_moveto:
code = (*vdev_proc(vdev, moveto))
(vdev, x_prev, y_prev, (x = fixed2float(vs[0]) / scale.x),
(y = fixed2float(vs[1]) / scale.y), first);
if (first)
x_start = x, y_start = y, first = false;
break;
case gs_pe_lineto:
code = (*vdev_proc(vdev, lineto))
(vdev, x_prev, y_prev, (x = fixed2float(vs[0]) / scale.x),
(y = fixed2float(vs[1]) / scale.y));
break;
case gs_pe_curveto:
code = (*vdev_proc(vdev, curveto))
(vdev, x_prev, y_prev,
fixed2float(vs[0]) / scale.x,
fixed2float(vs[1]) / scale.y,
fixed2float(vs[2]) / scale.x,
fixed2float(vs[3]) / scale.y,
(x = fixed2float(vs[4]) / scale.x),
(y = fixed2float(vs[5]) / scale.y));
break;
case gs_pe_closepath:
x = x_start, y = y_start;
if (do_close) {
code = (*vdev_proc(vdev, closepath))
(vdev, x_prev, y_prev, x_start, y_start);
break;
}
pe_op = gx_path_enum_next(&cenum, (gs_fixed_point *) vs);
if (pe_op != 0) {
code = (*vdev_proc(vdev, closepath))
(vdev, x_prev, y_prev, x_start, y_start);
if (code < 0)
return code;
goto sw;
}
return (*vdev_proc(vdev, endpath)) (vdev, type);
default: /* can't happen */
return_error(gs_error_unknownerror);
}
if (code < 0)
return code;
x_prev = x, y_prev = y;
}
}
int
gdev_vector_dorect(gx_device_vector * vdev, fixed x0, fixed y0, fixed x1,
fixed y1, gx_path_type_t type)
{
int code = (*vdev_proc(vdev, beginpath)) (vdev, type);
if (code < 0)
return code;
code = gdev_vector_write_rectangle(vdev, x0, y0, x1, y1,
(type & gx_path_type_stroke) != 0,
gx_rect_x_first);
if (code < 0)
return code;
return (*vdev_proc(vdev, endpath)) (vdev, type);
}
/* ================ Utility procedures ================ */
/* Recompute the cached color values. */
private void
gdev_vector_load_cache(gx_device_vector * vdev)
{
vdev->black =
(*dev_proc(vdev, map_rgb_color))
((gx_device *) vdev, (gx_color_value) 0, (gx_color_value) 0, (gx_color_value) 0);
vdev->white =
(*dev_proc(vdev, map_rgb_color))
((gx_device *) vdev, gx_max_color_value, gx_max_color_value, gx_max_color_value);
}
/* Initialize the state. */
void
gdev_vector_init(gx_device_vector * vdev)
{
gdev_vector_reset(vdev);
vdev->scale.x = vdev->scale.y = 1.0;
vdev->in_page = false;
gdev_vector_load_cache(vdev);
}
/* Reset the remembered graphics state. */
void
gdev_vector_reset(gx_device_vector * vdev)
{
static const gs_imager_state state_initial =
{gs_imager_state_initial(1)};
vdev->state = state_initial;
color_unset(&vdev->fill_color);
color_unset(&vdev->stroke_color);
vdev->clip_path_id =
vdev->no_clip_path_id = gs_next_ids(1);
}
/* Open the output file and stream. */
int
gdev_vector_open_file_bbox(gx_device_vector * vdev, uint strmbuf_size,
bool bbox)
{ /* Open the file as positionable if possible. */
int code = gx_device_open_output_file((gx_device *) vdev, vdev->fname,
true, true, &vdev->file);
if (code < 0)
return code;
if ((vdev->strmbuf = gs_alloc_bytes(vdev->v_memory, strmbuf_size,
"vector_open(strmbuf)")) == 0 ||
(vdev->strm = s_alloc(vdev->v_memory,
"vector_open(strm)")) == 0 ||
(bbox &&
(vdev->bbox_device =
gs_alloc_struct_immovable(vdev->v_memory,
gx_device_bbox, &st_device_bbox,
"vector_open(bbox_device)")) == 0)
) {
if (vdev->bbox_device)
gs_free_object(vdev->v_memory, vdev->bbox_device,
"vector_open(bbox_device)");
vdev->bbox_device = 0;
if (vdev->strm)
gs_free_object(vdev->v_memory, vdev->strm,
"vector_open(strm)");
vdev->strm = 0;
if (vdev->strmbuf)
gs_free_object(vdev->v_memory, vdev->strmbuf,
"vector_open(strmbuf)");
vdev->strmbuf = 0;
fclose(vdev->file);
vdev->file = 0;
return_error(gs_error_VMerror);
}
vdev->strmbuf_size = strmbuf_size;
swrite_file(vdev->strm, vdev->file, vdev->strmbuf, strmbuf_size);
/*
* We don't want finalization to close the file, but we do want it
* to flush the stream buffer.
*/
vdev->strm->procs.close = vdev->strm->procs.flush;
if (vdev->bbox_device) {
gx_device_bbox_init(vdev->bbox_device, NULL);
gx_device_set_resolution((gx_device *) vdev->bbox_device,
vdev->HWResolution[0],
vdev->HWResolution[1]);
/* Do the right thing about upright vs. inverted. */
/* (This is dangerous in general, since the procedure */
/* might reference non-standard elements.) */
set_dev_proc(vdev->bbox_device, get_initial_matrix,
dev_proc(vdev, get_initial_matrix));
(*dev_proc(vdev->bbox_device, open_device))
((gx_device *) vdev->bbox_device);
}
return 0;
}
/* Get the current stream, calling beginpage if in_page is false. */
stream *
gdev_vector_stream(gx_device_vector * vdev)
{
if (!vdev->in_page) {
(*vdev_proc(vdev, beginpage)) (vdev);
vdev->in_page = true;
}
return vdev->strm;
}
/* Compare two drawing colors. */
/* Right now we don't attempt to handle non-pure colors. */
private bool
drawing_color_eq(const gx_drawing_color * pdc1, const gx_drawing_color * pdc2)
{
return (gx_dc_is_pure(pdc1) ?
gx_dc_is_pure(pdc2) &&
gx_dc_pure_color(pdc1) == gx_dc_pure_color(pdc2) :
gx_dc_is_null(pdc1) ?
gx_dc_is_null(pdc2) :
false);
}
/* Update the logical operation. */
int
gdev_vector_update_log_op(gx_device_vector * vdev, gs_logical_operation_t lop)
{
gs_logical_operation_t diff = lop ^ vdev->state.log_op;
if (diff != 0) {
int code = (*vdev_proc(vdev, setlogop)) (vdev, lop, diff);
if (code < 0)
return code;
vdev->state.log_op = lop;
}
return 0;
}
/* Update the fill color. */
int
gdev_vector_update_fill_color(gx_device_vector * vdev,
const gx_drawing_color * pdcolor)
{
if (!drawing_color_eq(pdcolor, &vdev->fill_color)) {
int code = (*vdev_proc(vdev, setfillcolor)) (vdev, pdcolor);
if (code < 0)
return code;
vdev->fill_color = *pdcolor;
}
return 0;
}
/* Update the state for filling a region. */
private int
update_fill(gx_device_vector * vdev, const gx_drawing_color * pdcolor,
gs_logical_operation_t lop)
{
int code = gdev_vector_update_fill_color(vdev, pdcolor);
if (code < 0)
return code;
return gdev_vector_update_log_op(vdev, lop);
}
/* Bring state up to date for filling. */
int
gdev_vector_prepare_fill(gx_device_vector * vdev, const gs_imager_state * pis,
const gx_fill_params * params, const gx_drawing_color * pdcolor)
{
if (params->flatness != vdev->state.flatness) {
int code = (*vdev_proc(vdev, setflat)) (vdev, params->flatness);
if (code < 0)
return code;
vdev->state.flatness = params->flatness;
}
return update_fill(vdev, pdcolor, pis->log_op);
}
/* Compare two dash patterns. */
private bool
dash_pattern_eq(const float *stored, const gx_dash_params * set, floatp scale)
{
int i;
for (i = 0; i < set->pattern_size; ++i)
if (stored[i] != (float)(set->pattern[i] * scale))
return false;
return true;
}
/* Bring state up to date for stroking. */
int
gdev_vector_prepare_stroke(gx_device_vector * vdev, const gs_imager_state * pis,
const gx_stroke_params * params, const gx_drawing_color * pdcolor,
floatp scale)
{
int pattern_size = pis->line_params.dash.pattern_size;
float dash_offset = pis->line_params.dash.offset * scale;
float half_width = pis->line_params.half_width * scale;
if (pattern_size > max_dash)
return_error(gs_error_limitcheck);
if (dash_offset != vdev->state.line_params.dash.offset ||
pattern_size != vdev->state.line_params.dash.pattern_size ||
(pattern_size != 0 &&
!dash_pattern_eq(vdev->dash_pattern, &pis->line_params.dash,
scale))
) {
float pattern[max_dash];
int i, code;
for (i = 0; i < pattern_size; ++i)
pattern[i] = pis->line_params.dash.pattern[i] * scale;
code = (*vdev_proc(vdev, setdash))
(vdev, pattern, pattern_size, dash_offset);
if (code < 0)
return code;
memcpy(vdev->dash_pattern, pattern, pattern_size * sizeof(float));
vdev->state.line_params.dash.pattern_size = pattern_size;
vdev->state.line_params.dash.offset = dash_offset;
}
if (params->flatness != vdev->state.flatness) {
int code = (*vdev_proc(vdev, setflat)) (vdev, params->flatness);
if (code < 0)
return code;
vdev->state.flatness = params->flatness;
}
if (half_width != vdev->state.line_params.half_width) {
int code = (*vdev_proc(vdev, setlinewidth))
(vdev, pis->line_params.half_width * 2);
if (code < 0)
return code;
vdev->state.line_params.half_width = half_width;
}
if (pis->line_params.miter_limit != vdev->state.line_params.miter_limit) {
int code = (*vdev_proc(vdev, setmiterlimit))
(vdev, pis->line_params.miter_limit);
if (code < 0)
return code;
gx_set_miter_limit(&vdev->state.line_params,
pis->line_params.miter_limit);
}
if (pis->line_params.cap != vdev->state.line_params.cap) {
int code = (*vdev_proc(vdev, setlinecap))
(vdev, pis->line_params.cap);
if (code < 0)
return code;
vdev->state.line_params.cap = pis->line_params.cap;
}
if (pis->line_params.join != vdev->state.line_params.join) {
int code = (*vdev_proc(vdev, setlinejoin))
(vdev, pis->line_params.join);
if (code < 0)
return code;
vdev->state.line_params.join = pis->line_params.join;
} {
int code = gdev_vector_update_log_op(vdev, pis->log_op);
if (code < 0)
return code;
}
if (!drawing_color_eq(pdcolor, &vdev->stroke_color)) {
int code = (*vdev_proc(vdev, setstrokecolor)) (vdev, pdcolor);
if (code < 0)
return code;
vdev->stroke_color = *pdcolor;
}
return 0;
}
/* Write a polygon as part of a path. */
/* May call beginpath, moveto, lineto, closepath, endpath. */
int
gdev_vector_write_polygon(gx_device_vector * vdev, const gs_fixed_point * points,
uint count, bool close, gx_path_type_t type)
{
int code = 0;
if (type != gx_path_type_none &&
(code = (*vdev_proc(vdev, beginpath)) (vdev, type)) < 0
)
return code;
if (count > 0) {
double x = fixed2float(points[0].x) / vdev->scale.x, y = fixed2float(points[0].y) / vdev->scale.y;
double x_start = x, y_start = y, x_prev, y_prev;
uint i;
code = (*vdev_proc(vdev, moveto))
(vdev, 0.0, 0.0, x, y, true);
if (code >= 0)
for (i = 1; i < count && code >= 0; ++i) {
x_prev = x, y_prev = y;
code = (*vdev_proc(vdev, lineto))
(vdev, x_prev, y_prev,
(x = fixed2float(points[i].x) / vdev->scale.x),
(y = fixed2float(points[i].y) / vdev->scale.y));
}
if (code >= 0 && close)
code = (*vdev_proc(vdev, closepath))
(vdev, x, y, x_start, y_start);
}
return (code >= 0 && type != gx_path_type_none ?
(*vdev_proc(vdev, endpath)) (vdev, type) : code);
}
/* Write a rectangle as part of a path. */
/* May call moveto, lineto, closepath. */
int
gdev_vector_write_rectangle(gx_device_vector * vdev, fixed x0, fixed y0,
fixed x1, fixed y1, bool close, gx_rect_direction_t direction)
{
gs_fixed_point points[4];
points[0].x = x0, points[0].y = y0;
points[2].x = x1, points[2].y = y1;
if (direction == gx_rect_x_first)
points[1].x = x1, points[1].y = y0,
points[3].x = x0, points[3].y = y1;
else
points[1].x = x0, points[1].y = y1,
points[3].x = x1, points[3].y = y0;
return gdev_vector_write_polygon(vdev, points, 4, close,
gx_path_type_none);
}
/* Write a clipping path by calling the path procedures. */
int
gdev_vector_write_clip_path(gx_device_vector * vdev, const gx_clip_path * pcpath)
{
const gx_clip_rect *prect;
gx_clip_rect page_rect;
int code;
if (pcpath == 0) { /* There's no special provision for initclip. */
/* Write a rectangle that covers the entire page. */
page_rect.xmin = page_rect.ymin = 0;
page_rect.xmax = vdev->width;
page_rect.ymax = vdev->height;
page_rect.next = 0;
prect = &page_rect;
} else if (pcpath->segments_valid)
return (*vdev_proc(vdev, dopath)) (vdev, &pcpath->path,
gx_path_type_clip);
else {
prect = pcpath->list.head;
if (prect == 0)
prect = &pcpath->list.single;
}
/* Write out the rectangles. */
code = (*vdev_proc(vdev, beginpath)) (vdev, gx_path_type_clip);
for (; code >= 0 && prect != 0; prect = prect->next)
if (prect->xmax > prect->xmin && prect->ymax > prect->ymin)
code = gdev_vector_write_rectangle
(vdev, int2fixed(prect->xmin), int2fixed(prect->ymin),
int2fixed(prect->xmax), int2fixed(prect->ymax),
false, gx_rect_x_first);
if (code >= 0)
code = (*vdev_proc(vdev, endpath)) (vdev, gx_path_type_clip);
return code;
}
/* Update the clipping path if needed. */
int
gdev_vector_update_clip_path(gx_device_vector * vdev,
const gx_clip_path * pcpath)
{
if (pcpath) {
if (pcpath->id != vdev->clip_path_id) {
int code = gdev_vector_write_clip_path(vdev, pcpath);
if (code < 0)
return code;
vdev->clip_path_id = pcpath->id;
}
} else {
if (vdev->clip_path_id != vdev->no_clip_path_id) {
int code = gdev_vector_write_clip_path(vdev, NULL);
if (code < 0)
return code;
vdev->clip_path_id = vdev->no_clip_path_id;
}
}
return 0;
}
/* Close the output file and stream. */
void
gdev_vector_close_file(gx_device_vector * vdev)
{
gs_free_object(vdev->v_memory, vdev->bbox_device,
"vector_close(bbox_device)");
vdev->bbox_device = 0;
sclose(vdev->strm);
gs_free_object(vdev->v_memory, vdev->strm, "vector_close(strm)");
vdev->strm = 0;
gs_free_object(vdev->v_memory, vdev->strmbuf, "vector_close(strmbuf)");
vdev->strmbuf = 0;
fclose(vdev->file); /* we prevented sclose from doing this */
vdev->file = 0;
}
/* ---------------- Image enumeration ---------------- */
/* Initialize for enumerating an image. */
int
gdev_vector_begin_image(gx_device_vector * vdev,
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, gdev_vector_image_enum_t * pie)
{
const gs_color_space *pcs = pim->ColorSpace;
int num_components;
int bits_per_pixel;
int code;
if (pim->ImageMask)
bits_per_pixel = num_components = 1;
else
num_components = gs_color_space_num_components(pcs),
bits_per_pixel = pim->BitsPerComponent;
switch (format) {
case gs_image_format_chunky:
pie->num_planes = 1;
pie->bits_per_pixel = bits_per_pixel * num_components;
break;
case gs_image_format_component_planar:
pie->num_planes = num_components;
pie->bits_per_pixel = bits_per_pixel;
break;
default:
return_error(gs_error_rangecheck);
}
pie->default_info = 0;
pie->bbox_info = 0;
if ((code = gdev_vector_update_log_op(vdev, pis->log_op)) < 0 ||
(code = gdev_vector_update_clip_path(vdev, pcpath)) < 0 ||
((pim->ImageMask ||
(pim->CombineWithColor && rop3_uses_T(pis->log_op))) &&
(code = gdev_vector_update_fill_color(vdev, pdcolor)) < 0) ||
(vdev->bbox_device &&
(code = gx_device_begin_image((gx_device *) vdev->bbox_device,
pis, pim, format, prect,
pdcolor, pcpath, mem, &pie->bbox_info)) < 0)
)
return code;
pie->memory = mem;
if (prect)
pie->width = prect->q.x - prect->p.x,
pie->height = prect->q.y - prect->p.y;
else
pie->width = pim->Width, pie->height = pim->Height;
pie->bits_per_row = pie->width * pie->bits_per_pixel;
pie->y = 0;
return 0;
}
/* End an image, optionally supplying any necessary blank padding rows. */
/* Return 0 if we used the default implementation, 1 if not. */
int
gdev_vector_end_image(gx_device_vector * vdev,
gdev_vector_image_enum_t * pie, bool draw_last, gx_color_index pad)
{
int code;
if (pie->default_info) {
code = gx_default_end_image((gx_device *) vdev, pie->default_info,
draw_last);
if (code >= 0)
code = 0;
} else { /* Fill out to the full image height. */
if (pie->y < pie->height && pad != gx_no_color_index) {
uint bytes_per_row = (pie->bits_per_row + 7) >> 3;
byte *row = gs_alloc_bytes(pie->memory, bytes_per_row,
"gdev_vector_end_image(fill)");
if (row == 0)
return_error(gs_error_VMerror);
/****** FILL VALUE IS WRONG ******/
memset(row, (byte) pad, bytes_per_row);
for (; pie->y < pie->height; pie->y++)
(*dev_proc(vdev, image_data)) ((gx_device *) vdev, pie,
(const byte **)&row, 0,
bytes_per_row, 1);
gs_free_object(pie->memory, row,
"gdev_vector_end_image(fill)");
}
code = 1;
}
if (vdev->bbox_device) {
int bcode = (*dev_proc(vdev->bbox_device, end_image))
((gx_device *) vdev->bbox_device, pie->bbox_info, draw_last);
if (bcode < 0)
code = bcode;
}
gs_free_object(pie->memory, pie, "pclxl_end_image");
return code;
}
/* ================ Device procedures ================ */
#define vdev ((gx_device_vector *)dev)
/* Get parameters. */
int
gdev_vector_get_params(gx_device * dev, gs_param_list * plist)
{
int code = gx_default_get_params(dev, plist);
int ecode;
gs_param_string ofns;
if (code < 0)
return code;
ofns.data = (const byte *)vdev->fname,
ofns.size = strlen(vdev->fname),
ofns.persistent = false;
if ((ecode = param_write_string(plist, "OutputFile", &ofns)) < 0)
return ecode;
return code;
}
/* Put parameters. */
int
gdev_vector_put_params(gx_device * dev, gs_param_list * plist)
{
int ecode = 0;
int code;
gs_param_name param_name;
gs_param_string ofns;
switch (code = param_read_string(plist, (param_name = "OutputFile"), &ofns)) {
case 0:
if (ofns.size > fname_size)
ecode = gs_error_limitcheck;
else
break;
goto ofe;
default:
ecode = code;
ofe:param_signal_error(plist, param_name, ecode);
case 1:
ofns.data = 0;
break;
}
if (ecode < 0)
return ecode;
{
bool open = dev->is_open;
/* Don't let gx_default_put_params close the device. */
dev->is_open = false;
code = gx_default_put_params(dev, plist);
dev->is_open = open;
}
if (code < 0)
return code;
if (ofns.data != 0 &&
bytes_compare(ofns.data, ofns.size,
(const byte *)vdev->fname, strlen(vdev->fname))
) {
memcpy(vdev->fname, ofns.data, ofns.size);
vdev->fname[ofns.size] = 0;
if (vdev->file != 0) {
gdev_vector_close_file(vdev);
return gdev_vector_open_file(vdev, vdev->strmbuf_size);
}
}
gdev_vector_load_cache(vdev); /* in case color mapping changed */
return 0;
}
/* ---------------- Defaults ---------------- */
int
gdev_vector_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
gx_color_index color)
{
gx_drawing_color dcolor;
/* Ignore the initial fill with white. */
if (!vdev->in_page && color == vdev->white)
return 0;
color_set_pure(&dcolor, color);
{
int code = update_fill(vdev, &dcolor, rop3_T);
if (code < 0)
return code;
}
if (vdev->bbox_device) {
int code = (*dev_proc(vdev->bbox_device, fill_rectangle))
((gx_device *) vdev->bbox_device, x, y, w, h, color);
if (code < 0)
return code;
}
return (*vdev_proc(vdev, dorect)) (vdev, int2fixed(x), int2fixed(y),
int2fixed(x + w), int2fixed(y + h),
gx_path_type_fill);
}
int
gdev_vector_fill_path(gx_device * dev, const gs_imager_state * pis,
gx_path * ppath, const gx_fill_params * params,
const gx_device_color * pdevc, const gx_clip_path * pcpath)
{
int code;
if ((code = gdev_vector_prepare_fill(vdev, pis, params, pdevc)) < 0 ||
(code = gdev_vector_update_clip_path(vdev, pcpath)) < 0 ||
(vdev->bbox_device &&
(code = (*dev_proc(vdev->bbox_device, fill_path))
((gx_device *) vdev->bbox_device, pis, ppath, params,
pdevc, pcpath)) < 0) ||
(code = (*vdev_proc(vdev, dopath))
(vdev, ppath,
(params->rule > 0 ? gx_path_type_even_odd :
gx_path_type_winding_number) | gx_path_type_fill)) < 0
)
return gx_default_fill_path(dev, pis, ppath, params, pdevc, pcpath);
return code;
}
int
gdev_vector_stroke_path(gx_device * dev, const gs_imager_state * pis,
gx_path * ppath, const gx_stroke_params * params,
const gx_drawing_color * pdcolor, const gx_clip_path * pcpath)
{
int code;
/****** HANDLE SCALE ******/
if ((code = gdev_vector_prepare_stroke(vdev, pis, params, pdcolor,
dev->HWResolution[0])) < 0 ||
(code = gdev_vector_update_clip_path(vdev, pcpath)) < 0 ||
(vdev->bbox_device &&
(code = (*dev_proc(vdev->bbox_device, stroke_path))
((gx_device *) vdev->bbox_device, pis, ppath, params,
pdcolor, pcpath)) < 0) ||
(code = (*vdev_proc(vdev, dopath))
(vdev, ppath, gx_path_type_stroke)) < 0
)
return gx_default_stroke_path(dev, pis, ppath, params, pdcolor, pcpath);
return code;
}
int
gdev_vector_fill_trapezoid(gx_device * dev, const gs_fixed_edge * left,
const gs_fixed_edge * right, fixed ybot, fixed ytop, bool swap_axes,
const gx_device_color * pdevc, gs_logical_operation_t lop)
{
fixed xl = left->start.x;
fixed wl = left->end.x - xl;
fixed yl = left->start.y;
fixed hl = left->end.y - yl;
fixed xr = right->start.x;
fixed wr = right->end.x - xr;
fixed yr = right->start.y;
fixed hr = right->end.y - yr;
fixed x0l = xl + fixed_mult_quo(wl, ybot - yl, hl);
fixed x1l = xl + fixed_mult_quo(wl, ytop - yl, hl);
fixed x0r = xr + fixed_mult_quo(wr, ybot - yr, hr);
fixed x1r = xr + fixed_mult_quo(wr, ytop - yr, hr);
#define y0 ybot
#define y1 ytop
gs_fixed_point points[4];
if (swap_axes)
points[0].y = x0l, points[1].y = x0r,
points[0].x = points[1].x = y0,
points[2].y = x1r, points[3].y = x1l,
points[2].x = points[3].x = y1;
else
points[0].x = x0l, points[1].x = x0r,
points[0].y = points[1].y = y0,
points[2].x = x1r, points[3].x = x1l,
points[2].y = points[3].y = y1;
#undef y0
#undef y1
if (vdev->bbox_device) {
int code = (*dev_proc(vdev->bbox_device, fill_trapezoid))
((gx_device *) vdev->bbox_device, left, right, ybot, ytop,
swap_axes, pdevc, lop);
if (code < 0)
return code;
}
return gdev_vector_write_polygon(vdev, points, 4, true,
gx_path_type_fill);
}
int
gdev_vector_fill_parallelogram(gx_device * dev,
fixed px, fixed py, fixed ax, fixed ay, fixed bx, fixed by,
const gx_device_color * pdevc, gs_logical_operation_t lop)
{
fixed pax = px + ax, pay = py + ay;
int code = update_fill(vdev, pdevc, lop);
gs_fixed_point points[4];
if (code < 0)
return code;
if (vdev->bbox_device) {
code = (*dev_proc(vdev->bbox_device, fill_parallelogram))
((gx_device *) vdev->bbox_device, px, py, ax, ay, bx, by,
pdevc, lop);
if (code < 0)
return code;
}
points[0].x = px, points[0].y = py;
points[1].x = pax, points[0].y = pay;
points[2].x = pax + bx, points[2].y = pay + by;
points[3].x = px + bx, points[3].y = py + by;
return gdev_vector_write_polygon(vdev, points, 4, true,
gx_path_type_fill);
}
int
gdev_vector_fill_triangle(gx_device * dev,
fixed px, fixed py, fixed ax, fixed ay, fixed bx, fixed by,
const gx_device_color * pdevc, gs_logical_operation_t lop)
{
int code = update_fill(vdev, pdevc, lop);
gs_fixed_point points[3];
if (code < 0)
return code;
if (vdev->bbox_device) {
code = (*dev_proc(vdev->bbox_device, fill_triangle))
((gx_device *) vdev->bbox_device, px, py, ax, ay, bx, by,
pdevc, lop);
if (code < 0)
return code;
}
points[0].x = px, points[0].y = py;
points[1].x = px + ax, points[1].y = py + ay;
points[2].x = px + bx, points[2].y = py + by;
return gdev_vector_write_polygon(vdev, points, 3, true,
gx_path_type_fill);
}
#undef vdev
|