Debian Codegraph @

texk/dvipdfm-x/spc_misc.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2021 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 <string.h>
28
29 #include "system.h"
30 #include "mem.h"
31 #include "error.h"
32
33 #include "mfileio.h"
34
35 #include "dpxutil.h"
36
37 #include "pdfparse.h"
38 #include "pdfobj.h"
39
40 #include "pdfcolor.h"
41 #include "pdfdraw.h"
42 #include "pdfximage.h"
43 #include "pdfdev.h"
44
45 #include "mpost.h"
46
47 #include "specials.h"
48
49 #include "spc_util.h"
50 #include "spc_misc.h"
51
52 /* pdfcolorstack:
53 * This special is provoded as a compatibility feature to pdftex.
54 */
55 struct stack
56 {
57 int page;
58 int direct;
59 dpx_stack stack;
60 };
61
62 #define PDFCOLORSTACK_MAX_STACK 256
63 struct spc_stack
64 {
65 struct stack stacks[PDFCOLORSTACK_MAX_STACK];
66 };
67
68 static struct spc_stack spc_stack;
69
70 static int
71 pdfcolorstack__get_id (struct spc_env *spe, int *id, struct spc_arg *args)
72 {
73 char *q;
74
75 if (args->curptr >= args->endptr) {
76 spc_warn(spe, "Stack ID number expected but not found.");
77 return -1;
78 }
79 q = parse_number(&args->curptr, args->endptr);
80 if (!q) {
81 spc_warn(spe, "Stack ID number expected but not found.");
82 return -1;
83 }
84 *id = atoi(q);
85 RELEASE(q);
86
87 skip_white(&args->curptr, args->endptr);
88
89 return 0;
90 }
91
92 static int
93 pdfcolorstack__init (void *dp)
94 {
95 struct spc_stack *sd = dp;
96 int i;
97
98 for (i = 0; i < PDFCOLORSTACK_MAX_STACK; i++) {
99 sd->stacks[i].page = 0;
100 sd->stacks[i].direct = 0;
101 dpx_stack_init(&sd->stacks[i].stack);
102 }
103
104 return 0;
105 }
106
107 static int
108 pdfcolorstack__clean (void *dp)
109 {
110 struct spc_stack *sd = dp;
111 int i;
112
113 for (i = 0; i < PDFCOLORSTACK_MAX_STACK; i++) {
114 pdf_obj *litstr;
115 dpx_stack *stk = &sd->stacks[i].stack;
116
117 while ((litstr = dpx_stack_pop(stk)) != NULL) {
118 pdf_release_obj(litstr);
119 }
120 }
121
122 return 0;
123 }
124
125 static void
126 pdfcolorstack__set_litstr (pdf_coord cp, pdf_obj *litstr, int direct)
127 {
128 pdf_tmatrix M;
129
130 if (!litstr)
131 return;
132
133 if (!direct) {
134 M.a = M.d = 1.0; M.b = M.c = 0.0;
135 M.e = cp.x; M.f = cp.y;
136 pdf_dev_concat(&M);
137 }
138 pdf_doc_add_page_content(" ", 1);
139 pdf_doc_add_page_content(pdf_string_value(litstr), pdf_string_length(litstr));
140 if (!direct) {
141 M.e = -cp.x; M.f = -cp.y;
142 pdf_dev_concat(&M);
143 }
144 }
145
146 static int
147 spc_handler_pdfcolorstackinit (struct spc_env *spe, struct spc_arg *args)
148 {
149 int id = -1;
150 struct stack *st;
151 char *q;
152 pdf_coord cp = {0.0, 0.0};
153 pdf_obj *litstr;
154
155 skip_white(&args->curptr, args->endptr);
156 if (args->curptr >= args->endptr)
157 return -1;
158
159 if (pdfcolorstack__get_id(spe, &id, args) < 0)
160 return -1;
161 if (id < 0 || id >= PDFCOLORSTACK_MAX_STACK) {
162 spc_warn(spe, "Invalid stack number specified: %d", id);
163 return -1;
164 }
165 skip_white(&args->curptr, args->endptr);
166
167 st = &spc_stack.stacks[id];
168 if (dpx_stack_depth(&st->stack) > 0) {
169 spc_warn(spe, "Stadk ID=%d already initialized?", id);
170 return -1;
171 }
172
173 while ((q = parse_c_ident(&args->curptr, args->endptr)) != NULL) {
174 if (!strcmp(q, "page")) {
175 st->page = 1;
176 } else if (!strcmp(q, "direct")) {
177 st->direct = 1;
178 } else {
179 spc_warn(spe, "Ignoring unknown option for pdfcolorstack special (init): %s", q);
180 }
181 RELEASE(q);
182 skip_white(&args->curptr, args->endptr);
183 }
184
185 if (args->curptr < args->endptr) {
186 litstr = parse_pdf_string(&args->curptr, args->endptr);
187 if (litstr) {
188 dpx_stack_push(&st->stack, litstr);
189 pdfcolorstack__set_litstr(cp, litstr, st->direct);
190 }
191 skip_white(&args->curptr, args->endptr);
192 } else {
193 spc_warn(spe, "No valid PDF literal specified.");
194 return -1;
195 }
196
197 return 0;
198 }
199
200 static int
201 pdfcolorstack__set (struct spc_env *spe, struct stack *st, pdf_coord cp, struct spc_arg *args)
202 {
203 pdf_obj *litstr;
204
205 skip_white(&args->curptr, args->endptr);
206 if (args->curptr >= args->endptr)
207 return -1;
208
209 litstr = dpx_stack_pop(&st->stack);
210 if (!litstr) {
211 spc_warn(spe, "Stack empty!");
212 return -1;
213 }
214 pdf_release_obj(litstr);
215
216 litstr = parse_pdf_string(&args->curptr, args->endptr);
217 if (litstr) {
218 dpx_stack_push(&st->stack, litstr);
219 pdfcolorstack__set_litstr(cp, litstr, st->direct);
220 skip_white(&args->curptr, args->endptr);
221 }
222
223 return 0;
224 }
225
226 static int
227 pdfcolorstack__push (struct spc_env *spe, struct stack *st, pdf_coord cp, struct spc_arg *args)
228 {
229 pdf_obj *litstr;
230
231 skip_white(&args->curptr, args->endptr);
232 if (args->curptr >= args->endptr)
233 return -1;
234
235 litstr = parse_pdf_string(&args->curptr, args->endptr);
236 if (litstr) {
237 dpx_stack_push(&st->stack, litstr);
238 pdfcolorstack__set_litstr(cp, litstr, st->direct);
239 skip_white(&args->curptr, args->endptr);
240 }
241
242 return 0;
243 }
244
245 static int
246 pdfcolorstack__current (struct spc_env *spe, struct stack *st, pdf_coord cp, struct spc_arg *args)
247 {
248 pdf_obj *litstr;
249
250 litstr = dpx_stack_top(&st->stack);
251 if (litstr) {
252 pdfcolorstack__set_litstr(cp, litstr, st->direct);
253 skip_white(&args->curptr, args->endptr);
254 } else {
255 spc_warn(spe, "Stack empty!");
256 return -1;
257 }
258
259 return 0;
260 }
261
262 static int
263 pdfcolorstack__pop (struct spc_env *spe, struct stack *st, pdf_coord cp, struct spc_arg *args)
264 {
265 int error = 0;
266 pdf_obj *litstr;
267
268 /* "default" at the bottom */
269 if (dpx_stack_depth(&st->stack) < 2) {
270 spc_warn(spe, "Stack underflow");
271 return -1;
272 }
273 litstr = dpx_stack_pop(&st->stack);
274 if (litstr) {
275 pdf_release_obj(litstr);
276 }
277 litstr = dpx_stack_top(&st->stack);
278 if (litstr) {
279 pdfcolorstack__set_litstr(cp, litstr, st->direct);
280 }
281
282 return error;
283 }
284
285 static int
286 spc_handler_pdfcolorstack (struct spc_env *spe, struct spc_arg *args)
287 {
288 int error = 0;
289 int id;
290 char *command;
291 struct stack *st;
292 pdf_coord cp;
293
294 skip_white(&args->curptr, args->endptr);
295 if (args->curptr >= args->endptr)
296 return -1;
297
298 if (pdfcolorstack__get_id(spe, &id, args) < 0)
299 return -1;
300 if (id < 0 || id >= PDFCOLORSTACK_MAX_STACK) {
301 spc_warn(spe, "Invalid stack ID specified: %d", id);
302 return -1;
303 }
304 skip_white(&args->curptr, args->endptr);
305
306 st = &spc_stack.stacks[id];
307 if (dpx_stack_depth(&st->stack) < 1) {
308 spc_warn(spe, "Stack ID=%d not properly initialized?", id);
309 return -1;
310 }
311
312 command = parse_c_ident(&args->curptr, args->endptr);
313 if (!command)
314 return -1;
315
316 spc_get_current_point(spe, &cp);
317 if (!strcmp(command, "set")) {
318 error = pdfcolorstack__set(spe, st, cp, args);
319 } else if (!strcmp(command, "push")) {
320 error = pdfcolorstack__push(spe, st, cp, args);
321 } else if (!strcmp(command, "pop")) {
322 error = pdfcolorstack__pop(spe, st, cp, args);
323 } else if (!strcmp(command, "current")) {
324 error = pdfcolorstack__current(spe, st, cp, args);
325 } else {
326 spc_warn(spe, "Unknown action: %s", command);
327 }
328
329 if (error) {
330 spc_warn(spe, "Error occurred while processing pdfcolorstack: id=%d command=\"%s\"", id, command);
331 }
332
333 RELEASE(command);
334
335 return error;
336 }
337
338 /* Duplicate from spc_pdfm */
339 static pdf_obj *
340 parse_pdf_reference (const char **start, const char *end, void *user_data)
341 {
342 pdf_obj *result = NULL;
343 char *name;
344
345 skip_white(start, end);
346 name = parse_opt_ident(start, end);
347 if (name) {
348 result = spc_lookup_reference(name);
349 if (!result) {
350 WARN("Could not find the named reference (@%s).", name);
351 }
352 RELEASE(name);
353 } else {
354 WARN("Could not find a reference name.");
355 result = NULL;
356 }
357
358 return result;
359 }
360
361 /* pdffontattr */
362 #include "pdffont.h"
363
364 struct fontattr {
365 char *ident;
366 double size;
367 pdf_obj *attr;
368 };
369
370 struct fontattr *fontattrs = NULL;
371 int num_fontattrs = 0;
372 int max_fontattrs = 0;
373
374 static int
375 process_fontattr (const char *ident, double size, pdf_obj *attr)
376 {
377 int font_id;
378 pdf_obj *fontdict;
379
380 ASSERT(ident && attr);
381
382 font_id = pdf_font_findresource(ident, size);
383 if (font_id < 0) {
384 WARN("Could not find specified font resource: %s (%gpt)", ident, size);
385 return -1;
386 }
387
388 fontdict = pdf_get_font_resource(font_id);
389 if (!fontdict) {
390 WARN("Specified object not exist: %s (%gpt)", ident, size);
391 return -1;
392 }
393
394 pdf_merge_dict(fontdict, attr);
395
396 return 0;
397 }
398
399 static int
400 spc_handler_pdffontattr (struct spc_env *spe, struct spc_arg *ap)
401 {
402 struct fontattr *fontattr;
403 char *ident = NULL;
404 double size = 0.0;
405 pdf_obj *attr = NULL;
406
407 skip_white(&ap->curptr, ap->endptr);
408 if (ap->curptr >= ap->endptr)
409 return -1;
410
411 ident = parse_ident(&ap->curptr, ap->endptr);
412 if (!ident) {
413 spc_warn(spe, "Missing a font name.");
414 return -1;
415 }
416 skip_white(&ap->curptr, ap->endptr);
417
418 if (ap->curptr < ap->endptr && ap->curptr[0] != '<') {
419 int error = dpx_util_read_length(&size, 1.0, &ap->curptr, ap->endptr);
420 if (error) {
421 spc_warn(spe, "Font size expected but not found.");
422 RELEASE(ident);
423 return -1;
424 }
425 skip_white(&ap->curptr, ap->endptr);
426 }
427
428 attr = parse_pdf_object_extended(&ap->curptr, ap->endptr, NULL, parse_pdf_reference, spe);
429 if (!attr) {
430 spc_warn(spe, "Failed to parse a PDF dictionary object: %s", ident);
431 RELEASE(ident);
432 return -1;
433 } else if (!PDF_OBJ_DICTTYPE(attr)) {
434 spc_warn(spe, "PDF dict expected but non-dict object found: %s", ident);
435 RELEASE(ident);
436 pdf_release_obj(attr);
437 return -1;
438 }
439 skip_white(&ap->curptr, ap->endptr);
440
441 if (num_fontattrs >= max_fontattrs) {
442 fontattrs = RENEW(fontattrs, max_fontattrs + 256, struct fontattr);
443 max_fontattrs += 256;
444 }
445 fontattr = &fontattrs[num_fontattrs];
446 num_fontattrs += 1;
447
448 fontattr->ident = ident;
449 fontattr->size = size;
450 fontattr->attr = attr;
451
452 return 0;
453 }
454
455 int
456 spc_misc_at_begin_document (void)
457 {
458 struct spc_stack *sd = &spc_stack;
459
460 if (!fontattrs) {
461 fontattrs = NEW(256, struct fontattr);
462 num_fontattrs = 0;
463 max_fontattrs = 256;
464 }
465
466 return pdfcolorstack__init(sd);
467 }
468
469 int
470 spc_misc_at_end_document (void)
471 {
472 struct spc_stack *sd = &spc_stack;
473
474 if (fontattrs) {
475 int i;
476
477 for (i = 0; i < num_fontattrs; i++) {
478 process_fontattr(fontattrs[i].ident, fontattrs[i].size, fontattrs[i].attr);
479 RELEASE(fontattrs[i].ident);
480 pdf_release_obj(fontattrs[i].attr);
481 }
482 RELEASE(fontattrs);
483 fontattrs = NULL;
484 max_fontattrs = num_fontattrs = 0;
485 }
486
487 return pdfcolorstack__clean(sd);
488 }
489
490 int
491 spc_misc_at_begin_page (void)
492 {
493 struct spc_stack *sd = &spc_stack;
494 int i;
495
496 for (i = 0; i < PDFCOLORSTACK_MAX_STACK; i++) {
497 dpx_stack *stk = &sd->stacks[i].stack;
498
499 if (sd->stacks[i].page) {
500 pdf_obj *litstr = dpx_stack_top(stk);
501 pdf_coord cp = {0.0, 0.0};
502
503 if (litstr)
504 pdfcolorstack__set_litstr(cp, litstr, sd->stacks[i].direct);
505 }
506 }
507
508 return 0;
509 }
510
511 int
512 spc_misc_at_begin_form (void)
513 {
514 return spc_misc_at_begin_page();
515 }
516
517 int
518 spc_misc_at_end_form (void)
519 {
520 return spc_misc_at_begin_page();
521 }
522
523 static int
524 spc_handler_postscriptbox (struct spc_env *spe, struct spc_arg *ap)
525 {
526 int form_id, len;
527 transform_info ti;
528 load_options options = {1, 0, NULL};
529 char filename[256], *fullname;
530 char buf[512];
531 FILE *fp;
532
533 ASSERT(spe && ap);
534
535 if (ap->curptr >= ap->endptr) {
536 spc_warn(spe, "No width/height/filename given for postscriptbox special.");
537 return -1;
538 }
539
540 /* input is not NULL terminated */
541 len = (int) (ap->endptr - ap->curptr);
542 len = MIN(511, len);
543 memcpy(buf, ap->curptr, len);
544 buf[len] = '\0';
545
546 transform_info_clear(&ti);
547
548 spc_warn(spe, buf);
549 if (sscanf(buf, "{%lfpt}{%lfpt}{%255[^}]}",
550 &ti.width, &ti.height, filename) != 3) {
551 spc_warn(spe, "Syntax error in postscriptbox special?");
552 return -1;
553 }
554 ap->curptr = ap->endptr;
555
556 ti.width *= 72.0 / 72.27;
557 ti.height *= 72.0 / 72.27;
558
559 fullname = kpse_find_pict(filename);
560 if (!fullname) {
561 spc_warn(spe, "Image file \"%s\" not found.", filename);
562 return -1;
563 }
564
565 fp = MFOPEN(fullname, FOPEN_R_MODE);
566 if (!fp) {
567 spc_warn(spe, "Could not open image file: %s", fullname);
568 RELEASE(fullname);
569 return -1;
570 }
571 RELEASE(fullname);
572
573 ti.flags |= (INFO_HAS_WIDTH|INFO_HAS_HEIGHT);
574
575 for (;;) {
576 const char *p = mfgets(buf, 512, fp);
577 if (!p)
578 break;
579 if (mps_scan_bbox(&p, p + strlen(p), &ti.bbox) >= 0) {
580 ti.flags |= INFO_HAS_USER_BBOX;
581 break;
582 }
583 }
584 MFCLOSE(fp);
585
586 form_id = pdf_ximage_load_image(NULL, filename, options);
587 if (form_id < 0) {
588 spc_warn(spe, "Failed to load image file: %s", filename);
589 return -1;
590 }
591
592 spc_put_image(spe, form_id, &ti, spe->x_user, spe->y_user);
593
594 return 0;
595 }
596
597 static int
598 spc_handler_null (struct spc_env *spe, struct spc_arg *args)
599 {
600 args->curptr = args->endptr;
601
602 return 0;
603 }
604
605 static struct spc_handler misc_handlers[] = {
606 {"postscriptbox", spc_handler_postscriptbox},
607 {"pdfcolorstackinit", spc_handler_pdfcolorstackinit},
608 {"pdfcolorstack", spc_handler_pdfcolorstack},
609 {"pdffontattr", spc_handler_pdffontattr},
610 {"landscape", spc_handler_null}, /* handled at bop */
611 {"papersize", spc_handler_null}, /* handled at bop */
612 {"src:", spc_handler_null}, /* simply ignore */
613 {"pos:", spc_handler_null}, /* simply ignore */
614 {"om:", spc_handler_null} /* simply ignore */
615 };
616
617
618 int
619 spc_misc_check_special (const char *buffer, int size)
620 {
621 const char *p, *endptr;
622 int i;
623
624 p = buffer;
625 endptr = p + size;
626
627 skip_white(&p, endptr);
628 size = (int) (endptr - p);
629 for (i = 0; i < sizeof(misc_handlers)/sizeof(struct spc_handler); i++) {
630 if (size >= strlen(misc_handlers[i].key) &&
631 !strncmp(p, misc_handlers[i].key, strlen(misc_handlers[i].key))) {
632 return 1;
633 }
634 }
635
636 return 0;
637 }
638
639 int
640 spc_misc_setup_handler (struct spc_handler *handle, struct spc_env *spe, struct spc_arg *args)
641 {
642 const char *key;
643 int i, keylen;
644
645 ASSERT(handle && spe && args);
646
647 skip_white(&args->curptr, args->endptr);
648
649 key = args->curptr;
650 while (args->curptr < args->endptr && isalpha((unsigned char)args->curptr[0])) {
651 args->curptr++;
652 }
653
654 if (args->curptr < args->endptr &&
655 args->curptr[0] == ':') {
656 args->curptr++;
657 }
658
659 keylen = (int) (args->curptr - key);
660 if (keylen < 1) {
661 return -1;
662 }
663
664 for (i = 0; i < sizeof(misc_handlers)/sizeof(struct spc_handler); i++) {
665 if (keylen == strlen(misc_handlers[i].key) &&
666 !strncmp(key, misc_handlers[i].key, keylen)) {
667
668 skip_white(&args->curptr, args->endptr);
669
670 args->command = misc_handlers[i].key;
671
672 handle->key = "???:";
673 handle->exec = misc_handlers[i].exec;
674
675 return 0;
676 }
677 }
678
679 return -1;
680 }