Debian Codegraph @

texk/dvipdfm-x/spc_tpic.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 "system.h"
28 #include "mem.h"
29 #include "error.h"
30
31 #include "numbers.h"
32 #include "dpxutil.h"
33
34 #include "pdfdoc.h"
35
36 #include "pdfdraw.h"
37 #include "pdfdev.h"
38
39 #include "specials.h"
40 #include "spc_tpic.h"
41
42 #define DEBUG 1
43 #define ENABLE_SPC_NAMESPACE 1
44
45 /*
46 * Following "constant" converts milli-inches to
47 * device (in this case PDF stream) coordinates.
48 */
49
50 #define MI2DEV (0.072/pdf_dev_scale())
51
52 /*
53 * Value for 'sh' command 'g' is interpreted as
54 *
55 * gray color value 1-g for "solid"
56 * opacity value g for "opacity"
57 * shape value g for "shape"
58 */
59 #define TPIC_MODE__FILL_SOLID 0
60 #define TPIC_MODE__FILL_OPACITY 1
61 #define TPIC_MODE__FILL_SHAPE 2
62
63 #ifndef ISBLANK
64 # define ISBLANK(c) ( (c) == ' ' || (c) == '\t' )
65 #endif
66
67 static void
68 skip_blank (const char **pp, const char *endptr)
69 {
70 const char *p = *pp;
71 for ( ; p < endptr && ISBLANK(*p); p++);
72 *pp = p;
73 }
74
75 struct spc_tpic_
76 {
77 struct {
78 int fill;
79 } mode;
80
81 /* state */
82 double pen_size;
83 int fill_shape; /* boolean */
84 double fill_color;
85
86 pdf_coord *points;
87 int num_points;
88 int max_points;
89 };
90
91 #if 1
92 static struct spc_tpic_ _tpic_state;
93 #endif
94
95 /* We use pdf_doc_add_page_content() here
96 * since we always draw isolated graphics.
97 */
98 static void
99 tpic__clear (struct spc_tpic_ *tp)
100 {
101 if (tp->points) {
102 RELEASE(tp->points);
103 tp->points = NULL;
104 }
105 tp->num_points = 0;
106 tp->max_points = 0;
107 tp->fill_shape = 0;
108 tp->fill_color = 0.0;
109 }
110
111
112 static pdf_obj *
113 create_xgstate (double a /* alpha */, int f_ais /* alpha is shape */)
114 {
115 pdf_obj *dict;
116
117 dict = pdf_new_dict();
118 pdf_add_dict(dict,
119 pdf_new_name("Type"),
120 pdf_new_name("ExtGState"));
121 if (f_ais) {
122 pdf_add_dict(dict,
123 pdf_new_name("AIS"),
124 pdf_new_boolean(1));
125 }
126 pdf_add_dict(dict,
127 pdf_new_name("ca"),
128 pdf_new_number(a));
129
130 return dict;
131 }
132
133 static int
134 check_resourcestatus (const char *category, const char *resname)
135 {
136 pdf_obj *dict1, *dict2;
137
138 dict1 = pdf_doc_current_page_resources();
139 if (!dict1)
140 return 0;
141
142 dict2 = pdf_lookup_dict(dict1, category);
143 if (dict2 &&
144 pdf_obj_typeof(dict2) == PDF_DICT) {
145 if (pdf_lookup_dict(dict2, resname))
146 return 1;
147 }
148 return 0;
149 }
150
151 static int
152 set_linestyle (double pn, double da)
153 {
154 double dp[2]; /* dash pattern */
155
156 pdf_dev_setlinejoin(1);
157 pdf_dev_setmiterlimit(1.4);
158 pdf_dev_setlinewidth(pn);
159 if (da > 0.0) {
160 dp[0] = da * 72.0;
161 pdf_dev_setdash(1, dp, 0);
162 pdf_dev_setlinecap(0);
163 } else if (da < 0.0) {
164 dp[0] = pn;
165 dp[1] = -da * 72.0;
166 pdf_dev_setdash(2, dp, 0);
167 pdf_dev_setlinecap(1);
168 } else {
169 pdf_dev_setlinecap(0);
170 }
171
172 return 0;
173 }
174
175 static int
176 set_fillstyle (double g, double a, int f_ais)
177 {
178 pdf_obj *dict;
179 char resname[32];
180 char buf[256];
181 int alp, len = 0;
182
183 if (a > 0.0) {
184 alp = round(100.0 * a);
185 sprintf(resname, "_Tps_a%03d_", alp);
186 if (!check_resourcestatus("ExtGState", resname)) {
187 dict = create_xgstate(ROUND(0.01 * alp, 0.01), f_ais);
188 pdf_doc_add_page_resource("ExtGState",
189 resname, pdf_ref_obj(dict));
190 pdf_release_obj(dict);
191 }
192 len += sprintf(buf + len, " /%s gs", resname);
193
194 pdf_doc_add_page_content(buf, len); /* op: gs */
195 }
196
197 {
198 pdf_color *sc, *fc, new_fc;
199
200 pdf_color_get_current (&sc, &fc); /* get stroking and fill colors */
201 pdf_color_brighten_color(&new_fc, fc, g);
202 pdf_dev_set_nonstrokingcolor(&new_fc);
203 }
204
205 return 0;
206 }
207
208 static void
209 set_styles (struct spc_tpic_ *tp,
210 const pdf_coord *c,
211 int f_fs,
212 int f_vp,
213 double pn,
214 double da) {
215 pdf_tmatrix M;
216
217 pdf_setmatrix (&M, 1.0, 0.0, 0.0, -1.0, c->x, c->y);
218 pdf_dev_concat(&M);
219
220 if (f_vp)
221 set_linestyle(pn, da);
222
223 if (f_fs) {
224 double g, a;
225 int f_ais;
226
227 if (tp->mode.fill == TPIC_MODE__FILL_SOLID || !tp->fill_color) {
228 g = 1.0 - tp->fill_color;
229 a = 0.0;
230 } else {
231 g = 0.0;
232 a = tp->fill_color;
233 }
234
235 f_ais = (tp->mode.fill == TPIC_MODE__FILL_SHAPE) ? 1 : 0;
236
237 set_fillstyle(g, a, f_ais);
238 }
239 }
240
241 static void
242 showpath (int f_vp, int f_fs) /* visible_path, fill_shape */
243 {
244 if (f_vp) {
245 if (f_fs)
246 pdf_dev_flushpath('b', PDF_FILL_RULE_NONZERO);
247 else {
248 pdf_dev_flushpath('S', PDF_FILL_RULE_NONZERO);
249 }
250 } else {
251 /*
252 * Acrobat claims 'Q' as illegal operation when there are unfinished
253 * path (a path without path-painting operator applied)?
254 */
255 if (f_fs)
256 pdf_dev_flushpath('f', PDF_FILL_RULE_NONZERO);
257 else {
258 pdf_dev_newpath();
259 }
260 }
261 }
262
263 #define CLOSED_PATH(s) (\
264 (s)->points[0].x == (s)->points[(s)->num_points-1].x && \
265 (s)->points[0].y == (s)->points[(s)->num_points-1].y \
266 )
267
268 static int
269 tpic__polyline (struct spc_tpic_ *tp,
270 const pdf_coord *c,
271 int f_vp,
272 double da)
273 {
274 double pn = tp->pen_size;
275 int f_fs = tp->fill_shape;
276 int i, error = 0;
277
278 /* Shading is applied only to closed path. */
279 f_fs = CLOSED_PATH(tp) ? f_fs : 0;
280 f_vp = (pn > 0.0) ? f_vp : 0;
281
282 if (f_vp || f_fs) {
283 pdf_dev_gsave();
284
285 set_styles(tp, c, f_fs, f_vp, pn, da);
286
287 pdf_dev_moveto(tp->points[0].x, tp->points[0].y);
288 for (i = 0; i < tp->num_points; i++)
289 pdf_dev_lineto(tp->points[i].x, tp->points[i].y);
290
291 showpath(f_vp, f_fs);
292
293 pdf_dev_grestore();
294 }
295
296 tpic__clear(tp);
297
298 return error;
299 }
300
301 /*
302 * Accroding to
303 * "Tpic: Pic for TEX", Tim Morgan, Original by Brian Kernighan, p.20:
304 *
305 * A spline is a smooth curve guided by a set of straight lines just
306 * like the line above. It begins at the same place, ends at the same
307 * place, and in between is tangent to the mid-point of each guiding
308 * line. The syntax for a spline is identical to a (path) line except
309 * for using spline instead of line.
310 *
311 * Spline is not a curve drawn by spline-fitting points p0, p1, ..., pn,
312 * defined by tpic special "pa" command. Instead, a path defined by set
313 * of points p0, p1, ... is guiding line mentioned above.
314 *
315 * Dvipsk draws them as a straight line from p0 to q1 = (p0 + p1)/2,
316 * followed by a quadratic B-spline curve with starting point q1, (off-
317 * curve) control point p1, end point q2 = (p1 + p2)/2, ..., and a
318 * straight line from qn to pn.
319 */
320
321 static int
322 tpic__spline (struct spc_tpic_ *tp,
323 const pdf_coord *c,
324 int f_vp,
325 double da)
326 {
327 double v[6];
328 double pn = tp->pen_size;
329 int f_fs = tp->fill_shape;
330 int i, error = 0;
331
332 f_fs = CLOSED_PATH(tp) ? f_fs : 0;
333 f_vp = (pn > 0.0) ? f_vp : 0;
334
335 if (f_vp || f_fs) {
336 pdf_dev_gsave();
337
338 set_styles(tp, c, f_fs, f_vp, pn, da);
339
340 pdf_dev_moveto(tp->points[0].x, tp->points[0].y);
341
342 v[0] = 0.5 * (tp->points[0].x + tp->points[1].x);
343 v[1] = 0.5 * (tp->points[0].y + tp->points[1].y);
344 pdf_dev_lineto(v[0], v[1]);
345 for (i = 1; i < tp->num_points - 1; i++) {
346 /* B-spline control points */
347 v[0] = 0.5 * (tp->points[i-1].x + tp->points[i].x);
348 v[1] = 0.5 * (tp->points[i-1].y + tp->points[i].y);
349 v[2] = tp->points[i].x;
350 v[3] = tp->points[i].y;
351 v[4] = 0.5 * (tp->points[i].x + tp->points[i+1].x);
352 v[5] = 0.5 * (tp->points[i].y + tp->points[i+1].y);
353 pdf_dev_bspline(v[0], v[1], v[2], v[3], v[4], v[5]);
354 }
355 pdf_dev_lineto(tp->points[i].x, tp->points[i].y);
356
357 showpath(f_vp, f_fs);
358
359 pdf_dev_grestore();
360 }
361 tpic__clear(tp);
362
363 return error;
364 }
365
366 static int
367 tpic__arc (struct spc_tpic_ *tp,
368 const pdf_coord *c,
369 int f_vp,
370 double da,
371 double *v /* 6 numbers */ )
372 {
373 double pn = tp->pen_size;
374 int f_fs = tp->fill_shape;
375
376 f_fs = (round(fabs(v[4] - v[5]) + 0.5) >= 360) ? f_fs : 0;
377 f_vp = (pn > 0.0) ? f_vp : 0;
378
379 if (f_vp || f_fs) {
380 pdf_dev_gsave();
381
382 set_styles(tp, c, f_fs, f_vp, pn, da);
383
384 /* The arcx operator here draws an excess straight line from current
385 * point to the starting point of the arc if they are different, as in
386 * PostScript language. It may cuase an unexpected behavior when DVIPS
387 * transformation command is inserted before TPIC ar command: it invokes
388 * moveto and sets currentpoint which may be different from the starting
389 * point of arc to be drawn. We use newpath here to avoid drawing an
390 * excess line. I'm not sure if it is proper TPIC implementation but this
391 * seems to be DVIPS compatible behavior.
392 */
393 pdf_dev_newpath();
394 pdf_dev_arcx(v[0], v[1], v[2], v[3], v[4], v[5], +1, 0.0);
395
396 showpath(f_vp, f_fs);
397
398 pdf_dev_grestore();
399 }
400 tpic__clear(tp);
401
402 return 0;
403 }
404
405 #if 1
406 static int
407 spc_currentpoint (struct spc_env *spe, int *pg, pdf_coord *cp)
408 {
409 *pg = 0;
410 cp->x = spe->x_user;
411 cp->y = spe->y_user;
412 return 0;
413 }
414 #endif
415
416 static int
417 spc_handler_tpic_pn (struct spc_env *spe,
418 struct spc_arg *ap ) /* , void *dp) */
419 {
420 struct spc_tpic_ *tp = &_tpic_state;
421 char *q;
422
423 ASSERT(spe && ap && tp);
424
425 skip_blank(&ap->curptr, ap->endptr);
426 q = parse_float_decimal(&ap->curptr, ap->endptr);
427 if (!q) {
428 spc_warn(spe, "Invalid pen size specified?");
429 return -1;
430 }
431 tp->pen_size = atof(q) * MI2DEV;
432 RELEASE(q);
433
434 return 0;
435 }
436
437 static int
438 spc_handler_tpic_pa (struct spc_env *spe,
439 struct spc_arg *ap ) /* , void *dp) */
440 {
441 struct spc_tpic_ *tp = &_tpic_state;
442 char *q;
443 int i;
444 double v[2];
445
446 ASSERT(spe && ap && tp);
447
448 skip_blank(&ap->curptr, ap->endptr);
449 for (i = 0;
450 i < 2 && ap->curptr < ap->endptr; i++) {
451 q = parse_float_decimal(&ap->curptr, ap->endptr);
452 if (!q) {
453 spc_warn(spe, "Missing numbers for TPIC \"pa\" command.");
454 return -1;
455 }
456 v[i] = atof(q);
457 RELEASE(q);
458 skip_blank(&ap->curptr, ap->endptr);
459 }
460 if (i != 2) {
461 spc_warn(spe, "Invalid arg for TPIC \"pa\" command.");
462 return -1;
463 }
464
465 if (tp->num_points >= tp->max_points) {
466 tp->max_points += 256;
467 tp->points = RENEW(tp->points, tp->max_points, pdf_coord);
468 }
469 tp->points[tp->num_points].x = v[0] * MI2DEV;
470 tp->points[tp->num_points].y = v[1] * MI2DEV;
471 tp->num_points += 1;
472
473 return 0;
474 }
475
476 static int
477 spc_handler_tpic_fp (struct spc_env *spe,
478 struct spc_arg *ap ) /* , void *dp) */
479 {
480 struct spc_tpic_ *tp = &_tpic_state;
481 pdf_coord cp;
482 int pg;
483
484 ASSERT(spe && ap && tp);
485
486 if (tp->num_points <= 1) {
487 spc_warn(spe, "Too few points (< 2) for polyline path.");
488 return -1;
489 }
490
491 spc_currentpoint(spe, &pg, &cp);
492
493 return tpic__polyline(tp, &cp, 1, 0.0);
494 }
495
496 static int
497 spc_handler_tpic_ip (struct spc_env *spe,
498 struct spc_arg *ap ) /* , void *dp) */
499 {
500 struct spc_tpic_ *tp = &_tpic_state;
501 pdf_coord cp;
502 int pg;
503
504 ASSERT(spe && ap && tp);
505
506 if (tp->num_points <= 1) {
507 spc_warn(spe, "Too few points (< 2) for polyline path.");
508 return -1;
509 }
510
511 spc_currentpoint(spe, &pg, &cp);
512
513 return tpic__polyline(tp, &cp, 0, 0.0);
514 }
515
516 static int
517 spc_handler_tpic_da (struct spc_env *spe,
518 struct spc_arg *ap ) /* , void *dp) */
519 {
520 struct spc_tpic_ *tp = &_tpic_state;
521 char *q;
522 double da = 0.0;
523 pdf_coord cp;
524 int pg;
525
526 ASSERT(spe && ap && tp);
527
528 skip_blank(&ap->curptr, ap->endptr);
529 q = parse_float_decimal(&ap->curptr, ap->endptr);
530 if (q) {
531 da = atof(q);
532 RELEASE(q);
533 }
534 if (tp->num_points <= 1) {
535 spc_warn(spe, "Too few points (< 2) for polyline path.");
536 return -1;
537 }
538
539 spc_currentpoint(spe, &pg, &cp);
540
541 return tpic__polyline(tp, &cp, 1, da);
542 }
543
544 static int
545 spc_handler_tpic_dt (struct spc_env *spe,
546 struct spc_arg *ap ) /* , void *dp) */
547 {
548 struct spc_tpic_ *tp = &_tpic_state;
549 char *q;
550 double da = 0.0;
551 pdf_coord cp;
552 int pg;
553
554 ASSERT(spe && ap && tp);
555
556 skip_blank(&ap->curptr, ap->endptr);
557 q = parse_float_decimal(&ap->curptr, ap->endptr);
558 if (q) {
559 da = -atof(q);
560 RELEASE(q);
561 }
562 if (tp->num_points <= 1) {
563 spc_warn(spe, "Too few points (< 2) for polyline path.");
564 return -1;
565 }
566
567 spc_currentpoint(spe, &pg, &cp);
568
569 return tpic__polyline(tp, &cp, 1, da);
570 }
571
572 static int
573 spc_handler_tpic_sp (struct spc_env *spe,
574 struct spc_arg *ap ) /* , void *dp) */
575 {
576 struct spc_tpic_ *tp = &_tpic_state;
577 char *q;
578 double da = 0.0;
579 pdf_coord cp;
580 int pg;
581
582 ASSERT(spe && ap && tp);
583
584 skip_blank(&ap->curptr, ap->endptr);
585 q = parse_float_decimal(&ap->curptr, ap->endptr);
586 if (q) {
587 da = atof(q);
588 RELEASE(q);
589 }
590 if (tp->num_points <= 2) {
591 spc_warn(spe, "Too few points (< 3) for spline path.");
592 return -1;
593 }
594
595 spc_currentpoint(spe, &pg, &cp);
596
597 return tpic__spline(tp, &cp, 1, da);
598 }
599
600 static int
601 spc_handler_tpic_ar (struct spc_env *spe,
602 struct spc_arg *ap ) /* , void *dp) */
603 {
604 struct spc_tpic_ *tp = &_tpic_state;
605 double v[6];
606 pdf_coord cp;
607 int pg;
608 char *q;
609 int i;
610
611 ASSERT(spe && ap && tp);
612
613 skip_blank(&ap->curptr, ap->endptr);
614 for (i = 0;
615 i < 6 && ap->curptr < ap->endptr; i++) {
616 q = parse_float_decimal(&ap->curptr, ap->endptr);
617 if (!q) {
618 spc_warn(spe, "Invalid args. in TPIC \"ar\" command.");
619 return -1;
620 }
621 v[i] = atof(q);
622 RELEASE(q);
623 skip_blank(&ap->curptr, ap->endptr);
624 }
625 if (i != 6) {
626 spc_warn(spe, "Invalid arg for TPIC \"ar\" command.");
627 return -1;
628 }
629
630 v[0] *= MI2DEV; v[1] *= MI2DEV;
631 v[2] *= MI2DEV; v[3] *= MI2DEV;
632 v[4] *= 180.0 / M_PI;
633 v[5] *= 180.0 / M_PI;
634
635 spc_currentpoint(spe, &pg, &cp);
636
637 return tpic__arc(tp, &cp, 1, 0.0, v);
638 }
639
640 static int
641 spc_handler_tpic_ia (struct spc_env *spe,
642 struct spc_arg *ap ) /* , void *dp) */
643 {
644 struct spc_tpic_ *tp = &_tpic_state;
645 double v[6];
646 pdf_coord cp;
647 int pg;
648 char *q;
649 int i;
650
651 ASSERT(spe && ap && tp);
652
653 skip_blank(&ap->curptr, ap->endptr);
654 for (i = 0;
655 i < 6 && ap->curptr < ap->endptr; i++) {
656 q = parse_float_decimal(&ap->curptr, ap->endptr);
657 if (!q) {
658 spc_warn(spe, "Invalid args. in TPIC \"ia\" command.");
659 return -1;
660 }
661 v[i] = atof(q);
662 RELEASE(q);
663 skip_blank(&ap->curptr, ap->endptr);
664 }
665 if (i != 6) {
666 spc_warn(spe, "Invalid arg for TPIC \"ia\" command.");
667 return -1;
668 }
669
670 v[0] *= MI2DEV; v[1] *= MI2DEV;
671 v[2] *= MI2DEV; v[3] *= MI2DEV;
672 v[4] *= 180.0 / M_PI;
673 v[5] *= 180.0 / M_PI;
674
675 spc_currentpoint(spe, &pg, &cp);
676
677 return tpic__arc(tp, &cp, 0, 0.0, v);
678 }
679
680 static int
681 spc_handler_tpic_sh (struct spc_env *spe,
682 struct spc_arg *ap ) /* , void *dp) */
683 {
684 struct spc_tpic_ *tp = &_tpic_state;
685 char *q;
686
687 ASSERT(spe && ap && tp);
688
689 tp->fill_shape = 1;
690 tp->fill_color = 0.5;
691
692 skip_blank(&ap->curptr, ap->endptr);
693 q = parse_float_decimal(&ap->curptr, ap->endptr);
694 if (q) {
695 double g = atof(q);
696 RELEASE(q);
697 if (g >= 0.0 && g <= 1.0)
698 tp->fill_color = g;
699 else {
700 WARN("Invalid fill color specified: %g\n", g);
701 return -1;
702 }
703 }
704
705 return 0;
706 }
707
708 static int
709 spc_handler_tpic_wh (struct spc_env *spe,
710 struct spc_arg *ap ) /* , void *dp) */
711 {
712 struct spc_tpic_ *tp = &_tpic_state;
713
714 ASSERT(spe && ap && tp);
715
716 tp->fill_shape = 1;
717 tp->fill_color = 0.0;
718
719 return 0;
720 }
721
722 static int
723 spc_handler_tpic_bk (struct spc_env *spe,
724 struct spc_arg *ap ) /* , void *dp) */
725 {
726 struct spc_tpic_ *tp = &_tpic_state;
727
728 ASSERT(spe && ap && tp);
729
730 tp->fill_shape = 1;
731 tp->fill_color = 1.0;
732
733 return 0;
734 }
735
736 static int
737 spc_handler_tpic_tx (struct spc_env *spe,
738 struct spc_arg *ap ) /* , void *dp) */
739 {
740 struct spc_tpic_ *tp = &_tpic_state;
741
742 ASSERT(spe && ap && tp);
743
744 spc_warn(spe, "TPIC command \"tx\" not supported.");
745
746 return -1;
747 }
748
749
750 static int
751 spc_handler_tpic__init (struct spc_env *spe, void *dp)
752 {
753 struct spc_tpic_ *tp = dp;
754
755 #if 0
756 tp->mode.fill = TPIC_MODE__FILL_SOLID;
757 #endif
758 tp->pen_size = 1.0;
759 tp->fill_shape = 0;
760 tp->fill_color = 0.0;
761
762 tp->points = NULL;
763 tp->num_points = 0;
764 tp->max_points = 0;
765
766 if (tp->mode.fill != TPIC_MODE__FILL_SOLID &&
767 pdf_check_version(1, 4) < 0) {
768 spc_warn(spe, "Tpic shading support requires PDF version 1.4.");
769 tp->mode.fill = TPIC_MODE__FILL_SOLID;
770 }
771
772 return 0;
773 }
774
775 static int
776 spc_handler_tpic__bophook (void *dp)
777 {
778 struct spc_tpic_ *tp = dp;
779
780 ASSERT(tp);
781
782 tpic__clear(tp);
783
784 return 0;
785 }
786
787 static int
788 spc_handler_tpic__eophook (struct spc_env *spe, void *dp)
789 {
790 struct spc_tpic_ *tp = dp;
791
792 ASSERT(tp);
793
794 if (tp->num_points > 0)
795 spc_warn(spe, "Unflushed tpic path at end of the page.");
796 tpic__clear(tp);
797
798 return 0;
799 }
800
801 static int
802 spc_handler_tpic__clean (struct spc_env *spe, void *dp)
803 {
804 struct spc_tpic_ *tp = dp;
805
806 ASSERT(tp);
807
808 if (tp->num_points > 0)
809 spc_warn(spe, "Unflushed tpic path at end of the document.");
810
811 tpic__clear(tp);
812 #if 0
813 RELEASE(tp);
814 #endif
815
816 return 0;
817 }
818
819 void
820 tpic_set_fill_mode (int mode)
821 {
822 struct spc_tpic_ *tp = &_tpic_state;
823 tp->mode.fill = mode;
824 }
825
826
827 int
828 spc_tpic_at_begin_page (void)
829 {
830 struct spc_tpic_ *tp = &_tpic_state;
831 return spc_handler_tpic__bophook(tp);
832 }
833
834 int
835 spc_tpic_at_end_page (void)
836 {
837 struct spc_tpic_ *tp = &_tpic_state;
838 return spc_handler_tpic__eophook(NULL, tp);
839 }
840
841
842 int
843 spc_tpic_at_begin_document (void)
844 {
845 struct spc_tpic_ *tp = &_tpic_state;
846 return spc_handler_tpic__init(NULL, tp);
847 }
848
849 int
850 spc_tpic_at_end_document (void)
851 {
852 struct spc_tpic_ *tp = &_tpic_state;
853 return spc_handler_tpic__clean(NULL, tp);
854 }
855
856
857 #if DEBUG
858 #include "pdfparse.h" /* parse_val_ident :( */
859
860 static pdf_obj *
861 spc_parse_kvpairs (struct spc_arg *ap)
862 {
863 pdf_obj *dict;
864 char *kp, *vp;
865 int error = 0;
866
867 dict = pdf_new_dict();
868
869 skip_blank(&ap->curptr, ap->endptr);
870 while (!error && ap->curptr < ap->endptr) {
871 kp = parse_val_ident(&ap->curptr, ap->endptr);
872 if (!kp)
873 break;
874 skip_blank(&ap->curptr, ap->endptr);
875 if (ap->curptr < ap->endptr &&
876 ap->curptr[0] == '=') {
877 ap->curptr++;
878 skip_blank(&ap->curptr, ap->endptr);
879 if (ap->curptr == ap->endptr) {
880 RELEASE(kp);
881 error = -1;
882 break;
883 }
884 vp = parse_c_string(&ap->curptr, ap->endptr);
885 if (!vp)
886 error = -1;
887 else {
888 pdf_add_dict(dict,
889 pdf_new_name(kp),
890 pdf_new_string(vp, strlen(vp) + 1)); /* NULL terminate */
891 RELEASE(vp);
892 }
893 } else {
894 /* Treated as 'flag' */
895 pdf_add_dict(dict,
896 pdf_new_name(kp),
897 pdf_new_boolean(1));
898 }
899 RELEASE(kp);
900 if (!error)
901 skip_blank(&ap->curptr, ap->endptr);
902 }
903
904 if (error) {
905 pdf_release_obj(dict);
906 dict = NULL;
907 }
908
909 return dict;
910 }
911
912 static int
913 tpic_filter_getopts (pdf_obj *kp, pdf_obj *vp, void *dp)
914 {
915 struct spc_tpic_ *tp = dp;
916 char *k, *v;
917 int error = 0;
918
919 ASSERT( kp && vp && tp );
920
921 k = pdf_name_value(kp);
922 if (!strcmp(k, "fill-mode")) {
923 if (pdf_obj_typeof(vp) != PDF_STRING) {
924 WARN("Invalid value for TPIC option fill-mode...");
925 error = -1;
926 } else {
927 v = pdf_string_value(vp);
928 if (!strcmp(v, "shape"))
929 tp->mode.fill = TPIC_MODE__FILL_SHAPE;
930 else if (!strcmp(v, "opacity"))
931 tp->mode.fill = TPIC_MODE__FILL_OPACITY;
932 else if (!strcmp(v, "solid"))
933 tp->mode.fill = TPIC_MODE__FILL_SOLID;
934 else {
935 WARN("Invalid value for TPIC option fill-mode: %s", v);
936 error = -1;
937 }
938 }
939 } else {
940 WARN("Unrecognized option for TPIC special handler: %s", k);
941 error = -1;
942 }
943
944 return error;
945 }
946
947 static int
948 spc_handler_tpic__setopts (struct spc_env *spe,
949 struct spc_arg *ap)
950 {
951 struct spc_tpic_ *tp = &_tpic_state;
952 pdf_obj *dict;
953 int error = 0;
954
955 dict = spc_parse_kvpairs(ap);
956 if (!dict)
957 return -1;
958 error = pdf_foreach_dict(dict, tpic_filter_getopts, tp);
959 if (!error) {
960 if (tp->mode.fill != TPIC_MODE__FILL_SOLID &&
961 pdf_check_version(1, 4) < 0) {
962 spc_warn(spe, "Transparent fill mode requires PDF version 1.4.");
963 tp->mode.fill = TPIC_MODE__FILL_SOLID;
964 }
965 }
966
967 return error;
968 }
969 #endif /* DEBUG */
970
971
972 static struct spc_handler tpic_handlers[] = {
973 {"pn", spc_handler_tpic_pn},
974 {"pa", spc_handler_tpic_pa},
975 {"fp", spc_handler_tpic_fp},
976 {"ip", spc_handler_tpic_ip},
977 {"da", spc_handler_tpic_da},
978 {"dt", spc_handler_tpic_dt},
979 {"sp", spc_handler_tpic_sp},
980 {"ar", spc_handler_tpic_ar},
981 {"ia", spc_handler_tpic_ia},
982 {"sh", spc_handler_tpic_sh},
983 {"wh", spc_handler_tpic_wh},
984 {"bk", spc_handler_tpic_bk},
985 {"tx", spc_handler_tpic_tx}
986 };
987
988 int
989 spc_tpic_check_special (const char *buf, int len)
990 {
991 int istpic = 0;
992 char *q;
993 const char *p, *endptr;
994 int i, hasnsp = 0;
995
996 p = buf;
997 endptr = p + len;
998
999 skip_blank(&p, endptr);
1000 #if ENABLE_SPC_NAMESPACE
1001 if (p + strlen("tpic:") < endptr &&
1002 !memcmp(p, "tpic:", strlen("tpic:")))
1003 {
1004 p += strlen("tpic:");
1005 hasnsp = 1;
1006 }
1007 #endif
1008 q = parse_c_ident(&p, endptr);
1009
1010 if (!q)
1011 istpic = 0;
1012 else if (q && hasnsp && !strcmp(q, "__setopt__")) {
1013 #if DEBUG
1014 istpic = 1;
1015 #endif
1016 RELEASE(q);
1017 } else {
1018 for (i = 0;
1019 i < sizeof(tpic_handlers)/sizeof(struct spc_handler); i++) {
1020 if (!strcmp(q, tpic_handlers[i].key)) {
1021 istpic = 1;
1022 break;
1023 }
1024 }
1025 RELEASE(q);
1026 }
1027
1028 return istpic;
1029 }
1030
1031
1032 int
1033 spc_tpic_setup_handler (struct spc_handler *sph,
1034 struct spc_env *spe, struct spc_arg *ap)
1035 {
1036 char *q;
1037 int i, hasnsp = 0, error = -1;
1038
1039 ASSERT(sph && spe && ap);
1040
1041 skip_blank(&ap->curptr, ap->endptr);
1042 #if ENABLE_SPC_NAMESPACE
1043 if (ap->curptr + strlen("tpic:") < ap->endptr &&
1044 !memcmp(ap->curptr, "tpic:", strlen("tpic:")))
1045 {
1046 ap->curptr += strlen("tpic:");
1047 hasnsp = 1;
1048 }
1049 #endif
1050 q = parse_c_ident(&ap->curptr, ap->endptr);
1051
1052 if (!q)
1053 error = -1;
1054 else if (q && hasnsp && !strcmp(q, "__setopt__")) {
1055 #if DEBUG
1056 ap->command = "__setopt__";
1057 sph->key = "tpic:";
1058 sph->exec = spc_handler_tpic__setopts;
1059 skip_blank(&ap->curptr, ap->endptr);
1060 error = 0;
1061 #endif
1062 RELEASE(q);
1063 } else {
1064 for (i = 0;
1065 i < sizeof(tpic_handlers)/sizeof(struct spc_handler); i++) {
1066 if (!strcmp(q, tpic_handlers[i].key)) {
1067 ap->command = tpic_handlers[i].key;
1068 sph->key = "tpic:";
1069 sph->exec = tpic_handlers[i].exec;
1070 skip_blank(&ap->curptr, ap->endptr);
1071 error = 0;
1072 break;
1073 }
1074 }
1075 RELEASE(q);
1076 }
1077
1078 return error;
1079 }
1080
1081
1082 #if 0
1083 int
1084 spc_load_tpic_special (struct spc_env *spe, pdf_obj *lopts)
1085 {
1086 struct spc_def *spd;
1087 struct spc_tpic_ *sd;
1088
1089 sd = NEW(1, struct spc_tpic_);
1090
1091 spd = NEW(1, struct spc_def);
1092 spc_init_def(spd);
1093
1094 spc_def_init (spd, &spc_handler_tpic__init);
1095 spc_def_setopts(spd, &spc_handler_tpic__setopts);
1096 spc_def_bophook(spd, &spc_handler_tpic__bophook);
1097 spc_def_eophook(spd, &spc_handler_tpic__eophook);
1098 spc_def_clean (spd, &spc_handler_tpic__clean);
1099
1100 spc_def_func(spd, "pn", &spc_handler_tpic_pn);
1101 spc_def_func(spd, "pa", &spc_handler_tpic_pa);
1102 spc_def_func(spd, "fp", &spc_handler_tpic_fp);
1103 spc_def_func(spd, "ip", &spc_handler_tpic_ip);
1104 spc_def_func(spd, "da", &spc_handler_tpic_da);
1105 spc_def_func(spd, "dt", &spc_handler_tpic_dt);
1106 spc_def_func(spd, "sp", &spc_handler_tpic_sp);
1107 spc_def_func(spd, "ar", &spc_handler_tpic_ar);
1108 spc_def_func(spd, "ia", &spc_handler_tpic_ia);
1109 spc_def_func(spd, "sh", &spc_handler_tpic_sh);
1110 spc_def_func(spd, "wh", &spc_handler_tpic_wh);
1111 spc_def_func(spd, "bk", &spc_handler_tpic_bk);
1112 spc_def_func(spd, "tx", &spc_handler_tpic_tx);
1113
1114 spc_add_special(spe, "tpic", spd, sd);
1115
1116 return 0;
1117 }
1118 #endif /* 0 */