pretext icon indicating copy to clipboard operation
pretext copied to clipboard

Canvas doesn't account for `letter-spacing`, `font-optical-sizing` and `font-feature-settings`

Open vincecao opened this issue 8 months ago • 12 comments

Image

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!

vincecao avatar Apr 05 '26 16:04 vincecao

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.

chenglou avatar Apr 18 '26 05:04 chenglou

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

mordechaim avatar Apr 19 '26 16:04 mordechaim

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.

mayrang avatar Apr 21 '26 18:04 mayrang

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.

chenglou avatar Apr 22 '26 05:04 chenglou

@mordechaim try the new release! And ping me if you ever end up using it in the tool

chenglou avatar Apr 22 '26 16:04 chenglou

Thank you, will definitely try it out

mordechaim avatar Apr 23 '26 04:04 mordechaim

@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 🙏

nikilok avatar Apr 24 '26 21:04 nikilok

@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.

mordechaim avatar May 05 '26 20:05 mordechaim

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)

mordechaim avatar May 06 '26 16:05 mordechaim

@chenglou What about font feature settings? I ran into this now

mordechaim avatar Jul 29 '26 23:07 mordechaim

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 avatar Sep 13 '26 00:09 chenglou

@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

mordechaim avatar Sep 14 '26 01:09 mordechaim