Skip to content

Commit

Permalink
Use correct reference data for AFM values
Browse files Browse the repository at this point in the history
CapHeight, XHeight, Ascender, and Descender values should be based off of character data.

fixes #7
  • Loading branch information
bsweeney committed Dec 30, 2023
1 parent 05446c4 commit cdcfde2
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions src/FontLib/AdobeFontMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"])));
}

Expand All @@ -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");
Expand Down

0 comments on commit cdcfde2

Please sign in to comment.