-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpdf.c
1148 lines (1012 loc) · 30.9 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.c
* Copyright (C) 2003-2005 A.J. van Os; Released under GNU GPL
*
* Description:
* Functions to deal with the Adobe Portable Document Format (pdf)
*
*/
#include <stdarg.h>
#include <string.h>
#include "version.h"
#include "antiword.h"
/* Constants for the file positions */
#define INITIAL_LOCATION_SIZE 20
#define INITIAL_PAGEOBJECT_SIZE 5
#if defined(DEBUG)
#define EXTENSION_ARRAY_SIZE 10
#else
#define EXTENSION_ARRAY_SIZE 30
#endif /* DEBUG */
/* The character set */
static encoding_type eEncoding = encoding_neutral;
/* Current creator for a PDF header */
static const char *szProducer = NULL;
/* The height and width of a PDF page (in DrawUnits) */
static long lPageHeight = LONG_MAX;
static long lPageWidth = LONG_MAX;
/* The height of the footer on the current page (in DrawUnits) */
static long lFooterHeight = 0;
/* Inside a footer (to prevent an infinite loop when the footer is too big) */
static BOOL bInFtrSpace = FALSE;
/* Current font information */
static drawfile_fontref tFontRefCurr = (drawfile_fontref)-1;
static USHORT usFontSizeCurr = 0;
static int iFontColorCurr = -1;
/* Current vertical position information */
static long lYtopCurr = -1;
/* Image counter */
static int iImageCount = 0;
/* Section index */
static int iSectionIndex = 0;
/* Are we on the first page of the section? */
static BOOL bFirstInSection = TRUE;
/* File positions */
static long lFilePosition = 0;
static long *alLocation = NULL;
static size_t tLocations = 0;
static int iMaxLocationNumber = 0;
/* File position at the start of a page */
static long lStreamStart = -1;
/* Page objects */
static int *aiPageObject = NULL;
static int iPageCount = 0;
static size_t tMaxPageObjects = 0;
/* Current object number */
/* 1 = root; 2 = info; 3 = pages; 4 = encoding; 5-16 = fonts; 17 = resources */
static int iObjectNumberCurr = 17;
static void vMoveTo(diagram_type *, long);
static const struct {
const char *szPDFname;
const char *szPSname;
} atFontname[] = {
{ "Courier", FONT_MONOSPACED_PLAIN },
{ "Courier-Bold", FONT_MONOSPACED_BOLD },
{ "Courier-Oblique", FONT_MONOSPACED_ITALIC },
{ "Courier-BoldOblique", FONT_MONOSPACED_BOLDITALIC },
{ "Helvetica", FONT_SANS_SERIF_PLAIN },
{ "Helvetica-Bold", FONT_SANS_SERIF_BOLD },
{ "Helvetica-Oblique", FONT_SANS_SERIF_ITALIC },
{ "Helvetica-BoldOblique", FONT_SANS_SERIF_BOLDITALIC },
{ "Times-Roman", FONT_SERIF_PLAIN },
{ "Times-Bold", FONT_SERIF_BOLD },
{ "Times-Italic", FONT_SERIF_ITALIC },
{ "Times-BoldItalic", FONT_SERIF_BOLDITALIC },
};
static const char *iso_8859_1[] = {
"128 /Euro",
"140 /ellipsis /trademark /perthousand /bullet",
" /quoteleft /quoteright /guilsinglleft /guilsinglright",
" /quotedblleft /quotedblright /quotedblbase /endash /emdash",
" /minus /OE /oe /dagger /daggerdbl /fi /fl",
"160 /space /exclamdown /cent /sterling /currency",
" /yen /brokenbar /section /dieresis /copyright",
" /ordfeminine /guillemotleft /logicalnot /hyphen /registered",
" /macron /degree /plusminus /twosuperior /threesuperior",
" /acute /mu /paragraph /periodcentered /cedilla",
" /onesuperior /ordmasculine /guillemotright /onequarter",
" /onehalf /threequarters /questiondown /Agrave /Aacute",
" /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla",
" /Egrave /Eacute /Ecircumflex /Edieresis /Igrave /Iacute",
" /Icircumflex /Idieresis /Eth /Ntilde /Ograve /Oacute",
" /Ocircumflex /Otilde /Odieresis /multiply /Oslash",
" /Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn",
" /germandbls /agrave /aacute /acircumflex /atilde",
" /adieresis /aring /ae /ccedilla /egrave /eacute",
" /ecircumflex /edieresis /igrave /iacute /icircumflex",
" /idieresis /eth /ntilde /ograve /oacute /ocircumflex",
" /otilde /odieresis /divide /oslash /ugrave /uacute",
" /ucircumflex /udieresis /yacute /thorn /ydieresis",
};
static const char *iso_8859_2[] = {
"160 /space /Aogonek /breve /Lslash /currency /Lcaron",
" /Sacute /section /dieresis /Scaron /Scommaaccent",
" /Tcaron /Zacute /hyphen /Zcaron /Zdotaccent /degree",
" /aogonek /ogonek /lslash /acute /lcaron /sacute",
" /caron /cedilla /scaron /scommaaccent /tcaron",
" /zacute /hungarumlaut /zcaron /zdotaccent /Racute",
" /Aacute /Acircumflex /Abreve /Adieresis /Lacute",
" /Cacute /Ccedilla /Ccaron /Eacute /Eogonek",
" /Edieresis /Ecaron /Iacute /Icircumflex /Dcaron",
" /.notdef /Nacute /Ncaron /Oacute /Ocircumflex",
" /Ohungarumlaut /Odieresis /multiply /Rcaron /Uring",
" /Uacute /Uhungarumlaut /Udieresis /Yacute /Tcommaaccent",
" /germandbls /racute /aacute /acircumflex /abreve",
" /adieresis /lacute /cacute /ccedilla /ccaron /eacute",
" /eogonek /edieresis /ecaron /iacute /icircumflex",
" /dcaron /.notdef /nacute /ncaron /oacute /ocircumflex",
" /ohungarumlaut /odieresis /divide /rcaron /uring",
" /uacute /uhungarumlaut /udieresis /yacute /tcommaaccent",
" /dotaccent",
};
/*
* tGetFontIndex - get the font index
*/
static size_t
tGetFontIndex(drawfile_fontref tFontRef)
{
const char *szFontname;
size_t tIndex;
/* Get the font name */
szFontname = szGetFontname(tFontRef);
fail(szFontname == NULL);
if (szFontname == NULL) {
return 0;
}
/* Find the name in the table */
for (tIndex = 0; tIndex < elementsof(atFontname); tIndex++) {
if (STRCEQ(atFontname[tIndex].szPSname, szFontname)) {
return tIndex;
}
}
/* Not found */
DBG_DEC(tFontRef);
DBG_MSG(szFontname);
return 0;
} /* end of tGetFontIndex */
/*
* vSetLocation - store the location of objects
*/
static void
vSetLocation(int iLocationNumber)
{
fail(iLocationNumber <= 0);
if ((size_t)iLocationNumber >= tLocations) {
/* Extend and set to zero */
tLocations += EXTENSION_ARRAY_SIZE;
alLocation = xrealloc(alLocation, tLocations * sizeof(long));
memset(alLocation + tLocations - EXTENSION_ARRAY_SIZE,
0,
EXTENSION_ARRAY_SIZE * sizeof(long));
DBG_DEC(tLocations);
}
if (iLocationNumber > iMaxLocationNumber) {
iMaxLocationNumber = iLocationNumber;
}
DBG_DEC_C((size_t)iLocationNumber >= tLocations, iLocationNumber);
DBG_DEC_C((size_t)iLocationNumber >= tLocations, tLocations);
fail((size_t)iLocationNumber >= tLocations);
alLocation[iLocationNumber] = lFilePosition;
} /* end of vSetLocation */
/*
* vFillNextPageObject - fil the next page object with the current object number
*/
static void
vFillNextPageObject(void)
{
iPageCount++;
if ((size_t)iPageCount >= tMaxPageObjects) {
/* Extend the array */
tMaxPageObjects += EXTENSION_ARRAY_SIZE;
aiPageObject = xrealloc(aiPageObject,
tMaxPageObjects * sizeof(int));
DBG_DEC(tMaxPageObjects);
}
aiPageObject[iPageCount] = iObjectNumberCurr;
} /* end of vFillNextPageObject */
/*
* vFPprintf - printf and update the fileposition
*
* called with arguments like fprintf(3)
*/
static void
vFPprintf(FILE *pOutFile, const char *szFormat, ...)
{
va_list tArg;
va_start(tArg, szFormat);
lFilePosition += vfprintf(pOutFile, szFormat, tArg);
va_end(tArg);
} /* end of vFPprintf */
/*
* vCreateInfoDictionary - create the document information dictionary
*/
void
vCreateInfoDictionary(diagram_type *pDiag, int iWordVersion)
{
FILE *pOutFile;
const char *szTitle, *szAuthor, *szSubject, *szCreator;
const char *szCreationDate, *szModDate;
fail(pDiag == NULL);
fail(pDiag->pOutFile == NULL);
fail(iWordVersion < 0);
fail(szProducer == NULL || szProducer[0] == '\0');
szTitle = szGetTitle();
szAuthor = szGetAuthor();
szSubject = szGetSubject();
szCreationDate = szGetCreationDate();
szModDate = szGetModDate();
switch (iWordVersion) {
case 0: szCreator = "Word for DOS"; break;
case 1: szCreator = "WinWord 1.x"; break;
case 2: szCreator = "WinWord 2.0"; break;
case 4: szCreator = "MacWord 4"; break;
case 5: szCreator = "MacWord 5"; break;
case 6: szCreator = "Word 6"; break;
case 7: szCreator = "Word 7/95"; break;
case 8: szCreator = "Word 97 or later"; break;
default: szCreator = NULL; break;
}
pOutFile = pDiag->pOutFile;
vSetLocation(2);
vFPprintf(pOutFile, "2 0 obj\n");
vFPprintf(pOutFile, "<<\n");
if (szTitle != NULL && szTitle[0] != '\0') {
vFPprintf(pOutFile, "/Title (%s)\n", szTitle);
}
if (szAuthor != NULL && szAuthor[0] != '\0') {
vFPprintf(pOutFile, "/Author (%s)\n", szAuthor);
}
if (szSubject != NULL && szSubject[0] != '\0') {
vFPprintf(pOutFile, "/Subject (%s)\n", szSubject);
}
if (szCreator != NULL && szCreator[0] != '\0') {
vFPprintf(pOutFile, "/Creator (%s)\n", szCreator);
}
vFPprintf(pOutFile, "/Producer (%s %s)\n", szProducer, VERSIONSTRING);
if (szCreationDate != NULL && szCreationDate[0] != '\0') {
vFPprintf(pOutFile, "/CreationDate (%s)\n", szCreationDate);
}
if (szModDate != NULL && szModDate[0] != '\0') {
vFPprintf(pOutFile, "/ModDate (%s)\n", szModDate);
}
vFPprintf(pOutFile, ">>\n");
vFPprintf(pOutFile, "endobj\n");
} /* end of vCreateInfoDictionary */
/*
* vAddHdrFtr - add a header or footer
*/
static void
vAddHdrFtr(diagram_type *pDiag, const hdrftr_block_type *pHdrFtrInfo)
{
output_type *pStart, *pPrev, *pNext;
fail(pDiag == NULL);
fail(pHdrFtrInfo == NULL);
vStartOfParagraphPDF(pDiag, 0);
pStart = pHdrFtrInfo->pText;
while (pStart != NULL) {
pNext = pStart;
while (pNext != NULL &&
(pNext->tNextFree != 1 ||
(pNext->szStorage[0] != PAR_END &&
pNext->szStorage[0] != HARD_RETURN))) {
pNext = pNext->pNext;
}
if (pNext == NULL) {
if (bOutputContainsText(pStart)) {
vAlign2Window(pDiag, pStart,
lChar2MilliPoints(DEFAULT_SCREEN_WIDTH),
ALIGNMENT_LEFT);
} else {
vMove2NextLinePDF(pDiag, pStart->usFontSize);
}
break;
}
fail(pNext->tNextFree != 1);
fail(pNext->szStorage[0] != PAR_END &&
pNext->szStorage[0] != HARD_RETURN);
if (pStart != pNext) {
/* There is something to print */
pPrev = pNext->pPrev;
fail(pPrev->pNext != pNext);
/* Cut the chain */
pPrev->pNext = NULL;
if (bOutputContainsText(pStart)) {
/* Print it */
vAlign2Window(pDiag, pStart,
lChar2MilliPoints(DEFAULT_SCREEN_WIDTH),
ALIGNMENT_LEFT);
} else {
/* Just an empty line */
vMove2NextLinePDF(pDiag, pStart->usFontSize);
}
/* Repair the chain */
pPrev->pNext = pNext;
}
if (pNext->szStorage[0] == PAR_END) {
vEndOfParagraphPDF(pDiag, pNext->usFontSize,
(long)pNext->usFontSize * 200);
}
pStart = pNext->pNext;
}
} /* end of vAddHdrFtr */
/*
* vAddHeader - add a page header
*/
static void
vAddHeader(diagram_type *pDiag)
{
const hdrftr_block_type *pHdrInfo;
const hdrftr_block_type *pFtrInfo;
fail(pDiag == NULL);
NO_DBG_MSG("vAddHeader");
pHdrInfo = pGetHdrFtrInfo(iSectionIndex, TRUE,
odd(iPageCount), bFirstInSection);
pFtrInfo = pGetHdrFtrInfo(iSectionIndex, FALSE,
odd(iPageCount), bFirstInSection);
/* Set the height of the footer of this page */
lFooterHeight = pFtrInfo == NULL ? 0 : pFtrInfo->lHeight;
fail(lFooterHeight < 0);
if (pHdrInfo == NULL ||
pHdrInfo->pText == NULL ||
pHdrInfo->lHeight <= 0) {
fail(pHdrInfo != NULL && pHdrInfo->lHeight < 0);
fail(pHdrInfo != NULL &&
pHdrInfo->pText != NULL &&
pHdrInfo->lHeight == 0);
return;
}
vAddHdrFtr(pDiag, pHdrInfo);
DBG_DEC_C(pHdrInfo->lHeight !=
lPageHeight - PS_TOP_MARGIN - pDiag->lYtop,
pHdrInfo->lHeight);
DBG_DEC_C(pHdrInfo->lHeight !=
lPageHeight - PS_TOP_MARGIN - pDiag->lYtop,
lPageHeight - PS_TOP_MARGIN - pDiag->lYtop);
} /* end of vAddHeader */
/*
* vAddFooter - add a page footer
*/
static void
vAddFooter(diagram_type *pDiag)
{
const hdrftr_block_type *pFtrInfo;
fail(pDiag == NULL);
NO_DBG_MSG("vAddFooter");
pFtrInfo = pGetHdrFtrInfo(iSectionIndex, FALSE,
odd(iPageCount), bFirstInSection);
bFirstInSection = FALSE;
if (pFtrInfo == NULL ||
pFtrInfo->pText == NULL ||
pFtrInfo->lHeight <= 0) {
fail(pFtrInfo != NULL && pFtrInfo->lHeight < 0);
fail(pFtrInfo != NULL &&
pFtrInfo->pText != NULL &&
pFtrInfo->lHeight == 0);
return;
}
bInFtrSpace = TRUE;
DBG_DEC_C(pFtrInfo->lHeight != lFooterHeight, pFtrInfo->lHeight);
DBG_DEC_C(pFtrInfo->lHeight != lFooterHeight, lFooterHeight);
DBG_DEC_C(pDiag->lYtop < lFooterHeight + PS_BOTTOM_MARGIN,
pDiag->lYtop);
DBG_DEC_C(pDiag->lYtop < lFooterHeight + PS_BOTTOM_MARGIN,
lFooterHeight + PS_BOTTOM_MARGIN);
if (pDiag->lYtop > lFooterHeight + PS_BOTTOM_MARGIN) {
/* Move down to the start of the footer */
pDiag->lYtop = lFooterHeight + PS_BOTTOM_MARGIN;
vMoveTo(pDiag, 0);
} else if (pDiag->lYtop < lFooterHeight + PS_BOTTOM_MARGIN / 2) {
DBG_FIXME();
/*
* Move up to the start of the footer, to prevent moving
* of the bottom edge of the paper
*/
pDiag->lYtop = lFooterHeight + PS_BOTTOM_MARGIN;
vMoveTo(pDiag, 0);
}
DBG_FLT_C(pDiag->lYtop < lFooterHeight + PS_BOTTOM_MARGIN,
dDrawUnits2Points(lFooterHeight + PS_BOTTOM_MARGIN - pDiag->lYtop));
vAddHdrFtr(pDiag, pFtrInfo);
bInFtrSpace = FALSE;
} /* end of vAddFooter */
/*
* vEndPageObject - end the current page object
*/
static void
vEndPageObject(FILE *pOutFile)
{
long lStreamEnd;
if (lStreamStart < 0) {
/* There is no current page object */
return;
}
vFPprintf(pOutFile, "ET\n");
lStreamEnd = lFilePosition;
vFPprintf(pOutFile, "endstream\n");
vFPprintf(pOutFile, "endobj\n");
iObjectNumberCurr++;
vSetLocation(iObjectNumberCurr);
vFPprintf(pOutFile, "%d 0 obj\n", iObjectNumberCurr);
vFPprintf(pOutFile, "%lu\n", lStreamEnd - lStreamStart);
vFPprintf(pOutFile, "endobj\n");
} /* end of vEndPageObject */
/*
* vMove2NextPage - move to the start of the next page
*/
static void
vMove2NextPage(diagram_type *pDiag, BOOL bNewSection)
{
FILE *pOutFile;
fail(pDiag == NULL);
fail(pDiag->pOutFile == NULL);
pOutFile = pDiag->pOutFile;
vAddFooter(pDiag);
/* End the old page object */
vEndPageObject(pOutFile);
if (bNewSection) {
iSectionIndex++;
bFirstInSection = TRUE;
}
/* Start the new page object */
iObjectNumberCurr++;
vSetLocation(iObjectNumberCurr);
vFillNextPageObject();
vFPprintf(pOutFile, "%d 0 obj\n", iObjectNumberCurr);
vFPprintf(pOutFile, "<<\n");
vFPprintf(pOutFile, "/Type /Page\n");
vFPprintf(pOutFile, "/Parent 3 0 R\n");
vFPprintf(pOutFile, "/Resources 17 0 R\n");
vFPprintf(pOutFile, "/Contents %d 0 R\n", iObjectNumberCurr + 1);
vFPprintf(pOutFile, ">>\n");
vFPprintf(pOutFile, "endobj\n");
/* Start the new text object */
iObjectNumberCurr++;
vSetLocation(iObjectNumberCurr);
vFPprintf(pOutFile, "%d 0 obj\n", iObjectNumberCurr);
vFPprintf(pOutFile, "<<\n");
vFPprintf(pOutFile, "/Length %d 0 R\n", iObjectNumberCurr + 1);
vFPprintf(pOutFile, ">>\n");
vFPprintf(pOutFile, "stream\n");
lStreamStart = lFilePosition;
vFPprintf(pOutFile, "BT\n");
/* Set variables to their start of page values */
pDiag->lYtop = lPageHeight - PS_TOP_MARGIN;
tFontRefCurr = (drawfile_fontref)-1;
usFontSizeCurr = 0;
iFontColorCurr = -1;
lYtopCurr = -1;
vAddHeader(pDiag);
} /* end of vMove2NextPage */
/*
* vMoveTo - move to the specified X,Y coordinates
*
* Move the current position of the specified diagram to its X,Y coordinates,
* start on a new page if needed
*/
static void
vMoveTo(diagram_type *pDiag, long lLastVerticalMovement)
{
fail(pDiag == NULL);
fail(pDiag->pOutFile == NULL);
if (pDiag->lYtop <= lFooterHeight + PS_BOTTOM_MARGIN && !bInFtrSpace) {
vMove2NextPage(pDiag, FALSE);
/* Repeat the last vertical movement on the new page */
pDiag->lYtop -= lLastVerticalMovement;
}
fail(pDiag->lYtop < lFooterHeight + PS_BOTTOM_MARGIN && !bInFtrSpace);
DBG_DEC_C(pDiag->lYtop < PS_BOTTOM_MARGIN, pDiag->lYtop);
fail(pDiag->lYtop < PS_BOTTOM_MARGIN / 3);
if (pDiag->lYtop != lYtopCurr) {
vFPprintf(pDiag->pOutFile, "1 0 0 1 %.2f %.2f Tm\n",
dDrawUnits2Points(pDiag->lXleft + PS_LEFT_MARGIN),
dDrawUnits2Points(pDiag->lYtop));
lYtopCurr = pDiag->lYtop;
}
} /* end of vMoveTo */
/*
* vProloguePDF - set options and perform the PDF initialization
*/
void
vProloguePDF(diagram_type *pDiag,
const char *szTask, const options_type *pOptions)
{
FILE *pOutFile;
fail(pDiag == NULL);
fail(pDiag->pOutFile == NULL);
fail(pOptions == NULL);
pOutFile = pDiag->pOutFile;
eEncoding = pOptions->eEncoding;
/* Create an empty location array */
tLocations = INITIAL_LOCATION_SIZE;
alLocation = xcalloc(tLocations, sizeof(long));
/* Create an empty pageobject array */
tMaxPageObjects = INITIAL_PAGEOBJECT_SIZE;
aiPageObject = xcalloc(tMaxPageObjects, sizeof(int));
if (pOptions->iPageHeight == INT_MAX) {
lPageHeight = LONG_MAX;
} else {
lPageHeight = lPoints2DrawUnits(pOptions->iPageHeight);
}
DBG_DEC(lPageHeight);
if (pOptions->iPageWidth == INT_MAX) {
lPageWidth = LONG_MAX;
} else {
lPageWidth = lPoints2DrawUnits(pOptions->iPageWidth);
}
DBG_DEC(lPageWidth);
lFooterHeight = 0;
bInFtrSpace = FALSE;
tFontRefCurr = (drawfile_fontref)-1;
usFontSizeCurr = 0;
iFontColorCurr = -1;
lYtopCurr = -1;
iPageCount = 0;
iImageCount = 0;
iSectionIndex = 0;
bFirstInSection = TRUE;
lFilePosition = 0;
iMaxLocationNumber = 0;
lStreamStart = -1;
iObjectNumberCurr = 17;
pDiag->lXleft = 0;
pDiag->lYtop = 0;
szProducer = szTask;
vFPprintf(pOutFile, "%%PDF-1.3\n");
vFPprintf(pOutFile, "%%%c%c%c%c\n", 0xe2, 0xe3, 0xcf, 0xd3);
/* Root catalog */
vSetLocation(1);
vFPprintf(pOutFile, "1 0 obj\n");
vFPprintf(pOutFile, "<<\n");
vFPprintf(pOutFile, "/Type /Catalog\n");
vFPprintf(pOutFile, "/Pages 3 0 R\n");
vFPprintf(pOutFile, ">>\n");
vFPprintf(pOutFile, "endobj\n");
} /* end of vProloguePDF */
/*
* vEpiloguePDF - clean up after everything is done
*/
void
vEpiloguePDF(diagram_type *pDiag)
{
FILE *pOutFile;
long lXref;
int iIndex;
fail(pDiag == NULL);
fail(pDiag->pOutFile == NULL);
pOutFile = pDiag->pOutFile;
vAddFooter(pDiag);
/* End the old page object */
vEndPageObject(pOutFile);
vSetLocation(3);
vFPprintf(pOutFile, "3 0 obj\n");
vFPprintf(pOutFile, "<<\n");
vFPprintf(pOutFile, "/Type /Pages\n");
vFPprintf(pOutFile, "/Count %d\n", iPageCount);
vFPprintf(pOutFile, "/MediaBox [ 0 0 %.0f %.0f ]\n",
dDrawUnits2Points(lPageWidth),
dDrawUnits2Points(lPageHeight));
vFPprintf(pOutFile, "/Kids [ ");
for (iIndex = 1; iIndex <= iPageCount; iIndex++) {
vFPprintf(pOutFile, "\t%d 0 R\n", aiPageObject[iIndex]);
}
vFPprintf(pOutFile, "]\n");
vFPprintf(pOutFile, ">>\n");
vFPprintf(pOutFile, "endobj\n");
lXref = lFilePosition;
vFPprintf(pOutFile, "xref\n");
vFPprintf(pOutFile, "0 %d\n", iMaxLocationNumber + 1);
vFPprintf(pOutFile, "0000000000 65535 f \n");
for (iIndex = 1; iIndex <= iMaxLocationNumber; iIndex++) {
vFPprintf(pOutFile, "%.10ld 00000 n \n", alLocation[iIndex]);
}
vFPprintf(pOutFile, "trailer\n");
vFPprintf(pOutFile, "<<\n");
vFPprintf(pOutFile, "/Size %d\n", iMaxLocationNumber + 1);
vFPprintf(pOutFile, "/Root 1 0 R\n");
vFPprintf(pOutFile, "/Info 2 0 R\n");
vFPprintf(pOutFile, ">>\n");
vFPprintf(pOutFile, "startxref\n");
vFPprintf(pOutFile, "%ld\n", lXref);
vFPprintf(pOutFile, "%%%%EOF\n");
szProducer = NULL;
aiPageObject = xfree(aiPageObject);
alLocation = xfree(alLocation);
} /* end of vEpiloguePDF */
/*
* vPrintPalette - print a pdf color space (palette)
*/
static void
vPrintPalette(FILE *pOutFile, const imagedata_type *pImg)
{
int iIndex;
fail(pOutFile == NULL);
fail(pImg == NULL);
fail(pImg->iColorsUsed < 2);
fail(pImg->iColorsUsed > 256);
vFPprintf(pOutFile, "\t/ColorSpace [ /Indexed\n");
vFPprintf(pOutFile, "\t/Device%s %d\n",
pImg->bColorImage ? "RGB" : "Gray", pImg->iColorsUsed - 1);
vFPprintf(pOutFile, "<");
for (iIndex = 0; iIndex < pImg->iColorsUsed; iIndex++) {
vFPprintf(pOutFile, "%02x",
(unsigned int)pImg->aucPalette[iIndex][0]);
if (pImg->bColorImage) {
vFPprintf(pOutFile, "%02x%02x",
(unsigned int)pImg->aucPalette[iIndex][1],
(unsigned int)pImg->aucPalette[iIndex][2]);
}
if (iIndex % 8 == 7) {
vFPprintf(pOutFile, "\n");
} else {
vFPprintf(pOutFile, " ");
}
}
vFPprintf(pOutFile, "> ]\n");
} /* end of vPrintPalette */
/*
* vImageProloguePDF - perform the image initialization
*/
void
vImageProloguePDF(diagram_type *pDiag, const imagedata_type *pImg)
{
FILE *pOutFile;
fail(pDiag == NULL);
fail(pDiag->pOutFile == NULL);
fail(pImg == NULL);
if (pImg->iVerSizeScaled <= 0 || pImg->iHorSizeScaled <= 0) {
return;
}
iImageCount++;
DBG_DEC_C(pDiag->lXleft != 0, pDiag->lXleft);
pDiag->lYtop -= lPoints2DrawUnits(pImg->iVerSizeScaled);
vMoveTo(pDiag, lPoints2DrawUnits(pImg->iVerSizeScaled));
pOutFile = pDiag->pOutFile;
vFPprintf(pOutFile, "ET\n");
vFPprintf(pOutFile, "q %% Image %03d\n", iImageCount);
if (pImg->eImageType == imagetype_is_dib) {
/* Scanning from left to right and bottom to top */
vFPprintf(pOutFile, "%d 0 0 %d %.2f %.2f cm\n",
pImg->iHorSizeScaled, -pImg->iVerSizeScaled,
dDrawUnits2Points(pDiag->lXleft + PS_LEFT_MARGIN),
dDrawUnits2Points(pDiag->lYtop) + pImg->iVerSizeScaled);
} else {
/* Scanning from left to right and top to bottom */
vFPprintf(pOutFile, "%d 0 0 %d %.2f %.2f cm\n",
pImg->iHorSizeScaled, pImg->iVerSizeScaled,
dDrawUnits2Points(pDiag->lXleft + PS_LEFT_MARGIN),
dDrawUnits2Points(pDiag->lYtop));
}
vFPprintf(pOutFile, "BI\n");
vFPprintf(pOutFile, "\t/Width %d\n", pImg->iWidth);
vFPprintf(pOutFile, "\t/Height %d\n", pImg->iHeight);
switch (pImg->eImageType) {
case imagetype_is_jpeg:
switch (pImg->iComponents) {
case 1:
vFPprintf(pOutFile, "\t/ColorSpace /DeviceGray\n");
break;
case 3:
vFPprintf(pOutFile, "\t/ColorSpace /DeviceRGB\n");
break;
case 4:
vFPprintf(pOutFile, "\t/ColorSpace /DeviceCMYK\n");
if (pImg->bAdobe) {
/*
* Adobe-conforming CMYK file
* applying workaround for color inversion
*/
vFPprintf(pOutFile,
"\t/Decode [1 0 1 0 1 0 1 0]\n");
}
break;
default:
DBG_DEC(pImg->iComponents);
break;
}
vFPprintf(pOutFile, "\t/BitsPerComponent 8\n");
vFPprintf(pOutFile,
"\t/Filter [ /ASCII85Decode /DCTDecode ]\n");
break;
case imagetype_is_png:
if (pImg->iComponents == 3 || pImg->iComponents == 4) {
vFPprintf(pOutFile, "\t/ColorSpace /DeviceRGB\n");
vFPprintf(pOutFile, "\t/BitsPerComponent 8\n");
} else if (pImg->iColorsUsed > 0) {
vPrintPalette(pOutFile, pImg);
fail(pImg->uiBitsPerComponent > 8);
vFPprintf(pOutFile, "\t/BitsPerComponent %u\n",
pImg->uiBitsPerComponent);
} else {
vFPprintf(pOutFile, "\t/ColorSpace /DeviceGray\n");
vFPprintf(pOutFile, "\t/BitsPerComponent 8\n");
}
vFPprintf(pOutFile,
"\t/Filter [ /ASCII85Decode /FlateDecode ]\n");
vFPprintf(pOutFile, "\t/DecodeParms [ null <<\n");
vFPprintf(pOutFile, "\t\t/Predictor 10\n");
vFPprintf(pOutFile, "\t\t/Colors %d\n", pImg->iComponents);
vFPprintf(pOutFile, "\t\t/BitsPerComponent %u\n",
pImg->uiBitsPerComponent);
vFPprintf(pOutFile, "\t\t/Columns %d\n", pImg->iWidth);
vFPprintf(pOutFile, "\t\t>> ]\n");
break;
case imagetype_is_dib:
if (pImg->uiBitsPerComponent <= 8) {
vPrintPalette(pOutFile, pImg);
} else {
vFPprintf(pOutFile, "\t/ColorSpace /DeviceRGB\n");
}
vFPprintf(pOutFile, "\t/BitsPerComponent 8\n");
vFPprintf(pOutFile, "\t/Filter /ASCII85Decode\n");
break;
default:
vFPprintf(pOutFile, "\t/ColorSpace /Device%s\n",
pImg->bColorImage ? "RGB" : "Gray");
vFPprintf(pOutFile, "\t/BitsPerComponent 8\n");
vFPprintf(pOutFile, "\t/Filter /ASCIIHexDecode\n");
break;
}
vFPprintf(pOutFile, "ID\n");
} /* end of vImageProloguePDF */
/*
* vImageEpiloguePDF - clean up after the image
*/
void
vImageEpiloguePDF(diagram_type *pDiag)
{
FILE *pOutFile;
fail(pDiag == NULL);
fail(pDiag->pOutFile == NULL);
pOutFile = pDiag->pOutFile;
/* Correction for the image bytes */
lFilePosition = ftell(pOutFile);
vFPprintf(pOutFile, "EI\n");
vFPprintf(pOutFile, "Q\n");
vFPprintf(pOutFile, "BT\n");
pDiag->lXleft = 0;
} /* end of vImageEpiloguePDF */
/*
* bAddDummyImagePDF - add a dummy image
*
* return TRUE when successful, otherwise FALSE
*/
BOOL
bAddDummyImagePDF(diagram_type *pDiag, const imagedata_type *pImg)
{
FILE *pOutFile;
fail(pDiag == NULL);
fail(pDiag->pOutFile == NULL);
fail(pImg == NULL);
if (pImg->iVerSizeScaled <= 0 || pImg->iHorSizeScaled <= 0) {
return FALSE;
}
iImageCount++;
DBG_DEC_C(pDiag->lXleft != 0, pDiag->lXleft);
pDiag->lYtop -= lPoints2DrawUnits(pImg->iVerSizeScaled);
vMoveTo(pDiag, lPoints2DrawUnits(pImg->iVerSizeScaled));
pOutFile = pDiag->pOutFile;
vFPprintf(pOutFile, "ET\n");
vFPprintf(pOutFile, "q %% Image %03d\n", iImageCount);
vFPprintf(pOutFile, "\t1.0 w\n");
vFPprintf(pOutFile, "\t0.3 G\n");
vFPprintf(pOutFile, "\t%.2f %.2f %d %d re\n",
dDrawUnits2Points(pDiag->lXleft + PS_LEFT_MARGIN),
dDrawUnits2Points(pDiag->lYtop),
pImg->iHorSizeScaled,
pImg->iVerSizeScaled);
vFPprintf(pOutFile, "\tS\n");
vFPprintf(pOutFile, "Q\n");
vFPprintf(pOutFile, "BT\n");
pDiag->lXleft = 0;
return TRUE;
} /* end of bAddDummyImagePDF */
/*
* vAddFontsPDF - add the font information
*/
void
vAddFontsPDF(diagram_type *pDiag)
{
FILE *pOutFile;
size_t tIndex;
fail(pDiag == NULL);
fail(pDiag->pOutFile == NULL);
pOutFile = pDiag->pOutFile;
/* The font encoding */
vSetLocation(4);
vFPprintf(pOutFile, "4 0 obj\n");
vFPprintf(pOutFile, "<<\n");
vFPprintf(pOutFile, "/Type /Encoding\n");
vFPprintf(pOutFile, "/BaseEncoding /StandardEncoding\n");
vFPprintf(pOutFile, "/Differences [\n");
switch (eEncoding) {
case encoding_latin_1:
for (tIndex = 0;
tIndex < elementsof(iso_8859_1);
tIndex++) {
vFPprintf(pOutFile, "%s\n", iso_8859_1[tIndex]);
}
break;
case encoding_latin_2:
for (tIndex = 0;
tIndex < elementsof(iso_8859_2);
tIndex++) {
vFPprintf(pOutFile, "%s\n", iso_8859_2[tIndex]);
}
break;
case encoding_cyrillic:
werr(1,
"The combination PDF and Cyrillic is not supported");
break;
case encoding_utf_8:
werr(1,
"The combination PDF and UTF-8 is not supported");
break;
default:
DBG_DEC(eEncoding);
break;
}
vFPprintf(pOutFile, "]\n");
vFPprintf(pOutFile, ">>\n");
vFPprintf(pOutFile, "endobj\n");
/* Twelve of the standard type 1 fonts */
for (tIndex = 0; tIndex < 12; tIndex++) {
vSetLocation(5 + tIndex);
vFPprintf(pOutFile, "%u 0 obj\n", 5 + tIndex);
vFPprintf(pOutFile, "<<\n");
vFPprintf(pOutFile, "/Type /Font\n");
vFPprintf(pOutFile, "/Subtype /Type1\n");
vFPprintf(pOutFile, "/Name /F%u\n", 1 + tIndex);
vFPprintf(pOutFile, "/BaseFont /%s\n",
atFontname[tIndex].szPDFname);
vFPprintf(pOutFile, "/Encoding 4 0 R\n");
vFPprintf(pOutFile, ">>\n");
vFPprintf(pOutFile, "endobj\n");
}
/* The Resources */
vSetLocation(17);
vFPprintf(pOutFile, "17 0 obj\n");
vFPprintf(pOutFile, "<<\n");
vFPprintf(pOutFile, "/ProcSet [ /PDF /Text ]\n");
vFPprintf(pOutFile, "/Font <<\n");
for (tIndex = 0; tIndex < 12; tIndex++) {
vFPprintf(pOutFile, "\t/F%u %u 0 R\n", 1 + tIndex, 5 + tIndex);
}
vFPprintf(pOutFile, "\t>>\n");
vFPprintf(pOutFile, ">>\n");
vFPprintf(pOutFile, "endobj\n");
vAddHeader(pDiag);
} /* end of vAddFontsPDF */
/*
* vPrintPDF - print a PDF string
*/
static void
vPrintPDF(FILE *pFile, const char *szString, size_t tStringLength,
USHORT usFontstyle)
{
const UCHAR *aucBytes;
double dMove;
size_t tCount;
fail(szString == NULL);
if (szString == NULL || szString[0] == '\0' || tStringLength == 0) {
return;
}
DBG_DEC_C(usFontSizeCurr < MIN_FONT_SIZE, usFontSizeCurr);
dMove = 0.0;
/* Up for superscript */
if (bIsSuperscript(usFontstyle) && usFontSizeCurr != 0) {
dMove = (double)((usFontSizeCurr + 1) / 2) * 0.375;
vFPprintf(pFile, "%.2f Ts\n", dMove);
}
/* Down for subscript */