Skip to content

Font Size in ASS

Zhenye Wei edited this page Aug 25, 2024 · 5 revisions

Fontsize or \fs in ASS is not same as font-size in CSS, see:

https://github.com/libass/libass/issues/644#issuecomment-1228412302

https://github.com/libass/libass/issues/668#issuecomment-1364554011

There are some line metrics in OpenType, using Font Inspector we can get metrics of Roboto:

  • head
    • unitsPerEm: 2048
  • hhea
    • Ascender: 1900
    • Descender: -500
    • LineGap: 0
  • OS/2
    • sTypoAscender: 1536
    • sTypoDescender: -512
    • sTypoLineGap: 102
    • usWinAscent: 1946
    • usWinDescent: 512

If we set font-family: Roboto; font-size: 2048px; line-height: normal;, we'll get el.clientHeight === 2400 in macOS and el.clientHeight === 2458 in Windows.

It seems line-height: normal; means hhea.Ascender + -hhea.Descender + hhea.LineGap in macOS and os2.usWinAscent + os2.usWinDescent in Windows, which can be verified by adjusting ascent-override, descent-override, line-gap-override to 0%. More info here.

ASS uses os2.usWinAscent + os2.usWinDescent for \fs, so we can get the formula fontSizeInCSS = fontSizeInASS / (usWinAscent + usWinDescent) * unitsPerEm. We can't get the accurate values of usWin*, but we can get the ratio of unitsPerEm / (usWinAscent + usWinDescent) by set font-size: 2048px; line-height: normal; and get the clientHeight, and finally get the formula fontSizeInCSS = fontSizeInASS / clientHeightWhenFontSizeEquals2048 * 2048.

However it seems we have no way to get usWin* or unitsPerEm in macOS, text-box-edge uses sTypo*, it doesn't help.

For most fonts, maybe, usWinAscent + usWinDescent approximately equals hhea.Ascender + -hhea.Descender + hhea.LineGap, so the result is an approximation.


Update:

I found there are fontBoundingBoxAscent and fontBoundingBoxDescent in TextMetrics, which can simplify the calculation, but it's still not usWin* in macOS.

Clone this wiki locally