1use crate::{LogicalPixels, PhysicalPerLogical};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum FontBudgetResource {
6 FontBytes,
8 FontGlyphs,
10 TextBytes,
12 ShapedGlyphs,
14 GlyphPixels,
16 OutlineSegments,
18}
19
20#[derive(Debug, Clone, PartialEq, Eq)]
22pub enum FontError {
23 InvalidFont,
25 BudgetExceeded {
27 resource: FontBudgetResource,
29 required: usize,
31 limit: usize,
33 },
34 InvalidScale,
36 UnsupportedText {
38 byte_index: usize,
40 },
41 MissingGlyph {
43 byte_index: usize,
45 },
46 InvalidGlyph {
48 glyph_id: u16,
50 },
51 UnsupportedGlyph {
53 glyph_id: u16,
55 },
56 InvalidGeometry,
58 AllocationFailed {
60 requested_bytes: usize,
62 },
63}
64
65impl std::fmt::Display for FontError {
66 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67 match self {
68 Self::InvalidFont => formatter.write_str("invalid or unsupported outline font"),
69 Self::BudgetExceeded {
70 resource,
71 required,
72 limit,
73 } => write!(
74 formatter,
75 "font {resource:?} requires {required}, limit is {limit}"
76 ),
77 Self::InvalidScale => formatter.write_str("invalid physical font scale"),
78 Self::UnsupportedText { byte_index } => write!(
79 formatter,
80 "unsupported single-line character at UTF-8 byte {byte_index}"
81 ),
82 Self::MissingGlyph { byte_index } => write!(
83 formatter,
84 "font has no glyph for UTF-8 cluster at byte {byte_index}"
85 ),
86 Self::InvalidGlyph { glyph_id } => {
87 write!(formatter, "invalid font glyph id {glyph_id}")
88 }
89 Self::UnsupportedGlyph { glyph_id } => write!(
90 formatter,
91 "font glyph {glyph_id} requires unsupported color or bitmap presentation"
92 ),
93 Self::InvalidGeometry => {
94 formatter.write_str("font geometry is not finite or representable")
95 }
96 Self::AllocationFailed { requested_bytes } => write!(
97 formatter,
98 "font allocation failed for {requested_bytes} bytes"
99 ),
100 }
101 }
102}
103
104impl std::error::Error for FontError {}
105
106#[derive(Debug, Clone, Copy, PartialEq, Eq)]
109pub struct FontBudget {
110 max_font_bytes: usize,
111 max_font_glyphs: usize,
112}
113
114impl FontBudget {
115 pub const fn new(max_font_bytes: usize, max_font_glyphs: usize) -> Self {
117 Self {
118 max_font_bytes,
119 max_font_glyphs,
120 }
121 }
122 pub const fn max_font_bytes(self) -> usize {
124 self.max_font_bytes
125 }
126 pub const fn max_font_glyphs(self) -> usize {
128 self.max_font_glyphs
129 }
130}
131
132impl Default for FontBudget {
133 fn default() -> Self {
134 Self::new(8 * 1024 * 1024, 65_535)
135 }
136}
137
138#[derive(Debug, Clone, Copy, PartialEq, Eq)]
146pub struct TextLayoutBudget {
147 max_text_bytes: usize,
148 max_shaped_glyphs: usize,
149 max_glyph_pixels: usize,
150 max_outline_segments: usize,
151}
152
153impl TextLayoutBudget {
154 pub const fn new(
156 text_bytes: usize,
157 shaped_glyphs: usize,
158 glyph_pixels: usize,
159 outline_segments: usize,
160 ) -> Self {
161 Self {
162 max_text_bytes: text_bytes,
163 max_shaped_glyphs: shaped_glyphs,
164 max_glyph_pixels: glyph_pixels,
165 max_outline_segments: outline_segments,
166 }
167 }
168 pub const fn max_text_bytes(self) -> usize {
170 self.max_text_bytes
171 }
172 pub const fn max_shaped_glyphs(self) -> usize {
174 self.max_shaped_glyphs
175 }
176 pub const fn max_glyph_pixels(self) -> usize {
178 self.max_glyph_pixels
179 }
180 pub const fn max_outline_segments(self) -> usize {
182 self.max_outline_segments
183 }
184}
185
186impl Default for TextLayoutBudget {
187 fn default() -> Self {
188 Self::new(16 * 1024, 16 * 1024, 1024 * 1024, 16 * 1024)
189 }
190}
191
192#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
195pub enum TextDirection {
196 #[default]
198 Auto,
199 Ltr,
201 Rtl,
203}
204
205#[derive(Debug, Clone, Copy, PartialEq)]
207pub struct TextStyle {
208 logical_em_size: LogicalPixels,
209 scale: PhysicalPerLogical,
210 direction: TextDirection,
211}
212
213impl TextStyle {
214 pub fn new(
217 logical_em_size: LogicalPixels,
218 scale: PhysicalPerLogical,
219 ) -> Result<Self, FontError> {
220 let physical = logical_em_size.get() * scale.get();
221 if !physical.is_normal() || physical <= 0.0 {
222 return Err(FontError::InvalidScale);
223 }
224 Ok(Self {
225 logical_em_size,
226 scale,
227 direction: TextDirection::Auto,
228 })
229 }
230 pub const fn with_direction(mut self, direction: TextDirection) -> Self {
232 self.direction = direction;
233 self
234 }
235 pub const fn logical_em_size(self) -> LogicalPixels {
237 self.logical_em_size
238 }
239 pub const fn scale(self) -> PhysicalPerLogical {
241 self.scale
242 }
243 pub const fn direction(self) -> TextDirection {
245 self.direction
246 }
247 pub fn physical_em_size(self) -> f32 {
249 self.logical_em_size.get() * self.scale.get()
250 }
251}
252
253pub(super) fn check(
254 resource: FontBudgetResource,
255 required: usize,
256 limit: usize,
257) -> Result<(), FontError> {
258 if required > limit {
259 Err(FontError::BudgetExceeded {
260 resource,
261 required,
262 limit,
263 })
264 } else {
265 Ok(())
266 }
267}
268
269pub(super) fn reserve<T>(values: &mut Vec<T>, count: usize) -> Result<(), FontError> {
270 let requested_bytes = count
271 .checked_mul(std::mem::size_of::<T>())
272 .ok_or(FontError::InvalidGeometry)?;
273 values
274 .try_reserve_exact(count)
275 .map_err(|_| FontError::AllocationFailed { requested_bytes })
276}