|
| 1 | +use std::{borrow::Cow, collections::HashMap, vec}; |
| 2 | + |
| 3 | +use fontspector_checkapi::{prelude::*, skip, testfont, FileTypeConvert}; |
| 4 | +use skrifa::MetadataProvider; |
| 5 | + |
| 6 | +fn regular_coords_correct(t: &Testable, _context: &Context) -> CheckFnResult { |
| 7 | + let f = testfont!(t); |
| 8 | + if !f.is_variable_font() { |
| 9 | + skip!("not-variable", "Not a variable font"); |
| 10 | + } |
| 11 | + let axes = f.font().axes(); |
| 12 | + let mut problems = vec![]; |
| 13 | + let regular_instance = f |
| 14 | + .font() |
| 15 | + .named_instances() |
| 16 | + .iter() |
| 17 | + .find(|ni| { |
| 18 | + f.font() |
| 19 | + .localized_strings(ni.subfamily_name_id()) |
| 20 | + .any(|s| "Regular" == s.chars().collect::<Cow<str>>()) |
| 21 | + }) |
| 22 | + .ok_or(CheckError::Skip { |
| 23 | + code: "no-regular".to_string(), |
| 24 | + message: "No Regular instance found".to_string(), |
| 25 | + })?; |
| 26 | + let regular_location: HashMap<String, f32> = regular_instance |
| 27 | + .user_coords() |
| 28 | + .zip(axes.iter()) |
| 29 | + .map(|(coord, axis)| (axis.tag().to_string(), coord)) |
| 30 | + .collect(); |
| 31 | + let expectations = vec![("wght", 400.0), ("wdth", 100.0), ("slnt", 0.0)]; |
| 32 | + for (axis, expected) in expectations { |
| 33 | + if let Some(actual) = regular_location.get(axis) { |
| 34 | + if *actual != expected { |
| 35 | + problems.push(Status::fail( |
| 36 | + axis, |
| 37 | + &format!( |
| 38 | + "Regular instance has {} coordinate of {}, expected {}", |
| 39 | + axis, actual, expected |
| 40 | + ), |
| 41 | + )); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + return_result(problems) |
| 46 | +} |
| 47 | + |
| 48 | +pub const CHECK_REGULAR_COORDS_CORRECT: Check = Check { |
| 49 | + id: "com.google.fonts/check/fvar/regular_coords_correct", |
| 50 | + title: "Regular instance coordinates are correct?", |
| 51 | + rationale: "According to the Open-Type spec's registered design-variation tags, the Regular instance in a variable font should have certain prescribed values. |
| 52 | + If a variable font has a 'wght' (Weight) axis, then the coordinate of its 'Regular' instance is required to be 400. |
| 53 | + If a variable font has a 'wdth' (Width) axis, then the coordinate of its 'Regular' instance is required to be 100. |
| 54 | + If a variable font has a 'slnt' (Slant) axis, then the coordinate of its 'Regular' instance is required to be 0.", |
| 55 | + proposal: "https://github.com/fonttools/fontbakery/issues/1707", |
| 56 | + check_one: Some(®ular_coords_correct), |
| 57 | + check_all: None, |
| 58 | + applies_to: "TTF", |
| 59 | + hotfix: None, |
| 60 | + fix_source: None, |
| 61 | + flags: CheckFlags::default(), |
| 62 | +}; |
0 commit comments