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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
|
/* $Header: /home/cvsroot/dvipdfmx/src/spc_dvips.c,v 1.11 2008/05/29 13:43:51 chofchof Exp $
This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
Copyright (C) 2002 by Jin-Hwan Cho and Shunsaku Hirata,
the dvipdfmx project team <dvipdfmx@project.ktug.or.kr>
Copyright (C) 1998, 1999 by Mark A. Wicks <mwicks@kettering.edu>
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.
*/
#include <string.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "system.h"
#include "mem.h"
#include "error.h"
#include "dpxfile.h"
#include "dvi.h"
#include "dvicodes.h"
#include "pdfparse.h"
#include "pdfdoc.h"
#include "mpost.h"
#include "pdfximage.h"
#include "pdfdraw.h"
#include "pdfcolor.h"
#include "pdfdev.h"
#include "specials.h"
#include "spc_util.h"
#include "mfileio.h"
#include "spc_dvips.h"
#include "spc_xtx.h"
#include "epdf.h"
static int block_pending = 0;
static double pending_x = 0.0;
static double pending_y = 0.0;
static int position_set = 0;
static char** ps_headers = 0;
static int num_ps_headers = 0;
static int
spc_handler_ps_header (struct spc_env *spe, struct spc_arg *args)
{
char *ps_header, *pro;
skip_white(&args->curptr, args->endptr);
if (args->curptr + 1 >= args->endptr ||
args->curptr[0] != '=') {
spc_warn(spe, "No filename specified for PSfile special.");
return -1;
}
args->curptr++;
pro = malloc(args->endptr - args->curptr + 1);
strncpy(pro, args->curptr, args->endptr - args->curptr);
pro[args->endptr - args->curptr] = 0;
ps_header = kpse_find_file(pro, kpse_tex_ps_header_format, 0);
if (!ps_header) {
spc_warn(spe, "PS header %s not found.", pro);
return -1;
}
free(pro);
if (!(num_ps_headers & 0x0f))
ps_headers = realloc(ps_headers, sizeof(char*) * (num_ps_headers + 16));
ps_headers[num_ps_headers++] = ps_header;
args->curptr = args->endptr;
return 0;
}
static char *
parse_filename (const char **pp, const char *endptr)
{
char *r;
const char *q = NULL, *p = *pp;
char qchar;
int n;
if (!p || p >= endptr)
return NULL;
else if (*p == '\"' || *p == '\'')
qchar = *p++;
else {
qchar = ' ';
}
for (n = 0, q = p; p < endptr && *p != qchar; n++, p++);
if (qchar != ' ') {
if (*p != qchar)
return NULL;
p++;
}
if (!q || n == 0)
return NULL;
#if 0
{
int i;
for (i = 0; i < n && isprint(q[i]); i++);
if (i != n) {
WARN("Non printable char in filename string...");
}
}
#endif
r = NEW(n + 1, char);
memcpy(r, q, n); r[n] = '\0';
*pp = p;
return r;
}
/* =filename ... */
static int
spc_handler_ps_file (struct spc_env *spe, struct spc_arg *args)
{
int form_id;
char *filename;
transform_info ti;
ASSERT(spe && args);
skip_white(&args->curptr, args->endptr);
if (args->curptr + 1 >= args->endptr ||
args->curptr[0] != '=') {
spc_warn(spe, "No filename specified for PSfile special.");
return -1;
}
args->curptr++;
filename = parse_filename(&args->curptr, args->endptr);
if (!filename) {
spc_warn(spe, "No filename specified for PSfile special.");
return -1;
}
transform_info_clear(&ti);
if (spc_util_read_dimtrns(spe, &ti, args, NULL, 1) < 0) {
RELEASE(filename);
return -1;
}
form_id = pdf_ximage_findresource(filename, 1, NULL);
if (form_id < 0) {
spc_warn(spe, "Failed to read image file: %s", filename);
RELEASE(filename);
return -1;
}
RELEASE(filename);
pdf_dev_put_image(form_id, &ti, spe->x_user, spe->y_user);
return 0;
}
/* This isn't correct implementation but dvipdfm supports... */
static int
spc_handler_ps_plotfile (struct spc_env *spe, struct spc_arg *args)
{
int error = 0;
int form_id;
char *filename;
transform_info p;
ASSERT(spe && args);
spc_warn(spe, "\"ps: plotfile\" found (not properly implemented)");
skip_white(&args->curptr, args->endptr);
filename = parse_filename(&args->curptr, args->endptr);
if (!filename) {
spc_warn(spe, "Expecting filename but not found...");
return -1;
}
form_id = pdf_ximage_findresource(filename, 1, NULL);
if (form_id < 0) {
spc_warn(spe, "Could not open PS file: %s", filename);
error = -1;
} else {
transform_info_clear(&p);
p.matrix.d = -1.0; /* xscale = 1.0, yscale = -1.0 */
#if 0
/* I don't know how to treat this... */
pdf_dev_put_image(form_id, &p,
block_pending ? pending_x : spe->x_user,
block_pending ? pending_y : spe->y_user);
#endif
pdf_dev_put_image(form_id, &p, 0, 0);
}
RELEASE(filename);
return error;
}
static int
spc_handler_ps_literal (struct spc_env *spe, struct spc_arg *args)
{
int error = 0;
int st_depth, gs_depth;
double x_user, y_user;
ASSERT(spe && args && args->curptr <= args->endptr);
if (args->curptr + strlen(":[begin]") <= args->endptr &&
!strncmp(args->curptr, ":[begin]", strlen(":[begin]"))) {
block_pending++;
position_set = 1;
x_user = pending_x = spe->x_user;
y_user = pending_y = spe->y_user;
args->curptr += strlen(":[begin]");
} else if (args->curptr + strlen(":[end]") <= args->endptr &&
!strncmp(args->curptr, ":[end]", strlen(":[end]"))) {
if (block_pending <= 0) {
spc_warn(spe, "No corresponding ::[begin] found.");
return -1;
}
block_pending--;
position_set = 0;
x_user = pending_x;
y_user = pending_y;
args->curptr += strlen(":[end]");
} else if (args->curptr < args->endptr &&
args->curptr[0] == ':') {
x_user = position_set ? pending_x : spe->x_user;
y_user = position_set ? pending_y : spe->y_user;
args->curptr++;
} else {
position_set = 1;
x_user = pending_x = spe->x_user;
y_user = pending_y = spe->y_user;
}
skip_white(&args->curptr, args->endptr);
if (args->curptr < args->endptr) {
st_depth = mps_stack_depth();
gs_depth = pdf_dev_current_depth();
error = mps_exec_inline(&args->curptr,
args->endptr,
x_user, y_user);
if (error) {
spc_warn(spe, "Interpreting PS code failed!!! Output might be broken!!!");
pdf_dev_grestore_to(gs_depth);
} else if (st_depth != mps_stack_depth()) {
spc_warn(spe, "Stack not empty after execution of inline PostScript code.");
spc_warn(spe, ">> Your macro package makes some assumption on internal behaviour of DVI drivers.");
spc_warn(spe, ">> It may not compatible with dvipdfmx.");
}
}
return error;
}
static char *global_defs = 0;
static char *page_defs = 0;
static char *temporary_defs = 0;
static char *distiller_template = 0;
static pdf_coord *put_stack;
static int put_stack_depth = -1;
static char *gs_in = 0;
#if 0
/* Not used */
static int
spc_handler_ps_tricks_gdef (struct spc_env *spe, struct spc_arg *args)
{
FILE* fp;
fp = fopen(global_defs, "ab");
fwrite(args->curptr, 1, args->endptr - args->curptr, fp);
fprintf(fp, "\n");
fclose(fp);
return 0;
}
#endif
static int
spc_handler_ps_tricks_pdef (struct spc_env *spe, struct spc_arg *args)
{
FILE* fp;
pdf_tmatrix M, T = { 1, 0, 0, 1, 0, 0 };
pdf_coord pt;
pdf_dev_currentmatrix(&M);
pdf_dev_get_fixed_point(&pt);
T.e = pt.x;
T.f = pt.y;
pdf_concatmatrix(&M, &T);
if (page_defs == 0)
page_defs = dpx_create_temp_file();
fp = fopen(page_defs, "ab");
fprintf(fp, "gsave initmatrix [%f %f %f %f %f %f] concat %f %f moveto\n", M.a, M.b, M.c, M.d, M.e, M.f, spe->x_user - pt.x, spe->y_user - pt.y);
fwrite(args->curptr, 1, args->endptr - args->curptr, fp);
fprintf(fp, "\ngrestore\n");
fclose(fp);
return 0;
}
static int
spc_handler_ps_tricks_tdef (struct spc_env *spe, struct spc_arg *args)
{
FILE* fp;
if (!temporary_defs)
temporary_defs = dpx_create_temp_file();
fp = fopen(temporary_defs, "wb");
fwrite(args->curptr, 1, args->endptr - args->curptr, fp);
fprintf(fp, "\n");
fclose(fp);
return 0;
}
static int calculate_PS (char *string, int length, double *res1, double *res2, double *res3, double *res4, double *res5, double *res6);
static int
spc_handler_ps_tricks_bput (struct spc_env *spe, struct spc_arg *args, int must_def, int pre_def)
{
char *PutBegin, *formula, *ncLabel;
int label = 0;
pdf_coord tr;
pdf_tmatrix M, T = { 1, 0, 0, 1, 0, 0 };
if (must_def != 0) {
ncLabel = strstr(args->curptr, "LPut");
if (ncLabel != 0 && ncLabel < args->endptr - 3)
label = 1;
ncLabel = strstr(args->curptr, "HPutPos");
if (ncLabel != 0 && ncLabel < args->endptr - 6)
label = 1;
}
if (pre_def == 0) {
dpx_delete_temp_file(temporary_defs);
temporary_defs = 0;
}
pdf_dev_currentmatrix(&M);
formula = malloc(args->endptr - args->curptr + 120);
if (label != 0) {
sprintf(formula, "[%f %f %f %f %f %f] concat %f %f moveto\n", M.a, M.b, M.c, M.d, M.e, M.f, spe->x_user + get_origin(1), spe->y_user + get_origin(0));
} else
sprintf(formula, "[%f %f %f %f %f %f] concat %f %f moveto\n", M.a, M.b, M.c, M.d, M.e, M.f, spe->x_user, spe->y_user);
strncat(formula, args->curptr, args->endptr - args->curptr);
PutBegin = strstr(formula, "PutBegin");
strcpy(PutBegin, "exch = =");
*(PutBegin + 8) = 0;
if (calculate_PS(formula, strlen(formula), &tr.x, &tr.y, 0, 0, 0, 0) == 0) {
if (!(++put_stack_depth & 0x0f))
put_stack = realloc(put_stack, (put_stack_depth + 16) * sizeof(pdf_coord));
put_stack[put_stack_depth] = tr;
}
T.e = tr.x; T.f = tr.y;
pdf_dev_concat(&T);
if (must_def != 0) {
FILE* fp;
if (!temporary_defs)
temporary_defs = dpx_create_temp_file();
fp = fopen(temporary_defs, "ab");
fprintf(fp, "gsave\n");
if (label == 0)
fprintf(fp, "[%f %f %f %f %f %f] concat %f %f moveto\n", M.a, M.b, M.c, M.d, M.e, M.f, spe->x_user, spe->y_user);
fwrite(args->curptr, 1, args->endptr - args->curptr, fp);
fprintf(fp, "\ngrestore\n");
fclose(fp);
}
free(formula);
return 0;
}
static int
spc_handler_ps_tricks_eput (struct spc_env *spe, struct spc_arg *args)
{
pdf_coord tr = put_stack[put_stack_depth--];
pdf_tmatrix M = { 1, 0, 0, 1, -tr.x, -tr.y };
pdf_dev_concat(&M);
return 0;
}
/* Rotation without gsave/grestore. */
static double* RAngles = 0;
static int RAngleCount = -1;
static int
spc_handler_ps_tricks_brotate (struct spc_env *spe, struct spc_arg *args)
{
double value, RAngle = 0;
char *cmd, *RotBegin;
int i, l = args->endptr - args->curptr;
static const char *pre = "tx@Dict begin /RAngle { %f } def\n";
static const char *post = "= end";
if (!(++RAngleCount & 0x0f))
RAngles = realloc(RAngles, (RAngleCount + 16) * sizeof(double));
for (i = 0; i < RAngleCount; i++)
RAngle += RAngles[i];
cmd = calloc(l + strlen(pre) + strlen(post) + 12, 1);
sprintf(cmd, pre, RAngle);
strncat(cmd, args->curptr, l);
RotBegin = strstr(cmd, "RotBegin");
strcpy(RotBegin, post);
if (calculate_PS(cmd, strlen(cmd), &value, 0, 0, 0, 0, 0) != 0)
return -1;
RAngles[RAngleCount] = value;
return spc_handler_xtx_do_transform (spe->x_user, spe->y_user,
cos(value * M_PI / 180), sin(value * M_PI / 180),
-sin(value * M_PI / 180), cos(value * M_PI / 180),
0, 0);
}
static int
spc_handler_ps_tricks_erotate (struct spc_env *spe, struct spc_arg *args)
{
double value = RAngles[RAngleCount--];
return spc_handler_xtx_do_transform (spe->x_user, spe->y_user,
cos(value * M_PI / 180), -sin(value * M_PI / 180),
sin(value * M_PI / 180), cos(value * M_PI / 180),
0, 0);
}
static int
spc_handler_ps_tricks_transform (struct spc_env *spe, struct spc_arg *args)
{
double d1, d2, d3, d4, d5, d6;
char *cmd, *concat;
int l = args->endptr - args->curptr;
static const char *post = "concat matrix currentmatrix ==";
cmd = calloc(l + 41, 1);
strncpy(cmd, "matrix setmatrix ", 17);
strncpy(cmd + 17, args->curptr, l);
concat = strstr(cmd, "concat");
if (concat != 0) {
strcpy(concat, post);
concat[strlen(post)] = 0;
concat = strstr(cmd, "{");
*concat = ' ';
if (calculate_PS(cmd, strlen(cmd), &d1, &d2, &d3, &d4, &d5, &d6) != 0)
return -1;
if (spc_handler_xtx_gsave (0, 0) != 0)
return -1;
return spc_handler_xtx_do_transform (spe->x_user, spe->y_user, d1, d2, d3, d4, d5, d6);
}
return spc_handler_xtx_grestore (0, 0);
}
static int
check_next_obj(const unsigned char * buffer)
{
switch (buffer[0]) {
case XXX1:
if (buffer[1] < 5)
return 0;
buffer += 2;
break;
case XXX2:
buffer += 3;
break;
case XXX3:
buffer += 4;
break;
case XXX4:
buffer += 5;
break;
default:
return 0;
}
if (strncmp((const char*)buffer, "pst:", 4))
return 0;
return 1;
}
static int
spc_handler_ps_tricks_parse_path (struct spc_env *spe, struct spc_arg *args,
int flag)
{
FILE* fp;
int k;
pdf_tmatrix M;
char *distiller_template = get_distiller_template();
char *gs_out;
const char *clip;
int error;
if (!distiller_template)
distiller_template = get_distiller_template();
pdf_dev_currentmatrix(&M);
if (!gs_in) {
gs_in = dpx_create_temp_file();
if (!gs_in) {
WARN("Failed to create temporary input file for PSTricks image conversion.");
return -1;
}
fp = fopen(gs_in, "wb");
for (k = 0; k < num_ps_headers; k++)
fprintf(fp, "(%s) run\n", ps_headers[k]);
fprintf(fp, "[%f %f %f %f %f %f] concat %f %f translate 0 0 moveto\n", M.a, M.b, M.c, M.d, M.e, M.f, spe->x_user, spe->y_user);
fprintf(fp, "(%s) run\n", global_defs);
if (page_defs != 0)
fprintf(fp, "(%s) run\n", page_defs);
#if 0
fprintf(fp, "/clip {stroke} def\n");
fwrite(args->curptr, 1, args->endptr - args->curptr, fp);
#else
clip = strstr(args->curptr, " clip");
if (clip == 0 || clip > args->endptr - 5) {
fprintf(fp, "tx@TextPathDict begin /stroke {} def\n");
fwrite(args->curptr, 1, args->endptr - args->curptr, fp);
fprintf(fp, "\nend\n");
fclose(fp);
return 0;
} else {
fwrite(args->curptr, 1, clip - args->curptr, fp);
fprintf(fp, " stroke ");
skip_white(&clip, args->endptr);
parse_ident(&clip, args->endptr);
fwrite(clip, 1, args->endptr - clip, fp);
}
#endif
} else {
fp = fopen(gs_in, "ab");
fprintf(fp, "flattenpath stroke\n");
}
fclose(fp);
gs_out = dpx_create_temp_file();
if (!gs_out) {
WARN("Failed to create temporary output file for PSTricks image conversion.");
RELEASE(gs_in);
gs_in = 0;
return -1;
}
#ifdef MIKTEX
{
char *p;
for (p = (char *)gs_in; *p; p++) {
if (*p == '\\') *p = '/';
}
for (p = (char *)gs_out; *p; p++) {
if (*p == '\\') *p = '/';
}
}
#endif
error = dpx_file_apply_filter(distiller_template, gs_in, gs_out,
(unsigned char) pdf_get_version());
if (error) {
WARN("Image format conversion for PSTricks failed.");
RELEASE(gs_in);
gs_in = 0;
return error;
}
fp = fopen(gs_out, "rb");
if (pdf_copy_clip(fp, 1, 0, 0) != 0) {
spc_warn(spe, "Failed to parse the clipping path.");
RELEASE(gs_in);
gs_in = 0;
RELEASE(gs_out);
return -1;
}
fclose(fp);
dpx_delete_temp_file(gs_out);
dpx_delete_temp_file(gs_in);
gs_in = 0;
return 0;
}
static int
spc_handler_ps_tricks_render (struct spc_env *spe, struct spc_arg *args)
{
FILE* fp;
int k;
pdf_tmatrix M;
if (!distiller_template)
distiller_template = get_distiller_template();
pdf_dev_currentmatrix(&M);
if (!gs_in) {
gs_in = dpx_create_temp_file();
if (!gs_in) {
WARN("Failed to create temporary input file for PSTricks image conversion.");
return -1;
}
fp = fopen(gs_in, "wb");
for (k = 0; k < num_ps_headers; k++)
fprintf(fp, "(%s) run\n", ps_headers[k]);
fprintf(fp, "[%f %f %f %f %f %f] concat %f %f translate 0 0 moveto\n", M.a, M.b, M.c, M.d, M.e, M.f, spe->x_user, spe->y_user);
fprintf(fp, "(%s) run\n", global_defs);
if (page_defs != 0)
fprintf(fp, "(%s) run\n", page_defs);
} else
fp = fopen(gs_in, "ab");
fprintf(fp, "\nsave\n");
fwrite(args->curptr, 1, args->endptr - args->curptr, fp);
fprintf(fp, "\ncount 1 sub {pop} repeat restore\n");
if (check_next_obj((const unsigned char*)args->endptr)) {
fclose(fp);
} else {
char *distiller_template = get_distiller_template();
char *gs_out;
int error, form_id;
transform_info p;
transform_info_clear(&p);
pdf_invertmatrix(&M);
p.matrix = M;
fclose(fp);
gs_out = dpx_create_temp_file();
if (!gs_out) {
WARN("Failed to create temporary output file for PSTricks image conversion.");
RELEASE(gs_in);
gs_in = 0;
return -1;
}
#ifdef MIKTEX
{
char *p;
for (p = (char *)gs_in; *p; p++) {
if (*p == '\\') *p = '/';
}
for (p = (char *)gs_out; *p; p++) {
if (*p == '\\') *p = '/';
}
}
#endif
error = dpx_file_apply_filter(distiller_template, gs_in, gs_out,
(unsigned char) pdf_get_version());
if (error) {
WARN("Image format conversion for PSTricks failed.");
RELEASE(gs_in);
gs_in = 0;
return error;
}
form_id = pdf_ximage_findresource(gs_out, 1, NULL);
if (form_id < 0) {
spc_warn(spe, "Failed to read converted PSTricks image file.");
RELEASE(gs_in);
gs_in = 0;
RELEASE(gs_out);
return -1;
}
pdf_dev_put_image(form_id, &p, 0, 0);
dpx_delete_temp_file(gs_out);
dpx_delete_temp_file(gs_in);
gs_in = 0;
}
return 0;
}
typedef enum {
render = 1 << 0,
global_def = 1 << 1,
page_def = 1 << 2,
new_temp = 1 << 3,
add_temp = 1 << 4,
begin_put = 1 << 5,
end_put = 1 << 6,
begin_rotate = 1 << 7,
end_rotate = 1 << 8,
parse = 1 << 9,
req_ref = 1 << 10,
transform = 1 << 11
} Operation;
/* ToDo: all the substring search must be centralized so that *
* keys can be read from external configuration. */
struct pstricks_key_ {
const char * key;
Operation exec;
} pstricks_key[] = {
/* The first 5 are hard-coded here. */
{"LPut", add_temp | req_ref},
{"HPutPos", add_temp | req_ref},
{"PutBegin", begin_put},
{"RotBegin", begin_rotate},
{"clip", parse},
/* The rest can be read from an external source. */
{"NewNode", page_def | req_ref},
{"InitNC", render | new_temp},
{"/Glbx", add_temp},
{"NewtonSolving", add_temp},
{"tx@LightThreeDDict", page_def},
{"PutEnd", end_put},
{"RotEnd", end_rotate},
{"mtrxc", parse},
{"stroke", render},
{"fill", render},
{"Fill", render},
{" Glbx", req_ref},
{"TextPathShow", parse},
{"/rotAngle", page_def},
{"NAngle", req_ref},
{"TMatrix", transform}
};
static int
spc_handler_ps_trickscmd (struct spc_env *spe, struct spc_arg *args)
{
char *test_string;
int k, error = 0, f_exec = 0;
/* Hack time! */
/* The problem is that while any macros in pstricks.tex
* can be overridden by the codes in pstricks.con, you cannot
* modify the pst@Verb specials generated by other PSTricks
* packages. So pstricks generate specials won't signal what
* to expect for you.
*/
test_string = malloc(args->endptr - args->curptr + 1);
strncpy(test_string, args->curptr, args->endptr - args->curptr);
test_string[args->endptr - args->curptr] = 0;
for (k = 0; k < sizeof(pstricks_key) / sizeof(pstricks_key[0]); k++) {
if (strstr(test_string, pstricks_key[k].key) != 0)
f_exec |= pstricks_key[k].exec;
}
free(test_string);
if (f_exec & new_temp)
error |= spc_handler_ps_tricks_tdef(spe, args);
if (f_exec & render)
error |= spc_handler_ps_tricks_render(spe, args);
if (f_exec & parse)
error |= spc_handler_ps_tricks_parse_path(spe, args, f_exec);
if (f_exec & begin_put)
error |= spc_handler_ps_tricks_bput(spe, args, (f_exec & add_temp), (f_exec & req_ref));
if (f_exec & end_put)
error |= spc_handler_ps_tricks_eput(spe, args);
if (f_exec & begin_rotate)
error |= spc_handler_ps_tricks_brotate(spe, args);
if (f_exec & end_rotate)
error |= spc_handler_ps_tricks_erotate(spe, args);
if (f_exec & transform)
error |= spc_handler_ps_tricks_transform(spe, args);
if (f_exec & page_def)
error |= spc_handler_ps_tricks_pdef (spe, args);
if (f_exec == 0)
error |= spc_handler_ps_tricks_pdef (spe, args);
args->curptr = args->endptr;
return error;
}
static int
spc_handler_ps_tricksobj (struct spc_env *spe, struct spc_arg *args)
{
int error = spc_handler_ps_tricks_render(spe, args);
args->curptr = args->endptr;
return error;
}
static int
spc_handler_ps_default (struct spc_env *spe, struct spc_arg *args)
{
int error;
int st_depth, gs_depth;
ASSERT(spe && args);
pdf_dev_gsave();
st_depth = mps_stack_depth();
gs_depth = pdf_dev_current_depth();
{
pdf_tmatrix M;
M.a = M.d = 1.0; M.b = M.c = 0.0; M.e = spe->x_user; M.f = spe->y_user;
pdf_dev_concat(&M);
error = mps_exec_inline(&args->curptr,
args->endptr,
spe->x_user, spe->y_user);
M.e = -spe->x_user; M.f = -spe->y_user;
pdf_dev_concat(&M);
}
if (error)
spc_warn(spe, "Interpreting PS code failed!!! Output might be broken!!!");
else {
if (st_depth != mps_stack_depth()) {
spc_warn(spe, "Stack not empty after execution of inline PostScript code.");
spc_warn(spe, ">> Your macro package makes some assumption on internal behaviour of DVI drivers.");
spc_warn(spe, ">> It may not compatible with dvipdfmx.");
}
}
pdf_dev_grestore_to(gs_depth);
pdf_dev_grestore();
return error;
}
static struct spc_handler dvips_handlers[] = {
{"header", spc_handler_ps_header},
{"PSfile", spc_handler_ps_file},
{"psfile", spc_handler_ps_file},
{"ps: plotfile ", spc_handler_ps_plotfile},
{"PS: plotfile ", spc_handler_ps_plotfile},
{"PS:", spc_handler_ps_literal},
{"ps:", spc_handler_ps_literal},
{"PST:", spc_handler_ps_trickscmd},
{"pst:", spc_handler_ps_tricksobj},
{"\" ", spc_handler_ps_default}
};
int
spc_dvips_at_begin_page (void)
{
if (page_defs) {
dpx_delete_temp_file(page_defs);
page_defs = 0;
}
put_stack_depth = -1;
return 0;
}
int
spc_dvips_at_end_page (void)
{
mps_eop_cleanup();
if (!temporary_defs) {
dpx_delete_temp_file(temporary_defs);
temporary_defs = 0;
}
return 0;
}
int
spc_dvips_at_begin_document (void)
{
FILE* fp;
/* This, together with \pscharpath support code, must be moved to xtex.pro header. */
global_defs = dpx_create_temp_file();
fp = fopen(global_defs, "wb");
fprintf(fp, "tx@Dict begin /STV {} def end\n");
fclose(fp);
return 0;
}
int
spc_dvips_at_end_document (void)
{
if (ps_headers) {
while (num_ps_headers > 0)
RELEASE(ps_headers[--num_ps_headers]);
free(ps_headers);
ps_headers = NULL;
}
dpx_delete_temp_file(global_defs);
dpx_delete_temp_file(page_defs);
return 0;
}
int
spc_dvips_check_special (const char *buf, long len)
{
const char *p, *endptr;
int i;
p = buf;
endptr = p + len;
skip_white(&p, endptr);
if (p >= endptr)
return 0;
len = (long) (endptr - p);
for (i = 0;
i < sizeof(dvips_handlers)/sizeof(struct spc_handler); i++) {
if (len >= strlen(dvips_handlers[i].key) &&
!memcmp(p, dvips_handlers[i].key,
strlen(dvips_handlers[i].key))) {
return 1;
}
}
return 0;
}
int
spc_dvips_setup_handler (struct spc_handler *handle,
struct spc_env *spe, struct spc_arg *args)
{
const char *key;
int i, keylen;
ASSERT(handle && spe && args);
skip_white(&args->curptr, args->endptr);
key = args->curptr;
while (args->curptr < args->endptr &&
isalpha(args->curptr[0])) {
args->curptr++;
}
/* Test for "ps:". The "ps::" special is subsumed under this case. */
if (args->curptr < args->endptr &&
args->curptr[0] == ':') {
args->curptr++;
if (args->curptr+strlen(" plotfile ") <= args->endptr &&
!strncmp(args->curptr, " plotfile ", strlen(" plotfile "))) {
args->curptr += strlen(" plotfile ");
}
} else if (args->curptr+1 < args->endptr &&
args->curptr[0] == '"' && args->curptr[1] == ' ') {
args->curptr += 2;
}
keylen = (int) (args->curptr - key);
if (keylen < 1) {
spc_warn(spe, "Not ps: special???");
return -1;
}
for (i = 0;
i < sizeof(dvips_handlers) / sizeof(struct spc_handler); i++) {
if (keylen <= strlen(dvips_handlers[i].key) &&
!strncmp(key, dvips_handlers[i].key, strlen(dvips_handlers[i].key))) {
skip_white(&args->curptr, args->endptr);
args->command = dvips_handlers[i].key;
handle->key = "ps:";
handle->exec = dvips_handlers[i].exec;
return 0;
}
}
return -1;
}
#ifdef __EMX__
#define GS_CALCULATOR "gsos2 -q -dNOPAUSE -dBATCH -sDEVICE=nullpage -f "
#elif defined(WIN32)
#define GS_CALCULATOR "gswin32c -q -dNOPAUSE -dBATCH -sDEVICE=nullpage -f "
#else
#define GS_CALCULATOR "gs -q -dNOPAUSE -dBATCH -sDEVICE=nullpage -f "
#endif
static
int calculate_PS (char *string, int length, double *res1, double *res2, double *res3, double *res4, double *res5, double *res6) {
char *formula, *cmd;
FILE *fp, *coord;
int k;
if (res1 == 0 && res2 == 0)
return -1;
formula = dpx_create_temp_file();
fp = fopen(formula, "wb");
for (k = 0; k < num_ps_headers; k++)
fprintf(fp, "(%s) run\n", ps_headers[k]);
fprintf(fp, "0 0 moveto\n");
fprintf(fp, "(%s) run\n", global_defs);
if (page_defs != 0)
fprintf(fp, "(%s) run\n", page_defs);
if (temporary_defs)
fprintf(fp, "(%s) run\n", temporary_defs);
fwrite(string, 1, length, fp);
fclose(fp);
#ifdef MIKTEX
{
char *p;
for (p = formula; *p; p++)
if (*p == '\\')
*p = '/';
}
#endif
k = strlen(GS_CALCULATOR) + strlen(formula) + 1;
cmd = NEW(k, char);
strcpy(cmd, GS_CALCULATOR);
strcat(cmd, formula);
coord = popen(cmd, "r");
if (coord) {
if (res1 == 0)
fscanf(coord, " %lf ", res2);
else if (res2 == 0)
fscanf(coord, " %lf ", res1);
else if (res3 == 0)
fscanf(coord, " %lf %lf ", res1, res2);
else
fscanf(coord, " [%lf %lf %lf %lf %lf %lf] ", res1, res2, res3, res4, res5, res6);
} else
return -1;
pclose(coord);
RELEASE(cmd);
dpx_delete_temp_file(formula);
return 0;
}
|