Hacker News new | ask | show | jobs
by hamza512b 21 days ago
It is done by hand, char by char. It measures each char on a “temp” canvas, and when the line is full it goes back to the last space and breaks there. CJK can break anywhere. RTL/mixed text gets reordered per line after that. Now that I been thinking about it, I could use Intl.Segmenter for languages like Thai that don’t use spaces, but it gives word boundaries, not real line break rules. But that is how it works at least. There is a wrapText function that handles this in packages/editor/src/fonts.ts.

You might be thinking, where does the performance come from? Well, the logic I described is only run when needed. I render only the blocks you see, and recompute only what changed. Initially I thought of making it like Google Docs, with a fixed page width, but I chose this approach because if the user resizes the window, for normal documents this is still fine.

1 comments

In my experience Intl.Segmenter it's very limited, does give you word boundaries on multi lang sure, but doesn't give you wrapping opportunities, and unfortunately that's language dependent. I'd love Intl to expose browser api for line breaking/word wrapping or even word splitting