veloren_voxygen/ui/ice/renderer/widget/
text.rs

1use super::super::{super::FontId, IcedRenderer, Primitive};
2use glyph_brush::GlyphCruncher;
3use iced::{Color, HorizontalAlignment, Rectangle, Size, VerticalAlignment, mouse, text};
4
5impl text::Renderer for IcedRenderer {
6    type Font = FontId;
7
8    // TODO: expose as setting
9    fn default_size(&self) -> u16 { 20 }
10
11    fn measure(&self, content: &str, size: u16, font: Self::Font, bounds: Size) -> (f32, f32) {
12        // Using the physical scale might make these cached info usable below?
13        // Although we also have a position of the screen so this could be useless
14        let p_scale = self.p_scale;
15        // TODO: would be nice if the method was mut
16        let section = glyph_brush::Section {
17            screen_position: (0.0, 0.0),
18            bounds: (bounds.width * p_scale, bounds.height * p_scale),
19            layout: Default::default(),
20            text: vec![glyph_brush::Text {
21                text: content,
22                scale: (size as f32 * p_scale).into(),
23                font_id: font.0,
24                extra: (),
25            }],
26        };
27
28        let maybe_rect = self.cache.glyph_calculator().glyph_bounds(section);
29        maybe_rect.map_or((0.0, 0.0), |rect| {
30            (rect.width() / p_scale, rect.height() / p_scale)
31        })
32    }
33
34    fn draw(
35        &mut self,
36        defaults: &Self::Defaults,
37        bounds: Rectangle,
38        content: &str,
39        size: u16,
40        font: Self::Font,
41        color: Option<Color>,
42        horizontal_alignment: HorizontalAlignment,
43        vertical_alignment: VerticalAlignment,
44    ) -> Self::Output {
45        let glyphs = self.position_glyphs(
46            bounds,
47            horizontal_alignment,
48            vertical_alignment,
49            content,
50            size,
51            font,
52        );
53
54        (
55            Primitive::Text {
56                glyphs,
57                bounds,
58                linear_color: color.unwrap_or(defaults.text_color).into_linear().into(),
59            },
60            mouse::Interaction::default(),
61        )
62    }
63}