Skip to content

Commit

Permalink
Fix composite glyph output (#581)
Browse files Browse the repository at this point in the history
* Fix composite glyph output

* Pad zeros before checksum calulation

---------

Co-authored-by: Nicola Asuni <nicolaasuni@users.noreply.github.com>
  • Loading branch information
ren1244 and nicolaasuni committed Sep 6, 2023
1 parent de332db commit 7e3fb6f
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions include/tcpdf_fonts.php
Expand Up @@ -1323,43 +1323,43 @@ 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'];
$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]);
}
}
// 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'];
$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);
$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']);
$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
Expand All @@ -1383,7 +1383,7 @@ public static function _getTrueTypeFontSubset($font, $subsetchars) {
}
// 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;
}

Expand Down

0 comments on commit 7e3fb6f

Please sign in to comment.