frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Pretext: TypeScript library for multiline text measurement and layout

https://github.com/chenglou/pretext
97•emersonmacro•1d ago
https://x.com/_chenglou/status/2037713766205608234, https://xcancel.com/_chenglou/status/2037713766205608234

Demos: https://chenglou.me/pretext/, https://somnai-dreams.github.io/pretext-demos/

https://kevinho.com/experiments/biomap/

Comments

rattray•1h ago
Regardless of the subject matter, the tweets announcing this are a masterclass in demoing why an architectural/platform improvement can be impactful.
rattray•1h ago
Some details on how it works from a code comment:

Problem: DOM-based text measurement (getBoundingClientRect, offsetHeight) forces synchronous layout reflow. When components independently measure text, each measurement triggers a reflow of the entire document. This creates read/write interleaving that can cost 30ms+ per frame for 500 text blocks.

Solution: two-phase measurement centered around canvas measureText.

prepare(text, font) — segments text via Intl.Segmenter, measures each word via canvas, caches widths, and does one cached DOM calibration read per font when emoji correction is needed. Call once when text first appears.

layout(prepared, maxWidth, lineHeight) — walks cached word widths with pure arithmetic to count lines and compute height. Call on every resize. ~0.0002ms per text.

https://github.com/chenglou/pretext/blob/main/src/layout.ts

mgaunard•1h ago
What about hyphenization?
simonw•33m ago
That's in there too: https://github.com/chenglou/pretext/blob/53a065b8a345fc29831...
gastonmorixe•56m ago
Has someone ever found a good solution for long / infinite lists / grids virtualization not breaking browsers native text search?

Maybe for this we need a new web "Search" API instead of JS. Not sure it can be done otherwise without browser's help.

dalmo3•42m ago
This is awesome! I had this problem when building a datagrid where cells would dynamically render textarea. IIRC I ended up doing a simple canvas measurement, but I had all the text and font properties static, and even then it was hellish to get it right.
rpastuszak•38m ago
Love this. I especially liked shape based reflow example.

This is something I've been thinking for ages and would love to add to Ensō (enso.sonnet.io), purely because it would allow me to apply better caret transitions between the lines of text.

(I'm not gonna do that because I'm trying to keep it simple, but it's a strong temptation)

Now a CSS tangent: regarding the accordion example from the site (https://chenglou.me/pretext/accordion), this can be solved with pure CSS (and then perhaps a JS fallback) using the `interpolate-size` property.

https://www.joshwcomeau.com/snippets/html/interpolate-size/

Regarding the text bubbles problem (https://chenglou.me/pretext/bubbles), you can use `text-wrap: balance | pretty` to achieve the same result.

(`balance` IIRC evens out the # of lines)

lewisjoe•35m ago
Quick overview of pretext: if you want to layout text on the web, you have to use canvas.measureText API and implement line-breaking / segmentation / RTL yourself.

Pretext makes this easier. Just pass the text and text properties (font, color, size, etc) into a pure JS API and it layouts the content into given viewport dimension.

Earlier you'll have to either use measureText or ship harbuzz to browser somehow. I guess pretext is not a technical breakthrough, just the right things assembled to make layouting as a pure JS API.

I have one question though: how is this different from Skia-wasm / Canvaskit? Skia already has sophisticated API to layout multiline text and it also is a pure algorithmic API.

lewisjoe•33m ago
If the author is right, this is going to be huge for GUI web frameworks and for future rich text editors.
madeofpalk•29m ago
> how is this different from Skia-wasm

It’s not wasm?

simonw•27m ago
This thing is very impressive.

The problem it solves is efficiently calculating the width/height of some text on a web page, without actually rendering that text to the page first (very expensive).

It does that by pre-calculating the width/height of individual segments - think words - and caching those. Then it implements the full algorithm for how browsers construct text strings by line-wrapping those segments using custom code.

This is absurdly hard because of the many different types of wrapping and characters (emoji, Chinese, etc) that need to be taken into account - plus the fact that different browsers (in particular Safari) have slight differences in their rendering algorithms.

It tests the resulting library against real browsers using a wide variety of long text documents, see https://github.com/chenglou/pretext/tree/main/corpora and https://github.com/chenglou/pretext/blob/main/pages/accuracy...

jimkleiber•22m ago
I had struggled so much to measure text and number of lines when creating dynamic subtitles for remotion videos, not sure if it was my incompetence or a complexity with the DOM itself. I feel hopeful this will make it much easier :-)
rikroots•8m ago
> This thing is very impressive.

Agreed! Text layout engines are stupidly hard. You start out thinking "It's a hard task, but I can do it" and then 3 months later you find yourself in a corner screaming "Why, Chinese? Why do you need to rotate your punctuation differently when you render in columns??"

This effort feeds back to the DOM, making it far more useful than my efforts which are confined to rendering multiline text on a canvas - for example: https://scrawl-v8.rikweb.org.uk/demo/canvas-206.html

Trufa•8m ago
I said it elsewhere but will repeat it here:

This is incredibly impressive, many of this things have been missing for forever! I remember the first time I couldn't figure out how do a proper responsive accordion, it was with bootstrap 1, released in 2011 !! Today it's still not properly solved (until now?).

Many of thing things belong in css no in js, but this has been the pattern with so many things in the web

1) web needs evolve into more complex needs 2) hacky js/css implementation and workarounds 3) gets implemented as css standard

This is a not so hacky step 2. Really impressive,

I would have thunk that if this was actually possible someone would have done it already, apparently not, at some point I really want to understand what's the real insight in the library, their https://github.com/chenglou/pretext/blob/main/RESEARCH.md is interesting, they seem to have just done the hard work, of browser discrepancies to the last detail of what does an emoji measure in each browser, hope this is not a maintenance nightmare.

All in all this will push the web forward no doubt.

Voyager 1 runs on 69 KB of memory and an 8-track tape recorder

https://techfixated.com/a-1977-time-capsule-voyager-1-runs-on-69-kb-of-memory-and-an-8-track-tape...
209•speckx•3h ago•90 comments

C++26 is done ISO C++ standards meeting, Trip Report

https://herbsutter.com/2026/03/29/c26-is-done-trip-report-march-2026-iso-c-standards-meeting-lond...
68•pjmlp•1h ago•25 comments

Typing and Keyboards

https://lzon.ca/posts/series/grateful/typing-and-keyboards/
20•jpmitchell•1h ago•21 comments

Pretext: TypeScript library for multiline text measurement and layout

https://github.com/chenglou/pretext
98•emersonmacro•1d ago•14 comments

The RISE RISC-V Runners: free, native RISC-V CI on GitHub

https://riseproject.dev/2026/03/24/announcing-the-rise-risc-v-runners-free-native-risc-v-ci-on-gi...
73•thebeardisred•3d ago•18 comments

Neovim 0.12.0

https://github.com/neovim/neovim/releases/tag/v0.12.0
120•pawelgrzybek•2h ago•57 comments

The rise and fall of IBM's 4 Pi aerospace computers: an illustrated history

https://www.righto.com/2026/03/ibm-4-pi-computer-history.html
38•zdw•3h ago•7 comments

AyaFlow: A high-performance, eBPF-based network traffic analyzer written in Rust

https://github.com/DavidHavoc/ayaFlow
50•tanelpoder•4h ago•3 comments

Show HN: QuickBEAM – run JavaScript as supervised Erlang/OTP processes

https://github.com/elixir-volt/quickbeam
37•dannote•22h ago•4 comments

The Epistemology of Microphysics

https://www.edwardfeser.com/unpublishedpapers/microphysics.html
17•danielam•4d ago•9 comments

Nitrile and latex gloves may cause overestimation of microplastics

https://news.umich.edu/nitrile-and-latex-gloves-may-cause-overestimation-of-microplastics-u-m-stu...
446•giuliomagnifico•9h ago•189 comments

Police used AI facial recognition to wrongly arrest TN woman for crimes in ND

https://www.cnn.com/2026/03/29/us/angela-lipps-ai-facial-recognition
226•ourmandave•5h ago•88 comments

LinkedIn uses 2.4 GB RAM across two tabs

432•hrncode•10h ago•275 comments

A nearly perfect USB cable tester

https://blog.literarily-starved.com/2026/02/technology-the-nearly-perfect-usb-cable-tester-does-e...
233•birdculture•3d ago•116 comments

Miasma: A tool to trap AI web scrapers in an endless poison pit

https://github.com/austin-weeks/miasma
234•LucidLynx•9h ago•182 comments

Full network of clitoral nerves mapped out for first time

https://www.theguardian.com/society/2026/mar/29/full-network-clitoral-nerves-mapped-out-first-tim...
112•onei•3h ago•37 comments

Observations from carbon dioxide monitoring

https://grieve-smith.com/ftn/2026/03/nine-observations-from-carbon-dioxide-monitoring/
13•coloneltcb•2d ago•2 comments

First Western Digital, now Sony: The tech giant suspends SD card sales

https://mashable.com/article/sony-sd-card-sales-suspended-memory-shortage
45•_tk_•2h ago•33 comments

I turned my Kindle into my own personal newspaper

https://manualdousuario.net/en/how-to-kindle-personal-newspaper/
152•rpgbr•2d ago•51 comments

Show HN: Create a full language server in Go with 3.17 spec support

https://github.com/owenrumney/go-lsp
73•rumno0•4d ago•14 comments

Show HN: BreezePDF – Free, in-browser PDF editor

https://breezepdf.com/?v=3
37•philjohnson•5h ago•22 comments

Show HN: Sheet Ninja – Google Sheets as a CRUD Back End for Vibe Coders

https://sheetninja.io
57•sxa001•7h ago•65 comments

Netscape News Feed Straight Out of the Late 00s

https://isp.netscape.com/
30•mistyvales•2h ago•7 comments

The Failure of the Thermodynamics of Computation (2010)

https://sites.pitt.edu/~jdnorton/Goodies/Idealization/index.html
42•nill0•2d ago•8 comments

The bot situation on the internet is worse than you could imagine

https://gladeart.com/blog/the-bot-situation-on-the-internet-is-actually-worse-than-you-could-imag...
159•ohjeez•3h ago•105 comments

When do we become adults, really?

https://www.newyorker.com/culture/annals-of-inquiry/when-do-we-become-adults-really
54•benbreen•3d ago•75 comments

Alzheimer's disease mortality among taxi and ambulance drivers (2024)

https://www.bmj.com/content/387/bmj-2024-082194
201•bookofjoe•18h ago•131 comments

The loneliness of A Room of One’s Own

https://newrepublic.com/article/206731/loneliness-room-one-virginia-woolf-hold-up
36•prismatic•3d ago•6 comments

Cuts in publishing and book reviewing imperil the future of narrative nonfiction

https://newrepublic.com/article/207659/non-fiction-publishing-threat-important-ever
44•Hooke•3d ago•31 comments

Founder of GitLab battles cancer by founding companies

https://sytse.com/cancer/
1309•bob_theslob646•1d ago•249 comments