New

docx-editor 1.x has shipped. Vue support, i18n, agents. Read the migration guide →

API Referencev1.0.2

@eigenpal/docx-editor-core/layout-bridge/measuring

Text Measurement Module

Provides text measurement utilities for the layout engine. Uses Canvas API for accurate, cached measurements.

Functions(39)

Build a CSS font string from styling properties

Font sizes are in points and need to be converted to pixels for canvas. 1pt = 96/72 px ≈ 1.333px at standard web DPI.

Uses the font resolver to get category-appropriate fallback stacks (serif fonts get serif fallbacks, sans-serif get sans-serif, etc.) matching the same stacks used in rendering for consistent measurements.

declare function buildFontString(style: FontStyle): string;
buildFontString( fontFamily: "Arial", fontSize: 12, bold: true ) // Returns: "bold 16px Arial, Arimo, Helvetica, sans-serif" (12pt = 16px)

When a float's wrap margins consume the entire content width (or more), there is no horizontal strip beside it for body text. Word renders the following lines at full content width instead of squeezing them into a 1-pixel column. Unchecked margins from near-full-width tables/images can exceed contentWidth and collapse every line to ~1 glyph (the "single character per line after a wide floating table" bug).

Returned margins are zeroed when: - either side alone is = contentWidth (no strip on that side at all), or - their sum is = contentWidth (no strip exists between the two sides).

declare function clampFloatingWrapMargins(leftMargin: number, rightMargin: number, contentWidth: number): {
    leftMargin: number;
    rightMargin: number;
};

Clear all measurement caches Call when fonts change, page width changes, or for testing

declare function clearAllCaches(): void;

Clear the font metrics cache

declare function clearFontMetricsCache(): void;

Clear the paragraph measure cache

declare function clearParagraphMeasureCache(): void;

Clear the text width cache

declare function clearTextWidthCache(): void;

Find the character offset at a given X position within a text run

declare function findCharacterAtX(x: number, charWidths: number[]): number;

Get cached font metrics or return undefined

declare function getCachedFontMetrics(fontFamily: string, fontSize: number, bold?: boolean, italic?: boolean): FontMetricsEntry | undefined;

Get cached paragraph measurement or return undefined

declare function getCachedParagraphMeasure(block: ParagraphBlock, maxWidth: number): ParagraphMeasure | undefined;

Get cached text width or return undefined

declare function getCachedTextWidth(text: string, font: string, letterSpacing?: number): number | undefined;

Get or create a canvas 2D context for text measurement

declare function getCanvasContext(): CanvasRenderingContext2D;
declare function getFloatingMargins(lineY: number, lineHeight: number, zones: FloatingImageZone[] | undefined, paragraphYOffset: number): FloatingLineMargins;

Get current font metrics cache size

declare function getFontCacheSize(): number;

Get typography metrics for a given font size and family

Uses Canvas TextMetrics API when available for precise metrics, falls back to ratio-based approximations.

declare function getFontMetrics(style: FontStyle): FontMetrics;

Get current paragraph measure cache size

declare function getParagraphCacheSize(): number;

Get per-character widths for a text run (for click positioning)

declare function getRunCharWidths(run: TextRun): number[];

Get current text width cache size

declare function getTextCacheSize(): number;

Get total size of all caches

declare function getTotalCacheSize(): number;

Get the X position of a character offset within a text run

declare function getXForCharacter(offset: number, charWidths: number[]): number;

Convert OOXML half-points to pixels OOXML font sizes are in half-points (24 = 12pt)

declare function halfPtToPx(halfPt: number): number;

Generate a simple hash for a paragraph block Used as cache key to identify identical content

declare function hashParagraphBlock(block: ParagraphBlock): string;

Measure a paragraph block and compute line breaks

declare function measureParagraph(block: ParagraphBlock, maxWidth: number, options?: MeasureParagraphOptions): ParagraphMeasure;

Measure multiple paragraph blocks

declare function measureParagraphs(blocks: ParagraphBlock[], maxWidth: number): ParagraphMeasure[];

Measure a run of text and return per-character widths for click positioning

declare function measureRun(text: string, style: FontStyle): RunMeasurement;

Measure text and return full metrics

declare function measureText(text: string, style: FontStyle): TextMeasurement;

Measure the width of a text string with specific styling

declare function measureTextWidth(text: string, style: FontStyle): number;

Convert points to pixels

declare function ptToPx(pt: number): number;

Convert pixels to OOXML half-points

declare function pxToHalfPt(px: number): number;

Convert pixels to points

declare function pxToPt(px: number): number;

Convert pixels to twips

declare function pxToTwips(px: number): number;
declare function rectsToFloatingZones(rects: FloatingExclusionRect[], contentWidth: number): FloatingImageZone[];

Reset the canvas context (useful for testing)

declare function resetCanvasContext(): void;

Store font metrics in cache

declare function setCachedFontMetrics(fontFamily: string, fontSize: number, bold: boolean, italic: boolean, metrics: FontMetricsEntry): void;

Store paragraph measurement in cache

declare function setCachedParagraphMeasure(block: ParagraphBlock, maxWidth: number, measure: ParagraphMeasure): void;

Store text width in cache

declare function setCachedTextWidth(text: string, font: string, letterSpacing: number, width: number): void;

Set the maximum size of the font metrics cache

declare function setFontCacheSize(size: number): void;

Set the maximum size of the paragraph measure cache

declare function setParagraphCacheSize(size: number): void;

Set the maximum size of the text width cache

declare function setTextCacheSize(size: number): void;

Convert twips to pixels

declare function twipsToPx(twips: number): number;

Interfaces(8)

interface FloatingExclusionRect
MemberTypeSummary
distBottomnumber
distLeftnumber
distRightnumber
distTopnumber
heightnumber
side'left' | 'right'Which side the object is on for simple one-sided wrapping.
widthnumber
wrapText?WrapTextDirection
wrapType?string
xnumberX position relative to the content area.
ynumberY position relative to the content area.
interface FloatingImageZone
MemberTypeSummary
bottomYnumber
leftMarginnumber
rightMarginnumber
segments?FloatingLineSegmentZone[]
topYnumber
interface FloatingLineSegmentZone
MemberTypeSummary
availableWidthnumber
leftOffsetnumber

Typography metrics for a font

interface FontMetrics
MemberTypeSummary
ascentnumber
descentnumber
fontFamilystring
fontSizenumber
lineHeightnumber
singleLineRationumberOS/2 single-line ratio for OOXML line spacing calculation

Font styling properties for measurement

interface FontStyle
MemberTypeSummary
bold?boolean
fontFamily?string
fontSize?number
italic?boolean
letterSpacing?number

Options for paragraph measurement

interface MeasureParagraphOptions
MemberTypeSummary
floatingZones?FloatingImageZone[]Floating image exclusion zones that affect line widths
paragraphYOffset?numberY offset of this paragraph relative to the exclusion zones (default: 0)

Result of measuring a run of text

interface RunMeasurement
MemberTypeSummary
charWidthsnumber[]
metricsFontMetrics
widthnumber

Result of measuring a text string

interface TextMeasurement
MemberTypeSummary
ascentnumber
descentnumber
heightnumber
widthnumber