font_cache.rs

  1use crate::{
  2    fonts::{Features, FontId, Metrics, Properties},
  3    geometry::vector::{vec2f, Vector2F},
  4    platform,
  5    text_layout::LineWrapper,
  6};
  7use anyhow::{anyhow, Result};
  8use ordered_float::OrderedFloat;
  9use parking_lot::{RwLock, RwLockUpgradableReadGuard};
 10use std::{
 11    collections::HashMap,
 12    ops::{Deref, DerefMut},
 13    sync::Arc,
 14};
 15
 16#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
 17pub struct FamilyId(usize);
 18
 19struct Family {
 20    name: Arc<str>,
 21    font_features: Features,
 22    font_ids: Vec<FontId>,
 23}
 24
 25pub struct FontCache(RwLock<FontCacheState>);
 26
 27pub struct FontCacheState {
 28    font_system: Arc<dyn platform::FontSystem>,
 29    families: Vec<Family>,
 30    default_family: Option<FamilyId>,
 31    font_selections: HashMap<FamilyId, HashMap<Properties, FontId>>,
 32    metrics: HashMap<FontId, Metrics>,
 33    wrapper_pool: HashMap<(FontId, OrderedFloat<f32>), Vec<LineWrapper>>,
 34}
 35
 36pub struct LineWrapperHandle {
 37    wrapper: Option<LineWrapper>,
 38    font_cache: Arc<FontCache>,
 39}
 40
 41unsafe impl Send for FontCache {}
 42
 43impl FontCache {
 44    pub fn new(fonts: Arc<dyn platform::FontSystem>) -> Self {
 45        Self(RwLock::new(FontCacheState {
 46            font_system: fonts,
 47            families: Default::default(),
 48            default_family: None,
 49            font_selections: Default::default(),
 50            metrics: Default::default(),
 51            wrapper_pool: Default::default(),
 52        }))
 53    }
 54
 55    pub fn family_name(&self, family_id: FamilyId) -> Result<Arc<str>> {
 56        self.0
 57            .read()
 58            .families
 59            .get(family_id.0)
 60            .ok_or_else(|| anyhow!("invalid family id"))
 61            .map(|family| family.name.clone())
 62    }
 63
 64    pub fn load_family(&self, names: &[&str], features: &Features) -> Result<FamilyId> {
 65        for name in names {
 66            let state = self.0.upgradable_read();
 67
 68            if let Some(ix) = state
 69                .families
 70                .iter()
 71                .position(|f| f.name.as_ref() == *name && f.font_features == *features)
 72            {
 73                return Ok(FamilyId(ix));
 74            }
 75
 76            let mut state = RwLockUpgradableReadGuard::upgrade(state);
 77
 78            if let Ok(font_ids) = state.font_system.load_family(name, features) {
 79                if font_ids.is_empty() {
 80                    continue;
 81                }
 82
 83                let family_id = FamilyId(state.families.len());
 84                for font_id in &font_ids {
 85                    if state.font_system.glyph_for_char(*font_id, 'm').is_none() {
 86                        return Err(anyhow!("font must contain a glyph for the 'm' character"));
 87                    }
 88                }
 89
 90                state.families.push(Family {
 91                    name: Arc::from(*name),
 92                    font_features: features.clone(),
 93                    font_ids,
 94                });
 95                return Ok(family_id);
 96            }
 97        }
 98
 99        Err(anyhow!(
100            "could not find a non-empty font family matching one of the given names"
101        ))
102    }
103
104    /// Returns an arbitrary font family that is available on the system.
105    pub fn known_existing_family(&self) -> FamilyId {
106        if let Some(family_id) = self.0.read().default_family {
107            return family_id;
108        }
109
110        let default_family = self
111            .load_family(
112                &["Courier", "Helvetica", "Arial", "Verdana"],
113                &Default::default(),
114            )
115            .unwrap_or_else(|_| {
116                let all_family_names = self.0.read().font_system.all_families();
117                let all_family_names: Vec<_> = all_family_names
118                    .iter()
119                    .map(|string| string.as_str())
120                    .collect();
121                self.load_family(&all_family_names, &Default::default())
122                    .expect("could not load any default font family")
123            });
124
125        self.0.write().default_family = Some(default_family);
126        default_family
127    }
128
129    pub fn default_font(&self, family_id: FamilyId) -> FontId {
130        self.select_font(family_id, &Properties::default()).unwrap()
131    }
132
133    pub fn select_font(&self, family_id: FamilyId, properties: &Properties) -> Result<FontId> {
134        let inner = self.0.upgradable_read();
135        if let Some(font_id) = inner
136            .font_selections
137            .get(&family_id)
138            .and_then(|f| f.get(properties))
139        {
140            Ok(*font_id)
141        } else {
142            let mut inner = RwLockUpgradableReadGuard::upgrade(inner);
143            let family = &inner.families[family_id.0];
144            let font_id = inner
145                .font_system
146                .select_font(&family.font_ids, properties)
147                .unwrap_or(family.font_ids[0]);
148
149            inner
150                .font_selections
151                .entry(family_id)
152                .or_default()
153                .insert(*properties, font_id);
154            Ok(font_id)
155        }
156    }
157
158    pub fn metric<F, T>(&self, font_id: FontId, f: F) -> T
159    where
160        F: FnOnce(&Metrics) -> T,
161        T: 'static,
162    {
163        let state = self.0.upgradable_read();
164        if let Some(metrics) = state.metrics.get(&font_id) {
165            f(metrics)
166        } else {
167            let metrics = state.font_system.font_metrics(font_id);
168            let metric = f(&metrics);
169            let mut state = RwLockUpgradableReadGuard::upgrade(state);
170            state.metrics.insert(font_id, metrics);
171            metric
172        }
173    }
174
175    pub fn bounding_box(&self, font_id: FontId, font_size: f32) -> Vector2F {
176        let bounding_box = self.metric(font_id, |m| m.bounding_box);
177        let width = bounding_box.width() * self.em_scale(font_id, font_size);
178        let height = bounding_box.height() * self.em_scale(font_id, font_size);
179        vec2f(width, height)
180    }
181
182    pub fn em_width(&self, font_id: FontId, font_size: f32) -> f32 {
183        let glyph_id;
184        let bounds;
185        {
186            let state = self.0.read();
187            glyph_id = state.font_system.glyph_for_char(font_id, 'm').unwrap();
188            bounds = state
189                .font_system
190                .typographic_bounds(font_id, glyph_id)
191                .unwrap();
192        }
193        bounds.width() * self.em_scale(font_id, font_size)
194    }
195
196    pub fn em_advance(&self, font_id: FontId, font_size: f32) -> f32 {
197        let glyph_id;
198        let advance;
199        {
200            let state = self.0.read();
201            glyph_id = state.font_system.glyph_for_char(font_id, 'm').unwrap();
202            advance = state.font_system.advance(font_id, glyph_id).unwrap();
203        }
204        advance.x() * self.em_scale(font_id, font_size)
205    }
206
207    pub fn line_height(&self, font_size: f32) -> f32 {
208        (font_size * 1.618).round()
209    }
210
211    pub fn cap_height(&self, font_id: FontId, font_size: f32) -> f32 {
212        self.metric(font_id, |m| m.cap_height) * self.em_scale(font_id, font_size)
213    }
214
215    pub fn x_height(&self, font_id: FontId, font_size: f32) -> f32 {
216        self.metric(font_id, |m| m.x_height) * self.em_scale(font_id, font_size)
217    }
218
219    pub fn ascent(&self, font_id: FontId, font_size: f32) -> f32 {
220        self.metric(font_id, |m| m.ascent) * self.em_scale(font_id, font_size)
221    }
222
223    pub fn descent(&self, font_id: FontId, font_size: f32) -> f32 {
224        self.metric(font_id, |m| -m.descent) * self.em_scale(font_id, font_size)
225    }
226
227    pub fn em_scale(&self, font_id: FontId, font_size: f32) -> f32 {
228        font_size / self.metric(font_id, |m| m.units_per_em as f32)
229    }
230
231    pub fn baseline_offset(&self, font_id: FontId, font_size: f32) -> f32 {
232        let line_height = self.line_height(font_size);
233        let ascent = self.ascent(font_id, font_size);
234        let descent = self.descent(font_id, font_size);
235        let padding_top = (line_height - ascent - descent) / 2.;
236        padding_top + ascent
237    }
238
239    pub fn line_wrapper(self: &Arc<Self>, font_id: FontId, font_size: f32) -> LineWrapperHandle {
240        let mut state = self.0.write();
241        let wrappers = state
242            .wrapper_pool
243            .entry((font_id, OrderedFloat(font_size)))
244            .or_default();
245        let wrapper = wrappers
246            .pop()
247            .unwrap_or_else(|| LineWrapper::new(font_id, font_size, state.font_system.clone()));
248        LineWrapperHandle {
249            wrapper: Some(wrapper),
250            font_cache: self.clone(),
251        }
252    }
253}
254
255impl Drop for LineWrapperHandle {
256    fn drop(&mut self) {
257        let mut state = self.font_cache.0.write();
258        let wrapper = self.wrapper.take().unwrap();
259        state
260            .wrapper_pool
261            .get_mut(&(wrapper.font_id, OrderedFloat(wrapper.font_size)))
262            .unwrap()
263            .push(wrapper);
264    }
265}
266
267impl Deref for LineWrapperHandle {
268    type Target = LineWrapper;
269
270    fn deref(&self) -> &Self::Target {
271        self.wrapper.as_ref().unwrap()
272    }
273}
274
275impl DerefMut for LineWrapperHandle {
276    fn deref_mut(&mut self) -> &mut Self::Target {
277        self.wrapper.as_mut().unwrap()
278    }
279}
280
281#[cfg(test)]
282mod tests {
283    use super::*;
284    use crate::{
285        fonts::{Style, Weight},
286        platform::{test, Platform as _},
287    };
288
289    #[test]
290    fn test_select_font() {
291        let platform = test::platform();
292        let fonts = FontCache::new(platform.fonts());
293        let arial = fonts
294            .load_family(
295                &["Arial"],
296                &Features {
297                    calt: Some(false),
298                    ..Default::default()
299                },
300            )
301            .unwrap();
302        let arial_regular = fonts.select_font(arial, &Properties::new()).unwrap();
303        let arial_italic = fonts
304            .select_font(arial, Properties::new().style(Style::Italic))
305            .unwrap();
306        let arial_bold = fonts
307            .select_font(arial, Properties::new().weight(Weight::BOLD))
308            .unwrap();
309        assert_ne!(arial_regular, arial_italic);
310        assert_ne!(arial_regular, arial_bold);
311        assert_ne!(arial_italic, arial_bold);
312
313        let arial_with_calt = fonts
314            .load_family(
315                &["Arial"],
316                &Features {
317                    calt: Some(true),
318                    ..Default::default()
319                },
320            )
321            .unwrap();
322        assert_ne!(arial_with_calt, arial);
323    }
324}