skia_safe/modules/svg/types/
font.rs1use std::fmt;
2
3use super::Length;
4use crate::{interop, prelude::*};
5use skia_bindings as sb;
6
7pub type FontStyle = sb::SkSVGFontStyle_Type;
8variant_name!(FontStyle::Normal);
9pub type FontWeight = sb::SkSVGFontWeight_Type;
10variant_name!(FontWeight::Lighter);
11
12#[repr(C)]
13#[derive(Copy, Clone)]
14pub struct FontSize {
15 ty: sb::SkSVGFontSize_Type,
16 size: sb::SkSVGLength,
17}
18
19native_transmutable!(sb::SkSVGFontSize, FontSize, svg_font_size_layout);
20
21impl fmt::Debug for FontSize {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 f.debug_struct("SvgFontSize")
24 .field("size", &self.size())
25 .finish()
26 }
27}
28
29impl FontSize {
30 pub fn size(&self) -> Option<&Length> {
31 if self.ty == sb::SkSVGFontSize_Type::Length {
32 Some(Length::from_native_ref(&self.size))
33 } else {
34 None
35 }
36 }
37
38 pub fn inherit() -> Self {
39 Self {
40 ty: sb::SkSVGFontSize_Type::Inherit,
41 size: sb::SkSVGLength {
42 fValue: 0.0,
43 fUnit: sb::SkSVGLength_Unit::Unknown,
44 },
45 }
46 }
47
48 pub fn new(size: Length) -> Self {
49 Self {
50 ty: sb::SkSVGFontSize_Type::Length,
51 size: size.into_native(),
52 }
53 }
54}
55
56#[repr(C)]
57#[derive(Clone)]
58pub struct FontFamily {
59 ty: sb::SkSVGFontFamily_Type,
60 family: interop::String,
61}
62
63impl fmt::Debug for FontFamily {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 f.debug_struct("SvgFontFamily")
66 .field("family", &self.family())
67 .finish()
68 }
69}
70
71impl FontFamily {
72 pub fn family(&self) -> Option<&str> {
73 if self.ty == sb::SkSVGFontFamily_Type::Family {
74 Some(self.family.as_str())
75 } else {
76 None
77 }
78 }
79
80 pub fn inherit() -> Self {
81 Self {
82 ty: sb::SkSVGFontFamily_Type::Inherit,
83 family: interop::String::default(),
84 }
85 }
86
87 pub fn new<T: AsRef<str>>(family: T) -> Self {
88 Self {
89 ty: sb::SkSVGFontFamily_Type::Family,
90 family: interop::String::from_str(family.as_ref()),
91 }
92 }
93}
94
95native_transmutable!(sb::SkSVGFontFamily, FontFamily, svg_font_family_layout);