1use crate::{px, Bounds, LineWrapper, Pixels, PlatformTextSystem, Result, Size};
2use anyhow::anyhow;
3pub use font_kit::properties::{
4 Properties as FontProperties, Stretch as FontStretch, Style as FontStyle, Weight as FontWeight,
5};
6use parking_lot::{RwLock, RwLockUpgradableReadGuard};
7use schemars::JsonSchema;
8use serde::{Deserialize, Serialize};
9use std::{
10 collections::HashMap,
11 ops::{Deref, DerefMut},
12 sync::Arc,
13};
14
15#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
16pub struct FontFamilyId(usize);
17
18#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
19pub struct FontId(pub usize);
20
21#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize, JsonSchema)]
22pub struct FontFeatures {
23 pub calt: Option<bool>,
24 pub case: Option<bool>,
25 pub cpsp: Option<bool>,
26 pub frac: Option<bool>,
27 pub liga: Option<bool>,
28 pub onum: Option<bool>,
29 pub ordn: Option<bool>,
30 pub pnum: Option<bool>,
31 pub ss01: Option<bool>,
32 pub ss02: Option<bool>,
33 pub ss03: Option<bool>,
34 pub ss04: Option<bool>,
35 pub ss05: Option<bool>,
36 pub ss06: Option<bool>,
37 pub ss07: Option<bool>,
38 pub ss08: Option<bool>,
39 pub ss09: Option<bool>,
40 pub ss10: Option<bool>,
41 pub ss11: Option<bool>,
42 pub ss12: Option<bool>,
43 pub ss13: Option<bool>,
44 pub ss14: Option<bool>,
45 pub ss15: Option<bool>,
46 pub ss16: Option<bool>,
47 pub ss17: Option<bool>,
48 pub ss18: Option<bool>,
49 pub ss19: Option<bool>,
50 pub ss20: Option<bool>,
51 pub subs: Option<bool>,
52 pub sups: Option<bool>,
53 pub swsh: Option<bool>,
54 pub titl: Option<bool>,
55 pub tnum: Option<bool>,
56 pub zero: Option<bool>,
57}
58
59#[allow(non_camel_case_types)]
60#[derive(Deserialize)]
61enum WeightJson {
62 thin,
63 extra_light,
64 light,
65 normal,
66 medium,
67 semibold,
68 bold,
69 extra_bold,
70 black,
71}
72
73struct Family {
74 name: Arc<str>,
75 font_features: FontFeatures,
76 font_ids: Vec<FontId>,
77}
78
79pub struct FontCache(RwLock<FontCacheState>);
80
81pub struct FontCacheState {
82 font_system: Arc<dyn PlatformTextSystem>,
83 families: Vec<Family>,
84 default_family: Option<FontFamilyId>,
85 font_selections: HashMap<FontFamilyId, HashMap<(FontWeight, FontStyle), FontId>>,
86 metrics: HashMap<FontId, FontMetrics>,
87 wrapper_pool: HashMap<(FontId, Pixels), Vec<LineWrapper>>,
88}
89
90unsafe impl Send for FontCache {}
91
92impl FontCache {
93 pub fn new(fonts: Arc<dyn PlatformTextSystem>) -> Self {
94 Self(RwLock::new(FontCacheState {
95 font_system: fonts,
96 families: Default::default(),
97 default_family: None,
98 font_selections: Default::default(),
99 metrics: Default::default(),
100 wrapper_pool: Default::default(),
101 }))
102 }
103
104 pub fn family_name(&self, family_id: FontFamilyId) -> Result<Arc<str>> {
105 self.0
106 .read()
107 .families
108 .get(family_id.0)
109 .ok_or_else(|| anyhow!("invalid family id"))
110 .map(|family| family.name.clone())
111 }
112
113 pub fn load_family(&self, names: &[&str], features: &FontFeatures) -> Result<FontFamilyId> {
114 for name in names {
115 let state = self.0.upgradable_read();
116
117 if let Some(ix) = state
118 .families
119 .iter()
120 .position(|f| f.name.as_ref() == *name && f.font_features == *features)
121 {
122 return Ok(FontFamilyId(ix));
123 }
124
125 let mut state = RwLockUpgradableReadGuard::upgrade(state);
126
127 if let Ok(font_ids) = state.font_system.load_family(name, features) {
128 if font_ids.is_empty() {
129 continue;
130 }
131
132 let family_id = FontFamilyId(state.families.len());
133 for font_id in &font_ids {
134 if state.font_system.glyph_for_char(*font_id, 'm').is_none() {
135 return Err(anyhow!("font must contain a glyph for the 'm' character"));
136 }
137 }
138
139 state.families.push(Family {
140 name: Arc::from(*name),
141 font_features: features.clone(),
142 font_ids,
143 });
144 return Ok(family_id);
145 }
146 }
147
148 Err(anyhow!(
149 "could not find a non-empty font family matching one of the given names"
150 ))
151 }
152
153 /// Returns an arbitrary font family that is available on the system.
154 pub fn known_existing_family(&self) -> FontFamilyId {
155 if let Some(family_id) = self.0.read().default_family {
156 return family_id;
157 }
158
159 let default_family = self
160 .load_family(
161 &["Courier", "Helvetica", "Arial", "Verdana"],
162 &Default::default(),
163 )
164 .unwrap_or_else(|_| {
165 let all_family_names = self.0.read().font_system.all_families();
166 let all_family_names: Vec<_> = all_family_names
167 .iter()
168 .map(|string| string.as_str())
169 .collect();
170 self.load_family(&all_family_names, &Default::default())
171 .expect("could not load any default font family")
172 });
173
174 self.0.write().default_family = Some(default_family);
175 default_family
176 }
177
178 pub fn default_font(&self, family_id: FontFamilyId) -> FontId {
179 self.select_font(family_id, Default::default(), Default::default())
180 .unwrap()
181 }
182
183 pub fn select_font(
184 &self,
185 family_id: FontFamilyId,
186 weight: FontWeight,
187 style: FontStyle,
188 ) -> Result<FontId> {
189 let inner = self.0.upgradable_read();
190 if let Some(font_id) = inner
191 .font_selections
192 .get(&family_id)
193 .and_then(|fonts| fonts.get(&(weight, style)))
194 {
195 Ok(*font_id)
196 } else {
197 let mut inner = RwLockUpgradableReadGuard::upgrade(inner);
198 let family = &inner.families[family_id.0];
199 let font_id = inner
200 .font_system
201 .select_font(&family.font_ids, weight, style)
202 .unwrap_or(family.font_ids[0]);
203 inner
204 .font_selections
205 .entry(family_id)
206 .or_default()
207 .insert((weight, style), font_id);
208 Ok(font_id)
209 }
210 }
211
212 pub fn metric<F, T>(&self, font_id: FontId, f: F) -> T
213 where
214 F: FnOnce(&FontMetrics) -> T,
215 T: 'static,
216 {
217 let state = self.0.upgradable_read();
218 if let Some(metrics) = state.metrics.get(&font_id) {
219 f(metrics)
220 } else {
221 let metrics = state.font_system.font_metrics(font_id);
222 let metric = f(&metrics);
223 let mut state = RwLockUpgradableReadGuard::upgrade(state);
224 state.metrics.insert(font_id, metrics);
225 metric
226 }
227 }
228
229 pub fn bounding_box(&self, font_id: FontId, font_size: Pixels) -> Size<Pixels> {
230 let bounding_box = self.metric(font_id, |m| m.bounding_box);
231
232 let width = px(bounding_box.size.width) * self.em_size(font_id, font_size);
233 let height = px(bounding_box.size.height) * self.em_size(font_id, font_size);
234 Size { width, height }
235 }
236
237 pub fn em_width(&self, font_id: FontId, font_size: Pixels) -> Pixels {
238 let glyph_id;
239 let bounds;
240 {
241 let state = self.0.read();
242 glyph_id = state.font_system.glyph_for_char(font_id, 'm').unwrap();
243 bounds = state
244 .font_system
245 .typographic_bounds(font_id, glyph_id)
246 .unwrap();
247 }
248 self.em_size(font_id, font_size) * bounds.size.width
249 }
250
251 pub fn em_advance(&self, font_id: FontId, font_size: Pixels) -> Pixels {
252 let glyph_id;
253 let advance;
254 {
255 let state = self.0.read();
256 glyph_id = state.font_system.glyph_for_char(font_id, 'm').unwrap();
257 advance = state.font_system.advance(font_id, glyph_id).unwrap();
258 }
259 self.em_size(font_id, font_size) * advance.width
260 }
261
262 pub fn line_height(&self, font_size: Pixels) -> Pixels {
263 (font_size * 1.618).round()
264 }
265
266 pub fn cap_height(&self, font_id: FontId, font_size: Pixels) -> Pixels {
267 self.em_size(font_id, font_size) * self.metric(font_id, |m| m.cap_height)
268 }
269
270 pub fn x_height(&self, font_id: FontId, font_size: Pixels) -> Pixels {
271 self.em_size(font_id, font_size) * self.metric(font_id, |m| m.x_height)
272 }
273
274 pub fn ascent(&self, font_id: FontId, font_size: Pixels) -> Pixels {
275 self.em_size(font_id, font_size) * self.metric(font_id, |m| m.ascent)
276 }
277
278 pub fn descent(&self, font_id: FontId, font_size: Pixels) -> Pixels {
279 self.em_size(font_id, font_size) * self.metric(font_id, |m| -m.descent)
280 }
281
282 pub fn em_size(&self, font_id: FontId, font_size: Pixels) -> Pixels {
283 font_size / self.metric(font_id, |m| m.units_per_em as f32)
284 }
285
286 pub fn baseline_offset(&self, font_id: FontId, font_size: Pixels) -> Pixels {
287 let line_height = self.line_height(font_size);
288 let ascent = self.ascent(font_id, font_size);
289 let descent = self.descent(font_id, font_size);
290 let padding_top = (line_height - ascent - descent) / 2.;
291 padding_top + ascent
292 }
293
294 pub fn line_wrapper(self: &Arc<Self>, font_id: FontId, font_size: Pixels) -> LineWrapperHandle {
295 let mut state = self.0.write();
296 let wrappers = state.wrapper_pool.entry((font_id, font_size)).or_default();
297 let wrapper = wrappers
298 .pop()
299 .unwrap_or_else(|| LineWrapper::new(font_id, font_size, state.font_system.clone()));
300 LineWrapperHandle {
301 wrapper: Some(wrapper),
302 font_cache: self.clone(),
303 }
304 }
305}
306
307pub struct LineWrapperHandle {
308 wrapper: Option<LineWrapper>,
309 font_cache: Arc<FontCache>,
310}
311
312impl Drop for LineWrapperHandle {
313 fn drop(&mut self) {
314 let mut state = self.font_cache.0.write();
315 let wrapper = self.wrapper.take().unwrap();
316 state
317 .wrapper_pool
318 .get_mut(&(wrapper.font_id, wrapper.font_size))
319 .unwrap()
320 .push(wrapper);
321 }
322}
323
324impl Deref for LineWrapperHandle {
325 type Target = LineWrapper;
326
327 fn deref(&self) -> &Self::Target {
328 self.wrapper.as_ref().unwrap()
329 }
330}
331
332impl DerefMut for LineWrapperHandle {
333 fn deref_mut(&mut self) -> &mut Self::Target {
334 self.wrapper.as_mut().unwrap()
335 }
336}
337
338#[derive(Clone, Copy, Debug)]
339pub struct FontMetrics {
340 pub units_per_em: u32,
341 pub ascent: f32,
342 pub descent: f32,
343 pub line_gap: f32,
344 pub underline_position: f32,
345 pub underline_thickness: f32,
346 pub cap_height: f32,
347 pub x_height: f32,
348 pub bounding_box: Bounds<f32>,
349}
350
351#[cfg(test)]
352mod tests {
353 use super::*;
354 use crate::{FontStyle, FontWeight, Platform, TestPlatform};
355
356 #[test]
357 fn test_select_font() {
358 let platform = TestPlatform::new();
359 let fonts = FontCache::new(platform.text_system());
360 let arial = fonts
361 .load_family(
362 &["Arial"],
363 &FontFeatures {
364 calt: Some(false),
365 ..Default::default()
366 },
367 )
368 .unwrap();
369 let arial_regular = fonts
370 .select_font(arial, FontWeight::default(), FontStyle::default())
371 .unwrap();
372 let arial_italic = fonts
373 .select_font(arial, FontWeight::default(), FontStyle::Italic)
374 .unwrap();
375 let arial_bold = fonts
376 .select_font(arial, FontWeight::BOLD, FontStyle::default())
377 .unwrap();
378 assert_ne!(arial_regular, arial_italic);
379 assert_ne!(arial_regular, arial_bold);
380 assert_ne!(arial_italic, arial_bold);
381
382 let arial_with_calt = fonts
383 .load_family(
384 &["Arial"],
385 &FontFeatures {
386 calt: Some(true),
387 ..Default::default()
388 },
389 )
390 .unwrap();
391 assert_ne!(arial_with_calt, arial);
392 }
393}