makepad_draw/text/
loader.rs1use {
2 super::{
3 font::{Font, FontId},
5 font_face::FontFace,
6 font_family::{FontFamily, FontFamilyId},
7 rasterizer,
8 rasterizer::Rasterizer,
9 shaper,
10 shaper::Shaper,
11 },
12 std::{cell::RefCell, collections::HashMap, rc::Rc},
13};
14
15#[derive(Clone, Debug)]
16pub struct Loader {
17 shaper: Rc<RefCell<Shaper>>,
18 rasterizer: Rc<RefCell<rasterizer::Rasterizer>>,
19 font_family_definitions: HashMap<FontFamilyId, FontFamilyDefinition>,
20 font_definitions: HashMap<FontId, FontDefinition>,
21 font_family_cache: HashMap<FontFamilyId, Rc<FontFamily>>,
22 font_cache: HashMap<FontId, Rc<Font>>,
23}
24
25impl Loader {
26 pub fn new(settings: Settings) -> Self {
27 let loader = Self {
28 shaper: Rc::new(RefCell::new(Shaper::new(settings.shaper))),
29 rasterizer: Rc::new(RefCell::new(Rasterizer::new(settings.rasterizer))),
30 font_family_definitions: HashMap::new(),
31 font_definitions: HashMap::new(),
32 font_family_cache: HashMap::new(),
33 font_cache: HashMap::new(),
34 };
35 loader
37 }
38
39 pub fn rasterizer(&self) -> &Rc<RefCell<Rasterizer>> {
40 &self.rasterizer
41 }
42
43 pub fn is_font_family_known(&self, id: FontFamilyId) -> bool {
44 if self.font_family_definitions.contains_key(&id) {
45 return true;
46 }
47 if self.font_family_cache.contains_key(&id) {
48 return true;
49 }
50 false
51 }
52
53 pub fn is_font_known(&self, id: FontId) -> bool {
54 if self.font_definitions.contains_key(&id) {
55 return true;
56 }
57 if self.font_cache.contains_key(&id) {
58 return true;
59 }
60 false
61 }
62
63 pub fn define_font_family(&mut self, id: FontFamilyId, definition: FontFamilyDefinition) {
64 debug_assert!(
65 !self.is_font_family_known(id),
66 "can't redefine a font family that is already known"
67 );
68 self.font_family_definitions.insert(id, definition);
69 }
70
71 pub fn define_font(&mut self, id: FontId, definition: FontDefinition) {
72 debug_assert!(
73 !self.is_font_known(id),
74 "can't redefine a font that is already known"
75 );
76 self.font_definitions.insert(id, definition);
77 }
78
79 pub fn get_or_load_font_family(&mut self, id: FontFamilyId) -> &Rc<FontFamily> {
80 if !self.font_family_cache.contains_key(&id) {
81 let font_family = self.load_font_family(id);
82 self.font_family_cache.insert(id, Rc::new(font_family));
83 }
84 self.font_family_cache.get(&id).unwrap()
85 }
86
87 fn load_font_family(&mut self, id: FontFamilyId) -> FontFamily {
88 let definition = self
89 .font_family_definitions
90 .remove(&id)
91 .unwrap_or_else(|| panic!("font family {:?} is not defined", id));
92 FontFamily::new(
93 id,
94 self.shaper.clone(),
95 definition
96 .font_ids
97 .into_iter()
98 .map(|font_id| self.get_or_load_font(font_id).clone())
99 .collect(),
100 )
101 }
102
103 pub fn get_or_load_font(&mut self, id: FontId) -> &Rc<Font> {
104 if !self.font_cache.contains_key(&id) {
105 let font = self.load_font(id);
106 self.font_cache.insert(id, Rc::new(font));
107 }
108 self.font_cache.get(&id).unwrap()
109 }
110
111 fn load_font(&mut self, id: FontId) -> Font {
112 let definition = self
113 .font_definitions
114 .remove(&id)
115 .expect("font is not defined");
116 Font::new(
117 id.clone(),
118 self.rasterizer.clone(),
119 FontFace::from_data_and_index(definition.data, definition.index)
120 .expect("failed to load font from definition"),
121 definition.ascender_fudge_in_ems,
122 definition.descender_fudge_in_ems,
123 )
124 }
125}
126
127#[derive(Clone, Copy, Debug)]
128pub struct Settings {
129 pub shaper: shaper::Settings,
130 pub rasterizer: rasterizer::Settings,
131}
132
133#[derive(Clone, Debug)]
134pub struct FontFamilyDefinition {
135 pub font_ids: Vec<FontId>,
136}
137
138#[derive(Clone, Debug)]
139pub struct FontDefinition {
140 pub data: Rc<Vec<u8>>,
141 pub index: u32,
142 pub ascender_fudge_in_ems: f32,
143 pub descender_fudge_in_ems: f32,
144}