|
| 1 | +use std::collections::HashMap; |
| 2 | + |
1 | 3 | use font_types::NameId;
|
2 |
| -use fontspector_checkapi::{prelude::*, testfont, FileTypeConvert}; |
| 4 | +use fontspector_checkapi::{prelude::*, skip, testfont, FileTypeConvert}; |
3 | 5 | use read_fonts::TableProvider;
|
4 | 6 |
|
5 | 7 | #[check(
|
@@ -117,6 +119,170 @@ fn check_name_match_familyname_fullfont(t: &Testable, _context: &Context) -> Che
|
117 | 119 | return_result(vec![])
|
118 | 120 | }
|
119 | 121 |
|
| 122 | +#[check( |
| 123 | + id = "opentype/family/max_4_fonts_per_family_name", |
| 124 | + rationale = " |
| 125 | + Per the OpenType spec: |
| 126 | +
|
| 127 | + 'The Font Family name [...] should be shared among at most four fonts that |
| 128 | + differ only in weight or style [...]' |
| 129 | + ", |
| 130 | + proposal = "https://github.com/fonttools/fontbakery/pull/2372", |
| 131 | + title = "Verify that each group of fonts with the same nameID 1 has maximum of 4 fonts.", |
| 132 | + implementation = "all" |
| 133 | +)] |
| 134 | +fn family_max_4_fonts_per_family_name(t: &TestableCollection, _context: &Context) -> CheckFnResult { |
| 135 | + let fonts = TTF.from_collection(t); |
| 136 | + let mut counter = HashMap::new(); |
| 137 | + let mut problems = vec![]; |
| 138 | + for font in fonts { |
| 139 | + let family_name = font |
| 140 | + .get_name_entry_strings(NameId::FAMILY_NAME) |
| 141 | + .next() |
| 142 | + .ok_or_else(|| { |
| 143 | + CheckError::Error(format!( |
| 144 | + "Font {} is missing a Family Name entry", |
| 145 | + font.filename.to_string_lossy() |
| 146 | + )) |
| 147 | + })?; |
| 148 | + *counter.entry(family_name).or_insert(0) += 1; |
| 149 | + } |
| 150 | + for (family_name, count) in counter { |
| 151 | + if count > 4 { |
| 152 | + problems.push(Status::fail( |
| 153 | + "too-many-fonts", |
| 154 | + &format!( |
| 155 | + "Family name '{}' has {} fonts, which is more than the maximum of 4", |
| 156 | + family_name, count |
| 157 | + ), |
| 158 | + )); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return_result(problems) |
| 163 | +} |
| 164 | + |
| 165 | +#[check( |
| 166 | + id = "opentype/postscript_name", |
| 167 | + title = "PostScript name follows OpenType specification requirements?", |
| 168 | + rationale = "The PostScript name is used by some applications to identify the font. It should only consist of characters from the set A-Z, a-z, 0-9, and hyphen.", |
| 169 | + proposal = "https://github.com/miguelsousa/openbakery/issues/62" |
| 170 | +)] |
| 171 | +fn postscript_name(t: &Testable, _context: &Context) -> CheckFnResult { |
| 172 | + let font = testfont!(t); |
| 173 | + let mut problems = vec![]; |
| 174 | + for name in font.get_name_entry_strings(NameId::POSTSCRIPT_NAME) { |
| 175 | + if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') { |
| 176 | + problems.push(Status::fail( |
| 177 | + "invalid-postscript-name", |
| 178 | + &format!("PostScript name '{}' contains invalid characters", name), |
| 179 | + )); |
| 180 | + } |
| 181 | + } |
| 182 | + return_result(problems) |
| 183 | +} |
| 184 | + |
| 185 | +const NAME_LIMITS: [(NameId, usize); 6] = [ |
| 186 | + (NameId::FULL_NAME, 63), |
| 187 | + (NameId::POSTSCRIPT_NAME, 63), |
| 188 | + (NameId::FAMILY_NAME, 31), |
| 189 | + (NameId::SUBFAMILY_NAME, 31), |
| 190 | + (NameId::TYPOGRAPHIC_FAMILY_NAME, 31), |
| 191 | + (NameId::TYPOGRAPHIC_SUBFAMILY_NAME, 31), |
| 192 | +]; |
| 193 | + |
| 194 | +#[check( |
| 195 | + id = "opentype/family_naming_recommendations", |
| 196 | + rationale = " |
| 197 | + This check ensures that the length of various family name and style |
| 198 | + name strings in the name table are within the maximum length |
| 199 | + recommended by the OpenType specification. |
| 200 | + ", |
| 201 | + proposal = "https://github.com/fonttools/fontbakery/issues/4829", |
| 202 | + title = "Font follows the family naming recommendations?" |
| 203 | +)] |
| 204 | +fn family_naming_recommendations(t: &Testable, _context: &Context) -> CheckFnResult { |
| 205 | + let font = testfont!(t); |
| 206 | + |
| 207 | + let mut problems = vec![]; |
| 208 | + for (name_id, max_length) in NAME_LIMITS.iter() { |
| 209 | + for name in font.get_name_entry_strings(*name_id) { |
| 210 | + if name.len() > *max_length { |
| 211 | + problems.push(Status::warn( |
| 212 | + "name-too-long", |
| 213 | + &format!( |
| 214 | + "{:?} (\"{}\") is too long ({} > {})", |
| 215 | + name_id, |
| 216 | + name, |
| 217 | + name.len(), |
| 218 | + max_length |
| 219 | + ), |
| 220 | + )); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + return_result(problems) |
| 225 | +} |
| 226 | + |
| 227 | +#[check( |
| 228 | + id = "opentype/name/italic_names", |
| 229 | + rationale = " |
| 230 | + This check ensures that several entries in the name table |
| 231 | + conform to the font's Upright or Italic style, |
| 232 | + namely IDs 1 & 2 as well as 16 & 17 if they're present. |
| 233 | + ", |
| 234 | + proposal = "https://github.com/fonttools/fontbakery/issues/3666", |
| 235 | + title = "Check name table IDs 1, 2, 16, 17 to conform to Italic style." |
| 236 | +)] |
| 237 | +fn name_italic_names(t: &Testable, _context: &Context) -> CheckFnResult { |
| 238 | + let font = testfont!(t); |
| 239 | + let mut problems = vec![]; |
| 240 | + skip!( |
| 241 | + !font.filename_suggests_italic(), |
| 242 | + "not-italic", |
| 243 | + "Font is not italic" |
| 244 | + ); |
| 245 | + if let Some(family_name) = font.get_name_entry_strings(NameId::FAMILY_NAME).next() { |
| 246 | + if family_name.contains("Italic") { |
| 247 | + problems.push(Status::fail( |
| 248 | + "bad-familyname", |
| 249 | + "Name ID 1 (Family Name) must not contain 'Italic'.", |
| 250 | + )); |
| 251 | + } |
| 252 | + } |
| 253 | + if let Some(subfamily_name) = font.get_name_entry_strings(NameId::SUBFAMILY_NAME).next() { |
| 254 | + if subfamily_name != "Italic" && subfamily_name != "Bold Italic" { |
| 255 | + problems.push(Status::fail( |
| 256 | + "bad-subfamilyname", |
| 257 | + &format!("Name ID 2 (Subfamily Name) does not conform to specs. Only R/I/B/BI are allowed, found {}", subfamily_name) |
| 258 | + )); |
| 259 | + } |
| 260 | + } |
| 261 | + if let Some(typo_family_name) = font |
| 262 | + .get_name_entry_strings(NameId::TYPOGRAPHIC_FAMILY_NAME) |
| 263 | + .next() |
| 264 | + { |
| 265 | + if typo_family_name.contains("Italic") { |
| 266 | + problems.push(Status::fail( |
| 267 | + "bad-typographicfamilyname", |
| 268 | + "Name ID 16 (Typographic Family Name) must not contain 'Italic'.", |
| 269 | + )); |
| 270 | + } |
| 271 | + } |
| 272 | + if let Some(typo_subfamily_name) = font |
| 273 | + .get_name_entry_strings(NameId::TYPOGRAPHIC_SUBFAMILY_NAME) |
| 274 | + .next() |
| 275 | + { |
| 276 | + if !typo_subfamily_name.ends_with("Italic") { |
| 277 | + problems.push(Status::fail( |
| 278 | + "bad-typographicsubfamilyname", |
| 279 | + "Name ID 16 (Typographic Family Name) must contain 'Italic'.", |
| 280 | + )); |
| 281 | + } |
| 282 | + } |
| 283 | + return_result(problems) |
| 284 | +} |
| 285 | + |
120 | 286 | #[cfg(test)]
|
121 | 287 | #[allow(clippy::unwrap_used)]
|
122 | 288 | mod tests {
|
|
0 commit comments