I'm trying to parse truetype font for building and store the ascender, descender for each fontsize, I'm using http://nodebox.github.io/opentype.js/ that did an amazing work, but I don't understand how to compute the returned values.
Example font Blackoak (adobe)
I've got [Ascender, Descender] = 1900 and -5OO
I presume that these numbers indicates the distance from the baseline in em space, but did I need other meta information for computing these values?
If you want to compute ascender/descender in pixels for specific font size, then you need to lookup OS/2
table for sTypoAscender
and sTypoDescender
values (as instructed by W3C). The ascender
and descender
values of hhea
table are marking only font designer's intention which not necessarily corresponds to real computed values. Concerning value type, you're right, ascender and descender are in UPM units and negative means below the baseline.
In order to convert these numbers to pixels for specific font size you also need to extract how many units per em are in your font. You can do that by reading unitsPerEm
value from the head
table. Usually, for OTF its 1000 UPM, for TTF - 1024 or 2048 UPM. Absolute values of sTypoAscender
and sTypoDescender
should add up to unitsPerEm
value. And then its just a matter of ratio.
For example, lets take a look at the opentype.js Font Inspector and assume 16px font size. Value unitsPerEm
is 2048 UPM, sTypoAscender
and sTypoDescender
are 1536 and -512 correspondingly (1536 + 512 = 2048):
Ascender = 16 * 1536/2048 = 12px (above baseline)
Descender = 16 * -512/2048 = -4px (below baseline)