Skip to main content

orinium_browser/engine/bridge/text/
mod.rs

1//! Text measurement abstraction for layout and rendering.
2//!
3//! # Overview
4//!
5//! This module defines the interface between the layout engine and
6//! platform-specific text measurement implementations.
7//!
8//! It does **not** own or define visual text styles.
9//! Instead, it consumes already-resolved text attributes provided
10//! by higher-level layout or rendering layers.
11//!
12//! # Responsibilities
13//!
14//! - Accept text content and layout-related parameters
15//! - Measure intrinsic text size (width, height, baseline)
16//! - Provide a backend-agnostic text measurement abstraction
17//!
18//! # Non-Responsibilities
19//!
20//! - CSS resolution or inheritance
21//! - Interpretation of visual styling semantics
22//! - Rendering or draw command generation
23//!
24//! # Data Flow
25//!
26//! ```text
27//! CSS → Layout → TextMeasurer → TextMetrics
28//! ```
29
30use std::fmt;
31
32/* ============================
33 * Measure Request
34 * ============================ */
35
36#[derive(Debug, Clone)]
37pub struct TextMeasureRequest<S> {
38    /// UTF-8 text content
39    pub text: String,
40
41    /// Opaque, resolved text attributes provided by the caller
42    pub style: S,
43}
44
45/* ============================
46 * Measured Result
47 * ============================ */
48
49/// A measured text fragment produced by [`TextMeasurer::measure_fragments`].
50///
51/// Contains the original text segment along with its measured dimensions,
52/// so callers can both retrieve the split text and obtain fragment widths
53/// for inline layout.
54#[derive(Debug, Clone)]
55pub struct MeasuredFragment {
56    pub text: String,
57    pub width: f32,
58    pub height: f32,
59}
60
61/* ============================
62 * Optional Glyph Info (Future)
63 * ============================ */
64
65#[derive(Debug, Clone)]
66pub struct GlyphMetrics {
67    pub glyph_id: u32,
68    pub x: f32,
69    pub y: f32,
70    pub advance: f32,
71}
72
73/* ============================
74 * Errors
75 * ============================ */
76
77#[derive(Debug)]
78pub enum TextMeasureError {
79    FontUnavailable,
80    UnsupportedScript,
81    LayoutOverflow,
82    Internal(String),
83}
84
85impl fmt::Display for TextMeasureError {
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        match self {
88            Self::FontUnavailable => write!(f, "Font unavailable"),
89            Self::UnsupportedScript => write!(f, "Unsupported script"),
90            Self::LayoutOverflow => write!(f, "Layout overflow"),
91            Self::Internal(s) => write!(f, "Internal error: {s}"),
92        }
93    }
94}
95
96impl std::error::Error for TextMeasureError {}
97
98/* ============================
99 * Trait
100 * ============================ */
101
102pub trait TextMeasurer<S>: Send + Sync {
103    /// Measure a single block of text and return its metrics.
104    fn measure(
105        &self,
106        request: &TextMeasureRequest<S>,
107    ) -> Result<Vec<MeasuredFragment>, TextMeasureError>;
108}
109
110/* ============================
111 * Fallback
112 * ============================ */
113
114pub mod fallback;
115pub use fallback::FallbackTextMeasurer;