Debian Codegraph @

texk/dvipdfm-x/specials.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 <stdarg.h>
28
29 #include "system.h"
30 #include "mem.h"
31 #include "error.h"
32 #include "numbers.h"
33 #include "dpxconf.h"
34
35 #include "dvi.h"
36
37 #include "pdfobj.h"
38 #include "pdfparse.h"
39 #include "pdfdoc.h"
40 #include "pdfnames.h"
41
42 #include "pdfdraw.h"
43 #include "pdfdev.h"
44
45 #include "spc_pdfm.h"
46 #include "spc_tpic.h"
47 #include "spc_html.h"
48 #include "spc_misc.h"
49 #include "spc_color.h"
50 #include "spc_dvips.h"
51 #include "spc_dvipdfmx.h"
52 #include "spc_xtx.h"
53
54 #include "specials.h"
55
56 #define THEBUFFLENGTH 1024
57 void
58 spc_warn (struct spc_env *spe, const char *fmt, ...)
59 {
60 va_list ap;
61 static char buf[THEBUFFLENGTH];
62
63 va_start(ap, fmt);
64
65 vsnprintf(buf, THEBUFFLENGTH, fmt, ap);
66 WARN(buf);
67
68 va_end(ap);
69
70 return;
71 }
72
73
74 /* This is currently just to make other spc_xxx to not directly
75 * call dvi_xxx.
76 */
77 int
78 spc_begin_annot (struct spc_env *spe, pdf_obj *dict)
79 {
80 pdf_doc_begin_annot(dict);
81 dvi_tag_depth(); /* Tell dvi interpreter to handle line-break. */
82 return 0;
83 }
84
85 int
86 spc_end_annot (struct spc_env *spe)
87 {
88 dvi_untag_depth();
89 pdf_doc_end_annot();
90 return 0;
91 }
92
93 int
94 spc_resume_annot (struct spc_env *spe)
95 {
96 dvi_link_annot(1);
97 return 0;
98 }
99
100 int
101 spc_suspend_annot (struct spc_env *spe)
102 {
103 dvi_link_annot(0);
104 return 0;
105 }
106
107 /* Added this for supporting bann-eann erea only with \phantom text */
108 int
109 spc_is_tracking_boxes (struct spc_env *spe)
110 {
111 return dvi_is_tracking_boxes();
112 }
113
114 void
115 spc_set_linkmode (struct spc_env *spe, int mode)
116 {
117 dvi_set_linkmode(mode);
118 }
119
120 void
121 spc_set_phantom (struct spc_env *spe, double height, double depth)
122 {
123 dvi_set_phantom_height(height, depth);
124 }
125
126
127 /* reserved keys */
128 static const char *_rkeys[] = {
129 #define K_OBJ__XPOS 0
130 #define K_OBJ__YPOS 1
131 "xpos", "ypos",
132 #define K_OBJ__THISPAGE 2
133 #define K_OBJ__PREVPAGE 3
134 #define K_OBJ__NEXTPAGE 4
135 "thispage", "prevpage", "nextpage",
136 #define K_OBJ__RESOURCES 5
137 "resources",
138 #define K_OBJ__PAGES 6
139 #define K_OBJ__NAMES 7
140 "pages", "names",
141 #define K_OBJ__CATALOG 8
142 #define K_OBJ__DOCINFO 9
143 "catalog", "docinfo",
144 #if 0
145 #define K_OBJ__TRAILER 10
146 "trailer",
147 #endif /* NYI */
148 NULL
149 };
150
151 /* pageN where N is a positive integer.
152 * Note that page need not exist at this time.
153 */
154 static int
155 ispageref (const char *key)
156 {
157 const char *p;
158 if (strlen(key) <= strlen("page") ||
159 memcmp(key, "page", strlen("page")))
160 return 0;
161 else {
162 for (p = key + 4; *p && *p >= '0' && *p <= '9'; p++);
163 if (*p != '\0')
164 return 0;
165 }
166 return 1;
167 }
168
169 /*
170 * The following routine returns copies, not the original object.
171 */
172 pdf_obj *
173 spc_lookup_reference (const char *key)
174 {
175 pdf_obj *value = NULL;
176 pdf_coord cp;
177 int k;
178
179 if (!key)
180 return NULL;
181
182 for (k = 0; _rkeys[k] && strcmp(key, _rkeys[k]); k++);
183 switch (k) {
184 /* xpos and ypos must be position in device space here. */
185 case K_OBJ__XPOS:
186 cp.x = dvi_dev_xpos(); cp.y = dvi_dev_ypos();
187 pdf_dev_transform(&cp, NULL);
188 value = pdf_new_number(ROUND(cp.x, .01));
189 break;
190 case K_OBJ__YPOS:
191 cp.x = dvi_dev_xpos(); cp.y = dvi_dev_ypos();
192 pdf_dev_transform(&cp, NULL);
193 value = pdf_new_number(ROUND(cp.y, .01));
194 break;
195 case K_OBJ__THISPAGE:
196 value = pdf_doc_this_page_ref();
197 break;
198 case K_OBJ__PREVPAGE:
199 value = pdf_doc_prev_page_ref();
200 break;
201 case K_OBJ__NEXTPAGE:
202 value = pdf_doc_next_page_ref();
203 break;
204 case K_OBJ__PAGES:
205 value = pdf_ref_obj(pdf_doc_page_tree());
206 break;
207 case K_OBJ__NAMES:
208 value = pdf_ref_obj(pdf_doc_names());
209 break;
210 case K_OBJ__RESOURCES:
211 value = pdf_ref_obj(pdf_doc_current_page_resources());
212 break;
213 case K_OBJ__CATALOG:
214 value = pdf_ref_obj(pdf_doc_catalog());
215 break;
216 case K_OBJ__DOCINFO:
217 value = pdf_ref_obj(pdf_doc_docinfo());
218 break;
219 default:
220 if (ispageref(key))
221 value = pdf_doc_ref_page(atoi(key + 4));
222 else {
223 value = pdf_names_lookup_reference(global_names, key, strlen(key));
224 }
225 break;
226 }
227
228 if (!value) {
229 ERROR("Object reference %s not exist.", key);
230 }
231
232 return value;
233 }
234
235 pdf_obj *
236 spc_lookup_object (const char *key)
237 {
238 pdf_obj *value = NULL;
239 pdf_coord cp;
240 int k;
241
242 if (!key)
243 return NULL;
244
245 for (k = 0; _rkeys[k] && strcmp(key, _rkeys[k]); k++);
246 switch (k) {
247 case K_OBJ__XPOS:
248 cp.x = dvi_dev_xpos(); cp.y = dvi_dev_ypos();
249 pdf_dev_transform(&cp, NULL);
250 value = pdf_new_number(ROUND(cp.x, .01));
251 break;
252 case K_OBJ__YPOS:
253 cp.x = dvi_dev_xpos(); cp.y = dvi_dev_ypos();
254 pdf_dev_transform(&cp, NULL);
255 value = pdf_new_number(ROUND(cp.y, .01));
256 break;
257 case K_OBJ__THISPAGE:
258 value = pdf_doc_this_page();
259 break;
260 case K_OBJ__PAGES:
261 value = pdf_doc_page_tree();
262 break;
263 case K_OBJ__NAMES:
264 value = pdf_doc_names();
265 break;
266 case K_OBJ__RESOURCES:
267 value = pdf_doc_current_page_resources();
268 break;
269 case K_OBJ__CATALOG:
270 value = pdf_doc_catalog();
271 break;
272 case K_OBJ__DOCINFO:
273 value = pdf_doc_docinfo();
274 break;
275 default:
276 value = pdf_names_lookup_object(global_names, key, strlen(key));
277 break;
278 }
279
280 /* spc_handler_pdfm_bead() in spc_pdfm.c controls NULL too.
281 if (!value) {
282 ERROR("Object reference %s not exist.", key);
283 }
284 */
285
286 return value;
287 }
288
289 void
290 spc_push_object (struct spc_env *spe, const char *key, pdf_obj *value)
291 {
292 if (!key || !value)
293 return;
294
295 pdf_names_add_object(global_names, key, strlen(key), value);
296 }
297
298 void
299 spc_flush_object (struct spc_env *spe, const char *key)
300 {
301 pdf_names_close_object(global_names, key, strlen(key));
302 }
303
304 void
305 spc_clear_objects (struct spc_env *spe)
306 {
307 /* Do nothing... */
308 }
309
310 /* Migrated form pdf_dev.c
311 * No need to palce this into pdfdev.c at all.
312 */
313 static dpx_stack coords;
314
315 void
316 spc_get_coord (struct spc_env *spe, double *x, double *y)
317 {
318 ASSERT(x && y );
319
320 if (dpx_stack_depth(&coords) > 0) {
321 pdf_coord *p = dpx_stack_top(&coords);
322 *x = p->x;
323 *y = p->y;
324 } else {
325 *x = *y = 0.0;
326 }
327 }
328
329 void
330 spc_push_coord (struct spc_env *spe, double x, double y)
331 {
332 pdf_coord *p;
333
334 p = NEW(1, pdf_coord);
335 p->x = x; p->y = y;
336 dpx_stack_push(&coords, p);
337 dvi_set_compensation(x, y);
338 }
339
340 void
341 spc_pop_coord (struct spc_env *spe)
342 {
343 double x, y;
344 pdf_coord *p;
345
346 p = dpx_stack_pop(&coords);
347 if (p)
348 RELEASE(p);
349
350 spc_get_coord(spe, &x, &y);
351 dvi_set_compensation(x, y);
352 }
353
354 /* Migrated from pdfdraw.c.
355 *
356 * pt_fixee is obviously not a PDF graphics state parameter.
357 *
358 */
359
360 static dpx_stack pt_fixee;
361
362 void
363 spc_set_fixed_point (struct spc_env *spe, double x, double y)
364 {
365 pdf_coord *p;
366
367 p = dpx_stack_top(&pt_fixee);
368 if (p) {
369 p->x = x;
370 p->y = y;
371 } else {
372 p = NEW(1, pdf_coord);
373 p->x = x;
374 p->y = y;
375 dpx_stack_push(&pt_fixee, p);
376 }
377 }
378
379 void
380 spc_get_fixed_point (struct spc_env *spe, double *x, double *y)
381 {
382 pdf_coord *p;
383
384 ASSERT(x && y);
385
386 p = dpx_stack_top(&pt_fixee);
387 if (p) {
388 *x = p->x;
389 *y = p->y;
390 } else {
391 *x = 0.0;
392 *y = 0.0;
393 }
394 }
395
396 void
397 spc_put_fixed_point (struct spc_env *spe, double x, double y)
398 {
399 pdf_coord *p;
400
401 p = NEW(1, pdf_coord);
402 p->x = x;
403 p->y = y;
404 dpx_stack_push(&pt_fixee, p);
405 }
406
407 void
408 spc_dup_fixed_point (struct spc_env *spe)
409 {
410 pdf_coord *p1, *p2;
411
412 p1 = dpx_stack_top(&pt_fixee);
413 p2 = NEW(1, pdf_coord);
414 if (p1) {
415 p2->x = p1->x; p2->y = p1->y;
416 } else {
417 p2->x = 0.0; p2->y = 0.0;
418 }
419 dpx_stack_push(&pt_fixee, p2);
420 }
421
422 void
423 spc_pop_fixed_point (struct spc_env *spe)
424 {
425 pdf_coord *p;
426 p = dpx_stack_pop(&pt_fixee);
427 if (p)
428 RELEASE(p);
429 }
430
431 void
432 spc_clear_fixed_point (struct spc_env *spe)
433 {
434 pdf_coord *p;
435
436 for (;;) {
437 p = dpx_stack_pop(&pt_fixee);
438 if (!p)
439 break;
440 else
441 RELEASE(p);
442 }
443 }
444
445 void
446 spc_get_current_point (struct spc_env *spe, pdf_coord *cp)
447 {
448 double xoff, yoff;
449
450 if (!spe || !cp)
451 return;
452
453 spc_get_coord(spe, &xoff, &yoff);
454 cp->x = spe->x_user - xoff;
455 cp->y = spe->y_user - yoff;
456 }
457
458 void
459 spc_put_image (struct spc_env *spe, int res_id, transform_info *ti, double xpos, double ypos)
460 {
461 double xoff, yoff;
462
463 spc_get_coord(spe, &xoff, &yoff);
464 pdf_dev_put_image(res_id, ti, xpos - xoff, ypos - yoff, &spe->info.rect);
465 spe->info.is_drawable = 1;
466 }
467
468
469 static int
470 spc_handler_unknown (struct spc_env *spe, struct spc_arg *args)
471 {
472 ASSERT(spe && args);
473
474 args->curptr = args->endptr;
475
476 return -1;
477 }
478
479 static void
480 init_special (struct spc_handler *special,
481 struct spc_env *spe,
482 struct spc_arg *args,
483 const char *p, uint32_t size,
484 double x_user, double y_user, double mag)
485 {
486
487 special->key = NULL;
488 special->exec = (spc_handler_fn_ptr) &spc_handler_unknown;
489
490 spe->x_user = x_user;
491 spe->y_user = y_user;
492 spe->mag = mag;
493 spe->pg = pdf_doc_current_page_number(); /* _FIXME_ */
494 spe->info.is_drawable = 0;
495 spe->info.rect.llx = 0.0;
496 spe->info.rect.lly = 0.0;
497 spe->info.rect.urx = 0.0;
498 spe->info.rect.ury = 0.0;
499
500 args->curptr = p;
501 args->endptr = args->curptr + size;
502 args->base = args->curptr;
503 args->command = NULL;
504
505 return;
506 }
507
508 static void
509 check_garbage (struct spc_arg *args)
510 {
511 ASSERT(args);
512
513 if (args->curptr >= args->endptr)
514 return;
515
516 skip_white(&args->curptr, args->endptr);
517 if (args->curptr < args->endptr) {
518 WARN("Unparsed material at end of special ignored.");
519 dump(args->curptr, args->endptr);
520 }
521
522 return;
523 }
524
525 static struct {
526 const char *key;
527 int (*bodhk_func) (void);
528 int (*eodhk_func) (void);
529 int (*bophk_func) (void);
530 int (*eophk_func) (void);
531 int (*bofhk_func) (void);
532 int (*eofhk_func) (void);
533 int (*check_func) (const char *, int);
534 int (*setup_func) (struct spc_handler *, struct spc_env *, struct spc_arg *);
535 } known_specials[] = {
536
537 {"pdf:",
538 spc_pdfm_at_begin_document,
539 spc_pdfm_at_end_document,
540 NULL,
541 spc_pdfm_at_end_page,
542 NULL,
543 NULL,
544 spc_pdfm_check_special,
545 spc_pdfm_setup_handler
546 },
547
548 {"x:",
549 NULL,
550 NULL,
551 NULL,
552 NULL,
553 NULL,
554 NULL,
555 spc_xtx_check_special,
556 spc_xtx_setup_handler
557 },
558
559 {"dvipdfmx:",
560 NULL,
561 NULL,
562 NULL,
563 NULL,
564 NULL,
565 NULL,
566 spc_dvipdfmx_check_special,
567 spc_dvipdfmx_setup_handler
568 },
569
570 {"ps:",
571 spc_dvips_at_begin_document,
572 spc_dvips_at_end_document,
573 spc_dvips_at_begin_page,
574 spc_dvips_at_end_page,
575 NULL,
576 NULL,
577 spc_dvips_check_special,
578 spc_dvips_setup_handler
579 },
580
581 {"color",
582 NULL,
583 NULL,
584 NULL,
585 NULL,
586 NULL,
587 NULL,
588 spc_color_check_special,
589 spc_color_setup_handler
590 },
591
592 {"tpic",
593 spc_tpic_at_begin_document,
594 spc_tpic_at_end_document,
595 spc_tpic_at_begin_page,
596 spc_tpic_at_end_page,
597 NULL,
598 NULL,
599 spc_tpic_check_special,
600 spc_tpic_setup_handler
601 },
602
603 {"html:",
604 spc_html_at_begin_document,
605 spc_html_at_end_document,
606 spc_html_at_begin_page,
607 spc_html_at_end_page,
608 NULL,
609 NULL,
610 spc_html_check_special,
611 spc_html_setup_handler
612 },
613
614 {"compat",
615 spc_misc_at_begin_document,
616 spc_misc_at_end_document,
617 spc_misc_at_begin_page,
618 NULL,
619 spc_misc_at_begin_form,
620 spc_misc_at_end_form,
621 spc_misc_check_special,
622 spc_misc_setup_handler
623 },
624
625 {NULL} /* end */
626 };
627
628 int
629 spc_begin_form (struct spc_env *spe, const char *ident, pdf_coord cp, pdf_rect *cropbox)
630 {
631 int error = 0;
632 int i, xobj_id;
633
634 xobj_id = pdf_doc_begin_grabbing(ident, cp.x, cp.y, cropbox);
635
636 if (xobj_id < 0) {
637 error = -1;
638 } else {
639 for (i = 0; known_specials[i].key != NULL; i++) {
640 if (known_specials[i].bofhk_func) {
641 error = known_specials[i].bofhk_func();
642 }
643 }
644 }
645
646 return error;
647 }
648
649 int
650 spc_end_form (struct spc_env *spe, pdf_obj *attr)
651 {
652 int error = 0;
653 int i;
654
655 pdf_doc_end_grabbing(attr);
656
657 for (i = 0; known_specials[i].key != NULL; i++) {
658 if (known_specials[i].eofhk_func) {
659 error = known_specials[i].eofhk_func();
660 }
661 }
662
663 return error;
664 }
665
666 int
667 spc_exec_at_begin_page (void)
668 {
669 int error = 0;
670 int i;
671
672 for (i = 0; known_specials[i].key != NULL; i++) {
673 if (known_specials[i].bophk_func) {
674 error = known_specials[i].bophk_func();
675 }
676 }
677
678 return error;
679 }
680
681 int
682 spc_exec_at_end_page (void)
683 {
684 int error = 0;
685 int i;
686
687 for (i = 0; known_specials[i].key != NULL; i++) {
688 if (known_specials[i].eophk_func) {
689 error = known_specials[i].eophk_func();
690 }
691 }
692
693 return error;
694 }
695
696 int
697 spc_exec_at_begin_document (void)
698 {
699 int error = 0;
700 int i;
701
702 for (i = 0; known_specials[i].key != NULL; i++) {
703 if (known_specials[i].bodhk_func) {
704 error = known_specials[i].bodhk_func();
705 }
706 }
707
708 dpx_stack_init(&coords);
709 dpx_stack_init(&pt_fixee);
710
711 return error;
712 }
713
714 int
715 spc_exec_at_end_document (void)
716 {
717 int error = 0;
718 int i;
719 pdf_coord *p;
720
721 for (i = 0; known_specials[i].key != NULL; i++) {
722 if (known_specials[i].eodhk_func) {
723 error = known_specials[i].eodhk_func();
724 }
725 }
726
727 while ((p = dpx_stack_pop(&coords)) != NULL) {
728 RELEASE(p);
729 }
730 while ((p = dpx_stack_pop(&pt_fixee)) != NULL) {
731 RELEASE(p);
732 }
733
734 return error;
735 }
736
737 static void
738 print_error (const char *name, struct spc_env *spe, struct spc_arg *ap)
739 {
740 const char *p;
741 char ebuf[64];
742 int i;
743 int pg = spe->pg;
744 pdf_coord c;
745
746 c.x = spe->x_user; c.y = spe->y_user;
747 pdf_dev_transform(&c, NULL);
748
749 if (ap->command && name) {
750 WARN("Interpreting special command %s (%s) failed.", ap->command, name);
751 WARN(">> at page=\"%ld\" position=\"(%g, %g)\" (in PDF)", pg, c.x, c.y);
752 }
753 for (i = 0, p = ap->base; i < 63 && p < ap->endptr; p++) {
754 if (isprint((unsigned char)*p))
755 ebuf[i++] = *p;
756 else if (i + 4 < 63)
757 i += sprintf(ebuf + i, "\\x%02x", (unsigned char)*p);
758 else
759 break;
760 }
761 ebuf[i] = '\0';
762 if (ap->curptr < ap->endptr) {
763 while (i-- > 60)
764 ebuf[i] = '.';
765 }
766 WARN(">> xxx \"%s\"", ebuf);
767
768 if (ap->curptr < ap->endptr) {
769 for (i = 0, p = ap->curptr; i < 63 && p < ap->endptr; p++) {
770 if (isprint((unsigned char)*p))
771 ebuf[i++] = *p;
772 else if (i + 4 < 63)
773 i += sprintf(ebuf + i, "\\x%02x", (unsigned char)*p);
774 else
775 break;
776 }
777 ebuf[i] = '\0';
778 if (ap->curptr < ap->endptr) {
779 while (i-- > 60)
780 ebuf[i] = '.';
781 }
782 WARN(">> Reading special command stopped around >>%s<<", ebuf);
783
784 ap->curptr = ap->endptr;
785 }
786 }
787
788 int
789 spc_exec_special (const char *buffer, int32_t size,
790 double x_user, double y_user, double mag,
791 int *is_drawable, pdf_rect *rect)
792 {
793 int error = -1;
794 int i, found;
795 struct spc_env spe;
796 struct spc_arg args;
797 struct spc_handler special;
798
799 if (dpx_conf.verbose_level > 3) {
800 MESG("Executing special command: ");
801 dump(buffer, buffer + size);
802 }
803
804 init_special(&special, &spe, &args, buffer, size, x_user, y_user, mag);
805
806 for (i = 0; known_specials[i].key != NULL; i++) {
807 found = known_specials[i].check_func(buffer, size);
808 if (found) {
809 error = known_specials[i].setup_func(&special, &spe, &args);
810 if (!error) {
811 error = special.exec(&spe, &args);
812 }
813 if (error) {
814 print_error(known_specials[i].key, &spe, &args);
815 } else {
816 if (is_drawable)
817 *is_drawable = spe.info.is_drawable;
818 if (rect) {
819 rect->llx = spe.info.rect.llx;
820 rect->lly = spe.info.rect.lly;
821 rect->urx = spe.info.rect.urx;
822 rect->ury = spe.info.rect.ury;
823 }
824 }
825 break;
826 }
827 }
828
829 check_garbage(&args);
830
831 return error;
832 }