Debian Codegraph @

texk/dvipdfm-x/pdfdraw.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <math.h>
28
29 #include "system.h"
30 #include "error.h"
31 #include "mem.h"
32 #include "mfileio.h"
33 #include "dpxconf.h"
34 #include "dpxutil.h"
35 #include "numbers.h"
36
37 #include "pdfdoc.h"
38 #include "pdfdev.h"
39 #include "pdfcolor.h"
40 #include "pdfparse.h"
41 #include "pdfobj.h"
42
43 #include "pdfdraw.h"
44
45 #define OUR_EPSILON 2.5e-16
46
47 /*
48 * Numbers are rounding to 0-5 fractional digits
49 * in output routine.
50 */
51 #define detM(M) ((M).a * (M).d - (M).b * (M).c)
52 #define detP(M) ((M)->a * (M)->d - (M)->b * (M)->c)
53
54
55 static /* __inline__ */ int
56 inversematrix (pdf_tmatrix *W, const pdf_tmatrix *M)
57 {
58 double det;
59
60 det = detP(M);
61 if (fabs(det) < OUR_EPSILON) {
62 WARN("Inverting matrix with zero determinant...");
63 return -1; /* result is undefined. */
64 }
65
66 W->a = (M->d) / det; W->b = -(M->b) / det;
67 W->c = -(M->c) / det; W->d = (M->a) / det;
68 W->e = (M->c) * (M->f) - (M->d) * (M->e);
69 W->f = (M->b) * (M->e) - (M->a) * (M->f);
70 W->e /= det;
71 W->f /= det;
72
73 return 0;
74 }
75
76 /* pdf_coord as vector */
77 #define vecprd(v,w) ((v).x * (w).x + (v).y * (w).y)
78 #define vecrot(v,w) ((v).x * (w).y - (v).y * (w).x)
79 #define dsign(v) (((v) >= 0.0) ? 1.0 : -1.0)
80 /* acos => [0, pi] */
81 #define vecang(v,w) ( \
82 dsign(vecrot((v),(w))) * \
83 acos(vecprd((v),(w)) / sqrt(vecprd((v),(v)) * vecprd((w),(w)))) \
84 )
85
86 static /* __inline__ */ int
87 pdf_coord__equal (const pdf_coord *p1, const pdf_coord *p2)
88 {
89 if (fabs(p1->x - p2->x) < 1.e-7 &&
90 fabs(p1->y - p2->y) < 1.e-7)
91 return 1;
92 return 0;
93 }
94 #define COORD_EQUAL(p,q) pdf_coord__equal((p),(q))
95
96 #if 0
97 static int
98 pdf_coord__sort_compar_X (const void *pp1, const void *pp2)
99 {
100 pdf_coord *p1 = (pdf_coord *)pp1;
101 pdf_coord *p2 = (pdf_coord *)pp2;
102
103 if (pdf_coord__equal(p1, p2))
104 return 0;
105 else
106 return (int) dsign(p1->x - p2->x);
107
108 return 1;
109 }
110
111 static int
112 pdf_coord__sort_compar_Y (const void *pp1, const void *pp2)
113 {
114 pdf_coord *p1 = (pdf_coord *)pp1;
115 pdf_coord *p2 = (pdf_coord *)pp2;
116
117 if (pdf_coord__equal(p1, p2))
118 return 0;
119 else
120 return (int) dsign(p1->y - p2->y);
121
122 return 1;
123 }
124 #endif
125
126
127 static /* __inline__ */ int
128 pdf_coord__transform (pdf_coord *p, const pdf_tmatrix *M)
129 {
130 double x, y;
131
132 x = p->x; y = p->y;
133 p->x = x * M->a + y * M->c + M->e;
134 p->y = x * M->b + y * M->d + M->f;
135
136 return 0;
137 }
138
139 static /* __inline__ */ int
140 pdf_coord__itransform (pdf_coord *p, const pdf_tmatrix *M)
141 {
142 pdf_tmatrix W;
143 double x, y;
144 int error;
145
146 error = inversematrix(&W, M);
147 if (error)
148 return error;
149
150 x = p->x; y = p->y;
151 p->x = x * W.a + y * W.c + W.e;
152 p->y = x * W.b + y * W.d + W.f;
153
154 return 0;
155 }
156
157 static /* __inline__ */ int
158 pdf_coord__dtransform (pdf_coord *p, const pdf_tmatrix *M)
159 {
160 double x, y;
161
162 x = p->x; y = p->y;
163 p->x = x * M->a + y * M->c;
164 p->y = x * M->b + y * M->d;
165
166 return 0;
167 }
168
169 static /* __inline__ */ int
170 pdf_coord__idtransform (pdf_coord *p, const pdf_tmatrix *M)
171 {
172 pdf_tmatrix W;
173 double x, y;
174 int error;
175
176 error = inversematrix(&W, M);
177 if (error)
178 return error;
179
180 x = p->x; y = p->y;
181 p->x = x * W.a + y * W.c;
182 p->y = x * W.b + y * W.d;
183
184 return 0;
185 }
186
187
188 /* Modify M itself */
189 void
190 pdf_invertmatrix (pdf_tmatrix *M)
191 {
192 pdf_tmatrix W;
193 double det;
194
195 ASSERT(M);
196
197 det = detP(M);
198 if (fabs(det) < OUR_EPSILON) {
199 WARN("Inverting matrix with zero determinant...");
200 W.a = 1.0; W.c = 0.0;
201 W.b = 0.0; W.d = 1.0;
202 W.e = 0.0; W.f = 0.0;
203 } else {
204 W.a = (M->d) / det; W.b = -(M->b) / det;
205 W.c = -(M->c) / det; W.d = (M->a) / det;
206 W.e = (M->c) * (M->f) - (M->d) * (M->e);
207 W.f = (M->b) * (M->e) - (M->a) * (M->f);
208 W.e /= det; W.f /= det;
209 }
210
211 pdf_copymatrix(M, &W);
212
213 return;
214 }
215
216
217 typedef struct pa_elem_
218 {
219 int type;
220 pdf_coord p[3];
221 } pa_elem;
222
223 /* each subpath delimitted by moveto */
224 struct pdf_path_
225 {
226 int num_paths;
227 int max_paths;
228 pa_elem *path;
229 };
230
231 static const struct {
232 char opchr; /* PDF operator char */
233 int n_pts; /* number of *points* */
234 const char *strkey;
235 } petypes[] = {
236 #define PE_TYPE__INVALID -1
237 #define PE_TYPE__MOVETO 0
238 {'m', 1, "moveto" },
239 #define PE_TYPE__LINETO 1
240 {'l', 1, "lineto" },
241 #define PE_TYPE__CURVETO 2
242 {'c', 3, "curveto" },
243 /* no PS correspondence for v and y */
244 #define PE_TYPE__CURVETO_V 3
245 {'v', 2, "vcurveto"}, /* current point replicated */
246 #define PE_TYPE__CURVETO_Y 4
247 {'y', 2, "ycurveto"}, /* last point replicated */
248 #define PE_TYPE__CLOSEPATH 5
249 {'h', 0, "closepath"},
250 #define PE_TYPE__TERMINATE 6
251 {' ', 0, NULL}
252 };
253
254 #define PE_VALID(p) ((p) && \
255 (p)->type > PE_TYPE__INVALID && (p)->type < PE_TYPE__TERMINATE)
256 #define PE_N_PTS(p) (PE_VALID((p)) ? petypes[(p)->type].n_pts : 0)
257 #define PE_OPCHR(p) (PE_VALID((p)) ? petypes[(p)->type].opchr : ' ')
258
259 #define PA_LENGTH(pa) ((pa)->num_paths)
260
261 #define GS_FLAG_CURRENTPOINT_SET (1 << 0)
262
263
264 #define FORMAT_BUFF_LEN 1024
265 static char fmt_buf[FORMAT_BUFF_LEN];
266
267 static void
268 init_a_path (pdf_path *p)
269 {
270 ASSERT(p);
271
272 p->num_paths = 0;
273 p->max_paths = 0;
274 p->path = NULL;
275
276 return;
277 }
278
279 static void
280 pdf_path__clearpath (pdf_path *p)
281 {
282 ASSERT(p);
283
284 p->num_paths = 0;
285
286 return;
287 }
288
289 static int
290 pdf_path__growpath (pdf_path *p, int max_pe)
291 {
292 if (max_pe < p->max_paths)
293 return 0;
294
295 p->max_paths = MAX(p->max_paths + 8, max_pe);
296 p->path = RENEW(p->path, p->max_paths, pa_elem);
297
298 return 0;
299 }
300
301 static void
302 clear_a_path (pdf_path *p)
303 {
304 ASSERT(p);
305
306 if (p->path)
307 RELEASE(p->path);
308 p->path = NULL;
309 p->num_paths = 0;
310 p->max_paths = 0;
311
312 return;
313 }
314
315 static int
316 pdf_path__copypath (pdf_path *p1, const pdf_path *p0)
317 {
318 pa_elem *pe0, *pe1;
319 int i;
320
321 pdf_path__growpath(p1, PA_LENGTH(p0));
322 for (i = 0; i < PA_LENGTH(p0); i++) {
323 pe1 = &(p1->path[i]);
324 pe0 = &(p0->path[i]);
325 /* FIXME */
326 pe1->type = pe0->type;
327 pe1->p[0].x = pe0->p[0].x;
328 pe1->p[0].y = pe0->p[0].y;
329 pe1->p[1].x = pe0->p[1].x;
330 pe1->p[1].y = pe0->p[1].y;
331 pe1->p[2].x = pe0->p[2].x;
332 pe1->p[2].y = pe0->p[2].y;
333 }
334 p1->num_paths = PA_LENGTH(p0);
335
336 return 0;
337 }
338
339
340 /* start new subpath */
341 static int
342 pdf_path__moveto (pdf_path *pa,
343 pdf_coord *cp,
344 const pdf_coord *p0)
345 {
346 pa_elem *pe;
347
348 pdf_path__growpath(pa, PA_LENGTH(pa) + 1);
349 if (PA_LENGTH(pa) > 0) {
350 pe = &pa->path[pa->num_paths-1];
351 if (pe->type == PE_TYPE__MOVETO) {
352 pe->p[0].x = cp->x = p0->x;
353 pe->p[0].y = cp->y = p0->y;
354 return 0;
355 }
356 }
357 pe = &pa->path[pa->num_paths++];
358 pe->type = PE_TYPE__MOVETO;
359 pe->p[0].x = cp->x = p0->x;
360 pe->p[0].y = cp->y = p0->y;
361
362 return 0;
363 }
364
365 /* Do 'compression' of path while adding new path elements.
366 * Sequantial moveto command will be replaced with a
367 * single moveto. If cp is not equal to the last point in pa,
368 * then moveto is inserted (starting new subpath).
369 * FIXME:
370 * 'moveto' must be used to enforce starting new path.
371 * This affects how 'closepath' is treated.
372 */
373 static pa_elem *
374 pdf_path__next_pe (pdf_path *pa, const pdf_coord *cp)
375 {
376 pa_elem *pe;
377
378 pdf_path__growpath(pa, PA_LENGTH(pa) + 2);
379 if (PA_LENGTH(pa) == 0) {
380 pe = &pa->path[pa->num_paths++];
381 pe->type = PE_TYPE__MOVETO;
382 pe->p[0].x = cp->x;
383 pe->p[0].y = cp->y;
384
385 return &pa->path[pa->num_paths++];
386 }
387
388 pe = &pa->path[pa->num_paths-1];
389 switch (pe->type) {
390 case PE_TYPE__MOVETO:
391 pe->p[0].x = cp->x;
392 pe->p[0].y = cp->y;
393 break;
394 case PE_TYPE__LINETO:
395 if (!COORD_EQUAL(&pe->p[0], cp)) {
396 pe = &pa->path[pa->num_paths++];
397 pe->type = PE_TYPE__MOVETO;
398 pe->p[0].x = cp->x;
399 pe->p[0].y = cp->y;
400 }
401 break;
402 case PE_TYPE__CURVETO:
403 if (!COORD_EQUAL(&pe->p[2], cp)) {
404 pe = &pa->path[pa->num_paths++];
405 pe->type = PE_TYPE__MOVETO;
406 pe->p[0].x = cp->x;
407 pe->p[0].y = cp->y;
408 }
409 break;
410 case PE_TYPE__CURVETO_Y:
411 case PE_TYPE__CURVETO_V:
412 if (!COORD_EQUAL(&pe->p[1], cp)) {
413 pe = &pa->path[pa->num_paths++];
414 pe->type = PE_TYPE__MOVETO;
415 pe->p[0].x = cp->x;
416 pe->p[0].y = cp->y;
417 }
418 break;
419 case PE_TYPE__CLOSEPATH:
420 pe = &pa->path[pa->num_paths++];
421 pe->type = PE_TYPE__MOVETO;
422 pe->p[0].x = cp->x;
423 pe->p[0].y = cp->y;
424 break;
425 }
426
427 return &pa->path[pa->num_paths++];
428 }
429
430 static int
431 pdf_path__transform (pdf_path *pa, const pdf_tmatrix *M)
432 {
433 pa_elem *pe;
434 int n = 0, i;
435
436 ASSERT(pa && M);
437
438 for (i = 0; i < PA_LENGTH(pa); i++) {
439 pe = &(pa->path[i]);
440 n = PE_N_PTS(pe);
441 while (n-- > 0)
442 pdf_coord__transform(&(pe->p[n]), M);
443 }
444
445 return 0;
446 }
447
448
449 /* Path Construction */
450 static int
451 pdf_path__lineto (pdf_path *pa,
452 pdf_coord *cp,
453 const pdf_coord *p0)
454 {
455 pa_elem *pe;
456
457 pe = pdf_path__next_pe(pa, cp);
458 pe->type = PE_TYPE__LINETO;
459 pe->p[0].x = cp->x = p0->x;
460 pe->p[0].y = cp->y = p0->y;
461
462 return 0;
463 }
464
465 static int
466 pdf_path__curveto (pdf_path *pa,
467 pdf_coord *cp,
468 const pdf_coord *p0,
469 const pdf_coord *p1,
470 const pdf_coord *p2
471 )
472 {
473 pa_elem *pe;
474
475 pe = pdf_path__next_pe(pa, cp);
476 if (COORD_EQUAL(cp, p0)) {
477 pe->type = PE_TYPE__CURVETO_V;
478 pe->p[0].x = p1->x;
479 pe->p[0].y = p1->y;
480 pe->p[1].x = cp->x = p2->x;
481 pe->p[1].y = cp->y = p2->y;
482 } else if (COORD_EQUAL(p1, p2)) {
483 pe->type = PE_TYPE__CURVETO_Y;
484 pe->p[0].x = p0->x;
485 pe->p[0].y = p0->y;
486 pe->p[1].x = cp->x = p1->x;
487 pe->p[1].y = cp->y = p1->y;
488 } else {
489 pe->type = PE_TYPE__CURVETO;
490 pe->p[0].x = p0->x;
491 pe->p[0].y = p0->y;
492 pe->p[1].x = p1->x;
493 pe->p[1].y = p1->y;
494 pe->p[2].x = cp->x = p2->x;
495 pe->p[2].y = cp->y = p2->y;
496 }
497
498 return 0;
499 }
500
501 #if 0
502 #define QB_TWO_THIRD (2.0/3.0)
503 #define QB_ONE_THIRD (1.0/3.0)
504
505 static int
506 pdf_path__curveto_QB (pdf_path *pa,
507 pdf_coord *cp,
508 const pdf_coord *p0,
509 const pdf_coord *p1
510 )
511 {
512 pdf_coord q0, q1;
513
514 q0.x = cp->x + QB_TWO_THIRD * (p0->x - cp->x);
515 q0.y = cp->y + QB_TWO_THIRD * (p0->y - cp->y);
516 q1.x = p0->x + QB_ONE_THIRD * (p1->x - p0->x);
517 q1.y = p0->y + QB_ONE_THIRD * (p1->y - p0->y);
518 /* q2 == p1 */
519
520 return pdf_path__curveto(pa, cp, &q0, &q1, p1);
521 }
522 #endif
523
524
525 /* This isn't specified as cp to somewhere. */
526 static int
527 pdf_path__elliptarc (pdf_path *pa,
528 pdf_coord *cp,
529 const pdf_coord *ca, /* ellipsis center */
530 double r_x, /* x radius */
531 double r_y, /* y radius */
532 double xar, /* x-axis-rotation (deg!) */
533 double a_0, /* start angle */
534 double a_1, /* stop angle */
535 int a_d /* arc orientation */
536 )
537 {
538 double b, b_x, b_y;
539 double d_a, q;
540 pdf_coord p0, p1, p2, p3;
541 pdf_coord e0, e1;
542 pdf_tmatrix T;
543 int n_c; /* number of segments */
544 int i, error = 0;
545
546 if (fabs(r_x) < OUR_EPSILON ||
547 fabs(r_y) < OUR_EPSILON)
548 return -1;
549
550 if (a_d < 0) {
551 for ( ; a_1 > a_0; a_1 -= 360.0);
552 } else {
553 for ( ; a_1 < a_0; a_0 -= 360.0);
554 }
555
556 d_a = a_1 - a_0;
557 for (n_c = 1; fabs(d_a) > 90.0 * n_c; n_c++);
558 d_a /= n_c;
559 if (fabs(d_a) < OUR_EPSILON)
560 return -1;
561
562 a_0 *= M_PI / 180.0;
563 a_1 *= M_PI / 180.0;
564 d_a *= M_PI / 180.0;
565 xar *= M_PI / 180.0;
566 T.a = cos(xar); T.c = -sin(xar);
567 T.b = -T.c ; T.d = T.a;
568 T.e = 0.0 ; T.f = 0.0;
569
570 /* A parameter that controls cb-curve (off-curve) points */
571 b = 4.0 * (1.0 - cos(.5 * d_a)) / (3.0 * sin(.5 * d_a));
572 b_x = r_x * b;
573 b_y = r_y * b;
574
575 p0.x = r_x * cos(a_0);
576 p0.y = r_y * sin(a_0);
577 pdf_coord__transform(&p0, &T);
578 p0.x += ca->x; p0.y += ca->y;
579 if (PA_LENGTH(pa) == 0) {
580 pdf_path__moveto(pa, cp, &p0);
581 } else if (!COORD_EQUAL(cp, &p0)) {
582 pdf_path__lineto(pa, cp, &p0); /* add line seg */
583 }
584 for (i = 0; !error && i < n_c; i++) {
585 q = a_0 + i * d_a;
586 e0.x = cos(q); e0.y = sin(q);
587 e1.x = cos(q + d_a); e1.y = sin(q + d_a);
588
589 /* Condition for tangent vector requirs
590 * d1 = p1 - p0 = f ( sin a, -cos a)
591 * d2 = p2 - p3 = g ( sin b, -cos b)
592 * and from symmetry
593 * g^2 = f^2
594 */
595 p0.x = r_x * e0.x; /* s.p. */
596 p0.y = r_y * e0.y;
597 p3.x = r_x * e1.x; /* e.p. */
598 p3.y = r_y * e1.y;
599
600 p1.x = -b_x * e0.y;
601 p1.y = b_y * e0.x;
602 p2.x = b_x * e1.y;
603 p2.y = -b_y * e1.x;
604
605 pdf_coord__transform(&p0, &T);
606 pdf_coord__transform(&p1, &T);
607 pdf_coord__transform(&p2, &T);
608 pdf_coord__transform(&p3, &T);
609 p0.x += ca->x; p0.y += ca->y;
610 p3.x += ca->x; p3.y += ca->y;
611 p1.x += p0.x ; p1.y += p0.y ;
612 p2.x += p3.x ; p2.y += p3.y ;
613
614 error = pdf_path__curveto(pa, &p0, &p1, &p2, &p3);
615 cp->x = p3.x; cp->y = p3.y;
616 }
617
618 return error;
619 }
620
621 static int
622 pdf_path__closepath (pdf_path *pa, pdf_coord *cp /* no arg */)
623 {
624 pa_elem *pe = NULL;
625 int i;
626
627 /* search for start point of the last subpath */
628 for (i = PA_LENGTH(pa) - 1; i >= 0; i--) {
629 pe = &pa->path[i];
630 if (pe->type == PE_TYPE__MOVETO)
631 break;
632 }
633
634 if (!pe || i < 0)
635 return -1; /* No path or no start point(!) */
636
637 cp->x = pe->p[0].x;
638 cp->y = pe->p[0].y;
639
640 pdf_path__growpath(pa, PA_LENGTH(pa) + 1);
641
642 /* NOTE:
643 * Manually closed path without closepath is not
644 * affected by linejoin. A path with coincidental
645 * starting and ending point is not the same as
646 * 'closed' path.
647 */
648 pe = &pa->path[pa->num_paths++];
649 pe->type = PE_TYPE__CLOSEPATH;
650
651 return 0;
652 }
653
654 /*
655 * x y width height re
656 *
657 * is equivalent to
658 *
659 * x y m
660 * (x + width) y l
661 * (x + width) (y + height) l
662 * x (y + height) l
663 * h
664 */
665 /* Just for quick test */
666 static /* __inline__ */ int
667 pdf_path__isarect (pdf_path *pa,
668 int f_ir /* fill-rule is ignorable */
669 )
670 {
671 pa_elem *pe0, *pe1, *pe2, *pe3, *pe4;
672
673 if (PA_LENGTH(pa) == 5) {
674 pe0 = &(pa->path[0]);
675 pe1 = &(pa->path[1]);
676 pe2 = &(pa->path[2]);
677 pe3 = &(pa->path[3]);
678 pe4 = &(pa->path[4]);
679 if (pe0->type == PE_TYPE__MOVETO &&
680 pe1->type == PE_TYPE__LINETO &&
681 pe2->type == PE_TYPE__LINETO &&
682 pe3->type == PE_TYPE__LINETO &&
683 pe4->type == PE_TYPE__CLOSEPATH) {
684 if (pe1->p[0].y - pe0->p[0].y == 0 &&
685 pe2->p[0].x - pe1->p[0].x == 0 &&
686 pe3->p[0].y - pe2->p[0].y == 0) {
687 if (pe1->p[0].x - pe0->p[0].x
688 == pe2->p[0].x - pe3->p[0].x) {
689 return 1;
690 }
691 /* Winding number is different but ignore it here. */
692 } else if (f_ir &&
693 pe1->p[0].x - pe0->p[0].x == 0 &&
694 pe2->p[0].y - pe1->p[0].y == 0 &&
695 pe3->p[0].x - pe2->p[0].x == 0) {
696 if (pe1->p[0].y - pe0->p[0].y
697 == pe2->p[0].y - pe3->p[0].y) {
698 return 1;
699 }
700 }
701 }
702 }
703
704 return 0;
705 }
706
707 /* Path Painting */
708 /* F is obsoleted */
709 #define PT_OP_VALID(c) ( \
710 (c) == 'f' || (c) == 'F' || \
711 (c) == 's' || (c) == 'S' || \
712 (c) == 'b' || (c) == 'B' || \
713 (c) == 'W' \
714 )
715
716 static /* __inline__ */ int
717 INVERTIBLE_MATRIX (const pdf_tmatrix *M)
718 {
719 if (fabs(detP(M)) < OUR_EPSILON) {
720 WARN("Transformation matrix not invertible.");
721 WARN("--- M = [%g %g %g %g %g %g]",
722 M->a, M->b, M->c, M->d, M->e, M->f);
723 return -1;
724 }
725 return 0;
726 }
727
728 /* rectfill, rectstroke, rectclip, recteoclip
729 *
730 * Draw isolated rectangle without actually doing gsave/grestore operation.
731 *
732 * TODO:
733 * linestyle, fill-opacity, stroke-opacity,....
734 * As this routine draw a single graphics object
735 * each time, there should be options for specifying
736 * various drawing styles, which might inherite
737 * current graphcs state parameter.
738 */
739 static int
740 pdf_dev__rectshape (const pdf_rect *r,
741 const pdf_tmatrix *M,
742 char opchr
743 )
744 {
745 char *buf = fmt_buf;
746 int len = 0;
747 int isclip = 0;
748 pdf_coord p;
749 double wd, ht;
750
751 ASSERT(r && PT_OP_VALID(opchr));
752
753 isclip = (opchr == 'W') ? 1 : 0;
754
755 /* disallow matrix for clipping.
756 * q ... clip Q does nothing and
757 * n M cm ... clip n alter CTM.
758 */
759 if (M && (isclip ||
760 !INVERTIBLE_MATRIX(M)))
761 return -1;
762
763 graphics_mode();
764
765 if (!isclip) {
766 pdf_dev_gsave();
767 }
768 pdf_dev_newpath();
769 if (!isclip && M) {
770 pdf_dev_concat(M);
771 }
772
773 p.x = r->llx; p.y = r->lly;
774 wd = r->urx - r->llx;
775 ht = r->ury - r->lly;
776 buf[len++] = ' ';
777 len += pdf_sprint_coord (buf + len, &p);
778 buf[len++] = ' ';
779 len += pdf_sprint_length(buf + len, wd);
780 buf[len++] = ' ';
781 len += pdf_sprint_length(buf + len, ht);
782 buf[len++] = ' ';
783 buf[len++] = 'r'; buf[len++] = 'e';
784
785 buf[len++] = ' ';
786 buf[len++] = opchr;
787
788 pdf_doc_add_page_content(buf, len);
789
790 if (isclip) {
791 pdf_dev_newpath();
792 } else {
793 pdf_dev_grestore();
794 }
795
796 return 0;
797 }
798
799 /* FIXME */
800 static int
801 pdf_dev__flushpath (pdf_path *pa,
802 char opchr,
803 int rule,
804 int ignore_rule)
805 {
806 pa_elem *pe, *pe1;
807 char *b = fmt_buf;
808 int b_len = FORMAT_BUFF_LEN;
809 pdf_rect r; /* FIXME */
810 pdf_coord *pt;
811 int n_pts, n_seg;
812 int len = 0;
813 int isrect, i, j;
814
815 ASSERT(pa && PT_OP_VALID(opchr));
816
817 if (PA_LENGTH(pa) <= 0)
818 return 0;
819
820 graphics_mode();
821 isrect = pdf_path__isarect(pa, ignore_rule);
822 if (isrect) {
823 pe = &(pa->path[0]);
824 pe1 = &(pa->path[2]);
825
826 r.llx = pe->p[0].x;
827 r.lly = pe->p[0].y;
828 r.urx = pe1->p[0].x - pe->p[0].x; /* width... */
829 r.ury = pe1->p[0].y - pe->p[0].y; /* height... */
830
831 b[len++] = ' ';
832 len += pdf_sprint_rect(b + len, &r);
833 b[len++] = ' ';
834 b[len++] = 'r';
835 b[len++] = 'e';
836 pdf_doc_add_page_content(b, len); /* op: re */
837 len = 0;
838 } else {
839 n_seg = PA_LENGTH(pa);
840 for (i = 0, len = 0, pe = &pa->path[0];
841 i < n_seg; pe++, i++) {
842 n_pts = PE_N_PTS(pe);
843 for (j = 0, pt = &pe->p[0];
844 j < n_pts; j++, pt++) {
845 b[len++] = ' ';
846 len += pdf_sprint_coord(b + len, pt);
847 }
848 b[len++] = ' ';
849 b[len++] = PE_OPCHR(pe);
850 if (len + 128 > b_len) {
851 pdf_doc_add_page_content(b, len); /* op: m l c v y h */
852 len = 0;
853 }
854 }
855 if (len > 0) {
856 pdf_doc_add_page_content(b, len); /* op: m l c v y h */
857 len = 0;
858 }
859 }
860
861 b[len++] = ' ';
862 b[len++] = opchr;
863 if (rule == PDF_FILL_RULE_EVENODD)
864 b[len++] = '*';
865
866 pdf_doc_add_page_content(b, len); /* op: f F s S b B W f* F* s* S* b* B* W* */
867
868 return 0;
869 }
870
871 /* ExtGState stack */
872 static dpx_stack xgs_stack;
873 static int xgs_count = 0;
874
875 struct xgs_res {
876 pdf_obj *object;
877 pdf_obj *accumlated;
878 };
879
880 static const char default_xgs[] = "\
881 << /Type /ExtGState\
882 /LW 1 /LC 0 /LJ 0 /ML 10 /D [[] 0]\
883 /RI /RelativeColorimetric /SA false /BM /Normal /SMask /None\
884 /AIS false /TK false /CA 1 /ca 1\
885 /OP false /op false /OPM 0 /FL 1\
886 >>";
887
888 static void
889 init_xgstate (void)
890 {
891 dpx_stack_init(&xgs_stack);
892 xgs_count = 0;
893
894 return;
895 }
896
897 static void
898 clear_xgstate (void)
899 {
900 struct xgs_res *xgs;
901
902 while ((xgs = dpx_stack_pop(&xgs_stack)) != NULL) {
903 pdf_release_obj(xgs->object);
904 pdf_release_obj(xgs->accumlated);
905 RELEASE(xgs);
906 }
907
908 return;
909 }
910
911 /* Graphics State */
912 typedef struct pdf_gstate_
913 {
914 pdf_coord cp;
915
916 pdf_tmatrix matrix; /* cm, - */
917
918 pdf_color strokecolor;
919 pdf_color fillcolor;
920 /* colorspace here */
921
922 struct {
923 int num_dash;
924 double pattern[PDF_DASH_SIZE_MAX];
925 double offset;
926 } linedash; /* d, D */
927
928 double linewidth; /* w, LW */
929
930 int linecap; /* J, LC */
931 int linejoin; /* j, LJ */
932 double miterlimit; /* M, ML */
933
934 int flatness; /* i, FL, 0 to 100 (0 for use device-default) */
935
936 /* internal */
937 pdf_path path;
938 int flags;
939
940 pdf_obj *extgstate;
941 } pdf_gstate;
942
943 static dpx_stack gs_stack;
944
945 static void
946 init_a_gstate (pdf_gstate *gs)
947 {
948 gs->cp.x = 0.0;
949 gs->cp.y = 0.0;
950
951 pdf_setmatrix(&gs->matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
952
953 pdf_color_black(&gs->strokecolor);
954 pdf_color_black(&gs->fillcolor);
955
956 gs->linedash.num_dash = 0;
957 gs->linedash.offset = 0;
958 gs->linecap = 0;
959 gs->linejoin = 0;
960 gs->linewidth = 1.0;
961 gs->miterlimit = 10.0;
962
963 gs->flatness = 1; /* default to 1 in PDF */
964
965 /* Internal variables */
966 gs->flags = 0;
967 init_a_path(&gs->path);
968
969 gs->extgstate = NULL;
970
971 return;
972 }
973
974 static void
975 clear_a_gstate (pdf_gstate *gs)
976 {
977 clear_a_path(&gs->path);
978 if (gs->extgstate)
979 pdf_release_obj(gs->extgstate);
980 memset(gs, 0, sizeof(pdf_gstate));
981
982 return;
983 }
984
985 static void
986 copy_a_gstate (pdf_gstate *gs1, pdf_gstate *gs2)
987 {
988 int i;
989
990 ASSERT(gs1 && gs2);
991
992 gs1->cp.x = gs2->cp.x;
993 gs1->cp.y = gs2->cp.y;
994
995 pdf_copymatrix(&gs1->matrix, &gs2->matrix);
996
997 /* TODO:
998 * Path should be linked list and gsave only
999 * record starting point within path rather than
1000 * copying whole path.
1001 */
1002 pdf_path__copypath(&gs1->path, &gs2->path);
1003
1004 gs1->linedash.num_dash = gs2->linedash.num_dash;
1005 for (i = 0; i < gs2->linedash.num_dash; i++) {
1006 gs1->linedash.pattern[i] = gs2->linedash.pattern[i];
1007 }
1008 gs1->linedash.offset = gs2->linedash.offset;
1009
1010 gs1->linecap = gs2->linecap;
1011 gs1->linejoin = gs2->linejoin;
1012 gs1->linewidth = gs2->linewidth;
1013 gs1->miterlimit = gs2->miterlimit;
1014 gs1->flatness = gs2->flatness;
1015
1016 pdf_color_copycolor(&gs1->fillcolor , &gs2->fillcolor);
1017 pdf_color_copycolor(&gs1->strokecolor, &gs2->strokecolor);
1018
1019 gs1->extgstate = gs2->extgstate ? pdf_link_obj(gs2->extgstate) : NULL;
1020
1021 return;
1022 }
1023
1024 static int
1025 pdf_dev_set_xgstate (pdf_obj *diff, pdf_obj *accumlated)
1026 {
1027 dpx_stack *gss = &gs_stack;
1028 pdf_gstate *gs = dpx_stack_top(gss);
1029 char buf[64], res_name[16];
1030 int len = 0, id;
1031
1032 id = xgs_count;
1033 snprintf(res_name, 16, "DPX_GS%d", id);
1034 res_name[15] = '\0';
1035 len += snprintf(buf, 64, " /%s gs", res_name);
1036 pdf_doc_add_page_content(buf, len);
1037 pdf_doc_add_page_resource("ExtGState", res_name, pdf_link_obj(diff));
1038 if (gs->extgstate)
1039 pdf_release_obj(gs->extgstate);
1040 gs->extgstate = pdf_link_obj(accumlated);
1041 xgs_count++;
1042
1043 return 0;
1044 }
1045
1046 int
1047 pdf_dev_reset_xgstate (int force)
1048 {
1049 dpx_stack *gss = &gs_stack;
1050 pdf_gstate *gs;
1051 pdf_obj *current, *target, *keys, *diff;
1052 struct xgs_res *xgs;
1053 int i, need_reset = 0;
1054
1055 gs = dpx_stack_top(gss);
1056 xgs = dpx_stack_top(&xgs_stack);
1057 if (xgs) {
1058 target = pdf_link_obj(xgs->accumlated);
1059 } else {
1060 const char *ptr, *endptr;
1061
1062 if (!gs->extgstate && !force)
1063 return 0;
1064 ptr = default_xgs;
1065 endptr = ptr + strlen(ptr);
1066 target = parse_pdf_dict(&ptr, endptr, NULL);
1067 }
1068 if (gs->extgstate) {
1069 current = pdf_link_obj(gs->extgstate);
1070 } else {
1071 const char *ptr, *endptr;
1072 ptr = default_xgs;
1073 endptr = ptr + strlen(ptr);
1074 current = parse_pdf_dict(&ptr, endptr, NULL);
1075 }
1076
1077 diff = pdf_new_dict();
1078 keys = pdf_dict_keys(target);
1079 for (i = 0; i < pdf_array_length(keys); i++) {
1080 pdf_obj *key, *value1, *value2;
1081 int is_diff = 0;
1082
1083 key = pdf_get_array(keys, i);
1084 value1 = pdf_lookup_dict(target, pdf_name_value(key));
1085 value2 = pdf_lookup_dict(current, pdf_name_value(key));
1086 is_diff = pdf_compare_object(value1, value2);
1087 if (is_diff) {
1088 pdf_add_dict(diff, pdf_link_obj(key), pdf_link_obj(value1));
1089 need_reset = 1;
1090 }
1091 }
1092 pdf_release_obj(keys);
1093 if (need_reset)
1094 pdf_dev_set_xgstate(diff, target);
1095 pdf_release_obj(diff);
1096 pdf_release_obj(current);
1097 pdf_release_obj(target);
1098
1099 return 0;
1100 }
1101
1102 void
1103 pdf_dev_xgstate_push (pdf_obj *object)
1104 {
1105 struct xgs_res *target, *current;
1106 pdf_obj *accumlated;
1107
1108 target = NEW(1, struct xgs_res);
1109 target->object = object;
1110 current = dpx_stack_top(&xgs_stack);
1111 if (!current) {
1112 const char *ptr, *endptr;
1113 ptr = default_xgs;
1114 endptr = ptr + strlen(default_xgs);
1115 accumlated = parse_pdf_dict(&ptr, endptr, NULL);
1116 } else {
1117 accumlated = pdf_new_dict();
1118 pdf_merge_dict(accumlated, current->accumlated);
1119 }
1120 pdf_merge_dict(accumlated, object);
1121 target->accumlated = accumlated;
1122 dpx_stack_push(&xgs_stack, target);
1123
1124 pdf_dev_set_xgstate(target->object, target->accumlated);
1125
1126 return;
1127 }
1128
1129 void
1130 pdf_dev_xgstate_pop (void)
1131 {
1132 struct xgs_res *current, *target;
1133 pdf_obj *accumlated, *revert, *keys;
1134 int i;
1135
1136 current = dpx_stack_pop(&xgs_stack);
1137 target = dpx_stack_top(&xgs_stack);
1138 if (!current) {
1139 WARN("Too many pop operation for ExtGState!");
1140 return;
1141 }
1142 if (!target) {
1143 const char *ptr, *endptr;
1144 ptr = default_xgs;
1145 endptr = ptr + strlen(default_xgs);
1146 accumlated = parse_pdf_dict(&ptr, endptr, NULL);
1147 } else {
1148 accumlated = pdf_link_obj(target->accumlated);
1149 }
1150 keys = pdf_dict_keys(current->object);
1151 revert = pdf_new_dict();
1152 for (i = 0; i < pdf_array_length(keys); i++) {
1153 pdf_obj *key, *value;
1154 key = pdf_get_array(keys, i);
1155 value = pdf_lookup_dict(accumlated, pdf_name_value(key));
1156 if (!value) {
1157 WARN("No previous ExtGState entry known for \"%s\", ignoring...", pdf_name_value(key));
1158 } else {
1159 pdf_add_dict(revert, pdf_link_obj(key), pdf_link_obj(value));
1160 }
1161 }
1162 pdf_dev_set_xgstate(revert, accumlated);
1163 pdf_release_obj(revert);
1164 pdf_release_obj(keys);
1165 pdf_release_obj(accumlated);
1166
1167 pdf_release_obj(current->object);
1168 pdf_release_obj(current->accumlated);
1169 RELEASE(current);
1170
1171 return;
1172 }
1173
1174 void
1175 pdf_dev_init_gstates (void)
1176 {
1177 pdf_gstate *gs;
1178
1179 dpx_stack_init(&gs_stack);
1180
1181 gs = NEW(1, pdf_gstate);
1182 init_a_gstate(gs);
1183
1184 dpx_stack_push(&gs_stack, gs); /* Initial state */
1185 init_xgstate();
1186
1187 return;
1188 }
1189
1190 void
1191 pdf_dev_clear_gstates (void)
1192 {
1193 pdf_gstate *gs;
1194
1195 if (dpx_stack_depth(&gs_stack) > 1) /* at least 1 elem. */
1196 WARN("GS stack depth is not zero at the end of the document.");
1197
1198 while ((gs = dpx_stack_pop(&gs_stack)) != NULL) {
1199 clear_a_gstate(gs);
1200 RELEASE(gs);
1201 }
1202
1203 clear_xgstate();
1204
1205 return;
1206 }
1207
1208 int
1209 pdf_dev_gsave (void)
1210 {
1211 pdf_gstate *gs0, *gs1;
1212
1213 gs0 = dpx_stack_top(&gs_stack);
1214 gs1 = NEW(1, pdf_gstate);
1215 init_a_gstate(gs1);
1216 copy_a_gstate(gs1, gs0);
1217 dpx_stack_push(&gs_stack, gs1);
1218
1219 pdf_doc_add_page_content(" q", 2); /* op: q */
1220
1221 return 0;
1222 }
1223
1224 int
1225 pdf_dev_grestore (void)
1226 {
1227 pdf_gstate *gs;
1228
1229 if (dpx_stack_depth(&gs_stack) <= 1) { /* Initial state at bottom */
1230 WARN("Too many grestores.");
1231 return -1;
1232 }
1233
1234 gs = dpx_stack_pop(&gs_stack);
1235 clear_a_gstate(gs);
1236 RELEASE(gs);
1237
1238 pdf_doc_add_page_content(" Q", 2); /* op: Q */
1239
1240 pdf_dev_reset_fonts(0);
1241
1242 return 0;
1243 }
1244
1245
1246 int
1247 pdf_dev_push_gstate (void)
1248 {
1249 dpx_stack *gss = &gs_stack;
1250 pdf_gstate *gs0;
1251
1252 gs0 = NEW(1, pdf_gstate);
1253
1254 init_a_gstate(gs0);
1255
1256 dpx_stack_push(gss, gs0);
1257
1258 return 0;
1259 }
1260
1261
1262 int
1263 pdf_dev_pop_gstate (void)
1264 {
1265 dpx_stack *gss = &gs_stack;
1266 pdf_gstate *gs;
1267
1268 if (dpx_stack_depth(gss) <= 1) { /* Initial state at bottom */
1269 WARN("Too many grestores.");
1270 return -1;
1271 }
1272
1273 gs = dpx_stack_pop(gss);
1274 clear_a_gstate(gs);
1275 RELEASE(gs);
1276
1277 return 0;
1278 }
1279
1280
1281 int
1282 pdf_dev_current_depth (void)
1283 {
1284 return (dpx_stack_depth(&gs_stack) - 1); /* 0 means initial state */
1285 }
1286
1287 void
1288 pdf_dev_grestore_to (int depth)
1289 {
1290 dpx_stack *gss = &gs_stack;
1291 pdf_gstate *gs;
1292
1293 ASSERT(depth >= 0);
1294
1295 if (dpx_stack_depth(gss) > depth + 1) {
1296 WARN("Closing pending transformations at end of page/XObject.");
1297 }
1298
1299 while (dpx_stack_depth(gss) > depth + 1) {
1300 pdf_doc_add_page_content(" Q", 2); /* op: Q */
1301 gs = dpx_stack_pop(gss);
1302 clear_a_gstate(gs);
1303 RELEASE(gs);
1304 }
1305 pdf_dev_reset_fonts(0);
1306
1307 return;
1308 }
1309
1310 int
1311 pdf_dev_currentpoint (pdf_coord *p)
1312 {
1313 dpx_stack *gss = &gs_stack;
1314 pdf_gstate *gs = dpx_stack_top(gss);
1315 pdf_coord *cpt = &gs->cp;
1316
1317 ASSERT(p);
1318
1319 p->x = cpt->x; p->y = cpt->y;
1320
1321 return 0;
1322 }
1323
1324 int
1325 pdf_dev_currentmatrix (pdf_tmatrix *M)
1326 {
1327 dpx_stack *gss = &gs_stack;
1328 pdf_gstate *gs = dpx_stack_top(gss);
1329 pdf_tmatrix *CTM = &gs->matrix;
1330
1331 ASSERT(M);
1332
1333 pdf_copymatrix(M, CTM);
1334
1335 return 0;
1336 }
1337
1338 #if 0
1339 int
1340 pdf_dev_currentcolor (pdf_color *color, int is_fill)
1341 {
1342 dpx_stack *gss = &gs_stack;
1343 pdf_gstate *gs = dpx_stack_top(gss);
1344 pdf_color *fcl = &gs->fillcolor;
1345 pdf_color *scl = &gs->strokecolor;
1346
1347 ASSERT(color);
1348
1349 pdf_color_copycolor(color, is_fill ? fcl : scl);
1350
1351 return 0;
1352 }
1353 #endif /* 0 */
1354
1355 /*
1356 * FIXME: Please use a macro instead of using 0x20!
1357 * mask == 0 means stroking color, mask == 0x20 nonstroking color
1358 *
1359 * force == 1 means that operators will be generated even if
1360 * the color is the same as the current graphics state color
1361 */
1362 void
1363 pdf_dev_set_color (const pdf_color *color, char mask, int force)
1364 {
1365 int len;
1366
1367 pdf_gstate *gs = dpx_stack_top(&gs_stack);
1368 pdf_color *current = mask ? &gs->fillcolor : &gs->strokecolor;
1369
1370 if (!(pdf_dev_get_param(PDF_DEV_PARAM_COLORMODE) &&
1371 (force || pdf_color_compare(color, current))))
1372 /* If "color" is already the current color, then do nothing
1373 * unless a color operator is forced
1374 */
1375 return;
1376
1377 graphics_mode();
1378 len = pdf_color_set_color(color, fmt_buf, FORMAT_BUFF_LEN, mask);
1379 pdf_doc_add_page_content(fmt_buf, len); /* op: RG K G rg k g etc. */
1380 pdf_color_copycolor(current, color);
1381 }
1382
1383 int
1384 pdf_dev_concat (const pdf_tmatrix *M)
1385 {
1386 dpx_stack *gss = &gs_stack;
1387 pdf_gstate *gs = dpx_stack_top(gss);
1388 pdf_path *cpa = &gs->path;
1389 pdf_coord *cpt = &gs->cp;
1390 pdf_tmatrix *CTM = &gs->matrix;
1391 pdf_tmatrix W = {0, 0, 0, 0, 0, 0}; /* Init to avoid compiler warning */
1392 char *buf = fmt_buf;
1393 int len = 0;
1394
1395 ASSERT(M);
1396
1397 /* Adobe Reader erases page content if there are
1398 * non invertible transformation.
1399 */
1400 if (fabs(detP(M)) < OUR_EPSILON) {
1401 WARN("Transformation matrix not invertible.");
1402 WARN("--- M = [%g %g %g %g %g %g]",
1403 M->a, M->b, M->c, M->d, M->e, M->f);
1404 return -1;
1405 }
1406
1407 if (fabs(M->a - 1.0) > OUR_EPSILON || fabs(M->b) > OUR_EPSILON
1408 || fabs(M->c) > OUR_EPSILON || fabs(M->d - 1.0) > OUR_EPSILON
1409 || fabs(M->e) > OUR_EPSILON || fabs(M->f) > OUR_EPSILON) {
1410 buf[len++] = ' ';
1411 len += pdf_sprint_matrix(buf + len, M);
1412 buf[len++] = ' ';
1413 buf[len++] = 'c';
1414 buf[len++] = 'm';
1415 pdf_doc_add_page_content(buf, len); /* op: cm */
1416
1417 pdf_concatmatrix(CTM, M);
1418 }
1419 inversematrix(&W, M);
1420
1421 pdf_path__transform (cpa, &W);
1422 pdf_coord__transform(cpt, &W);
1423
1424 return 0;
1425 }
1426
1427 /*
1428 * num w LW linewidth (g.t. 0)
1429 * int J LC linecap
1430 * int j LJ linejoin
1431 * num M ML miter limit (g.t. 0)
1432 * array num d D line dash
1433 * int ri RI renderint intnet
1434 * int i FL flatness tolerance (0-100)
1435 * name gs -- name: res. name of ExtGState dict.
1436 */
1437 int
1438 pdf_dev_setmiterlimit (double mlimit)
1439 {
1440 dpx_stack *gss = &gs_stack;
1441 pdf_gstate *gs = dpx_stack_top(gss);
1442 int len = 0;
1443 char *buf = fmt_buf;
1444
1445 if (gs->miterlimit != mlimit) {
1446 buf[len++] = ' ';
1447 len += pdf_sprint_length(buf + len, mlimit);
1448 buf[len++] = ' ';
1449 buf[len++] = 'M';
1450 pdf_doc_add_page_content(buf, len); /* op: M */
1451 gs->miterlimit = mlimit;
1452 }
1453
1454 return 0;
1455 }
1456
1457 int
1458 pdf_dev_setlinecap (int capstyle)
1459 {
1460 dpx_stack *gss = &gs_stack;
1461 pdf_gstate *gs = dpx_stack_top(gss);
1462 int len = 0;
1463 char *buf = fmt_buf;
1464
1465 if (gs->linecap != capstyle) {
1466 len = sprintf(buf, " %d J", capstyle);
1467 pdf_doc_add_page_content(buf, len); /* op: J */
1468 gs->linecap = capstyle;
1469 }
1470
1471 return 0;
1472 }
1473
1474 int
1475 pdf_dev_setlinejoin (int joinstyle)
1476 {
1477 dpx_stack *gss = &gs_stack;
1478 pdf_gstate *gs = dpx_stack_top(gss);
1479 int len = 0;
1480 char *buf = fmt_buf;
1481
1482 if (gs->linejoin != joinstyle) {
1483 len = sprintf(buf, " %d j", joinstyle);
1484 pdf_doc_add_page_content(buf, len); /* op: j */
1485 gs->linejoin = joinstyle;
1486 }
1487
1488 return 0;
1489 }
1490
1491 int
1492 pdf_dev_setlinewidth (double width)
1493 {
1494 dpx_stack *gss = &gs_stack;
1495 pdf_gstate *gs = dpx_stack_top(gss);
1496 int len = 0;
1497 char *buf = fmt_buf;
1498
1499 if (gs->linewidth != width) {
1500 buf[len++] = ' ';
1501 len += pdf_sprint_length(buf + len, width);
1502 buf[len++] = ' ';
1503 buf[len++] = 'w';
1504 pdf_doc_add_page_content(buf, len); /* op: w */
1505 gs->linewidth = width;
1506 }
1507
1508 return 0;
1509 }
1510
1511 int
1512 pdf_dev_setdash (int count, double *pattern, double offset)
1513 {
1514 dpx_stack *gss = &gs_stack;
1515 pdf_gstate *gs = dpx_stack_top(gss);
1516 int len = 0;
1517 char *buf = fmt_buf;
1518 int i;
1519
1520 gs->linedash.num_dash = count;
1521 gs->linedash.offset = offset;
1522 pdf_doc_add_page_content(" [", 2); /* op: */
1523 for (i = 0; i < count; i++) {
1524 buf[0] = ' ';
1525 len = pdf_sprint_length (buf + 1, pattern[i]);
1526 pdf_doc_add_page_content(buf, len + 1); /* op: */
1527 gs->linedash.pattern[i] = pattern[i];
1528 }
1529 pdf_doc_add_page_content("] ", 2); /* op: */
1530 len = pdf_sprint_length (buf, offset);
1531 pdf_doc_add_page_content(buf, len); /* op: */
1532 pdf_doc_add_page_content(" d", 2); /* op: d */
1533
1534 return 0;
1535 }
1536
1537 #if 0
1538 int
1539 pdf_dev_setflat (int flatness)
1540 {
1541 dpx_stack *gss = &gs_stack;
1542 pdf_gstate *gs = dpx_stack_top(gss);
1543 int len = 0;
1544 char *buf = fmt_buf;
1545
1546 if (flatness < 0 || flatness > 100)
1547 return -1;
1548
1549 if (gs->flatness != flatness) {
1550 gs->flatness = flatness;
1551 len = sprintf(buf, " %d i", flatness);
1552 pdf_doc_add_page_content(buf, len); /* op: i */
1553 }
1554
1555 return 0;
1556 }
1557 #endif
1558
1559 /* ZSYUEDVEDEOF */
1560 int
1561 pdf_dev_clip (void)
1562 {
1563 dpx_stack *gss = &gs_stack;
1564 pdf_gstate *gs = dpx_stack_top(gss);
1565 pdf_path *cpa = &gs->path;
1566
1567 return pdf_dev__flushpath(cpa, 'W', PDF_FILL_RULE_NONZERO, 0);
1568 }
1569
1570 int
1571 pdf_dev_eoclip (void)
1572 {
1573 dpx_stack *gss = &gs_stack;
1574 pdf_gstate *gs = dpx_stack_top(gss);
1575 pdf_path *cpa = &gs->path;
1576
1577 return pdf_dev__flushpath(cpa, 'W', PDF_FILL_RULE_EVENODD, 0);
1578 }
1579
1580 int
1581 pdf_dev_flushpath (char p_op, int fill_rule)
1582 {
1583 dpx_stack *gss = &gs_stack;
1584 pdf_gstate *gs = dpx_stack_top(gss);
1585 pdf_path *cpa = &gs->path;
1586 int error = 0;
1587
1588 /* last arg 'ignore_rule' is only for single object
1589 * that can be converted to a rect where fill rule
1590 * is inessential.
1591 */
1592 error = pdf_dev__flushpath(cpa, p_op, fill_rule, 1);
1593 pdf_path__clearpath(cpa);
1594
1595 gs->flags &= ~GS_FLAG_CURRENTPOINT_SET;
1596
1597 return error;
1598 }
1599
1600 int
1601 pdf_dev_newpath (void)
1602 {
1603 dpx_stack *gss = &gs_stack;
1604 pdf_gstate *gs = dpx_stack_top(gss);
1605 pdf_path *p = &gs->path;
1606
1607 if (PA_LENGTH(p) > 0) {
1608 pdf_path__clearpath (p);
1609 }
1610 /* The following is required for "newpath" operator in mpost.c. */
1611 pdf_doc_add_page_content(" n", 2); /* op: n */
1612
1613 return 0;
1614 }
1615
1616 int
1617 pdf_dev_moveto (double x, double y)
1618 {
1619 dpx_stack *gss = &gs_stack;
1620 pdf_gstate *gs = dpx_stack_top(gss);
1621 pdf_path *cpa = &gs->path;
1622 pdf_coord *cpt = &gs->cp;
1623 pdf_coord p;
1624
1625 p.x = x; p.y = y;
1626 return pdf_path__moveto(cpa, cpt, &p); /* cpt updated */
1627 }
1628
1629 int
1630 pdf_dev_rmoveto (double x, double y)
1631 {
1632 dpx_stack *gss = &gs_stack;
1633 pdf_gstate *gs = dpx_stack_top(gss);
1634 pdf_path *cpa = &gs->path;
1635 pdf_coord *cpt = &gs->cp;
1636 pdf_coord p;
1637
1638 p.x = cpt->x + x;
1639 p.y = cpt->y + y;
1640 return pdf_path__moveto(cpa, cpt, &p); /* cpt updated */
1641 }
1642
1643 int
1644 pdf_dev_lineto (double x, double y)
1645 {
1646 dpx_stack *gss = &gs_stack;
1647 pdf_gstate *gs = dpx_stack_top(gss);
1648 pdf_path *cpa = &gs->path;
1649 pdf_coord *cpt = &gs->cp;
1650 pdf_coord p0;
1651
1652 p0.x = x; p0.y = y;
1653
1654 return pdf_path__lineto(cpa, cpt, &p0);
1655 }
1656
1657 int
1658 pdf_dev_rlineto (double x, double y)
1659 {
1660 dpx_stack *gss = &gs_stack;
1661 pdf_gstate *gs = dpx_stack_top(gss);
1662 pdf_path *cpa = &gs->path;
1663 pdf_coord *cpt = &gs->cp;
1664 pdf_coord p0;
1665
1666 p0.x = x + cpt->x; p0.y = y + cpt->y;
1667
1668 return pdf_path__lineto(cpa, cpt, &p0);
1669 }
1670
1671 int
1672 pdf_dev_curveto (double x0, double y0,
1673 double x1, double y1,
1674 double x2, double y2)
1675 {
1676 dpx_stack *gss = &gs_stack;
1677 pdf_gstate *gs = dpx_stack_top(gss);
1678 pdf_path *cpa = &gs->path;
1679 pdf_coord *cpt = &gs->cp;
1680 pdf_coord p0, p1, p2;
1681
1682 p0.x = x0; p0.y = y0;
1683 p1.x = x1; p1.y = y1;
1684 p2.x = x2; p2.y = y2;
1685
1686 return pdf_path__curveto(cpa, cpt, &p0, &p1, &p2);
1687 }
1688
1689 int
1690 pdf_dev_rcurveto (double x0, double y0,
1691 double x1, double y1,
1692 double x2, double y2)
1693 {
1694 dpx_stack *gss = &gs_stack;
1695 pdf_gstate *gs = dpx_stack_top(gss);
1696 pdf_path *cpa = &gs->path;
1697 pdf_coord *cpt = &gs->cp;
1698 pdf_coord p0, p1, p2;
1699
1700 p0.x = x0 + cpt->x; p0.y = y0 + cpt->y;
1701 p1.x = x1 + cpt->x; p1.y = y1 + cpt->y;
1702 p2.x = x2 + cpt->x; p2.y = y2 + cpt->y;
1703
1704 return pdf_path__curveto(cpa, cpt, &p0, &p1, &p2);
1705 }
1706
1707
1708 int
1709 pdf_dev_closepath (void)
1710 {
1711 dpx_stack *gss = &gs_stack;
1712 pdf_gstate *gs = dpx_stack_top(gss);
1713 pdf_coord *cpt = &gs->cp;
1714 pdf_path *cpa = &gs->path;
1715
1716 return pdf_path__closepath(cpa, cpt);
1717 }
1718
1719
1720 void
1721 pdf_dev_dtransform (pdf_coord *p, const pdf_tmatrix *M)
1722 {
1723 dpx_stack *gss = &gs_stack;
1724 pdf_gstate *gs = dpx_stack_top(gss);
1725 pdf_tmatrix *CTM = &gs->matrix;
1726
1727 ASSERT(p);
1728
1729 pdf_coord__dtransform(p, (M ? M : CTM));
1730
1731 return;
1732 }
1733
1734 void
1735 pdf_dev_idtransform (pdf_coord *p, const pdf_tmatrix *M)
1736 {
1737 dpx_stack *gss = &gs_stack;
1738 pdf_gstate *gs = dpx_stack_top(gss);
1739 pdf_tmatrix *CTM = &gs->matrix;
1740
1741 ASSERT(p);
1742
1743 pdf_coord__idtransform(p, (M ? M : CTM));
1744
1745 return;
1746 }
1747
1748 void
1749 pdf_dev_transform (pdf_coord *p, const pdf_tmatrix *M)
1750 {
1751 dpx_stack *gss = &gs_stack;
1752 pdf_gstate *gs = dpx_stack_top(gss);
1753 pdf_tmatrix *CTM = &gs->matrix;
1754
1755 ASSERT(p);
1756
1757 pdf_coord__transform(p, (M ? M : CTM));
1758
1759 return;
1760 }
1761
1762 void
1763 pdf_dev_itransform (pdf_coord *p, const pdf_tmatrix *M)
1764 {
1765 dpx_stack *gss = &gs_stack;
1766 pdf_gstate *gs = dpx_stack_top(gss);
1767 pdf_tmatrix *CTM = &gs->matrix;
1768
1769 ASSERT(p);
1770
1771 pdf_coord__itransform(p, (M ? M : CTM));
1772
1773 return;
1774 }
1775
1776 int
1777 pdf_dev_arc (double c_x , double c_y, double r,
1778 double a_0 , double a_1)
1779 {
1780 dpx_stack *gss = &gs_stack;
1781 pdf_gstate *gs = dpx_stack_top(gss);
1782 pdf_path *cpa = &gs->path;
1783 pdf_coord *cpt = &gs->cp;
1784 pdf_coord c;
1785
1786 c.x = c_x; c.y = c_y;
1787
1788 return pdf_path__elliptarc(cpa, cpt, &c, r, r, 0.0, a_0, a_1, +1);
1789 }
1790
1791 /* *negative* arc */
1792 int
1793 pdf_dev_arcn (double c_x , double c_y, double r,
1794 double a_0 , double a_1)
1795 {
1796 dpx_stack *gss = &gs_stack;
1797 pdf_gstate *gs = dpx_stack_top(gss);
1798 pdf_path *cpa = &gs->path;
1799 pdf_coord *cpt = &gs->cp;
1800 pdf_coord c;
1801
1802 c.x = c_x; c.y = c_y;
1803
1804 return pdf_path__elliptarc(cpa, cpt, &c, r, r, 0.0, a_0, a_1, -1);
1805 }
1806
1807 int
1808 pdf_dev_arcx (double c_x , double c_y,
1809 double r_x , double r_y,
1810 double a_0 , double a_1,
1811 int a_d ,
1812 double xar)
1813 {
1814 dpx_stack *gss = &gs_stack;
1815 pdf_gstate *gs = dpx_stack_top(gss);
1816 pdf_path *cpa = &gs->path;
1817 pdf_coord *cpt = &gs->cp;
1818 pdf_coord c;
1819
1820 c.x = c_x; c.y = c_y;
1821
1822 return pdf_path__elliptarc(cpa, cpt, &c, r_x, r_y, xar, a_0, a_1, a_d);
1823 }
1824
1825 /* Required by Tpic */
1826 int
1827 pdf_dev_bspline (double x0, double y0,
1828 double x1, double y1, double x2, double y2)
1829 {
1830 dpx_stack *gss = &gs_stack;
1831 pdf_gstate *gs = dpx_stack_top(gss);
1832 pdf_path *cpa = &gs->path;
1833 pdf_coord *cpt = &gs->cp;
1834 pdf_coord p1, p2, p3;
1835
1836 p1.x = x0 + 2.0 * (x1 - x0) / 3.0;
1837 p1.y = y0 + 2.0 * (y1 - y0) / 3.0;
1838 p2.x = x1 + (x2 - x1) / 3.0;
1839 p2.y = y1 + (y2 - y1) / 3.0;
1840 p3.x = x2;
1841 p3.y = y2;
1842
1843 return pdf_path__curveto(cpa, cpt, &p1, &p2, &p3);
1844 }
1845
1846 int
1847 pdf_dev_rectstroke (double x, double y,
1848 double w, double h,
1849 const pdf_tmatrix *M /* optional */
1850 )
1851 {
1852 pdf_rect r;
1853
1854 r.llx = x;
1855 r.lly = y;
1856 r.urx = x + w;
1857 r.ury = y + h;
1858
1859 return pdf_dev__rectshape(&r, M, 'S');
1860 }
1861
1862 int
1863 pdf_dev_rectfill (double x, double y,
1864 double w, double h)
1865 {
1866 pdf_rect r;
1867
1868 r.llx = x;
1869 r.lly = y;
1870 r.urx = x + w;
1871 r.ury = y + h;
1872
1873 return pdf_dev__rectshape(&r, NULL, 'f');
1874 }
1875
1876 int
1877 pdf_dev_rectclip (double x, double y,
1878 double w, double h)
1879 {
1880 pdf_rect r;
1881
1882 r.llx = x;
1883 r.lly = y;
1884 r.urx = x + w;
1885 r.ury = y + h;
1886
1887 return pdf_dev__rectshape(&r, NULL, 'W');
1888 }