skia_safe/modules/svg/types/
font.rs

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::fmt;

use super::Length;
use crate::{interop, prelude::*};
use skia_bindings as sb;

pub type FontStyle = sb::SkSVGFontStyle_Type;
variant_name!(FontStyle::Normal);
pub type FontWeight = sb::SkSVGFontWeight_Type;
variant_name!(FontWeight::Lighter);

#[repr(C)]
#[derive(Copy, Clone)]
pub struct FontSize {
    ty: sb::SkSVGFontSize_Type,
    size: sb::SkSVGLength,
}

native_transmutable!(sb::SkSVGFontSize, FontSize, svg_font_size_layout);

impl fmt::Debug for FontSize {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SvgFontSize")
            .field("size", &self.size())
            .finish()
    }
}

impl FontSize {
    pub fn size(&self) -> Option<&Length> {
        if self.ty == sb::SkSVGFontSize_Type::Length {
            Some(Length::from_native_ref(&self.size))
        } else {
            None
        }
    }

    pub fn inherit() -> Self {
        Self {
            ty: sb::SkSVGFontSize_Type::Inherit,
            size: sb::SkSVGLength {
                fValue: 0.0,
                fUnit: sb::SkSVGLength_Unit::Unknown,
            },
        }
    }

    pub fn new(size: Length) -> Self {
        Self {
            ty: sb::SkSVGFontSize_Type::Length,
            size: size.into_native(),
        }
    }
}

#[repr(C)]
#[derive(Clone)]
pub struct FontFamily {
    ty: sb::SkSVGFontFamily_Type,
    family: interop::String,
}

impl fmt::Debug for FontFamily {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SvgFontFamily")
            .field("family", &self.family())
            .finish()
    }
}

impl FontFamily {
    pub fn family(&self) -> Option<&str> {
        if self.ty == sb::SkSVGFontFamily_Type::Family {
            Some(self.family.as_str())
        } else {
            None
        }
    }

    pub fn inherit() -> Self {
        Self {
            ty: sb::SkSVGFontFamily_Type::Inherit,
            family: interop::String::default(),
        }
    }

    pub fn new<T: AsRef<str>>(family: T) -> Self {
        Self {
            ty: sb::SkSVGFontFamily_Type::Family,
            family: interop::String::from_str(family.as_ref()),
        }
    }
}

native_transmutable!(sb::SkSVGFontFamily, FontFamily, svg_font_family_layout);