makepad_draw/text/
font_family.rs

1use {
2    super::{
3        font::Font,
4        intern::Intern,
5        shaper::{ShapeParams, ShapedText, Shaper},
6        substr::Substr,
7    },
8    std::{
9        cell::RefCell,
10        hash::{Hash, Hasher},
11        rc::Rc,
12    },
13};
14
15#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
16pub struct FontFamilyId(u64);
17
18impl From<u64> for FontFamilyId {
19    fn from(value: u64) -> Self {
20        Self(value)
21    }
22}
23
24impl From<&str> for FontFamilyId {
25    fn from(value: &str) -> Self {
26        Self(value.intern().as_ptr() as u64)
27    }
28}
29
30#[derive(Debug)]
31pub struct FontFamily {
32    id: FontFamilyId,
33    shaper: Rc<RefCell<Shaper>>,
34    fonts: Rc<[Rc<Font>]>,
35}
36
37impl FontFamily {
38    pub fn new(id: FontFamilyId, shaper: Rc<RefCell<Shaper>>, fonts: Rc<[Rc<Font>]>) -> Self {
39        Self { id, shaper, fonts }
40    }
41
42    pub fn id(&self) -> FontFamilyId {
43        self.id
44    }
45
46    pub fn get_or_shape(&self, text: Substr) -> Rc<ShapedText> {
47        self.shaper.borrow_mut().get_or_shape(ShapeParams {
48            text: text.into(),
49            fonts: self.fonts.clone(),
50        })
51    }
52
53    pub fn fonts(&self) -> &[Rc<Font>] {
54        &self.fonts
55    }
56}
57
58impl Eq for FontFamily {}
59
60impl Hash for FontFamily {
61    fn hash<H: Hasher>(&self, state: &mut H) {
62        self.id.hash(state);
63    }
64}
65
66impl PartialEq for FontFamily {
67    fn eq(&self, other: &Self) -> bool {
68        self.id() == other.id()
69    }
70}