Skip to content

💡 Expose display-width helpers (stringWidth / charWidth) #72

@natemoo-re

Description

@natemoo-re

Is your feature request related to a problem?

The C layer already measures the display width of a character and of a whole string—it's how text gets laid out—but that isn't surfaced to consumers. There are real use cases for measuring text JS-side (truncating to a column budget, padding or aligning labels) with no way to do it today short of a throwaway render.

Describe the solution you'd like

Expose two pure helpers (exact names TBD), backed by the internal wcwidth table:

charWidth(0x4f60); // 2  (CJK)
stringWidth("你好"); // 4

charWidth returns 0/1/2; stringWidth sums codepoints with proper handling for ANSI escape sequences.

Describe alternatives you've considered

Add a JS-side width dependency (duplicates the table we already ship and can drift to a different Unicode version than layout uses), or render into a fit element and read back info.get(id).bounds.width (slow, and gets VS16 emoji clusters wrong).

Additional context

Failing test case on nm/repro/string-width (test · diff). The table is already in C (src/wcwidth.h:8) and exported from the wasm (Makefile:12, --export-all); this is mostly surfacing it from mod.ts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions