ab_glyph/ttfp/
variable.rs

1use crate::{FontRef, FontVec, VariableFont, VariationAxis};
2#[cfg(not(feature = "std"))]
3use alloc::vec::Vec;
4use owned_ttf_parser::{self as ttfp, AsFaceRef, FaceMut};
5
6impl VariableFont for FontRef<'_> {
7    fn set_variation(&mut self, axis: &[u8; 4], value: f32) -> bool {
8        let tag = ttfp::Tag::from_bytes(axis);
9        // TODO remove existence check in next breaking version
10        let exists = self
11            .0
12            .as_face_ref()
13            .variation_axes()
14            .into_iter()
15            .any(|axis| axis.tag == tag);
16        if exists {
17            self.0.set_variation(tag, value);
18        }
19        exists
20    }
21
22    fn variations(&self) -> Vec<VariationAxis> {
23        variations(self.0.as_face_ref())
24    }
25}
26
27impl VariableFont for FontVec {
28    fn set_variation(&mut self, axis: &[u8; 4], value: f32) -> bool {
29        self.0
30            .set_variation(ttfp::Tag::from_bytes(axis), value)
31            .is_some()
32    }
33
34    fn variations(&self) -> Vec<VariationAxis> {
35        variations(self.0.as_face_ref())
36    }
37}
38
39fn variations(face: &ttfp::Face<'_>) -> Vec<VariationAxis> {
40    face.variation_axes()
41        .into_iter()
42        .map(|axis| {
43            #[cfg(feature = "std")]
44            let name = face.names().into_iter().find_map(|n| {
45                if n.name_id == axis.name_id {
46                    n.to_string()
47                } else {
48                    None
49                }
50            });
51            #[cfg(not(feature = "std"))]
52            let name = None;
53            VariationAxis {
54                tag: axis.tag.to_bytes(),
55                name,
56                min_value: axis.min_value,
57                default_value: axis.def_value,
58                max_value: axis.max_value,
59                hidden: axis.hidden,
60            }
61        })
62        .collect()
63}