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    /// Maximum line width (None = unconstrained)
45    pub max_width: Option<f32>,
46
47    /// Enable line wrapping
48    pub wrap: bool,
49}
50
51/* ============================
52 * Measure Result
53 * ============================ */
54
55#[derive(Debug, Clone)]
56pub struct TextMetrics {
57    /// Logical width
58    pub width: f32,
59
60    /// Logical height
61    pub height: f32,
62
63    /// Baseline position from top
64    pub baseline: f32,
65
66    /// Number of layouted lines
67    pub line_count: usize,
68}
69
70/* ============================
71 * Optional Glyph Info (Future)
72 * ============================ */
73
74#[derive(Debug, Clone)]
75pub struct GlyphMetrics {
76    pub glyph_id: u32,
77    pub x: f32,
78    pub y: f32,
79    pub advance: f32,
80}
81
82/* ============================
83 * Errors
84 * ============================ */
85
86#[derive(Debug)]
87pub enum TextMeasureError {
88    FontUnavailable,
89    UnsupportedScript,
90    LayoutOverflow,
91    Internal(String),
92}
93
94impl fmt::Display for TextMeasureError {
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        match self {
97            Self::FontUnavailable => write!(f, "Font unavailable"),
98            Self::UnsupportedScript => write!(f, "Unsupported script"),
99            Self::LayoutOverflow => write!(f, "Layout overflow"),
100            Self::Internal(s) => write!(f, "Internal error: {s}"),
101        }
102    }
103}
104
105impl std::error::Error for TextMeasureError {}
106
107/* ============================
108 * Trait
109 * ============================ */
110
111pub trait TextMeasurer<S>: Send + Sync {
112    fn measure(&self, request: &TextMeasureRequest<S>) -> Result<TextMetrics, TextMeasureError>;
113}
114
115/* ============================
116 * Fallback
117 * ============================ */
118
119pub mod fallback;
120pub use fallback::FallbackTextMeasurer;