1 | use crate::{prelude::*, FontMgr}; |
2 | use skia_bindings::{self as sb, SkOrderedFontMgr, SkRefCntBase}; |
3 | use std::{ |
4 | fmt, |
5 | mem::transmute, |
6 | ops::{Deref, DerefMut}, |
7 | }; |
8 | |
9 | pub type OrderedFontMgr = RCHandle<SkOrderedFontMgr>; |
10 | require_base_type!(SkOrderedFontMgr, sb::SkFontMgr); |
11 | |
12 | impl NativeRefCountedBase for SkOrderedFontMgr { |
13 | type Base = SkRefCntBase; |
14 | } |
15 | |
16 | impl Deref for OrderedFontMgr { |
17 | type Target = FontMgr; |
18 | fn deref(&self) -> &Self::Target { |
19 | unsafe { transmute_ref(self) } |
20 | } |
21 | } |
22 | |
23 | impl DerefMut for OrderedFontMgr { |
24 | fn deref_mut(&mut self) -> &mut Self::Target { |
25 | unsafe { transmute_ref_mut(self) } |
26 | } |
27 | } |
28 | |
29 | impl Default for OrderedFontMgr { |
30 | fn default() -> Self { |
31 | Self::new() |
32 | } |
33 | } |
34 | |
35 | impl From<OrderedFontMgr> for FontMgr { |
36 | fn from(font_mgr: OrderedFontMgr) -> Self { |
37 | unsafe { transmute(src:font_mgr) } |
38 | } |
39 | } |
40 | |
41 | impl fmt::Debug for OrderedFontMgr { |
42 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
43 | f&mut DebugStruct<'_, '_>.debug_struct("OrderedFontMgr" ) |
44 | .field(name:"base" , self as &FontMgr) |
45 | .finish() |
46 | } |
47 | } |
48 | |
49 | impl OrderedFontMgr { |
50 | pub fn new() -> Self { |
51 | Self::from_ptr(unsafe { sb::C_SkOrderedFontMgr_new() }).unwrap() |
52 | } |
53 | |
54 | pub fn append(&mut self, font_mgr: impl Into<FontMgr>) { |
55 | let font_mgr = font_mgr.into(); |
56 | unsafe { sb::C_SkOrderedFontMgr_append(self.native_mut(), fontMgr:font_mgr.into_ptr()) } |
57 | } |
58 | } |
59 | |
60 | #[cfg (test)] |
61 | mod tests { |
62 | use super::OrderedFontMgr; |
63 | |
64 | #[test ] |
65 | fn can_use_font_mgr_functions() { |
66 | let ordered = OrderedFontMgr::default(); |
67 | let _families = ordered.count_families(); |
68 | } |
69 | |
70 | #[test ] |
71 | fn can_pass_ordered_font_mgr_where_a_font_mgr_is_expected() { |
72 | let mut ordered = OrderedFontMgr::default(); |
73 | let another = OrderedFontMgr::default(); |
74 | ordered.append(another); |
75 | } |
76 | } |
77 | |