npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@abmaelsouza/pretext-styled-renderer

v1.0.2-build.2437134747

Published

Headless text layout engine for TypeScript and JavaScript. Supports text measurement, line breaking, hyphenation, and rich text rendering without relying on the DOM.

Readme

@abmaelsouza/pretext-styled-renderer

A high-performance, framework-agnostic rich-text rendering engine for TypeScript. It performs layout calculations entirely outside the DOM, making it compatible with any environment that provides a DOM (React, Vue, Svelte, Angular, or Vanilla JS). It provides pixel-perfect text wrapping, rich styling support, and a comprehensive Query API for inspecting layout metrics without relying on getBoundingClientRect.

Features

  • 🚀 Zero-DOM Layout: Calculates line breaks and fragment positions using a canvas-like measurement engine.
  • 🎨 Rich Styling per Span: Apply unique fonts, weights, colors, and backgrounds to any text fragment.
  • 📏 Programmatic Metrics: Query bounding boxes for the total block, individual lines, or text fragments.
  • ⌨️ Hit-Testing: Determine which line or fragment exists at a specific coordinate.
  • 🔠 Smart Hyphenation: Built-in syllable-aware hyphenation logic.
  • Performance: Highly optimized for dynamic layouts and frequent resizing.

Installation

npm install @abmaelsouza/pretext-styled-renderer

Quick Start

1. Initialize the Engine

import { PretextStyledEngine, type StyledSpan } from '@abmaelsouza/pretext-styled-renderer';

const engine = new PretextStyledEngine({
  outerPaddingPx: 20,
  defaultStyle: {
    fontSize: 18,
    fontFamily: 'system-ui, sans-serif',
    lineHeight: 1.4,
    color: '#1e293b'
  }
});

2. Define Styled Spans

const spans: StyledSpan[] = [
  { text: 'Hello ', style: { fontWeight: 'bold', color: 'blue' } },
  { text: 'World!', style: { fontStyle: 'italic', backgroundColor: '#f0f0f0' } }
];

3. Calculate Layout & Sync to DOM

// Perform the layout calculation (no DOM used here)
engine.layout(spans, 400); // 400px width

// Sync the results to a container element
const container = document.getElementById('text-container');
engine.syncDom(container);

Responsive Layout (Handling Resizes)

The engine calculates layout based on a fixed width. To make the text responsive, you must recalibrate the layout and call syncDom whenever the container size changes.

Using ResizeObserver (Recommended)

This is the most efficient way to detect when the actual container element changes size.

const container = document.getElementById('text-container');

const observer = new ResizeObserver(entries => {
  for (let entry of entries) {
    const newWidth = entry.contentRect.width;
    
    // Recalculate and update the DOM
    engine.layout(spans, newWidth);
    engine.syncDom(container);
  }
});

observer.observe(container);

Using Window Resize

For simpler layouts, you can listen to the window's resize event.

window.addEventListener('resize', () => {
  const newWidth = container.clientWidth;
  engine.layout(spans, newWidth);
  engine.syncDom(container);
});

The Query API (Layout Metrics)

The engine provides a set of methods to inspect the rendered content programmatically. This is useful for drawing overlays, implementing custom selections, or hit-testing.

| Method | Description | | :--- | :--- | | getTotalRect() | Returns the {x, y, width, height} of the entire text block. | | getLineRect(row) | Returns the bounding box of a specific line index. | | getFragmentRects(row) | Returns an array of bounding boxes for every styled fragment in a line. | | getLineAtHeight(y) | Returns the index of the line located at a specific vertical offset. | | getLineWidths() | Returns an array of widths for every rendered line. | | getLineText(row) | Returns the raw string content of a specific line. | | getFullText() | Returns the entire concatenated text content. |


Styling Options (TextStyle)

Every StyledSpan can be customized with the following properties:

  • fontSize: Number (px) or String (e.g., '18px')
  • fontFamily: Font family stack (e.g., 'Inter, sans-serif')
  • fontWeight: 'normal', 'bold', or numeric values like 600.
  • fontStyle: 'normal', 'italic', or 'oblique'.
  • lineHeight: Multiplier (e.g., 1.5) or pixel value.
  • color: CSS color string.
  • backgroundColor: CSS background-color string.
  • letterSpacing: CSS-like letter-spacing (e.g., '0.5px').
  • textAlign: 'left', 'center', 'right'.
  • textDecoration: 'none', 'underline', 'line-through'.
  • opacity: Number between 0 and 1.

License

MIT