From cdcfde297c626c87753f30d72d5bd97f6dabcfd0 Mon Sep 17 00:00:00 2001 From: Brian Sweeney Date: Tue, 12 Dec 2023 19:54:56 -0500 Subject: [PATCH] Use correct reference data for AFM values CapHeight, XHeight, Ascender, and Descender values should be based off of character data. fixes #7 --- src/FontLib/AdobeFontMetrics.php | 43 +++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/FontLib/AdobeFontMetrics.php b/src/FontLib/AdobeFontMetrics.php index e75385f..424f827 100644 --- a/src/FontLib/AdobeFontMetrics.php +++ b/src/FontLib/AdobeFontMetrics.php @@ -75,12 +75,49 @@ function write($file, $encoding = null) { if (isset($hhea["ascent"])) { $this->addPair("FontHeightOffset", $font->normalizeFUnit($hhea["lineGap"])); - $this->addPair("Ascender", $font->normalizeFUnit($hhea["ascent"])); - $this->addPair("Descender", $font->normalizeFUnit($hhea["descent"])); } else { $this->addPair("FontHeightOffset", $font->normalizeFUnit($os2["typoLineGap"])); + } + + $glyf = $font->getData("glyf"); + $glyphIndexArray = $font->getUnicodeCharMap(); + + // capHeight is based on capital H + if (\array_key_exists(72, $glyphIndexArray)) { + $upperH = $glyf[$glyphIndexArray[72]]; + $upperH->parseData(); + $this->addPair("CapHeight", $font->normalizeFUnit($upperH->yMax)); + } + + // xHeight is based on lowercase x + if (\array_key_exists(120, $glyphIndexArray)) { + $lowerX = $glyf[$glyphIndexArray[120]]; + $lowerX->parseData(); + $this->addPair("XHeight", $font->normalizeFUnit($lowerX->yMax)); + } + + // ascender is based on lowercase d + if (\array_key_exists(100, $glyphIndexArray)) { + $lowerD = $glyf[$glyphIndexArray[100]]; + $lowerD->parseData(); + $this->addPair("Ascender", $font->normalizeFUnit($lowerD->yMax)); + } elseif (isset($hhea["ascent"])) { + $this->addPair("Ascender", $font->normalizeFUnit($hhea["ascent"])); + } + else { $this->addPair("Ascender", $font->normalizeFUnit($os2["typoAscender"])); + } + + // descender is based on lowercase p + if (\array_key_exists(112, $glyphIndexArray)) { + $lowerP = $glyf[$glyphIndexArray[112]]; + $lowerP->parseData(); + $this->addPair("Descender", $font->normalizeFUnit($lowerP->yMin)); + } elseif (isset($hhea["ascent"])) { + $this->addPair("Descender", $font->normalizeFUnit($hhea["descent"])); + } + else { $this->addPair("Descender", -abs($font->normalizeFUnit($os2["typoDescender"]))); } @@ -92,8 +129,6 @@ function write($file, $encoding = null) { $font->normalizeFUnit($head["yMax"]), )); - $glyphIndexArray = $font->getUnicodeCharMap(); - if ($glyphIndexArray) { $hmtx = $font->getData("hmtx"); $names = $font->getData("post", "names");