Search code examples
macosfontsmetricsnsfont

How does NSFont font size relate to the font metrics


Figure 6.1 on this page shows the font metrics you can get from an NSFont: https://developer.apple.com/library/archive/documentation/TextFonts/Conceptual/CocoaTextArchitecture/FontHandling/FontHandling.html

If I create an Arial font with a size of 256, I get the following values:


nsfont pointSize 256
nsfont ascender 231.75
nsfont descender -54.25
nsfont leading 8.375
nsfont capHeight 183.375
nsfont xHeight 132.75

I don't understand how the font size (256) relates to the metrics. I would have expected the font size to be equal to the ascent plus the descent or maybe equal to the Cap height. Thank you for any help.


Solution

  • Let's take a look at the Arial font on macOS. Open it with FontForge (or any other tool) and look at metrics.

    • Em size 2048 (aka Em square, UPM = units per em, ...)
    • Win ascent 1854
    • Win descent 434
    • Capital height 1467
    • X height 1062
    • ...

    Simplified explanation:

    • Em size - let's say it's a container where each character will be drawn
      • You can find fonts with 1000, 1024, 2048, ... values
    • Other metrics (ascent, ...) are in units relative to em size
      • Ascent for example - 1854 / 2048 = 0.905...
    • When you ask for a 256pt font size, ascent is 0.905... * 256 = 231.75

    How does macOS get all these values you listed:

    • Ascender = (1854 / 2048) * 256 = 231.75
    • Descender = -(434 / 2048) * 256 = -54.25
    • Capital height = (1467 / 2048) * 256 = 183.375
    • ...

    I would have expected the font size to be equal to the ascent plus the descent or maybe equal to the Cap height.

    Nope. When you ask for 256pt font size you're basically saying that you'd like to scale Em size container to 256pt. And everything else depends on metrics in the font itself. This vary a lot per font.