From 59e16d174d5400b2985c45ecfcb31b5d1efafb51 Mon Sep 17 00:00:00 2001 From: ren1244 Date: Mon, 16 Jan 2023 00:21:29 +0800 Subject: [PATCH 1/2] Fix composite glyph output --- include/tcpdf_fonts.php | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/include/tcpdf_fonts.php b/include/tcpdf_fonts.php index 30053d3e..ee32d3f7 100644 --- a/include/tcpdf_fonts.php +++ b/include/tcpdf_fonts.php @@ -1323,14 +1323,9 @@ public static function _getTrueTypeFontSubset($font, $subsetchars) { // set the checkSumAdjustment to 0 $table[$tag]['data'] = substr($table[$tag]['data'], 0, 8)."\x0\x0\x0\x0".substr($table[$tag]['data'], 12); } - $pad = 4 - ($table[$tag]['length'] % 4); - if ($pad != 4) { - // the length of a table must be a multiple of four bytes - $table[$tag]['length'] += $pad; - $table[$tag]['data'] .= str_repeat("\x0", $pad); - } $table[$tag]['offset'] = $offset; $offset += $table[$tag]['length']; + $offset = $offset + 3 & ~3; // check sum is not changed (so keep the following line commented) //$table[$tag]['checkSum'] = self::_getTTFtableChecksum($table[$tag]['data'], $table[$tag]['length']); } else { @@ -1338,26 +1333,17 @@ public static function _getTrueTypeFontSubset($font, $subsetchars) { } } // add loca + $table['loca'] = array(); $table['loca']['data'] = $loca; $table['loca']['length'] = strlen($loca); - $pad = 4 - ($table['loca']['length'] % 4); - if ($pad != 4) { - // the length of a table must be a multiple of four bytes - $table['loca']['length'] += $pad; - $table['loca']['data'] .= str_repeat("\x0", $pad); - } $table['loca']['offset'] = $offset; $table['loca']['checkSum'] = self::_getTTFtableChecksum($table['loca']['data'], $table['loca']['length']); $offset += $table['loca']['length']; + $offset = $offset + 3 & ~3; // add glyf + $table['glyf'] = array(); $table['glyf']['data'] = $glyf; $table['glyf']['length'] = strlen($glyf); - $pad = 4 - ($table['glyf']['length'] % 4); - if ($pad != 4) { - // the length of a table must be a multiple of four bytes - $table['glyf']['length'] += $pad; - $table['glyf']['data'] .= str_repeat("\x0", $pad); - } $table['glyf']['offset'] = $offset; $table['glyf']['checkSum'] = self::_getTTFtableChecksum($table['glyf']['data'], $table['glyf']['length']); // rebuild font @@ -1379,11 +1365,12 @@ public static function _getTrueTypeFontSubset($font, $subsetchars) { $font .= pack('N', $data['length']); // length } foreach ($table as $data) { - $font .= $data['data']; + $pad = (4 - $data['length'] % 4) % 4; + $font .= $data['data'].str_repeat("\x0", $pad); } // set checkSumAdjustment on head table $checkSumAdjustment = 0xB1B0AFBA - self::_getTTFtableChecksum($font, strlen($font)); - $font = substr($font, 0, $table['head']['offset'] + 8).pack('N', $checkSumAdjustment).substr($font, $table['head']['offset'] + 12); + $font = substr($font, 0, $table['head']['offset'] + $offset + 8).pack('N', $checkSumAdjustment).substr($font, $table['head']['offset'] + $offset + 12); return $font; } From bff5f0ff85138dfbeb6aacfcad1e27b4ce676282 Mon Sep 17 00:00:00 2001 From: ren1244 Date: Sat, 21 Jan 2023 10:33:18 +0800 Subject: [PATCH 2/2] Pad zeros before checksum calulation --- include/tcpdf_fonts.php | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/include/tcpdf_fonts.php b/include/tcpdf_fonts.php index ee32d3f7..305bfc64 100644 --- a/include/tcpdf_fonts.php +++ b/include/tcpdf_fonts.php @@ -1325,9 +1325,13 @@ public static function _getTrueTypeFontSubset($font, $subsetchars) { } $table[$tag]['offset'] = $offset; $offset += $table[$tag]['length']; - $offset = $offset + 3 & ~3; + $numPad = ($offset + 3 & ~3) - $offset; + if($numPad > 0) { + $table[$tag]['data'] .= str_repeat("\x0", $numPad); + $offset += $numPad; + } // check sum is not changed (so keep the following line commented) - //$table[$tag]['checkSum'] = self::_getTTFtableChecksum($table[$tag]['data'], $table[$tag]['length']); + //$table[$tag]['checkSum'] = self::_getTTFtableChecksum($table[$tag]['data'], $table[$tag]['length'] + $numPad); } else { unset($table[$tag]); } @@ -1337,15 +1341,25 @@ public static function _getTrueTypeFontSubset($font, $subsetchars) { $table['loca']['data'] = $loca; $table['loca']['length'] = strlen($loca); $table['loca']['offset'] = $offset; - $table['loca']['checkSum'] = self::_getTTFtableChecksum($table['loca']['data'], $table['loca']['length']); $offset += $table['loca']['length']; - $offset = $offset + 3 & ~3; + $numPad = ($offset + 3 & ~3) - $offset; + if($numPad > 0) { + $table['loca']['data'] .= str_repeat("\x0", $numPad); + $offset += $numPad; + } + $table['loca']['checkSum'] = self::_getTTFtableChecksum($table['loca']['data'], $table['loca']['length'] + $numPad); // add glyf $table['glyf'] = array(); $table['glyf']['data'] = $glyf; $table['glyf']['length'] = strlen($glyf); $table['glyf']['offset'] = $offset; - $table['glyf']['checkSum'] = self::_getTTFtableChecksum($table['glyf']['data'], $table['glyf']['length']); + $offset += $table['glyf']['length']; + $numPad = ($offset + 3 & ~3) - $offset; + if($numPad > 0) { + $table['glyf']['data'] .= str_repeat("\x0", $numPad); + $offset += $numPad; + } + $table['glyf']['checkSum'] = self::_getTTFtableChecksum($table['glyf']['data'], $table['glyf']['length'] + $numPad); // rebuild font $font = ''; $font .= pack('N', 0x10000); // sfnt version @@ -1365,8 +1379,7 @@ public static function _getTrueTypeFontSubset($font, $subsetchars) { $font .= pack('N', $data['length']); // length } foreach ($table as $data) { - $pad = (4 - $data['length'] % 4) % 4; - $font .= $data['data'].str_repeat("\x0", $pad); + $font .= $data['data']; } // set checkSumAdjustment on head table $checkSumAdjustment = 0xB1B0AFBA - self::_getTTFtableChecksum($font, strlen($font));