|
1 |
| -// use std::collections::HashSet; |
| 1 | +use std::collections::HashSet; |
2 | 2 |
|
3 |
| -// use fontspector_checkapi::{return_result, Check, FontCollection, Status, StatusCode, StatusList}; |
4 |
| -// use read_fonts::tables::os2::SelectionFlags; |
5 |
| -// use skrifa::string::StringId; |
| 3 | +use fontspector_checkapi::{prelude::*, FileTypeConvert}; |
| 4 | +use read_fonts::tables::os2::SelectionFlags; |
| 5 | +use skrifa::string::StringId; |
6 | 6 |
|
7 |
| -// fn bold_italic_unique(c: &FontCollection) -> StatusList { |
8 |
| -// let ribbi = c.ribbi_fonts(); |
9 |
| -// let mut problems = vec![]; |
10 |
| -// let mut flags: HashSet<(bool, bool)> = HashSet::new(); |
11 |
| -// for font in ribbi.iter() { |
12 |
| -// let _names_list = font.get_name_entry_strings(StringId::FAMILY_NAME); |
13 |
| -// match font.get_os2_fsselection() { |
14 |
| -// Ok(fsselection) => { |
15 |
| -// let val = ( |
16 |
| -// fsselection.intersects(SelectionFlags::BOLD), |
17 |
| -// fsselection.intersects(SelectionFlags::ITALIC), |
18 |
| -// ); |
19 |
| -// if flags.contains(&val) { |
20 |
| -// problems.push(Status { |
21 |
| -// message: Some(format!( |
22 |
| -// "Font {} has the same selection flags ({}{}{}) as another font", |
23 |
| -// font.filename, |
24 |
| -// if val.0 { "bold" } else { "" }, |
25 |
| -// if val.0 && val.1 { " & " } else { "" }, |
26 |
| -// if val.1 { "italic" } else { "" } |
27 |
| -// )), |
28 |
| -// code: StatusCode::Error, |
29 |
| -// }); |
30 |
| -// } else { |
31 |
| -// flags.insert(val); |
32 |
| -// } |
33 |
| -// } |
34 |
| -// Err(_e) => problems.push(Status { |
35 |
| -// message: Some(format!("Font {} had no OS2 table", font.filename)), |
36 |
| -// code: StatusCode::Error, |
37 |
| -// }), |
38 |
| -// } |
39 |
| -// } |
40 |
| -// return_result(problems) |
41 |
| -// } |
42 |
| -// pub const BOLD_ITALIC_UNIQUE_CHECK: Check = Check { |
43 |
| -// id: "opentype/family/bold_italic_unique_for_nameid1", |
44 |
| -// title: "Check that OS/2.fsSelection bold & italic settings are unique for each NameID1", |
45 |
| -// rationale: None, |
46 |
| -// proposal: Some("https://github.com/googlefonts/fontbakery/pull/2388"), |
47 |
| -// check_all: Some(&bold_italic_unique), |
48 |
| -// check_one: None, |
49 |
| -// }; |
| 7 | +#[check( |
| 8 | + id = "opentype/family/bold_italic_unique_for_nameid1", |
| 9 | + title = "Check that OS/2.fsSelection bold & italic settings are unique for each NameID1", |
| 10 | + rationale = "Per the OpenType spec: name ID 1 'is used in combination with Font Subfamily |
| 11 | + name (name ID 2), and should be shared among at most four fonts that differ |
| 12 | + only in weight or style. |
| 13 | +
|
| 14 | + This four-way distinction should also be reflected in the OS/2.fsSelection |
| 15 | + field, using bits 0 and 5. |
| 16 | + ", |
| 17 | + proposal = "legacy:check/153", |
| 18 | + implementation = "all" |
| 19 | +)] |
| 20 | +fn bold_italic_unique(c: &TestableCollection, _context: &Context) -> CheckFnResult { |
| 21 | + let fonts = TTF.from_collection(c); |
| 22 | + let mut problems = vec![]; |
| 23 | + let mut flags: HashSet<(bool, bool)> = HashSet::new(); |
| 24 | + let ribbi = fonts.iter().filter(|f| f.is_ribbi()); |
| 25 | + for font in ribbi { |
| 26 | + let _names_list = font.get_name_entry_strings(StringId::FAMILY_NAME); |
| 27 | + let fsselection = font.get_os2_fsselection()?; |
| 28 | + let val = ( |
| 29 | + fsselection.intersects(SelectionFlags::BOLD), |
| 30 | + fsselection.intersects(SelectionFlags::ITALIC), |
| 31 | + ); |
| 32 | + if flags.contains(&val) { |
| 33 | + problems.push(Status::fail( |
| 34 | + "unique-fsselection", |
| 35 | + &(format!( |
| 36 | + "Font {} has the same selection flags ({}{}{}) as another font", |
| 37 | + font.filename.to_string_lossy(), |
| 38 | + if val.0 { "bold" } else { "" }, |
| 39 | + if val.0 && val.1 { " & " } else { "" }, |
| 40 | + if val.1 { "italic" } else { "" } |
| 41 | + )), |
| 42 | + )); |
| 43 | + } else { |
| 44 | + flags.insert(val); |
| 45 | + } |
| 46 | + } |
| 47 | + return_result(problems) |
| 48 | +} |
0 commit comments