-
Notifications
You must be signed in to change notification settings - Fork 0
/
GfxFont.cc
2511 lines (2308 loc) · 87.2 KB
/
GfxFont.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
//========================================================================
//
// GfxFont.cc
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2005, 2006, 2008-2010, 2012, 2014, 2015, 2017-2023 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2005, 2006 Kristian Høgsberg <krh@redhat.com>
// Copyright (C) 2006 Takashi Iwai <tiwai@suse.de>
// Copyright (C) 2007 Julien Rebetez <julienr@svn.gnome.org>
// Copyright (C) 2007 Jeff Muizelaar <jeff@infidigm.net>
// Copyright (C) 2007 Koji Otani <sho@bbr.jp>
// Copyright (C) 2007 Ed Catmur <ed@catmur.co.uk>
// Copyright (C) 2008 Jonathan Kew <jonathan_kew@sil.org>
// Copyright (C) 2008 Ed Avis <eda@waniasset.com>
// Copyright (C) 2008, 2010 Hib Eris <hib@hiberis.nl>
// Copyright (C) 2009 Peter Kerzum <kerzum@yandex-team.ru>
// Copyright (C) 2009, 2010 David Benjamin <davidben@mit.edu>
// Copyright (C) 2011 Axel Strübing <axel.struebing@freenet.de>
// Copyright (C) 2011, 2012, 2014 Adrian Johnson <ajohnson@redneon.com>
// Copyright (C) 2012 Yi Yang <ahyangyi@gmail.com>
// Copyright (C) 2012 Suzuki Toshiya <mpsuzuki@hiroshima-u.ac.jp>
// Copyright (C) 2012, 2017 Thomas Freitag <Thomas.Freitag@alfa.de>
// Copyright (C) 2013-2016, 2018 Jason Crain <jason@aquaticape.us>
// Copyright (C) 2014 Olly Betts <olly@survex.com>
// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich
// Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
// Copyright (C) 2019 LE GARREC Vincent <legarrec.vincent@gmail.com>
// Copyright (C) 2021, 2022 Oliver Sander <oliver.sander@tu-dresden.de>
// Copyright (C) 2023 Khaled Hosny <khaled@aliftype.com>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#include <config.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <climits>
#include <algorithm>
#include "goo/gmem.h"
#include "Error.h"
#include "Object.h"
#include "Dict.h"
#include "GlobalParams.h"
#include "CMap.h"
#include "CharCodeToUnicode.h"
#include "FontEncodingTables.h"
#include "BuiltinFont.h"
#include "UnicodeTypeTable.h"
#include <fofi/FoFiIdentifier.h>
#include <fofi/FoFiType1.h>
#include <fofi/FoFiType1C.h>
#include <fofi/FoFiTrueType.h>
#include "GfxFont.h"
#include "PSOutputDev.h"
//------------------------------------------------------------------------
struct Base14FontMapEntry
{
const char *altName;
const char *base14Name;
};
static const Base14FontMapEntry base14FontMap[] = { { "Arial", "Helvetica" },
{ "Arial,Bold", "Helvetica-Bold" },
{ "Arial,BoldItalic", "Helvetica-BoldOblique" },
{ "Arial,Italic", "Helvetica-Oblique" },
{ "Arial-Bold", "Helvetica-Bold" },
{ "Arial-BoldItalic", "Helvetica-BoldOblique" },
{ "Arial-BoldItalicMT", "Helvetica-BoldOblique" },
{ "Arial-BoldMT", "Helvetica-Bold" },
{ "Arial-Italic", "Helvetica-Oblique" },
{ "Arial-ItalicMT", "Helvetica-Oblique" },
{ "ArialMT", "Helvetica" },
{ "Courier", "Courier" },
{ "Courier,Bold", "Courier-Bold" },
{ "Courier,BoldItalic", "Courier-BoldOblique" },
{ "Courier,Italic", "Courier-Oblique" },
{ "Courier-Bold", "Courier-Bold" },
{ "Courier-BoldOblique", "Courier-BoldOblique" },
{ "Courier-Oblique", "Courier-Oblique" },
{ "CourierNew", "Courier" },
{ "CourierNew,Bold", "Courier-Bold" },
{ "CourierNew,BoldItalic", "Courier-BoldOblique" },
{ "CourierNew,Italic", "Courier-Oblique" },
{ "CourierNew-Bold", "Courier-Bold" },
{ "CourierNew-BoldItalic", "Courier-BoldOblique" },
{ "CourierNew-Italic", "Courier-Oblique" },
{ "CourierNewPS-BoldItalicMT", "Courier-BoldOblique" },
{ "CourierNewPS-BoldMT", "Courier-Bold" },
{ "CourierNewPS-ItalicMT", "Courier-Oblique" },
{ "CourierNewPSMT", "Courier" },
{ "Helvetica", "Helvetica" },
{ "Helvetica,Bold", "Helvetica-Bold" },
{ "Helvetica,BoldItalic", "Helvetica-BoldOblique" },
{ "Helvetica,Italic", "Helvetica-Oblique" },
{ "Helvetica-Bold", "Helvetica-Bold" },
{ "Helvetica-BoldItalic", "Helvetica-BoldOblique" },
{ "Helvetica-BoldOblique", "Helvetica-BoldOblique" },
{ "Helvetica-Italic", "Helvetica-Oblique" },
{ "Helvetica-Oblique", "Helvetica-Oblique" },
{ "Symbol", "Symbol" },
{ "Symbol,Bold", "Symbol" },
{ "Symbol,BoldItalic", "Symbol" },
{ "Symbol,Italic", "Symbol" },
{ "SymbolMT", "Symbol" },
{ "SymbolMT,Bold", "Symbol" },
{ "SymbolMT,BoldItalic", "Symbol" },
{ "SymbolMT,Italic", "Symbol" },
{ "Times-Bold", "Times-Bold" },
{ "Times-BoldItalic", "Times-BoldItalic" },
{ "Times-Italic", "Times-Italic" },
{ "Times-Roman", "Times-Roman" },
{ "TimesNewRoman", "Times-Roman" },
{ "TimesNewRoman,Bold", "Times-Bold" },
{ "TimesNewRoman,BoldItalic", "Times-BoldItalic" },
{ "TimesNewRoman,Italic", "Times-Italic" },
{ "TimesNewRoman-Bold", "Times-Bold" },
{ "TimesNewRoman-BoldItalic", "Times-BoldItalic" },
{ "TimesNewRoman-Italic", "Times-Italic" },
{ "TimesNewRomanPS", "Times-Roman" },
{ "TimesNewRomanPS-Bold", "Times-Bold" },
{ "TimesNewRomanPS-BoldItalic", "Times-BoldItalic" },
{ "TimesNewRomanPS-BoldItalicMT", "Times-BoldItalic" },
{ "TimesNewRomanPS-BoldMT", "Times-Bold" },
{ "TimesNewRomanPS-Italic", "Times-Italic" },
{ "TimesNewRomanPS-ItalicMT", "Times-Italic" },
{ "TimesNewRomanPSMT", "Times-Roman" },
{ "TimesNewRomanPSMT,Bold", "Times-Bold" },
{ "TimesNewRomanPSMT,BoldItalic", "Times-BoldItalic" },
{ "TimesNewRomanPSMT,Italic", "Times-Italic" },
{ "ZapfDingbats", "ZapfDingbats" } };
//------------------------------------------------------------------------
// index: {fixed:0, sans-serif:4, serif:8} + bold*2 + italic
// NB: must be in same order as psSubstFonts in PSOutputDev.cc
static const char *base14SubstFonts[14] = { "Courier", "Courier-Oblique", "Courier-Bold", "Courier-BoldOblique", "Helvetica", "Helvetica-Oblique", "Helvetica-Bold", "Helvetica-BoldOblique", "Times-Roman", "Times-Italic", "Times-Bold",
"Times-BoldItalic",
// the last two are never used for substitution
"Symbol", "ZapfDingbats" };
//------------------------------------------------------------------------
static int parseCharName(char *charName, Unicode *uBuf, int uLen, bool names, bool ligatures, bool numeric, bool hex, bool variants);
//------------------------------------------------------------------------
static int readFromStream(void *data)
{
return ((Stream *)data)->getChar();
}
//------------------------------------------------------------------------
// GfxFontLoc
//------------------------------------------------------------------------
GfxFontLoc::GfxFontLoc()
{
fontNum = 0;
substIdx = -1;
}
GfxFontLoc::~GfxFontLoc() = default;
GfxFontLoc::GfxFontLoc(GfxFontLoc &&other) noexcept = default;
GfxFontLoc &GfxFontLoc::operator=(GfxFontLoc &&other) noexcept = default;
void GfxFontLoc::setPath(GooString *pathA)
{
path = pathA->toStr();
delete pathA;
}
const GooString *GfxFontLoc::pathAsGooString() const
{
return (const GooString *)(&path);
}
//------------------------------------------------------------------------
// GfxFont
//------------------------------------------------------------------------
std::unique_ptr<GfxFont> GfxFont::makeFont(XRef *xref, const char *tagA, Ref idA, Dict *fontDict)
{
std::optional<std::string> name;
Ref embFontIDA;
GfxFontType typeA;
// get base font name
Object obj1 = fontDict->lookup("BaseFont");
if (obj1.isName()) {
name = obj1.getName();
}
// There is no BaseFont in Type 3 fonts, try fontDescriptor.FontName
if (!name) {
Object fontDesc = fontDict->lookup("FontDescriptor");
if (fontDesc.isDict()) {
Object obj2 = fontDesc.dictLookup("FontName");
if (obj2.isName()) {
name = obj2.getName();
}
}
}
// As a last resort try the Name key
if (!name) {
Object obj2 = fontDict->lookup("Name");
if (obj2.isName()) {
name = obj2.getName();
}
}
// get embedded font ID and font type
typeA = getFontType(xref, fontDict, &embFontIDA);
// create the font object
GfxFont *font;
if (typeA < fontCIDType0) {
font = new Gfx8BitFont(xref, tagA, idA, std::move(name), typeA, embFontIDA, fontDict);
} else {
font = new GfxCIDFont(xref, tagA, idA, std::move(name), typeA, embFontIDA, fontDict);
}
return std::unique_ptr<GfxFont>(font);
}
GfxFont::GfxFont(const char *tagA, Ref idA, std::optional<std::string> &&nameA, GfxFontType typeA, Ref embFontIDA) : tag(tagA), id(idA), name(std::move(nameA)), type(typeA)
{
ok = false;
embFontID = embFontIDA;
embFontName = nullptr;
family = nullptr;
stretch = StretchNotDefined;
weight = WeightNotDefined;
hasToUnicode = false;
}
GfxFont::~GfxFont()
{
delete family;
if (embFontName) {
delete embFontName;
}
}
bool GfxFont::isSubset() const
{
if (name) {
unsigned int i;
for (i = 0; i < name->size(); ++i) {
if ((*name)[i] < 'A' || (*name)[i] > 'Z') {
break;
}
}
return i == 6 && name->size() > 7 && (*name)[6] == '+';
}
return false;
}
std::string GfxFont::getNameWithoutSubsetTag() const
{
if (!name) {
return {};
}
if (!isSubset()) {
return *name;
}
return name->substr(7);
}
// This function extracts three pieces of information:
// 1. the "expected" font type, i.e., the font type implied by
// Font.Subtype, DescendantFont.Subtype, and
// FontDescriptor.FontFile3.Subtype
// 2. the embedded font object ID
// 3. the actual font type - determined by examining the embedded font
// if there is one, otherwise equal to the expected font type
// If the expected and actual font types don't match, a warning
// message is printed. The expected font type is not used for
// anything else.
GfxFontType GfxFont::getFontType(XRef *xref, Dict *fontDict, Ref *embID)
{
GfxFontType t, expectedType;
FoFiIdentifierType fft;
Dict *fontDict2;
bool isType0, err;
t = fontUnknownType;
*embID = Ref::INVALID();
err = false;
Object subtype = fontDict->lookup("Subtype");
expectedType = fontUnknownType;
isType0 = false;
if (subtype.isName("Type1") || subtype.isName("MMType1")) {
expectedType = fontType1;
} else if (subtype.isName("Type1C")) {
expectedType = fontType1C;
} else if (subtype.isName("Type3")) {
expectedType = fontType3;
} else if (subtype.isName("TrueType")) {
expectedType = fontTrueType;
} else if (subtype.isName("Type0")) {
isType0 = true;
} else {
error(errSyntaxWarning, -1, "Unknown font type: '{0:s}'", subtype.isName() ? subtype.getName() : "???");
}
fontDict2 = fontDict;
Object obj1 = fontDict->lookup("DescendantFonts");
Object obj2; // Do not move to inside the if
// we need it around so that fontDict2 remains valid
if (obj1.isArray()) {
if (obj1.arrayGetLength() == 0) {
error(errSyntaxWarning, -1, "Empty DescendantFonts array in font");
} else {
obj2 = obj1.arrayGet(0);
if (obj2.isDict()) {
if (!isType0) {
error(errSyntaxWarning, -1, "Non-CID font with DescendantFonts array");
}
fontDict2 = obj2.getDict();
subtype = fontDict2->lookup("Subtype");
if (subtype.isName("CIDFontType0")) {
if (isType0) {
expectedType = fontCIDType0;
}
} else if (subtype.isName("CIDFontType2")) {
if (isType0) {
expectedType = fontCIDType2;
}
}
}
}
}
Object fontDesc = fontDict2->lookup("FontDescriptor");
if (fontDesc.isDict()) {
Object obj3 = fontDesc.dictLookupNF("FontFile").copy();
if (obj3.isRef()) {
*embID = obj3.getRef();
if (expectedType != fontType1) {
err = true;
}
}
if (*embID == Ref::INVALID() && (obj3 = fontDesc.dictLookupNF("FontFile2").copy(), obj3.isRef())) {
*embID = obj3.getRef();
if (isType0) {
expectedType = fontCIDType2;
} else if (expectedType != fontTrueType) {
err = true;
}
}
if (*embID == Ref::INVALID() && (obj3 = fontDesc.dictLookupNF("FontFile3").copy(), obj3.isRef())) {
*embID = obj3.getRef();
Object obj4 = obj3.fetch(xref);
if (obj4.isStream()) {
subtype = obj4.streamGetDict()->lookup("Subtype");
if (subtype.isName("Type1")) {
if (expectedType != fontType1) {
err = true;
expectedType = isType0 ? fontCIDType0 : fontType1;
}
} else if (subtype.isName("Type1C")) {
if (expectedType == fontType1) {
expectedType = fontType1C;
} else if (expectedType != fontType1C) {
err = true;
expectedType = isType0 ? fontCIDType0C : fontType1C;
}
} else if (subtype.isName("TrueType")) {
if (expectedType != fontTrueType) {
err = true;
expectedType = isType0 ? fontCIDType2 : fontTrueType;
}
} else if (subtype.isName("CIDFontType0C")) {
if (expectedType == fontCIDType0) {
expectedType = fontCIDType0C;
} else {
err = true;
expectedType = isType0 ? fontCIDType0C : fontType1C;
}
} else if (subtype.isName("OpenType")) {
if (expectedType == fontTrueType) {
expectedType = fontTrueTypeOT;
} else if (expectedType == fontType1) {
expectedType = fontType1COT;
} else if (expectedType == fontCIDType0) {
expectedType = fontCIDType0COT;
} else if (expectedType == fontCIDType2) {
expectedType = fontCIDType2OT;
} else {
err = true;
}
} else {
error(errSyntaxError, -1, "Unknown font type '{0:s}'", subtype.isName() ? subtype.getName() : "???");
}
}
}
}
t = fontUnknownType;
if (*embID != Ref::INVALID()) {
Object obj3(*embID);
Object obj4 = obj3.fetch(xref);
if (obj4.isStream()) {
obj4.streamReset();
fft = FoFiIdentifier::identifyStream(&readFromStream, obj4.getStream());
obj4.streamClose();
switch (fft) {
case fofiIdType1PFA:
case fofiIdType1PFB:
t = fontType1;
break;
case fofiIdCFF8Bit:
t = isType0 ? fontCIDType0C : fontType1C;
break;
case fofiIdCFFCID:
t = fontCIDType0C;
break;
case fofiIdTrueType:
case fofiIdTrueTypeCollection:
t = isType0 ? fontCIDType2 : fontTrueType;
break;
case fofiIdOpenTypeCFF8Bit:
t = isType0 ? fontCIDType0COT : fontType1COT;
break;
case fofiIdOpenTypeCFFCID:
t = fontCIDType0COT;
break;
default:
error(errSyntaxError, -1, "Embedded font file may be invalid");
break;
}
}
}
if (t == fontUnknownType) {
t = expectedType;
}
if (t != expectedType) {
err = true;
}
if (err) {
error(errSyntaxWarning, -1, "Mismatch between font type and embedded font file");
}
return t;
}
void GfxFont::readFontDescriptor(XRef *xref, Dict *fontDict)
{
double t;
// assume Times-Roman by default (for substitution purposes)
flags = fontSerif;
missingWidth = 0;
Object obj1 = fontDict->lookup("FontDescriptor");
if (obj1.isDict()) {
// get flags
Object obj2 = obj1.dictLookup("Flags");
if (obj2.isInt()) {
flags = obj2.getInt();
}
// get name
obj2 = obj1.dictLookup("FontName");
if (obj2.isName()) {
embFontName = new GooString(obj2.getName());
}
if (embFontName == nullptr) {
// get name with typo
obj2 = obj1.dictLookup("Fontname");
if (obj2.isName()) {
embFontName = new GooString(obj2.getName());
error(errSyntaxWarning, -1, "The file uses Fontname instead of FontName please notify the creator that the file is broken");
}
}
// get family
obj2 = obj1.dictLookup("FontFamily");
if (obj2.isString()) {
family = new GooString(obj2.getString());
}
// get stretch
obj2 = obj1.dictLookup("FontStretch");
if (obj2.isName()) {
if (strcmp(obj2.getName(), "UltraCondensed") == 0) {
stretch = UltraCondensed;
} else if (strcmp(obj2.getName(), "ExtraCondensed") == 0) {
stretch = ExtraCondensed;
} else if (strcmp(obj2.getName(), "Condensed") == 0) {
stretch = Condensed;
} else if (strcmp(obj2.getName(), "SemiCondensed") == 0) {
stretch = SemiCondensed;
} else if (strcmp(obj2.getName(), "Normal") == 0) {
stretch = Normal;
} else if (strcmp(obj2.getName(), "SemiExpanded") == 0) {
stretch = SemiExpanded;
} else if (strcmp(obj2.getName(), "Expanded") == 0) {
stretch = Expanded;
} else if (strcmp(obj2.getName(), "ExtraExpanded") == 0) {
stretch = ExtraExpanded;
} else if (strcmp(obj2.getName(), "UltraExpanded") == 0) {
stretch = UltraExpanded;
} else {
error(errSyntaxWarning, -1, "Invalid Font Stretch");
}
}
// get weight
obj2 = obj1.dictLookup("FontWeight");
if (obj2.isNum()) {
if (obj2.getNum() == 100) {
weight = W100;
} else if (obj2.getNum() == 200) {
weight = W200;
} else if (obj2.getNum() == 300) {
weight = W300;
} else if (obj2.getNum() == 400) {
weight = W400;
} else if (obj2.getNum() == 500) {
weight = W500;
} else if (obj2.getNum() == 600) {
weight = W600;
} else if (obj2.getNum() == 700) {
weight = W700;
} else if (obj2.getNum() == 800) {
weight = W800;
} else if (obj2.getNum() == 900) {
weight = W900;
} else {
error(errSyntaxWarning, -1, "Invalid Font Weight");
}
}
// look for MissingWidth
obj2 = obj1.dictLookup("MissingWidth");
if (obj2.isNum()) {
missingWidth = obj2.getNum();
}
// get Ascent and Descent
obj2 = obj1.dictLookup("Ascent");
if (obj2.isNum()) {
t = 0.001 * obj2.getNum();
// some broken font descriptors specify a negative ascent
if (t < 0) {
t = -t;
}
// some broken font descriptors set ascent and descent to 0;
// others set it to ridiculous values (e.g., 32768)
if (t != 0 && t < 3) {
ascent = t;
}
}
obj2 = obj1.dictLookup("Descent");
if (obj2.isNum()) {
t = 0.001 * obj2.getNum();
// some broken font descriptors specify a positive descent
if (t > 0) {
t = -t;
}
// some broken font descriptors set ascent and descent to 0
if (t != 0 && t > -3) {
descent = t;
}
}
// font FontBBox
obj2 = obj1.dictLookup("FontBBox");
if (obj2.isArray()) {
for (int i = 0; i < 4 && i < obj2.arrayGetLength(); ++i) {
Object obj3 = obj2.arrayGet(i);
if (obj3.isNum()) {
fontBBox[i] = 0.001 * obj3.getNum();
}
}
}
}
}
CharCodeToUnicode *GfxFont::readToUnicodeCMap(Dict *fontDict, int nBits, CharCodeToUnicode *ctu)
{
GooString *buf;
Object obj1 = fontDict->lookup("ToUnicode");
if (!obj1.isStream()) {
return nullptr;
}
buf = new GooString();
obj1.getStream()->fillGooString(buf);
obj1.streamClose();
if (ctu) {
ctu->mergeCMap(buf, nBits);
} else {
ctu = CharCodeToUnicode::parseCMap(buf, nBits);
}
hasToUnicode = true;
delete buf;
return ctu;
}
std::optional<GfxFontLoc> GfxFont::locateFont(XRef *xref, PSOutputDev *ps, GooString *substituteFontName)
{
SysFontType sysFontType;
GooString *path, *base14Name;
int substIdx, fontNum;
bool embed;
if (type == fontType3) {
return std::nullopt;
}
//----- embedded font
if (embFontID != Ref::INVALID()) {
embed = true;
Object refObj(embFontID);
Object embFontObj = refObj.fetch(xref);
if (!embFontObj.isStream()) {
error(errSyntaxError, -1, "Embedded font object is wrong type");
embed = false;
}
if (embed) {
if (ps) {
switch (type) {
case fontType1:
case fontType1C:
case fontType1COT:
embed = ps->getEmbedType1();
break;
case fontTrueType:
case fontTrueTypeOT:
embed = ps->getEmbedTrueType();
break;
case fontCIDType0C:
case fontCIDType0COT:
embed = ps->getEmbedCIDPostScript();
break;
case fontCIDType2:
case fontCIDType2OT:
embed = ps->getEmbedCIDTrueType();
break;
default:
break;
}
}
if (embed) {
GfxFontLoc fontLoc;
fontLoc.locType = gfxFontLocEmbedded;
fontLoc.fontType = type;
fontLoc.embFontID = embFontID;
return fontLoc;
}
}
}
//----- PS passthrough
if (ps && !isCIDFont() && ps->getFontPassthrough()) {
GfxFontLoc fontLoc;
fontLoc.locType = gfxFontLocResident;
fontLoc.fontType = fontType1;
fontLoc.path = *name;
return fontLoc;
}
//----- PS resident Base-14 font
if (ps && !isCIDFont() && ((Gfx8BitFont *)this)->base14) {
GfxFontLoc fontLoc;
fontLoc.locType = gfxFontLocResident;
fontLoc.fontType = fontType1;
fontLoc.path = ((Gfx8BitFont *)this)->base14->base14Name;
return fontLoc;
}
//----- external font file (fontFile, fontDir)
if (name && (path = globalParams->findFontFile(*name))) {
if (std::optional<GfxFontLoc> fontLoc = getExternalFont(path, isCIDFont())) {
return fontLoc;
}
}
//----- external font file for Base-14 font
if (!ps && !isCIDFont() && ((Gfx8BitFont *)this)->base14) {
base14Name = new GooString(((Gfx8BitFont *)this)->base14->base14Name);
if ((path = globalParams->findBase14FontFile(base14Name, this, substituteFontName))) {
if (std::optional<GfxFontLoc> fontLoc = getExternalFont(path, false)) {
delete base14Name;
return fontLoc;
}
}
delete base14Name;
}
//----- system font
if ((path = globalParams->findSystemFontFile(this, &sysFontType, &fontNum, substituteFontName))) {
if (isCIDFont()) {
if (sysFontType == sysFontTTF || sysFontType == sysFontTTC) {
GfxFontLoc fontLoc;
fontLoc.locType = gfxFontLocExternal;
fontLoc.fontType = fontCIDType2;
fontLoc.setPath(path);
fontLoc.fontNum = fontNum;
return fontLoc;
}
} else {
GfxFontLoc fontLoc;
fontLoc.setPath(path);
fontLoc.locType = gfxFontLocExternal;
if (sysFontType == sysFontTTF || sysFontType == sysFontTTC) {
fontLoc.fontType = fontTrueType;
} else if (sysFontType == sysFontPFA || sysFontType == sysFontPFB) {
fontLoc.fontType = fontType1;
fontLoc.fontNum = fontNum;
}
return fontLoc;
}
delete path;
}
if (!isCIDFont()) {
//----- 8-bit font substitution
if (flags & fontFixedWidth) {
substIdx = 0;
} else if (flags & fontSerif) {
substIdx = 8;
} else {
substIdx = 4;
}
if (isBold()) {
substIdx += 2;
}
if (isItalic()) {
substIdx += 1;
}
const std::string substName = base14SubstFonts[substIdx];
if (ps) {
error(errSyntaxWarning, -1, "Substituting font '{0:s}' for '{1:s}'", base14SubstFonts[substIdx], name ? name->c_str() : "null");
GfxFontLoc fontLoc;
fontLoc.locType = gfxFontLocResident;
fontLoc.fontType = fontType1;
fontLoc.path = substName;
fontLoc.substIdx = substIdx;
return fontLoc;
} else {
path = globalParams->findFontFile(substName);
if (path) {
if (std::optional<GfxFontLoc> fontLoc = getExternalFont(path, false)) {
error(errSyntaxWarning, -1, "Substituting font '{0:s}' for '{1:s}'", base14SubstFonts[substIdx], name ? name->c_str() : "");
name = base14SubstFonts[substIdx];
fontLoc->substIdx = substIdx;
return fontLoc;
}
}
}
// failed to find a substitute font
return std::nullopt;
}
// failed to find a substitute font
return std::nullopt;
}
std::optional<GfxFontLoc> GfxFont::getExternalFont(GooString *path, bool cid)
{
FoFiIdentifierType fft;
GfxFontType fontType;
fft = FoFiIdentifier::identifyFile(path->c_str());
switch (fft) {
case fofiIdType1PFA:
case fofiIdType1PFB:
fontType = fontType1;
break;
case fofiIdCFF8Bit:
fontType = fontType1C;
break;
case fofiIdCFFCID:
fontType = fontCIDType0C;
break;
case fofiIdTrueType:
case fofiIdTrueTypeCollection:
fontType = cid ? fontCIDType2 : fontTrueType;
break;
case fofiIdOpenTypeCFF8Bit:
fontType = fontType1COT;
break;
case fofiIdOpenTypeCFFCID:
fontType = fontCIDType0COT;
break;
case fofiIdUnknown:
case fofiIdError:
default:
fontType = fontUnknownType;
break;
}
if (fontType == fontUnknownType || (cid ? (fontType < fontCIDType0) : (fontType >= fontCIDType0))) {
delete path;
return std::nullopt;
}
GfxFontLoc fontLoc;
fontLoc.locType = gfxFontLocExternal;
fontLoc.fontType = fontType;
fontLoc.setPath(path);
return fontLoc;
}
std::optional<std::vector<unsigned char>> GfxFont::readEmbFontFile(XRef *xref)
{
Stream *str;
Object obj1(embFontID);
Object obj2 = obj1.fetch(xref);
if (!obj2.isStream()) {
error(errSyntaxError, -1, "Embedded font file is not a stream");
embFontID = Ref::INVALID();
return {};
}
str = obj2.getStream();
std::vector<unsigned char> buf = str->toUnsignedChars();
str->close();
return buf;
}
struct AlternateNameMap
{
const char *name;
const char *alt;
};
static const AlternateNameMap alternateNameMap[] = { { "fi", "f_i" }, { "fl", "f_l" }, { "ff", "f_f" }, { "ffi", "f_f_i" }, { "ffl", "f_f_l" }, { nullptr, nullptr } };
const char *GfxFont::getAlternateName(const char *name)
{
const AlternateNameMap *map = alternateNameMap;
while (map->name) {
if (strcmp(name, map->name) == 0) {
return map->alt;
}
map++;
}
return nullptr;
}
//------------------------------------------------------------------------
// Gfx8BitFont
//------------------------------------------------------------------------
// Parse character names of the form 'Axx', 'xx', 'Ann', 'ABnn', or
// 'nn', where 'A' and 'B' are any letters, 'xx' is two hex digits,
// and 'nn' is decimal digits.
static bool parseNumericName(const char *s, bool hex, unsigned int *u)
{
char *endptr;
// Strip leading alpha characters.
if (hex) {
int n = 0;
// Get string length while ignoring junk at end.
while (isalnum(s[n])) {
++n;
}
// Only 2 hex characters with optional leading alpha is allowed.
if (n == 3 && isalpha(*s)) {
++s;
} else if (n != 2) {
return false;
}
} else {
// Strip up to two alpha characters.
for (int i = 0; i < 2 && isalpha(*s); ++i) {
++s;
}
}
int v = strtol(s, &endptr, hex ? 16 : 10);
if (endptr == s) {
return false;
}
// Skip trailing junk characters.
while (*endptr != '\0' && !isalnum(*endptr)) {
++endptr;
}
if (*endptr == '\0') {
if (u) {
*u = v;
}
return true;
}
return false;
}
// Returns true if the font has character names like xx or Axx which
// should be parsed for hex or decimal values.
static bool testForNumericNames(Dict *fontDict, bool hex)
{
bool numeric = true;
Object enc = fontDict->lookup("Encoding");
if (!enc.isDict()) {
return false;
}
Object diff = enc.dictLookup("Differences");
if (!diff.isArray()) {
return false;
}
for (int i = 0; i < diff.arrayGetLength() && numeric; ++i) {
Object obj = diff.arrayGet(i);
if (obj.isInt()) {
// All sequences must start between character codes 0 and 5.
if (obj.getInt() > 5) {
numeric = false;
}
} else if (obj.isName()) {
// All character names must successfully parse.
if (!parseNumericName(obj.getName(), hex, nullptr)) {
numeric = false;
}
} else {
numeric = false;
}
}
return numeric;
}
Gfx8BitFont::Gfx8BitFont(XRef *xref, const char *tagA, Ref idA, std::optional<std::string> &&nameA, GfxFontType typeA, Ref embFontIDA, Dict *fontDict) : GfxFont(tagA, idA, std::move(nameA), typeA, embFontIDA)
{
const BuiltinFont *builtinFont;
const char **baseEnc;
bool baseEncFromFontFile;
int len;
FoFiType1 *ffT1;
FoFiType1C *ffT1C;
char *charName;
bool missing, hex;
bool numeric;
Unicode toUnicode[256];
Unicode uBuf[8];
double mul;
int firstChar, lastChar;
unsigned short w;
Object obj1;
int n, a, b, m;
ctu = nullptr;
// do font name substitution for various aliases of the Base 14 font
// names
base14 = nullptr;
if (name) {
std::string name2 = *name;
size_t i = 0;
while (i < name2.size()) {
if (name2[i] == ' ') {
name2.erase(i, 1);
} else {
++i;
}
}
a = 0;
b = sizeof(base14FontMap) / sizeof(Base14FontMapEntry);