Skip to main content

sim_logic/rendering/extraction/
text.rs

1//! Shared prepared labels and application-provenance checks without GPU work.
2
3use sim_engine::{Color, Layer, LogicalScreenPosition};
4
5use crate::{
6    identity::{LogicEntity, WorldGeneration},
7    text::{ScreenTextVisual, TextFont, TextMetrics, TextRegistry},
8};
9
10use super::super::ExtractionError;
11
12/// One prepared screen label retained by a complete CPU snapshot.
13///
14/// Text and its immutable font are shared with the component; extraction does
15/// not reshape strings, copy font bytes, or hold a renderer resource. Positions
16/// use current logical pixels, independently of fixed-step interpolation.
17#[derive(Debug, Clone, PartialEq)]
18pub struct ResolvedScreenText {
19    source: LogicEntity,
20    visual: ScreenTextVisual,
21}
22
23impl ResolvedScreenText {
24    /// Returns the managed entity that produced this label.
25    pub const fn source(&self) -> LogicEntity {
26        self.source
27    }
28
29    /// Returns the immutable application-registered font used for preparation.
30    pub fn font(&self) -> &TextFont {
31        self.visual.font()
32    }
33
34    /// Returns the shared UTF-8 source string.
35    pub fn text(&self) -> &str {
36        self.visual.text()
37    }
38
39    /// Returns shaping metrics already calculated when the label was prepared.
40    pub fn metrics(&self) -> TextMetrics {
41        self.visual.metrics()
42    }
43
44    /// Returns the requested baseline anchor in logical screen pixels.
45    pub fn position(&self) -> LogicalScreenPosition {
46        self.visual.position()
47    }
48
49    /// Returns the left baseline origin after horizontal alignment is applied.
50    pub fn baseline_origin(&self) -> LogicalScreenPosition {
51        self.visual.baseline_origin()
52    }
53
54    /// Returns normalized straight-linear RGBA tint for this placement.
55    pub fn tint(&self) -> Color {
56        self.visual.tint()
57    }
58
59    /// Returns the primary mixed-screen painter-order layer.
60    pub fn layer(&self) -> Layer {
61        self.visual.layer()
62    }
63
64    /// Returns secondary mixed-screen painter order, not geometric camera depth.
65    pub fn draw_order_depth(&self) -> f32 {
66        self.visual.draw_order_depth()
67    }
68
69    /// Returns the complete immutable snapshot value, including shared preparation.
70    pub const fn visual(&self) -> &ScreenTextVisual {
71        &self.visual
72    }
73}
74
75#[derive(Debug, Clone, PartialEq)]
76pub(crate) struct ScreenTextSource {
77    entity: LogicEntity,
78    visual: ScreenTextVisual,
79    registered: bool,
80}
81
82impl ScreenTextSource {
83    pub(crate) fn new(
84        entity: LogicEntity,
85        visual: &ScreenTextVisual,
86        registry: &TextRegistry,
87    ) -> Self {
88        Self {
89            entity,
90            visual: visual.clone(),
91            registered: registry.contains(visual.font()),
92        }
93    }
94
95    pub(super) fn resolve(
96        self,
97        generation: WorldGeneration,
98    ) -> Result<ResolvedScreenText, ExtractionError> {
99        if self.entity.world_generation() != generation {
100            return Err(ExtractionError::ForeignEntity {
101                entity: self.entity,
102                expected: generation,
103            });
104        }
105        if !self.registered {
106            return Err(ExtractionError::UnregisteredTextFont {
107                entity: self.entity,
108                font: self.visual.font().clone(),
109            });
110        }
111        self.visual
112            .validate()
113            .map_err(|error| ExtractionError::InvalidScreenText {
114                entity: self.entity,
115                error,
116            })?;
117        Ok(ResolvedScreenText {
118            source: self.entity,
119            visual: self.visual,
120        })
121    }
122}