Skip to content

Commit

Permalink
Re-calculate font header when generating TTF
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bsweeney committed Dec 12, 2023
1 parent 610eafe commit 6fa468e
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/FontLib/TrueType/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,14 @@ function encode($tags = array()) {
$entries[$tag] = $this->directory[$tag];
}

$num_tables = count($entries);
$exponent = floor(log($num_tables, 2));
$power_of_two = pow(2, $exponent);

$this->header->data["numTables"] = $num_tables;
$this->header->data["searchRange"] = $power_of_two * 16;
$this->header->data["entrySelector"] = log($power_of_two, 2);
$this->header->data["rangeShift"] = $num_tables * 16 - $this->header->data["searchRange"];
$this->header->encode();

$directory_offset = $this->pos();
Expand Down

0 comments on commit 6fa468e

Please sign in to comment.