"The Future of Text Layout is Not CSS": Midjourney Engineer Open Sources Pretext

robot
Abstract generation in progress

According to 1M AI News, Cheng Lou, a Midjourney engineer and the author of the React animation library react-motion, has open-sourced Pretext, a pure JavaScript/TypeScript text measurement and layout library with zero dependencies. Pretext bypasses browser DOM measurements (such as getBoundingClientRect and offsetHeight), avoiding layout reflow by calculating text height and line breaks using pure arithmetic. Pretext operates in two phases: prepare() measures character widths in one go using the Canvas API, while layout() performs pure arithmetic calculations based on cached width data. In a benchmark test with 500 text segments, prepare() took about 19ms, while layout() only required 0.09ms. Cheng Lou noted in a supporting article, “The performance improvement is not incremental, but a qualitative change: 0.05ms compared to 30ms, zero reflows compared to five hundred.” The library supports all languages including Chinese, Japanese, Korean, and Arabic, as well as emojis and mixed-direction text, and can render to DOM, Canvas, and SVG. Typical application scenarios include: 1. Virtual lists and occlusion culling without height guessing 2. JavaScript-driven custom layouts like masonry layouts 3. Chat bubbles with multi-line text that adaptively shrink in width 4. Text wrapping around images in editorial layouts.

This page may contain third-party content, which is provided for information purposes only (not representations/warranties) and should not be considered as an endorsement of its views by Gate, nor as financial or professional advice. See Disclaimer for details.
  • Reward
  • Comment
  • Repost
  • Share
Comment
Add a comment
Add a comment
No comments
  • Pin