satori icon indicating copy to clipboard operation
satori copied to clipboard

Correctly position font baseline and line-height

Open tonsky opened this issue 1 year ago • 2 comments

This patch changes logic how baseline and line-height are calculated, to match what browsers do and tools like Figma.

This is especially noticeable on font IBM Plex Sans due to how its metrics are set up. It was less noticeable on Inter, because previous calculations somehow arrived at almost correct numbers.

Implementation notes:

  • useOS2Table is removed, because it’s not what browsers/Figma seem to be using
  • yMax, yMin are not used in text positioning at all
  • lineHeight is calculated before as a fraction of fontSize, so height just recalculates it back

Before:

Screenshot 2024-03-06 at 19 12 12

After:

Screenshot 2024-03-06 at 19 12 35

Background blue/orange text is a static PNG rendered with Figma, black text is rendered with Satori.

Should solve #577

References: https://iamvdo.me/en/blog/css-font-metrics-line-height-and-vertical-align

Screenshot 2024-03-06 at 19 22 47

And https://www.figma.com/blog/line-height-changes/:

Screenshot 2024-03-06 at 19 27 16

Background image (if needed):

https://github.com/vercel/satori/assets/285292/3c8d6a75-cdca-4774-b285-7bd64bae51ee

tonsky avatar Mar 06 '24 18:03 tonsky

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
satori-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 8, 2024 0:22am

vercel[bot] avatar Mar 06 '24 18:03 vercel[bot]

Update: default line-height is not 1.2, it’s actually ascender + descender + lineGap, so I fixed that too

Demo:

Screenshot 2024-03-07 at 17 31 42

Reference:

Screenshot 2024-03-07 at 17 30 29

tonsky avatar Mar 07 '24 16:03 tonsky

Great work, thank you!

shuding avatar Jun 02 '24 19:06 shuding

@shuding Could you please assist in releasing and publishing the bugfixes on the main branch to npm? The latest version on npm is 0.10.13(5 month ago). I found that the action-run corresponding to this Pull Request has failed, and subsequent commits have skipped the Maybe Release step. It may require some manual intervention. Thank you.

Vizards avatar Jun 26 '24 10:06 Vizards