forked from flexpaper/pdf2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FoFiTrueType.cc
2027 lines (1903 loc) · 61.9 KB
/
FoFiTrueType.cc
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
//========================================================================
//
// FoFiTrueType.cc
//
// Copyright 1999-2003 Glyph & Cog, LLC
//
//========================================================================
#include <aconf.h>
#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif
#include <stdlib.h>
#include <string.h>
#include "gtypes.h"
#include "gmem.h"
#include "GString.h"
#include "GHash.h"
#include "FoFiType1C.h"
#include "FoFiTrueType.h"
//
// Terminology
// -----------
//
// character code = number used as an element of a text string
//
// character name = glyph name = name for a particular glyph within a
// font
//
// glyph index = GID = position (within some internal table in the font)
// where the instructions to draw a particular glyph are
// stored
//
// Type 1 fonts
// ------------
//
// Type 1 fonts contain:
//
// Encoding: array of glyph names, maps char codes to glyph names
//
// Encoding[charCode] = charName
//
// CharStrings: dictionary of instructions, keyed by character names,
// maps character name to glyph data
//
// CharStrings[charName] = glyphData
//
// TrueType fonts
// --------------
//
// TrueType fonts contain:
//
// 'cmap' table: mapping from character code to glyph index; there may
// be multiple cmaps in a TrueType font
//
// cmap[charCode] = gid
//
// 'post' table: mapping from glyph index to glyph name
//
// post[gid] = glyphName
//
// Type 42 fonts
// -------------
//
// Type 42 fonts contain:
//
// Encoding: array of glyph names, maps char codes to glyph names
//
// Encoding[charCode] = charName
//
// CharStrings: dictionary of glyph indexes, keyed by character names,
// maps character name to glyph index
//
// CharStrings[charName] = gid
//
//------------------------------------------------------------------------
#define ttcfTag 0x74746366
//------------------------------------------------------------------------
struct TrueTypeTable {
Guint tag;
Guint checksum;
int offset;
int origOffset;
int len;
};
struct TrueTypeCmap {
int platform;
int encoding;
int offset;
int len;
int fmt;
};
struct TrueTypeLoca {
int idx;
int origOffset;
int newOffset;
int len;
};
#define cmapTag 0x636d6170
#define glyfTag 0x676c7966
#define headTag 0x68656164
#define hheaTag 0x68686561
#define hmtxTag 0x686d7478
#define locaTag 0x6c6f6361
#define nameTag 0x6e616d65
#define os2Tag 0x4f532f32
#define postTag 0x706f7374
static int cmpTrueTypeLocaOffset(const void *p1, const void *p2) {
TrueTypeLoca *loca1 = (TrueTypeLoca *)p1;
TrueTypeLoca *loca2 = (TrueTypeLoca *)p2;
if (loca1->origOffset == loca2->origOffset) {
return loca1->idx - loca2->idx;
}
return loca1->origOffset - loca2->origOffset;
}
static int cmpTrueTypeLocaIdx(const void *p1, const void *p2) {
TrueTypeLoca *loca1 = (TrueTypeLoca *)p1;
TrueTypeLoca *loca2 = (TrueTypeLoca *)p2;
return loca1->idx - loca2->idx;
}
static int cmpTrueTypeTableTag(const void *p1, const void *p2) {
TrueTypeTable *tab1 = (TrueTypeTable *)p1;
TrueTypeTable *tab2 = (TrueTypeTable *)p2;
return (int)tab1->tag - (int)tab2->tag;
}
//------------------------------------------------------------------------
struct T42Table {
char *tag; // 4-byte tag
GBool required; // required by the TrueType spec?
};
// TrueType tables to be embedded in Type 42 fonts.
// NB: the table names must be in alphabetical order here.
#define nT42Tables 11
static T42Table t42Tables[nT42Tables] = {
{ "cvt ", gTrue },
{ "fpgm", gTrue },
{ "glyf", gTrue },
{ "head", gTrue },
{ "hhea", gTrue },
{ "hmtx", gTrue },
{ "loca", gTrue },
{ "maxp", gTrue },
{ "prep", gTrue },
{ "vhea", gFalse },
{ "vmtx", gFalse }
};
#define t42HeadTable 3
#define t42LocaTable 6
#define t42GlyfTable 2
#define t42VheaTable 9
#define t42VmtxTable 10
//------------------------------------------------------------------------
// Glyph names in some arbitrary standard order that Apple uses for
// their TrueType fonts.
static char *macGlyphNames[258] = {
".notdef", "null", "CR", "space",
"exclam", "quotedbl", "numbersign", "dollar",
"percent", "ampersand", "quotesingle", "parenleft",
"parenright", "asterisk", "plus", "comma",
"hyphen", "period", "slash", "zero",
"one", "two", "three", "four",
"five", "six", "seven", "eight",
"nine", "colon", "semicolon", "less",
"equal", "greater", "question", "at",
"A", "B", "C", "D",
"E", "F", "G", "H",
"I", "J", "K", "L",
"M", "N", "O", "P",
"Q", "R", "S", "T",
"U", "V", "W", "X",
"Y", "Z", "bracketleft", "backslash",
"bracketright", "asciicircum", "underscore", "grave",
"a", "b", "c", "d",
"e", "f", "g", "h",
"i", "j", "k", "l",
"m", "n", "o", "p",
"q", "r", "s", "t",
"u", "v", "w", "x",
"y", "z", "braceleft", "bar",
"braceright", "asciitilde", "Adieresis", "Aring",
"Ccedilla", "Eacute", "Ntilde", "Odieresis",
"Udieresis", "aacute", "agrave", "acircumflex",
"adieresis", "atilde", "aring", "ccedilla",
"eacute", "egrave", "ecircumflex", "edieresis",
"iacute", "igrave", "icircumflex", "idieresis",
"ntilde", "oacute", "ograve", "ocircumflex",
"odieresis", "otilde", "uacute", "ugrave",
"ucircumflex", "udieresis", "dagger", "degree",
"cent", "sterling", "section", "bullet",
"paragraph", "germandbls", "registered", "copyright",
"trademark", "acute", "dieresis", "notequal",
"AE", "Oslash", "infinity", "plusminus",
"lessequal", "greaterequal", "yen", "mu1",
"partialdiff", "summation", "product", "pi",
"integral", "ordfeminine", "ordmasculine", "Ohm",
"ae", "oslash", "questiondown", "exclamdown",
"logicalnot", "radical", "florin", "approxequal",
"increment", "guillemotleft", "guillemotright", "ellipsis",
"nbspace", "Agrave", "Atilde", "Otilde",
"OE", "oe", "endash", "emdash",
"quotedblleft", "quotedblright", "quoteleft", "quoteright",
"divide", "lozenge", "ydieresis", "Ydieresis",
"fraction", "currency", "guilsinglleft", "guilsinglright",
"fi", "fl", "daggerdbl", "periodcentered",
"quotesinglbase", "quotedblbase", "perthousand", "Acircumflex",
"Ecircumflex", "Aacute", "Edieresis", "Egrave",
"Iacute", "Icircumflex", "Idieresis", "Igrave",
"Oacute", "Ocircumflex", "applelogo", "Ograve",
"Uacute", "Ucircumflex", "Ugrave", "dotlessi",
"circumflex", "tilde", "overscore", "breve",
"dotaccent", "ring", "cedilla", "hungarumlaut",
"ogonek", "caron", "Lslash", "lslash",
"Scaron", "scaron", "Zcaron", "zcaron",
"brokenbar", "Eth", "eth", "Yacute",
"yacute", "Thorn", "thorn", "minus",
"multiply", "onesuperior", "twosuperior", "threesuperior",
"onehalf", "onequarter", "threequarters", "franc",
"Gbreve", "gbreve", "Idot", "Scedilla",
"scedilla", "Cacute", "cacute", "Ccaron",
"ccaron", "dmacron"
};
//------------------------------------------------------------------------
// FoFiTrueType
//------------------------------------------------------------------------
FoFiTrueType *FoFiTrueType::make(char *fileA, int lenA) {
FoFiTrueType *ff;
ff = new FoFiTrueType(fileA, lenA, gFalse);
if (!ff->parsedOk) {
delete ff;
return NULL;
}
return ff;
}
FoFiTrueType *FoFiTrueType::load(char *fileName) {
FoFiTrueType *ff;
char *fileA;
int lenA;
if (!(fileA = FoFiBase::readFile(fileName, &lenA))) {
return NULL;
}
ff = new FoFiTrueType(fileA, lenA, gTrue);
if (!ff->parsedOk) {
delete ff;
return NULL;
}
return ff;
}
FoFiTrueType::FoFiTrueType(char *fileA, int lenA, GBool freeFileDataA):
FoFiBase(fileA, lenA, freeFileDataA)
{
tables = NULL;
nTables = 0;
cmaps = NULL;
nCmaps = 0;
nameToGID = NULL;
parsedOk = gFalse;
parse();
}
FoFiTrueType::~FoFiTrueType() {
gfree(tables);
gfree(cmaps);
if (nameToGID) {
delete nameToGID;
}
}
int FoFiTrueType::getNumCmaps() {
return nCmaps;
}
int FoFiTrueType::getCmapPlatform(int i) {
return cmaps[i].platform;
}
int FoFiTrueType::getCmapEncoding(int i) {
return cmaps[i].encoding;
}
int FoFiTrueType::findCmap(int platform, int encoding) {
int i;
for (i = 0; i < nCmaps; ++i) {
if (cmaps[i].platform == platform && cmaps[i].encoding == encoding) {
return i;
}
}
return -1;
}
Gushort FoFiTrueType::mapCodeToGID(int i, int c) {
Gushort gid;
int segCnt, segEnd, segStart, segDelta, segOffset;
int cmapFirst, cmapLen;
int pos, a, b, m;
GBool ok;
if (i < 0 || i >= nCmaps) {
return 0;
}
ok = gTrue;
pos = cmaps[i].offset;
switch (cmaps[i].fmt) {
case 0:
if (c < 0 || c >= cmaps[i].len - 6) {
return 0;
}
gid = getU8(cmaps[i].offset + 6 + c, &ok);
break;
case 4:
segCnt = getU16BE(pos + 6, &ok) / 2;
a = -1;
b = segCnt - 1;
segEnd = getU16BE(pos + 14 + 2*b, &ok);
if (c > segEnd) {
// malformed font -- the TrueType spec requires the last segEnd
// to be 0xffff
return 0;
}
// invariant: seg[a].end < code <= seg[b].end
while (b - a > 1 && ok) {
m = (a + b) / 2;
segEnd = getU16BE(pos + 14 + 2*m, &ok);
if (segEnd < c) {
a = m;
} else {
b = m;
}
}
segStart = getU16BE(pos + 16 + 2*segCnt + 2*b, &ok);
segDelta = getU16BE(pos + 16 + 4*segCnt + 2*b, &ok);
segOffset = getU16BE(pos + 16 + 6*segCnt + 2*b, &ok);
if (c < segStart) {
return 0;
}
if (segOffset == 0) {
gid = (c + segDelta) & 0xffff;
} else {
gid = getU16BE(pos + 16 + 6*segCnt + 2*b +
segOffset + 2 * (c - segStart), &ok);
if (gid != 0) {
gid = (gid + segDelta) & 0xffff;
}
}
break;
case 6:
cmapFirst = getU16BE(pos + 6, &ok);
cmapLen = getU16BE(pos + 8, &ok);
if (c < cmapFirst || c >= cmapFirst + cmapLen) {
return 0;
}
gid = getU16BE(pos + 10 + 2 * (c - cmapFirst), &ok);
break;
default:
return 0;
}
if (!ok) {
return 0;
}
return gid;
}
int FoFiTrueType::mapNameToGID(char *name) {
if (!nameToGID) {
return 0;
}
return nameToGID->lookupInt(name);
}
Gushort *FoFiTrueType::getCIDToGIDMap(int *nCIDs) {
FoFiType1C *ff;
Gushort *map;
int i;
*nCIDs = 0;
if (!openTypeCFF) {
return NULL;
}
i = seekTable("CFF ");
if (!checkRegion(tables[i].offset, tables[i].len)) {
return NULL;
}
if (!(ff = FoFiType1C::make((char *)file + tables[i].offset,
tables[i].len))) {
return NULL;
}
map = ff->getCIDToGIDMap(nCIDs);
delete ff;
return map;
}
int FoFiTrueType::getEmbeddingRights() {
int i, fsType;
GBool ok;
if ((i = seekTable("OS/2")) < 0) {
return 4;
}
ok = gTrue;
fsType = getU16BE(tables[i].offset + 8, &ok);
if (!ok) {
return 4;
}
if (fsType & 0x0008) {
return 2;
}
if (fsType & 0x0004) {
return 1;
}
if (fsType & 0x0002) {
return 0;
}
return 3;
}
void FoFiTrueType::convertToType42(char *psName, char **encoding,
Gushort *codeToGID,
FoFiOutputFunc outputFunc,
void *outputStream) {
GString *buf;
GBool ok;
if (openTypeCFF) {
return;
}
// write the header
ok = gTrue;
buf = GString::format("%!PS-TrueTypeFont-{0:2g}\n",
(double)getS32BE(0, &ok) / 65536.0);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
// begin the font dictionary
(*outputFunc)(outputStream, "10 dict begin\n", 14);
(*outputFunc)(outputStream, "/FontName /", 11);
(*outputFunc)(outputStream, psName, strlen(psName));
(*outputFunc)(outputStream, " def\n", 5);
(*outputFunc)(outputStream, "/FontType 42 def\n", 17);
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
buf = GString::format("/FontBBox [{0:d} {1:d} {2:d} {3:d}] def\n",
bbox[0], bbox[1], bbox[2], bbox[3]);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
(*outputFunc)(outputStream, "/PaintType 0 def\n", 17);
// write the guts of the dictionary
cvtEncoding(encoding, outputFunc, outputStream);
cvtCharStrings(encoding, codeToGID, outputFunc, outputStream);
cvtSfnts(outputFunc, outputStream, NULL, gFalse);
// end the dictionary and define the font
(*outputFunc)(outputStream, "FontName currentdict end definefont pop\n", 40);
}
void FoFiTrueType::convertToType1(char *psName, char **newEncoding,
GBool ascii, FoFiOutputFunc outputFunc,
void *outputStream) {
FoFiType1C *ff;
int i;
if (!openTypeCFF) {
return;
}
i = seekTable("CFF ");
if (!checkRegion(tables[i].offset, tables[i].len)) {
return;
}
if (!(ff = FoFiType1C::make((char *)file + tables[i].offset,
tables[i].len))) {
return;
}
ff->convertToType1(psName, newEncoding, ascii, outputFunc, outputStream);
delete ff;
}
void FoFiTrueType::convertToCIDType2(char *psName,
Gushort *cidMap, int nCIDs,
GBool needVerticalMetrics,
FoFiOutputFunc outputFunc,
void *outputStream) {
GString *buf;
Gushort cid;
GBool ok;
int i, j, k;
if (openTypeCFF) {
return;
}
// write the header
ok = gTrue;
buf = GString::format("%!PS-TrueTypeFont-{0:2g}\n",
(double)getS32BE(0, &ok) / 65536.0);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
// begin the font dictionary
(*outputFunc)(outputStream, "20 dict begin\n", 14);
(*outputFunc)(outputStream, "/CIDFontName /", 14);
(*outputFunc)(outputStream, psName, strlen(psName));
(*outputFunc)(outputStream, " def\n", 5);
(*outputFunc)(outputStream, "/CIDFontType 2 def\n", 19);
(*outputFunc)(outputStream, "/FontType 42 def\n", 17);
(*outputFunc)(outputStream, "/CIDSystemInfo 3 dict dup begin\n", 32);
(*outputFunc)(outputStream, " /Registry (Adobe) def\n", 24);
(*outputFunc)(outputStream, " /Ordering (Identity) def\n", 27);
(*outputFunc)(outputStream, " /Supplement 0 def\n", 20);
(*outputFunc)(outputStream, " end def\n", 10);
(*outputFunc)(outputStream, "/GDBytes 2 def\n", 15);
if (cidMap) {
buf = GString::format("/CIDCount {0:d} def\n", nCIDs);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
if (nCIDs > 32767) {
(*outputFunc)(outputStream, "/CIDMap [", 9);
for (i = 0; i < nCIDs; i += 32768 - 16) {
(*outputFunc)(outputStream, "<\n", 2);
for (j = 0; j < 32768 - 16 && i+j < nCIDs; j += 16) {
(*outputFunc)(outputStream, " ", 2);
for (k = 0; k < 16 && i+j+k < nCIDs; ++k) {
cid = cidMap[i+j+k];
buf = GString::format("{0:02x}{1:02x}",
(cid >> 8) & 0xff, cid & 0xff);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
}
(*outputFunc)(outputStream, "\n", 1);
}
(*outputFunc)(outputStream, " >", 3);
}
(*outputFunc)(outputStream, "\n", 1);
(*outputFunc)(outputStream, "] def\n", 6);
} else {
(*outputFunc)(outputStream, "/CIDMap <\n", 10);
for (i = 0; i < nCIDs; i += 16) {
(*outputFunc)(outputStream, " ", 2);
for (j = 0; j < 16 && i+j < nCIDs; ++j) {
cid = cidMap[i+j];
buf = GString::format("{0:02x}{1:02x}",
(cid >> 8) & 0xff, cid & 0xff);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
}
(*outputFunc)(outputStream, "\n", 1);
}
(*outputFunc)(outputStream, "> def\n", 6);
}
} else {
// direct mapping - just fill the string(s) with s[i]=i
buf = GString::format("/CIDCount {0:d} def\n", nGlyphs);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
if (nGlyphs > 32767) {
(*outputFunc)(outputStream, "/CIDMap [\n", 10);
for (i = 0; i < nGlyphs; i += 32767) {
j = nGlyphs - i < 32767 ? nGlyphs - i : 32767;
buf = GString::format(" {0:d} string 0 1 {1:d} {{\n", 2 * j, j - 1);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
buf = GString::format(" 2 copy dup 2 mul exch {0:d} add -8 bitshift put\n",
i);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
buf = GString::format(" 1 index exch dup 2 mul 1 add exch {0:d} add"
" 255 and put\n", i);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
(*outputFunc)(outputStream, " } for\n", 8);
}
(*outputFunc)(outputStream, "] def\n", 6);
} else {
buf = GString::format("/CIDMap {0:d} string\n", 2 * nGlyphs);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
buf = GString::format(" 0 1 {0:d} {{\n", nGlyphs - 1);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
(*outputFunc)(outputStream,
" 2 copy dup 2 mul exch -8 bitshift put\n", 42);
(*outputFunc)(outputStream,
" 1 index exch dup 2 mul 1 add exch 255 and put\n", 50);
(*outputFunc)(outputStream, " } for\n", 8);
(*outputFunc)(outputStream, "def\n", 4);
}
}
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
buf = GString::format("/FontBBox [{0:d} {1:d} {2:d} {3:d}] def\n",
bbox[0], bbox[1], bbox[2], bbox[3]);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
(*outputFunc)(outputStream, "/PaintType 0 def\n", 17);
(*outputFunc)(outputStream, "/Encoding [] readonly def\n", 26);
(*outputFunc)(outputStream, "/CharStrings 1 dict dup begin\n", 30);
(*outputFunc)(outputStream, " /.notdef 0 def\n", 17);
(*outputFunc)(outputStream, " end readonly def\n", 19);
// write the guts of the dictionary
cvtSfnts(outputFunc, outputStream, NULL, needVerticalMetrics);
// end the dictionary and define the font
(*outputFunc)(outputStream,
"CIDFontName currentdict end /CIDFont defineresource pop\n",
56);
}
void FoFiTrueType::convertToCIDType0(char *psName,
FoFiOutputFunc outputFunc,
void *outputStream) {
FoFiType1C *ff;
int i;
if (!openTypeCFF) {
return;
}
i = seekTable("CFF ");
if (!checkRegion(tables[i].offset, tables[i].len)) {
return;
}
if (!(ff = FoFiType1C::make((char *)file + tables[i].offset,
tables[i].len))) {
return;
}
ff->convertToCIDType0(psName, outputFunc, outputStream);
delete ff;
}
void FoFiTrueType::convertToType0(char *psName, Gushort *cidMap, int nCIDs,
GBool needVerticalMetrics,
FoFiOutputFunc outputFunc,
void *outputStream) {
GString *buf;
GString *sfntsName;
int n, i, j;
if (openTypeCFF) {
return;
}
// write the Type 42 sfnts array
sfntsName = (new GString(psName))->append("_sfnts");
cvtSfnts(outputFunc, outputStream, sfntsName, needVerticalMetrics);
delete sfntsName;
// write the descendant Type 42 fonts
n = cidMap ? nCIDs : nGlyphs;
for (i = 0; i < n; i += 256) {
(*outputFunc)(outputStream, "10 dict begin\n", 14);
(*outputFunc)(outputStream, "/FontName /", 11);
(*outputFunc)(outputStream, psName, strlen(psName));
buf = GString::format("_{0:02x} def\n", i >> 8);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
(*outputFunc)(outputStream, "/FontType 42 def\n", 17);
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
buf = GString::format("/FontBBox [{0:d} {1:d} {2:d} {3:d}] def\n",
bbox[0], bbox[1], bbox[2], bbox[3]);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
(*outputFunc)(outputStream, "/PaintType 0 def\n", 17);
(*outputFunc)(outputStream, "/sfnts ", 7);
(*outputFunc)(outputStream, psName, strlen(psName));
(*outputFunc)(outputStream, "_sfnts def\n", 11);
(*outputFunc)(outputStream, "/Encoding 256 array\n", 20);
for (j = 0; j < 256 && i+j < n; ++j) {
buf = GString::format("dup {0:d} /c{1:02x} put\n", j, j);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
}
(*outputFunc)(outputStream, "readonly def\n", 13);
(*outputFunc)(outputStream, "/CharStrings 257 dict dup begin\n", 32);
(*outputFunc)(outputStream, "/.notdef 0 def\n", 15);
for (j = 0; j < 256 && i+j < n; ++j) {
buf = GString::format("/c{0:02x} {1:d} def\n",
j, cidMap ? cidMap[i+j] : i+j);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
}
(*outputFunc)(outputStream, "end readonly def\n", 17);
(*outputFunc)(outputStream,
"FontName currentdict end definefont pop\n", 40);
}
// write the Type 0 parent font
(*outputFunc)(outputStream, "16 dict begin\n", 14);
(*outputFunc)(outputStream, "/FontName /", 11);
(*outputFunc)(outputStream, psName, strlen(psName));
(*outputFunc)(outputStream, " def\n", 5);
(*outputFunc)(outputStream, "/FontType 0 def\n", 16);
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
(*outputFunc)(outputStream, "/FMapType 2 def\n", 16);
(*outputFunc)(outputStream, "/Encoding [\n", 12);
for (i = 0; i < n; i += 256) {
buf = GString::format("{0:d}\n", i >> 8);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
}
(*outputFunc)(outputStream, "] def\n", 6);
(*outputFunc)(outputStream, "/FDepVector [\n", 14);
for (i = 0; i < n; i += 256) {
(*outputFunc)(outputStream, "/", 1);
(*outputFunc)(outputStream, psName, strlen(psName));
buf = GString::format("_{0:02x} findfont\n", i >> 8);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
}
(*outputFunc)(outputStream, "] def\n", 6);
(*outputFunc)(outputStream, "FontName currentdict end definefont pop\n", 40);
}
void FoFiTrueType::convertToType0(char *psName,
FoFiOutputFunc outputFunc,
void *outputStream) {
FoFiType1C *ff;
int i;
if (!openTypeCFF) {
return;
}
i = seekTable("CFF ");
if (!checkRegion(tables[i].offset, tables[i].len)) {
return;
}
if (!(ff = FoFiType1C::make((char *)file + tables[i].offset,
tables[i].len))) {
return;
}
ff->convertToType0(psName, outputFunc, outputStream);
delete ff;
}
void FoFiTrueType::writeTTF(FoFiOutputFunc outputFunc,
void *outputStream, char *name,
Gushort *codeToGID) {
// this substitute cmap table maps char codes 0000-ffff directly to
// glyphs 0000-ffff
static char cmapTab[36] = {
0, 0, // table version number
0, 1, // number of encoding tables
0, 1, // platform ID
0, 0, // encoding ID
0, 0, 0, 12, // offset of subtable
0, 4, // subtable format
0, 24, // subtable length
0, 0, // subtable version
0, 2, // segment count * 2
0, 2, // 2 * 2 ^ floor(log2(segCount))
0, 0, // floor(log2(segCount))
0, 0, // 2*segCount - 2*2^floor(log2(segCount))
(char)0xff, (char)0xff, // endCount[0]
0, 0, // reserved
0, 0, // startCount[0]
0, 0, // idDelta[0]
0, 0 // pad to a mulitple of four bytes
};
static char nameTab[8] = {
0, 0, // format
0, 0, // number of name records
0, 6, // offset to start of string storage
0, 0 // pad to multiple of four bytes
};
static char postTab[32] = {
0, 1, 0, 0, // format
0, 0, 0, 0, // italic angle
0, 0, // underline position
0, 0, // underline thickness
0, 0, 0, 0, // fixed pitch
0, 0, 0, 0, // min Type 42 memory
0, 0, 0, 0, // max Type 42 memory
0, 0, 0, 0, // min Type 1 memory
0, 0, 0, 0 // max Type 1 memory
};
static char os2Tab[86] = {
0, 1, // version
0, 1, // xAvgCharWidth
0, 0, // usWeightClass
0, 0, // usWidthClass
0, 0, // fsType
0, 0, // ySubscriptXSize
0, 0, // ySubscriptYSize
0, 0, // ySubscriptXOffset
0, 0, // ySubscriptYOffset
0, 0, // ySuperscriptXSize
0, 0, // ySuperscriptYSize
0, 0, // ySuperscriptXOffset
0, 0, // ySuperscriptYOffset
0, 0, // yStrikeoutSize
0, 0, // yStrikeoutPosition
0, 0, // sFamilyClass
0, 0, 0, 0, 0, // panose
0, 0, 0, 0, 0,
0, 0, 0, 0, // ulUnicodeRange1
0, 0, 0, 0, // ulUnicodeRange2
0, 0, 0, 0, // ulUnicodeRange3
0, 0, 0, 0, // ulUnicodeRange4
0, 0, 0, 0, // achVendID
0, 0, // fsSelection
0, 0, // usFirstCharIndex
0, 0, // usLastCharIndex
0, 0, // sTypoAscender
0, 0, // sTypoDescender
0, 0, // sTypoLineGap
0, 0, // usWinAscent
0, 0, // usWinDescent
0, 0, 0, 0, // ulCodePageRange1
0, 0, 0, 0 // ulCodePageRange2
};
GBool missingCmap, missingName, missingPost, missingOS2;
GBool unsortedLoca, badCmapLen, abbrevHMTX;
int nZeroLengthTables;
int nHMetrics, advWidth, lsb;
TrueTypeLoca *locaTable;
TrueTypeTable *newTables;
char *newNameTab, *newCmapTab, *newHHEATab, *newHMTXTab;
int nNewTables, cmapIdx, cmapLen, glyfLen, newNameLen, newCmapLen, next;
int newHHEALen, newHMTXLen;
Guint locaChecksum, glyfChecksum, fileChecksum;
char *tableDir;
char locaBuf[4], checksumBuf[4];
GBool ok;
Guint t;
int pos, i, j, k, n;
if (openTypeCFF) {
return;
}
// check for missing tables
// (Note: if the OS/2 table is missing, the Microsoft PCL5 driver
// will embed a PCL TrueType font with the pitch field set to zero,
// which apparently causes divide-by-zero errors. As far as I can
// tell, the only important field in the OS/2 table is
// xAvgCharWidth.)
missingCmap = (cmapIdx = seekTable("cmap")) < 0;
missingName = seekTable("name") < 0;
missingPost = seekTable("post") < 0;
missingOS2 = seekTable("OS/2") < 0;
// read the loca table, check to see if it's sorted
locaTable = (TrueTypeLoca *)gmallocn(nGlyphs + 1, sizeof(TrueTypeLoca));
unsortedLoca = gFalse;
i = seekTable("loca");
pos = tables[i].offset;
ok = gTrue;
for (i = 0; i <= nGlyphs; ++i) {
if (locaFmt) {
locaTable[i].origOffset = (int)getU32BE(pos + i*4, &ok);
} else {
locaTable[i].origOffset = 2 * getU16BE(pos + i*2, &ok);
}
if (i > 0 && locaTable[i].origOffset < locaTable[i-1].origOffset) {
unsortedLoca = gTrue;
}
// glyph descriptions must be at least 12 bytes long (nContours,
// xMin, yMin, xMax, yMax, instructionLength - two bytes each);
// invalid glyph descriptions (even if they're never used) make
// Windows choke, so we work around that problem here (ideally,
// this would parse the glyph descriptions in the glyf table and
// remove any that were invalid, but this quick test is a decent
// start)
if (i > 0 &&
locaTable[i].origOffset - locaTable[i-1].origOffset > 0 &&
locaTable[i].origOffset - locaTable[i-1].origOffset < 12) {
locaTable[i-1].origOffset = locaTable[i].origOffset;
unsortedLoca = gTrue;
}
locaTable[i].idx = i;
}
// check for zero-length tables
nZeroLengthTables = 0;
for (i = 0; i < nTables; ++i) {
if (tables[i].len == 0) {
++nZeroLengthTables;
}
}
// check for an incorrect cmap table length
badCmapLen = gFalse;
cmapLen = 0; // make gcc happy
if (!missingCmap) {
cmapLen = cmaps[0].offset + cmaps[0].len;
for (i = 1; i < nCmaps; ++i) {
if (cmaps[i].offset + cmaps[i].len > cmapLen) {
cmapLen = cmaps[i].offset + cmaps[i].len;
}
}
cmapLen -= tables[cmapIdx].offset;
if (cmapLen > tables[cmapIdx].len) {
badCmapLen = gTrue;
}
}
// check for an abbreviated hmtx table (this is completely legal,
// but confuses the Microsoft PCL5 printer driver, which generates
// embedded fonts with the pitch field set to zero)
i = seekTable("hhea");
nHMetrics = getU16BE(tables[i].offset + 34, &ok);
abbrevHMTX = nHMetrics < nGlyphs;
// if nothing is broken, just write the TTF file as is
if (!missingCmap && !missingName && !missingPost && !missingOS2 &&
!unsortedLoca && !badCmapLen && !abbrevHMTX && nZeroLengthTables == 0 &&
!name && !codeToGID) {
(*outputFunc)(outputStream, (char *)file, len);
goto done1;
}
// sort the 'loca' table: some (non-compliant) fonts have
// out-of-order loca tables; in order to correctly handle the case
// where (compliant) fonts have empty entries in the middle of the
// table, cmpTrueTypeLocaOffset uses offset as its primary sort key,
// and idx as its secondary key (ensuring that adjacent entries with
// the same pos value remain in the same order)
glyfLen = 0; // make gcc happy
if (unsortedLoca) {
qsort(locaTable, nGlyphs + 1, sizeof(TrueTypeLoca),
&cmpTrueTypeLocaOffset);
for (i = 0; i < nGlyphs; ++i) {
locaTable[i].len = locaTable[i+1].origOffset - locaTable[i].origOffset;
}
locaTable[nGlyphs].len = 0;
qsort(locaTable, nGlyphs + 1, sizeof(TrueTypeLoca),
&cmpTrueTypeLocaIdx);
pos = 0;
for (i = 0; i <= nGlyphs; ++i) {
locaTable[i].newOffset = pos;
pos += locaTable[i].len;
if (pos & 3) {
pos += 4 - (pos & 3);
}
}
glyfLen = pos;
}
// compute checksums for the loca and glyf tables
locaChecksum = glyfChecksum = 0;
if (unsortedLoca) {
if (locaFmt) {
for (j = 0; j <= nGlyphs; ++j) {
locaChecksum += locaTable[j].newOffset;
}
} else {
for (j = 0; j <= nGlyphs; j += 2) {
locaChecksum += locaTable[j].newOffset << 16;
if (j + 1 <= nGlyphs) {
locaChecksum += locaTable[j+1].newOffset;
}
}
}
pos = tables[seekTable("glyf")].offset;
for (j = 0; j < nGlyphs; ++j) {
n = locaTable[j].len;
if (n > 0) {
k = locaTable[j].origOffset;
if (checkRegion(pos + k, n)) {
glyfChecksum += computeTableChecksum(file + pos + k, n);
}
}
}
}
// construct the new name table
if (name) {
n = strlen(name);
newNameLen = (6 + 4*12 + 2 * (3*n + 7) + 3) & ~3;
newNameTab = (char *)gmalloc(newNameLen);
memset(newNameTab, 0, newNameLen);
newNameTab[0] = 0; // format selector
newNameTab[1] = 0;
newNameTab[2] = 0; // number of name records
newNameTab[3] = 4;