cn_font_split/pre_subset/
fvar.rs1use opentype::tables::FontVariations;
2use opentype::truetype::q32;
3use opentype::Font;
4use std::io::Cursor;
5
6#[derive(Default, Clone)]
7pub struct FvarTable {
8 pub vf_weight: String,
9 pub vf_default_weight: String,
10}
11pub fn analyze_fvar_table(
13 font: &Font,
14 font_file: &mut Cursor<&Vec<u8>>,
15) -> Option<FvarTable> {
16 let data: Option<FontVariations> = font.take(font_file).unwrap();
17 if data.is_none() {
18 return None;
19 }
20
21 let mut vf = FvarTable::default();
22 data.unwrap().axis_records.iter().for_each(|x| {
23 let tag = x.tag.as_str().unwrap_or("");
24 if tag == "wght".to_string() {
25 let max = q32_to_int_truncate(x.max_value);
26 let min = q32_to_int_truncate(x.min_value);
27 let font_weight = format!("{} {}", min, max).clone();
28 vf.vf_weight = font_weight;
29 vf.vf_default_weight =
30 q32_to_int_truncate(x.default_value).to_string();
31 }
32 });
33 Some(vf)
34}
35pub fn q32_to_int_truncate(input: q32) -> i32 {
36 input.0 >> 16
37}
38#[test]
39fn test_fvar_table() {
40 use cn_font_utils::read_binary_file;
41 let path = "./packages/demo/public/WorkSans-VariableFont_wght.ttf";
42 let file_binary = read_binary_file(&path).expect("Failed to read file");
43 let mut font_file = Cursor::new(&file_binary);
44 let font = Font::read(&mut font_file).expect("TODO: panic message");
45 let data = analyze_fvar_table(&font, &mut font_file).unwrap();
46
47 assert_eq!(data.vf_default_weight, "400");
48 assert_eq!(data.vf_weight, "100 900")
49 }