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
use crate::{
    prelude::*,
    utils::{Brush, Point, Rectangle, String16},
};

/// Used to render a text.
pub struct TextRenderObject;

impl Into<Box<dyn RenderObject>> for TextRenderObject {
    fn into(self) -> Box<dyn RenderObject> {
        Box::new(self)
    }
}

impl RenderObject for TextRenderObject {
    fn render_self(&self, ctx: &mut Context<'_>, global_position: &Point) {
        let (bounds, text, foreground, font, font_size) = {
            let widget = ctx.widget();
            let text = widget.clone::<String16>("text");

            let txt = {
                if !text.is_empty() {
                    text
                } else {
                    widget.clone_or_default::<String16>("water_mark")
                }
            };
            (
                *widget.get::<Rectangle>("bounds"),
                txt.to_string(),
                widget.get::<Brush>("foreground").clone(),
                widget.get::<String>("font").clone(),
                *widget.get::<f64>("font_size"),
            )
        };

        if bounds.width == 0.0
            || bounds.height == 0.0
            || foreground.is_transparent()
            || font_size == 0.0
            || text.is_empty()
        {
            return;
        }

        if !text.is_empty() {
            ctx.render_context_2_d().begin_path();
            ctx.render_context_2_d().set_font_family(font);
            ctx.render_context_2_d().set_font_size(font_size);
            ctx.render_context_2_d().set_fill_style(foreground);

            ctx.render_context_2_d().fill_text(
                &text,
                global_position.x + bounds.x,
                global_position.y + bounds.y,
            );
            ctx.render_context_2_d().close_path();
        }
    }
}