Debian Codegraph @

texk/dvipdfm-x/spc_pdfm.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 <ctype.h>
28
29 #include "system.h"
30 #include "mem.h"
31 #include "error.h"
32 #include "mfileio.h"
33
34 #include "numbers.h"
35
36 #include "fontmap.h"
37 #include "dpxfile.h"
38 #include "dpxutil.h"
39 #include "dpxconf.h"
40
41 #include "unicode.h"
42
43 #include "pdfobj.h"
44 #include "pdfparse.h"
45
46 #include "pdfdoc.h"
47
48 #include "pdfximage.h"
49 #include "pdfdraw.h"
50 #include "pdfcolor.h"
51 #include "pdfdev.h"
52
53 #include "specials.h"
54
55 #include "spc_util.h"
56 #include "spc_pdfm.h"
57
58 #include "dvipdfmx.h"
59
60 #define SPC_PDFM_SUPPORT_ANNOT_TRANS 1
61
62 struct tounicode {
63 int cmap_id;
64 int unescape_backslash;
65 pdf_obj *taintkeys; /* An array of PDF names. */
66 };
67
68 struct spc_pdf_
69 {
70 pdf_obj *annot_dict; /* pending annotation dict */
71 int lowest_level; /* current min level of outlines */
72 struct tounicode cd; /* For to-UTF16-BE conversion :( */
73 pdf_obj *pageresources; /* Add to all page resource dict */
74 };
75
76 static struct spc_pdf_ _pdf_stat = {
77 NULL,
78 255,
79 { -1, 0, NULL },
80 NULL
81 };
82
83 static pdf_obj *
84 parse_pdf_reference (const char **start, const char *end, void *user_data)
85 {
86 pdf_obj *result = NULL;
87 char *name;
88
89 skip_white(start, end);
90 name = parse_opt_ident(start, end);
91 if (name) {
92 result = spc_lookup_reference(name);
93 if (!result) {
94 WARN("Could not find the named reference (@%s).", name);
95 }
96 RELEASE(name);
97 } else {
98 WARN("Could not find a reference name.");
99 result = NULL;
100 }
101
102 return result;
103 }
104
105 static int
106 spc_handler_pdfm__init (void *dp)
107 {
108 struct spc_pdf_ *sd = dp;
109 /* The folllowing dictionary entry keys are considered as keys for
110 * text strings. Be sure that string object is NOT always a text string.
111 */
112 static const char *default_taintkeys[] = {
113 "Title", "Author", "Subject", "Keywords",
114 "Creator", "Producer", "Contents", "Subj",
115 "TU", "T", "TM", NULL /* EOD */
116 };
117 int i;
118
119 sd->annot_dict = NULL;
120 sd->lowest_level = 255;
121 sd->cd.taintkeys = pdf_new_array();
122 for (i = 0; default_taintkeys[i] != NULL; i++) {
123 pdf_add_array(sd->cd.taintkeys,
124 pdf_new_name(default_taintkeys[i]));
125 }
126 sd->pageresources = NULL;
127
128 return 0;
129 }
130
131 static int
132 spc_handler_pdfm__clean (void *dp)
133 {
134 struct spc_pdf_ *sd = dp;
135
136 if (sd->annot_dict) {
137 WARN("Unbalanced bann and eann found.");
138 pdf_release_obj(sd->annot_dict);
139 }
140 sd->lowest_level = 255;
141 sd->annot_dict = NULL;
142 if (sd->cd.taintkeys)
143 pdf_release_obj(sd->cd.taintkeys);
144 sd->cd.taintkeys = NULL;
145 if (sd->pageresources)
146 pdf_release_obj(sd->pageresources);
147 sd->pageresources = NULL;
148
149 return 0;
150 }
151
152
153 int
154 spc_pdfm_at_begin_document (void)
155 {
156 struct spc_pdf_ *sd = &_pdf_stat;
157 return spc_handler_pdfm__init(sd);
158 }
159
160 int
161 spc_pdfm_at_end_document (void)
162 {
163 struct spc_pdf_ *sd = &_pdf_stat;
164 return spc_handler_pdfm__clean(sd);
165 }
166
167 /* Why should we have this kind of things? */
168 static int
169 safeputresdent (pdf_obj *kp, pdf_obj *vp, void *dp)
170 {
171 char *key;
172
173 ASSERT(kp && vp && dp);
174
175 key = pdf_name_value(kp);
176 if (pdf_lookup_dict(dp, key))
177 WARN("Object \"%s\" already defined in dict! (ignored)", key);
178 else {
179 pdf_add_dict(dp,
180 pdf_link_obj(kp), pdf_link_obj(vp));
181 }
182 return 0;
183 }
184
185 static int
186 safeputresdict (pdf_obj *kp, pdf_obj *vp, void *dp)
187 {
188 char *key;
189 pdf_obj *dict;
190
191 ASSERT(kp && vp && dp);
192
193 key = pdf_name_value(kp);
194 dict = pdf_lookup_dict(dp, key);
195
196 /* Not sure what is the best way to handle this situation */
197 if (PDF_OBJ_INDIRECTTYPE(dict)) {
198 if (PDF_OBJ_INDIRECTTYPE(vp)) {
199 /* If two indirect objects are pointing the same object, do nothing. */
200 if (!pdf_compare_reference(dict, vp)) {
201 return 0;
202 } else {
203 /* otherwise merge the content of old one (see below) */
204 dict = pdf_deref_obj(dict);
205 pdf_release_obj(dict); /* decrement link count */
206 }
207 }
208 }
209
210 if (pdf_obj_typeof(vp) == PDF_INDIRECT) {
211 /* Copy the content of old resource category dict (if exists) */
212 if (dict) {
213 pdf_obj *dst = pdf_deref_obj(vp);
214 if (dst) {
215 if (pdf_obj_typeof(dst) == PDF_DICT) {
216 pdf_foreach_dict(dict, safeputresdent, dst);
217 pdf_release_obj(dst);
218 } else {
219 WARN("Invalid type (not DICT) for page/form resource dict entry: key=\"%s\"", key);
220 pdf_release_obj(dst);
221 return -1;
222 }
223 }
224 }
225 pdf_add_dict(dp, pdf_new_name(key), pdf_link_obj(vp));
226 } else if (pdf_obj_typeof(vp) == PDF_DICT) {
227 if (dict)
228 pdf_foreach_dict(vp, safeputresdent, dict);
229 else {
230 pdf_add_dict(dp, pdf_new_name(key), pdf_link_obj(vp));
231 }
232 } else {
233 WARN("Invalid type (not DICT) for page/form resource dict entry: key=\"%s\"", key);
234 return -1;
235 }
236
237 return 0;
238 }
239
240 static
241 int putpageresources (pdf_obj *kp, pdf_obj *vp, void *dp)
242 {
243 char *resource_name;
244
245 ASSERT(kp && vp);
246
247 resource_name = pdf_name_value(kp);
248 pdf_doc_add_page_resource(dp, resource_name, pdf_link_obj(vp));
249
250 return 0;
251 }
252
253 static
254 int forallresourcecategory (pdf_obj *kp, pdf_obj *vp, void *dp)
255 {
256 int r = -1;
257 char *category;
258
259 ASSERT(kp && vp);
260
261 category = pdf_name_value(kp);
262 switch (pdf_obj_typeof(vp)) {
263 case PDF_DICT:
264 r = pdf_foreach_dict(vp, putpageresources, category);
265 break;
266 case PDF_INDIRECT:
267 {
268 /* In case pdf:pageresouces << /Category @res >> */
269 pdf_obj *obj;
270 obj = pdf_deref_obj(vp);
271 if (!obj) {
272 WARN("Can't deref object for page resource: %s", category);
273 r = -1;
274 } else if (pdf_obj_typeof(obj) != PDF_DICT) {
275 WARN("Invalid object type for page resource: %s", category);
276 r = -1;
277 } else {
278 pdf_obj *res_dict, *dict;
279
280 res_dict = pdf_doc_current_page_resources();
281 dict = pdf_lookup_dict(res_dict, category);
282 if (!dict) {
283 pdf_add_dict(res_dict, pdf_new_name(category), pdf_link_obj(vp));
284 } else {
285 if (pdf_obj_typeof(dict) == PDF_INDIRECT) {
286 dict = pdf_deref_obj(dict);
287 pdf_release_obj(dict); /* FIXME: jus to decrement link counter */
288 }
289 #if 0
290 /* This will leave garbage (object "res") since object "res"
291 * supplied as resource dictionary will have label but we copy the
292 * content of res here and never use reference to it.
293 */
294 pdf_foreach_dict(obj, safeputresdent, dict);
295 #else
296 /* With the below code resource dictionary is replaced by user
297 * supplied one, @res. However, there is a problem that all
298 * resources including internally generated one may go into single
299 * dictionary referenced by @res, and will be visible from any
300 * subsequent pages.
301 */
302 pdf_foreach_dict(dict, safeputresdent, obj);
303 pdf_add_dict(res_dict, pdf_new_name(category), pdf_link_obj(vp));
304 #endif
305 }
306 pdf_release_obj(obj);
307 }
308 }
309 break;
310 default:
311 WARN("Invalid object type for page resource specified for \"%s\"", category);
312 }
313
314 return r;
315 }
316
317 int
318 spc_pdfm_at_end_page (void)
319 {
320 struct spc_pdf_ *sd = &_pdf_stat;
321
322 if (sd->pageresources) {
323 pdf_foreach_dict(sd->pageresources, forallresourcecategory, NULL);
324 }
325
326 return 0;
327 }
328
329 /* Dvipdfm specials */
330 static int
331 spc_handler_pdfm_bop (struct spc_env *spe, struct spc_arg *args)
332 {
333 if (args->curptr < args->endptr) {
334 pdf_doc_set_bop_content(args->curptr,
335 (int) (args->endptr - args->curptr));
336 }
337
338 args->curptr = args->endptr;
339
340 return 0;
341 }
342
343 static int
344 spc_handler_pdfm_eop (struct spc_env *spe, struct spc_arg *args)
345 {
346 if (args->curptr < args->endptr) {
347 pdf_doc_set_eop_content(args->curptr,
348 (int) (args->endptr - args->curptr));
349 }
350
351 args->curptr = args->endptr;
352
353 return 0;
354 }
355
356 #define streamfiltered(o) \
357 (pdf_lookup_dict(pdf_stream_dict((o)), "Filter") ? 1 : 0)
358
359 /* Think what happens if you do
360 *
361 * pdf:put @resources << /Font << >> >>
362 *
363 */
364 static int
365 spc_handler_pdfm_put (struct spc_env *spe, struct spc_arg *ap)
366 {
367 pdf_obj *obj1, *obj2; /* put obj2 into obj1 */
368 char *ident;
369 int error = 0;
370
371 skip_white(&ap->curptr, ap->endptr);
372
373 ident = parse_opt_ident(&ap->curptr, ap->endptr);
374 if (!ident) {
375 spc_warn(spe, "Missing object identifier.");
376 return -1;
377 }
378 obj1 = spc_lookup_object(ident);
379 if (!obj1) {
380 spc_warn(spe, "Specified object not exist: %s", ident);
381 RELEASE(ident);
382 return -1;
383 }
384 skip_white(&ap->curptr, ap->endptr);
385
386 obj2 = parse_pdf_object_extended(&ap->curptr, ap->endptr, NULL, parse_pdf_reference, spe);
387 if (!obj2) {
388 spc_warn(spe, "Missing (an) object(s) to put into \"%s\"!", ident);
389 RELEASE(ident);
390 return -1;
391 }
392
393 switch (pdf_obj_typeof(obj1)) {
394 case PDF_DICT:
395 if (pdf_obj_typeof(obj2) != PDF_DICT) {
396 spc_warn(spe, "Inconsistent object type for \"put\" (expecting DICT): %s", ident);
397 error = -1;
398 } else {
399 if (!strcmp(ident, "resources"))
400 error = pdf_foreach_dict(obj2, safeputresdict, obj1);
401 else {
402 pdf_merge_dict(obj1, obj2);
403 }
404 }
405 break;
406
407 case PDF_STREAM:
408 if (pdf_obj_typeof(obj2) == PDF_DICT)
409 pdf_merge_dict(pdf_stream_dict(obj1), obj2);
410 else if (pdf_obj_typeof(obj2) == PDF_STREAM)
411 #if 0
412 {
413 pdf_merge_dict(pdf_stream_dict(obj1), pdf_stream_dict(obj2));
414 pdf_add_stream(obj1, pdf_stream_dataptr(obj2), pdf_stream_length(obj2));
415 }
416 #else
417 {
418 spc_warn(spe, "\"put\" operation not supported for STREAM <- STREAM: %s", ident);
419 error = -1;
420 }
421 #endif
422 else {
423 spc_warn(spe, "Invalid type: expecting a DICT or STREAM: %s", ident);
424 error = -1;
425 }
426 break;
427
428 case PDF_ARRAY:
429 /* dvipdfm */
430 pdf_add_array(obj1, pdf_link_obj(obj2));
431 while (ap->curptr < ap->endptr) {
432 pdf_obj *obj3 = parse_pdf_object_extended(&ap->curptr, ap->endptr, NULL, parse_pdf_reference, spe);
433 if (!obj3)
434 break;
435 pdf_add_array(obj1, obj3);
436 skip_white(&ap->curptr, ap->endptr);
437 }
438 break;
439
440 default:
441 spc_warn(spe, "Can't \"put\" object into non-DICT/STREAM/ARRAY type object: %s", ident);
442 error = -1;
443 break;
444 }
445 pdf_release_obj(obj2);
446 RELEASE(ident);
447
448 return error;
449 }
450
451
452 /* For pdf:tounicode support
453 * This feature is provided for convenience. TeX can't do
454 * input encoding conversion.
455 */
456 #include "cmap.h"
457
458 static size_t
459 calculate_size_utf16 (const unsigned char *p, const unsigned char *endptr)
460 {
461 size_t len = 0;
462
463 while (p < endptr) {
464 unsigned char c = *p;
465 if (c < 0x80) {
466 len += 2;
467 p += 1;
468 } else if (c < 0xE0) {
469 len += 2;
470 p += 2;
471 } else if (c < 0xF0) {
472 len += 2;
473 p += 3;
474 } else if (c < 0xF8) {
475 len += 4; /* Surrogate */
476 p += 4;
477 } else if (c < 0xFC) {
478 len += 4; /* Surrogate */
479 p += 5;
480 } else if (c < 0xFE) {
481 len += 4; /* Surrogate */
482 p += 6;
483 }
484 }
485
486 return len;
487 }
488
489 static int
490 reencode_string_from_utf8_to_utf16be (pdf_obj *instring)
491 {
492 int error = 0;
493 unsigned char *strptr;
494 size_t length;
495 int non_ascii;
496 const unsigned char *p, *endptr;
497
498 ASSERT(instring);
499 ASSERT(PDF_OBJ_STRINGTYPE(instring));
500
501 strptr = pdf_string_value(instring);
502 length = pdf_string_length(instring);
503
504 /* check if the input string is strictly ASCII */
505 p = strptr;
506 endptr = strptr + length;
507 non_ascii = 0;
508 for ( ; p < endptr; p++) {
509 if (*p > 127)
510 non_ascii++;
511 }
512 if (non_ascii == 0)
513 return 0; /* no need to reencode ASCII strings */
514
515 if (!UC_UTF8_is_valid_string(strptr, endptr)) {
516 error = -1;
517 } else {
518 unsigned char *q, *buf, *limptr;
519 size_t len;
520
521 p = strptr;
522 /* Rough estimate of output length. */
523 len = calculate_size_utf16(p, endptr) + 2;
524 buf = NEW(len, unsigned char);
525 q = buf;
526 limptr = buf + len;
527 q[0] = 0xfe; q[1] = 0xff;
528 q += 2;
529 while (p < endptr && q < limptr && !error) {
530 int32_t ucv;
531 size_t count;
532
533 ucv = UC_UTF8_decode_char(&p, endptr);
534 if (!UC_is_valid(ucv)) {
535 error = -1;
536 } else {
537 count = UC_UTF16BE_encode_char(ucv, &q, limptr);
538 if (count == 0) {
539 error = -1;
540 }
541 }
542 }
543 if (!error)
544 pdf_set_string(instring, buf, q - buf);
545 RELEASE(buf);
546 }
547
548 return error;
549 }
550
551 static int
552 reencode_string (CMap *cmap, pdf_obj *instring)
553 {
554 int error = 0;
555
556 if (!instring || !PDF_OBJ_STRINGTYPE(instring))
557 return -1;
558
559 if (cmap) {
560 unsigned char *obuf;
561 unsigned char *obufcur;
562 const unsigned char *inbufcur;
563 int inbufleft, obufleft, obufsize;
564
565 inbufleft = pdf_string_length(instring);
566 inbufcur = pdf_string_value (instring);
567
568 obufsize = inbufleft * 4 + 2;
569 obuf = NEW(obufsize, unsigned char);
570 obuf[0] = 0xfe;
571 obuf[1] = 0xff;
572 obufcur = obuf + 2;
573 obufleft = obufsize - 2;
574
575 CMap_decode(cmap, &inbufcur, &inbufleft, &obufcur, &obufleft);
576
577 if (inbufleft > 0)
578 error = -1;
579 if (!error)
580 pdf_set_string(instring, obuf, obufsize - obufleft);
581 RELEASE(obuf);
582 }
583
584 return error;
585 }
586
587 /* The purpose of this routine is to check if given string object is
588 * surely an object for *text* strings. It does not do a complete check
589 * but does a quick check. Please add entries for taintkeys if you have found
590 * additional dictionary entries which is considered as a text string.
591 */
592 static int
593 need_reencode (pdf_obj *kp, pdf_obj *vp, struct tounicode *cd)
594 {
595 int r = 0, i;
596 pdf_obj *tk;
597
598 ASSERT( cd && cd->taintkeys );
599 ASSERT( pdf_obj_typeof(kp) == PDF_NAME );
600 ASSERT( pdf_obj_typeof(vp) == PDF_STRING );
601
602 for (i = 0; i < pdf_array_length(cd->taintkeys); i++) {
603 tk = pdf_get_array(cd->taintkeys, i);
604 ASSERT( tk && pdf_obj_typeof(tk) == PDF_NAME );
605 if (!strcmp(pdf_name_value(kp), pdf_name_value(tk))) {
606 r = 1;
607 break;
608 }
609 }
610 if (r) {
611 /* Check UTF-16BE BOM. */
612 if (pdf_string_length(vp) >= 2 &&
613 !memcmp(pdf_string_value(vp), "\xfe\xff", 2))
614 r = 0;
615 }
616
617 return r;
618 }
619
620 static int
621 modify_strings (pdf_obj *kp, pdf_obj *vp, void *dp)
622 {
623 int r = 0; /* continue */
624 struct tounicode *cd = dp;
625
626 ASSERT( pdf_obj_typeof(kp) == PDF_NAME );
627
628 switch (pdf_obj_typeof(vp)) {
629 case PDF_STRING:
630 if (cd && cd->cmap_id >= 0 && cd->taintkeys) {
631 CMap *cmap = CMap_cache_get(cd->cmap_id);
632 if (need_reencode(kp, vp, cd))
633 r = reencode_string(cmap, vp);
634 } else if ((dpx_conf.compat_mode == dpx_mode_xdv_mode || dpx_conf.pdfm_str_utf8) &&
635 cd && cd->taintkeys) {
636 if (need_reencode(kp, vp, cd))
637 r = reencode_string_from_utf8_to_utf16be(vp);
638 }
639 if (r < 0) /* error occured... */
640 WARN("Input string conversion (to UTF16BE) failed for %s...", pdf_name_value(kp));
641 break;
642 /* Array elements are also checked. */
643 case PDF_ARRAY:
644 {
645 int i;
646 for (i = 0; i < pdf_array_length(vp); i++) {
647 pdf_obj *obj;
648 obj = pdf_get_array(vp, i);
649 r = modify_strings(kp, obj, dp);
650 if (r < 0)
651 break;
652 }
653 }
654 break;
655 case PDF_DICT:
656 r = pdf_foreach_dict(vp, modify_strings, dp);
657 break;
658 case PDF_STREAM:
659 r = pdf_foreach_dict(pdf_stream_dict(vp), modify_strings, dp);
660 break;
661 }
662
663 return r;
664 }
665
666 static pdf_obj *
667 parse_pdf_dict_with_tounicode (const char **pp, const char *endptr, struct tounicode *cd)
668 {
669 pdf_obj *dict;
670
671 /* disable this test for XDV files, as we do UTF8 reencoding with no cmap */
672 if ((dpx_conf.compat_mode != dpx_mode_xdv_mode && !dpx_conf.pdfm_str_utf8) && cd->cmap_id < 0) {
673 dict = parse_pdf_object_extended(pp, endptr, NULL, parse_pdf_reference, NULL);
674 if (dict && !PDF_OBJ_DICTTYPE(dict)) {
675 WARN("Dictionary type object expected but non-dictionary type found.");
676 pdf_release_obj(dict);
677 dict = NULL;
678 }
679 } else {
680 /* :( */
681 if (cd && cd->unescape_backslash) {
682 dict = parse_pdf_tainted_dict(pp, endptr, parse_pdf_reference, NULL);
683 } else {
684 dict = parse_pdf_object_extended(pp, endptr, NULL, parse_pdf_reference, NULL);
685 }
686 if (dict) {
687 if (!PDF_OBJ_DICTTYPE(dict)) {
688 WARN("Dictionary type object expected but non-dictionary type found.");
689 pdf_release_obj(dict);
690 dict = NULL;
691 } else {
692 pdf_foreach_dict(dict, modify_strings, cd);
693 }
694 }
695 }
696
697 return dict;
698 }
699
700 static void
701 set_rect_for_annot (struct spc_env *spe, pdf_rect *rect, transform_info ti)
702 {
703 pdf_coord cp, cp1, cp2, cp3, cp4;
704
705 spc_get_current_point(spe, &cp);
706
707 if (ti.flags & INFO_HAS_USER_BBOX) {
708 cp1.x = cp.x + ti.bbox.llx;
709 cp1.y = cp.y + ti.bbox.lly;
710 cp2.x = cp.x + ti.bbox.urx;
711 cp2.y = cp.y + ti.bbox.lly;
712 cp3.x = cp.x + ti.bbox.urx;
713 cp3.y = cp.y + ti.bbox.ury;
714 cp4.x = cp.x + ti.bbox.llx;
715 cp4.y = cp.y + ti.bbox.ury;
716 } else {
717 cp1.x = cp.x;
718 cp1.y = cp.y - spe->mag * ti.depth;
719 cp2.x = cp.x + spe->mag * ti.width;
720 cp2.y = cp.y - spe->mag * ti.depth;
721 cp3.x = cp.x + spe->mag * ti.width;
722 cp3.y = cp.y + spe->mag * ti.height;
723 cp4.x = cp.x;
724 cp4.y = cp.y + spe->mag * ti.height;
725 }
726 pdf_dev_transform(&cp1, NULL);
727 pdf_dev_transform(&cp2, NULL);
728 pdf_dev_transform(&cp3, NULL);
729 pdf_dev_transform(&cp4, NULL);
730 rect->llx = min4(cp1.x, cp2.x, cp3.x, cp4.x);
731 rect->lly = min4(cp1.y, cp2.y, cp3.y, cp4.y);
732 rect->urx = max4(cp1.x, cp2.x, cp3.x, cp4.x);
733 rect->ury = max4(cp1.y, cp2.y, cp3.y, cp4.y);
734 }
735
736 static int
737 spc_handler_pdfm_annot (struct spc_env *spe, struct spc_arg *args)
738 {
739 struct spc_pdf_ *sd = &_pdf_stat;
740 pdf_obj *annot_dict;
741 pdf_rect rect;
742 char *ident = NULL;
743 transform_info ti;
744
745 skip_white(&args->curptr, args->endptr);
746 if (args->curptr[0] == '@') {
747 ident = parse_opt_ident(&args->curptr, args->endptr);
748 skip_white(&args->curptr, args->endptr);
749 }
750
751 transform_info_clear(&ti);
752 if (spc_util_read_dimtrns(spe, &ti, args, 0) < 0) {
753 if (ident)
754 RELEASE(ident);
755 return -1;
756 }
757
758 if ((ti.flags & INFO_HAS_USER_BBOX) &&
759 ((ti.flags & INFO_HAS_WIDTH) || (ti.flags & INFO_HAS_HEIGHT))) {
760 spc_warn(spe, "You can't specify both bbox and width/height.");
761 if (ident)
762 RELEASE(ident);
763 return -1;
764 }
765
766 annot_dict = parse_pdf_dict_with_tounicode(&args->curptr, args->endptr, &sd->cd);
767 if (!annot_dict) {
768 spc_warn(spe, "Could not find dictionary object.");
769 if (ident)
770 RELEASE(ident);
771 return -1;
772 } else if (!PDF_OBJ_DICTTYPE(annot_dict)) {
773 spc_warn(spe, "Invalid type: not dictionary object.");
774 if (ident)
775 RELEASE(ident);
776 pdf_release_obj(annot_dict);
777 return -1;
778 }
779
780 set_rect_for_annot(spe, &rect, ti);
781
782 /* Order is important... */
783 if (ident)
784 spc_push_object(spe, ident, pdf_link_obj(annot_dict));
785 /* Add this reference. */
786 pdf_doc_add_annot(pdf_doc_current_page_number(), &rect, annot_dict, 1);
787
788 if (ident) {
789 RELEASE(ident);
790 }
791 pdf_release_obj(annot_dict);
792
793 return 0;
794 }
795
796 /* NOTE: This can't have ident. See "Dvipdfm User's Manual".
797 * 1 Jul. 2020: ident allowed (upon request)
798 * Only first annotation can be accessed in line break cases.
799 */
800 static int
801 spc_handler_pdfm_bann (struct spc_env *spe, struct spc_arg *args)
802 {
803 struct spc_pdf_ *sd = &_pdf_stat;
804 char *ident = NULL;
805 int error = 0;
806
807 if (sd->annot_dict) {
808 spc_warn(spe, "Can't begin an annotation when one is pending.");
809 return -1;
810 }
811
812 skip_white(&args->curptr, args->endptr);
813 if (args->curptr[0] == '@') {
814 ident = parse_opt_ident(&args->curptr, args->endptr);
815 skip_white(&args->curptr, args->endptr);
816 }
817
818 sd->annot_dict = parse_pdf_dict_with_tounicode(&args->curptr, args->endptr, &sd->cd);
819 if (!sd->annot_dict) {
820 spc_warn(spe, "Ignoring annotation with invalid dictionary.");
821 if (ident)
822 RELEASE(ident);
823 return -1;
824 } else if (!PDF_OBJ_DICTTYPE(sd->annot_dict)) {
825 spc_warn(spe, "Invalid type: not a dictionary object.");
826 pdf_release_obj(sd->annot_dict);
827 sd->annot_dict = NULL;
828 if (ident)
829 RELEASE(ident);
830 return -1;
831 }
832
833 error = spc_begin_annot(spe, pdf_link_obj(sd->annot_dict));
834 if (ident) {
835 spc_push_object(spe, ident, pdf_link_obj(sd->annot_dict));
836 RELEASE(ident);
837 }
838
839 return error;
840 }
841
842 static int
843 spc_handler_pdfm_eann (struct spc_env *spe, struct spc_arg *args)
844 {
845 struct spc_pdf_ *sd = &_pdf_stat;
846 int error = 0;
847
848 if (!sd->annot_dict) {
849 spc_warn(spe, "Tried to end an annotation without starting one!");
850 return -1;
851 }
852
853 error = spc_end_annot(spe);
854
855 pdf_release_obj(sd->annot_dict);
856 sd->annot_dict = NULL;
857
858 return error;
859 }
860
861 /* For supporting \phantom within bann-eann.
862 *
863 * \special{pdf:xann width 50pt height 8pt depth 1pt}
864 *
865 * tells dvipdfmx to extend the current annotation rectangle
866 * by the amount specified (witdh 50pt, height 8pt, depth 1pt)
867 * This was introduced since in the following situation
868 *
869 * \special{pdf:bann ...}\phantom{Some texts}\special{pdf:eann}
870 *
871 * annotation is not created since there is no annotation
872 * rectangle calculated due to no object being put.
873 */
874 static int
875 spc_handler_pdfm_xann (struct spc_env *spe, struct spc_arg *args)
876 {
877 pdf_rect rect;
878 transform_info ti;
879
880 if (!spc_is_tracking_boxes(spe)) {
881 /* Silently ignore */
882 args->curptr = args->endptr;
883 return 0;
884 }
885
886 skip_white(&args->curptr, args->endptr);
887
888 transform_info_clear(&ti);
889 if (spc_util_read_dimtrns(spe, &ti, args, 0) < 0) {
890 return -1;
891 }
892
893 if ((ti.flags & INFO_HAS_USER_BBOX) &&
894 ((ti.flags & INFO_HAS_WIDTH) || (ti.flags & INFO_HAS_HEIGHT))) {
895 spc_warn(spe, "You can't specify both bbox and width/height.");
896 return -1;
897 }
898
899 set_rect_for_annot(spe, &rect, ti);
900 pdf_doc_expand_box(&rect);
901
902 return 0;
903 }
904
905 /* Color:.... */
906 static int
907 spc_handler_pdfm_bcolor (struct spc_env *spe, struct spc_arg *ap)
908 {
909 int error = 0;
910 pdf_color fc, sc;
911 pdf_color *pfc, *psc;
912
913 skip_white(&ap->curptr, ap->endptr);
914
915 pdf_color_get_current(&psc, &pfc);
916 if (ap->curptr < ap->endptr &&
917 (ap->curptr[0] == 'f' || ap->curptr[0] == 's')) {
918 pdf_color_copycolor(&sc, psc);
919 pdf_color_copycolor(&fc, pfc);
920 while (!error && ap->curptr < ap->endptr) {
921 if (ap->curptr <= ap->endptr + strlen("fill") &&
922 !memcmp(ap->curptr, "fill", strlen("fill"))) {
923 ap->curptr += strlen("fill");
924 skip_white(&ap->curptr, ap->endptr);
925 error = spc_util_read_pdfcolor(spe, &fc, ap, pfc);
926 } else if (ap->curptr <= ap->endptr + strlen("stroke") &&
927 !memcmp(ap->curptr, "stroke", strlen("stroke"))) {
928 ap->curptr += strlen("stroke");
929 skip_white(&ap->curptr, ap->endptr);
930 error = spc_util_read_pdfcolor(spe, &sc, ap, psc);
931 }
932 skip_white(&ap->curptr, ap->endptr);
933 }
934 } else {
935 error = spc_util_read_pdfcolor(spe, &fc, ap, pfc);
936 if (!error) {
937 if (ap->curptr < ap->endptr) {
938 error = spc_util_read_pdfcolor(spe, &sc, ap, psc);
939 } else {
940 pdf_color_copycolor(&sc, &fc);
941 }
942 }
943 }
944
945 if (error) {
946 spc_warn(spe, "Invalid color specification?");
947 } else {
948 skip_white(&ap->curptr, ap->endptr);
949 pdf_color_push(&sc, &fc); /* save currentcolor */
950 }
951
952 return error;
953 }
954
955 /*
956 * This special changes the current color without clearing the color stack.
957 * It therefore differs from "color rgb 1 0 0".
958 */
959 static int
960 spc_handler_pdfm_scolor (struct spc_env *spe, struct spc_arg *ap)
961 {
962 int error = 0;
963 pdf_color fc, sc;
964 pdf_color *pfc, *psc;
965
966 skip_white(&ap->curptr, ap->endptr);
967
968 pdf_color_get_current(&psc, &pfc);
969 if (ap->curptr < ap->endptr &&
970 (ap->curptr[0] == 'f' || ap->curptr[0] == 's')) {
971 pdf_color_copycolor(&sc, psc);
972 pdf_color_copycolor(&fc, pfc);
973 while (!error && ap->curptr < ap->endptr) {
974 if (ap->curptr <= ap->endptr + strlen("fill") &&
975 !memcmp(ap->curptr, "fill", strlen("fill"))) {
976 ap->curptr += strlen("fill");
977 skip_white(&ap->curptr, ap->endptr);
978 error = spc_util_read_pdfcolor(spe, &fc, ap, pfc);
979 } else if (ap->curptr <= ap->endptr + strlen("stroke") &&
980 !memcmp(ap->curptr, "stroke", strlen("stroke"))) {
981 ap->curptr += strlen("stroke");
982 skip_white(&ap->curptr, ap->endptr);
983 error = spc_util_read_pdfcolor(spe, &sc, ap, psc);
984 }
985 skip_white(&ap->curptr, ap->endptr);
986 }
987 } else {
988 error = spc_util_read_pdfcolor(spe, &fc, ap, pfc);
989 if (!error) {
990 if (ap->curptr < ap->endptr) {
991 error = spc_util_read_pdfcolor(spe, &sc, ap, psc);
992 } else {
993 pdf_color_copycolor(&sc, &fc);
994 }
995 }
996 }
997
998 if (error) {
999 spc_warn(spe, "Invalid color specification?");
1000 } else {
1001 pdf_color_set(&sc, &fc);
1002 }
1003
1004 return error;
1005 }
1006
1007 static int
1008 spc_handler_pdfm_ecolor (struct spc_env *spe, struct spc_arg *args)
1009 {
1010 pdf_color_pop();
1011 return 0;
1012 }
1013
1014
1015 static int
1016 spc_handler_pdfm_btrans (struct spc_env *spe, struct spc_arg *args)
1017 {
1018 pdf_tmatrix M;
1019 pdf_coord cp;
1020 transform_info ti;
1021
1022 transform_info_clear(&ti);
1023 if (spc_util_read_dimtrns(spe, &ti, args, 0) < 0) {
1024 return -1;
1025 }
1026
1027 /* btrans inside bcontent-econtent bug fix.
1028 * I don't know if this is the only place needs to be fixed...
1029 */
1030 spc_get_current_point(spe, &cp);
1031 /* Create transformation matrix */
1032 pdf_copymatrix(&M, &(ti.matrix));
1033 M.e += ((1.0 - M.a) * cp.x - M.c * cp.y);
1034 M.f += ((1.0 - M.d) * cp.y - M.b * cp.x);
1035
1036 pdf_dev_gsave();
1037 pdf_dev_concat(&M);
1038
1039 return 0;
1040 }
1041
1042 static int
1043 spc_handler_pdfm_etrans (struct spc_env *spe, struct spc_arg *args)
1044 {
1045 pdf_dev_grestore();
1046
1047 /*
1048 * Unfortunately, the following line is necessary in case
1049 * of a color change inside of the save/restore pair.
1050 * (Font changes are automatically corrected by pdf_dev_grestore().)
1051 * Anything that was done there must be redone, so in effect,
1052 * we make no assumptions about what fonts. We act like we are
1053 * starting a new page.
1054 */
1055 pdf_dev_reset_color(0);
1056 pdf_dev_reset_xgstate(0);
1057
1058 return 0;
1059 }
1060
1061 static int
1062 spc_handler_pdfm_outline (struct spc_env *spe, struct spc_arg *args)
1063 {
1064 struct spc_pdf_ *sd = &_pdf_stat;
1065 pdf_obj *item_dict, *tmp;
1066 int level, is_open = -1;
1067 int current_depth;
1068
1069 skip_white(&args->curptr, args->endptr);
1070
1071 /*
1072 * pdf:outline is extended to support open/close feature
1073 *
1074 * pdf:outline 1 ... (as DVIPDFM)
1075 * pdf:outline [] 1 ... (open bookmark)
1076 * pdf:outline [-] 1 ... (closed bookmark)
1077 */
1078 if (args->curptr+3 < args->endptr && *args->curptr == '[') {
1079 args->curptr++;
1080 if (*args->curptr == '-') {
1081 args->curptr++;
1082 } else {
1083 is_open = 1;
1084 }
1085 args->curptr++;
1086 }
1087 skip_white(&args->curptr, args->endptr);
1088
1089 tmp = parse_pdf_object(&args->curptr, args->endptr, NULL);
1090 if (!tmp) {
1091 spc_warn(spe, "Missing number for outline item depth.");
1092 return -1;
1093 } else if (!PDF_OBJ_NUMBERTYPE(tmp)) {
1094 pdf_release_obj(tmp);
1095 spc_warn(spe, "Expecting number for outline item depth.");
1096 return -1;
1097 }
1098
1099 item_dict = NULL;
1100
1101 level = (int) pdf_number_value(tmp);
1102 pdf_release_obj(tmp);
1103
1104 /* What is this? Starting at level 3 and can go down to level 1?
1105 *
1106 * Here is the original comment:
1107 * Make sure we know where the starting level is
1108 *
1109 * NOTE: added
1110 * We need this for converting pages from 3rd to... :(
1111 */
1112 sd->lowest_level = MIN(sd->lowest_level, level);
1113
1114 level += 1 - sd->lowest_level;
1115
1116 item_dict = parse_pdf_dict_with_tounicode(&args->curptr, args->endptr, &sd->cd);
1117 if (!item_dict) {
1118 spc_warn(spe, "Ignoring invalid dictionary.");
1119 return -1;
1120 }
1121 current_depth = pdf_doc_bookmarks_depth();
1122 if (current_depth > level) {
1123 while (current_depth-- > level)
1124 pdf_doc_bookmarks_up();
1125 } else if (current_depth < level) {
1126 while (current_depth++ < level)
1127 pdf_doc_bookmarks_down();
1128 }
1129
1130 pdf_doc_bookmarks_add(item_dict, is_open);
1131
1132 return 0;
1133 }
1134
1135 static int
1136 spc_handler_pdfm_article (struct spc_env *spe, struct spc_arg *args)
1137 {
1138 struct spc_pdf_ *sd = &_pdf_stat;
1139 char *ident;
1140 pdf_obj *info_dict;
1141
1142 skip_white (&args->curptr, args->endptr);
1143
1144 ident = parse_opt_ident(&args->curptr, args->endptr);
1145 if (!ident) {
1146 spc_warn(spe, "Article name expected but not found.");
1147 return -1;
1148 }
1149
1150 info_dict = parse_pdf_dict_with_tounicode(&args->curptr, args->endptr, &sd->cd);
1151 if (!info_dict) {
1152 spc_warn(spe, "Ignoring article with invalid info dictionary.");
1153 RELEASE(ident);
1154 return -1;
1155 }
1156
1157 pdf_doc_begin_article(ident, pdf_link_obj(info_dict));
1158 spc_push_object(spe, ident, info_dict);
1159 RELEASE(ident);
1160
1161 return 0;
1162 }
1163
1164 static int
1165 spc_handler_pdfm_bead (struct spc_env *spe, struct spc_arg *args)
1166 {
1167 struct spc_pdf_ *sd = &_pdf_stat;
1168 pdf_obj *article;
1169 pdf_obj *article_info;
1170 char *article_name;
1171 pdf_rect rect;
1172 int page_no;
1173 transform_info ti;
1174
1175 skip_white(&args->curptr, args->endptr);
1176
1177 if (args->curptr[0] != '@') {
1178 spc_warn(spe, "Article identifier expected but not found.");
1179 return -1;
1180 }
1181
1182 article_name = parse_opt_ident(&args->curptr, args->endptr);
1183 if (!article_name) {
1184 spc_warn(spe, "Article reference expected but not found.");
1185 return -1;
1186 }
1187
1188 /* If okay so far, try to get a bounding box */
1189 transform_info_clear(&ti);
1190 if (spc_util_read_dimtrns(spe, &ti, args, 0) < 0) {
1191 RELEASE(article_name);
1192 return -1;
1193 }
1194
1195 if ((ti.flags & INFO_HAS_USER_BBOX) &&
1196 ((ti.flags & INFO_HAS_WIDTH) || (ti.flags & INFO_HAS_HEIGHT))) {
1197 spc_warn(spe, "You can't specify both bbox and width/height.");
1198 RELEASE(article_name);
1199 return -1;
1200 }
1201
1202 skip_white(&args->curptr, args->endptr);
1203 if (args->curptr[0] != '<') {
1204 article_info = pdf_new_dict();
1205 } else {
1206 article_info = parse_pdf_dict_with_tounicode(&args->curptr, args->endptr, &sd->cd);
1207 if (!article_info) {
1208 spc_warn(spe, "Error in reading dictionary.");
1209 RELEASE(article_name);
1210 return -1;
1211 }
1212 }
1213
1214 /* Does this article exist yet */
1215 article = spc_lookup_object(article_name);
1216 if (article) {
1217 pdf_merge_dict (article, article_info);
1218 pdf_release_obj(article_info);
1219 } else {
1220 pdf_doc_begin_article(article_name, pdf_link_obj(article_info));
1221 spc_push_object(spe, article_name, article_info);
1222 }
1223 page_no = pdf_doc_current_page_number();
1224 set_rect_for_annot(spe, &rect, ti);
1225 pdf_doc_add_bead(article_name, NULL, page_no, &rect);
1226
1227 RELEASE(article_name);
1228 return 0;
1229 }
1230
1231 static int
1232 spc_handler_pdfm_image (struct spc_env *spe, struct spc_arg *args)
1233 {
1234 int xobj_id;
1235 char *ident = NULL;
1236 pdf_obj *fspec;
1237 transform_info ti;
1238 load_options options = {1, 0, NULL, NULL};
1239
1240 skip_white(&args->curptr, args->endptr);
1241 if (args->curptr[0] == '@') {
1242 ident = parse_opt_ident(&args->curptr, args->endptr);
1243 skip_white(&args->curptr, args->endptr);
1244 }
1245
1246 /* 2015/12/29
1247 * There should not be "page" and "pagebox" in read_dimtrns().
1248 * It is for reading "dimensions" and "transformations" and "page" is
1249 * completely unrelated.
1250 */
1251 transform_info_clear(&ti);
1252 if (spc_util_read_blahblah(spe, &ti,
1253 &options.page_no, &options.bbox_type,
1254 &options.page_name, args) < 0) {
1255 spc_warn(spe, "Reading option field in pdf:image failed.");
1256 if (ident)
1257 RELEASE(ident);
1258 return -1;
1259 }
1260
1261 skip_white(&args->curptr, args->endptr);
1262 fspec = parse_pdf_object(&args->curptr, args->endptr, NULL);
1263 if (!fspec) {
1264 spc_warn(spe, "Missing filename string for pdf:image.");
1265 if (ident)
1266 RELEASE(ident);
1267 return -1;
1268 } else if (!PDF_OBJ_STRINGTYPE(fspec)) {
1269 spc_warn(spe, "Missing filename string for pdf:image.");
1270 pdf_release_obj(fspec);
1271 if (ident)
1272 RELEASE(ident);
1273 return -1;
1274 }
1275
1276 skip_white(&args->curptr, args->endptr);
1277 if (args->curptr < args->endptr) {
1278 options.dict = parse_pdf_object_extended(&args->curptr, args->endptr, NULL, parse_pdf_reference, spe);
1279 }
1280
1281 xobj_id = pdf_ximage_load_image(ident, pdf_string_value(fspec), options);
1282
1283 if (xobj_id < 0) {
1284 spc_warn(spe, "Could not find image resource...");
1285 pdf_release_obj(fspec);
1286 if (ident)
1287 RELEASE(ident);
1288 return -1;
1289 }
1290
1291 if (!(ti.flags & INFO_DO_HIDE)) {
1292 spc_put_image(spe, xobj_id, &ti, spe->x_user, spe->y_user);
1293 }
1294
1295 if (ident) {
1296 if ((dpx_conf.compat_mode == dpx_mode_compat_mode) &&
1297 pdf_ximage_get_subtype(xobj_id) == PDF_XOBJECT_TYPE_IMAGE)
1298 pdf_ximage_set_attr(xobj_id, 1, 1, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0);
1299 RELEASE(ident);
1300 }
1301
1302 pdf_release_obj(fspec);
1303
1304 return 0;
1305 }
1306
1307 /* Use do_names instead. */
1308 static int
1309 spc_handler_pdfm_dest (struct spc_env *spe, struct spc_arg *args)
1310 {
1311 pdf_obj *name, *array;
1312
1313 skip_white(&args->curptr, args->endptr);
1314
1315 name = parse_pdf_object(&args->curptr, args->endptr, NULL);
1316 if (!name) {
1317 spc_warn(spe, "PDF string expected for destination name but not found.");
1318 return -1;
1319 } else if (!PDF_OBJ_STRINGTYPE(name)) {
1320 spc_warn(spe, "PDF string expected for destination name but invalid type.");
1321 pdf_release_obj(name);
1322 return -1;
1323 }
1324
1325 #if 0
1326 if ((dpx_conf.compat_mode == dpx_mode_xdv_mode) && maybe_reencode_utf8(name) < 0)
1327 WARN("Failed to convert input string to UTF16...");
1328 #endif
1329
1330 array = parse_pdf_object_extended(&args->curptr, args->endptr, NULL, parse_pdf_reference, spe);
1331 if (!array) {
1332 spc_warn(spe, "No destination specified for pdf:dest.");
1333 pdf_release_obj(name);
1334 return -1;
1335 } else if (!PDF_OBJ_ARRAYTYPE(array)) {
1336 spc_warn(spe, "Destination not specified as an array object!");
1337 pdf_release_obj(name);
1338 pdf_release_obj(array);
1339 return -1;
1340 }
1341
1342 pdf_doc_add_names("Dests",
1343 pdf_string_value (name),
1344 pdf_string_length(name),
1345 array);
1346 pdf_release_obj(name);
1347
1348 return 0;
1349 }
1350
1351 static int
1352 spc_handler_pdfm_names (struct spc_env *spe, struct spc_arg *args)
1353 {
1354 pdf_obj *category, *key, *value, *tmp;
1355 int i, size;
1356
1357 category = parse_pdf_object(&args->curptr, args->endptr, NULL);
1358 if (!category) {
1359 spc_warn(spe, "PDF name expected but not found.");
1360 return -1;
1361 } else if (!PDF_OBJ_NAMETYPE(category)) {
1362 spc_warn(spe, "PDF name expected but not found.");
1363 pdf_release_obj(category);
1364 return -1;
1365 }
1366
1367 tmp = parse_pdf_object_extended(&args->curptr, args->endptr, NULL, parse_pdf_reference, spe);
1368 if (!tmp) {
1369 spc_warn(spe, "PDF object expected but not found.");
1370 pdf_release_obj(category);
1371 return -1;
1372 } else if (PDF_OBJ_ARRAYTYPE(tmp)) {
1373 size = pdf_array_length(tmp);
1374 if (size % 2 != 0) {
1375 spc_warn(spe, "Array size not multiple of 2 for pdf:names.");
1376 pdf_release_obj(category);
1377 pdf_release_obj(tmp);
1378 return -1;
1379 }
1380
1381 for (i = 0; i < size / 2; i++) {
1382 key = pdf_get_array(tmp, 2 * i);
1383 value = pdf_get_array(tmp, 2 * i + 1);
1384 if (!PDF_OBJ_STRINGTYPE(key)) {
1385 spc_warn(spe, "Name tree key must be string.");
1386 pdf_release_obj(category);
1387 pdf_release_obj(tmp);
1388 return -1;
1389 } else if (pdf_doc_add_names(pdf_name_value(category),
1390 pdf_string_value (key),
1391 pdf_string_length(key),
1392 pdf_link_obj(value)) < 0) {
1393 spc_warn(spe, "Failed to add Name tree entry...");
1394 pdf_release_obj(category);
1395 pdf_release_obj(tmp);
1396 return -1;
1397 }
1398 }
1399 pdf_release_obj(tmp);
1400 } else if (PDF_OBJ_STRINGTYPE(tmp)) {
1401 key = tmp;
1402 value = parse_pdf_object_extended(&args->curptr, args->endptr, NULL, parse_pdf_reference, spe);
1403 if (!value) {
1404 pdf_release_obj(category);
1405 pdf_release_obj(key);
1406 spc_warn(spe, "PDF object expected but not found.");
1407 return -1;
1408 }
1409 if (pdf_doc_add_names(pdf_name_value(category),
1410 pdf_string_value (key),
1411 pdf_string_length(key),
1412 value) < 0) {
1413 spc_warn(spe, "Failed to add Name tree entry...");
1414 pdf_release_obj(category);
1415 pdf_release_obj(key);
1416 return -1;
1417 }
1418 pdf_release_obj(key);
1419 } else {
1420 pdf_release_obj(tmp);
1421 pdf_release_obj(category);
1422 spc_warn(spe, "Invalid object type for pdf:names.");
1423 return -1;
1424 }
1425 pdf_release_obj(category);
1426
1427 return 0;
1428 }
1429
1430 static int
1431 spc_handler_pdfm_docinfo (struct spc_env *spe, struct spc_arg *args)
1432 {
1433 struct spc_pdf_ *sd = &_pdf_stat;
1434 pdf_obj *docinfo, *dict;
1435
1436 dict = parse_pdf_dict_with_tounicode(&args->curptr, args->endptr, &sd->cd);
1437 if (!dict) {
1438 spc_warn(spe, "Dictionary object expected but not found.");
1439 return -1;
1440 }
1441
1442 docinfo = pdf_doc_docinfo();
1443 pdf_merge_dict(docinfo, dict);
1444 pdf_release_obj(dict);
1445
1446 return 0;
1447 }
1448
1449 static int
1450 spc_handler_pdfm_docview (struct spc_env *spe, struct spc_arg *args)
1451 {
1452 struct spc_pdf_ *sd = &_pdf_stat;
1453 pdf_obj *catalog, *dict;
1454 pdf_obj *pref_old, *pref_add;
1455
1456 dict = parse_pdf_dict_with_tounicode(&args->curptr, args->endptr, &sd->cd);
1457 if (!dict) {
1458 spc_warn(spe, "Dictionary object expected but not found.");
1459 return -1;
1460 }
1461
1462 catalog = pdf_doc_catalog();
1463 /* Avoid overriding whole ViewerPreferences */
1464 pref_old = pdf_lookup_dict(catalog, "ViewerPreferences");
1465 pref_add = pdf_lookup_dict(dict, "ViewerPreferences");
1466 if (pref_old && pref_add) {
1467 pdf_merge_dict (pref_old, pref_add);
1468 pdf_remove_dict(dict, "ViewerPreferences");
1469 }
1470 pdf_merge_dict (catalog, dict);
1471 pdf_release_obj(dict);
1472
1473 return 0;
1474 }
1475
1476 static int
1477 spc_handler_pdfm_close (struct spc_env *spe, struct spc_arg *args)
1478 {
1479 char *ident;
1480
1481 skip_white(&args->curptr, args->endptr);
1482 ident = parse_opt_ident(&args->curptr, args->endptr);
1483 if (ident) {
1484 spc_flush_object(spe, ident);
1485 RELEASE(ident);
1486 } else { /* Close all? */
1487 spc_warn(spe, "pdf:close without an argument no longer supported!");
1488 spc_clear_objects(spe);
1489 }
1490
1491 return 0;
1492 }
1493
1494 static int
1495 spc_handler_pdfm_object (struct spc_env *spe, struct spc_arg *args)
1496 {
1497 char *ident;
1498 pdf_obj *object;
1499
1500 skip_white(&args->curptr, args->endptr);
1501 ident = parse_opt_ident(&args->curptr, args->endptr);
1502 if (!ident) {
1503 spc_warn(spe, "Could not find a object identifier.");
1504 return -1;
1505 }
1506
1507 object = parse_pdf_object_extended(&args->curptr, args->endptr, NULL, parse_pdf_reference, spe);
1508 if (!object) {
1509 spc_warn(spe, "Could not find an object definition for \"%s\".", ident);
1510 RELEASE(ident);
1511 return -1;
1512 } else {
1513 spc_push_object(spe, ident, object);
1514 }
1515 RELEASE(ident);
1516
1517 return 0;
1518 }
1519
1520 static int
1521 spc_handler_pdfm_content (struct spc_env *spe, struct spc_arg *args)
1522 {
1523 int len = 0;
1524
1525 skip_white(&args->curptr, args->endptr);
1526 if (args->curptr < args->endptr) {
1527 pdf_tmatrix M;
1528 pdf_coord cp;
1529
1530 spc_get_current_point(spe, &cp);
1531 pdf_setmatrix(&M, 1.0, 0.0, 0.0, 1.0, cp.x, cp.y);
1532 work_buffer[len++] = ' ';
1533 work_buffer[len++] = 'q';
1534 work_buffer[len++] = ' ';
1535 len += pdf_sprint_matrix(work_buffer + len, &M);
1536 work_buffer[len++] = ' ';
1537 work_buffer[len++] = 'c';
1538 work_buffer[len++] = 'm';
1539 work_buffer[len++] = ' ';
1540
1541 pdf_doc_add_page_content(work_buffer, len); /* op: q cm */
1542 len = (int) (args->endptr - args->curptr);
1543 pdf_doc_add_page_content(args->curptr, len); /* op: ANY */
1544 pdf_doc_add_page_content(" Q", 2); /* op: Q */
1545 }
1546 args->curptr = args->endptr;
1547
1548 return 0;
1549 }
1550
1551 static int
1552 spc_handler_pdfm_literal (struct spc_env *spe, struct spc_arg *args)
1553 {
1554 int direct = 0;
1555
1556 skip_white(&args->curptr, args->endptr);
1557 while (args->curptr < args->endptr) {
1558 if (args->curptr + 7 <= args->endptr &&
1559 !strncmp(args->curptr, "reverse", 7)) {
1560 args->curptr += 7;
1561 WARN("The special \"pdf:literal reverse ...\" is no longer supported.\nIgnore the \"reverse\" option.");
1562 } else if (args->curptr + 6 <= args->endptr &&
1563 !strncmp(args->curptr, "direct", 6)) {
1564 direct = 1;
1565 args->curptr += 6;
1566 } else {
1567 break;
1568 }
1569 skip_white(&args->curptr, args->endptr);
1570 }
1571
1572 if (args->curptr < args->endptr) {
1573 pdf_tmatrix M;
1574 pdf_coord cp;
1575
1576 spc_get_current_point(spe, &cp);
1577 if (!direct) {
1578 M.a = M.d = 1.0; M.b = M.c = 0.0;
1579 M.e = cp.x; M.f = cp.y;
1580 pdf_dev_concat(&M);
1581 }
1582 pdf_doc_add_page_content(" ", 1); /* op: */
1583 pdf_doc_add_page_content(args->curptr, (int) (args->endptr - args->curptr)); /* op: ANY */
1584 if (!direct) {
1585 M.e = -cp.x; M.f = -cp.y;
1586 pdf_dev_concat(&M);
1587 }
1588 }
1589
1590 args->curptr = args->endptr;
1591
1592 return 0;
1593 }
1594
1595 static int
1596 spc_handler_pdfm_bcontent (struct spc_env *spe, struct spc_arg *args)
1597 {
1598 pdf_tmatrix M;
1599 double xpos, ypos;
1600
1601 pdf_dev_gsave();
1602 spc_get_coord(spe, &xpos, &ypos);
1603 pdf_setmatrix(&M, 1.0, 0.0, 0.0, 1.0, spe->x_user - xpos, spe->y_user - ypos);
1604 pdf_dev_concat(&M);
1605 spc_push_coord(spe, spe->x_user, spe->y_user);
1606
1607 return 0;
1608 }
1609
1610 static int
1611 spc_handler_pdfm_econtent (struct spc_env *spe, struct spc_arg *args)
1612 {
1613 spc_pop_coord(spe);
1614 pdf_dev_grestore();
1615 pdf_dev_reset_color(0);
1616 pdf_dev_reset_xgstate(0);
1617
1618 return 0;
1619 }
1620
1621 static int
1622 spc_handler_pdfm_code (struct spc_env *spe, struct spc_arg *args)
1623 {
1624 skip_white(&args->curptr, args->endptr);
1625
1626 if (args->curptr < args->endptr) {
1627 pdf_doc_add_page_content(" ", 1); /* op: */
1628 pdf_doc_add_page_content(args->curptr, (int) (args->endptr - args->curptr)); /* op: ANY */
1629 args->curptr = args->endptr;
1630 }
1631
1632 return 0;
1633 }
1634
1635 static int
1636 spc_handler_pdfm_do_nothing (struct spc_env *spe, struct spc_arg *args)
1637 {
1638 args->curptr = args->endptr;
1639 return 0;
1640 }
1641
1642 #define STRING_STREAM 0
1643 #define FILE_STREAM 1
1644
1645 static int
1646 spc_handler_pdfm_stream_with_type (struct spc_env *spe, struct spc_arg *args, int type)
1647 {
1648 pdf_obj *fstream;
1649 int nb_read;
1650 char *ident, *instring, *fullname;
1651 pdf_obj *tmp;
1652 FILE *fp;
1653
1654 skip_white(&args->curptr, args->endptr);
1655
1656 ident = parse_opt_ident(&args->curptr, args->endptr);
1657 if (!ident) {
1658 spc_warn(spe, "Missing objname for pdf:(f)stream.");
1659 return -1;
1660 }
1661
1662 skip_white(&args->curptr, args->endptr);
1663
1664 tmp = parse_pdf_object(&args->curptr, args->endptr, NULL);
1665 if (!tmp) {
1666 spc_warn(spe, "Missing input string for pdf:(f)stream.");
1667 RELEASE(ident);
1668 return -1;
1669 } else if (!PDF_OBJ_STRINGTYPE(tmp)) {
1670 spc_warn(spe, "Invalid type of input string for pdf:(f)stream.");
1671 pdf_release_obj(tmp);
1672 RELEASE(ident);
1673 return -1;
1674 }
1675
1676 instring = pdf_string_value(tmp);
1677
1678 switch (type) {
1679 case FILE_STREAM:
1680 if (!instring) {
1681 spc_warn(spe, "Missing filename for pdf:fstream.");
1682 pdf_release_obj(tmp);
1683 RELEASE(ident);
1684 return -1;
1685 }
1686 fullname = kpse_find_pict(instring);
1687 if (!fullname) {
1688 spc_warn(spe, "File \"%s\" not found.", instring);
1689 pdf_release_obj(tmp);
1690 RELEASE(ident);
1691 return -1;
1692 }
1693 fp = DPXFOPEN(fullname, DPX_RES_TYPE_BINARY);
1694 if (!fp) {
1695 spc_warn(spe, "Could not open file: %s", instring);
1696 pdf_release_obj(tmp);
1697 RELEASE(ident);
1698 RELEASE(fullname);
1699 return -1;
1700 }
1701 fstream = pdf_new_stream(STREAM_COMPRESS);
1702 while ((nb_read =
1703 fread(work_buffer, sizeof(char), WORK_BUFFER_SIZE, fp)) > 0)
1704 pdf_add_stream(fstream, work_buffer, nb_read);
1705 MFCLOSE(fp);
1706 RELEASE(fullname);
1707 break;
1708 case STRING_STREAM:
1709 fstream = pdf_new_stream(STREAM_COMPRESS);
1710 pdf_add_stream(fstream, pdf_string_value(tmp), pdf_string_length(tmp));
1711 break;
1712 default:
1713 pdf_release_obj(tmp);
1714 RELEASE(ident);
1715 return -1;
1716 }
1717 pdf_release_obj(tmp);
1718
1719 /*
1720 * Optional dict.
1721 *
1722 * TODO: check Length, Filter...
1723 */
1724 skip_white(&args->curptr, args->endptr);
1725
1726 if (args->curptr[0] == '<') {
1727 pdf_obj *stream_dict;
1728
1729 stream_dict = pdf_stream_dict(fstream);
1730
1731 tmp = parse_pdf_object_extended(&args->curptr, args->endptr, NULL, parse_pdf_reference, spe);
1732 if (!tmp) {
1733 spc_warn(spe, "Parsing dictionary failed.");
1734 pdf_release_obj(fstream);
1735 RELEASE(ident);
1736 return -1;
1737 } else if (!PDF_OBJ_DICTTYPE(tmp)) {
1738 spc_warn(spe, "Expecting dictionary type object but non-dictionary type found.");
1739 pdf_release_obj(fstream);
1740 pdf_release_obj(tmp);
1741 RELEASE(ident);
1742 return -1;
1743 }
1744 if (pdf_lookup_dict(tmp, "Length")) {
1745 pdf_remove_dict(tmp, "Length");
1746 } else if (pdf_lookup_dict(tmp, "Filter")) {
1747 pdf_remove_dict(tmp, "Filter");
1748 }
1749 pdf_merge_dict(stream_dict, tmp);
1750 pdf_release_obj(tmp);
1751 }
1752
1753 /* Users should explicitly close this. */
1754 spc_push_object(spe, ident, fstream);
1755 RELEASE(ident);
1756
1757 return 0;
1758 }
1759
1760 /*
1761 * STREAM: Create a PDF stream object from an input string.
1762 *
1763 * pdf: stream @objname (input_string) [PDF_DICT]
1764 */
1765 static int
1766 spc_handler_pdfm_stream (struct spc_env *spe, struct spc_arg *args)
1767 {
1768 return spc_handler_pdfm_stream_with_type (spe, args, STRING_STREAM);
1769 }
1770
1771 /*
1772 * FSTREAM: Create a PDF stream object from an existing file.
1773 *
1774 * pdf: fstream @objname (filename) [PDF_DICT]
1775 */
1776 static int
1777 spc_handler_pdfm_fstream (struct spc_env *spe, struct spc_arg *args)
1778 {
1779 return spc_handler_pdfm_stream_with_type (spe, args, FILE_STREAM);
1780 }
1781
1782 /* Grab page content as follows:
1783 *
1784 * Reference point = (x_user, y_user)
1785 *
1786 * Case 1. \special{pdf:bxobj @obj width WD height HT depth DP}
1787 *
1788 * Grab the box with the lower-left corner (x_user, y_user-DP)
1789 * and the upper right corner (x_user+WD, y_user+HT).
1790 *
1791 * Case 2. \special{pdf:bxobj @obj bbox LLX LLY URX, URY}
1792 *
1793 * Grab the box with the lower-left corner (x_user+LLX, y_user+LLY)
1794 * and the upper right corner (x_user+URX, y_user+URY).
1795 *
1796 * Note that scale, xscale, yscale, xoffset, yoffset options are ignored.
1797 */
1798 static int
1799 spc_handler_pdfm_bform (struct spc_env *spe, struct spc_arg *args)
1800 {
1801 int error;
1802 char *ident;
1803 pdf_rect cropbox;
1804 pdf_coord cp;
1805 transform_info ti;
1806
1807 skip_white(&args->curptr, args->endptr);
1808
1809 ident = parse_opt_ident(&args->curptr, args->endptr);
1810 if (!ident) {
1811 spc_warn(spe, "A form XObject must have name.");
1812 return -1;
1813 }
1814
1815 transform_info_clear(&ti);
1816 if (spc_util_read_dimtrns(spe, &ti, args, 0) < 0) {
1817 RELEASE(ident);
1818 return -1;
1819 }
1820
1821 /* A XForm with zero dimension results in a non-invertible transformation
1822 * matrix. And it may result in unpredictable behaviour. It might be an
1823 * error in Acrobat. Bounding box with zero dimension may cause division
1824 * by zero.
1825 */
1826 if (ti.flags & INFO_HAS_USER_BBOX) {
1827 if (ti.bbox.urx - ti.bbox.llx == 0.0 ||
1828 ti.bbox.ury - ti.bbox.lly == 0.0) {
1829 spc_warn(spe, "Bounding box has a zero dimension.");
1830 RELEASE(ident);
1831 return -1;
1832 }
1833 cropbox.llx = ti.bbox.llx;
1834 cropbox.lly = ti.bbox.lly;
1835 cropbox.urx = ti.bbox.urx;
1836 cropbox.ury = ti.bbox.ury;
1837 } else {
1838 if (ti.width == 0.0 ||
1839 ti.depth + ti.height == 0.0) {
1840 spc_warn(spe, "Bounding box has a zero dimension.");
1841 RELEASE(ident);
1842 return -1;
1843 }
1844 cropbox.llx = 0.0;
1845 cropbox.lly = -ti.depth;
1846 cropbox.urx = ti.width;
1847 cropbox.ury = ti.height;
1848 }
1849
1850 spc_get_current_point(spe, &cp);
1851 error = spc_begin_form(spe, ident, cp, &cropbox);
1852
1853 if (error)
1854 spc_warn(spe, "Couldn't start form object.");
1855
1856 RELEASE(ident);
1857
1858 return error;
1859 }
1860
1861 /* An extra dictionary after exobj must be merged to the form dictionary,
1862 * not resource dictionary.
1863 * Please use pdf:put @resources (before pdf:exobj) instead.
1864 */
1865 static int
1866 spc_handler_pdfm_eform (struct spc_env *spe, struct spc_arg *args)
1867 {
1868 int error;
1869 pdf_obj *attrib = NULL;
1870 struct spc_pdf_ *sd = &_pdf_stat;
1871
1872 skip_white(&args->curptr, args->endptr);
1873
1874 if (args->curptr < args->endptr) {
1875 attrib = parse_pdf_object_extended(&args->curptr, args->endptr, NULL, parse_pdf_reference, spe);
1876 if (attrib && !PDF_OBJ_DICTTYPE(attrib)) {
1877 pdf_release_obj(attrib);
1878 attrib = NULL;
1879 }
1880 }
1881 /* pageresources here too */
1882 if (sd->pageresources) {
1883 pdf_foreach_dict(sd->pageresources, forallresourcecategory, NULL);
1884 }
1885 error = spc_end_form(spe, attrib);
1886
1887 return error;
1888 }
1889
1890 /* Saved XObjects can be used as follows:
1891 *
1892 * Reference point = (x_user, y_user)
1893 *
1894 * Case 1. \special{pdf:uxobj @obj width WD height HT depth DP}
1895 *
1896 * Scale the XObject to fit in the box
1897 * [x_user, y_user-DP, x_user+WD, y_user+HT].
1898 *
1899 * Case 2. \special{pdf:uxobj @obj xscale XS yscale YS}
1900 *
1901 * Scale the XObject with XS and YS. Note that width and xscale
1902 * or height and yscale cannot be used together.
1903 *
1904 * Case 3. \special{pdf:bxobj @obj bbox LLX LLY URX, URY}
1905 *
1906 * Scale the XObject to fit in the box
1907 * [x_user+LLX, y_user+LLY, x_user+URX, y_user+URY].
1908 *
1909 * Note that xoffset and yoffset moves the reference point where the
1910 * lower-left corner of the XObject will be put.
1911 */
1912 static int
1913 spc_handler_pdfm_uxobj (struct spc_env *spe, struct spc_arg *args)
1914 {
1915 int xobj_id;
1916 char *ident;
1917 transform_info ti;
1918
1919 skip_white(&args->curptr, args->endptr);
1920
1921 ident = parse_opt_ident(&args->curptr, args->endptr);
1922 if (!ident) {
1923 spc_warn(spe, "No object identifier given.");
1924 return -1;
1925 }
1926
1927 transform_info_clear(&ti);
1928 if (args->curptr < args->endptr) {
1929 if (spc_util_read_dimtrns(spe, &ti, args, 0) < 0) {
1930 RELEASE(ident);
1931 return -1;
1932 }
1933 }
1934
1935 xobj_id = pdf_ximage_findresource(ident);
1936 if (xobj_id < 0) {
1937 xobj_id = pdf_ximage_reserve(ident);
1938 }
1939
1940 spc_put_image(spe, xobj_id, &ti, spe->x_user, spe->y_user);
1941 RELEASE(ident);
1942
1943 return 0;
1944 }
1945
1946 static int
1947 spc_handler_pdfm_link (struct spc_env *spe, struct spc_arg *args)
1948 {
1949 return spc_resume_annot(spe);
1950 }
1951
1952 static int
1953 spc_handler_pdfm_nolink (struct spc_env *spe, struct spc_arg *args)
1954 {
1955 return spc_suspend_annot(spe);
1956 }
1957
1958
1959
1960 /* Handled at BOP */
1961 static int
1962 spc_handler_pdfm_pagesize (struct spc_env *spe, struct spc_arg *args)
1963 {
1964 args->curptr = args->endptr;
1965
1966 return 0;
1967 }
1968
1969 /* Please remove this.
1970 * This should be handled before processing pages!
1971 */
1972 static int
1973 spc_handler_pdfm_bgcolor (struct spc_env *spe, struct spc_arg *args)
1974 {
1975 int error;
1976 pdf_color colorspec;
1977
1978 error = spc_util_read_pdfcolor(spe, &colorspec, args, NULL);
1979 if (error)
1980 spc_warn(spe, "No valid color specified?");
1981 else {
1982 pdf_doc_set_bgcolor(&colorspec);
1983 }
1984
1985 return error;
1986 }
1987
1988 #define THEBUFFLENGTH 1024
1989 static int
1990 spc_handler_pdfm_mapline (struct spc_env *spe, struct spc_arg *ap)
1991 {
1992 fontmap_rec *mrec;
1993 char *map_name, opchr;
1994 int error = 0;
1995 static char buffer[THEBUFFLENGTH];
1996 const char *p;
1997 char *q;
1998 int count;
1999
2000 skip_white(&ap->curptr, ap->endptr);
2001 if (ap->curptr >= ap->endptr) {
2002 spc_warn(spe, "Empty mapline special?");
2003 return -1;
2004 }
2005
2006 opchr = ap->curptr[0];
2007 if (opchr == '-' || opchr == '+')
2008 ap->curptr++;
2009
2010 skip_white(&ap->curptr, ap->endptr);
2011
2012 switch (opchr) {
2013 case '-':
2014 map_name = parse_ident(&ap->curptr, ap->endptr);
2015 if (map_name) {
2016 pdf_remove_fontmap_record(map_name);
2017 RELEASE(map_name);
2018 } else {
2019 spc_warn(spe, "Invalid fontmap line: Missing TFM name.");
2020 error = -1;
2021 }
2022 break;
2023 default:
2024 p = ap->curptr;
2025 q = buffer;
2026 count = 0;
2027 while (p < ap->endptr && count < THEBUFFLENGTH - 1) {
2028 *q++ = *p++;
2029 count++;
2030 }
2031 if (count == THEBUFFLENGTH - 1) {
2032 spc_warn(spe, "Invalid fontmap line: Too long a line.");
2033 *q = 0;
2034 return -1;
2035 }
2036 *q = '\0';
2037 mrec = NEW(1, fontmap_rec);
2038 pdf_init_fontmap_record(mrec);
2039 error = pdf_read_fontmap_line(mrec, buffer, (int) (ap->endptr - ap->curptr), is_pdfm_mapline(buffer));
2040 if (error)
2041 spc_warn(spe, "Invalid fontmap line.");
2042 else if (opchr == '+')
2043 pdf_append_fontmap_record(mrec->map_name, mrec);
2044 else
2045 pdf_insert_fontmap_record(mrec->map_name, mrec);
2046 pdf_clear_fontmap_record(mrec);
2047 RELEASE(mrec);
2048 break;
2049 }
2050 if (!error)
2051 ap->curptr = ap->endptr;
2052
2053 return 0;
2054 }
2055
2056 static int
2057 spc_handler_pdfm_mapfile (struct spc_env *spe, struct spc_arg *args)
2058 {
2059 char *mapfile;
2060 int mode, error = 0;
2061
2062 skip_white(&args->curptr, args->endptr);
2063 if (args->curptr >= args->endptr)
2064 return 0;
2065
2066 switch (args->curptr[0]) {
2067 case '-':
2068 mode = FONTMAP_RMODE_REMOVE;
2069 args->curptr++;
2070 break;
2071 case '+':
2072 mode = FONTMAP_RMODE_APPEND;
2073 args->curptr++;
2074 break;
2075 default:
2076 mode = FONTMAP_RMODE_REPLACE;
2077 break;
2078 }
2079
2080 mapfile = parse_val_ident(&args->curptr, args->endptr);
2081 if (!mapfile) {
2082 spc_warn(spe, "No fontmap file specified.");
2083 return -1;
2084 } else {
2085 error = pdf_load_fontmap_file(mapfile, mode);
2086 }
2087 RELEASE(mapfile);
2088
2089 return error;
2090 }
2091
2092
2093 static int
2094 spc_handler_pdfm_tounicode (struct spc_env *spe, struct spc_arg *args)
2095 {
2096 struct spc_pdf_ *sd = &_pdf_stat;
2097 char *cmap_name;
2098 pdf_obj *taint_keys;
2099
2100 /* First clear */
2101 sd->cd.cmap_id = -1;
2102 sd->cd.unescape_backslash = 0;
2103
2104 skip_white(&args->curptr, args->endptr);
2105 if (args->curptr >= args->endptr) {
2106 spc_warn(spe, "Missing CMap name for pdf:tounicode.");
2107 return -1;
2108 }
2109
2110 /* _FIXME_
2111 * Any valid char allowed for PDF name object should be allowed here.
2112 * The argument to this special should be a PDF name obejct.
2113 * But it's too late to change this special.
2114 */
2115 cmap_name = parse_ident(&args->curptr, args->endptr);
2116 if (!cmap_name) {
2117 spc_warn(spe, "Missing ToUnicode mapping name...");
2118 return -1;
2119 }
2120
2121 sd->cd.cmap_id = CMap_cache_find(cmap_name);
2122 if (sd->cd.cmap_id < 0) {
2123 spc_warn(spe, "Failed to load ToUnicode mapping: %s", cmap_name);
2124 RELEASE(cmap_name);
2125 return -1;
2126 }
2127
2128 /* Shift-JIS like encoding may contain backslash in 2nd byte.
2129 * WARNING: This will add nasty extension to PDF parser.
2130 */
2131 if (sd->cd.cmap_id >= 0) {
2132 if (strstr(cmap_name, "RKSJ") ||
2133 strstr(cmap_name, "B5") ||
2134 strstr(cmap_name, "GBK") ||
2135 strstr(cmap_name, "KSC"))
2136 sd->cd.unescape_backslash = 1;
2137 }
2138 RELEASE(cmap_name);
2139
2140 /* Additional "taint key"
2141 * An array of PDF name objects can be supplied optionally.
2142 * Dictionary entries specified by this option will be added to the list
2143 * of dictionary keys to be treated as the target of "ToUnicode" conversion.
2144 */
2145 skip_white(&args->curptr, args->endptr);
2146 if (args->curptr < args->endptr) {
2147 taint_keys = parse_pdf_object(&args->curptr, args->endptr, NULL);
2148 if (taint_keys) {
2149 if (PDF_OBJ_ARRAYTYPE(taint_keys)) {
2150 int i;
2151 for (i = 0; i < pdf_array_length(taint_keys); i++) {
2152 pdf_obj *key;
2153
2154 key = pdf_get_array(taint_keys, i);
2155 if (PDF_OBJ_NAMETYPE(key))
2156 pdf_add_array(sd->cd.taintkeys, pdf_link_obj(key));
2157 else {
2158 spc_warn(spe, "Invalid argument specified in pdf:tounicode special.");
2159 }
2160 }
2161 } else {
2162 spc_warn(spe, "Invalid argument specified in pdf:unicode special.");
2163 }
2164 pdf_release_obj(taint_keys);
2165 }
2166 }
2167
2168 return 0;
2169 }
2170
2171 static int
2172 spc_handler_pdfm_pageresources (struct spc_env *spe, struct spc_arg *args)
2173 {
2174 struct spc_pdf_ *sd = &_pdf_stat;
2175 pdf_obj *dict;
2176
2177 dict = parse_pdf_object_extended(&args->curptr, args->endptr, NULL, parse_pdf_reference, spe);
2178 if (!dict) {
2179 spc_warn(spe, "Dictionary object expected but not found.");
2180 return -1;
2181 }
2182
2183 if (sd->pageresources)
2184 pdf_release_obj(sd->pageresources);
2185 sd->pageresources = dict;
2186
2187 return 0;
2188 }
2189
2190 static int
2191 spc_handler_pdfm_bxgstate (struct spc_env *spe, struct spc_arg *args)
2192 {
2193 pdf_obj *obj;
2194
2195 skip_white(&args->curptr, args->endptr);
2196 obj = parse_pdf_object_extended(&args->curptr, args->endptr, NULL, parse_pdf_reference, spe);
2197 if (!obj) {
2198 spc_warn(spe, "Could not find an object definition.");
2199 return -1;
2200 } else if (!PDF_OBJ_DICTTYPE(obj)) {
2201 spc_warn(spe, "Parsed object for ExtGState not a dictionary object!");
2202 pdf_release_obj(obj);
2203 return -1;
2204 }
2205 pdf_dev_xgstate_push(obj);
2206
2207 skip_white(&args->curptr, args->endptr);
2208
2209 return 0;
2210 }
2211
2212 static int
2213 spc_handler_pdfm_exgstate (struct spc_env *spe, struct spc_arg *args)
2214 {
2215 pdf_dev_xgstate_pop();
2216 skip_white(&args->curptr, args->endptr);
2217 return 0;
2218 }
2219
2220 static int
2221 spc_handler_pdft_compat_page (struct spc_env *spe, struct spc_arg *args)
2222 {
2223 skip_white(&args->curptr, args->endptr);
2224 if (args->curptr < args->endptr) {
2225 pdf_doc_add_page_content(" ", 1); /* op: */
2226 pdf_doc_add_page_content(args->curptr, (int) (args->endptr - args->curptr)); /* op: ANY */
2227 }
2228
2229 args->curptr = args->endptr;
2230
2231 return 0;
2232 }
2233
2234 static struct spc_handler pdfm_handlers[] = {
2235 {"annotation", spc_handler_pdfm_annot},
2236 {"annotate", spc_handler_pdfm_annot},
2237 {"annot", spc_handler_pdfm_annot},
2238 {"ann", spc_handler_pdfm_annot},
2239
2240 {"outline", spc_handler_pdfm_outline},
2241 {"out", spc_handler_pdfm_outline},
2242
2243 {"article", spc_handler_pdfm_article},
2244 {"art", spc_handler_pdfm_article},
2245
2246 {"bead", spc_handler_pdfm_bead},
2247 {"thread", spc_handler_pdfm_bead},
2248
2249 {"destination", spc_handler_pdfm_dest},
2250 {"dest", spc_handler_pdfm_dest},
2251
2252
2253 {"object", spc_handler_pdfm_object},
2254 {"obj", spc_handler_pdfm_object},
2255
2256
2257 {"docinfo", spc_handler_pdfm_docinfo},
2258 {"docview", spc_handler_pdfm_docview},
2259
2260 {"content", spc_handler_pdfm_content},
2261 {"put", spc_handler_pdfm_put},
2262 {"close", spc_handler_pdfm_close},
2263 {"bop", spc_handler_pdfm_bop},
2264 {"eop", spc_handler_pdfm_eop},
2265
2266 {"image", spc_handler_pdfm_image},
2267 {"img", spc_handler_pdfm_image},
2268 {"epdf", spc_handler_pdfm_image},
2269
2270 {"link", spc_handler_pdfm_link},
2271 {"nolink", spc_handler_pdfm_nolink},
2272
2273 {"begincolor", spc_handler_pdfm_bcolor},
2274 {"bcolor", spc_handler_pdfm_bcolor},
2275 {"bc", spc_handler_pdfm_bcolor},
2276
2277 {"setcolor", spc_handler_pdfm_scolor},
2278 {"scolor", spc_handler_pdfm_scolor},
2279 {"sc", spc_handler_pdfm_scolor},
2280
2281 {"endcolor", spc_handler_pdfm_ecolor},
2282 {"ecolor", spc_handler_pdfm_ecolor},
2283 {"ec", spc_handler_pdfm_ecolor},
2284
2285 {"begingray", spc_handler_pdfm_bcolor},
2286 {"bgray", spc_handler_pdfm_bcolor},
2287 {"bg", spc_handler_pdfm_bcolor},
2288
2289 {"endgray", spc_handler_pdfm_ecolor},
2290 {"egray", spc_handler_pdfm_ecolor},
2291 {"eg", spc_handler_pdfm_ecolor},
2292
2293 {"bgcolor", spc_handler_pdfm_bgcolor},
2294 {"bgc", spc_handler_pdfm_bgcolor},
2295 {"bbc", spc_handler_pdfm_bgcolor},
2296 {"bbg", spc_handler_pdfm_bgcolor},
2297
2298 {"pagesize", spc_handler_pdfm_pagesize},
2299
2300 {"bannot", spc_handler_pdfm_bann},
2301 {"beginann", spc_handler_pdfm_bann},
2302 {"bann", spc_handler_pdfm_bann},
2303
2304 {"eannot", spc_handler_pdfm_eann},
2305 {"endann", spc_handler_pdfm_eann},
2306 {"eann", spc_handler_pdfm_eann},
2307
2308 {"btrans", spc_handler_pdfm_btrans},
2309 {"begintransform", spc_handler_pdfm_btrans},
2310 {"begintrans", spc_handler_pdfm_btrans},
2311 {"bt", spc_handler_pdfm_btrans},
2312
2313 {"etrans", spc_handler_pdfm_etrans},
2314 {"endtransform", spc_handler_pdfm_etrans},
2315 {"endtrans", spc_handler_pdfm_etrans},
2316 {"et", spc_handler_pdfm_etrans},
2317
2318 {"bform", spc_handler_pdfm_bform},
2319 {"beginxobj", spc_handler_pdfm_bform},
2320 {"bxobj", spc_handler_pdfm_bform},
2321
2322 {"eform", spc_handler_pdfm_eform},
2323 {"endxobj", spc_handler_pdfm_eform},
2324 {"exobj", spc_handler_pdfm_eform},
2325
2326 {"usexobj", spc_handler_pdfm_uxobj},
2327 {"uxobj", spc_handler_pdfm_uxobj},
2328
2329 {"tounicode", spc_handler_pdfm_tounicode},
2330 {"literal", spc_handler_pdfm_literal},
2331 {"stream", spc_handler_pdfm_stream},
2332 {"fstream", spc_handler_pdfm_fstream},
2333 {"names", spc_handler_pdfm_names},
2334 {"mapline", spc_handler_pdfm_mapline},
2335 {"mapfile", spc_handler_pdfm_mapfile},
2336
2337 {"bcontent", spc_handler_pdfm_bcontent},
2338 {"econtent", spc_handler_pdfm_econtent},
2339 {"code", spc_handler_pdfm_code},
2340
2341 {"minorversion", spc_handler_pdfm_do_nothing},
2342 {"majorversion", spc_handler_pdfm_do_nothing},
2343 {"encrypt", spc_handler_pdfm_do_nothing},
2344
2345 {"pageresources", spc_handler_pdfm_pageresources},
2346 {"trailerid", spc_handler_pdfm_do_nothing},
2347
2348 {"xannot", spc_handler_pdfm_xann},
2349 {"extendann", spc_handler_pdfm_xann},
2350 {"xann", spc_handler_pdfm_xann},
2351
2352 {"bxgstate", spc_handler_pdfm_bxgstate},
2353 {"exgstate", spc_handler_pdfm_exgstate},
2354 };
2355
2356 static struct spc_handler pdft_compat_handlers[] = {
2357 /* Text supplied to "direct" command should go inside of BT/ET block
2358 * but dvipdfmx currently can't be implemented so.
2359 * Here, "direct" is for the moment just an alias of "page".
2360 */
2361 {"direct", spc_handler_pdft_compat_page},
2362 {"page", spc_handler_pdft_compat_page},
2363 };
2364
2365 int
2366 spc_pdfm_check_special (const char *buf, int len)
2367 {
2368 int r = 0;
2369 const char *p, *endptr;
2370
2371 p = buf;
2372 endptr = p + len;
2373
2374 skip_white(&p, endptr);
2375 if (p + strlen("pdf:") <= endptr &&
2376 !memcmp(p, "pdf:", strlen("pdf:"))) {
2377 r = 1;
2378 }
2379
2380 return r;
2381 }
2382
2383 int
2384 spc_pdfm_setup_handler (struct spc_handler *sph,
2385 struct spc_env *spe, struct spc_arg *ap)
2386 {
2387 int error = -1, i;
2388 char *q;
2389
2390 ASSERT(sph && spe && ap);
2391
2392 skip_white(&ap->curptr, ap->endptr);
2393 if (ap->curptr + strlen("pdf:") >= ap->endptr ||
2394 memcmp(ap->curptr, "pdf:", strlen("pdf:"))) {
2395 spc_warn(spe, "Not pdf: special???");
2396 return -1;
2397 }
2398 ap->curptr += strlen("pdf:");
2399
2400 skip_white(&ap->curptr, ap->endptr);
2401 q = parse_c_ident(&ap->curptr, ap->endptr);
2402 if (q) {
2403 int is_pdft_compat = 0;
2404 if (ap->curptr < ap->endptr) {
2405 if (ap->curptr[0] == ':') {
2406 is_pdft_compat = 1;
2407 ap->curptr++;
2408 }
2409 }
2410 if (is_pdft_compat) {
2411 for (i = 0; i < sizeof(pdft_compat_handlers) / sizeof(struct spc_handler); i++) {
2412 if (!strcmp(q, pdft_compat_handlers[i].key)) {
2413 ap->command = pdft_compat_handlers[i].key;
2414 sph->key = "pdf:";
2415 sph->exec = pdft_compat_handlers[i].exec;
2416 skip_white(&ap->curptr, ap->endptr);
2417 error = 0;
2418 break;
2419 }
2420 }
2421 } else {
2422 for (i = 0; i < sizeof(pdfm_handlers) / sizeof(struct spc_handler); i++) {
2423 if (!strcmp(q, pdfm_handlers[i].key)) {
2424 ap->command = pdfm_handlers[i].key;
2425 sph->key = "pdf:";
2426 sph->exec = pdfm_handlers[i].exec;
2427 skip_white(&ap->curptr, ap->endptr);
2428 error = 0;
2429 break;
2430 }
2431 }
2432 }
2433 RELEASE(q);
2434 }
2435
2436 return error;
2437 }