Skip to main content

sim_logic/text/
atlas_limits.rs

1//! GPU-independent configuration values for the retained text bridge.
2
3use super::TextError;
4
5/// Count and nominal retained-byte limits for one prepared glyph run.
6/// These are configuration values, not allocated CPU/GPU storage.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct TextRunLimits {
9    glyphs: usize,
10    bytes: usize,
11}
12
13impl TextRunLimits {
14    /// Engine 0.4.1's positioned-glyph + sprite + conversion record accounting.
15    /// A parity test against Engine's public constant guards dependency upgrades.
16    pub const RETAINED_BYTES_PER_GLYPH: usize = 132;
17    /// Requires at least one glyph and one complete retained record.
18    pub fn new(max_glyphs: usize, max_retained_bytes: usize) -> Result<Self, TextError> {
19        if max_glyphs == 0 || max_retained_bytes < Self::RETAINED_BYTES_PER_GLYPH {
20            return Err(TextError::InvalidAtlasLimits);
21        }
22        Ok(Self {
23            glyphs: max_glyphs,
24            bytes: max_retained_bytes,
25        })
26    }
27    /// Returns maximum glyph count, conservatively including whitespace.
28    pub const fn max_glyphs(self) -> usize {
29        self.glyphs
30    }
31    /// Returns the retained run-byte ceiling, excluding atlas texels.
32    pub const fn max_retained_bytes(self) -> usize {
33        self.bytes
34    }
35}
36impl Default for TextRunLimits {
37    fn default() -> Self {
38        Self {
39            glyphs: 100_000,
40            bytes: 32 * 1024 * 1024,
41        }
42    }
43}
44
45/// Headless configuration for a per-style desktop atlas and its glyph runs.
46/// Shared font source storage is separate. Creating this value allocates nothing.
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub struct TextAtlasLimits {
49    width: u32,
50    height: u32,
51    glyphs: usize,
52    run: TextRunLimits,
53}
54
55impl TextAtlasLimits {
56    /// Requires dimensions at least 5, a 1..=65535 glyph cache, and representable
57    /// complete RGBA texel storage. Actual device limits are checked on desktop.
58    pub fn new(
59        width: u32,
60        height: u32,
61        max_cached_glyphs: usize,
62        run: TextRunLimits,
63    ) -> Result<Self, TextError> {
64        if width < 5
65            || height < 5
66            || !(1..=65535).contains(&max_cached_glyphs)
67            || (width as usize)
68                .checked_mul(height as usize)
69                .and_then(|area| area.checked_mul(4))
70                .is_none()
71        {
72            return Err(TextError::InvalidAtlasLimits);
73        }
74        Ok(Self {
75            width,
76            height,
77            glyphs: max_cached_glyphs,
78            run,
79        })
80    }
81    /// Returns physical atlas width.
82    pub const fn width(self) -> u32 {
83        self.width
84    }
85    /// Returns physical atlas height.
86    pub const fn height(self) -> u32 {
87        self.height
88    }
89    /// Returns cached glyph count limit (including spacing glyphs).
90    pub const fn max_cached_glyphs(self) -> usize {
91        self.glyphs
92    }
93    /// Returns independent per-run limits.
94    pub const fn run_budget(self) -> TextRunLimits {
95        self.run
96    }
97
98    #[cfg(feature = "text")]
99    pub(crate) const fn from_engine(budget: sim_engine::TextAtlasBudget) -> Self {
100        Self {
101            width: budget.width(),
102            height: budget.height(),
103            glyphs: budget.max_cached_glyphs(),
104            run: TextRunLimits {
105                glyphs: budget.run_budget().max_glyphs(),
106                bytes: budget.run_budget().max_retained_bytes(),
107            },
108        }
109    }
110    #[cfg(feature = "text")]
111    pub(crate) fn engine_budget(self) -> Result<sim_engine::TextAtlasBudget, TextError> {
112        let run = sim_engine::GlyphRunBudget::new(self.run.glyphs, self.run.bytes)
113            .map_err(|_| TextError::InvalidAtlasLimits)?;
114        sim_engine::TextAtlasBudget::new(self.width, self.height, self.glyphs, run)
115            .map_err(|_| TextError::InvalidAtlasLimits)
116    }
117}
118impl Default for TextAtlasLimits {
119    fn default() -> Self {
120        Self {
121            width: 1024,
122            height: 1024,
123            glyphs: 4096,
124            run: TextRunLimits::default(),
125        }
126    }
127}
128
129#[cfg(all(test, feature = "text"))]
130mod tests {
131    use super::*;
132    #[test]
133    fn cpu_accounting_and_defaults_match_the_pinned_engine() -> crate::LogicResult {
134        assert_eq!(
135            TextRunLimits::RETAINED_BYTES_PER_GLYPH,
136            sim_engine::GlyphRunBudget::RETAINED_BYTES_PER_GLYPH
137        );
138        assert_eq!(
139            TextAtlasLimits::default().engine_budget()?,
140            sim_engine::TextAtlasBudget::default()
141        );
142        for (glyphs, bytes) in [(0, 132), (1, 131), (1, 132), (10, 132), (3, usize::MAX)] {
143            assert_eq!(
144                TextRunLimits::new(glyphs, bytes).is_ok(),
145                sim_engine::GlyphRunBudget::new(glyphs, bytes).is_ok()
146            );
147        }
148        Ok(())
149    }
150}