forked from aligrudi/neatpost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf.c
1109 lines (1042 loc) · 26.1 KB
/
pdf.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
/* PDF post-processor functions */
#include <ctype.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "post.h"
static char pdf_title[256]; /* document title */
static char pdf_author[256]; /* document author */
static int pdf_width; /* page width */
static int pdf_height; /* page height */
static int pdf_linewid; /* line width in thousands of ems */
static int pdf_linecap = 1; /* line cap style: 0 (butt), 1 (round), 2 (projecting square) */
static int pdf_linejoin = 1; /* line join style: 0 (miter), 1 (round), 2 (bevel) */
static int pdf_pages; /* pages object id */
static int pdf_root; /* root object id */
static int pdf_pos; /* current pdf file offset */
static int *obj_off; /* object offsets */
static int obj_sz, obj_n; /* number of pdf objects */
static int *page_id; /* page object ids */
static int page_sz, page_n; /* number of pages */
static int pdf_outline; /* pdf outline hierarchiy */
static int pdf_dests; /* named destinations */
static struct sbuf *pg; /* current page contents */
static int o_f, o_s, o_m; /* font and size */
static int o_h, o_v; /* current user position */
static int p_h, p_v; /* current output position */
static int o_i, p_i; /* output and pdf fonts (indices into pfont[]) */
static int p_f, p_s, p_m; /* output font */
static int o_queued; /* queued character type */
static char o_iset[1024]; /* fonts accesssed in this page */
static int xobj[128]; /* page xobject object ids */
static int xobj_n; /* number of xobjects in this page */
static int ann[128]; /* page annotations */
static int ann_n; /* number of annotations in this page */
/* loaded PDF fonts */
struct pfont {
char name[128]; /* font PostScript name */
char path[1024]; /* font path */
char desc[1024]; /* font descriptor path */
int gbeg; /* the first glyph */
int gend; /* the last glyph */
int sub; /* subfont number */
int obj; /* the font object */
int des; /* font descriptor */
int cid; /* CID-indexed */
};
static struct pfont *pfonts;
static int pfonts_n, pfonts_sz;
/* print formatted pdf output */
static void pdfout(char *s, ...)
{
va_list ap;
va_start(ap, s);
pdf_pos += vprintf(s, ap);
va_end(ap);
}
/* print pdf output */
static void pdfmem(char *s, int len)
{
fwrite(s, len, 1, stdout);
pdf_pos += len;
}
/* allocate an object number */
static int obj_map(void)
{
if (obj_n == obj_sz) {
obj_sz += 1024;
obj_off = mextend(obj_off, obj_n, obj_sz, sizeof(obj_off[0]));
}
return obj_n++;
}
/* start the definition of an object */
static int obj_beg(int id)
{
if (id <= 0)
id = obj_map();
obj_off[id] = pdf_pos;
pdfout("%d 0 obj\n", id);
return id;
}
/* end an object definition */
static void obj_end(void)
{
pdfout("endobj\n\n");
}
void out(char *s, ...)
{
}
/* the length of the clear-text, encrypted, and fixed-content portions */
static int type1lengths(char *t1, int l, int *l1, int *l2, int *l3)
{
int i;
char *cleartext = t1;
char *encrypted = NULL;
char *fixedcont = NULL;
for (i = 0; i < l - 5 && !encrypted; i++)
if (t1[i] == 'e' && !memcmp("eexec", t1 + i, 5))
encrypted = t1 + i;
if (!encrypted)
return 1;
for (; i < l - 512 && !fixedcont; i++)
if (t1[i] == '0' && !memcmp("00000", t1 + i, 5))
fixedcont = t1 + i;
*l1 = encrypted - cleartext;
*l2 = fixedcont ? fixedcont - cleartext : 0;
*l3 = fixedcont ? t1 + l - fixedcont : 0;
return 0;
}
/* return font type: 't': TrueType, '1': Type 1, 'o': OpenType */
static int fonttype(char *path)
{
char *ext = strrchr(path, '.');
if (ext && !strcmp(".ttf", ext))
return 't';
if (ext && !strcmp(".otf", ext))
return 't';
if (ext && (!strcmp(".ttc", ext) || !strcmp(".otc", ext)))
return 't';
return '1';
}
/* write the object corresponding to the given font */
static void pfont_write(struct pfont *ps)
{
int i;
int enc_obj;
struct font *fn = dev_fontopen(ps->desc);
/* the encoding object */
enc_obj = obj_beg(0);
pdfout("<<\n");
pdfout(" /Type /Encoding\n");
pdfout(" /Differences [ %d", ps->gbeg % 256);
for (i = ps->gbeg; i <= ps->gend; i++)
pdfout(" /%s", font_glget(fn, i)->id);
pdfout(" ]\n");
pdfout(">>\n");
obj_end();
/* the font object */
obj_beg(ps->obj);
pdfout("<<\n");
pdfout(" /Type /Font\n");
if (fonttype(ps->path) == 't')
pdfout(" /Subtype /TrueType\n");
else
pdfout(" /Subtype /Type1\n");
pdfout(" /BaseFont /%s\n", ps->name);
pdfout(" /FirstChar %d\n", ps->gbeg % 256);
pdfout(" /LastChar %d\n", ps->gend % 256);
pdfout(" /Widths [");
for (i = ps->gbeg; i <= ps->gend; i++)
pdfout(" %d", (long) font_glget(fn, i)->wid * 100 * 72 / dev_res);
pdfout(" ]\n");
pdfout(" /FontDescriptor %d 0 R\n", ps->des);
pdfout(" /Encoding %d 0 R\n", enc_obj);
pdfout(">>\n");
obj_end();
font_close(fn);
}
static void encodehex(struct sbuf *d, char *s, int n)
{
static char hex[] = "0123456789ABCDEF";
int i;
for (i = 0; i < n; i++) {
sbuf_chr(d, hex[((unsigned char) s[i]) >> 4]);
sbuf_chr(d, hex[((unsigned char) s[i]) & 0x0f]);
if (i % 40 == 39 && i + 1 < n)
sbuf_chr(d, '\n');
}
sbuf_str(d, ">\n");
}
/* write the object corresponding to this CID font */
static void pfont_writecid(struct pfont *ps)
{
int cid_obj;
struct font *fn = dev_fontopen(ps->desc);
int gcnt = 0;
int i;
/* CIDFont */
cid_obj = obj_beg(0);
pdfout("<<\n");
pdfout(" /Type /Font\n");
pdfout(" /Subtype /CIDFontType2\n");
pdfout(" /BaseFont /%s\n", ps->name);
pdfout(" /CIDSystemInfo <</Ordering(Identity)/Registry(Adobe)/Supplement 0>>\n");
pdfout(" /FontDescriptor %d 0 R\n", ps->des);
pdfout(" /DW 1000\n");
while (font_glget(fn, gcnt))
gcnt++;
pdfout(" /W [ %d [", ps->gbeg);
for (i = ps->gbeg; i <= ps->gend; i++)
pdfout(" %d", (long) font_glget(fn, i)->wid * 100 * 72 / dev_res);
pdfout(" ] ]\n");
pdfout(">>\n");
obj_end();
/* the font object */
obj_beg(ps->obj);
pdfout("<<\n");
pdfout(" /Type /Font\n");
pdfout(" /Subtype /Type0\n");
pdfout(" /BaseFont /%s\n", ps->name);
pdfout(" /Encoding /Identity-H\n");
pdfout(" /DescendantFonts [%d 0 R]\n", cid_obj);
pdfout(">>\n");
obj_end();
font_close(fn);
}
/* write font descriptor; returns its object ID */
static int writedesc(struct font *fn)
{
int str_obj = -1;
int des_obj;
char buf[1 << 10];
int fntype = fonttype(font_path(fn));
if (fntype == '1' || fntype == 't') {
int fd = open(font_path(fn), O_RDONLY);
struct sbuf *ffsb = sbuf_make();
struct sbuf *sb = sbuf_make();
int l1 = 0, l2 = 0, l3 = 0;
int nr;
/* reading the font file */
while ((nr = read(fd, buf, sizeof(buf))) > 0)
sbuf_mem(ffsb, buf, nr);
close(fd);
l1 = sbuf_len(ffsb);
/* initialize Type 1 lengths */
if (fntype == '1') {
if (type1lengths(sbuf_buf(ffsb), sbuf_len(ffsb),
&l1, &l2, &l3))
l1 = 0;
/* remove the fixed-content portion of the font */
if (l3)
sbuf_cut(ffsb, l1 + l2);
l1 -= l3;
l3 = 0;
}
/* encoding file contents */
encodehex(sb, sbuf_buf(ffsb), sbuf_len(ffsb));
/* write font data if it has nonzero length */
if (l1) {
str_obj = obj_beg(0);
pdfout("<<\n");
pdfout(" /Filter /ASCIIHexDecode\n");
pdfout(" /Length %d\n", sbuf_len(sb));
pdfout(" /Length1 %d\n", l1);
if (fntype == '1')
pdfout(" /Length2 %d\n", l2);
if (fntype == '1')
pdfout(" /Length3 %d\n", l3);
pdfout(">>\n");
pdfout("stream\n");
pdfmem(sbuf_buf(sb), sbuf_len(sb));
pdfout("endstream\n");
obj_end();
}
sbuf_free(ffsb);
sbuf_free(sb);
}
/* the font descriptor */
des_obj = obj_beg(0);
pdfout("<<\n");
pdfout(" /Type /FontDescriptor\n");
pdfout(" /FontName /%s\n", font_name(fn));
pdfout(" /Flags 32\n");
pdfout(" /FontBBox [-1000 -1000 1000 1000]\n");
pdfout(" /MissingWidth 1000\n");
pdfout(" /StemV 100\n");
pdfout(" /ItalicAngle 0\n");
pdfout(" /CapHeight 100\n");
pdfout(" /Ascent 100\n");
pdfout(" /Descent 100\n");
if (str_obj >= 0)
pdfout(" /FontFile%s %d 0 R\n",
fntype == 't' ? "2" : "", str_obj);
pdfout(">>\n");
obj_end();
return des_obj;
}
static int pfont_find(struct glyph *g)
{
struct font *fn = g->font;
char *name = font_name(fn);
struct pfont *ps = NULL;
int fntype = fonttype(font_path(fn));
int sub = fntype == '1' ? font_glnum(fn, g) / 256 : 0;
int i;
for (i = 0; i < pfonts_n; i++)
if (!strcmp(name, pfonts[i].name) && pfonts[i].sub == sub)
return i;
if (pfonts_n == pfonts_sz) {
pfonts_sz += 16;
pfonts = mextend(pfonts, pfonts_n,
pfonts_sz, sizeof(pfonts[0]));
}
ps = &pfonts[pfonts_n];
snprintf(ps->name, sizeof(ps->name), "%s", name);
snprintf(ps->path, sizeof(ps->path), "%s", font_path(fn));
snprintf(ps->desc, sizeof(ps->desc), "%s", font_desc(fn));
ps->cid = fntype == 't';
ps->obj = obj_map();
ps->sub = sub;
ps->gbeg = 1 << 20;
for (i = 0; i < pfonts_n; i++)
if (!strcmp(pfonts[i].name, ps->name))
break;
if (i < pfonts_n)
ps->des = pfonts[i].des;
else
ps->des = writedesc(fn);
return pfonts_n++;
}
static void pfont_done(void)
{
int i;
for (i = 0; i < pfonts_n; i++) {
if (pfonts[i].cid)
pfont_writecid(&pfonts[i]);
else
pfont_write(&pfonts[i]);
}
free(pfonts);
}
static void o_flush(void)
{
if (o_queued == 1)
sbuf_printf(pg, ">] TJ\n");
o_queued = 0;
}
static int o_loadfont(struct glyph *g)
{
int fn = pfont_find(g);
o_iset[fn] = 1;
return fn;
}
/* like pdfpos() but assume that uh and uv are multiplied by 100 */
static char *pdfpos00(int uh, int uv)
{
static char buf[64];
int h = (long) uh * 72 / dev_res;
int v = (long) pdf_height * 100 - (long) uv * 72 / dev_res;
sprintf(buf, "%s%d.%02d %s%d.%02d",
h < 0 ? "-" : "", abs(h) / 100, abs(h) % 100,
v < 0 ? "-" : "", abs(v) / 100, abs(v) % 100);
return buf;
}
/* convert troff position to pdf position; returns a static buffer */
static char *pdfpos(int uh, int uv)
{
return pdfpos00(uh * 100, uv * 100);
}
/* troff length to thousands of a unit of text space; returns a static buffer */
static char *pdfunit(int uh, int sz)
{
static char buf[64];
int h = (long) uh * 1000 * 72 / sz / dev_res;
sprintf(buf, "%s%d", h < 0 ? "-" : "", abs(h));
return buf;
}
/* convert troff color to pdf color; returns a static buffer */
static char *pdfcolor(int m)
{
static char buf[64];
int r = CLR_R(m) * 1000 / 255;
int g = CLR_G(m) * 1000 / 255;
int b = CLR_B(m) * 1000 / 255;
sbuf_printf(pg, "%d.%03d %d.%03d %d.%03d",
r / 1000, r % 1000, g / 1000, g % 1000, b / 1000, b % 1000);
return buf;
}
static void o_queue(struct glyph *g)
{
int gid;
if (o_v != p_v) {
o_flush();
sbuf_printf(pg, "1 0 0 1 %s Tm\n", pdfpos(o_h, o_v));
p_h = o_h;
p_v = o_v;
}
if (!o_queued)
sbuf_printf(pg, "[<");
o_queued = 1;
if (o_h != p_h)
sbuf_printf(pg, "> %s <", pdfunit(p_h - o_h, o_s));
/* printing glyph identifier */
gid = font_glnum(g->font, g);
if (pfonts[o_i].cid)
sbuf_printf(pg, "%04x", gid);
else
sbuf_printf(pg, "%02x", gid % 256);
/* updating gbeg and gend */
if (gid < pfonts[o_i].gbeg)
pfonts[o_i].gbeg = gid;
if (gid > pfonts[o_i].gend)
pfonts[o_i].gend = gid;
/* advancing */
p_h = o_h + font_wid(g->font, o_s, g->wid);
}
static void out_fontup(void)
{
if (o_m != p_m) {
o_flush();
sbuf_printf(pg, "%s rg\n", pdfcolor(o_m));
p_m = o_m;
}
if (o_i >= 0 && (o_i != p_i || o_s != p_s)) {
struct pfont *ps = &pfonts[o_i];
o_flush();
if (ps->cid)
sbuf_printf(pg, "/%s %d Tf\n", ps->name, o_s);
else
sbuf_printf(pg, "/%s.%d %d Tf\n", ps->name, ps->sub, o_s);
p_i = o_i;
p_s = o_s;
}
}
void outc(char *c)
{
struct glyph *g;
struct font *fn;
g = dev_glyph(c, o_f);
fn = g ? g->font : dev_font(o_f);
if (!g) {
outrel(*c == ' ' && fn ? font_swid(fn, o_s) : 1, 0);
return;
}
o_i = o_loadfont(g);
out_fontup();
o_queue(g);
}
void outh(int h)
{
o_h = h;
}
void outv(int v)
{
o_v = v;
}
void outrel(int h, int v)
{
o_h += h;
o_v += v;
}
void outfont(int f)
{
if (dev_font(f))
o_f = f;
}
void outsize(int s)
{
if (s > 0)
o_s = s;
}
void outcolor(int c)
{
o_m = c;
}
void outrotate(int deg)
{
}
void outeps(char *eps, int hwid, int vwid)
{
}
/* return a copy of a PDF object; returns a static buffer */
static char *pdf_copy(char *pdf, int len, int pos)
{
static char buf[1 << 12];
int datlen;
pos += pdf_ws(pdf, len, pos);
datlen = pdf_len(pdf, len, pos);
if (datlen > sizeof(buf) - 1)
datlen = sizeof(buf) - 1;
memcpy(buf, pdf + pos, datlen);
buf[datlen] = '\0';
return buf;
}
static void pdf_dictcopy(char *pdf, int len, int pos, struct sbuf *sb);
/* write stream to sb */
static int pdf_strcopy(char *pdf, int len, int pos, struct sbuf *sb)
{
int slen, val;
int beg;
if ((val = pdf_dval_val(pdf, len, pos, "/Length")) < 0)
return -1;
slen = atoi(pdf + val);
pos = pos + pdf_len(pdf, len, pos);
pos += pdf_ws(pdf, len, pos);
if (pos + slen + 15 > len)
return -1;
beg = pos;
pos += strlen("stream");
if (pdf[pos] == '\r')
pos++;
pos += 1 + slen;
if (pdf[pos] == '\r' || pdf[pos] == ' ')
pos++;
if (pdf[pos] == '\n')
pos++;
pos += strlen("endstream") + 1;
sbuf_mem(sb, pdf + beg, pos - beg);
return 0;
}
/* copy a PDF object and return its new identifier */
static int pdf_objcopy(char *pdf, int len, int pos)
{
int id;
if ((pos = pdf_ref(pdf, len, pos)) < 0)
return -1;
if (pdf_type(pdf, len, pos) == 'd') {
struct sbuf *sb = sbuf_make();
pdf_dictcopy(pdf, len, pos, sb);
sbuf_chr(sb, '\n');
if (pdf_dval(pdf, len, pos, "/Length") >= 0)
pdf_strcopy(pdf, len, pos, sb);
id = obj_beg(0);
pdfmem(sbuf_buf(sb), sbuf_len(sb));
obj_end();
sbuf_free(sb);
} else {
id = obj_beg(0);
pdfmem(pdf + pos, pdf_len(pdf, len, pos));
pdfout("\n");
obj_end();
}
return id;
}
/* copy a PDF dictionary recursively */
static void pdf_dictcopy(char *pdf, int len, int pos, struct sbuf *sb)
{
int i;
int key, val, id;
sbuf_printf(sb, "<<");
for (i = 0; ; i++) {
if ((key = pdf_dkey(pdf, len, pos, i)) < 0)
break;
sbuf_printf(sb, " %s", pdf_copy(pdf, len, key));
val = pdf_dval(pdf, len, pos, pdf_copy(pdf, len, key));
if (pdf_type(pdf, len, val) == 'r') {
if ((id = pdf_objcopy(pdf, len, val)) >= 0)
sbuf_printf(sb, " %d 0 R", id);
} else {
sbuf_printf(sb, " %s", pdf_copy(pdf, len, val));
}
}
sbuf_printf(sb, " >>");
}
/* copy resources dictionary */
static void pdf_rescopy(char *pdf, int len, int pos, struct sbuf *sb)
{
char *res_fields[] = {"/ProcSet", "/ExtGState", "/ColorSpace",
"/Pattern", "/Shading", "/Properties", "/Font", "/XObject"};
int res, i;
sbuf_printf(sb, " /Resources <<\n");
for (i = 0; i < LEN(res_fields); i++) {
if ((res = pdf_dval_val(pdf, len, pos, res_fields[i])) >= 0) {
if (pdf_type(pdf, len, res) == 'd') {
sbuf_printf(sb, " %s ", res_fields[i]);
pdf_dictcopy(pdf, len, res, sb);
sbuf_printf(sb, "\n");
} else {
sbuf_printf(sb, " %s %s\n", res_fields[i],
pdf_copy(pdf, len, res));
}
}
}
sbuf_printf(sb, " >>\n");
}
static int pdfbbox100(char *pdf, int len, int pos, int dim[4])
{
int val;
int i;
for (i = 0; i < 4; i++) {
int n = 0, f1 = 0, f2 = 0;
if ((val = pdf_lval(pdf, len, pos, i)) < 0)
return -1;
for (; isdigit((unsigned char) pdf[val]); val++)
n = n * 10 + pdf[val] - '0';
if (pdf[val] == '.') {
if (isdigit((unsigned char) pdf[val + 1])) {
f1 = pdf[val + 1] - '0';
if (isdigit((unsigned char) pdf[val + 2]))
f2 = pdf[val + 2] - '0';
}
}
dim[i] = n * 100 + f1 * 10 + f2;
}
return 0;
}
static int pdfext(char *pdf, int len, int hwid, int vwid)
{
char *cont_fields[] = {"/Filter", "/DecodeParms"};
int trailer, root, cont, pages, page1, res;
int kids_val, page1_val, val, bbox;
int xobj_id, length;
int dim[4];
int hzoom = 100, vzoom = 100;
struct sbuf *sb;
int i;
if (xobj_n == LEN(xobj))
return -1;
if ((trailer = pdf_trailer(pdf, len)) < 0)
return -1;
if ((root = pdf_dval_obj(pdf, len, trailer, "/Root")) < 0)
return -1;
if ((pages = pdf_dval_obj(pdf, len, root, "/Pages")) < 0)
return -1;
if ((kids_val = pdf_dval_val(pdf, len, pages, "/Kids")) < 0)
return -1;
if ((page1_val = pdf_lval(pdf, len, kids_val, 0)) < 0)
return -1;
if ((page1 = pdf_ref(pdf, len, page1_val)) < 0)
return -1;
if ((cont = pdf_dval_obj(pdf, len, page1, "/Contents")) < 0)
return -1;
if ((val = pdf_dval_val(pdf, len, cont, "/Length")) < 0)
return -1;
res = pdf_dval_val(pdf, len, page1, "/Resources");
length = atoi(pdf + val);
bbox = pdf_dval_val(pdf, len, page1, "/MediaBox");
if (bbox < 0)
bbox = pdf_dval_val(pdf, len, pages, "/MediaBox");
if (bbox >= 0 && !pdfbbox100(pdf, len, bbox, dim)) {
if (hwid > 0)
hzoom = (long) hwid * (100 * 7200 / dev_res) / (dim[2] - dim[0]);
if (vwid > 0)
vzoom = (long) vwid * (100 * 7200 / dev_res) / (dim[3] - dim[1]);
if (vwid <= 0)
vzoom = hzoom;
if (hwid <= 0)
hzoom = vzoom;
}
sb = sbuf_make();
sbuf_printf(sb, "<<\n");
sbuf_printf(sb, " /Type /XObject\n");
sbuf_printf(sb, " /Subtype /Form\n");
sbuf_printf(sb, " /FormType 1\n");
if (bbox >= 0)
sbuf_printf(sb, " /BBox %s\n", pdf_copy(pdf, len, bbox));
sbuf_printf(sb, " /Matrix [%d.%02d 0 0 %d.%02d %s]\n",
hzoom / 100, hzoom % 100, vzoom / 100, vzoom % 100,
pdfpos(o_h, o_v));
if (res >= 0)
pdf_rescopy(pdf, len, res, sb);
sbuf_printf(sb, " /Length %d\n", length);
for (i = 0; i < LEN(cont_fields); i++)
if ((val = pdf_dval_val(pdf, len, cont, cont_fields[i])) >= 0)
sbuf_printf(sb, " %s %s\n", cont_fields[i],
pdf_copy(pdf, len, val));
sbuf_printf(sb, ">>\n");
pdf_strcopy(pdf, len, cont, sb);
xobj_id = obj_beg(0);
pdfmem(sbuf_buf(sb), sbuf_len(sb));
obj_end();
sbuf_free(sb);
xobj[xobj_n++] = xobj_id;
return xobj_n - 1;
}
void outpdf(char *pdf, int hwid, int vwid)
{
char buf[1 << 12];
struct sbuf *sb;
int xobj_id;
int fd, nr;
/* reading the pdf file */
sb = sbuf_make();
fd = open(pdf, O_RDONLY);
while ((nr = read(fd, buf, sizeof(buf))) > 0)
sbuf_mem(sb, buf, nr);
close(fd);
/* the XObject */
xobj_id = pdfext(sbuf_buf(sb), sbuf_len(sb), hwid, vwid);
sbuf_free(sb);
o_flush();
out_fontup();
if (xobj_id >= 0)
sbuf_printf(pg, "ET /FO%d Do BT\n", xobj_id);
p_h = -1;
p_v = -1;
}
void outlink(char *lnk, int hwid, int vwid)
{
if (ann_n == LEN(ann))
return;
o_flush();
ann[ann_n++] = obj_beg(0);
pdfout("<<\n");
pdfout(" /Type /Annot\n");
pdfout(" /Subtype /Link\n");
pdfout(" /Rect [%s", pdfpos(o_h, o_v));
pdfout(" %s]\n", pdfpos(o_h + hwid, o_v + vwid));
if (lnk[0] == '#') { /* internal links */
pdfout(" /A << /S /GoTo /D (%s) >>\n", lnk + 1);
} else { /* external links */
pdfout(" /A << /S /URI /URI %s >>\n", pdftext_static(lnk));
}
pdfout(">>\n");
obj_end();
}
void outname(int n, char (*desc)[64], int *page, int *off)
{
int i;
o_flush();
pdf_dests = obj_beg(0);
pdfout("<<\n");
for (i = 0; i < n; i++) {
if (page[i] > 0 && page[i] - 1 < page_n)
pdfout(" /%s [ %d 0 R /XYZ 0 %d 0 ]\n",
desc[i], page_id[page[i] - 1],
pdf_height - (off[i] * 72 / dev_res));
}
pdfout(">>\n");
obj_end();
}
void outmark(int n, char (*desc)[256], int *page, int *off, int *level)
{
int *objs = malloc(n * sizeof(objs[0]));
int i, j;
int cnt = 0;
/* allocating objects */
pdf_outline = obj_map();
for (i = 0; i < n; i++)
objs[i] = obj_map();
o_flush();
/* root object */
obj_beg(pdf_outline);
pdfout("<<\n");
for (i = 0; i < n; i++)
if (level[i] == level[0])
cnt++;
pdfout(" /Count %d\n", cnt);
pdfout(" /First %d 0 R\n", objs[0]);
for (i = n - 1; i > 0 && level[i] > level[0]; i--)
;
pdfout(" /Last %d 0 R\n", objs[i]);
pdfout(">>\n");
obj_end();
/* other objects */
for (i = 0; i < n; i++) {
int cnt = 0;
for (j = i + 1; j < n && level[j] > level[i]; j++)
if (level[j] == level[i] + 1)
cnt++;
obj_beg(objs[i]);
pdfout("<<\n");
pdfout(" /Title %s\n", pdftext_static(desc[i]));
/* the parent field */
for (j = i - 1; j >= 0 && level[j] >= level[i]; j--)
;
pdfout(" /Parent %d 0 R\n", j >= 0 ? objs[j] : pdf_outline);
/* the next field */
for (j = i + 1; j < n && level[j] > level[i]; j++)
;
if (j < n && level[j] == level[i])
pdfout(" /Next %d 0 R\n", objs[j]);
/* the prev field */
for (j = i - 1; j >= 0 && level[j] > level[i]; j--)
;
if (j >= 0 && level[j] == level[i])
pdfout(" /Prev %d 0 R\n", objs[j]);
/* node children */
if (cnt) {
int last = 0;
pdfout(" /Count %d\n", cnt);
pdfout(" /First %d 0 R\n", objs[i + 1]);
for (j = i + 1; j < n && level[j] > level[i]; j++)
if (level[j] == level[i] + 1)
last = j;
pdfout(" /Last %d 0 R\n", objs[last]);
}
if (page[i] > 0 && page[i] - 1 < page_n)
pdfout(" /Dest [ %d 0 R /XYZ 0 %d 0 ]\n",
page_id[page[i] - 1],
pdf_height - (off[i] * 72 / dev_res));
pdfout(">>\n");
obj_end();
}
free(objs);
}
void outinfo(char *kwd, char *val)
{
if (!strcmp("Author", kwd))
snprintf(pdf_author, sizeof(pdf_author), "%s", val);
if (!strcmp("Title", kwd))
snprintf(pdf_title, sizeof(pdf_title), "%s", val);
}
void outset(char *var, char *val)
{
if (!strcmp("linewidth", var))
pdf_linewid = atoi(val);
if (!strcmp("linecap", var))
pdf_linecap = atoi(val);
if (!strcmp("linejoin", var))
pdf_linejoin = atoi(val);
}
void outpage(void)
{
o_v = 0;
o_h = 0;
p_i = 0;
p_v = 0;
p_h = 0;
p_s = 0;
p_f = 0;
p_m = 0;
o_i = -1;
}
void outmnt(int f)
{
if (p_f == f)
p_f = -1;
}
void outgname(int g)
{
}
void drawbeg(void)
{
o_flush();
out_fontup();
sbuf_printf(pg, "%s m\n", pdfpos(o_h, o_v));
}
static int l_page, l_size, l_wid, l_cap, l_join; /* drawing line properties */
void drawend(int close, int fill)
{
fill = !fill ? 2 : fill;
if (l_page != page_n || l_size != o_s || l_wid != pdf_linewid ||
l_cap != pdf_linecap || l_join != pdf_linejoin) {
int lwid = pdf_linewid * o_s;
sbuf_printf(pg, "%d.%03d w\n", lwid / 1000, lwid % 1000);
sbuf_printf(pg, "%d J %d j\n", pdf_linecap, pdf_linejoin);
l_page = page_n;
l_size = o_s;
l_wid = pdf_linewid;
l_cap = pdf_linecap;
l_join = pdf_linejoin;
}
if (fill & 2) /* stroking color */
sbuf_printf(pg, "%s RG\n", pdfcolor(o_m));
if (fill & 1)
sbuf_printf(pg, (fill & 2) ? "b\n" : "f\n");
else
sbuf_printf(pg, close ? "s\n" : "S\n");
p_v = 0;
p_h = 0;
}
void drawmbeg(char *s)
{
}
void drawmend(char *s)
{
}
void drawl(int h, int v)
{
outrel(h, v);
sbuf_printf(pg, "%s l\n", pdfpos(o_h, o_v));
}
/* draw circle/ellipse quadrant */
static void drawquad(int ch, int cv)
{
long b = 551915;
long x0 = o_h * 1000;
long y0 = o_v * 1000;
long x3 = x0 + ch * 1000 / 2;
long y3 = y0 + cv * 1000 / 2;
long x1 = x0;
long y1 = y0 + cv * b / 1000 / 2;
long x2 = x0 + ch * b / 1000 / 2;
long y2 = y3;
if (ch * cv < 0) {
x1 = x3 - ch * b / 1000 / 2;
y1 = y0;
x2 = x3;
y2 = y3 - cv * b / 1000 / 2;
}
sbuf_printf(pg, "%s ", pdfpos00(x1 / 10, y1 / 10));
sbuf_printf(pg, "%s ", pdfpos00(x2 / 10, y2 / 10));
sbuf_printf(pg, "%s c\n", pdfpos00(x3 / 10, y3 / 10));
outrel(ch / 2, cv / 2);
}
/* draw a circle */
void drawc(int c)
{
drawquad(+c, +c);
drawquad(+c, -c);
drawquad(-c, -c);
drawquad(-c, +c);
outrel(c, 0);
}
/* draw an ellipse */
void drawe(int h, int v)
{
drawquad(+h, +v);
drawquad(+h, -v);
drawquad(-h, -v);
drawquad(-h, +v);
outrel(h, 0);
}
/* draw an arc */
void drawa(int h1, int v1, int h2, int v2)
{
drawl(h1 + h2, v1 + v2);
}
/* draw an spline */
void draws(int h1, int v1, int h2, int v2)
{
outrel(h1, v1);
sbuf_printf(pg, "%s l\n", pdfpos(o_h, o_v));
}
void docheader(char *title, int pagewidth, int pageheight, int linewidth)
{
if (title)
outinfo("Title", title);
obj_map();
pdf_root = obj_map();
pdf_pages = obj_map();
pdfout("%%PDF-1.6\n\n");
pdf_width = (pagewidth * 72 + 127) / 254;
pdf_height = (pageheight * 72 + 127) / 254;
pdf_linewid = linewidth;
}
void doctrailer(int pages)
{
int i;
int xref_off;
int info_id;
/* pdf pages object */
obj_beg(pdf_pages);
pdfout("<<\n");
pdfout(" /Type /Pages\n");
pdfout(" /MediaBox [ 0 0 %d %d ]\n", pdf_width, pdf_height);
pdfout(" /Count %d\n", page_n);
pdfout(" /Kids [");
for (i = 0; i < page_n; i++)
pdfout(" %d 0 R", page_id[i]);
pdfout(" ]\n");
pdfout(">>\n");