pub trait TextMeasurer {
Show 19 methods
// 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_raw_text_bbox_width_px(
&self,
text: &str,
style: &TextStyle,
) -> f64 { ... }
fn measure_svg_raw_text_bbox_height_px(
&self,
text: &str,
style: &TextStyle,
) -> f64 { ... }
fn measure_svg_text_bounding_client_rect_width_px(
&self,
text: &str,
style: &TextStyle,
) -> f64 { ... }
fn measure_svg_tspan_text_bbox_width_px(
&self,
text: &str,
style: &TextStyle,
) -> f64 { ... }
fn measure_svg_tspan_text_bbox_height_px(
&self,
text: &str,
style: &TextStyle,
) -> f64 { ... }
fn measure_svg_create_text_bbox_y_offset_px(
&self,
_text: &str,
_style: &TextStyle,
) -> f64 { ... }
fn measure_svg_create_text_middle_bbox_y_offset_px(
&self,
_text: &str,
_style: &TextStyle,
) -> f64 { ... }
fn measure_svg_simple_text_bbox_width_for_wrap_px(
&self,
text: &str,
style: &TextStyle,
) -> f64 { ... }
fn measure_mermaid_calculate_text_dimensions(
&self,
text: &str,
style: &TextStyle,
) -> TextMetrics { ... }
fn measure_canvas_text_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>) { ... }
}Expand description
Measures label text for layout decisions.
This trait is deliberately infallible. Implementations must not call a host and silently choose
their own fallback; that would bypass the operation’s phase routing. Use
crate::environment::HostTextMeasurer for host callbacks that can decline or fail.
TextMeasurer is the extension point for editors and other hosts that need layout to match
their own font system. Implementations should cache aggressively: flowchart/class/sequence
layout can ask for the same label in several wrap modes while computing nodes, edges, and final
SVG.
The default deterministic measurer provides portable, bounded heuristics without font assets. A host-provided measurer can instead use platform text APIs, a UI toolkit text system, or an optional font engine while preserving the rest of merman’s parser/layout/render pipeline.
Required Methods§
fn measure(&self, text: &str, style: &TextStyle) -> TextMetrics
Provided Methods§
Sourcefn measure_svg_text_computed_length_px(
&self,
text: &str,
style: &TextStyle,
) -> f64
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.
Sourcefn measure_svg_text_bbox_x(&self, text: &str, style: &TextStyle) -> (f64, f64)
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.
Sourcefn measure_svg_text_bbox_x_with_ascii_overhang(
&self,
text: &str,
style: &TextStyle,
) -> (f64, f64)
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.
Sourcefn measure_svg_title_bbox_x(&self, text: &str, style: &TextStyle) -> (f64, f64)
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().
Sourcefn measure_svg_simple_text_bbox_width_px(
&self,
text: &str,
style: &TextStyle,
) -> f64
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.
Sourcefn measure_svg_raw_text_bbox_width_px(
&self,
text: &str,
style: &TextStyle,
) -> f64
fn measure_svg_raw_text_bbox_width_px( &self, text: &str, style: &TextStyle, ) -> f64
Measures raw SVG <text>.getBBox().width for diagram renderers that append text directly.
Unlike TextMeasurer::measure_svg_simple_text_bbox_width_px, this models the DOM shape
directly and does not inherit drawSimpleText(...)-specific behavior.
Sourcefn measure_svg_raw_text_bbox_height_px(
&self,
text: &str,
style: &TextStyle,
) -> f64
fn measure_svg_raw_text_bbox_height_px( &self, text: &str, style: &TextStyle, ) -> f64
Measures raw SVG <text>.getBBox().height for direct text content.
This stays distinct from tspan and Mermaid helper probes so browser hosts can preserve the exact DOM shape used by renderers such as TreeView.
Sourcefn measure_svg_text_bounding_client_rect_width_px(
&self,
text: &str,
style: &TextStyle,
) -> f64
fn measure_svg_text_bounding_client_rect_width_px( &self, text: &str, style: &TextStyle, ) -> f64
Measures the CSS pixel width returned by SVG <text>.getBoundingClientRect().width.
This is a distinct browser primitive from getBBox().width: transforms, CSS pixel
quantization, and layout-engine rounding can make their results diverge. Mermaid uses the
client rect directly for Journey actor labels and Pie legend/title sizing. Headless
profiles fall back to the closest raw SVG text width; browser hosts should implement the
exact DOM operation.
Sourcefn measure_svg_tspan_text_bbox_width_px(
&self,
text: &str,
style: &TextStyle,
) -> f64
fn measure_svg_tspan_text_bbox_width_px( &self, text: &str, style: &TextStyle, ) -> f64
Measures a <text> node whose visible rows are emitted as child <tspan> elements.
Browsers can apply different hinting and endpoint extents to this DOM shape than to a direct text node. The default keeps custom measurers source-compatible by falling back to raw SVG text measurement.
Sourcefn measure_svg_tspan_text_bbox_height_px(
&self,
text: &str,
style: &TextStyle,
) -> f64
fn measure_svg_tspan_text_bbox_height_px( &self, text: &str, style: &TextStyle, ) -> f64
Measures the bbox height of a <text> node whose visible rows are child <tspan> nodes.
Sourcefn measure_svg_create_text_bbox_y_offset_px(
&self,
_text: &str,
_style: &TextStyle,
) -> f64
fn measure_svg_create_text_bbox_y_offset_px( &self, _text: &str, _style: &TextStyle, ) -> f64
Measures the getBBox().y offset of Mermaid’s createFormattedText(...) SVG label.
That DOM shape positions an outer <tspan> with y="-0.1em" and dy="1.1em" under a
<text y="-10.1"> element. The resulting offset depends on the selected font and may be
negative, so browser-backed hosts should implement this operation directly.
Sourcefn measure_svg_create_text_middle_bbox_y_offset_px(
&self,
_text: &str,
_style: &TextStyle,
) -> f64
fn measure_svg_create_text_middle_bbox_y_offset_px( &self, _text: &str, _style: &TextStyle, ) -> f64
Measures the same createFormattedText(...) bbox y offset after the outer Architecture
label container applies inherited dominant-baseline: middle.
SVG middle-baseline positioning depends on the selected font’s x-height and is distinct from the raw createText bbox operation. Browser-backed hosts should measure this DOM shape directly. Profiles without this operation return a neutral value rather than reusing the ordinary formatted-text result or guessing an x-height from an unrelated font.
Sourcefn measure_svg_simple_text_bbox_width_for_wrap_px(
&self,
text: &str,
style: &TextStyle,
) -> f64
fn measure_svg_simple_text_bbox_width_for_wrap_px( &self, text: &str, style: &TextStyle, ) -> f64
Measures simple SVG text for wrap decisions.
Incremental wrapLabel(...) probes and final rendered text can use different DOM shapes.
Implementations may specialize this method when the wrap probe’s browser API differs from
the final node’s bbox API.
Sourcefn measure_mermaid_calculate_text_dimensions(
&self,
text: &str,
style: &TextStyle,
) -> TextMetrics
fn measure_mermaid_calculate_text_dimensions( &self, text: &str, style: &TextStyle, ) -> TextMetrics
Measures one SVG row using Mermaid’s body-attached calculateTextDimensions(...) probe.
The probe’s CSSOM assignment and DOM attachment can affect both dimensions, so this stays a single metrics operation rather than combining an operation-specific width with a generic height. Profiles may specialize it when invalid CSS falls back to the host’s default font.
Sourcefn measure_canvas_text_width_px(&self, text: &str, style: &TextStyle) -> f64
fn measure_canvas_text_width_px(&self, text: &str, style: &TextStyle) -> f64
Measures Canvas2D measureText(...).width for canvas-backed layout engines.
Canvas text advance is a distinct host primitive from SVG bbox and computed-length APIs. The default uses the closest baseline-advance operation so existing custom profiles remain source-compatible; browser-backed hosts should implement the exact Canvas2D operation.
Sourcefn measure_svg_simple_text_bbox_height_px(
&self,
text: &str,
style: &TextStyle,
) -> f64
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.
Despite this method’s historical “simple text” name, upstream drawSimpleText(...)
appends one child <tspan> to the <text> node. Exact implementations therefore route it
through their single-tspan DOM-shape profile, not raw direct text content.
Default implementation falls back to measure(...).height.
fn measure_wrapped( &self, text: &str, style: &TextStyle, max_width: Option<f64>, wrap_mode: WrapMode, ) -> TextMetrics
Sourcefn measure_wrapped_with_raw_width(
&self,
text: &str,
style: &TextStyle,
max_width: Option<f64>,
wrap_mode: WrapMode,
) -> (TextMetrics, Option<f64>)
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.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".