| 1 | //! Building the STAT table |
| 2 | |
| 3 | // Largely stolen from fea-rs, where it's all private so we can't use it. |
| 4 | // We're uninterested in anything other than 3/1/0x409, so we use Strings instead |
| 5 | // of the more correct NameSpec. |
| 6 | |
| 7 | use fontations::write::{ |
| 8 | tables::{name::NameRecord, stat as write_stat}, |
| 9 | types::{Fixed, NameId, Tag}, |
| 10 | }; |
| 11 | use std::collections::HashMap; |
| 12 | |
| 13 | use crate::nametable::find_or_add_name; |
| 14 | |
| 15 | #[derive(Clone, Debug)] |
| 16 | pub(crate) struct StatBuilder { |
| 17 | // pub name: StatFallbackName, |
| 18 | pub records: Vec<AxisRecord>, |
| 19 | pub values: Vec<AxisValue>, |
| 20 | } |
| 21 | |
| 22 | #[derive(Clone, Debug)] |
| 23 | pub(crate) struct AxisRecord { |
| 24 | pub tag: Tag, |
| 25 | pub name: String, |
| 26 | pub ordering: u16, |
| 27 | } |
| 28 | |
| 29 | #[derive(Clone, Debug)] |
| 30 | pub(crate) struct AxisValue { |
| 31 | pub flags: u16, |
| 32 | pub name: String, |
| 33 | pub location: AxisLocation, |
| 34 | } |
| 35 | |
| 36 | #[allow(dead_code)] |
| 37 | #[derive(Clone, Debug)] |
| 38 | pub enum AxisLocation { |
| 39 | One { |
| 40 | tag: Tag, |
| 41 | value: Fixed, |
| 42 | }, |
| 43 | Two { |
| 44 | tag: Tag, |
| 45 | nominal: Fixed, |
| 46 | min: Fixed, |
| 47 | max: Fixed, |
| 48 | }, |
| 49 | Three { |
| 50 | tag: Tag, |
| 51 | value: Fixed, |
| 52 | linked: Fixed, |
| 53 | }, |
| 54 | Four(Vec<(Tag, Fixed)>), |
| 55 | } |
| 56 | |
| 57 | impl StatBuilder { |
| 58 | pub(crate) fn build(&self, name_records: &mut Vec<NameRecord>) -> write_stat::Stat { |
| 59 | // let elided_fallback_name_id = match &self.name { |
| 60 | // StatFallbackName::Id(id) => *id, |
| 61 | // StatFallbackName::Record(name) => find_or_add_name(name_records, name), |
| 62 | // }; |
| 63 | |
| 64 | //HACK: we jump through a bunch of hoops to ensure our output matches |
| 65 | //feaLib's; in particular we want to add our name table entries grouped by |
| 66 | //axis. |
| 67 | let mut sorted_values = HashMap::<Tag, Vec<_>>::new(); |
| 68 | let mut sorted_records = self.records.iter().collect::<Vec<_>>(); |
| 69 | sorted_records.sort_by_key(|x| x.ordering); |
| 70 | |
| 71 | for axis_value in &self.values { |
| 72 | match axis_value.location { |
| 73 | AxisLocation::One { tag, .. } |
| 74 | | AxisLocation::Two { tag, .. } |
| 75 | | AxisLocation::Three { tag, .. } => { |
| 76 | sorted_values.entry(tag).or_default().push(axis_value) |
| 77 | } |
| 78 | AxisLocation::Four(_) => sorted_values |
| 79 | .entry(Tag::default()) |
| 80 | .or_default() |
| 81 | .push(axis_value), |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | let mut design_axes = Vec::with_capacity(self.records.len()); |
| 86 | let mut axis_values = Vec::with_capacity(self.values.len()); |
| 87 | |
| 88 | for (i, record) in self.records.iter().enumerate() { |
| 89 | let name_id = find_or_add_name(name_records, &record.name); |
| 90 | let record = write_stat::AxisRecord { |
| 91 | axis_tag: record.tag, |
| 92 | axis_name_id: name_id, |
| 93 | axis_ordering: record.ordering, |
| 94 | }; |
| 95 | for axis_value in sorted_values |
| 96 | .get(&record.axis_tag) |
| 97 | .iter() |
| 98 | .flat_map(|x| x.iter()) |
| 99 | { |
| 100 | let flags = write_stat::AxisValueTableFlags::from_bits(axis_value.flags).unwrap(); |
| 101 | let name_id = find_or_add_name(name_records, &axis_value.name); |
| 102 | |
| 103 | let value = match &axis_value.location { |
| 104 | AxisLocation::One { value, .. } => write_stat::AxisValue::format_1( |
| 105 | //TODO: validate that all referenced tags refer to existing axes |
| 106 | i as u16, flags, name_id, *value, |
| 107 | ), |
| 108 | AxisLocation::Two { |
| 109 | nominal, min, max, .. |
| 110 | } => write_stat::AxisValue::format_2( |
| 111 | i as _, flags, name_id, *nominal, *min, *max, |
| 112 | ), |
| 113 | AxisLocation::Three { value, linked, .. } => { |
| 114 | write_stat::AxisValue::format_3(i as _, flags, name_id, *value, *linked) |
| 115 | } |
| 116 | |
| 117 | AxisLocation::Four(_) => panic!("assigned to separate group"), |
| 118 | }; |
| 119 | axis_values.push(value); |
| 120 | } |
| 121 | |
| 122 | design_axes.push(record); |
| 123 | } |
| 124 | |
| 125 | let format4 = sorted_values |
| 126 | .remove(&Tag::default()) |
| 127 | .unwrap_or_default() |
| 128 | .into_iter() |
| 129 | .map(|format4| { |
| 130 | let flags = write_stat::AxisValueTableFlags::from_bits(format4.flags).unwrap(); |
| 131 | let name_id = find_or_add_name(name_records, &format4.name); |
| 132 | |
| 133 | let AxisLocation::Four(values) = &format4.location else { |
| 134 | panic!("only format 4 in this group") |
| 135 | }; |
| 136 | let mapping = values |
| 137 | .iter() |
| 138 | .map(|(tag, value)| { |
| 139 | let axis_index = design_axes |
| 140 | .iter() |
| 141 | .position(|rec| rec.axis_tag == *tag) |
| 142 | .expect("validated"); |
| 143 | write_stat::AxisValueRecord::new(axis_index as _, *value) |
| 144 | }) |
| 145 | .collect(); |
| 146 | write_stat::AxisValue::format_4(flags, name_id, mapping) |
| 147 | }); |
| 148 | |
| 149 | //feaLib puts format4 records first |
| 150 | let axis_values = format4.chain(axis_values).collect(); |
| 151 | write_stat::Stat::new(design_axes, axis_values, NameId::from(2)) |
| 152 | } |
| 153 | } |