Skip to content

Commit 6fa468e

Browse files
committed
Re-calculate font header when generating TTF
The number of tables in the font may change when generating a font file. Because the font header is dependent on the number of tables the values need to be recalculated. ref #123
1 parent 610eafe commit 6fa468e

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/FontLib/TrueType/File.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,14 @@ function encode($tags = array()) {
239239
$entries[$tag] = $this->directory[$tag];
240240
}
241241

242+
$num_tables = count($entries);
243+
$exponent = floor(log($num_tables, 2));
244+
$power_of_two = pow(2, $exponent);
245+
242246
$this->header->data["numTables"] = $num_tables;
247+
$this->header->data["searchRange"] = $power_of_two * 16;
248+
$this->header->data["entrySelector"] = log($power_of_two, 2);
249+
$this->header->data["rangeShift"] = $num_tables * 16 - $this->header->data["searchRange"];
243250
$this->header->encode();
244251

245252
$directory_offset = $this->pos();

0 commit comments

Comments
 (0)