From db0bb448e4e65ed1b3c67dce86d78c1e2abf1bf7 Mon Sep 17 00:00:00 2001 From: Brian Sweeney Date: Sun, 15 Oct 2023 20:59:48 -0400 Subject: [PATCH] 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 --- src/FontLib/TrueType/File.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/FontLib/TrueType/File.php b/src/FontLib/TrueType/File.php index 446eac8..e25442e 100644 --- a/src/FontLib/TrueType/File.php +++ b/src/FontLib/TrueType/File.php @@ -239,8 +239,13 @@ function encode($tags = array()) { } $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();