Skip to main content

TextMeasurer

Trait TextMeasurer 

Source
pub trait TextMeasurer {
    // Required method
    fn measure(&self, text: &str, style: &TextStyle) -> TextMetrics;

    // Provided methods
    fn measure_svg_text_computed_length_px(
        &self,
        text: &str,
        style: &TextStyle,
    ) -> f64 { ... }
    fn measure_svg_text_bbox_x(
        &self,
        text: &str,
        style: &TextStyle,
    ) -> (f64, f64) { ... }
    fn measure_svg_text_bbox_x_with_ascii_overhang(
        &self,
        text: &str,
        style: &TextStyle,
    ) -> (f64, f64) { ... }
    fn measure_svg_title_bbox_x(
        &self,
        text: &str,
        style: &TextStyle,
    ) -> (f64, f64) { ... }
    fn measure_svg_simple_text_bbox_width_px(
        &self,
        text: &str,
        style: &TextStyle,
    ) -> f64 { ... }
    fn measure_svg_simple_text_bbox_height_px(
        &self,
        text: &str,
        style: &TextStyle,
    ) -> f64 { ... }
    fn measure_wrapped(
        &self,
        text: &str,
        style: &TextStyle,
        max_width: Option<f64>,
        wrap_mode: WrapMode,
    ) -> TextMetrics { ... }
    fn measure_wrapped_with_raw_width(
        &self,
        text: &str,
        style: &TextStyle,
        max_width: Option<f64>,
        wrap_mode: WrapMode,
    ) -> (TextMetrics, Option<f64>) { ... }
    fn measure_wrapped_raw(
        &self,
        text: &str,
        style: &TextStyle,
        max_width: Option<f64>,
        wrap_mode: WrapMode,
    ) -> TextMetrics { ... }
}

Required Methods§

Source

fn measure(&self, text: &str, style: &TextStyle) -> TextMetrics

Provided Methods§

Source

fn measure_svg_text_computed_length_px( &self, text: &str, style: &TextStyle, ) -> f64

Measures SVG <tspan>.getComputedTextLength()-like widths (advance length along the baseline).

Mermaid’s Timeline diagram uses getComputedTextLength() to decide when to wrap tokens into additional <tspan> lines. This length can differ meaningfully from getBBox().width (which includes glyph overhang), especially near wrapping boundaries.

Default implementation falls back to bbox-derived widths.

Source

fn measure_svg_text_bbox_x(&self, text: &str, style: &TextStyle) -> (f64, f64)

Measures the horizontal extents of an SVG <text> element relative to its anchor x.

Mermaid’s flowchart-v2 viewport sizing uses getBBox() on the rendered SVG. For <text> elements this bbox can be slightly asymmetric around the anchor due to glyph overhangs.

Default implementation assumes a symmetric bbox: left = right = width/2.

Source

fn measure_svg_text_bbox_x_with_ascii_overhang( &self, text: &str, style: &TextStyle, ) -> (f64, f64)

Measures SVG <text>.getBBox() horizontal extents while including ASCII overhang.

Upstream Mermaid bbox behavior can be asymmetric even for ASCII strings due to glyph outlines and hinting. Most diagrams in this codebase intentionally ignore ASCII overhang to avoid systematic viewBox drift, but some diagrams (notably timeline) rely on the actual getBBox() extents when labels can overflow node shapes.

Default implementation falls back to the symmetric bbox measurement.

Source

fn measure_svg_title_bbox_x(&self, text: &str, style: &TextStyle) -> (f64, f64)

Measures the horizontal extents for Mermaid diagram titles rendered as a single <text> node (no whitespace-tokenized <tspan> runs).

Mermaid flowchart-v2 uses this style for flowchartTitleText, and the bbox impacts the final viewBox / max-width computed via getBBox().

Source

fn measure_svg_simple_text_bbox_width_px( &self, text: &str, style: &TextStyle, ) -> f64

Measures the bbox width for Mermaid drawSimpleText(...).getBBox().width-style probes (used by upstream calculateTextWidth).

This should reflect actual glyph outline extents (including ASCII overhang where present), rather than the symmetric/center-anchored title bbox approximation.

Source

fn measure_svg_simple_text_bbox_height_px( &self, text: &str, style: &TextStyle, ) -> f64

Measures the bbox height for Mermaid drawSimpleText(...).getBBox().height-style probes.

Upstream Mermaid uses <text>.getBBox() for some diagrams (notably gitGraph commit/tag labels). Those <text> nodes are not split into <tspan> runs, and empirically their bbox height behaves closer to ~1.1em than the slightly taller first-line heuristic used by measure_wrapped(..., WrapMode::SvgLike).

Default implementation falls back to measure(...).height.

Source

fn measure_wrapped( &self, text: &str, style: &TextStyle, max_width: Option<f64>, wrap_mode: WrapMode, ) -> TextMetrics

Source

fn measure_wrapped_with_raw_width( &self, text: &str, style: &TextStyle, max_width: Option<f64>, wrap_mode: WrapMode, ) -> (TextMetrics, Option<f64>)

Measures wrapped text and (optionally) returns the unwrapped width for the same payload.

This exists mainly to avoid redundant measurement passes in diagrams that need both:

  • wrapped metrics (for height/line breaks), and
  • a raw “overflow width” probe (for sizing containers that can visually overflow).

Default implementation returns None for raw_width_px and callers may fall back to an explicit second measurement if needed.

Source

fn measure_wrapped_raw( &self, text: &str, style: &TextStyle, max_width: Option<f64>, wrap_mode: WrapMode, ) -> TextMetrics

Measures wrapped text while disabling any implementation-specific HTML overrides.

This is primarily used for Markdown labels measured via DOM in upstream Mermaid, where we want a raw regular-weight baseline before applying <strong>/<em> deltas.

Implementors§