-
Notifications
You must be signed in to change notification settings - Fork 0
/
spc_pdfm.c
2035 lines (1712 loc) · 51.7 KB
/
spc_pdfm.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) 2007-2014 by Jin-Hwan Cho and Shunsaku Hirata,
the dvipdfmx project team.
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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctype.h>
#include "dvi.h"
#include "system.h"
#include "mem.h"
#include "error.h"
#include "mfileio.h"
#include "numbers.h"
#include "libtexpdf/libtexpdf.h"
#include "dvipdfmx.h"
#include "fontmap.h"
#include "specials.h"
#include "spc_util.h"
#include "spc_pdfm.h"
#include "dvipdfmx.h"
/* PLEASE REMOVE THIS */
struct resource_map {
int type;
int res_id;
};
struct tounicode {
int cmap_id;
int unescape_backslash;
pdf_obj *taintkeys; /* An array of PDF names. */
};
struct spc_pdf_
{
pdf_obj *annot_dict; /* pending annotation dict */
int lowest_level; /* current min level of outlines */
struct ht_table *resourcemap; /* see remark below (somewhere) */
struct tounicode cd; /* For to-UTF16-BE conversion :( */
};
static struct spc_pdf_ _pdf_stat = {
NULL,
255,
NULL,
{ -1, 0, NULL }
};
/* PLEASE REMOVE THIS */
static void
hval_free (void *vp)
{
RELEASE(vp);
}
static int
addresource (struct spc_pdf_ *sd, const char *ident, int res_id)
{
struct resource_map *r;
if (!ident || res_id < 0)
return -1;
r = NEW(1, struct resource_map);
r->type = 0; /* unused */
r->res_id = res_id;
texpdf_ht_append_table(sd->resourcemap, ident, strlen(ident), r);
spc_push_object(ident, texpdf_ximage_get_reference(res_id));
return 0;
}
static int
findresource (struct spc_pdf_ *sd, const char *ident)
{
struct resource_map *r;
if (!ident)
return -1;
r = texpdf_ht_lookup_table(sd->resourcemap, ident, strlen(ident));
return (r ? r->res_id : -1);
}
static int
spc_handler_pdfm__init (void *dp)
{
struct spc_pdf_ *sd = dp;
static const char *default_taintkeys[] = {
"Title", "Author", "Subject", "Keywords",
"Creator", "Producer", "Contents", "Subj",
"TU", "T", "TM", NULL /* EOD */
};
int i;
sd->annot_dict = NULL;
sd->lowest_level = 255;
sd->resourcemap = NEW(1, struct ht_table);
texpdf_ht_init_table(sd->resourcemap, hval_free);
sd->cd.taintkeys = texpdf_new_array();
for (i = 0; default_taintkeys[i] != NULL; i++) {
texpdf_add_array(sd->cd.taintkeys,
texpdf_new_name(default_taintkeys[i]));
}
return 0;
}
static int
spc_handler_pdfm__clean (void *dp)
{
struct spc_pdf_ *sd = dp;
if (sd->annot_dict) {
WARN("Unbalanced bann and eann found.");
texpdf_release_obj(sd->annot_dict);
}
sd->lowest_level = 255;
sd->annot_dict = NULL;
if (sd->resourcemap) {
texpdf_ht_clear_table(sd->resourcemap);
RELEASE(sd->resourcemap);
}
sd->resourcemap = NULL;
if (sd->cd.taintkeys)
texpdf_release_obj(sd->cd.taintkeys);
sd->cd.taintkeys = NULL;
return 0;
}
int
spc_pdfm_at_begin_document (void)
{
struct spc_pdf_ *sd = &_pdf_stat;
return spc_handler_pdfm__init(sd);
}
int
spc_pdfm_at_end_document (void)
{
struct spc_pdf_ *sd = &_pdf_stat;
return spc_handler_pdfm__clean(sd);
}
/* Dvipdfm specials */
static int
spc_handler_pdfm_bop (struct spc_env *spe, struct spc_arg *args)
{
if (args->curptr < args->endptr) {
texpdf_doc_set_bop_content(pdf, args->curptr,
(long) (args->endptr - args->curptr));
}
args->curptr = args->endptr;
return 0;
}
static int
spc_handler_pdfm_eop (struct spc_env *spe, struct spc_arg *args)
{
if (args->curptr < args->endptr) {
texpdf_doc_set_eop_content(pdf, args->curptr,
(long) (args->endptr - args->curptr));
}
args->curptr = args->endptr;
return 0;
}
#define streamfiltered(o) \
(texpdf_lookup_dict(texpdf_stream_dict((o)), "Filter") ? 1 : 0)
/* Why should we have this kind of things? */
static int
safeputresdent (pdf_obj *kp, pdf_obj *vp, void *dp)
{
char *key;
ASSERT(kp && vp && dp);
key = texpdf_name_value(kp);
if (texpdf_lookup_dict(dp, key))
WARN("Object \"%s\" already defined in dict! (ignored)", key);
else {
texpdf_add_dict(dp,
texpdf_link_obj(kp), texpdf_link_obj(vp));
}
return 0;
}
#ifndef pdf_obj_isaref
#define pdf_obj_isaref(o) (texpdf_obj_typeof((o)) == PDF_INDIRECT)
#endif
static int
safeputresdict (pdf_obj *kp, pdf_obj *vp, void *dp)
{
char *key;
pdf_obj *dict;
ASSERT(kp && vp && dp);
key = texpdf_name_value(kp);
dict = texpdf_lookup_dict(dp, key);
if (pdf_obj_isaref(vp)) {
texpdf_add_dict(dp, texpdf_new_name(key), texpdf_link_obj(vp));
} else if (texpdf_obj_typeof(vp) == PDF_DICT) {
if (dict)
texpdf_foreach_dict(vp, safeputresdent, dict);
else {
texpdf_add_dict(dp, texpdf_new_name(key), texpdf_link_obj(vp));
}
} else {
WARN("Invalid type (not DICT) for page/form resource dict entry: key=\"%s\"", key);
return -1;
}
return 0;
}
/* Think what happens if you do
*
* pdf:put @resources << /Font << >> >>
*
*/
static int
spc_handler_pdfm_put (struct spc_env *spe, struct spc_arg *ap)
{
pdf_obj *obj1, *obj2; /* put obj2 into obj1 */
char *ident;
int error = 0;
texpdf_skip_white(&ap->curptr, ap->endptr);
ident = texpdf_parse_opt_ident(&ap->curptr, ap->endptr);
if (!ident) {
spc_warn(spe, "Missing object identifier.");
return -1;
}
obj1 = spc_lookup_object(ident);
if (!obj1) {
spc_warn(spe, "Specified object not exist: %s", ident);
RELEASE(ident);
return -1;
}
texpdf_skip_white(&ap->curptr, ap->endptr);
obj2 = texpdf_parse_pdf_object(&ap->curptr, ap->endptr, NULL);
if (!obj2) {
spc_warn(spe, "Missing (an) object(s) to put into \"%s\"!", ident);
RELEASE(ident);
return -1;
}
switch (texpdf_obj_typeof(obj1)) {
case PDF_DICT:
if (texpdf_obj_typeof(obj2) != PDF_DICT) {
spc_warn(spe, "Inconsistent object type for \"put\" (expecting DICT): %s", ident);
error = -1;
} else {
if (!strcmp(ident, "resources"))
error = texpdf_foreach_dict(obj2, safeputresdict, obj1);
else {
texpdf_merge_dict(obj1, obj2);
}
}
break;
case PDF_STREAM:
if (texpdf_obj_typeof(obj2) == PDF_DICT)
texpdf_merge_dict(texpdf_stream_dict(obj1), obj2);
else if (texpdf_obj_typeof(obj2) == PDF_STREAM)
#if 0
{
texpdf_merge_dict(texpdf_stream_dict(obj1), texpdf_stream_dict(obj2));
texpdf_add_stream(obj1, pdf_stream_dataptr(obj2), pdf_stream_length(obj2));
}
#else
{
spc_warn(spe, "\"put\" operation not supported for STREAM <- STREAM: %s", ident);
error = -1;
}
#endif
else {
spc_warn(spe, "Invalid type: expecting a DICT or STREAM: %s", ident);
error = -1;
}
break;
case PDF_ARRAY:
/* dvipdfm */
texpdf_add_array(obj1, texpdf_link_obj(obj2));
while (ap->curptr < ap->endptr) {
pdf_obj *obj3 = texpdf_parse_pdf_object(&ap->curptr, ap->endptr, NULL);
if (!obj3)
break;
texpdf_add_array(obj1, obj3);
texpdf_skip_white(&ap->curptr, ap->endptr);
}
break;
default:
spc_warn(spe, "Can't \"put\" object into non-DICT/STREAM/ARRAY type object: %s", ident);
error = -1;
break;
}
texpdf_release_obj(obj2);
RELEASE(ident);
return error;
}
/* For pdf:tounicode support
* This feature is provided for convenience. TeX can't do
* input encoding conversion.
*/
static int
reencodestring (CMap *cmap, pdf_obj *instring)
{
#define WBUF_SIZE 4096
unsigned char wbuf[WBUF_SIZE];
unsigned char *obufcur;
const unsigned char *inbufcur;
long inbufleft, obufleft;
if (!cmap || !instring)
return 0;
inbufleft = texpdf_string_length(instring);
inbufcur = texpdf_string_value (instring);
wbuf[0] = 0xfe;
wbuf[1] = 0xff;
obufcur = wbuf + 2;
obufleft = WBUF_SIZE - 2;
texpdf_CMap_decode(cmap,
&inbufcur, &inbufleft,
&obufcur, &obufleft);
if (inbufleft > 0) {
return -1;
}
texpdf_set_string(instring, wbuf, WBUF_SIZE - obufleft);
return 0;
}
/* tables/values used in UTF-8 interpretation -
code is based on ConvertUTF.[ch] sample code
published by the Unicode consortium */
static unsigned long
offsetsFromUTF8[6] = {
0x00000000UL,
0x00003080UL,
0x000E2080UL,
0x03C82080UL,
0xFA082080UL,
0x82082080UL
};
static unsigned char
bytesFromUTF8[256] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
};
static int
maybe_reencode_utf8(pdf_obj *instring)
{
unsigned char* inbuf;
int inlen;
int non_ascii = 0;
unsigned char* cp;
unsigned char* op;
unsigned char wbuf[WBUF_SIZE];
if (!instring)
return 0;
inlen = texpdf_string_length(instring);
inbuf = texpdf_string_value(instring);
/* check if the input string is strictly ASCII */
for (cp = inbuf; cp < inbuf + inlen; ++cp) {
if (*cp > 127) {
non_ascii = 1;
}
}
if (non_ascii == 0)
return 0; /* no need to reencode ASCII strings */
if (inbuf[0] == 0xfe && inbuf[1] == 0xff)
return 0; /* no need to reencode UTF16BE with BOM */
cp = inbuf;
op = wbuf;
*op++ = 0xfe;
*op++ = 0xff;
while (cp < inbuf + inlen) {
unsigned long usv = *cp++;
int extraBytes = bytesFromUTF8[usv];
if (cp + extraBytes > inbuf + inlen)
return -1; /* ill-formed, so give up reencoding */
switch (extraBytes) { /* note: code falls through cases! */
case 5: usv <<= 6; usv += *cp++;
case 4: usv <<= 6; usv += *cp++;
case 3: usv <<= 6; usv += *cp++;
case 2: usv <<= 6; usv += *cp++;
case 1: usv <<= 6; usv += *cp++;
case 0: ;
};
usv -= offsetsFromUTF8[extraBytes];
if (usv > 0x10FFFF)
return -1; /* out of valid Unicode range, give up */
if (usv > 0xFFFF) {
/* supplementary-plane character: generate high surrogate */
unsigned long hi = 0xdc00 + (usv - 0x10000) % 0x0400;
if (op > wbuf + WBUF_SIZE - 2)
return -1; /* out of space */
*op++ = hi / 256;
*op++ = hi % 256;
usv = 0xd800 + (usv - 0x10000) / 0x0400;
/* remaining value in usv is the low surrogate */
}
if (op > wbuf + WBUF_SIZE - 2)
return -1; /* out of space */
*op++ = usv / 256;
*op++ = usv % 256;
}
texpdf_set_string(instring, wbuf, op - wbuf);
return 0;
}
static int
needreencode (pdf_obj *kp, pdf_obj *vp, struct tounicode *cd)
{
int r = 0, i;
pdf_obj *tk;
ASSERT( cd && cd->taintkeys );
ASSERT( texpdf_obj_typeof(kp) == PDF_NAME );
ASSERT( texpdf_obj_typeof(vp) == PDF_STRING );
for (i = 0; i < texpdf_array_length(cd->taintkeys); i++) {
tk = texpdf_get_array(cd->taintkeys, i);
ASSERT( tk && texpdf_obj_typeof(tk) == PDF_NAME );
if (!strcmp(texpdf_name_value(kp), texpdf_name_value(tk))) {
r = 1;
break;
}
}
if (r) {
/* Check UTF-16BE BOM. */
if (texpdf_string_length(vp) >= 2 &&
!memcmp(texpdf_string_value(vp), "\xfe\xff", 2))
r = 0;
}
return r;
}
static int
modstrings (pdf_obj *kp, pdf_obj *vp, void *dp)
{
int r = 0; /* continue */
struct tounicode *cd = dp;
ASSERT( texpdf_obj_typeof(kp) == PDF_NAME );
switch (texpdf_obj_typeof(vp)) {
case PDF_STRING:
if (cd && cd->cmap_id >= 0 && cd->taintkeys) {
CMap *cmap = texpdf_CMap_cache_get(cd->cmap_id);
if (needreencode(kp, vp, cd))
r = reencodestring(cmap, vp);
} else if (is_xdv)
r = maybe_reencode_utf8(vp);
if (r < 0) /* error occured... */
WARN("Failed to convert input string to UTF16...");
break;
case PDF_DICT:
r = texpdf_foreach_dict(vp, modstrings, dp);
break;
case PDF_STREAM:
r = texpdf_foreach_dict(texpdf_stream_dict(vp), modstrings, dp);
break;
}
return r;
}
static pdf_obj *
texpdf_parse_pdf_dict_with_tounicode (const char **pp, const char *endptr, struct tounicode *cd)
{
pdf_obj *dict;
/* disable this test for XDV files, as we do UTF8 reencoding with no cmap */
if (!is_xdv && cd->cmap_id < 0)
return texpdf_parse_pdf_dict(pp, endptr, NULL);
/* :( */
if (cd && cd->unescape_backslash)
dict = texpdf_parse_texpdf_tainted_dict(pp, endptr);
else {
dict = texpdf_parse_pdf_dict(pp, endptr, NULL);
}
if (dict)
texpdf_foreach_dict(dict, modstrings, cd);
return dict;
}
static int
spc_handler_pdfm_annot (struct spc_env *spe, struct spc_arg *args)
{
struct spc_pdf_ *sd = &_pdf_stat;
pdf_obj *annot_dict;
pdf_rect rect;
char *ident = NULL;
pdf_coord cp;
transform_info ti;
texpdf_skip_white(&args->curptr, args->endptr);
if (args->curptr[0] == '@') {
ident = texpdf_parse_opt_ident(&args->curptr, args->endptr);
texpdf_skip_white(&args->curptr, args->endptr);
}
texpdf_transform_info_clear(&ti);
if (spc_util_read_dimtrns(spe, &ti, args, NULL, 0) < 0) {
if (ident)
RELEASE(ident);
return -1;
}
if ((ti.flags & INFO_HAS_USER_BBOX) &&
((ti.flags & INFO_HAS_WIDTH) || (ti.flags & INFO_HAS_HEIGHT))) {
spc_warn(spe, "You can't specify both bbox and width/height.");
if (ident)
RELEASE(ident);
return -1;
}
annot_dict = texpdf_parse_pdf_dict_with_tounicode(&args->curptr, args->endptr, &sd->cd);
if (!annot_dict) {
spc_warn(spe, "Could not find dictionary object.");
if (ident)
RELEASE(ident);
return -1;
} else if (!PDF_OBJ_DICTTYPE(annot_dict)) {
spc_warn(spe, "Invalid type: not dictionary object.");
if (ident)
RELEASE(ident);
texpdf_release_obj(annot_dict);
return -1;
}
cp.x = spe->x_user; cp.y = spe->y_user;
texpdf_dev_transform(&cp, NULL);
if (ti.flags & INFO_HAS_USER_BBOX) {
rect.llx = ti.bbox.llx + cp.x;
rect.lly = ti.bbox.lly + cp.y;
rect.urx = ti.bbox.urx + cp.x;
rect.ury = ti.bbox.ury + cp.y;
} else {
rect.llx = cp.x;
rect.lly = cp.y - spe->mag * ti.depth;
rect.urx = cp.x + spe->mag * ti.width;
rect.ury = cp.y + spe->mag * ti.height;
}
/* Order is important... */
if (ident)
spc_push_object(ident, texpdf_link_obj(annot_dict));
/* Add this reference. */
texpdf_doc_add_annot(pdf, texpdf_doc_current_page_number(pdf), &rect, annot_dict, 1);
if (ident) {
spc_flush_object(ident);
RELEASE(ident);
}
texpdf_release_obj(annot_dict);
return 0;
}
/* NOTE: This can't have ident. See "Dvipdfm User's Manual". */
static int
spc_handler_pdfm_bann (struct spc_env *spe, struct spc_arg *args)
{
struct spc_pdf_ *sd = &_pdf_stat;
int error = 0;
if (sd->annot_dict) {
spc_warn(spe, "Can't begin an annotation when one is pending.");
return -1;
}
texpdf_skip_white(&args->curptr, args->endptr);
sd->annot_dict = texpdf_parse_pdf_dict_with_tounicode(&args->curptr, args->endptr, &sd->cd);
if (!sd->annot_dict) {
spc_warn(spe, "Ignoring annotation with invalid dictionary.");
return -1;
} else if (!PDF_OBJ_DICTTYPE(sd->annot_dict)) {
spc_warn(spe, "Invalid type: not a dictionary object.");
texpdf_release_obj(sd->annot_dict);
sd->annot_dict = NULL;
return -1;
}
error = spc_begin_annot(spe, sd->annot_dict);
return error;
}
static int
spc_handler_pdfm_eann (struct spc_env *spe, struct spc_arg *args)
{
struct spc_pdf_ *sd = &_pdf_stat;
int error = 0;
if (!sd->annot_dict) {
spc_warn(spe, "Tried to end an annotation without starting one!");
return -1;
}
error = spc_end_annot(spe);
texpdf_release_obj(sd->annot_dict);
sd->annot_dict = NULL;
return error;
}
/* Color:.... */
static int
spc_handler_pdfm_bcolor (struct spc_env *spe, struct spc_arg *ap)
{
int error;
pdf_color fc, sc;
pdf_color *pfc, *psc;
texpdf_color_get_current(&psc, &pfc);
error = spc_util_read_pdfcolor(spe, &fc, ap, pfc);
if (!error) {
if (ap->curptr < ap->endptr) {
error = spc_util_read_pdfcolor(spe, &sc, ap, psc);
} else {
texpdf_color_copycolor(&sc, &fc);
}
}
if (error)
spc_warn(spe, "Invalid color specification?");
else {
texpdf_color_push(pdf, &sc, &fc); /* save currentcolor */
}
return error;
}
/*
* This special changes the current color without clearing the color stack.
* It therefore differs from "color rgb 1 0 0".
*/
static int
spc_handler_pdfm_scolor (struct spc_env *spe, struct spc_arg *ap)
{
int error;
pdf_color fc, sc;
pdf_color *pfc, *psc;
texpdf_color_get_current(&psc, &pfc);
error = spc_util_read_pdfcolor(spe, &fc, ap, pfc);
if (!error) {
if (ap->curptr < ap->endptr) {
error = spc_util_read_pdfcolor(spe, &sc, ap, psc);
} else {
texpdf_color_copycolor(&sc, &fc);
}
}
if (error)
spc_warn(spe, "Invalid color specification?");
else
texpdf_color_set(pdf, &sc, &fc);
return error;
}
static int
spc_handler_pdfm_ecolor (struct spc_env *spe, struct spc_arg *args)
{
texpdf_color_pop(pdf);
return 0;
}
static int
spc_handler_pdfm_btrans (struct spc_env *spe, struct spc_arg *args)
{
pdf_tmatrix M;
transform_info ti;
texpdf_transform_info_clear(&ti);
if (spc_util_read_dimtrns(spe, &ti, args, NULL, 0) < 0) {
return -1;
}
/* Create transformation matrix */
pdf_copymatrix(&M, &(ti.matrix));
M.e += ((1.0 - M.a) * spe->x_user - M.c * spe->y_user);
M.f += ((1.0 - M.d) * spe->y_user - M.b * spe->x_user);
texpdf_dev_gsave(pdf);
texpdf_dev_concat(pdf, &M);
return 0;
}
static int
spc_handler_pdfm_etrans (struct spc_env *spe, struct spc_arg *args)
{
texpdf_dev_grestore(pdf);
/*
* Unfortunately, the following line is necessary in case
* of a color change inside of the save/restore pair.
* (Font changes are automatically corrected by texpdf_dev_grestore().)
* Anything that was done there must be redone, so in effect,
* we make no assumptions about what fonts. We act like we are
* starting a new page.
*/
texpdf_dev_reset_color(pdf, 0);
return 0;
}
static int
spc_handler_pdfm_outline (struct spc_env *spe, struct spc_arg *args)
{
struct spc_pdf_ *sd = &_pdf_stat;
pdf_obj *item_dict, *tmp;
int level, is_open = -1;
int current_depth;
texpdf_skip_white(&args->curptr, args->endptr);
/*
* pdf:outline is extended to support open/close feature
*
* pdf:outline 1 ... (as DVIPDFM)
* pdf:outline [] 1 ... (open bookmark)
* pdf:outline [-] 1 ... (closed bookmark)
*/
if (args->curptr+3 < args->endptr && *args->curptr == '[') {
args->curptr++;
if (*args->curptr == '-') {
args->curptr++;
} else {
is_open = 1;
}
args->curptr++;
}
texpdf_skip_white(&args->curptr, args->endptr);
tmp = texpdf_parse_pdf_object(&args->curptr, args->endptr, NULL);
if (!tmp) {
spc_warn(spe, "Missing number for outline item depth.");
return -1;
} else if (!PDF_OBJ_NUMBERTYPE(tmp)) {
texpdf_release_obj(tmp);
spc_warn(spe, "Expecting number for outline item depth.");
return -1;
}
item_dict = NULL;
level = (int) texpdf_number_value(tmp);
texpdf_release_obj(tmp);
/* What is this? Starting at level 3 and can go down to level 1?
*
* Here is the original comment:
* Make sure we know where the starting level is
*
* NOTE: added
* We need this for converting pages from 3rd to... :(
*/
sd->lowest_level = MIN(sd->lowest_level, level);
level += 1 - sd->lowest_level;
item_dict = texpdf_parse_pdf_dict_with_tounicode(&args->curptr, args->endptr, &sd->cd);
if (!item_dict) {
spc_warn(spe, "Ignoring invalid dictionary.");
return -1;
}
current_depth = texpdf_doc_bookmarks_depth(pdf);
if (current_depth > level) {
while (current_depth-- > level)
texpdf_doc_bookmarks_up(pdf);
} else if (current_depth < level) {
while (current_depth++ < level)
texpdf_doc_bookmarks_down(pdf);
}
texpdf_doc_bookmarks_add(pdf, item_dict, is_open);
return 0;
}
static int
spc_handler_pdfm_article (struct spc_env *spe, struct spc_arg *args)
{
struct spc_pdf_ *sd = &_pdf_stat;
char *ident;
pdf_obj *info_dict;
texpdf_skip_white (&args->curptr, args->endptr);
ident = texpdf_parse_opt_ident(&args->curptr, args->endptr);
if (!ident) {
spc_warn(spe, "Article name expected but not found.");
return -1;
}
info_dict = texpdf_parse_pdf_dict_with_tounicode(&args->curptr, args->endptr, &sd->cd);
if (!info_dict) {
spc_warn(spe, "Ignoring article with invalid info dictionary.");
RELEASE(ident);
return -1;
}
texpdf_doc_begin_article(pdf, ident, texpdf_link_obj(info_dict));
spc_push_object(ident, info_dict);
RELEASE(ident);
return 0;
}
static int
spc_handler_pdfm_bead (struct spc_env *spe, struct spc_arg *args)
{
struct spc_pdf_ *sd = &_pdf_stat;
pdf_obj *article;
pdf_obj *article_info;
char *article_name;
pdf_rect rect;
long page_no;
transform_info ti;
pdf_coord cp;
texpdf_skip_white(&args->curptr, args->endptr);
if (args->curptr[0] != '@') {
spc_warn(spe, "Article identifier expected but not found.");
return -1;
}
article_name = texpdf_parse_opt_ident(&args->curptr, args->endptr);
if (!article_name) {
spc_warn(spe, "Article reference expected but not found.");
return -1;
}
/* If okay so far, try to get a bounding box */
texpdf_transform_info_clear(&ti);
if (spc_util_read_dimtrns(spe, &ti, args, NULL, 0) < 0) {
RELEASE(article_name);
return -1;
}
if ((ti.flags & INFO_HAS_USER_BBOX) &&
((ti.flags & INFO_HAS_WIDTH) || (ti.flags & INFO_HAS_HEIGHT))) {
spc_warn(spe, "You can't specify both bbox and width/height.");
RELEASE(article_name);
return -1;
}
cp.x = spe->x_user; cp.y = spe->y_user;
texpdf_dev_transform(&cp, NULL);
if (ti.flags & INFO_HAS_USER_BBOX) {
rect.llx = ti.bbox.llx + cp.x;
rect.lly = ti.bbox.lly + cp.y;
rect.urx = ti.bbox.urx + cp.x;
rect.ury = ti.bbox.ury + cp.y;
} else {
rect.llx = cp.x;
rect.lly = cp.y - spe->mag * ti.depth;
rect.urx = cp.x + spe->mag * ti.width;
rect.ury = cp.y + spe->mag * ti.height;
}
texpdf_skip_white(&args->curptr, args->endptr);
if (args->curptr[0] != '<') {
article_info = texpdf_new_dict();
} else {
article_info = texpdf_parse_pdf_dict_with_tounicode(&args->curptr, args->endptr, &sd->cd);
if (!article_info) {
spc_warn(spe, "Error in reading dictionary.");
RELEASE(article_name);
return -1;
}
}
/* Does this article exist yet */
article = spc_lookup_object(article_name);
if (article) {
texpdf_merge_dict (article, article_info);
texpdf_release_obj(article_info);
} else {
texpdf_doc_begin_article(pdf, article_name, texpdf_link_obj(article_info));
spc_push_object(article_name, article_info);
}
page_no = texpdf_doc_current_page_number(pdf);
texpdf_doc_add_bead(pdf, article_name, NULL, page_no, &rect);
RELEASE(article_name);
return 0;
}
static int
spc_handler_pdfm_image (struct spc_env *spe, struct spc_arg *args)
{
struct spc_pdf_ *sd = &_pdf_stat;
int xobj_id;
char *ident = NULL;
pdf_obj *fspec, *attr = NULL;
transform_info ti;
long page_no;
texpdf_skip_white(&args->curptr, args->endptr);
if (args->curptr[0] == '@') {
ident = texpdf_parse_opt_ident(&args->curptr, args->endptr);
xobj_id = findresource(sd, ident);
if (xobj_id >= 0) {
spc_warn(spe, "Object reference name for image \"%s\" already used.", ident);
RELEASE(ident);
return -1;
}
}
texpdf_transform_info_clear(&ti);
page_no = 1;
if (spc_util_read_dimtrns(spe, &ti, args, &page_no, 0) < 0) {
if (ident)