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::{Alignment, Shaping, Wrapping};
15use crate::core::{Color, Pixels, Point, Rectangle, Size, Transformation};
16
17use std::borrow::Cow;
18use std::collections::HashSet;
19use std::sync::{Arc, OnceLock, 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        align_x: Alignment,
58        /// The vertical alignment of the text.
59        align_y: 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        match self {
77            Text::Paragraph {
78                position,
79                paragraph,
80                clip_bounds,
81                transformation,
82                ..
83            } => Rectangle::new(*position, paragraph.min_bounds)
84                .intersection(clip_bounds)
85                .map(|bounds| bounds * *transformation),
86            Text::Editor {
87                editor,
88                position,
89                clip_bounds,
90                transformation,
91                ..
92            } => Rectangle::new(*position, editor.bounds)
93                .intersection(clip_bounds)
94                .map(|bounds| bounds * *transformation),
95            Text::Cached {
96                bounds,
97                clip_bounds,
98                ..
99            } => bounds.intersection(clip_bounds),
100            Text::Raw { raw, .. } => Some(raw.clip_bounds),
101        }
102    }
103}
104
105/// The regular variant of the [Fira Sans] font.
106///
107/// It is loaded as part of the default fonts when the `fira-sans`
108/// feature is enabled.
109///
110/// [Fira Sans]: https://mozilla.github.io/Fira/
111#[cfg(feature = "fira-sans")]
112pub const FIRA_SANS_REGULAR: &[u8] =
113    include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice();
114
115/// Returns the global [`FontSystem`].
116pub fn font_system() -> &'static RwLock<FontSystem> {
117    static FONT_SYSTEM: OnceLock<RwLock<FontSystem>> = OnceLock::new();
118
119    FONT_SYSTEM.get_or_init(|| {
120        RwLock::new(FontSystem {
121            raw: cosmic_text::FontSystem::new_with_fonts([
122                cosmic_text::fontdb::Source::Binary(Arc::new(
123                    include_bytes!("../fonts/Iced-Icons.ttf").as_slice(),
124                )),
125                #[cfg(feature = "fira-sans")]
126                cosmic_text::fontdb::Source::Binary(Arc::new(
127                    include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice(),
128                )),
129            ]),
130            loaded_fonts: HashSet::new(),
131            version: Version::default(),
132        })
133    })
134}
135
136/// A set of system fonts.
137pub struct FontSystem {
138    raw: cosmic_text::FontSystem,
139    loaded_fonts: HashSet<usize>,
140    version: Version,
141}
142
143impl FontSystem {
144    /// Returns the raw [`cosmic_text::FontSystem`].
145    pub fn raw(&mut self) -> &mut cosmic_text::FontSystem {
146        &mut self.raw
147    }
148
149    /// Loads a font from its bytes.
150    pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
151        if let Cow::Borrowed(bytes) = bytes {
152            let address = bytes.as_ptr() as usize;
153
154            if !self.loaded_fonts.insert(address) {
155                return;
156            }
157        }
158
159        let _ = self.raw.db_mut().load_font_source(
160            cosmic_text::fontdb::Source::Binary(Arc::new(bytes.into_owned())),
161        );
162
163        self.version = Version(self.version.0 + 1);
164    }
165
166    /// Returns the current [`Version`] of the [`FontSystem`].
167    ///
168    /// Loading a font will increase the version of a [`FontSystem`].
169    pub fn version(&self) -> Version {
170        self.version
171    }
172}
173
174/// A version number.
175#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
176pub struct Version(u32);
177
178/// A weak reference to a [`cosmic_text::Buffer`] that can be drawn.
179#[derive(Debug, Clone)]
180pub struct Raw {
181    /// A weak reference to a [`cosmic_text::Buffer`].
182    pub buffer: Weak<cosmic_text::Buffer>,
183    /// The position of the text.
184    pub position: Point,
185    /// The color of the text.
186    pub color: Color,
187    /// The clip bounds of the text.
188    pub clip_bounds: Rectangle,
189}
190
191impl PartialEq for Raw {
192    fn eq(&self, _other: &Self) -> bool {
193        // TODO: There is no proper way to compare raw buffers
194        // For now, no two instances of `Raw` text will be equal.
195        // This should be fine, but could trigger unnecessary redraws
196        // in the future.
197        false
198    }
199}
200
201/// Measures the dimensions of the given [`cosmic_text::Buffer`].
202pub fn measure(buffer: &cosmic_text::Buffer) -> (Size, bool) {
203    let (width, height, has_rtl) = buffer.layout_runs().fold(
204        (0.0, 0.0, false),
205        |(width, height, has_rtl), run| {
206            (
207                run.line_w.max(width),
208                height + run.line_height,
209                has_rtl || run.rtl,
210            )
211        },
212    );
213
214    (Size::new(width, height), has_rtl)
215}
216
217/// Aligns the given [`cosmic_text::Buffer`] with the given [`Alignment`]
218/// and returns its minimum [`Size`].
219pub fn align(
220    buffer: &mut cosmic_text::Buffer,
221    font_system: &mut cosmic_text::FontSystem,
222    alignment: Alignment,
223) -> Size {
224    let (min_bounds, has_rtl) = measure(buffer);
225    let mut needs_relayout = has_rtl;
226
227    if let Some(align) = to_align(alignment) {
228        let has_multiple_lines = buffer.lines.len() > 1
229            || buffer.lines.first().is_some_and(|line| {
230                line.layout_opt().is_some_and(|layout| layout.len() > 1)
231            });
232
233        if has_multiple_lines {
234            for line in &mut buffer.lines {
235                let _ = line.set_align(Some(align));
236            }
237
238            needs_relayout = true;
239        } else if let Some(line) = buffer.lines.first_mut() {
240            needs_relayout = line.set_align(None);
241        }
242    }
243
244    // TODO: Avoid relayout with some changes to `cosmic-text` (?)
245    if needs_relayout {
246        log::trace!("Relayouting paragraph...");
247
248        buffer.set_size(
249            font_system,
250            Some(min_bounds.width),
251            Some(min_bounds.height),
252        );
253    }
254
255    min_bounds
256}
257
258/// Returns the attributes of the given [`Font`].
259pub fn to_attributes(font: Font) -> cosmic_text::Attrs<'static> {
260    cosmic_text::Attrs::new()
261        .family(to_family(font.family))
262        .weight(to_weight(font.weight))
263        .stretch(to_stretch(font.stretch))
264        .style(to_style(font.style))
265}
266
267fn to_family(family: font::Family) -> cosmic_text::Family<'static> {
268    match family {
269        font::Family::Name(name) => cosmic_text::Family::Name(name),
270        font::Family::SansSerif => cosmic_text::Family::SansSerif,
271        font::Family::Serif => cosmic_text::Family::Serif,
272        font::Family::Cursive => cosmic_text::Family::Cursive,
273        font::Family::Fantasy => cosmic_text::Family::Fantasy,
274        font::Family::Monospace => cosmic_text::Family::Monospace,
275    }
276}
277
278fn to_weight(weight: font::Weight) -> cosmic_text::Weight {
279    match weight {
280        font::Weight::Thin => cosmic_text::Weight::THIN,
281        font::Weight::ExtraLight => cosmic_text::Weight::EXTRA_LIGHT,
282        font::Weight::Light => cosmic_text::Weight::LIGHT,
283        font::Weight::Normal => cosmic_text::Weight::NORMAL,
284        font::Weight::Medium => cosmic_text::Weight::MEDIUM,
285        font::Weight::Semibold => cosmic_text::Weight::SEMIBOLD,
286        font::Weight::Bold => cosmic_text::Weight::BOLD,
287        font::Weight::ExtraBold => cosmic_text::Weight::EXTRA_BOLD,
288        font::Weight::Black => cosmic_text::Weight::BLACK,
289    }
290}
291
292fn to_stretch(stretch: font::Stretch) -> cosmic_text::Stretch {
293    match stretch {
294        font::Stretch::UltraCondensed => cosmic_text::Stretch::UltraCondensed,
295        font::Stretch::ExtraCondensed => cosmic_text::Stretch::ExtraCondensed,
296        font::Stretch::Condensed => cosmic_text::Stretch::Condensed,
297        font::Stretch::SemiCondensed => cosmic_text::Stretch::SemiCondensed,
298        font::Stretch::Normal => cosmic_text::Stretch::Normal,
299        font::Stretch::SemiExpanded => cosmic_text::Stretch::SemiExpanded,
300        font::Stretch::Expanded => cosmic_text::Stretch::Expanded,
301        font::Stretch::ExtraExpanded => cosmic_text::Stretch::ExtraExpanded,
302        font::Stretch::UltraExpanded => cosmic_text::Stretch::UltraExpanded,
303    }
304}
305
306fn to_style(style: font::Style) -> cosmic_text::Style {
307    match style {
308        font::Style::Normal => cosmic_text::Style::Normal,
309        font::Style::Italic => cosmic_text::Style::Italic,
310        font::Style::Oblique => cosmic_text::Style::Oblique,
311    }
312}
313
314fn to_align(alignment: Alignment) -> Option<cosmic_text::Align> {
315    match alignment {
316        Alignment::Default => None,
317        Alignment::Left => Some(cosmic_text::Align::Left),
318        Alignment::Center => Some(cosmic_text::Align::Center),
319        Alignment::Right => Some(cosmic_text::Align::Right),
320        Alignment::Justified => Some(cosmic_text::Align::Justified),
321    }
322}
323
324/// Converts some [`Shaping`] strategy to a [`cosmic_text::Shaping`] strategy.
325pub fn to_shaping(shaping: Shaping, text: &str) -> cosmic_text::Shaping {
326    match shaping {
327        Shaping::Auto => {
328            if text.is_ascii() {
329                cosmic_text::Shaping::Basic
330            } else {
331                cosmic_text::Shaping::Advanced
332            }
333        }
334        Shaping::Basic => cosmic_text::Shaping::Basic,
335        Shaping::Advanced => cosmic_text::Shaping::Advanced,
336    }
337}
338
339/// Converts some [`Wrapping`] strategy to a [`cosmic_text::Wrap`] strategy.
340pub fn to_wrap(wrapping: Wrapping) -> cosmic_text::Wrap {
341    match wrapping {
342        Wrapping::None => cosmic_text::Wrap::None,
343        Wrapping::Word => cosmic_text::Wrap::Word,
344        Wrapping::Glyph => cosmic_text::Wrap::Glyph,
345        Wrapping::WordOrGlyph => cosmic_text::Wrap::WordOrGlyph,
346    }
347}
348
349/// Converts some [`Color`] to a [`cosmic_text::Color`].
350pub fn to_color(color: Color) -> cosmic_text::Color {
351    let [r, g, b, a] = color.into_rgba8();
352
353    cosmic_text::Color::rgba(r, g, b, a)
354}