iced_graphics/
text.rs

1//! Draw text.
2pub mod cache;
3pub mod editor;
4pub mod paragraph;
5
6pub use cache::Cache;
7pub use editor::Editor;
8pub use paragraph::Paragraph;
9
10pub use cosmic_text;
11
12use crate::core::alignment;
13use crate::core::font::{self, Font};
14use crate::core::text::{Shaping, Wrapping};
15use crate::core::{Color, Pixels, Point, Rectangle, Size, Transformation};
16
17use once_cell::sync::OnceCell;
18use std::borrow::Cow;
19use std::sync::{Arc, RwLock, Weak};
20
21/// A text primitive.
22#[derive(Debug, Clone, PartialEq)]
23pub enum Text {
24    /// A paragraph.
25    #[allow(missing_docs)]
26    Paragraph {
27        paragraph: paragraph::Weak,
28        position: Point,
29        color: Color,
30        clip_bounds: Rectangle,
31        transformation: Transformation,
32    },
33    /// An editor.
34    #[allow(missing_docs)]
35    Editor {
36        editor: editor::Weak,
37        position: Point,
38        color: Color,
39        clip_bounds: Rectangle,
40        transformation: Transformation,
41    },
42    /// Some cached text.
43    Cached {
44        /// The contents of the text.
45        content: String,
46        /// The bounds of the text.
47        bounds: Rectangle,
48        /// The color of the text.
49        color: Color,
50        /// The size of the text in logical pixels.
51        size: Pixels,
52        /// The line height of the text.
53        line_height: Pixels,
54        /// The font of the text.
55        font: Font,
56        /// The horizontal alignment of the text.
57        horizontal_alignment: alignment::Horizontal,
58        /// The vertical alignment of the text.
59        vertical_alignment: alignment::Vertical,
60        /// The shaping strategy of the text.
61        shaping: Shaping,
62        /// The clip bounds of the text.
63        clip_bounds: Rectangle,
64    },
65    /// Some raw text.
66    #[allow(missing_docs)]
67    Raw {
68        raw: Raw,
69        transformation: Transformation,
70    },
71}
72
73impl Text {
74    /// Returns the visible bounds of the [`Text`].
75    pub fn visible_bounds(&self) -> Option<Rectangle> {
76        let (bounds, horizontal_alignment, vertical_alignment) = match self {
77            Text::Paragraph {
78                position,
79                paragraph,
80                clip_bounds,
81                transformation,
82                ..
83            } => (
84                Rectangle::new(*position, paragraph.min_bounds)
85                    .intersection(clip_bounds)
86                    .map(|bounds| bounds * *transformation),
87                Some(paragraph.horizontal_alignment),
88                Some(paragraph.vertical_alignment),
89            ),
90            Text::Editor {
91                editor,
92                position,
93                clip_bounds,
94                transformation,
95                ..
96            } => (
97                Rectangle::new(*position, editor.bounds)
98                    .intersection(clip_bounds)
99                    .map(|bounds| bounds * *transformation),
100                None,
101                None,
102            ),
103            Text::Cached {
104                bounds,
105                clip_bounds,
106                horizontal_alignment,
107                vertical_alignment,
108                ..
109            } => (
110                bounds.intersection(clip_bounds),
111                Some(*horizontal_alignment),
112                Some(*vertical_alignment),
113            ),
114            Text::Raw { raw, .. } => (Some(raw.clip_bounds), None, None),
115        };
116
117        let mut bounds = bounds?;
118
119        if let Some(alignment) = horizontal_alignment {
120            match alignment {
121                alignment::Horizontal::Left => {}
122                alignment::Horizontal::Center => {
123                    bounds.x -= bounds.width / 2.0;
124                }
125                alignment::Horizontal::Right => {
126                    bounds.x -= bounds.width;
127                }
128            }
129        }
130
131        if let Some(alignment) = vertical_alignment {
132            match alignment {
133                alignment::Vertical::Top => {}
134                alignment::Vertical::Center => {
135                    bounds.y -= bounds.height / 2.0;
136                }
137                alignment::Vertical::Bottom => {
138                    bounds.y -= bounds.height;
139                }
140            }
141        }
142
143        Some(bounds)
144    }
145}
146
147/// The regular variant of the [Fira Sans] font.
148///
149/// It is loaded as part of the default fonts in Wasm builds.
150///
151/// [Fira Sans]: https://mozilla.github.io/Fira/
152#[cfg(all(target_arch = "wasm32", feature = "fira-sans"))]
153pub const FIRA_SANS_REGULAR: &'static [u8] =
154    include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice();
155
156/// Returns the global [`FontSystem`].
157pub fn font_system() -> &'static RwLock<FontSystem> {
158    static FONT_SYSTEM: OnceCell<RwLock<FontSystem>> = OnceCell::new();
159
160    FONT_SYSTEM.get_or_init(|| {
161        RwLock::new(FontSystem {
162            raw: cosmic_text::FontSystem::new_with_fonts([
163                cosmic_text::fontdb::Source::Binary(Arc::new(
164                    include_bytes!("../fonts/Iced-Icons.ttf").as_slice(),
165                )),
166                #[cfg(all(target_arch = "wasm32", feature = "fira-sans"))]
167                cosmic_text::fontdb::Source::Binary(Arc::new(
168                    include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice(),
169                )),
170            ]),
171            version: Version::default(),
172        })
173    })
174}
175
176/// A set of system fonts.
177#[allow(missing_debug_implementations)]
178pub struct FontSystem {
179    raw: cosmic_text::FontSystem,
180    version: Version,
181}
182
183impl FontSystem {
184    /// Returns the raw [`cosmic_text::FontSystem`].
185    pub fn raw(&mut self) -> &mut cosmic_text::FontSystem {
186        &mut self.raw
187    }
188
189    /// Loads a font from its bytes.
190    pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
191        let _ = self.raw.db_mut().load_font_source(
192            cosmic_text::fontdb::Source::Binary(Arc::new(bytes.into_owned())),
193        );
194
195        self.version = Version(self.version.0 + 1);
196    }
197
198    /// Returns the current [`Version`] of the [`FontSystem`].
199    ///
200    /// Loading a font will increase the version of a [`FontSystem`].
201    pub fn version(&self) -> Version {
202        self.version
203    }
204}
205
206/// A version number.
207#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
208pub struct Version(u32);
209
210/// A weak reference to a [`cosmic-text::Buffer`] that can be drawn.
211#[derive(Debug, Clone)]
212pub struct Raw {
213    /// A weak reference to a [`cosmic_text::Buffer`].
214    pub buffer: Weak<cosmic_text::Buffer>,
215    /// The position of the text.
216    pub position: Point,
217    /// The color of the text.
218    pub color: Color,
219    /// The clip bounds of the text.
220    pub clip_bounds: Rectangle,
221}
222
223impl PartialEq for Raw {
224    fn eq(&self, _other: &Self) -> bool {
225        // TODO: There is no proper way to compare raw buffers
226        // For now, no two instances of `Raw` text will be equal.
227        // This should be fine, but could trigger unnecessary redraws
228        // in the future.
229        false
230    }
231}
232
233/// Measures the dimensions of the given [`cosmic_text::Buffer`].
234pub fn measure(buffer: &cosmic_text::Buffer) -> Size {
235    let (width, height) =
236        buffer
237            .layout_runs()
238            .fold((0.0, 0.0), |(width, height), run| {
239                (run.line_w.max(width), height + run.line_height)
240            });
241    Size::new(width, height)
242
243    // let (max_width_opt, max_height_opt) = buffer.size();
244
245    // Size::new(
246    //     width.min(max_width_opt.unwrap_or(f32::MAX)),
247    //     (buffer.total_lines as f32 * buffer.metrics().line_height)
248    //         .min(max_height_opt.unwrap_or(f32::MAX)),
249    // )
250}
251
252/// Returns the attributes of the given [`Font`].
253pub fn to_attributes(font: Font) -> cosmic_text::Attrs<'static> {
254    cosmic_text::Attrs::new()
255        .family(to_family(font.family))
256        .weight(to_weight(font.weight))
257        .stretch(to_stretch(font.stretch))
258        .style(to_style(font.style))
259}
260
261fn to_family(family: font::Family) -> cosmic_text::Family<'static> {
262    match family {
263        font::Family::Name(name) => cosmic_text::Family::Name(name),
264        font::Family::SansSerif => cosmic_text::Family::SansSerif,
265        font::Family::Serif => cosmic_text::Family::Serif,
266        font::Family::Cursive => cosmic_text::Family::Cursive,
267        font::Family::Fantasy => cosmic_text::Family::Fantasy,
268        font::Family::Monospace => cosmic_text::Family::Monospace,
269    }
270}
271
272fn to_weight(weight: font::Weight) -> cosmic_text::Weight {
273    match weight {
274        font::Weight::Thin => cosmic_text::Weight::THIN,
275        font::Weight::ExtraLight => cosmic_text::Weight::EXTRA_LIGHT,
276        font::Weight::Light => cosmic_text::Weight::LIGHT,
277        font::Weight::Normal => cosmic_text::Weight::NORMAL,
278        font::Weight::Medium => cosmic_text::Weight::MEDIUM,
279        font::Weight::Semibold => cosmic_text::Weight::SEMIBOLD,
280        font::Weight::Bold => cosmic_text::Weight::BOLD,
281        font::Weight::ExtraBold => cosmic_text::Weight::EXTRA_BOLD,
282        font::Weight::Black => cosmic_text::Weight::BLACK,
283    }
284}
285
286fn to_stretch(stretch: font::Stretch) -> cosmic_text::Stretch {
287    match stretch {
288        font::Stretch::UltraCondensed => cosmic_text::Stretch::UltraCondensed,
289        font::Stretch::ExtraCondensed => cosmic_text::Stretch::ExtraCondensed,
290        font::Stretch::Condensed => cosmic_text::Stretch::Condensed,
291        font::Stretch::SemiCondensed => cosmic_text::Stretch::SemiCondensed,
292        font::Stretch::Normal => cosmic_text::Stretch::Normal,
293        font::Stretch::SemiExpanded => cosmic_text::Stretch::SemiExpanded,
294        font::Stretch::Expanded => cosmic_text::Stretch::Expanded,
295        font::Stretch::ExtraExpanded => cosmic_text::Stretch::ExtraExpanded,
296        font::Stretch::UltraExpanded => cosmic_text::Stretch::UltraExpanded,
297    }
298}
299
300fn to_style(style: font::Style) -> cosmic_text::Style {
301    match style {
302        font::Style::Normal => cosmic_text::Style::Normal,
303        font::Style::Italic => cosmic_text::Style::Italic,
304        font::Style::Oblique => cosmic_text::Style::Oblique,
305    }
306}
307
308/// Converts some [`Shaping`] strategy to a [`cosmic_text::Shaping`] strategy.
309pub fn to_shaping(shaping: Shaping) -> cosmic_text::Shaping {
310    match shaping {
311        Shaping::Basic => cosmic_text::Shaping::Basic,
312        Shaping::Advanced => cosmic_text::Shaping::Advanced,
313    }
314}
315
316/// Converts some [`Wrapping`] strategy to a [`cosmic_text::Wrap`] strategy.
317pub fn to_wrap(wrapping: Wrapping) -> cosmic_text::Wrap {
318    match wrapping {
319        Wrapping::None => cosmic_text::Wrap::None,
320        Wrapping::Word => cosmic_text::Wrap::Word,
321        Wrapping::Glyph => cosmic_text::Wrap::Glyph,
322        Wrapping::WordOrGlyph => cosmic_text::Wrap::WordOrGlyph,
323    }
324}
325
326/// Converts some [`Color`] to a [`cosmic_text::Color`].
327pub fn to_color(color: Color) -> cosmic_text::Color {
328    let [r, g, b, a] = color.into_rgba8();
329
330    cosmic_text::Color::rgba(r, g, b, a)
331}