-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpost.c
1679 lines (1450 loc) · 37.9 KB
/
mpost.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata,
the dvipdfmx project team.
Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctype.h>
#include <string.h>
#include <math.h>
#include "system.h"
#include "mem.h"
#include "error.h"
#include "mfileio.h"
#include "numbers.h"
#include "dpxconf.h"
#include "tfm.h"
#include "pdfobj.h"
#include "pdfparse.h"
#include "pdfdev.h"
#include "pdfdoc.h"
#include "pdfcolor.h"
#include "pdfdraw.h"
#include "fontmap.h"
#include "subfont.h"
#include "pdfximage.h"
#include "mpost.h"
#include "dvipdfmx.h"
/*
* Define the origin as (llx, lly) in order to
* match the new xetex.def and dvipdfmx.def
*/
static double Xorigin, Yorigin;
static int translate_origin = 0;
void
mps_set_translate_origin (int v) {
translate_origin = v;
}
/*
* In PDF, current path is not a part of graphics state parameter.
* Hence, current path is not saved by the "q" operator and is not
* recovered by the "Q" operator. This means that the following PS
* code
*
* <path construction> gsave <path painting> grestore ...
*
* can't be translated to PDF code
*
* <path construction> q <path painting> Q ...
*
* . Only clipping path (which is graphics state parameter in PDF
* too) is treated in the same way. So, we write clipping path
* immediately and forget about it but remember current path.
*/
static int mp_parse_body (const char **start, const char *end, double x_user, double y_user);
static struct mp_font
{
char *font_name;
int font_id;
int tfm_id; /* Used for text width calculation */
int subfont_id;
double pt_size;
} font_stack[PDF_GSAVE_MAX] = {
{NULL, -1, -1, -1, 0}
};
static int currentfont = 0;
#define CURRENT_FONT() ((currentfont < 0) ? NULL : &font_stack[currentfont])
#define FONT_DEFINED(f) ((f) && (f)->font_name && ((f)->font_id >= 0))
static void
clear_mp_font_struct (struct mp_font *font)
{
ASSERT(font);
if (font->font_name)
RELEASE(font->font_name);
font->font_name = NULL;
font->font_id = -1;
font->tfm_id = -1;
font->subfont_id = -1;
font->pt_size = 0.0;
}
/* Compatibility */
#define MP_CMODE_MPOST 0
#define MP_CMODE_DVIPSK 1
#define MP_CMODE_PTEXVERT 2
static int mp_cmode = MP_CMODE_MPOST;
static int
mp_setfont (const char *font_name, double pt_size)
{
const char *name = font_name;
struct mp_font *font;
int subfont_id = -1;
fontmap_rec *mrec;
font = CURRENT_FONT();
mrec = pdf_lookup_fontmap_record(font_name);
if (mrec && mrec->charmap.sfd_name && mrec->charmap.subfont_id) {
subfont_id = sfd_load_record(mrec->charmap.sfd_name, mrec->charmap.subfont_id);
}
/* See comments in dvi_locate_font() in dvi.c. */
if (mrec && mrec->map_name) {
name = mrec->map_name;
} else {
name = font_name;
}
if (font->font_name)
RELEASE(font->font_name);
font->font_name = NEW(strlen(font_name) + 1, char);
strcpy(font->font_name, font_name);
font->subfont_id = subfont_id;
font->pt_size = pt_size;
font->tfm_id = tfm_open(font_name, 0); /* Need not exist in MP mode */
font->font_id = pdf_dev_locate_font(name,
(spt_t) (pt_size * dev_unit_dviunit()));
if (font->font_id < 0) {
ERROR("MPOST: No physical font assigned for \"%s\".", font_name);
return 1;
}
return 0;
}
static void
save_font (void)
{
struct mp_font *current, *next;
current = &font_stack[currentfont++];
next = &font_stack[currentfont ];
if (FONT_DEFINED(current)) {
next->font_name = NEW(strlen(current->font_name)+1, char);
strcpy(next->font_name, current->font_name);
next->font_id = current->font_id;
next->pt_size = current->pt_size;
next->subfont_id = current->subfont_id;
next->tfm_id = current->tfm_id;
} else {
next->font_name = NULL;
next->font_id = -1;
next->pt_size = 0.0;
next->subfont_id = -1;
next->tfm_id = -1;
}
}
static void
restore_font (void)
{
struct mp_font *current;
current = CURRENT_FONT();
if (current) {
clear_mp_font_struct(current);
}
currentfont--;
}
static void
clear_fonts (void)
{
while (currentfont > 0) {
clear_mp_font_struct(&font_stack[currentfont]);
currentfont--;
}
}
static int
is_fontname (const char *token)
{
fontmap_rec *mrec;
mrec = pdf_lookup_fontmap_record(token);
if (mrec)
return 1;
return tfm_exists(token);
}
int
mps_scan_bbox (const char **pp, const char *endptr, pdf_rect *bbox)
{
char *number;
double values[4];
int i;
/* skip_white() skips lines starting '%'... */
while (*pp < endptr && isspace((unsigned char)**pp))
(*pp)++;
/* Scan for bounding box record */
while (*pp < endptr && **pp == '%') {
if (*pp + 14 < endptr &&
!strncmp(*pp, "%%BoundingBox:", 14)) {
*pp += 14;
for (i = 0; i < 4; i++) {
skip_white(pp, endptr);
number = parse_number(pp, endptr);
if (!number) {
break;
}
values[i] = atof(number);
RELEASE(number);
}
if (i < 4) {
return -1;
} else {
/* The new xetex.def and dvipdfmx.def require bbox->llx = bbox->lly = 0. */
if (translate_origin) {
bbox->llx = 0;
bbox->lly = 0;
bbox->urx = values[2] - values[0];
bbox->ury = values[3] - values[1];
Xorigin = (double)values[0];
Yorigin = (double)values[1];
} else {
bbox->llx = values[0];
bbox->lly = values[1];
bbox->urx = values[2];
bbox->ury = values[3];
Xorigin = 0.0;
Yorigin = 0.0;
}
return 0;
}
}
pdfparse_skip_line (pp, endptr);
while (*pp < endptr && isspace((unsigned char)**pp))
(*pp)++;
}
return -1;
}
static void
skip_prolog (const char **start, const char *end)
{
int found_prolog = 0;
const char *save;
save = *start;
while (*start < end) {
if (**start != '%')
skip_white(start, end);
if (*start >= end)
break;
if (!strncmp(*start, "%%EndProlog", 11)) {
found_prolog = 1;
pdfparse_skip_line(start, end);
break;
} else if (!strncmp(*start, "%%Page:", 7)) {
pdfparse_skip_line(start, end);
break;
}
pdfparse_skip_line(start, end);
}
if (!found_prolog) {
*start = save;
}
return;
}
/* PostScript Operators */
/* Acoid conflict with SET... from <wingdi.h>. */
#undef SETLINECAP
#undef SETLINEJOIN
#undef SETMITERLIMIT
#define ADD 1
#define SUB 2
#define MUL 3
#define DIV 4
#define NEG 5
#define TRUNCATE 6
#define CLEAR 10
#define EXCH 11
#define POP 12
#define NEWPATH 31
#define CLOSEPATH 32
#define MOVETO 33
#define RMOVETO 34
#define CURVETO 35
#define RCURVETO 36
#define LINETO 37
#define RLINETO 38
#define ARC 39
#define ARCN 40
#define FILL 41
#define STROKE 42
#define SHOW 43
#define CLIP 44
#define EOCLIP 45
#define SHOWPAGE 49
#define GSAVE 50
#define GRESTORE 51
#define CONCAT 52
#define SCALE 53
#define TRANSLATE 54
#define ROTATE 55
#define SETLINEWIDTH 60
#define SETDASH 61
#define SETLINECAP 62
#define SETLINEJOIN 63
#define SETMITERLIMIT 64
#define SETGRAY 70
#define SETRGBCOLOR 71
#define SETCMYKCOLOR 72
#define CURRENTPOINT 80
#define IDTRANSFORM 81
#define DTRANSFORM 82
#define FINDFONT 201
#define SCALEFONT 202
#define SETFONT 203
#define CURRENTFONT 204
#define STRINGWIDTH 210
#define DEF 999
#define FSHOW 1001
#define STEXFIG 1002
#define ETEXFIG 1003
#define HLW 1004
#define VLW 1005
#define RD 1006
#define B 1007
static struct operators
{
const char *token;
int opcode;
} ps_operators[] = {
{"add", ADD},
{"mul", MUL},
{"div", DIV},
{"neg", NEG},
{"sub", SUB},
{"truncate", TRUNCATE},
{"clear", CLEAR},
{"exch", EXCH},
{"pop", POP},
{"clip", CLIP},
{"eoclip", EOCLIP},
{"closepath", CLOSEPATH},
{"concat", CONCAT},
{"newpath", NEWPATH},
{"moveto", MOVETO},
{"rmoveto", RMOVETO},
{"lineto", LINETO},
{"rlineto", RLINETO},
{"curveto", CURVETO},
{"rcurveto", RCURVETO},
{"arc", ARC},
{"arcn", ARCN},
{"stroke", STROKE},
{"fill", FILL},
{"show", SHOW},
{"showpage", SHOWPAGE},
{"gsave", GSAVE},
{"grestore", GRESTORE},
{"translate", TRANSLATE},
{"rotate", ROTATE},
{"scale", SCALE},
{"setlinecap", SETLINECAP},
{"setlinejoin", SETLINEJOIN},
{"setlinewidth", SETLINEWIDTH},
{"setmiterlimit", SETMITERLIMIT},
{"setdash", SETDASH},
{"setgray", SETGRAY},
{"setrgbcolor", SETRGBCOLOR},
{"setcmykcolor", SETCMYKCOLOR},
{"currentpoint", CURRENTPOINT}, /* This is here for rotate support
in graphics package-not MP support */
{"dtransform", DTRANSFORM},
{"idtransform", IDTRANSFORM},
{"findfont", FINDFONT},
{"scalefont", SCALEFONT},
{"setfont", SETFONT},
{"currentfont", CURRENTFONT},
{"stringwidth", STRINGWIDTH},
{"def", DEF} /* not implemented yet; just work with mptopdf */
};
static struct operators mps_operators[] = {
{"fshow", FSHOW}, /* exch findfont exch scalefont setfont show */
{"startTexFig", STEXFIG},
{"endTexFig", ETEXFIG},
{"hlw", HLW}, /* 0 dtransform exch truncate exch idtransform pop setlinewidth */
{"vlw", VLW}, /* 0 exch dtransform truncate idtransform pop setlinewidth pop */
{"l", LINETO},
{"r", RLINETO},
{"c", CURVETO},
{"m", MOVETO},
{"p", CLOSEPATH},
{"n", NEWPATH},
{"C", SETCMYKCOLOR},
{"G", SETGRAY},
{"R", SETRGBCOLOR},
{"lj", SETLINEJOIN},
{"ml", SETMITERLIMIT},
{"lc", SETLINECAP},
{"S", STROKE},
{"F", FILL},
{"q", GSAVE},
{"Q", GRESTORE},
{"s", SCALE},
{"t", CONCAT},
{"sd", SETDASH},
{"rd", RD}, /* [] 0 setdash */
{"P", SHOWPAGE},
{"B", B}, /* gsave fill grestore */
{"W", CLIP}
};
#define NUM_PS_OPERATORS (sizeof(ps_operators)/sizeof(ps_operators[0]))
#define NUM_MPS_OPERATORS (sizeof(mps_operators)/sizeof(mps_operators[0]))
static int
get_opcode (const char *token)
{
int i;
for (i = 0; i < NUM_PS_OPERATORS; i++) {
if (!strcmp(token, ps_operators[i].token)) {
return ps_operators[i].opcode;
}
}
for (i = 0; i < NUM_MPS_OPERATORS; i++) {
if (!strcmp(token, mps_operators[i].token)) {
return mps_operators[i].opcode;
}
}
return -1;
}
#define PS_STACK_SIZE 1024
static pdf_obj *stack[PS_STACK_SIZE];
static unsigned top_stack = 0;
#define POP_STACK() ((top_stack > 0) ? stack[--top_stack] : NULL)
#define PUSH_STACK(o,e) { \
if (top_stack < PS_STACK_SIZE) { \
stack[top_stack++] = (o); \
} else { \
WARN("PS stack overflow including MetaPost file or inline PS code"); \
*(e) = 1; \
} \
}
static int
do_exch (void)
{
pdf_obj *tmp;
if (top_stack < 2)
return -1;
tmp = stack[top_stack-1];
stack[top_stack-1] = stack[top_stack-2];
stack[top_stack-2] = tmp;
return 0;
}
static int
do_clear (void)
{
pdf_obj *tmp;
while (top_stack > 0) {
tmp = POP_STACK();
if (tmp)
pdf_release_obj(tmp);
}
return 0;
}
/* This should be set_bottom and clear (or
* have independent stack) to ensure stack
* depth do not go below real stack bottom.
*/
static void
mps_stack_clear_to (int depth)
{
pdf_obj *tmp;
while (top_stack > depth) {
tmp = POP_STACK();
if (tmp)
pdf_release_obj(tmp);
}
return;
}
static int
pop_get_numbers (double *values, int count)
{
pdf_obj *tmp;
while (count-- > 0) {
tmp = POP_STACK();
if (!tmp) {
WARN("mpost: Stack underflow.");
break;
} else if (!PDF_OBJ_NUMBERTYPE(tmp)) {
WARN("mpost: Not a number!");
pdf_release_obj(tmp);
break;
}
values[count] = pdf_number_value(tmp);
pdf_release_obj(tmp);
}
return (count + 1);
}
static int
cvr_array (pdf_obj *array, double *values, int count)
{
if (!PDF_OBJ_ARRAYTYPE(array)) {
WARN("mpost: Not an array!");
} else {
pdf_obj *tmp;
while (count-- > 0) {
tmp = pdf_get_array(array, count);
if (!PDF_OBJ_NUMBERTYPE(tmp)) {
WARN("mpost: Not a number!");
break;
}
values[count] = pdf_number_value(tmp);
}
}
if (array)
pdf_release_obj(array);
return (count + 1);
}
static int
is_fontdict (pdf_obj *dict)
{
pdf_obj *tmp;
if (!PDF_OBJ_DICTTYPE(dict))
return 0;
tmp = pdf_lookup_dict(dict, "Type");
if (!tmp || !PDF_OBJ_NAMETYPE(tmp) ||
strcmp(pdf_name_value(tmp), "Font")) {
return 0;
}
tmp = pdf_lookup_dict(dict, "FontName");
if (!tmp || !PDF_OBJ_NAMETYPE(tmp)) {
return 0;
}
tmp = pdf_lookup_dict(dict, "FontScale");
if (!tmp || !PDF_OBJ_NUMBERTYPE(tmp)) {
return 0;
}
return 1;
}
static int
do_findfont (void)
{
int error = 0;
pdf_obj *font_dict, *font_name;
font_name = POP_STACK();
if (!font_name)
return 1;
else if (PDF_OBJ_STRINGTYPE(font_name) ||
PDF_OBJ_NAMETYPE(font_name)) {
/* Do not check the existence...
* The reason for this is that we cannot locate PK font without
* font scale.
*/
font_dict = pdf_new_dict();
pdf_add_dict(font_dict,
pdf_new_name("Type"), pdf_new_name("Font"));
if (PDF_OBJ_STRINGTYPE(font_name)) {
pdf_add_dict(font_dict,
pdf_new_name("FontName"),
pdf_new_name(pdf_string_value(font_name)));
pdf_release_obj(font_name);
} else {
pdf_add_dict(font_dict,
pdf_new_name("FontName"), font_name);
}
pdf_add_dict(font_dict,
pdf_new_name("FontScale"), pdf_new_number(1.0));
if (top_stack < PS_STACK_SIZE) {
stack[top_stack++] = font_dict;
} else {
WARN("PS stack overflow including MetaPost file or inline PS code");
pdf_release_obj(font_dict);
error = 1;
}
} else {
error = 1;
}
return error;
}
static int
do_scalefont (void)
{
int error = 0;
pdf_obj *font_dict;
pdf_obj *font_scale;
double scale;
error = pop_get_numbers(&scale, 1);
if (error)
return error;
font_dict = POP_STACK();
if (!font_dict)
error = 1;
else if (is_fontdict(font_dict)) {
font_scale = pdf_lookup_dict(font_dict, "FontScale");
pdf_set_number(font_scale, pdf_number_value(font_scale)*scale);
if (top_stack < PS_STACK_SIZE) {
stack[top_stack++] = font_dict;
} else {
WARN("PS stack overflow including MetaPost file or inline PS code");
pdf_release_obj(font_dict);
error = 1;
}
} else {
error = 1;
}
return error;
}
static int
do_setfont (void)
{
int error = 0;
char *font_name;
double font_scale;
pdf_obj *font_dict;
font_dict = POP_STACK();
if (!is_fontdict(font_dict))
error = 1;
else {
/* Subfont support prevent us from managing
* font in a single place...
*/
font_name = pdf_name_value (pdf_lookup_dict(font_dict, "FontName"));
font_scale = pdf_number_value(pdf_lookup_dict(font_dict, "FontScale"));
error = mp_setfont(font_name, font_scale);
}
pdf_release_obj(font_dict);
return error;
}
/* Push dummy font dict onto PS stack */
static int
do_currentfont (void)
{
int error = 0;
struct mp_font *font;
pdf_obj *font_dict;
font = CURRENT_FONT();
if (!FONT_DEFINED(font)) {
WARN("Currentfont undefined...");
return 1;
} else {
font_dict = pdf_new_dict();
pdf_add_dict(font_dict,
pdf_new_name("Type"),
pdf_new_name("Font"));
pdf_add_dict(font_dict,
pdf_new_name("FontName"),
pdf_new_name(font->font_name));
pdf_add_dict(font_dict,
pdf_new_name("FontScale"),
pdf_new_number(font->pt_size));
if (top_stack < PS_STACK_SIZE) {
stack[top_stack++] = font_dict;
} else {
WARN("PS stack overflow...");
pdf_release_obj(font_dict);
error = 1;
}
}
return error;
}
static int
do_show (void)
{
struct mp_font *font;
pdf_coord cp;
pdf_obj *text_str;
int length;
unsigned char *strptr;
double text_width;
font = CURRENT_FONT();
if (!FONT_DEFINED(font)) {
WARN("Currentfont not set."); /* Should not be error... */
return 1;
}
pdf_dev_currentpoint(&cp);
text_str = POP_STACK();
if (!PDF_OBJ_STRINGTYPE(text_str)) {
if (text_str)
pdf_release_obj(text_str);
return 1;
}
if (font->font_id < 0) {
WARN("mpost: not set."); /* Should not be error... */
pdf_release_obj(text_str);
return 1;
}
strptr = pdf_string_value (text_str);
length = pdf_string_length(text_str);
if (font->tfm_id < 0) {
WARN("mpost: TFM not found for \"%s\".", font->font_name);
WARN("mpost: Text width not calculated...");
}
text_width = 0.0;
if (font->subfont_id >= 0) {
unsigned short uch;
unsigned char *ustr;
int i;
ustr = NEW(length * 2, unsigned char);
for (i = 0; i < length; i++) {
uch = lookup_sfd_record(font->subfont_id, strptr[i]);
ustr[2*i ] = uch >> 8;
ustr[2*i+1] = uch & 0xff;
if (font->tfm_id >= 0) {
text_width += tfm_get_width(font->tfm_id, strptr[i]);
}
}
text_width *= font->pt_size;
pdf_dev_set_string((spt_t)(cp.x * dev_unit_dviunit()),
(spt_t)(cp.y * dev_unit_dviunit()),
ustr, length * 2,
(spt_t)(text_width*dev_unit_dviunit()),
font->font_id);
RELEASE(ustr);
} else {
#define FWBASE ((double) (1<<20))
if (font->tfm_id >= 0) {
text_width = (double) tfm_string_width(font->tfm_id, strptr, length)/FWBASE;
text_width *= font->pt_size;
}
pdf_dev_set_string((spt_t)(cp.x * dev_unit_dviunit()),
(spt_t)(cp.y * dev_unit_dviunit()),
strptr, length,
(spt_t)(text_width*dev_unit_dviunit()),
font->font_id);
}
if (pdf_dev_get_font_wmode(font->font_id)) {
pdf_dev_rmoveto(0.0, -text_width);
} else {
pdf_dev_rmoveto(text_width, 0.0);
}
graphics_mode();
pdf_release_obj(text_str);
return 0;
}
static int
do_mpost_bind_def (const char *ps_code, double x_user, double y_user)
{
int error = 0;
const char *start, *end;
start = ps_code;
end = start + strlen(start);
error = mp_parse_body(&start, end, x_user, y_user);
return error;
}
static int
do_texfig_operator (int opcode, double x_user, double y_user)
{
static transform_info fig_p;
static int in_tfig = 0;
static int xobj_id = -1;
static int count = 0;
double values[6];
int error = 0;
switch (opcode) {
case STEXFIG:
error = pop_get_numbers(values, 6);
if (!error) {
double dvi2pts;
char resname[256];
transform_info_clear(&fig_p);
dvi2pts = 1.0/dev_unit_dviunit();
fig_p.width = values[0] * dvi2pts;
fig_p.height = values[1] * dvi2pts;
fig_p.bbox.llx = values[2] * dvi2pts;
fig_p.bbox.lly = -values[3] * dvi2pts;
fig_p.bbox.urx = values[4] * dvi2pts;
fig_p.bbox.ury = -values[5] * dvi2pts;
fig_p.flags |= INFO_HAS_USER_BBOX;
sprintf(resname, "__tf%d__", count);
xobj_id = pdf_doc_begin_grabbing(resname, fig_p.bbox.llx, fig_p.bbox.ury, &fig_p.bbox);
in_tfig = 1;
count++;
}
break;
case ETEXFIG:
if (!in_tfig)
ERROR("endTexFig without valid startTexFig!.");
pdf_doc_end_grabbing(NULL);
pdf_dev_put_image(xobj_id, &fig_p, x_user, y_user, NULL); /* FIXME */
in_tfig = 0;
break;
default:
error = 1;
}
return error;
}
/*
* buggy...
*/
/*
* CTM(Current Transformation Matrix) means the transformation of User Space
* to Device Space coordinates. Because DVIPDFMx does not know the resolution
* of Device Space, we assume that the resolution is 1/1000.
*/
#define DEVICE_RESOLUTION 1000
static int
ps_dev_CTM (pdf_tmatrix *M)
{
pdf_dev_currentmatrix(M);
M->a *= DEVICE_RESOLUTION; M->b *= DEVICE_RESOLUTION;
M->c *= DEVICE_RESOLUTION; M->d *= DEVICE_RESOLUTION;
M->e *= DEVICE_RESOLUTION; M->f *= DEVICE_RESOLUTION;
return 0;
}
/*
* Again, the only piece that needs x_user and y_user is
* that piece dealing with texfig.
*/
static int
do_operator (const char *token, double x_user, double y_user)
{
int error = 0;
int opcode = 0;
double values[12];
pdf_obj *tmp = NULL;
pdf_tmatrix matrix;
pdf_coord cp;
pdf_color color;
#define PUSH(o) { \
if (top_stack < PS_STACK_SIZE) { \
stack[top_stack++] = (o); \
} else { \
WARN("PS stack overflow including MetaPost file or inline PS code"); \
error=1; \
break;\
} \
}
opcode = get_opcode(token);
switch (opcode) {
/*
* Arithmetic operators
*/
case ADD:
error = pop_get_numbers(values, 2);
if (!error)
PUSH(pdf_new_number(values[0] + values[1]));
break;
case MUL:
error = pop_get_numbers(values, 2);
if (!error)
PUSH(pdf_new_number(values[0]*values[1]));
break;
case NEG:
error = pop_get_numbers(values, 1);
if (!error)
PUSH(pdf_new_number(-values[0]));
break;
case SUB:
error = pop_get_numbers(values, 2);
if (!error)
PUSH(pdf_new_number(values[0] - values[1]));
break;
case DIV:
error = pop_get_numbers(values, 2);
if (!error)