@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)
buildFontString
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)clampFloatingWrapMargins
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;
};clearAllCaches
Clear all measurement caches Call when fonts change, page width changes, or for testing
declare function clearAllCaches(): void;clearFontMetricsCache
Clear the font metrics cache
declare function clearFontMetricsCache(): void;clearParagraphMeasureCache
Clear the paragraph measure cache
declare function clearParagraphMeasureCache(): void;clearTextWidthCache
Clear the text width cache
declare function clearTextWidthCache(): void;findCharacterAtX
Find the character offset at a given X position within a text run
declare function findCharacterAtX(x: number, charWidths: number[]): number;getCachedFontMetrics
Get cached font metrics or return undefined
declare function getCachedFontMetrics(fontFamily: string, fontSize: number, bold?: boolean, italic?: boolean): FontMetricsEntry | undefined;getCachedParagraphMeasure
Get cached paragraph measurement or return undefined
declare function getCachedParagraphMeasure(block: ParagraphBlock, maxWidth: number): ParagraphMeasure | undefined;getCachedTextWidth
Get cached text width or return undefined
declare function getCachedTextWidth(text: string, font: string, letterSpacing?: number): number | undefined;getCanvasContext
Get or create a canvas 2D context for text measurement
declare function getCanvasContext(): CanvasRenderingContext2D;getFloatingMargins
declare function getFloatingMargins(lineY: number, lineHeight: number, zones: FloatingImageZone[] | undefined, paragraphYOffset: number): FloatingLineMargins;getFontCacheSize
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;getParagraphCacheSize
Get current paragraph measure cache size
declare function getParagraphCacheSize(): number;getRunCharWidths
Get per-character widths for a text run (for click positioning)
declare function getRunCharWidths(run: TextRun): number[];getTextCacheSize
Get current text width cache size
declare function getTextCacheSize(): number;getTotalCacheSize
Get total size of all caches
declare function getTotalCacheSize(): number;getXForCharacter
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;hashParagraphBlock
Generate a simple hash for a paragraph block Used as cache key to identify identical content
declare function hashParagraphBlock(block: ParagraphBlock): string;measureParagraph
Measure a paragraph block and compute line breaks
declare function measureParagraph(block: ParagraphBlock, maxWidth: number, options?: MeasureParagraphOptions): ParagraphMeasure;measureParagraphs
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;measureTextWidth
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;rectsToFloatingZones
declare function rectsToFloatingZones(rects: FloatingExclusionRect[], contentWidth: number): FloatingImageZone[];resetCanvasContext
Reset the canvas context (useful for testing)
declare function resetCanvasContext(): void;setCachedFontMetrics
Store font metrics in cache
declare function setCachedFontMetrics(fontFamily: string, fontSize: number, bold: boolean, italic: boolean, metrics: FontMetricsEntry): void;setCachedParagraphMeasure
Store paragraph measurement in cache
declare function setCachedParagraphMeasure(block: ParagraphBlock, maxWidth: number, measure: ParagraphMeasure): void;setCachedTextWidth
Store text width in cache
declare function setCachedTextWidth(text: string, font: string, letterSpacing: number, width: number): void;setFontCacheSize
Set the maximum size of the font metrics cache
declare function setFontCacheSize(size: number): void;setParagraphCacheSize
Set the maximum size of the paragraph measure cache
declare function setParagraphCacheSize(size: number): void;setTextCacheSize
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)
FloatingExclusionRect
interface FloatingExclusionRect| Member | Type | Summary |
|---|---|---|
| distBottom | number | |
| distLeft | number | |
| distRight | number | |
| distTop | number | |
| height | number | |
| side | 'left' | 'right' | Which side the object is on for simple one-sided wrapping. |
| width | number | |
| wrapText? | WrapTextDirection | |
| wrapType? | string | |
| x | number | X position relative to the content area. |
| y | number | Y position relative to the content area. |
FloatingImageZone
interface FloatingImageZone| Member | Type | Summary |
|---|---|---|
| bottomY | number | |
| leftMargin | number | |
| rightMargin | number | |
| segments? | FloatingLineSegmentZone[] | |
| topY | number |
FloatingLineSegmentZone
interface FloatingLineSegmentZone| Member | Type | Summary |
|---|---|---|
| availableWidth | number | |
| leftOffset | number |
FontMetrics
Typography metrics for a font
interface FontMetrics| Member | Type | Summary |
|---|---|---|
| ascent | number | |
| descent | number | |
| fontFamily | string | |
| fontSize | number | |
| lineHeight | number | |
| singleLineRatio | number | OS/2 single-line ratio for OOXML line spacing calculation |
FontStyle
Font styling properties for measurement
interface FontStyle| Member | Type | Summary |
|---|---|---|
| bold? | boolean | |
| fontFamily? | string | |
| fontSize? | number | |
| italic? | boolean | |
| letterSpacing? | number |
MeasureParagraphOptions
Options for paragraph measurement
interface MeasureParagraphOptions| Member | Type | Summary |
|---|---|---|
| floatingZones? | FloatingImageZone[] | Floating image exclusion zones that affect line widths |
| paragraphYOffset? | number | Y offset of this paragraph relative to the exclusion zones (default: 0) |
RunMeasurement
Result of measuring a run of text
interface RunMeasurement| Member | Type | Summary |
|---|---|---|
| charWidths | number[] | |
| metrics | FontMetrics | |
| width | number |
TextMeasurement
Result of measuring a text string
interface TextMeasurement| Member | Type | Summary |
|---|---|---|
| ascent | number | |
| descent | number | |
| height | number | |
| width | number |