Skip to content

Commit dedde5a

Browse files
committed
Dedicated FontException, cleaner font-related font exception messages (Closes mpdf#1209)
1 parent f0a44d9 commit dedde5a

File tree

4 files changed

+86
-75
lines changed

4 files changed

+86
-75
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
mPDF 8.1.x
22
===========================
3+
34
* Add Page Number Myanmar Language Support
5+
* new `Mpdf\Exception\FontException` extending base `MpdfException` was introduced and is thrown on Font manipulation
6+
* A bit cleaner exception messages for font-related errors
47

58
mPDF 8.0.x
69
===========================

src/Exception/FontException.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Mpdf\Exception;
4+
5+
class FontException extends \Mpdf\MpdfException
6+
{
7+
8+
}

src/OtlDump.php

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ function getMetrics($file, $fontkey, $TTCfontID = 0, $debug = false, $BMPonly =
178178
$this->fh = fopen($file, 'rb');
179179

180180
if (!$this->fh) {
181-
throw new \Mpdf\MpdfException(sprintf('Unable to open file "%s"', $file));
181+
throw new \Mpdf\Exception\FontException(sprintf('Unable to open file "%s"', $file));
182182
}
183183

184184
$this->_pos = 0;
@@ -196,21 +196,21 @@ function getMetrics($file, $fontkey, $TTCfontID = 0, $debug = false, $BMPonly =
196196
$this->panose = [];
197197

198198
if ($version == 0x4F54544F) {
199-
throw new \Mpdf\MpdfException("Postscript outlines are not supported");
199+
throw new \Mpdf\Exception\FontException("Postscript outlines are not supported");
200200
}
201201

202202
if ($version == 0x74746366 && !$TTCfontID) {
203-
throw new \Mpdf\MpdfException("TTCfontID for a TrueType Collection has to be defined in ttfontdata configuration key (" . $file . ")");
203+
throw new \Mpdf\Exception\FontException("TTCfontID for a TrueType Collection has to be defined in ttfontdata configuration key (" . $file . ")");
204204
}
205205

206206
if (!in_array($version, [0x00010000, 0x74727565]) && !$TTCfontID) {
207-
throw new \Mpdf\MpdfException("Not a TrueType font: version=" . $version);
207+
throw new \Mpdf\Exception\FontException("Not a TrueType font: version=" . $version);
208208
}
209209

210210
if ($TTCfontID > 0) {
211211
$this->version = $version = $this->read_ulong(); // TTC Header version now
212212
if (!in_array($version, [0x00010000, 0x00020000])) {
213-
throw new \Mpdf\MpdfException("Error parsing TrueType Collection: version=" . $version . " - " . $file);
213+
throw new \Mpdf\Exception\FontException("Error parsing TrueType Collection: version=" . $version . " - " . $file);
214214
}
215215
$this->numTTCFonts = $this->read_ulong();
216216
for ($i = 1; $i <= $this->numTTCFonts; $i++) {
@@ -259,7 +259,7 @@ function checksumTables()
259259
}
260260
$xchecksum = $t['checksum'];
261261
if ($xchecksum != $checksum) {
262-
throw new \Mpdf\MpdfException(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])));
262+
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])));
263263
}
264264
}
265265
}
@@ -473,7 +473,7 @@ function extractInfo($debug = false, $BMPonly = false, $kerninfo = false, $useOT
473473
$name_offset = $this->seek_table("name");
474474
$format = $this->read_ushort();
475475
if ($format != 0 && $format != 1) {
476-
throw new \Mpdf\MpdfException("Unknown name table format " . $format);
476+
throw new \Mpdf\Exception\FontException("Error loading font: Unknown name table format " . $format);
477477
}
478478
$numRecords = $this->read_ushort();
479479
$string_data_offset = $name_offset + $this->read_ushort();
@@ -495,7 +495,7 @@ function extractInfo($debug = false, $BMPonly = false, $kerninfo = false, $useOT
495495
$opos = $this->_pos;
496496
$this->seek($string_data_offset + $offset);
497497
if ($length % 2 != 0) {
498-
throw new \Mpdf\MpdfException("PostScript name is UTF-16BE string of odd length");
498+
throw new \Mpdf\Exception\FontException("Error loading font: PostScript name is UTF-16BE string of odd length");
499499
}
500500
$length /= 2;
501501
$N = '';
@@ -536,14 +536,14 @@ function extractInfo($debug = false, $BMPonly = false, $kerninfo = false, $useOT
536536
}
537537
}
538538
if (!$psName) {
539-
throw new \Mpdf\MpdfException("Could not find PostScript font name: " . $this->filename);
539+
throw new \Mpdf\Exception\FontException("Error loading font: Could not find PostScript font name: " . $this->filename);
540540
}
541541
if ($debug) {
542542
for ($i = 0; $i < count($psName); $i++) {
543543
$c = $psName[$i];
544544
$oc = ord($c);
545545
if ($oc > 126 || strpos(' [](){}<>/%', $c) !== false) {
546-
throw new \Mpdf\MpdfException("psName=" . $psName . " contains invalid character " . $c . " ie U+" . ord(c));
546+
throw new \Mpdf\Exception\FontException("psName=" . $psName . " contains invalid character " . $c . " ie U+" . ord(c));
547547
}
548548
}
549549
}
@@ -581,14 +581,14 @@ function extractInfo($debug = false, $BMPonly = false, $kerninfo = false, $useOT
581581
$ver_maj = $this->read_ushort();
582582
$ver_min = $this->read_ushort();
583583
if ($ver_maj != 1) {
584-
throw new \Mpdf\MpdfException('Unknown head table version ' . $ver_maj . '.' . $ver_min);
584+
throw new \Mpdf\Exception\FontException('Error loading font: Unknown head table version ' . $ver_maj . '.' . $ver_min);
585585
}
586586
$this->fontRevision = $this->read_ushort() . $this->read_ushort();
587587

588588
$this->skip(4);
589589
$magic = $this->read_ulong();
590590
if ($magic != 0x5F0F3CF5) {
591-
throw new \Mpdf\MpdfException('Invalid head table magic ' . $magic);
591+
throw new \Mpdf\Exception\FontException('Error loading font: Invalid head table magic ' . $magic);
592592
}
593593
$this->skip(2);
594594
} else {
@@ -606,7 +606,7 @@ function extractInfo($debug = false, $BMPonly = false, $kerninfo = false, $useOT
606606
$indexToLocFormat = $this->read_ushort();
607607
$glyphDataFormat = $this->read_ushort();
608608
if ($glyphDataFormat != 0) {
609-
throw new \Mpdf\MpdfException('Unknown glyph data format ' . $glyphDataFormat);
609+
throw new \Mpdf\Exception\FontException('Error loading font: Unknown glyph data format ' . $glyphDataFormat);
610610
}
611611

612612
///////////////////////////////////
@@ -635,7 +635,7 @@ function extractInfo($debug = false, $BMPonly = false, $kerninfo = false, $useOT
635635
if ($fsType == 0x0002 || ($fsType & 0x0300) != 0) {
636636
global $overrideTTFFontRestriction;
637637
if (!$overrideTTFFontRestriction) {
638-
throw new \Mpdf\MpdfException('ERROR - Font file ' . $this->filename . ' cannot be embedded due to copyright restrictions.');
638+
throw new \Mpdf\Exception\FontException('Font file ' . $this->filename . ' cannot be embedded due to copyright restrictions.');
639639
}
640640
$this->restrictedUse = true;
641641
}
@@ -685,7 +685,7 @@ function extractInfo($debug = false, $BMPonly = false, $kerninfo = false, $useOT
685685
$ver_maj = $this->read_ushort();
686686
$ver_min = $this->read_ushort();
687687
if ($ver_maj < 1 || $ver_maj > 4) {
688-
throw new \Mpdf\MpdfException('Unknown post table version ' . $ver_maj);
688+
throw new \Mpdf\Exception\FontException('Error loading font: Unknown post table version ' . $ver_maj);
689689
}
690690
} else {
691691
$this->skip(4);
@@ -715,19 +715,19 @@ function extractInfo($debug = false, $BMPonly = false, $kerninfo = false, $useOT
715715
$ver_maj = $this->read_ushort();
716716
$ver_min = $this->read_ushort();
717717
if ($ver_maj != 1) {
718-
throw new \Mpdf\MpdfException('Unknown hhea table version ' . $ver_maj);
718+
throw new \Mpdf\Exception\FontException(sprintf('Error loading font: Unknown hhea table version %s', $ver_maj));
719719
}
720720
$this->skip(28);
721721
} else {
722722
$this->skip(32);
723723
}
724724
$metricDataFormat = $this->read_ushort();
725725
if ($metricDataFormat != 0) {
726-
throw new \Mpdf\MpdfException('Unknown horizontal metric data format ' . $metricDataFormat);
726+
throw new \Mpdf\Exception\FontException('Error loading font: Unknown horizontal metric data format ' . $metricDataFormat);
727727
}
728728
$numberOfHMetrics = $this->read_ushort();
729729
if ($numberOfHMetrics == 0) {
730-
throw new \Mpdf\MpdfException('Number of horizontal metrics is 0');
730+
throw new \Mpdf\Exception\FontException('Error loading font: Number of horizontal metrics is 0');
731731
}
732732

733733
///////////////////////////////////
@@ -738,7 +738,7 @@ function extractInfo($debug = false, $BMPonly = false, $kerninfo = false, $useOT
738738
$ver_maj = $this->read_ushort();
739739
$ver_min = $this->read_ushort();
740740
if ($ver_maj != 1) {
741-
throw new \Mpdf\MpdfException('Unknown maxp table version ' . $ver_maj);
741+
throw new \Mpdf\Exception\FontException('Error loading font: Unknown maxp table version ' . $ver_maj);
742742
}
743743
} else {
744744
$this->skip(4);
@@ -781,7 +781,7 @@ function extractInfo($debug = false, $BMPonly = false, $kerninfo = false, $useOT
781781
}
782782

783783
if (!$unicode_cmap_offset) {
784-
throw new \Mpdf\MpdfException('Font (' . $this->filename . ') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)');
784+
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)');
785785
}
786786

787787
$sipset = false;
@@ -858,7 +858,7 @@ function extractInfo($debug = false, $BMPonly = false, $kerninfo = false, $useOT
858858
$bctr++;
859859
}
860860
} else {
861-
throw new \Mpdf\MpdfException($names[1] . " : WARNING - The font does not have enough space to map all (unmapped) included glyphs into Private Use Area U+E000 - U+F8FF");
861+
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));
862862
}
863863
}
864864
$glyphToChar[$gid][] = $bctr;
@@ -1568,7 +1568,7 @@ function _getGSUBtables()
15681568
}
15691569
}
15701570
} else {
1571-
throw new \Mpdf\MpdfException("GPOS Lookup Type " . $Lookup[$i]['Type'] . ", Format " . $SubstFormat . " not supported (ttfontsuni.php).");
1571+
throw new \Mpdf\Exception\FontException("GPOS Lookup Type " . $Lookup[$i]['Type'] . ", Format " . $SubstFormat . " not supported (ttfontsuni.php).");
15721572
}
15731573
}
15741574
} // LookupType 6: Chaining Contextual Substitution Subtable
@@ -1625,7 +1625,7 @@ function _getGSUBtables()
16251625
}
16261626
}
16271627
} else {
1628-
throw new \Mpdf\MpdfException("Lookup Type " . $Lookup[$i]['Type'] . " not supported.");
1628+
throw new \Mpdf\Exception\FontException("Lookup Type " . $Lookup[$i]['Type'] . " not supported.");
16291629
}
16301630
}
16311631
}
@@ -1807,7 +1807,7 @@ function _getGSUBtables()
18071807
$glyphs = $this->_getCoverage();
18081808
$Lookup[$i]['Subtable'][$c]['CoverageInputGlyphs'][] = implode("|", $glyphs);
18091809
}
1810-
throw new \Mpdf\MpdfException("Lookup Type 5, SubstFormat 3 not tested. Please report this with the name of font used - " . $this->fontkey);
1810+
throw new \Mpdf\Exception\FontException("Lookup Type 5, SubstFormat 3 not tested. Please report this with the name of font used - " . $this->fontkey);
18111811
}
18121812
}
18131813
}
@@ -2728,7 +2728,7 @@ function _getGSUBignoreString($flag, $MarkFilteringSet)
27282728

27292729
// Flag & 0x0010 = UseMarkFilteringSet
27302730
if ($flag & 0x0010) {
2731-
throw new \Mpdf\MpdfException("This font " . $this->fontkey . " contains MarkGlyphSets");
2731+
throw new \Mpdf\Exception\FontException("This font " . $this->fontkey . " contains MarkGlyphSets");
27322732
$str = "Mark Glyph Set: ";
27332733
$str .= $this->MarkGlyphSets[$MarkFilteringSet];
27342734
}
@@ -3757,21 +3757,21 @@ function _getGPOSarray(&$Lookup, $lul, $scripttag, $level = 1, $lcoverage = '',
37573757
// Format 1:
37583758
//===========
37593759
if ($PosFormat == 1) {
3760-
throw new \Mpdf\MpdfException("GPOS Lookup Type " . $Type . " Format " . $PosFormat . " not YET TESTED.");
3760+
throw new \Mpdf\Exception\FontException("GPOS Lookup Type " . $Type . " Format " . $PosFormat . " not YET TESTED.");
37613761
} //===========
37623762
// Format 2:
37633763
//===========
37643764
else {
37653765
if ($PosFormat == 2) {
3766-
throw new \Mpdf\MpdfException("GPOS Lookup Type " . $Type . " Format " . $PosFormat . " not YET TESTED.");
3766+
throw new \Mpdf\Exception\FontException("GPOS Lookup Type " . $Type . " Format " . $PosFormat . " not YET TESTED.");
37673767
} //===========
37683768
// Format 3:
37693769
//===========
37703770
else {
37713771
if ($PosFormat == 3) {
3772-
throw new \Mpdf\MpdfException("GPOS Lookup Type " . $Type . " Format " . $PosFormat . " not YET TESTED.");
3772+
throw new \Mpdf\Exception\FontException("GPOS Lookup Type " . $Type . " Format " . $PosFormat . " not YET TESTED.");
37733773
} else {
3774-
throw new \Mpdf\MpdfException("GPOS Lookup Type " . $Type . ", Format " . $PosFormat . " not supported.");
3774+
throw new \Mpdf\Exception\FontException("GPOS Lookup Type " . $Type . ", Format " . $PosFormat . " not supported.");
37753775
}
37763776
}
37773777
}
@@ -3785,7 +3785,7 @@ function _getGPOSarray(&$Lookup, $lul, $scripttag, $level = 1, $lcoverage = '',
37853785
// Format 1:
37863786
//===========
37873787
if ($PosFormat == 1) {
3788-
throw new \Mpdf\MpdfException("GPOS Lookup Type " . $Type . " Format " . $PosFormat . " not TESTED YET.");
3788+
throw new \Mpdf\Exception\FontException("GPOS Lookup Type " . $Type . " Format " . $PosFormat . " not TESTED YET.");
37893789
} //===========
37903790
// Format 2:
37913791
//===========
@@ -3794,7 +3794,7 @@ function _getGPOSarray(&$Lookup, $lul, $scripttag, $level = 1, $lcoverage = '',
37943794
$html .= '<div>GPOS Lookup Type 8: Format 2 not yet supported in OTL dump</div>';
37953795
continue;
37963796
/* NB When developing - cf. GSUB 6.2 */
3797-
throw new \Mpdf\MpdfException("GPOS Lookup Type " . $Type . " Format " . $PosFormat . " not TESTED YET.");
3797+
throw new \Mpdf\Exception\FontException("GPOS Lookup Type " . $Type . " Format " . $PosFormat . " not TESTED YET.");
37983798
} //===========
37993799
// Format 3:
38003800
//===========
@@ -4179,7 +4179,7 @@ function getLOCA($indexToLocFormat, $numGlyphs)
41794179
$this->glyphPos[] = ($arr[$n + 1]);
41804180
}
41814181
} else {
4182-
throw new \Mpdf\MpdfException('Unknown location table format ' . $indexToLocFormat);
4182+
throw new \Mpdf\Exception\FontException('Unknown location table format ' . $indexToLocFormat);
41834183
}
41844184
}
41854185
}

0 commit comments

Comments
 (0)