- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOtlDump.php
More file actions
4369 lines (3964 loc) · 163 KB
/
OtlDump.php
File metadata and controls
4369 lines (3964 loc) · 163 KB
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
<?php
namespace Mpdf;
// Define the value used in the "head" table of a created TTF file
// 0x74727565 "true" for Mac
// 0x00010000 for Windows
// Either seems to work for a font embedded in a PDF file
// when read by Adobe Reader on a Windows PC(!)
use Mpdf\Fonts\GlyphOperator;
if (!defined('_TTF_MAC_HEADER')) {
define("_TTF_MAC_HEADER", false);
}
// Recalculate correct metadata/profiles when making subset fonts (not SIP/SMP)
// e.g. xMin, xMax, maxNContours
if (!defined('_RECALC_PROFILE')) {
define("_RECALC_PROFILE", false);
}
// mPDF 5.7.1
if (!function_exists('Mpdf\unicode_hex')) {
function unicode_hex($unicode_dec)
{
return (sprintf("%05s", strtoupper(dechex($unicode_dec))));
}
}
class OtlDump
{
var $GPOSFeatures; // mPDF 5.7.1
var $GPOSLookups; // mPDF 5.7.1
var $GPOSScriptLang; // mPDF 5.7.1
var $ignoreStrings; // mPDF 5.7.1
var $MarkAttachmentType; // mPDF 5.7.1
var $MarkGlyphSets; // mPDF 7.5.1
var $GlyphClassMarks; // mPDF 5.7.1
var $GlyphClassLigatures; // mPDF 5.7.1
var $GlyphClassBases; // mPDF 5.7.1
var $GlyphClassComponents; // mPDF 5.7.1
var $GSUBScriptLang; // mPDF 5.7.1
var $rtlPUAstr; // mPDF 5.7.1
var $rtlPUAarr; // mPDF 5.7.1
var $fontkey; // mPDF 5.7.1
var $useOTL; // mPDF 5.7.1
var $panose;
var $maxUni;
var $sFamilyClass;
var $sFamilySubClass;
var $sipset;
var $smpset;
var $_pos;
var $numTables;
var $searchRange;
var $entrySelector;
var $rangeShift;
var $tables;
var $otables;
var $filename;
var $fh;
var $glyphPos;
var $charToGlyph;
var $ascent;
var $descent;
var $name;
var $familyName;
var $styleName;
var $fullName;
var $uniqueFontID;
var $unitsPerEm;
var $bbox;
var $capHeight;
var $stemV;
var $italicAngle;
var $flags;
var $underlinePosition;
var $underlineThickness;
var $charWidths;
var $defaultWidth;
var $maxStrLenRead;
var $numTTCFonts;
var $TTCFonts;
var $maxUniChar;
var $kerninfo;
var $mode;
var $glyphToChar;
var $fontRevision;
var $glyphdata;
var $glyphIDtoUn;
var $restrictedUse;
var $GSUBFeatures;
var $GSUBLookups;
var $glyphIDtoUni;
var $GSLuCoverage;
var $version;
private $mpdf;
public function __construct(Mpdf $mpdf)
{
$this->mpdf = $mpdf;
$this->maxStrLenRead = 200000; // Maximum size of glyf table to read in as string (otherwise reads each glyph from file)
}
function getMetrics($file, $fontkey, $TTCfontID = 0, $debug = false, $BMPonly = false, $kerninfo = false, $useOTL = 0, $mode = null)
{
// mPDF 5.7.1
$this->mode = $mode;
$this->useOTL = $useOTL; // mPDF 5.7.1
$this->fontkey = $fontkey; // mPDF 5.7.1
$this->filename = $file;
$this->fh = fopen($file, 'rb');
if (!$this->fh) {
throw new \Mpdf\Exception\FontException(sprintf('Unable to open file "%s"', $file));
}
$this->_pos = 0;
$this->charWidths = '';
$this->glyphPos = [];
$this->charToGlyph = [];
$this->tables = [];
$this->otables = [];
$this->kerninfo = [];
$this->ascent = 0;
$this->descent = 0;
$this->numTTCFonts = 0;
$this->TTCFonts = [];
$this->version = $version = $this->read_ulong();
$this->panose = [];
if ($version == 0x4F54544F) {
throw new \Mpdf\Exception\FontException("Postscript outlines are not supported");
}
if ($version == 0x74746366 && !$TTCfontID) {
throw new \Mpdf\Exception\FontException("TTCfontID for a TrueType Collection has to be defined in ttfontdata configuration key (" . $file . ")");
}
if (!in_array($version, [0x00010000, 0x74727565]) && !$TTCfontID) {
throw new \Mpdf\Exception\FontException("Not a TrueType font: version=" . $version);
}
if ($TTCfontID > 0) {
$this->version = $version = $this->read_ulong(); // TTC Header version now
if (!in_array($version, [0x00010000, 0x00020000])) {
throw new \Mpdf\Exception\FontException("Error parsing TrueType Collection: version=" . $version . " - " . $file);
}
$this->numTTCFonts = $this->read_ulong();
for ($i = 1; $i <= $this->numTTCFonts; $i++) {
$this->TTCFonts[$i]['offset'] = $this->read_ulong();
}
$this->seek($this->TTCFonts[$TTCfontID]['offset']);
$this->version = $version = $this->read_ulong(); // TTFont version again now
}
$this->readTableDirectory($debug);
$this->extractInfo($debug, $BMPonly, $kerninfo, $useOTL);
fclose($this->fh);
}
function readTableDirectory($debug = false)
{
$this->numTables = $this->read_ushort();
$this->searchRange = $this->read_ushort();
$this->entrySelector = $this->read_ushort();
$this->rangeShift = $this->read_ushort();
$this->tables = [];
for ($i = 0; $i < $this->numTables; $i++) {
$record = [];
$record['tag'] = $this->read_tag();
$record['checksum'] = [$this->read_ushort(), $this->read_ushort()];
$record['offset'] = $this->read_ulong();
$record['length'] = $this->read_ulong();
$this->tables[$record['tag']] = $record;
}
if ($debug) {
$this->checksumTables();
}
}
function checksumTables()
{
// Check the checksums for all tables
foreach ($this->tables as $t) {
if ($t['length'] > 0 && $t['length'] < $this->maxStrLenRead) { // 1.02
$table = $this->get_chunk($t['offset'], $t['length']);
$checksum = $this->calcChecksum($table);
if ($t['tag'] == 'head') {
$up = unpack('n*', substr($table, 8, 4));
$adjustment[0] = $up[1];
$adjustment[1] = $up[2];
$checksum = $this->sub32($checksum, $adjustment);
}
$xchecksum = $t['checksum'];
if ($xchecksum != $checksum) {
throw new \Mpdf\Exception\FontException(sprintf('TTF file "%s": invalid checksum %s table: %s (expected %s)', $this->filename, dechex($checksum[0]) . dechex($checksum[1]), $t['tag'], dechex($xchecksum[0]) . dechex($xchecksum[1])));
}
}
}
}
function sub32($x, $y)
{
$xlo = $x[1];
$xhi = $x[0];
$ylo = $y[1];
$yhi = $y[0];
if ($ylo > $xlo) {
$xlo += 1 << 16;
$yhi += 1;
}
$reslo = $xlo - $ylo;
if ($yhi > $xhi) {
$xhi += 1 << 16;
}
$reshi = $xhi - $yhi;
$reshi = $reshi & 0xFFFF;
return [$reshi, $reslo];
}
function calcChecksum($data)
{
if (strlen($data) % 4) {
$data .= str_repeat("\0", (4 - (strlen($data) % 4)));
}
$len = strlen($data);
$hi = 0x0000;
$lo = 0x0000;
for ($i = 0; $i < $len; $i += 4) {
$hi += (ord($data[$i]) << 8) + ord($data[$i + 1]);
$lo += (ord($data[$i + 2]) << 8) + ord($data[$i + 3]);
$hi += ($lo >> 16) & 0xFFFF;
$lo = $lo & 0xFFFF;
}
return [$hi, $lo];
}
function get_table_pos($tag)
{
$offset = isset($this->tables[$tag]['offset']) ? $this->tables[$tag]['offset'] : null;
$length = isset($this->tables[$tag]['length']) ? $this->tables[$tag]['length'] : null;
return [$offset, $length];
}
function seek($pos)
{
$this->_pos = $pos;
fseek($this->fh, $this->_pos);
}
function skip($delta)
{
$this->_pos = $this->_pos + $delta;
fseek($this->fh, $delta, SEEK_CUR);
}
function seek_table($tag, $offset_in_table = 0)
{
$tpos = $this->get_table_pos($tag);
$this->_pos = $tpos[0] + $offset_in_table;
fseek($this->fh, $this->_pos);
return $this->_pos;
}
function read_tag()
{
$this->_pos += 4;
return fread($this->fh, 4);
}
function read_short()
{
$this->_pos += 2;
$s = fread($this->fh, 2);
$a = (ord($s[0]) << 8) + ord($s[1]);
if ($a & (1 << 15)) {
$a = ($a - (1 << 16));
}
return $a;
}
function unpack_short($s)
{
$a = (ord($s[0]) << 8) + ord($s[1]);
if ($a & (1 << 15)) {
$a = ($a - (1 << 16));
}
return $a;
}
function read_ushort()
{
$this->_pos += 2;
$s = fread($this->fh, 2);
return (ord($s[0]) << 8) + ord($s[1]);
}
function read_ulong()
{
$this->_pos += 4;
$s = fread($this->fh, 4);
// if large uInt32 as an integer, PHP converts it to -ve
return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]); // 16777216 = 1<<24
}
function get_ushort($pos)
{
fseek($this->fh, $pos);
$s = fread($this->fh, 2);
return (ord($s[0]) << 8) + ord($s[1]);
}
function get_ulong($pos)
{
fseek($this->fh, $pos);
$s = fread($this->fh, 4);
// iF large uInt32 as an integer, PHP converts it to -ve
return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]); // 16777216 = 1<<24
}
function pack_short($val)
{
if ($val < 0) {
$val = abs($val);
$val = ~$val;
$val += 1;
}
return pack("n", $val);
}
function splice($stream, $offset, $value)
{
return substr($stream, 0, $offset) . $value . substr($stream, $offset + strlen($value));
}
function _set_ushort($stream, $offset, $value)
{
$up = pack("n", $value);
return $this->splice($stream, $offset, $up);
}
function _set_short($stream, $offset, $val)
{
if ($val < 0) {
$val = abs($val);
$val = ~$val;
$val += 1;
}
$up = pack("n", $val);
return $this->splice($stream, $offset, $up);
}
function get_chunk($pos, $length)
{
fseek($this->fh, $pos);
if ($length < 1) {
return '';
}
return (fread($this->fh, $length));
}
function get_table($tag)
{
list($pos, $length) = $this->get_table_pos($tag);
if ($length == 0) {
return '';
}
fseek($this->fh, $pos);
return (fread($this->fh, $length));
}
function add($tag, $data)
{
if ($tag == 'head') {
$data = $this->splice($data, 8, "\0\0\0\0");
}
$this->otables[$tag] = $data;
}
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
function extractInfo($debug = false, $BMPonly = false, $kerninfo = false, $useOTL = 0)
{
$this->panose = [];
$this->sFamilyClass = 0;
$this->sFamilySubClass = 0;
///////////////////////////////////
// name - Naming table
///////////////////////////////////
$name_offset = $this->seek_table("name");
$format = $this->read_ushort();
if ($format != 0 && $format != 1) {
throw new \Mpdf\Exception\FontException("Error loading font: Unknown name table format " . $format);
}
$numRecords = $this->read_ushort();
$string_data_offset = $name_offset + $this->read_ushort();
$names = [1 => '', 2 => '', 3 => '', 4 => '', 6 => ''];
$K = array_keys($names);
$nameCount = count($names);
for ($i = 0; $i < $numRecords; $i++) {
$platformId = $this->read_ushort();
$encodingId = $this->read_ushort();
$languageId = $this->read_ushort();
$nameId = $this->read_ushort();
$length = $this->read_ushort();
$offset = $this->read_ushort();
if (!in_array($nameId, $K)) {
continue;
}
$N = '';
if ($platformId == 3 && $encodingId == 1 && $languageId == 0x409) { // Microsoft, Unicode, US English, PS Name
$opos = $this->_pos;
$this->seek($string_data_offset + $offset);
if ($length % 2 != 0) {
throw new \Mpdf\Exception\FontException("Error loading font: PostScript name is UTF-16BE string of odd length");
}
$length /= 2;
$N = '';
while ($length > 0) {
$char = $this->read_ushort();
$N .= (chr($char));
$length -= 1;
}
$this->_pos = $opos;
$this->seek($opos);
} else {
if ($platformId == 1 && $encodingId == 0 && $languageId == 0) { // Macintosh, Roman, English, PS Name
$opos = $this->_pos;
$N = $this->get_chunk($string_data_offset + $offset, $length);
$this->_pos = $opos;
$this->seek($opos);
}
}
if ($N && $names[$nameId] == '') {
$names[$nameId] = $N;
$nameCount -= 1;
if ($nameCount == 0) {
break;
}
}
}
if ($names[6]) {
$psName = $names[6];
} else {
if ($names[4]) {
$psName = preg_replace('/ /', '-', $names[4]);
} else {
if ($names[1]) {
$psName = preg_replace('/ /', '-', $names[1]);
} else {
$psName = '';
}
}
}
if (!$psName) {
throw new \Mpdf\Exception\FontException("Error loading font: Could not find PostScript font name: " . $this->filename);
}
if ($debug) {
for ($i = 0; $i < count($psName); $i++) {
$c = $psName[$i];
$oc = ord($c);
if ($oc > 126 || strpos(' [](){}<>/%', $c) !== false) {
throw new \Mpdf\Exception\FontException("psName=" . $psName . " contains invalid character " . $c . " ie U+" . ord(c));
}
}
}
$this->name = $psName;
if ($names[1]) {
$this->familyName = $names[1];
} else {
$this->familyName = $psName;
}
if ($names[2]) {
$this->styleName = $names[2];
} else {
$this->styleName = 'Regular';
}
if ($names[4]) {
$this->fullName = $names[4];
} else {
$this->fullName = $psName;
}
if ($names[3]) {
$this->uniqueFontID = $names[3];
} else {
$this->uniqueFontID = $psName;
}
if ($names[6]) {
$this->fullName = $names[6];
}
///////////////////////////////////
// head - Font header table
///////////////////////////////////
$this->seek_table("head");
if ($debug) {
$ver_maj = $this->read_ushort();
$ver_min = $this->read_ushort();
if ($ver_maj != 1) {
throw new \Mpdf\Exception\FontException('Error loading font: Unknown head table version ' . $ver_maj . '.' . $ver_min);
}
$this->fontRevision = $this->read_ushort() . $this->read_ushort();
$this->skip(4);
$magic = $this->read_ulong();
if ($magic != 0x5F0F3CF5) {
throw new \Mpdf\Exception\FontException('Error loading font: Invalid head table magic ' . $magic);
}
$this->skip(2);
} else {
$this->skip(18);
}
$this->unitsPerEm = $unitsPerEm = $this->read_ushort();
$scale = 1000 / $unitsPerEm;
$this->skip(16);
$xMin = $this->read_short();
$yMin = $this->read_short();
$xMax = $this->read_short();
$yMax = $this->read_short();
$this->bbox = [($xMin * $scale), ($yMin * $scale), ($xMax * $scale), ($yMax * $scale)];
$this->skip(3 * 2);
$indexToLocFormat = $this->read_ushort();
$glyphDataFormat = $this->read_ushort();
if ($glyphDataFormat != 0) {
throw new \Mpdf\Exception\FontException('Error loading font: Unknown glyph data format ' . $glyphDataFormat);
}
///////////////////////////////////
// hhea metrics table
///////////////////////////////////
// ttf2t1 seems to use this value rather than the one in OS/2 - so put in for compatibility
if (isset($this->tables["hhea"])) {
$this->seek_table("hhea");
$this->skip(4);
$hheaAscender = $this->read_short();
$hheaDescender = $this->read_short();
$this->ascent = ($hheaAscender * $scale);
$this->descent = ($hheaDescender * $scale);
}
///////////////////////////////////
// OS/2 - OS/2 and Windows metrics table
///////////////////////////////////
if (isset($this->tables["OS/2"])) {
$this->seek_table("OS/2");
$version = $this->read_ushort();
$this->skip(2);
$usWeightClass = $this->read_ushort();
$this->skip(2);
$fsType = $this->read_ushort();
if ($fsType == 0x0002 || ($fsType & 0x0300) != 0) {
global $overrideTTFFontRestriction;
if (!$overrideTTFFontRestriction) {
throw new \Mpdf\Exception\FontException('Font file ' . $this->filename . ' cannot be embedded due to copyright restrictions.');
}
$this->restrictedUse = true;
}
$this->skip(20);
$sF = $this->read_short();
$this->sFamilyClass = ($sF >> 8);
$this->sFamilySubClass = ($sF & 0xFF);
$this->_pos += 10; //PANOSE = 10 byte length
$panose = fread($this->fh, 10);
$this->panose = [];
for ($p = 0; $p < strlen($panose); $p++) {
$this->panose[] = ord($panose[$p]);
}
$this->skip(26);
$sTypoAscender = $this->read_short();
$sTypoDescender = $this->read_short();
if (!$this->ascent) {
$this->ascent = ($sTypoAscender * $scale);
}
if (!$this->descent) {
$this->descent = ($sTypoDescender * $scale);
}
if ($version > 1) {
$this->skip(16);
$sCapHeight = $this->read_short();
$this->capHeight = ($sCapHeight * $scale);
} else {
$this->capHeight = $this->ascent;
}
} else {
$usWeightClass = 500;
if (!$this->ascent) {
$this->ascent = ($yMax * $scale);
}
if (!$this->descent) {
$this->descent = ($yMin * $scale);
}
$this->capHeight = $this->ascent;
}
$this->stemV = 50 + intval(pow(($usWeightClass / 65.0), 2));
///////////////////////////////////
// post - PostScript table
///////////////////////////////////
$this->seek_table("post");
if ($debug) {
$ver_maj = $this->read_ushort();
$ver_min = $this->read_ushort();
if ($ver_maj < 1 || $ver_maj > 4) {
throw new \Mpdf\Exception\FontException('Error loading font: Unknown post table version ' . $ver_maj);
}
} else {
$this->skip(4);
}
$this->italicAngle = $this->read_short() + $this->read_ushort() / 65536.0;
$this->underlinePosition = $this->read_short() * $scale;
$this->underlineThickness = $this->read_short() * $scale;
$isFixedPitch = $this->read_ulong();
$this->flags = 4;
if ($this->italicAngle != 0) {
$this->flags = $this->flags | 64;
}
if ($usWeightClass >= 600) {
$this->flags = $this->flags | 262144;
}
if ($isFixedPitch) {
$this->flags = $this->flags | 1;
}
///////////////////////////////////
// hhea - Horizontal header table
///////////////////////////////////
$this->seek_table("hhea");
if ($debug) {
$ver_maj = $this->read_ushort();
$ver_min = $this->read_ushort();
if ($ver_maj != 1) {
throw new \Mpdf\Exception\FontException(sprintf('Error loading font: Unknown hhea table version %s', $ver_maj));
}
$this->skip(28);
} else {
$this->skip(32);
}
$metricDataFormat = $this->read_ushort();
if ($metricDataFormat != 0) {
throw new \Mpdf\Exception\FontException('Error loading font: Unknown horizontal metric data format ' . $metricDataFormat);
}
$numberOfHMetrics = $this->read_ushort();
if ($numberOfHMetrics == 0) {
throw new \Mpdf\Exception\FontException('Error loading font: Number of horizontal metrics is 0');
}
///////////////////////////////////
// maxp - Maximum profile table
///////////////////////////////////
$this->seek_table("maxp");
if ($debug) {
$ver_maj = $this->read_ushort();
$ver_min = $this->read_ushort();
if ($ver_maj != 1) {
throw new \Mpdf\Exception\FontException('Error loading font: Unknown maxp table version ' . $ver_maj);
}
} else {
$this->skip(4);
}
$numGlyphs = $this->read_ushort();
///////////////////////////////////
// cmap - Character to glyph index mapping table
///////////////////////////////////
$cmap_offset = $this->seek_table("cmap");
$this->skip(2);
$cmapTableCount = $this->read_ushort();
$unicode_cmap_offset = 0;
for ($i = 0; $i < $cmapTableCount; $i++) {
$platformID = $this->read_ushort();
$encodingID = $this->read_ushort();
$offset = $this->read_ulong();
$save_pos = $this->_pos;
if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode
$format = $this->get_ushort($cmap_offset + $offset);
if ($format == 4) {
if (!$unicode_cmap_offset) {
$unicode_cmap_offset = $cmap_offset + $offset;
}
if ($BMPonly) {
break;
}
}
} // Microsoft, Unicode Format 12 table HKCS
else {
if ((($platformID == 3 && $encodingID == 10) || $platformID == 0) && !$BMPonly) {
$format = $this->get_ushort($cmap_offset + $offset);
if ($format == 12) {
$unicode_cmap_offset = $cmap_offset + $offset;
break;
}
}
}
$this->seek($save_pos);
}
if (!$unicode_cmap_offset) {
throw new \Mpdf\Exception\FontException('Font (' . $this->filename . ') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)');
}
$sipset = false;
$smpset = false;
// mPDF 5.7.1
$this->GSUBScriptLang = [];
$this->rtlPUAstr = '';
$this->rtlPUAarr = [];
$this->GSUBFeatures = [];
$this->GSUBLookups = [];
$this->GPOSScriptLang = [];
$this->GPOSFeatures = [];
$this->GPOSLookups = [];
$this->glyphIDtoUni = '';
// Format 12 CMAP does characters above Unicode BMP i.e. some HKCS characters U+20000 and above
if ($format == 12 && !$BMPonly) {
$this->maxUniChar = 0;
$this->seek($unicode_cmap_offset + 4);
$length = $this->read_ulong();
$limit = $unicode_cmap_offset + $length;
$this->skip(4);
$nGroups = $this->read_ulong();
$glyphToChar = [];
$charToGlyph = [];
for ($i = 0; $i < $nGroups; $i++) {
$startCharCode = $this->read_ulong();
$endCharCode = $this->read_ulong();
$startGlyphCode = $this->read_ulong();
if ($endCharCode > 0x20000 && $endCharCode < 0x2FFFF) {
$sipset = true;
} else {
if ($endCharCode > 0x10000 && $endCharCode < 0x1FFFF) {
$smpset = true;
}
}
$offset = 0;
for ($unichar = $startCharCode; $unichar <= $endCharCode; $unichar++) {
$glyph = $startGlyphCode + $offset;
$offset++;
if ($unichar < 0x30000) {
$charToGlyph[$unichar] = $glyph;
$this->maxUniChar = max($unichar, $this->maxUniChar);
$glyphToChar[$glyph][] = $unichar;
}
}
}
} else {
$glyphToChar = [];
$charToGlyph = [];
$this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph);
}
$this->sipset = $sipset;
$this->smpset = $smpset;
///////////////////////////////////
// mPDF 5.7.1
// Map Unmapped glyphs - from $numGlyphs
if ($this->useOTL) {
$bctr = 0xE000;
for ($gid = 1; $gid < $numGlyphs; $gid++) {
if (!isset($glyphToChar[$gid])) {
while (isset($charToGlyph[$bctr])) {
$bctr++;
} // Avoid overwriting a glyph already mapped in PUA
if (($bctr > 0xF8FF) && ($bctr < 0x2CEB0)) {
if (!$BMPonly) {
$bctr = 0x2CEB0; // Use unassigned area 0x2CEB0 to 0x2F7FF (space for 10,000 characters)
$this->sipset = $sipset = true; // forces subsetting; also ensure charwidths are saved
while (isset($charToGlyph[$bctr])) {
$bctr++;
}
} else {
throw new \Mpdf\Exception\FontException(sprintf('Font "%s" does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)', $this->filename));
}
}
$glyphToChar[$gid][] = $bctr;
$charToGlyph[$bctr] = $gid;
$this->maxUniChar = max($bctr, $this->maxUniChar);
$bctr++;
}
}
}
$this->glyphToChar = $glyphToChar;
$this->charToGlyph = $charToGlyph;
///////////////////////////////////
// mPDF 5.7.1 OpenType Layout tables
$this->GSUBScriptLang = [];
$this->rtlPUAstr = '';
$this->rtlPUAarr = [];
if ($useOTL) {
$this->_getGDEFtables();
list($this->GSUBScriptLang, $this->GSUBFeatures, $this->GSUBLookups, $this->rtlPUAstr, $this->rtlPUAarr) = $this->_getGSUBtables();
list($this->GPOSScriptLang, $this->GPOSFeatures, $this->GPOSLookups) = $this->_getGPOStables();
$this->glyphIDtoUni = str_pad('', 256 * 256 * 3, "\x00");
foreach ($glyphToChar as $gid => $arr) {
if (isset($glyphToChar[$gid][0])) {
$char = $glyphToChar[$gid][0];
if ($char != 0 && $char != 65535) {
$this->glyphIDtoUni[$gid * 3] = chr($char >> 16);
$this->glyphIDtoUni[$gid * 3 + 1] = chr(($char >> 8) & 0xFF);
$this->glyphIDtoUni[$gid * 3 + 2] = chr($char & 0xFF);
}
}
}
}
///////////////////////////////////
///////////////////////////////////
// hmtx - Horizontal metrics table
///////////////////////////////////
$this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale);
///////////////////////////////////
// kern - Kerning pair table
///////////////////////////////////
if ($kerninfo) {
// Recognises old form of Kerning table - as required by Windows - Format 0 only
$kern_offset = $this->seek_table("kern");
$version = $this->read_ushort();
$nTables = $this->read_ushort();
// subtable header
$sversion = $this->read_ushort();
$slength = $this->read_ushort();
$scoverage = $this->read_ushort();
$format = $scoverage >> 8;
if ($kern_offset && $version == 0 && $format == 0) {
// Format 0
$nPairs = $this->read_ushort();
$this->skip(6);
for ($i = 0; $i < $nPairs; $i++) {
$left = $this->read_ushort();
$right = $this->read_ushort();
$val = $this->read_short();
if (count($glyphToChar[$left]) == 1 && count($glyphToChar[$right]) == 1) {
if ($left != 32 && $right != 32) {
$this->kerninfo[$glyphToChar[$left][0]][$glyphToChar[$right][0]] = intval($val * $scale);
}
}
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
function _getGDEFtables()
{
///////////////////////////////////
// GDEF - Glyph Definition
///////////////////////////////////
// http://www.microsoft.com/typography/otspec/gdef.htm
if (isset($this->tables["GDEF"])) {
if ($this->mode == 'summary') {
$this->mpdf->WriteHTML('<h1>GDEF table</h1>');
}
$gdef_offset = $this->seek_table("GDEF");
// ULONG Version of the GDEF table-currently 0x00010000
$ver_maj = $this->read_ushort();
$ver_min = $this->read_ushort();
// Version 0x00010002 of GDEF header contains additional Offset to a list defining mark glyph set definitions (MarkGlyphSetDef)
$GlyphClassDef_offset = $this->read_ushort();
$AttachList_offset = $this->read_ushort();
$LigCaretList_offset = $this->read_ushort();
$MarkAttachClassDef_offset = $this->read_ushort();
if ($ver_min == 2) {
$MarkGlyphSetsDef_offset = $this->read_ushort();
}
// GlyphClassDef
$this->seek($gdef_offset + $GlyphClassDef_offset);
/*
1 Base glyph (single character, spacing glyph)
2 Ligature glyph (multiple character, spacing glyph)
3 Mark glyph (non-spacing combining glyph)
4 Component glyph (part of single character, spacing glyph)
*/
$GlyphByClass = $this->_getClassDefinitionTable();
if ($this->mode == 'summary') {
$this->mpdf->WriteHTML('<h2>Glyph classes</h2>');
}
if (isset($GlyphByClass[1]) && count($GlyphByClass[1]) > 0) {
$this->GlyphClassBases = $this->formatClassArr($GlyphByClass[1]);
if ($this->mode == 'summary') {
$this->mpdf->WriteHTML('<h3>Glyph class 1</h3>');
$this->mpdf->WriteHTML('<h5>Base glyph (single character, spacing glyph)</h5>');
$html = '';
$html .= '<div class="glyphs">';
foreach ($GlyphByClass[1] as $g) {
$html .= '&#x' . $g . '; ';
}
$html .= '</div>';
$this->mpdf->WriteHTML($html);
}
} else {
$this->GlyphClassBases = '';
}
if (isset($GlyphByClass[2]) && count($GlyphByClass[2]) > 0) {
$this->GlyphClassLigatures = $this->formatClassArr($GlyphByClass[2]);
if ($this->mode == 'summary') {
$this->mpdf->WriteHTML('<h3>Glyph class 2</h3>');
$this->mpdf->WriteHTML('<h5>Ligature glyph (multiple character, spacing glyph)</h5>');
$html = '';
$html .= '<div class="glyphs">';
foreach ($GlyphByClass[2] as $g) {
$html .= '&#x' . $g . '; ';
}
$html .= '</div>';
$this->mpdf->WriteHTML($html);
}
} else {
$this->GlyphClassLigatures = '';
}