Debian Codegraph @

texk/dvipdfm-x/mpost.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 #include <string.h>
29 #include <math.h>
30
31 #include "system.h"
32 #include "mem.h"
33 #include "error.h"
34 #include "mfileio.h"
35 #include "numbers.h"
36 #include "dpxconf.h"
37
38 #include "tfm.h"
39
40 #include "pdfobj.h"
41 #include "pdfparse.h"
42 #include "pdfdev.h"
43 #include "pdfdoc.h"
44
45 #include "pdfcolor.h"
46 #include "pdfdraw.h"
47
48 #include "fontmap.h"
49 #include "subfont.h"
50
51 #include "pdfximage.h"
52
53 #include "mpost.h"
54 #include "dvipdfmx.h"
55
56 /*
57 * Define the origin as (llx, lly) in order to
58 * match the new xetex.def and dvipdfmx.def
59 */
60
61 static double Xorigin, Yorigin;
62 static int translate_origin = 0;
63
64 void
65 mps_set_translate_origin (int v) {
66 translate_origin = v;
67 }
68
69 /*
70 * In PDF, current path is not a part of graphics state parameter.
71 * Hence, current path is not saved by the "q" operator and is not
72 * recovered by the "Q" operator. This means that the following PS
73 * code
74 *
75 * <path construction> gsave <path painting> grestore ...
76 *
77 * can't be translated to PDF code
78 *
79 * <path construction> q <path painting> Q ...
80 *
81 * . Only clipping path (which is graphics state parameter in PDF
82 * too) is treated in the same way. So, we write clipping path
83 * immediately and forget about it but remember current path.
84 */
85
86 static int mp_parse_body (const char **start, const char *end, double x_user, double y_user);
87
88 static struct mp_font
89 {
90 char *font_name;
91 int font_id;
92 int tfm_id; /* Used for text width calculation */
93 int subfont_id;
94 double pt_size;
95 } font_stack[PDF_GSAVE_MAX] = {
96 {NULL, -1, -1, -1, 0}
97 };
98 static int currentfont = 0;
99
100 #define CURRENT_FONT() ((currentfont < 0) ? NULL : &font_stack[currentfont])
101 #define FONT_DEFINED(f) ((f) && (f)->font_name && ((f)->font_id >= 0))
102
103 static void
104 clear_mp_font_struct (struct mp_font *font)
105 {
106 ASSERT(font);
107
108 if (font->font_name)
109 RELEASE(font->font_name);
110 font->font_name = NULL;
111 font->font_id = -1;
112 font->tfm_id = -1;
113 font->subfont_id = -1;
114 font->pt_size = 0.0;
115 }
116
117 /* Compatibility */
118 #define MP_CMODE_MPOST 0
119 #define MP_CMODE_DVIPSK 1
120 #define MP_CMODE_PTEXVERT 2
121 static int mp_cmode = MP_CMODE_MPOST;
122
123 static int
124 mp_setfont (const char *font_name, double pt_size)
125 {
126 const char *name = font_name;
127 struct mp_font *font;
128 int subfont_id = -1;
129 fontmap_rec *mrec;
130
131 font = CURRENT_FONT();
132
133 mrec = pdf_lookup_fontmap_record(font_name);
134 if (mrec && mrec->charmap.sfd_name && mrec->charmap.subfont_id) {
135 subfont_id = sfd_load_record(mrec->charmap.sfd_name, mrec->charmap.subfont_id);
136 }
137
138 /* See comments in dvi_locate_font() in dvi.c. */
139 if (mrec && mrec->map_name) {
140 name = mrec->map_name;
141 } else {
142 name = font_name;
143 }
144
145 if (font->font_name)
146 RELEASE(font->font_name);
147 font->font_name = NEW(strlen(font_name) + 1, char);
148 strcpy(font->font_name, font_name);
149 font->subfont_id = subfont_id;
150 font->pt_size = pt_size;
151 font->tfm_id = tfm_open(font_name, 0); /* Need not exist in MP mode */
152 font->font_id = pdf_dev_locate_font(name,
153 (spt_t) (pt_size * dev_unit_dviunit()));
154
155 if (font->font_id < 0) {
156 ERROR("MPOST: No physical font assigned for \"%s\".", font_name);
157 return 1;
158 }
159
160 return 0;
161 }
162
163 static void
164 save_font (void)
165 {
166 struct mp_font *current, *next;
167
168 current = &font_stack[currentfont++];
169 next = &font_stack[currentfont ];
170 if (FONT_DEFINED(current)) {
171 next->font_name = NEW(strlen(current->font_name)+1, char);
172 strcpy(next->font_name, current->font_name);
173 next->font_id = current->font_id;
174 next->pt_size = current->pt_size;
175 next->subfont_id = current->subfont_id;
176 next->tfm_id = current->tfm_id;
177 } else {
178 next->font_name = NULL;
179 next->font_id = -1;
180 next->pt_size = 0.0;
181 next->subfont_id = -1;
182 next->tfm_id = -1;
183 }
184 }
185
186 static void
187 restore_font (void)
188 {
189 struct mp_font *current;
190
191 current = CURRENT_FONT();
192 if (current) {
193 clear_mp_font_struct(current);
194 }
195
196 currentfont--;
197 }
198
199 static void
200 clear_fonts (void)
201 {
202 while (currentfont > 0) {
203 clear_mp_font_struct(&font_stack[currentfont]);
204 currentfont--;
205 }
206 }
207
208 static int
209 is_fontname (const char *token)
210 {
211 fontmap_rec *mrec;
212
213 mrec = pdf_lookup_fontmap_record(token);
214 if (mrec)
215 return 1;
216
217 return tfm_exists(token);
218 }
219
220 int
221 mps_scan_bbox (const char **pp, const char *endptr, pdf_rect *bbox)
222 {
223 char *number;
224 double values[4];
225 int i;
226
227 /* skip_white() skips lines starting '%'... */
228 while (*pp < endptr && isspace((unsigned char)**pp))
229 (*pp)++;
230
231 /* Scan for bounding box record */
232 while (*pp < endptr && **pp == '%') {
233 if (*pp + 14 < endptr &&
234 !strncmp(*pp, "%%BoundingBox:", 14)) {
235
236 *pp += 14;
237
238 for (i = 0; i < 4; i++) {
239 skip_white(pp, endptr);
240 number = parse_number(pp, endptr);
241 if (!number) {
242 break;
243 }
244 values[i] = atof(number);
245 RELEASE(number);
246 }
247 if (i < 4) {
248 return -1;
249 } else {
250 /* The new xetex.def and dvipdfmx.def require bbox->llx = bbox->lly = 0. */
251 if (translate_origin) {
252 bbox->llx = 0;
253 bbox->lly = 0;
254 bbox->urx = values[2] - values[0];
255 bbox->ury = values[3] - values[1];
256
257 Xorigin = (double)values[0];
258 Yorigin = (double)values[1];
259 } else {
260 bbox->llx = values[0];
261 bbox->lly = values[1];
262 bbox->urx = values[2];
263 bbox->ury = values[3];
264
265 Xorigin = 0.0;
266 Yorigin = 0.0;
267 }
268 return 0;
269 }
270 }
271 pdfparse_skip_line (pp, endptr);
272 while (*pp < endptr && isspace((unsigned char)**pp))
273 (*pp)++;
274 }
275
276 return -1;
277 }
278
279 static void
280 skip_prolog (const char **start, const char *end)
281 {
282 int found_prolog = 0;
283 const char *save;
284
285 save = *start;
286 while (*start < end) {
287 if (**start != '%')
288 skip_white(start, end);
289 if (*start >= end)
290 break;
291 if (!strncmp(*start, "%%EndProlog", 11)) {
292 found_prolog = 1;
293 pdfparse_skip_line(start, end);
294 break;
295 } else if (!strncmp(*start, "%%Page:", 7)) {
296 pdfparse_skip_line(start, end);
297 break;
298 }
299 pdfparse_skip_line(start, end);
300 }
301 if (!found_prolog) {
302 *start = save;
303 }
304
305 return;
306 }
307
308 /* PostScript Operators */
309
310 /* Avoid conflict with SET... from <wingdi.h>. */
311 #undef SETLINECAP
312 #undef SETLINEJOIN
313 #undef SETMITERLIMIT
314
315 #define ADD 1
316 #define SUB 2
317 #define MUL 3
318 #define DIV 4
319 #define NEG 5
320 #define TRUNCATE 6
321
322 #define CLEAR 10
323 #define EXCH 11
324 #define POP 12
325
326 #define NEWPATH 31
327 #define CLOSEPATH 32
328 #define MOVETO 33
329 #define RMOVETO 34
330 #define CURVETO 35
331 #define RCURVETO 36
332 #define LINETO 37
333 #define RLINETO 38
334 #define ARC 39
335 #define ARCN 40
336
337 #define FILL 41
338 #define STROKE 42
339 #define SHOW 43
340
341 #define CLIP 44
342 #define EOCLIP 45
343
344 #define SHOWPAGE 49
345
346 #define GSAVE 50
347 #define GRESTORE 51
348
349 #define CONCAT 52
350 #define SCALE 53
351 #define TRANSLATE 54
352 #define ROTATE 55
353
354 #define SETLINEWIDTH 60
355 #define SETDASH 61
356 #define SETLINECAP 62
357 #define SETLINEJOIN 63
358 #define SETMITERLIMIT 64
359
360 #define SETGRAY 70
361 #define SETRGBCOLOR 71
362 #define SETCMYKCOLOR 72
363
364 #define CURRENTPOINT 80
365 #define IDTRANSFORM 81
366 #define DTRANSFORM 82
367
368 #define FINDFONT 201
369 #define SCALEFONT 202
370 #define SETFONT 203
371 #define CURRENTFONT 204
372
373 #define STRINGWIDTH 210
374
375 #define DEF 999
376
377 #define FSHOW 1001
378 #define STEXFIG 1002
379 #define ETEXFIG 1003
380 #define HLW 1004
381 #define VLW 1005
382 #define RD 1006
383 #define B 1007
384
385 static struct operators
386 {
387 const char *token;
388 int opcode;
389 } ps_operators[] = {
390 {"add", ADD},
391 {"mul", MUL},
392 {"div", DIV},
393 {"neg", NEG},
394 {"sub", SUB},
395 {"truncate", TRUNCATE},
396
397 {"clear", CLEAR},
398 {"exch", EXCH},
399 {"pop", POP},
400
401 {"clip", CLIP},
402 {"eoclip", EOCLIP},
403 {"closepath", CLOSEPATH},
404 {"concat", CONCAT},
405
406 {"newpath", NEWPATH},
407 {"moveto", MOVETO},
408 {"rmoveto", RMOVETO},
409 {"lineto", LINETO},
410 {"rlineto", RLINETO},
411 {"curveto", CURVETO},
412 {"rcurveto", RCURVETO},
413 {"arc", ARC},
414 {"arcn", ARCN},
415
416 {"stroke", STROKE},
417 {"fill", FILL},
418 {"show", SHOW},
419 {"showpage", SHOWPAGE},
420
421 {"gsave", GSAVE},
422 {"grestore", GRESTORE},
423 {"translate", TRANSLATE},
424 {"rotate", ROTATE},
425 {"scale", SCALE},
426
427 {"setlinecap", SETLINECAP},
428 {"setlinejoin", SETLINEJOIN},
429 {"setlinewidth", SETLINEWIDTH},
430 {"setmiterlimit", SETMITERLIMIT},
431 {"setdash", SETDASH},
432
433 {"setgray", SETGRAY},
434 {"setrgbcolor", SETRGBCOLOR},
435 {"setcmykcolor", SETCMYKCOLOR},
436
437 {"currentpoint", CURRENTPOINT}, /* This is here for rotate support
438 in graphics package-not MP support */
439 {"dtransform", DTRANSFORM},
440 {"idtransform", IDTRANSFORM},
441
442 {"findfont", FINDFONT},
443 {"scalefont", SCALEFONT},
444 {"setfont", SETFONT},
445 {"currentfont", CURRENTFONT},
446
447 {"stringwidth", STRINGWIDTH},
448
449 {"def", DEF} /* not implemented yet; just work with mptopdf */
450 };
451
452 static struct operators mps_operators[] = {
453 {"fshow", FSHOW}, /* exch findfont exch scalefont setfont show */
454 {"startTexFig", STEXFIG},
455 {"endTexFig", ETEXFIG},
456 {"hlw", HLW}, /* 0 dtransform exch truncate exch idtransform pop setlinewidth */
457 {"vlw", VLW}, /* 0 exch dtransform truncate idtransform pop setlinewidth pop */
458 {"l", LINETO},
459 {"r", RLINETO},
460 {"c", CURVETO},
461 {"m", MOVETO},
462 {"p", CLOSEPATH},
463 {"n", NEWPATH},
464 {"C", SETCMYKCOLOR},
465 {"G", SETGRAY},
466 {"R", SETRGBCOLOR},
467 {"lj", SETLINEJOIN},
468 {"ml", SETMITERLIMIT},
469 {"lc", SETLINECAP},
470 {"S", STROKE},
471 {"F", FILL},
472 {"q", GSAVE},
473 {"Q", GRESTORE},
474 {"s", SCALE},
475 {"t", CONCAT},
476 {"sd", SETDASH},
477 {"rd", RD}, /* [] 0 setdash */
478 {"P", SHOWPAGE},
479 {"B", B}, /* gsave fill grestore */
480 {"W", CLIP}
481 };
482
483 #define NUM_PS_OPERATORS (sizeof(ps_operators)/sizeof(ps_operators[0]))
484 #define NUM_MPS_OPERATORS (sizeof(mps_operators)/sizeof(mps_operators[0]))
485 static int
486 get_opcode (const char *token)
487 {
488 int i;
489
490 for (i = 0; i < NUM_PS_OPERATORS; i++) {
491 if (!strcmp(token, ps_operators[i].token)) {
492 return ps_operators[i].opcode;
493 }
494 }
495
496 for (i = 0; i < NUM_MPS_OPERATORS; i++) {
497 if (!strcmp(token, mps_operators[i].token)) {
498 return mps_operators[i].opcode;
499 }
500 }
501
502 return -1;
503 }
504
505 #define PS_STACK_SIZE 1024
506
507 static pdf_obj *stack[PS_STACK_SIZE];
508 static unsigned top_stack = 0;
509
510 #define POP_STACK() ((top_stack > 0) ? stack[--top_stack] : NULL)
511 #define PUSH_STACK(o,e) { \
512 if (top_stack < PS_STACK_SIZE) { \
513 stack[top_stack++] = (o); \
514 } else { \
515 WARN("PS stack overflow including MetaPost file or inline PS code"); \
516 *(e) = 1; \
517 } \
518 }
519
520 static int
521 do_exch (void)
522 {
523 pdf_obj *tmp;
524
525 if (top_stack < 2)
526 return -1;
527
528 tmp = stack[top_stack-1];
529 stack[top_stack-1] = stack[top_stack-2];
530 stack[top_stack-2] = tmp;
531
532 return 0;
533 }
534
535 static int
536 do_clear (void)
537 {
538 pdf_obj *tmp;
539
540 while (top_stack > 0) {
541 tmp = POP_STACK();
542 if (tmp)
543 pdf_release_obj(tmp);
544 }
545
546 return 0;
547 }
548
549 /* This should be set_bottom and clear (or
550 * have independent stack) to ensure stack
551 * depth do not go below real stack bottom.
552 */
553 static void
554 mps_stack_clear_to (int depth)
555 {
556 pdf_obj *tmp;
557
558 while (top_stack > depth) {
559 tmp = POP_STACK();
560 if (tmp)
561 pdf_release_obj(tmp);
562 }
563
564 return;
565 }
566
567 static int
568 pop_get_numbers (double *values, int count)
569 {
570 pdf_obj *tmp;
571
572 while (count-- > 0) {
573 tmp = POP_STACK();
574 if (!tmp) {
575 WARN("mpost: Stack underflow.");
576 break;
577 } else if (!PDF_OBJ_NUMBERTYPE(tmp)) {
578 WARN("mpost: Not a number!");
579 pdf_release_obj(tmp);
580 break;
581 }
582 values[count] = pdf_number_value(tmp);
583 pdf_release_obj(tmp);
584 }
585
586 return (count + 1);
587 }
588
589 static int
590 cvr_array (pdf_obj *array, double *values, int count)
591 {
592 if (!PDF_OBJ_ARRAYTYPE(array)) {
593 WARN("mpost: Not an array!");
594 } else {
595 pdf_obj *tmp;
596
597 while (count-- > 0) {
598 tmp = pdf_get_array(array, count);
599 if (!PDF_OBJ_NUMBERTYPE(tmp)) {
600 WARN("mpost: Not a number!");
601 break;
602 }
603 values[count] = pdf_number_value(tmp);
604 }
605 }
606 if (array)
607 pdf_release_obj(array);
608
609 return (count + 1);
610 }
611
612 static int
613 is_fontdict (pdf_obj *dict)
614 {
615 pdf_obj *tmp;
616
617 if (!PDF_OBJ_DICTTYPE(dict))
618 return 0;
619
620 tmp = pdf_lookup_dict(dict, "Type");
621 if (!tmp || !PDF_OBJ_NAMETYPE(tmp) ||
622 strcmp(pdf_name_value(tmp), "Font")) {
623 return 0;
624 }
625
626 tmp = pdf_lookup_dict(dict, "FontName");
627 if (!tmp || !PDF_OBJ_NAMETYPE(tmp)) {
628 return 0;
629 }
630
631 tmp = pdf_lookup_dict(dict, "FontScale");
632 if (!tmp || !PDF_OBJ_NUMBERTYPE(tmp)) {
633 return 0;
634 }
635
636 return 1;
637 }
638
639 static int
640 do_findfont (void)
641 {
642 int error = 0;
643 pdf_obj *font_dict, *font_name;
644
645 font_name = POP_STACK();
646 if (!font_name)
647 return 1;
648 else if (PDF_OBJ_STRINGTYPE(font_name) ||
649 PDF_OBJ_NAMETYPE(font_name)) {
650 /* Do not check the existence...
651 * The reason for this is that we cannot locate PK font without
652 * font scale.
653 */
654 font_dict = pdf_new_dict();
655 pdf_add_dict(font_dict,
656 pdf_new_name("Type"), pdf_new_name("Font"));
657 if (PDF_OBJ_STRINGTYPE(font_name)) {
658 pdf_add_dict(font_dict,
659 pdf_new_name("FontName"),
660 pdf_new_name(pdf_string_value(font_name)));
661 pdf_release_obj(font_name);
662 } else {
663 pdf_add_dict(font_dict,
664 pdf_new_name("FontName"), font_name);
665 }
666 pdf_add_dict(font_dict,
667 pdf_new_name("FontScale"), pdf_new_number(1.0));
668
669 if (top_stack < PS_STACK_SIZE) {
670 stack[top_stack++] = font_dict;
671 } else {
672 WARN("PS stack overflow including MetaPost file or inline PS code");
673 pdf_release_obj(font_dict);
674 error = 1;
675 }
676 } else {
677 error = 1;
678 }
679
680 return error;
681 }
682
683 static int
684 do_scalefont (void)
685 {
686 int error = 0;
687 pdf_obj *font_dict;
688 pdf_obj *font_scale;
689 double scale;
690
691 error = pop_get_numbers(&scale, 1);
692 if (error)
693 return error;
694
695 font_dict = POP_STACK();
696 if (!font_dict)
697 error = 1;
698 else if (is_fontdict(font_dict)) {
699 font_scale = pdf_lookup_dict(font_dict, "FontScale");
700 pdf_set_number(font_scale, pdf_number_value(font_scale)*scale);
701 if (top_stack < PS_STACK_SIZE) {
702 stack[top_stack++] = font_dict;
703 } else {
704 WARN("PS stack overflow including MetaPost file or inline PS code");
705 pdf_release_obj(font_dict);
706 error = 1;
707 }
708 } else {
709 error = 1;
710 }
711
712 return error;
713 }
714
715 static int
716 do_setfont (void)
717 {
718 int error = 0;
719 char *font_name;
720 double font_scale;
721 pdf_obj *font_dict;
722
723 font_dict = POP_STACK();
724 if (!is_fontdict(font_dict))
725 error = 1;
726 else {
727 /* Subfont support prevent us from managing
728 * font in a single place...
729 */
730 font_name = pdf_name_value (pdf_lookup_dict(font_dict, "FontName"));
731 font_scale = pdf_number_value(pdf_lookup_dict(font_dict, "FontScale"));
732
733 error = mp_setfont(font_name, font_scale);
734 }
735 pdf_release_obj(font_dict);
736
737 return error;
738 }
739
740 /* Push dummy font dict onto PS stack */
741 static int
742 do_currentfont (void)
743 {
744 int error = 0;
745 struct mp_font *font;
746 pdf_obj *font_dict;
747
748 font = CURRENT_FONT();
749 if (!FONT_DEFINED(font)) {
750 WARN("Currentfont undefined...");
751 return 1;
752 } else {
753 font_dict = pdf_new_dict();
754 pdf_add_dict(font_dict,
755 pdf_new_name("Type"),
756 pdf_new_name("Font"));
757 pdf_add_dict(font_dict,
758 pdf_new_name("FontName"),
759 pdf_new_name(font->font_name));
760 pdf_add_dict(font_dict,
761 pdf_new_name("FontScale"),
762 pdf_new_number(font->pt_size));
763 if (top_stack < PS_STACK_SIZE) {
764 stack[top_stack++] = font_dict;
765 } else {
766 WARN("PS stack overflow...");
767 pdf_release_obj(font_dict);
768 error = 1;
769 }
770 }
771
772 return error;
773 }
774
775 static int
776 do_show (void)
777 {
778 struct mp_font *font;
779 pdf_coord cp;
780 pdf_obj *text_str;
781 int length;
782 unsigned char *strptr;
783 double text_width;
784
785 font = CURRENT_FONT();
786 if (!FONT_DEFINED(font)) {
787 WARN("Currentfont not set."); /* Should not be error... */
788 return 1;
789 }
790
791 pdf_dev_currentpoint(&cp);
792
793 text_str = POP_STACK();
794 if (!PDF_OBJ_STRINGTYPE(text_str)) {
795 if (text_str)
796 pdf_release_obj(text_str);
797 return 1;
798 }
799 if (font->font_id < 0) {
800 WARN("mpost: not set."); /* Should not be error... */
801 pdf_release_obj(text_str);
802 return 1;
803 }
804
805 strptr = pdf_string_value (text_str);
806 length = pdf_string_length(text_str);
807
808 if (font->tfm_id < 0) {
809 WARN("mpost: TFM not found for \"%s\".", font->font_name);
810 WARN("mpost: Text width not calculated...");
811 }
812
813 text_width = 0.0;
814 if (font->subfont_id >= 0) {
815 unsigned short uch;
816 unsigned char *ustr;
817 int i;
818
819 ustr = NEW(length * 2, unsigned char);
820 for (i = 0; i < length; i++) {
821 uch = lookup_sfd_record(font->subfont_id, strptr[i]);
822 ustr[2*i ] = uch >> 8;
823 ustr[2*i+1] = uch & 0xff;
824 if (font->tfm_id >= 0) {
825 text_width += tfm_get_width(font->tfm_id, strptr[i]);
826 }
827 }
828 text_width *= font->pt_size;
829
830 pdf_dev_set_string((spt_t)(cp.x * dev_unit_dviunit()),
831 (spt_t)(cp.y * dev_unit_dviunit()),
832 ustr, length * 2,
833 (spt_t)(text_width*dev_unit_dviunit()),
834 font->font_id);
835 RELEASE(ustr);
836 } else {
837 #define FWBASE ((double) (1<<20))
838 if (font->tfm_id >= 0) {
839 text_width = (double) tfm_string_width(font->tfm_id, strptr, length)/FWBASE;
840 text_width *= font->pt_size;
841 }
842 pdf_dev_set_string((spt_t)(cp.x * dev_unit_dviunit()),
843 (spt_t)(cp.y * dev_unit_dviunit()),
844 strptr, length,
845 (spt_t)(text_width*dev_unit_dviunit()),
846 font->font_id);
847 }
848
849 if (pdf_dev_get_font_wmode(font->font_id)) {
850 pdf_dev_rmoveto(0.0, -text_width);
851 } else {
852 pdf_dev_rmoveto(text_width, 0.0);
853 }
854
855 graphics_mode();
856 pdf_release_obj(text_str);
857
858 return 0;
859 }
860
861 static int
862 do_mpost_bind_def (const char *ps_code, double x_user, double y_user)
863 {
864 int error = 0;
865 const char *start, *end;
866
867 start = ps_code;
868 end = start + strlen(start);
869
870 error = mp_parse_body(&start, end, x_user, y_user);
871
872 return error;
873 }
874
875 static int
876 do_texfig_operator (int opcode, double x_user, double y_user)
877 {
878 static transform_info fig_p;
879 static int in_tfig = 0;
880 static int xobj_id = -1;
881 static int count = 0;
882 double values[6];
883 int error = 0;
884
885 switch (opcode) {
886 case STEXFIG:
887 error = pop_get_numbers(values, 6);
888 if (!error) {
889 double dvi2pts;
890 char resname[256];
891
892 transform_info_clear(&fig_p);
893 dvi2pts = 1.0/dev_unit_dviunit();
894
895 fig_p.width = values[0] * dvi2pts;
896 fig_p.height = values[1] * dvi2pts;
897 fig_p.bbox.llx = values[2] * dvi2pts;
898 fig_p.bbox.lly = -values[3] * dvi2pts;
899 fig_p.bbox.urx = values[4] * dvi2pts;
900 fig_p.bbox.ury = -values[5] * dvi2pts;
901 fig_p.flags |= INFO_HAS_USER_BBOX;
902
903 sprintf(resname, "__tf%d__", count);
904 xobj_id = pdf_doc_begin_grabbing(resname, fig_p.bbox.llx, fig_p.bbox.ury, &fig_p.bbox);
905
906 in_tfig = 1;
907 count++;
908 }
909 break;
910 case ETEXFIG:
911 if (!in_tfig)
912 ERROR("endTexFig without valid startTexFig!.");
913
914 pdf_doc_end_grabbing(NULL);
915 pdf_dev_put_image(xobj_id, &fig_p, x_user, y_user, NULL); /* FIXME */
916 in_tfig = 0;
917 break;
918 default:
919 error = 1;
920 }
921
922 return error;
923 }
924
925 /*
926 * buggy...
927 */
928
929 /*
930 * CTM(Current Transformation Matrix) means the transformation of User Space
931 * to Device Space coordinates. Because DVIPDFMx does not know the resolution
932 * of Device Space, we assume that the resolution is 1/1000.
933 */
934 #define DEVICE_RESOLUTION 1000
935 static int
936 ps_dev_CTM (pdf_tmatrix *M)
937 {
938 pdf_dev_currentmatrix(M);
939 M->a *= DEVICE_RESOLUTION; M->b *= DEVICE_RESOLUTION;
940 M->c *= DEVICE_RESOLUTION; M->d *= DEVICE_RESOLUTION;
941 M->e *= DEVICE_RESOLUTION; M->f *= DEVICE_RESOLUTION;
942
943 return 0;
944 }
945
946 /*
947 * Again, the only piece that needs x_user and y_user is
948 * that piece dealing with texfig.
949 */
950 static int
951 do_operator (const char *token, double x_user, double y_user)
952 {
953 int error = 0;
954 int opcode = 0;
955 double values[12];
956 pdf_obj *tmp = NULL;
957 pdf_tmatrix matrix;
958 pdf_coord cp;
959 pdf_color color;
960
961 #define PUSH(o) { \
962 if (top_stack < PS_STACK_SIZE) { \
963 stack[top_stack++] = (o); \
964 } else { \
965 WARN("PS stack overflow including MetaPost file or inline PS code"); \
966 error=1; \
967 break;\
968 } \
969 }
970
971 opcode = get_opcode(token);
972
973 switch (opcode) {
974
975 /*
976 * Arithmetic operators
977 */
978 case ADD:
979 error = pop_get_numbers(values, 2);
980 if (!error)
981 PUSH(pdf_new_number(values[0] + values[1]));
982 break;
983 case MUL:
984 error = pop_get_numbers(values, 2);
985 if (!error)
986 PUSH(pdf_new_number(values[0]*values[1]));
987 break;
988 case NEG:
989 error = pop_get_numbers(values, 1);
990 if (!error)
991 PUSH(pdf_new_number(-values[0]));
992 break;
993 case SUB:
994 error = pop_get_numbers(values, 2);
995 if (!error)
996 PUSH(pdf_new_number(values[0] - values[1]));
997 break;
998 case DIV:
999 error = pop_get_numbers(values, 2);
1000 if (!error)
1001 PUSH(pdf_new_number(values[0]/values[1]));
1002 break;
1003 case TRUNCATE: /* Round toward zero. */
1004 error = pop_get_numbers(values, 1);
1005 if (!error)
1006 PUSH(pdf_new_number(((values[0] > 0) ? floor(values[0]) : ceil(values[0]))));
1007 break;
1008
1009 /* Stack operation */
1010 case CLEAR:
1011 error = do_clear();
1012 break;
1013 case POP:
1014 tmp = POP_STACK();
1015 if (tmp)
1016 pdf_release_obj(tmp);
1017 break;
1018 case EXCH:
1019 error = do_exch();
1020 break;
1021
1022 /* Path construction */
1023 case MOVETO:
1024 error = pop_get_numbers(values, 2);
1025 if (!error)
1026 error = pdf_dev_moveto(values[0], values[1]);
1027 break;
1028 case RMOVETO:
1029 error = pop_get_numbers(values, 2);
1030 if (!error)
1031 error = pdf_dev_rmoveto(values[0], values[1]);
1032 break;
1033 case LINETO:
1034 error = pop_get_numbers(values, 2);
1035 if (!error)
1036 error = pdf_dev_lineto(values[0], values[1]);
1037 break;
1038 case RLINETO:
1039 error = pop_get_numbers(values, 2);
1040 if (!error)
1041 error = pdf_dev_rlineto(values[0], values[1]);
1042 break;
1043 case CURVETO:
1044 error = pop_get_numbers(values, 6);
1045 if (!error)
1046 error = pdf_dev_curveto(values[0], values[1],
1047 values[2], values[3],
1048 values[4], values[5]);
1049 break;
1050 case RCURVETO:
1051 error = pop_get_numbers(values, 6);
1052 if (!error)
1053 error = pdf_dev_rcurveto(values[0], values[1],
1054 values[2], values[3],
1055 values[4], values[5]);
1056 break;
1057 case CLOSEPATH:
1058 error = pdf_dev_closepath();
1059 break;
1060 case ARC:
1061 error = pop_get_numbers(values, 5);
1062 if (!error)
1063 error = pdf_dev_arc(values[0], values[1],
1064 values[2], /* rad */
1065 values[3], values[4]);
1066 break;
1067 case ARCN:
1068 error = pop_get_numbers(values, 5);
1069 if (!error)
1070 error = pdf_dev_arcn(values[0], values[1],
1071 values[2], /* rad */
1072 values[3], values[4]);
1073 break;
1074
1075 case NEWPATH:
1076 pdf_dev_newpath();
1077 break;
1078 case STROKE:
1079 /* fill rule not supported yet */
1080 pdf_dev_flushpath('S', PDF_FILL_RULE_NONZERO);
1081 break;
1082 case FILL:
1083 pdf_dev_flushpath('f', PDF_FILL_RULE_NONZERO);
1084 break;
1085
1086 case CLIP:
1087 error = pdf_dev_clip();
1088 break;
1089 case EOCLIP:
1090 error = pdf_dev_eoclip();
1091 break;
1092
1093 /* Graphics state operators: */
1094 case GSAVE:
1095 error = pdf_dev_gsave();
1096 save_font();
1097 break;
1098 case GRESTORE:
1099 error = pdf_dev_grestore();
1100 restore_font();
1101 break;
1102
1103 case CONCAT:
1104 tmp = POP_STACK();
1105 error = cvr_array(tmp, values, 6); /* This does pdf_release_obj() */
1106 tmp = NULL;
1107 if (error)
1108 WARN("Missing array before \"concat\".");
1109 else {
1110 pdf_setmatrix(&matrix,
1111 values[0], values[1],
1112 values[2], values[3],
1113 values[4], values[5]);
1114 error = pdf_dev_concat(&matrix);
1115 }
1116 break;
1117 case SCALE:
1118 error = pop_get_numbers(values, 2);
1119 if (!error) {
1120 switch (mp_cmode) {
1121 #ifndef WITHOUT_ASCII_PTEX
1122 case MP_CMODE_PTEXVERT:
1123 pdf_setmatrix(&matrix,
1124 values[1], 0.0,
1125 0.0 , values[0],
1126 0.0 , 0.0);
1127 break;
1128 #endif /* !WITHOUT_ASCII_PTEX */
1129 default:
1130 pdf_setmatrix(&matrix,
1131 values[0], 0.0,
1132 0.0 , values[1],
1133 0.0 , 0.0);
1134 break;
1135 }
1136
1137 error = pdf_dev_concat(&matrix);
1138 }
1139 break;
1140 /* Positive angle means clock-wise direction in graphicx-dvips??? */
1141 case ROTATE:
1142 error = pop_get_numbers(values, 1);
1143 if (!error) {
1144 values[0] = values[0] * M_PI / 180;
1145
1146 switch (mp_cmode) {
1147 case MP_CMODE_DVIPSK:
1148 case MP_CMODE_MPOST: /* Really? */
1149 #ifndef WITHOUT_ASCII_PTEX
1150 case MP_CMODE_PTEXVERT:
1151 #endif /* !WITHOUT_ASCII_PTEX */
1152 pdf_setmatrix(&matrix,
1153 cos(values[0]), -sin(values[0]),
1154 sin(values[0]), cos(values[0]),
1155 0.0, 0.0);
1156 break;
1157 default:
1158 pdf_setmatrix(&matrix,
1159 cos(values[0]) , sin(values[0]),
1160 -sin(values[0]), cos(values[0]),
1161 0.0, 0.0);
1162 break;
1163 }
1164 error = pdf_dev_concat(&matrix);
1165 }
1166 break;
1167 case TRANSLATE:
1168 error = pop_get_numbers(values, 2);
1169 if (!error) {
1170 pdf_setmatrix(&matrix,
1171 1.0, 0.0,
1172 0.0, 1.0,
1173 values[0], values[1]);
1174 error = pdf_dev_concat(&matrix);
1175 }
1176 break;
1177
1178 case SETDASH:
1179 error = pop_get_numbers(values, 1);
1180 if (!error) {
1181 pdf_obj *pattern, *dash;
1182 int i, num_dashes;
1183 double dash_values[PDF_DASH_SIZE_MAX];
1184 double offset;
1185
1186 offset = values[0];
1187 pattern = POP_STACK();
1188 if (!PDF_OBJ_ARRAYTYPE(pattern)) {
1189 if (pattern)
1190 pdf_release_obj(pattern);
1191 error = 1;
1192 break;
1193 }
1194 num_dashes = pdf_array_length(pattern);
1195 if (num_dashes > PDF_DASH_SIZE_MAX) {
1196 WARN("Too many dashes...");
1197 pdf_release_obj(pattern);
1198 error = 1;
1199 break;
1200 }
1201 for (i = 0;
1202 i < num_dashes && !error ; i++) {
1203 dash = pdf_get_array(pattern, i);
1204 if (!PDF_OBJ_NUMBERTYPE(dash))
1205 error = 1;
1206 else {
1207 dash_values[i] = pdf_number_value(dash);
1208 }
1209 }
1210 pdf_release_obj(pattern);
1211 if (!error) {
1212 error = pdf_dev_setdash(num_dashes, dash_values, offset);
1213 }
1214 }
1215 break;
1216 case SETLINECAP:
1217 error = pop_get_numbers(values, 1);
1218 if (!error)
1219 error = pdf_dev_setlinecap((int)values[0]);
1220 break;
1221 case SETLINEJOIN:
1222 error = pop_get_numbers(values, 1);
1223 if (!error)
1224 error = pdf_dev_setlinejoin((int)values[0]);
1225 break;
1226 case SETLINEWIDTH:
1227 error = pop_get_numbers(values, 1);
1228 if (!error)
1229 error = pdf_dev_setlinewidth(values[0]);
1230 break;
1231 case SETMITERLIMIT:
1232 error = pop_get_numbers(values, 1);
1233 if (!error)
1234 error = pdf_dev_setmiterlimit(values[0]);
1235 break;
1236
1237 case SETCMYKCOLOR:
1238 error = pop_get_numbers(values, 4);
1239 /* Not handled properly */
1240 if (!error) {
1241 pdf_color_cmykcolor(&color,
1242 values[0], values[1],
1243 values[2], values[3]);
1244 pdf_dev_set_strokingcolor(&color);
1245 pdf_dev_set_nonstrokingcolor(&color);
1246 }
1247 break;
1248 case SETGRAY:
1249 /* Not handled properly */
1250 error = pop_get_numbers(values, 1);
1251 if (!error) {
1252 pdf_color_graycolor(&color, values[0]);
1253 pdf_dev_set_strokingcolor(&color);
1254 pdf_dev_set_nonstrokingcolor(&color);
1255 }
1256 break;
1257 case SETRGBCOLOR:
1258 error = pop_get_numbers(values, 3);
1259 if (!error) {
1260 pdf_color_rgbcolor(&color,
1261 values[0], values[1], values[2]);
1262 pdf_dev_set_strokingcolor(&color);
1263 pdf_dev_set_nonstrokingcolor(&color);
1264 }
1265 break;
1266
1267 case SHOWPAGE: /* Let's ignore this for now */
1268 break;
1269
1270 case CURRENTPOINT:
1271 error = pdf_dev_currentpoint(&cp);
1272 if (!error) {
1273 PUSH(pdf_new_number(cp.x));
1274 PUSH(pdf_new_number(cp.y));
1275 }
1276 break;
1277
1278 case DTRANSFORM:
1279 {
1280 int has_matrix = 0;
1281
1282 tmp = POP_STACK();
1283 if (PDF_OBJ_ARRAYTYPE(tmp)) {
1284 error = cvr_array(tmp, values, 6); /* This does pdf_release_obj() */
1285 tmp = NULL;
1286 if (error)
1287 break;
1288 pdf_setmatrix(&matrix,
1289 values[0], values[1],
1290 values[2], values[3],
1291 values[4], values[5]);
1292 tmp = POP_STACK();
1293 has_matrix = 1;
1294 }
1295
1296 if (!PDF_OBJ_NUMBERTYPE(tmp)) {
1297 error = 1;
1298 break;
1299 }
1300 cp.y = pdf_number_value(tmp);
1301 pdf_release_obj(tmp);
1302
1303 tmp = POP_STACK();
1304 if (!PDF_OBJ_NUMBERTYPE(tmp)) {
1305 error = 1;
1306 break;
1307 }
1308 cp.x = pdf_number_value(tmp);
1309 pdf_release_obj(tmp);
1310
1311 if (!has_matrix) {
1312 ps_dev_CTM(&matrix); /* Here, we need real PostScript CTM */
1313 }
1314 pdf_dev_dtransform(&cp, &matrix);
1315 PUSH(pdf_new_number(cp.x));
1316 PUSH(pdf_new_number(cp.y));
1317 }
1318 break;
1319
1320 case IDTRANSFORM:
1321 {
1322 int has_matrix = 0;
1323
1324 tmp = POP_STACK();
1325 if (PDF_OBJ_ARRAYTYPE(tmp)) {
1326 error = cvr_array(tmp, values, 6); /* This does pdf_release_obj() */
1327 tmp = NULL;
1328 if (error)
1329 break;
1330 pdf_setmatrix(&matrix,
1331 values[0], values[1],
1332 values[2], values[3],
1333 values[4], values[5]);
1334 tmp = POP_STACK();
1335 has_matrix = 1;
1336 }
1337
1338 if (!PDF_OBJ_NUMBERTYPE(tmp)) {
1339 error = 1;
1340 break;
1341 }
1342 cp.y = pdf_number_value(tmp);
1343 pdf_release_obj(tmp);
1344
1345 tmp = POP_STACK();
1346 if (!PDF_OBJ_NUMBERTYPE(tmp)) {
1347 error = 1;
1348 break;
1349 }
1350 cp.x = pdf_number_value(tmp);
1351 pdf_release_obj(tmp);
1352
1353 if (!has_matrix) {
1354 ps_dev_CTM(&matrix); /* Here, we need real PostScript CTM */
1355 }
1356 pdf_dev_idtransform(&cp, &matrix);
1357 PUSH(pdf_new_number(cp.x));
1358 PUSH(pdf_new_number(cp.y));
1359 break;
1360 }
1361
1362 case FINDFONT:
1363 error = do_findfont();
1364 break;
1365 case SCALEFONT:
1366 error = do_scalefont();
1367 break;
1368 case SETFONT:
1369 error = do_setfont();
1370 break;
1371 case CURRENTFONT:
1372 error = do_currentfont();
1373 break;
1374
1375 case SHOW:
1376 error = do_show();
1377 break;
1378
1379 case STRINGWIDTH:
1380 error = 1;
1381 break;
1382
1383 /* Extensions */
1384 case FSHOW:
1385 error = do_mpost_bind_def("exch findfont exch scalefont setfont show", x_user, y_user);
1386 break;
1387 case STEXFIG:
1388 case ETEXFIG:
1389 error = do_texfig_operator(opcode, x_user, y_user);
1390 break;
1391 case HLW:
1392 error = do_mpost_bind_def("0 dtransform exch truncate exch idtransform pop setlinewidth", x_user, y_user);
1393 break;
1394 case VLW:
1395 error = do_mpost_bind_def("0 exch dtransform truncate idtransform setlinewidth pop", x_user, y_user);
1396 break;
1397 case RD:
1398 error = do_mpost_bind_def("[] 0 setdash", x_user, y_user);
1399 break;
1400 case B:
1401 error = do_mpost_bind_def("gsave fill grestore", x_user, y_user);
1402 break;
1403
1404 case DEF:
1405 tmp = POP_STACK();
1406 tmp = POP_STACK();
1407 /* do nothing; not implemented yet */
1408 break;
1409
1410 default:
1411 if (is_fontname(token)) {
1412 PUSH(pdf_new_name(token));
1413 } else {
1414 WARN("Unknown token \"%s\"", token);
1415 error = 1;
1416 }
1417 break;
1418 }
1419
1420 return error;
1421 }
1422
1423 /*
1424 * The only sections that need to know x_user and y _user are those
1425 * dealing with texfig.
1426 */
1427 static int
1428 mp_parse_body (const char **start, const char *end, double x_user, double y_user)
1429 {
1430 char *token;
1431 pdf_obj *obj;
1432 int error = 0;
1433
1434 skip_white(start, end);
1435 while (*start < end && !error) {
1436 if (isdigit((unsigned char)**start) ||
1437 (*start < end - 1 &&
1438 (**start == '+' || **start == '-' || **start == '.' ))) {
1439 double value;
1440 char *next;
1441
1442 value = strtod(*start, &next);
1443 if (next < end && !strchr("<([{/%", *next) && !isspace((unsigned char)*next)) {
1444 WARN("Unkown PostScript operator.");
1445 dump(*start, next);
1446 error = 1;
1447 } else {
1448 PUSH(pdf_new_number(value));
1449 *start = next;
1450 }
1451 /*
1452 * PDF parser can't handle PS operator inside arrays.
1453 * This shouldn't use parse_pdf_array().
1454 */
1455 } else if (**start == '[' &&
1456 (obj = parse_pdf_array(start, end, NULL))) {
1457 PUSH(obj);
1458 /* This cannot handle ASCII85 string. */
1459 } else if (*start < end - 1 &&
1460 (**start == '<' && *(*start+1) == '<') &&
1461 (obj = parse_pdf_dict(start, end, NULL))) {
1462 PUSH(obj);
1463 } else if ((**start == '(' || **start == '<') &&
1464 (obj = parse_pdf_string (start, end))) {
1465 PUSH(obj);
1466 } else if (**start == '/' &&
1467 (obj = parse_pdf_name(start, end))) {
1468 PUSH(obj);
1469 } else {
1470 token = parse_ident(start, end);
1471 if (!token)
1472 error = 1;
1473 else {
1474 error = do_operator(token, x_user, y_user);
1475 RELEASE(token);
1476 }
1477 }
1478 skip_white(start, end);
1479 }
1480
1481 return error;
1482 }
1483
1484 void
1485 mps_eop_cleanup (void)
1486 {
1487 clear_fonts();
1488 do_clear();
1489 }
1490
1491 int
1492 mps_stack_depth (void)
1493 {
1494 return top_stack;
1495 }
1496
1497 int
1498 mps_exec_inline (const char **p, const char *endptr,
1499 double x_user, double y_user)
1500 {
1501 int error;
1502 int dirmode, autorotate;
1503
1504 /* Compatibility for dvipsk. */
1505 dirmode = pdf_dev_get_dirmode();
1506 if (dirmode) {
1507 mp_cmode = MP_CMODE_PTEXVERT;
1508 } else {
1509 mp_cmode = MP_CMODE_DVIPSK;
1510 }
1511
1512 autorotate = pdf_dev_get_param(PDF_DEV_PARAM_AUTOROTATE);
1513 pdf_dev_set_param(PDF_DEV_PARAM_AUTOROTATE, 0);
1514 //pdf_color_push(); /* ... */
1515
1516 /* Comment in dvipdfm:
1517 * Remember that x_user and y_user are off by 0.02 %
1518 */
1519 pdf_dev_moveto(x_user, y_user);
1520 error = mp_parse_body(p, endptr, x_user, y_user);
1521
1522 //pdf_color_pop(); /* ... */
1523 pdf_dev_set_param(PDF_DEV_PARAM_AUTOROTATE, autorotate);
1524 pdf_dev_set_dirmode(dirmode);
1525
1526 return error;
1527 }
1528
1529 /* mp inclusion is a bit of a hack. The routine
1530 * starts a form at the lower left corner of
1531 * the page and then calls begin_form_xobj telling
1532 * it to record the image drawn there and bundle it
1533 * up in an xojbect. This allows us to use the coordinates
1534 * in the MP file directly. This appears to be the
1535 * easiest way to be able to use the pdf_dev_set_string()
1536 * command (with its scaled and extended fonts) without
1537 * getting all confused about the coordinate system.
1538 * After the xobject is created, the whole thing can
1539 * be scaled any way the user wants
1540 */
1541
1542 /* Should implement save and restore. */
1543 int
1544 mps_include_page (const char *ident, FILE *fp)
1545 {
1546 int form_id;
1547 xform_info info;
1548 int st_depth, gs_depth;
1549 char *buffer;
1550 const char *p, *endptr;
1551 int length, nb_read;
1552 int dirmode, autorotate, error;
1553
1554 rewind(fp);
1555
1556 length = file_size(fp);
1557 if (length < 1) {
1558 WARN("Can't read any byte in the MPS file.");
1559 return -1;
1560 }
1561
1562 buffer = NEW(length + 1, char);
1563 buffer[length] = '\0';
1564 p = buffer;
1565 endptr = p + length;
1566
1567 while (length > 0) {
1568 nb_read = fread(buffer, sizeof(char), length, fp);
1569 if (nb_read < 0) {
1570 RELEASE(buffer);
1571 WARN("Reading file failed...");
1572 return -1;
1573 }
1574 length -= nb_read;
1575 }
1576
1577 error = mps_scan_bbox(&p, endptr, &(info.bbox));
1578 if (error) {
1579 WARN("Error occured while scanning MetaPost file headers: Could not find BoundingBox.");
1580 RELEASE(buffer);
1581 return -1;
1582 }
1583 skip_prolog(&p, endptr);
1584
1585 dirmode = pdf_dev_get_dirmode();
1586 autorotate = pdf_dev_get_param(PDF_DEV_PARAM_AUTOROTATE);
1587 pdf_dev_set_param(PDF_DEV_PARAM_AUTOROTATE, 0);
1588 //pdf_color_push();
1589
1590 form_id = pdf_doc_begin_grabbing(ident, Xorigin, Yorigin, &(info.bbox));
1591
1592 mp_cmode = MP_CMODE_MPOST;
1593 gs_depth = pdf_dev_current_depth();
1594 st_depth = mps_stack_depth();
1595 /* At this point the gstate must be initialized, since it starts a new
1596 * XObject. Note that it increase gs_depth by 1. */
1597 pdf_dev_push_gstate();
1598
1599 error = mp_parse_body(&p, endptr, 0.0, 0.0);
1600 RELEASE(buffer);
1601
1602 if (error) {
1603 WARN("Errors occured while interpreting MPS file.");
1604 /* WARN("Leaving garbage in output PDF file."); */
1605 form_id = -1;
1606 }
1607
1608 /* It's time to pop the new gstate above. */
1609 pdf_dev_pop_gstate();
1610 mps_stack_clear_to (st_depth);
1611 pdf_dev_grestore_to(gs_depth);
1612
1613 pdf_doc_end_grabbing(NULL);
1614
1615 //pdf_color_pop();
1616 pdf_dev_set_param(PDF_DEV_PARAM_AUTOROTATE, autorotate);
1617 pdf_dev_set_dirmode(dirmode);
1618
1619 return form_id;
1620 }
1621
1622 int
1623 mps_do_page (FILE *image_file)
1624 {
1625 int error = 0;
1626 pdf_rect bbox;
1627 char *buffer;
1628 const char *start, *end;
1629 int size;
1630 int dir_mode;
1631
1632 rewind(image_file);
1633 if ((size = file_size(image_file)) == 0) {
1634 WARN("Can't read any byte in the MPS file.");
1635 return -1;
1636 }
1637
1638 buffer = NEW(size+1, char);
1639 fread(buffer, sizeof(char), size, image_file);
1640 buffer[size] = 0;
1641 start = buffer;
1642 end = buffer + size;
1643
1644 error = mps_scan_bbox(&start, end, &bbox);
1645 if (error) {
1646 WARN("Error occured while scanning MetaPost file headers: Could not find BoundingBox.");
1647 RELEASE(buffer);
1648 return -1;
1649 }
1650
1651 mp_cmode = MP_CMODE_MPOST;
1652
1653 pdf_doc_begin_page (1.0, -Xorigin, -Yorigin); /* scale, xorig, yorig */
1654 pdf_doc_set_mediabox(pdf_doc_current_page_number(), &bbox);
1655
1656 dir_mode = pdf_dev_get_dirmode();
1657 pdf_dev_set_autorotate(0);
1658
1659 skip_prolog(&start, end);
1660
1661 error = mp_parse_body(&start, end, 0.0, 0.0);
1662
1663 if (error) {
1664 WARN("Errors occured while interpreting MetaPost file.");
1665 }
1666
1667 pdf_dev_set_autorotate(1);
1668 pdf_dev_set_dirmode(dir_mode);
1669
1670 pdf_doc_end_page();
1671
1672 RELEASE(buffer);
1673
1674 /*
1675 * The reason why we don't return XObject itself is
1676 * PDF inclusion may not be made so.
1677 */
1678 return (error ? -1 : 0);
1679 }