1 | #[cfg (not(feature = "std" ))] |
2 | use alloc::string::String; |
3 | #[cfg (not(feature = "std" ))] |
4 | use alloc::vec::Vec; |
5 | |
6 | /// Logic for variable fonts. |
7 | /// |
8 | /// Requires feature `variable-fonts` (enabled by default). |
9 | pub trait VariableFont { |
10 | /// Sets a variation axis coordinate value by it's tag. |
11 | /// |
12 | /// Returns false if there is no such axis tag. |
13 | /// |
14 | /// # Example |
15 | /// ``` |
16 | /// use ab_glyph::{FontRef, VariableFont}; |
17 | /// |
18 | /// # fn main() -> Result<(), ab_glyph::InvalidFont> { |
19 | /// let mut font = FontRef::try_from_slice(include_bytes!("../../dev/fonts/Cantarell-VF.otf" ))?; |
20 | /// |
21 | /// // set weight to 600 |
22 | /// assert!(font.set_variation(b"wght" , 600.0)); |
23 | /// |
24 | /// // no such variation tag "foob" so return false |
25 | /// assert!(!font.set_variation(b"foob" , 200.0)); |
26 | /// # Ok(()) } |
27 | /// ``` |
28 | fn set_variation(&mut self, tag: &[u8; 4], value: f32) -> bool; |
29 | |
30 | /// Returns variation axes. |
31 | /// |
32 | /// # Example |
33 | /// ``` |
34 | /// use ab_glyph::{FontRef, VariableFont}; |
35 | /// |
36 | /// # fn main() -> Result<(), ab_glyph::InvalidFont> { |
37 | /// let font = FontRef::try_from_slice(include_bytes!("../../dev/fonts/Cantarell-VF.otf" ))?; |
38 | /// let var = &font.variations()[0]; |
39 | /// # eprintln!("{var:#?}" ); |
40 | /// |
41 | /// assert_eq!(var.tag, *b"wght" ); |
42 | /// assert_eq!(var.name.as_deref(), Some("Weight" )); |
43 | /// assert!((var.min_value - 100.0).abs() < f32::EPSILON); |
44 | /// assert!((var.default_value - 400.0).abs() < f32::EPSILON); |
45 | /// assert!((var.max_value - 800.0).abs() < f32::EPSILON); |
46 | /// assert!(!var.hidden); |
47 | /// # Ok(()) } |
48 | /// ``` |
49 | fn variations(&self) -> Vec<VariationAxis>; |
50 | } |
51 | |
52 | #[non_exhaustive ] |
53 | #[derive (Debug, Clone)] |
54 | pub struct VariationAxis { |
55 | /// Tag identifying the design variation for the axis. |
56 | pub tag: [u8; 4], |
57 | /// Unicode name. |
58 | pub name: Option<String>, |
59 | /// The minimum coordinate value for the axis. |
60 | pub min_value: f32, |
61 | /// The default coordinate value for the axis. |
62 | pub default_value: f32, |
63 | /// The maximum coordinate value for the axis. |
64 | pub max_value: f32, |
65 | /// Whether the axis should be exposed directly in user interfaces. |
66 | pub hidden: bool, |
67 | } |
68 | |