skia_safe/modules/svg/types/
paint.rs

1use super::color::Fill;
2use crate::{prelude::*, Color};
3use skia_bindings as sb;
4use std::fmt;
5
6pub type Paint = Handle<sb::SkSVGPaint>;
7
8impl NativeDrop for sb::SkSVGPaint {
9    fn drop(&mut self) {
10        unsafe { sb::C_SkSVGPaint_destruct(self) }
11    }
12}
13
14impl fmt::Debug for Paint {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        if self.is_color() {
17            f.debug_tuple("SvgPaint::Color")
18                .field(&self.color().unwrap())
19                .finish()
20        } else if self.is_current_color() {
21            f.debug_tuple("SvgPaint::CurrentColor").finish()
22        } else {
23            f.debug_tuple("SvgPaint::None").finish()
24        }
25    }
26}
27
28impl Paint {
29    pub fn color(&self) -> Option<Color> {
30        let paint = self.native();
31
32        if self.is_color() {
33            Some(Color::new(paint.fColor.fColor))
34        } else {
35            None
36        }
37    }
38
39    pub fn is_current_color(&self) -> bool {
40        matches!(self.native().fType, sb::SkSVGPaint_Type::Color)
41            && matches!(
42                self.native().fColor.fType,
43                sb::SkSVGColor_Type::CurrentColor
44            )
45    }
46
47    pub fn is_color(&self) -> bool {
48        matches!(self.native().fType, sb::SkSVGPaint_Type::Color)
49            && matches!(self.native().fColor.fType, sb::SkSVGColor_Type::Color)
50    }
51
52    pub fn is_none(&self) -> bool {
53        matches!(self.native().fType, sb::SkSVGPaint_Type::None)
54    }
55
56    pub fn from_color(color: Color) -> Self {
57        Self::construct(|uninitialized| unsafe {
58            let color = Fill::from_color(color);
59
60            sb::C_SkSVGPaint_Construct1(uninitialized, color.native())
61        })
62    }
63
64    pub fn current_color() -> Self {
65        Self::construct(|uninitialized| unsafe {
66            let color = Fill::current_color();
67
68            sb::C_SkSVGPaint_Construct1(uninitialized, color.native())
69        })
70    }
71
72    pub fn none() -> Self {
73        Self::construct(|uninitialized| unsafe { sb::C_SkSVGPaint_Construct(uninitialized) })
74    }
75}