1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use super::super::{super::FontId, IcedRenderer, Primitive};
use glyph_brush::GlyphCruncher;
use iced::{mouse, text, Color, HorizontalAlignment, Rectangle, Size, VerticalAlignment};

impl text::Renderer for IcedRenderer {
    type Font = FontId;

    // TODO: expose as setting
    fn default_size(&self) -> u16 { 20 }

    fn measure(&self, content: &str, size: u16, font: Self::Font, bounds: Size) -> (f32, f32) {
        // Using the physical scale might make these cached info usable below?
        // Although we also have a position of the screen so this could be useless
        let p_scale = self.p_scale;
        // TODO: would be nice if the method was mut
        let section = glyph_brush::Section {
            screen_position: (0.0, 0.0),
            bounds: (bounds.width * p_scale, bounds.height * p_scale),
            layout: Default::default(),
            text: vec![glyph_brush::Text {
                text: content,
                scale: (size as f32 * p_scale).into(),
                font_id: font.0,
                extra: (),
            }],
        };

        let maybe_rect = self.cache.glyph_calculator().glyph_bounds(section);
        maybe_rect.map_or((0.0, 0.0), |rect| {
            (rect.width() / p_scale, rect.height() / p_scale)
        })
    }

    fn draw(
        &mut self,
        defaults: &Self::Defaults,
        bounds: Rectangle,
        content: &str,
        size: u16,
        font: Self::Font,
        color: Option<Color>,
        horizontal_alignment: HorizontalAlignment,
        vertical_alignment: VerticalAlignment,
    ) -> Self::Output {
        let glyphs = self.position_glyphs(
            bounds,
            horizontal_alignment,
            vertical_alignment,
            content,
            size,
            font,
        );

        (
            Primitive::Text {
                glyphs,
                bounds,
                linear_color: color.unwrap_or(defaults.text_color).into_linear().into(),
            },
            mouse::Interaction::default(),
        )
    }
}