From eceea732b6a15598c12a1d8a22733ed72cb00a20 Mon Sep 17 00:00:00 2001 From: Brian Sweeney Date: Fri, 20 Oct 2023 11:18:17 -0400 Subject: [PATCH] Fix glyph sizes to a multiple of 4 bytes Before this change when the font loca table was using the short format (glyph size / 2), an odd-numbered glyph byte length would be incorrectly record. To prevent this issue, when writing a font we will padd glyphs to align with a 4-byte boundary. Note: Official guidance needed. The 4-byte boundary is based on table requirements, as well as being a multiple of 2. --- src/FontLib/Table/Type/glyf.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/FontLib/Table/Type/glyf.php b/src/FontLib/Table/Type/glyf.php index b8f7c93..ad4a858 100644 --- a/src/FontLib/Table/Type/glyf.php +++ b/src/FontLib/Table/Type/glyf.php @@ -143,7 +143,16 @@ protected function _encode() { $length = 0; foreach ($subset as $gid) { $loca[] = $length; - $length += $data[$gid]->encode(); + + $bytes = $data[$gid]->encode(); + + $pad = 0; + $mod = $bytes % 4; + if ($mod != 0) { + $pad = 4 - $mod; + $font->write(str_pad("", $pad, "\0"), $pad); + } + $length += $bytes + $pad; } $loca[] = $length; // dummy loca