StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.
| 1 | use crate::fonts::{FontFallbackCache, RequestedFallbackFontSource}; |
| 2 | use crate::platform; |
| 3 | use crate::platform::LineStyle; |
| 4 | use crate::text_layout::{ClipConfig, Line, StyleAndFont, TextAlignment, TextFrame}; |
| 5 | use std::ops::Range; |
| 6 | |
| 7 | /// Struct to layout text, updating cached font fallback state as needed. |
| 8 | /// See [fonts::Cache::text_layout_system]. |
| 9 | pub struct TextLayoutSystem<'a> { |
| 10 | pub(super) platform: &'a dyn platform::TextLayoutSystem, |
| 11 | pub(super) cache: &'a FontFallbackCache, |
| 12 | } |
| 13 | |
| 14 | impl TextLayoutSystem<'_> { |
| 15 | /// Checks if the application specified a fallback font for the given char. |
| 16 | /// If yes, the UI framework will lazy load the fallback font and trigger |
| 17 | /// a re-render of the window. |
| 18 | pub(crate) fn request_fallback_font_for_char( |
| 19 | &self, |
| 20 | ch: char, |
| 21 | source: RequestedFallbackFontSource, |
| 22 | ) { |
| 23 | self.cache.request_fallback_font_for_char(ch, source) |
| 24 | } |
| 25 | |
| 26 | pub fn layout_line( |
| 27 | &self, |
| 28 | text: &str, |
| 29 | line_style: LineStyle, |
| 30 | style_runs: &[(Range<usize>, StyleAndFont)], |
| 31 | max_width: f32, |
| 32 | clip_config: ClipConfig, |
| 33 | ) -> Line { |
| 34 | self.platform |
| 35 | .layout_line(text, line_style, style_runs, max_width, clip_config) |
| 36 | } |
| 37 | |
| 38 | #[allow(clippy::too_many_arguments)] |
| 39 | pub fn layout_text( |
| 40 | &self, |
| 41 | text: &str, |
| 42 | line_style: LineStyle, |
| 43 | style_runs: &[(Range<usize>, StyleAndFont)], |
| 44 | max_width: f32, |
| 45 | max_height: f32, |
| 46 | alignment: TextAlignment, |
| 47 | first_line_head_indent: Option<f32>, |
| 48 | ) -> TextFrame { |
| 49 | self.platform.layout_text( |
| 50 | text, |
| 51 | line_style, |
| 52 | style_runs, |
| 53 | max_width, |
| 54 | max_height, |
| 55 | alignment, |
| 56 | first_line_head_indent, |
| 57 | ) |
| 58 | } |
| 59 | } |
| 60 |