|
| 1 | +//! Generates an [MVAR](https://learn.microsoft.com/en-us/typography/opentype/spec/MVAR) table. |
| 2 | +
|
| 3 | +use std::collections::{BTreeMap, BTreeSet, HashMap}; |
| 4 | + |
| 5 | +use fontdrasil::orchestration::AccessBuilder; |
| 6 | + |
| 7 | +use fontdrasil::{ |
| 8 | + coords::NormalizedLocation, |
| 9 | + orchestration::{Access, Work}, |
| 10 | + types::Axis, |
| 11 | +}; |
| 12 | +use fontir::{ |
| 13 | + ir::GlobalMetricValues, orchestration::WorkId as FeWorkId, variations::VariationModel, |
| 14 | +}; |
| 15 | +use write_fonts::types::MajorMinor; |
| 16 | +use write_fonts::{ |
| 17 | + tables::{ |
| 18 | + mvar::{Mvar, ValueRecord}, |
| 19 | + variations::{ivs_builder::VariationStoreBuilder, VariationRegion}, |
| 20 | + }, |
| 21 | + types::Tag, |
| 22 | + OtRound, |
| 23 | +}; |
| 24 | + |
| 25 | +use crate::{ |
| 26 | + error::Error, |
| 27 | + orchestration::{AnyWorkId, BeWork, Context, WorkId}, |
| 28 | +}; |
| 29 | + |
| 30 | +#[derive(Debug)] |
| 31 | +struct MvarWork {} |
| 32 | + |
| 33 | +pub fn create_mvar_work() -> Box<BeWork> { |
| 34 | + Box::new(MvarWork {}) |
| 35 | +} |
| 36 | + |
| 37 | +/// Helper to build MVAR table from global metrics sources. |
| 38 | +struct MvarBuilder { |
| 39 | + /// Variation axes |
| 40 | + axes: Vec<Axis>, |
| 41 | + /// Sparse variation models, keyed by the set of locations they define |
| 42 | + models: HashMap<BTreeSet<NormalizedLocation>, VariationModel>, |
| 43 | + /// Metrics deltas keyed by MVAR tag |
| 44 | + deltas: BTreeMap<Tag, Vec<(VariationRegion, i16)>>, |
| 45 | +} |
| 46 | + |
| 47 | +impl MvarBuilder { |
| 48 | + fn new(global_model: VariationModel) -> Self { |
| 49 | + let axes = global_model.axes().cloned().collect::<Vec<_>>(); |
| 50 | + let global_locations = global_model.locations().cloned().collect::<BTreeSet<_>>(); |
| 51 | + let mut models = HashMap::new(); |
| 52 | + models.insert(global_locations, global_model); |
| 53 | + MvarBuilder { |
| 54 | + axes, |
| 55 | + models, |
| 56 | + deltas: BTreeMap::new(), |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + fn add_sources(&mut self, mvar_tag: Tag, sources: &GlobalMetricValues) -> Result<(), Error> { |
| 61 | + let sources: HashMap<_, _> = sources |
| 62 | + .iter() |
| 63 | + .map(|(loc, src)| (loc.clone(), vec![src.into_inner() as f64])) |
| 64 | + .collect(); |
| 65 | + if sources.len() == 1 { |
| 66 | + assert!(sources.keys().next().unwrap().is_default()); |
| 67 | + // spare the model the work of computing no-op deltas |
| 68 | + return Ok(()); |
| 69 | + } |
| 70 | + let locations = sources.keys().cloned().collect::<BTreeSet<_>>(); |
| 71 | + let model = self.models.entry(locations).or_insert_with(|| { |
| 72 | + // this glyph defines its own set of locations, a new sparse model is needed |
| 73 | + VariationModel::new(sources.keys().cloned().collect(), self.axes.clone()).unwrap() |
| 74 | + }); |
| 75 | + let deltas: Vec<_> = model |
| 76 | + .deltas(&sources) |
| 77 | + .map_err(|e| Error::MvarDeltaError(mvar_tag, e))? |
| 78 | + .into_iter() |
| 79 | + .filter_map(|(region, values)| { |
| 80 | + if region.is_default() { |
| 81 | + return None; |
| 82 | + } |
| 83 | + // Only 1 value per region for our input |
| 84 | + assert!(values.len() == 1, "{} values?!", values.len()); |
| 85 | + Some(( |
| 86 | + region.to_write_fonts_variation_region(&self.axes), |
| 87 | + values[0].ot_round(), |
| 88 | + )) |
| 89 | + }) |
| 90 | + .collect(); |
| 91 | + // don't encode no-op deltas |
| 92 | + if deltas.iter().all(|(_, delta)| *delta == 0) { |
| 93 | + return Ok(()); |
| 94 | + } |
| 95 | + self.deltas.insert(mvar_tag, deltas); |
| 96 | + Ok(()) |
| 97 | + } |
| 98 | + |
| 99 | + fn build(self) -> Option<Mvar> { |
| 100 | + let mut builder = VariationStoreBuilder::new(self.axes.len() as u16); |
| 101 | + let delta_ids = self |
| 102 | + .deltas |
| 103 | + .into_iter() |
| 104 | + .map(|(tag, deltas)| (tag, builder.add_deltas(deltas))) |
| 105 | + .collect::<Vec<_>>(); |
| 106 | + |
| 107 | + let (varstore, index_map) = builder.build(); |
| 108 | + |
| 109 | + let records = delta_ids |
| 110 | + .into_iter() |
| 111 | + .map(|(tag, temp_id)| { |
| 112 | + let varidx = index_map.get(temp_id).unwrap(); |
| 113 | + ValueRecord::new( |
| 114 | + tag, |
| 115 | + varidx.delta_set_outer_index, |
| 116 | + varidx.delta_set_inner_index, |
| 117 | + ) |
| 118 | + }) |
| 119 | + .collect::<Vec<_>>(); |
| 120 | + |
| 121 | + (!records.is_empty()).then(|| Mvar::new(MajorMinor::VERSION_1_0, Some(varstore), records)) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +impl Work<Context, AnyWorkId, Error> for MvarWork { |
| 126 | + fn id(&self) -> AnyWorkId { |
| 127 | + WorkId::Mvar.into() |
| 128 | + } |
| 129 | + |
| 130 | + fn read_access(&self) -> Access<AnyWorkId> { |
| 131 | + AccessBuilder::new() |
| 132 | + .variant(FeWorkId::StaticMetadata) |
| 133 | + .variant(FeWorkId::GlobalMetrics) |
| 134 | + .build() |
| 135 | + } |
| 136 | + |
| 137 | + /// Generate [MVAR](https://learn.microsoft.com/en-us/typography/opentype/spec/MVAR) |
| 138 | + fn exec(&self, context: &Context) -> Result<(), Error> { |
| 139 | + let static_metadata = context.ir.static_metadata.get(); |
| 140 | + let metrics = context.ir.global_metrics.get(); |
| 141 | + let var_model = &static_metadata.variation_model; |
| 142 | + |
| 143 | + let mut mvar_builder = MvarBuilder::new(var_model.clone()); |
| 144 | + for (metric, values) in metrics.iter() { |
| 145 | + // maybe we should get rid of GlobalMetric variants that aren't MVAR-relevant? |
| 146 | + if let Some(mvar_tag) = metric.mvar_tag() { |
| 147 | + mvar_builder.add_sources(mvar_tag, values)?; |
| 148 | + } |
| 149 | + } |
| 150 | + let mvar = mvar_builder.build(); |
| 151 | + |
| 152 | + context.mvar.set_unconditionally(mvar.into()); |
| 153 | + |
| 154 | + Ok(()) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +#[cfg(test)] |
| 159 | +mod tests { |
| 160 | + use std::str::FromStr; |
| 161 | + |
| 162 | + use fontdrasil::{ |
| 163 | + coords::{CoordConverter, UserCoord}, |
| 164 | + types::Axis, |
| 165 | + }; |
| 166 | + use write_fonts::{ |
| 167 | + dump_table, |
| 168 | + read::{ |
| 169 | + tables::{mvar as read_mvar, variations::ItemVariationData}, |
| 170 | + FontData, FontRead, |
| 171 | + }, |
| 172 | + types::F2Dot14, |
| 173 | + }; |
| 174 | + |
| 175 | + use super::*; |
| 176 | + |
| 177 | + fn axis(tag: &str, min: f32, default: f32, max: f32) -> Axis { |
| 178 | + let min = UserCoord::new(min); |
| 179 | + let default = UserCoord::new(default); |
| 180 | + let max = UserCoord::new(max); |
| 181 | + Axis { |
| 182 | + name: tag.to_string(), |
| 183 | + tag: Tag::from_str(tag).unwrap(), |
| 184 | + min, |
| 185 | + default, |
| 186 | + max, |
| 187 | + hidden: false, |
| 188 | + converter: CoordConverter::unmapped(min, default, max), |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + fn new_mvar_builder(locations: Vec<&NormalizedLocation>, axes: Vec<Axis>) -> MvarBuilder { |
| 193 | + let locations = locations.into_iter().cloned().collect(); |
| 194 | + let model = VariationModel::new(locations, axes).unwrap(); |
| 195 | + MvarBuilder::new(model) |
| 196 | + } |
| 197 | + |
| 198 | + fn add_sources( |
| 199 | + builder: &mut MvarBuilder, |
| 200 | + mvar_tag: &str, |
| 201 | + sources: &[(&NormalizedLocation, f32)], |
| 202 | + ) { |
| 203 | + let sources = sources |
| 204 | + .iter() |
| 205 | + .map(|(loc, value)| ((*loc).clone(), (*value).into())) |
| 206 | + .collect::<HashMap<_, _>>(); |
| 207 | + builder |
| 208 | + .add_sources(Tag::from_str(mvar_tag).unwrap(), &sources) |
| 209 | + .unwrap(); |
| 210 | + } |
| 211 | + |
| 212 | + fn delta_sets(var_data: &ItemVariationData) -> Vec<Vec<i32>> { |
| 213 | + (0..var_data.item_count()) |
| 214 | + .map(|i| var_data.delta_set(i).collect::<Vec<_>>()) |
| 215 | + .collect() |
| 216 | + } |
| 217 | + |
| 218 | + #[test] |
| 219 | + fn smoke_test() { |
| 220 | + let regular = NormalizedLocation::for_pos(&[("wght", 0.0)]); |
| 221 | + let bold = NormalizedLocation::for_pos(&[("wght", 1.0)]); |
| 222 | + let mut builder = new_mvar_builder( |
| 223 | + vec![®ular, &bold], |
| 224 | + vec![axis("wght", 400.0, 400.0, 700.0)], |
| 225 | + ); |
| 226 | + |
| 227 | + add_sources(&mut builder, "xhgt", &[(®ular, 500.0), (&bold, 550.0)]); |
| 228 | + |
| 229 | + let Some(mvar) = builder.build() else { |
| 230 | + panic!("no MVAR?!"); |
| 231 | + }; |
| 232 | + |
| 233 | + let bytes = dump_table(&mvar).unwrap(); |
| 234 | + let mvar = read_mvar::Mvar::read(FontData::new(&bytes)).unwrap(); |
| 235 | + |
| 236 | + assert_eq!(mvar.version(), MajorMinor::VERSION_1_0); |
| 237 | + assert_eq!(mvar.value_records().len(), 1); |
| 238 | + |
| 239 | + let rec = &mvar.value_records()[0]; |
| 240 | + assert_eq!(rec.value_tag(), Tag::new(b"xhgt")); |
| 241 | + assert_eq!(rec.delta_set_outer_index(), 0); |
| 242 | + assert_eq!(rec.delta_set_inner_index(), 0); |
| 243 | + |
| 244 | + let Some(Ok(varstore)) = mvar.item_variation_store() else { |
| 245 | + panic!("MVAR has no ItemVariationStore?!"); |
| 246 | + }; |
| 247 | + |
| 248 | + assert_eq!(varstore.variation_region_list().unwrap().region_count(), 1); |
| 249 | + assert_eq!(varstore.item_variation_data_count(), 1); |
| 250 | + |
| 251 | + let vardata = varstore.item_variation_data().get(0).unwrap().unwrap(); |
| 252 | + assert_eq!(vardata.region_indexes(), &[0]); |
| 253 | + assert_eq!(delta_sets(&vardata), vec![vec![50]]); |
| 254 | + } |
| 255 | + |
| 256 | + #[test] |
| 257 | + fn no_variations_no_mvar() { |
| 258 | + let regular = NormalizedLocation::for_pos(&[("wght", 0.0)]); |
| 259 | + let bold = NormalizedLocation::for_pos(&[("wght", 1.0)]); |
| 260 | + let mut builder = new_mvar_builder( |
| 261 | + vec![®ular, &bold], |
| 262 | + vec![axis("wght", 400.0, 400.0, 700.0)], |
| 263 | + ); |
| 264 | + |
| 265 | + add_sources(&mut builder, "xhgt", &[(®ular, 500.0), (&bold, 500.0)]); |
| 266 | + add_sources(&mut builder, "cpht", &[(®ular, 800.0), (&bold, 800.0)]); |
| 267 | + |
| 268 | + // hence no MVAR needed |
| 269 | + assert!(builder.build().is_none()); |
| 270 | + } |
| 271 | + |
| 272 | + struct MvarReader<'a> { |
| 273 | + mvar: read_mvar::Mvar<'a>, |
| 274 | + } |
| 275 | + |
| 276 | + impl<'a> MvarReader<'a> { |
| 277 | + fn new(mvar: read_mvar::Mvar<'a>) -> Self { |
| 278 | + Self { mvar } |
| 279 | + } |
| 280 | + |
| 281 | + fn metric_delta(&self, mvar_tag: &str, coords: &[f32]) -> f64 { |
| 282 | + let mvar_tag = Tag::from_str(mvar_tag).unwrap(); |
| 283 | + let coords: Vec<F2Dot14> = coords |
| 284 | + .iter() |
| 285 | + .map(|coord| F2Dot14::from_f32(*coord)) |
| 286 | + .collect(); |
| 287 | + self.mvar.metric_delta(mvar_tag, &coords).unwrap().to_f64() |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + #[test] |
| 292 | + fn sparse_global_metrics() { |
| 293 | + let regular = NormalizedLocation::for_pos(&[("wght", 0.0)]); |
| 294 | + let medium = NormalizedLocation::for_pos(&[("wght", 0.5)]); |
| 295 | + let bold = NormalizedLocation::for_pos(&[("wght", 1.0)]); |
| 296 | + let mut builder = new_mvar_builder( |
| 297 | + vec![®ular, &medium, &bold], |
| 298 | + vec![axis("wght", 400.0, 400.0, 700.0)], |
| 299 | + ); |
| 300 | + // 'xhgt' defines a value for all three locations |
| 301 | + add_sources( |
| 302 | + &mut builder, |
| 303 | + "xhgt", |
| 304 | + &[(®ular, 500.0), (&medium, 530.0), (&bold, 550.0)], |
| 305 | + ); |
| 306 | + // 'strs' is sparse: defines a value for regular and bold, not medium |
| 307 | + add_sources(&mut builder, "strs", &[(®ular, 50.0), (&bold, 100.0)]); |
| 308 | + |
| 309 | + let Some(mvar) = builder.build() else { |
| 310 | + panic!("no MVAR?!"); |
| 311 | + }; |
| 312 | + |
| 313 | + let bytes = dump_table(&mvar).unwrap(); |
| 314 | + let mvar = read_mvar::Mvar::read(FontData::new(&bytes)).unwrap(); |
| 315 | + |
| 316 | + assert_eq!(mvar.value_records().len(), 2); |
| 317 | + assert_eq!(mvar.value_records()[0].value_tag(), Tag::new(b"strs")); |
| 318 | + assert_eq!(mvar.value_records()[1].value_tag(), Tag::new(b"xhgt")); |
| 319 | + |
| 320 | + let mvar = MvarReader::new(mvar); |
| 321 | + assert_eq!(mvar.metric_delta("xhgt", &[0.0]), 0.0); |
| 322 | + assert_eq!(mvar.metric_delta("xhgt", &[0.5]), 30.0); // not 25.0 |
| 323 | + assert_eq!(mvar.metric_delta("xhgt", &[1.0]), 50.0); |
| 324 | + |
| 325 | + assert_eq!(mvar.metric_delta("strs", &[0.0]), 0.0); |
| 326 | + assert_eq!(mvar.metric_delta("strs", &[0.5]), 25.0); // interpolated |
| 327 | + assert_eq!(mvar.metric_delta("strs", &[1.0]), 50.0); |
| 328 | + } |
| 329 | +} |
0 commit comments