Skip to main content

ferroui_base/media/
glyph_run_drawing.rs

1use crate::media::{Drawing, DrawingContext, DrawingImpl, GlyphRun, IBrush};
2use crate::{ferro_class, ferro_impl_classes, ferro_property, instantiate, FerroObjectImpl, FerroProperty, Rect, Ref, StyledProperty};
3use std::rc::Rc;
4
5/// Represents a drawing of a [`GlyphRun`] with a foreground brush.
6#[repr(C)]
7pub struct GlyphRunDrawing {
8    base: Drawing,
9}
10
11ferro_class!(GlyphRunDrawing: Drawing);
12crate::ferro_class_info!(GlyphRunDrawing { new: GlyphRunDrawing::new });
13ferro_impl_classes!(GlyphRunDrawing: FerroObjectImpl);
14
15impl DrawingImpl for GlyphRunDrawing {
16    fn draw_core(this: &Self, context: &mut DrawingContext) {
17        let Some(glyph_run) = this.glyph_run() else {
18            return;
19        };
20
21        context.draw_glyph_run(this.foreground().as_ref(), &glyph_run);
22    }
23
24    fn get_bounds(this: &Self) -> Rect {
25        this.glyph_run().map(|glyph_run| glyph_run.bounds()).unwrap_or_default()
26    }
27}
28
29crate::ferro_properties! { impl GlyphRunDrawing {
30    ferro_property!(
31        /// Defines the `Foreground` property.
32        pub fn foreground_property() -> StyledProperty<Option<Rc<dyn IBrush>>> {
33            FerroProperty::register::<GlyphRunDrawing, _>("Foreground", None)
34        }
35    );
36
37    ferro_property!(
38        /// Defines the `GlyphRun` property.
39        pub fn glyph_run_property() -> StyledProperty<Option<Rc<GlyphRun>>> {
40            FerroProperty::register::<GlyphRunDrawing, _>("GlyphRun", None)
41        }
42    );
43} }
44
45impl GlyphRunDrawing {
46    /// Creates the class data.
47    pub fn construct() -> Self {
48        Self { base: Drawing::construct() }
49    }
50
51    pub fn new() -> Ref<Self> {
52        instantiate(Self::construct())
53    }
54
55    /// The brush the glyph run is drawn with.
56    pub fn foreground(&self) -> Option<Rc<dyn IBrush>> {
57        self.get_value(Self::foreground_property())
58    }
59
60    pub fn set_foreground(&self, value: Option<Rc<dyn IBrush>>) {
61        self.set_value(Self::foreground_property(), value)
62    }
63
64    /// The glyph run that is drawn.
65    pub fn glyph_run(&self) -> Option<Rc<GlyphRun>> {
66        self.get_value(Self::glyph_run_property())
67    }
68
69    pub fn set_glyph_run(&self, value: Option<Rc<GlyphRun>>) {
70        self.set_value(Self::glyph_run_property(), value)
71    }
72}