Canvas doesn't account for `letter-spacing`, `font-optical-sizing` and `font-feature-settings`
Hi, I’m having trouble replicating the exact DOM behavior because some of the font adjustments aren’t available out of the box. Is there a way to match the same letter spacing and optical sizing settings so that the text doesn’t get cut off when the font size increases?
Thank you and love what you have done!
I documented the current limitation in cca3f1d: CSS text features outside the canvas font shorthand, including letter-spacing, font-optical-sizing, font-feature-settings, and standalone font-variation-settings, are not modeled separately today. Leaving this open for actual support work.
I'm building a design tool where letter spacing is a core feature. I'm looking forward for this to be resolved, as I can't use this library otherwise
Opened #156 with a letterSpacing option that uses the Canvas native ctx.letterSpacing (Baseline 2025.03) for exact measurement. This covers the letter-spacing part of the issue — font-optical-sizing and font-feature-settings are not in the Canvas 2D spec, so they remain unsupported for now.
Update: numeric px letterSpacing support landed on main in 1a8b2ae and is now covered by the compact Chrome + Safari oracle snapshot. Leaving this issue open because font-optical-sizing and font-feature-settings are still not modeled separately; those remain outside the current Canvas-backed measurement surface.
@mordechaim try the new release! And ping me if you ever end up using it in the tool
Thank you, will definitely try it out
@chenglou i had a similar glitch where the letter spacing was causing estimation errors. Once my package used the letterSpacing prepare provides the estimations are accurate on mobile safari.
Here is the commit in my virtualization text layout package
https://github.com/nikilok/virtual-text-layout/commit/a74ea4652cc7e2c944d29fe4f4b2fa91ab18f888
The above package then gets used in my app this PR shows the before and after states.
https://github.com/nikilok/learn-tanstack-start/pull/64
Thank you 🙏
@chenglou I've been trying but running into several issues. I opened #169 but my main challenge is rich text that works with pre-line wrapping.
I want to preserve spaces but also layout differently styled tokens (even in a single word, such as a bold first letter). I haven't found a reliable way. My naive approach was to use cursors but it kept breaking the very last word mid-word if the word had mixed styles.
I also found letter spacing differentials between the browser and Pretext when it comes to the very last word on a line. Browsers will preserve the spacing; although it's not spec compliant, it seems that the consensus is just to keep it anyway.
This is causing a whole lot of discrepancies where breaks are not the same and with rich text having to add those spaces between intra-word tokens (such bold first letter plus the rest of word, has a missing letter spacing between the first and 2nd letter)
@chenglou What about font feature settings? I ran into this now
On the latest question: canvas has no way to set font-feature-settings, so Pretext can't measure them today. The workaround is a font file with the features you need baked in, loaded under its own family name and used in both your CSS and Pretext. The trailing letter-spacing difference at line ends was fixed a while back, and words split across styled pieces (like a bold first letter) now wrap as one word in Chrome and Safari on main. That isn't released yet. Keeping this open for the other font settings.
@chenglou FWIW, Claude Code had me fork this library and added font-feature-settings using inline styles on a hidden canvas element (not offscreen canvas, which can't be styled). It has been working perfectly the past 2 months.
https://github.com/codaworks/pretext