Skip to content

Commit c69d124

Browse files
authored
refactor(fontwerk/names.rs): replace get_name_PEL_codes and get_name_entry_string (#480)
+ fix uniitest (because of different sorting of platform _tuples)
1 parent 2779526 commit c69d124

File tree

1 file changed

+14
-71
lines changed
  • profile-fontwerk/src/checks/fontwerk

1 file changed

+14
-71
lines changed

profile-fontwerk/src/checks/fontwerk/names.rs

Lines changed: 14 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use fontations::{
2-
read::{tables::name::NameString, TableProvider},
3-
skrifa::{font::FontRef, string::StringId},
4-
};
5-
use fontspector_checkapi::{prelude::*, skip, testfont, FileTypeConvert};
6-
use std::{
7-
collections::{HashMap, HashSet},
8-
vec,
1+
use fontations::skrifa::string::StringId;
2+
use fontspector_checkapi::{
3+
get_name_entry_string, get_name_platform_tuples, prelude::*, skip, testfont, FileTypeConvert,
4+
PlatformSelector,
95
};
6+
use std::{collections::HashMap, vec};
107

118
#[check(
129
id = "fontwerk/name_entries",
@@ -114,8 +111,8 @@ fn name_consistency(t: &Testable, context: &Context) -> CheckFnResult {
114111
),
115112
];
116113

117-
let name_PEL_codes = get_name_PEL_codes(font.font());
118-
for code in name_PEL_codes {
114+
let platform_tuples = get_name_platform_tuples(font.font());
115+
for platform_tuple in platform_tuples {
119116
let mut name_strings: Vec<(String, String)> = vec![];
120117
for (i, name_id_pair) in name_ids.iter().enumerate() {
121118
let mut full_name = String::new();
@@ -125,9 +122,12 @@ fn name_consistency(t: &Testable, context: &Context) -> CheckFnResult {
125122
id_pair.push(sub_name_id);
126123
}
127124
for name_id in id_pair.iter() {
128-
if let Some(name_string) =
129-
get_name_entry_string(&font.font(), code.0, code.1, code.2, *name_id)
130-
{
125+
let selector = PlatformSelector {
126+
platform_id: platform_tuple.0,
127+
encoding_id: platform_tuple.1,
128+
language_id: platform_tuple.2,
129+
};
130+
if let Some(name_string) = get_name_entry_string(&font.font(), selector, *name_id) {
131131
pair.push(true);
132132
full_name.push_str(&name_string.to_string());
133133
full_name.push(' ');
@@ -160,7 +160,7 @@ fn name_consistency(t: &Testable, context: &Context) -> CheckFnResult {
160160
if first.0 != name.0 {
161161
bad_names.push(format!(
162162
"Inconsistent names {:?}: {} ({}) != {} ({})",
163-
code, first.0, first.1, name.0, name.1
163+
platform_tuple, first.0, first.1, name.0, name.1
164164
));
165165
}
166166
}
@@ -180,34 +180,6 @@ fn name_consistency(t: &Testable, context: &Context) -> CheckFnResult {
180180
})
181181
}
182182

183-
/// Get a string from the font's name table by platform_id, encoding_id, language_id and name_id
184-
fn get_name_entry_string<'a>(
185-
font: &'a FontRef,
186-
platform_id: u16,
187-
encoding_id: u16,
188-
language_id: u16,
189-
name_id: StringId,
190-
) -> Option<NameString<'a>> {
191-
let name = font.name().ok();
192-
let mut records = name
193-
.as_ref()
194-
.map(|name| name.name_record().iter())
195-
.unwrap_or([].iter());
196-
records.find_map(|record| {
197-
if record.platform_id() == platform_id
198-
&& record.encoding_id() == encoding_id
199-
&& record.language_id() == language_id
200-
&& record.name_id() == name_id
201-
{
202-
// Use ? to extract the TableRef before calling string_data()
203-
let name_table = name.as_ref()?;
204-
record.string(name_table.string_data()).ok()
205-
} else {
206-
None
207-
}
208-
})
209-
}
210-
211183
fn get_string_id_from_string(name_id_string: &str) -> Option<StringId> {
212184
let registered_name_ids = HashMap::from([
213185
("COPYRIGHT_NOTICE", StringId::COPYRIGHT_NOTICE), // Name ID 0
@@ -250,24 +222,6 @@ fn get_string_id_from_string(name_id_string: &str) -> Option<StringId> {
250222
registered_name_ids.get(name_id_string).copied()
251223
}
252224

253-
fn get_name_PEL_codes(font: FontRef) -> Vec<(u16, u16, u16)> {
254-
let name_table = font.name().ok();
255-
256-
let mut codes_vec = vec![];
257-
if let Some(name_table) = name_table {
258-
for rec in name_table.name_record().iter() {
259-
let code = (rec.platform_id(), rec.encoding_id(), rec.language_id());
260-
codes_vec.push(code);
261-
}
262-
}
263-
// make set of codes_vec
264-
let unique_codes: HashSet<(u16, u16, u16)> = codes_vec.into_iter().collect();
265-
266-
let mut unique_codes: Vec<(u16, u16, u16)> = unique_codes.into_iter().collect();
267-
unique_codes.sort();
268-
unique_codes
269-
}
270-
271225
#[cfg(test)]
272226
mod tests {
273227
#![allow(clippy::unwrap_used, clippy::expect_used)]
@@ -289,17 +243,6 @@ mod tests {
289243
assert_eq!(string_id.unwrap(), StringId::TYPOGRAPHIC_FAMILY_NAME);
290244
}
291245

292-
#[test]
293-
fn test_get_name_PEL_codes() {
294-
let contents = include_bytes!(
295-
"../../../../fontspector-py/data/test/montserrat/Montserrat-Regular.ttf"
296-
);
297-
let font = FontRef::new(contents).expect("Failed to create FontRef from contents");
298-
let expected_codes = vec![(1, 0, 0), (3, 1, 1033)];
299-
let name_PEL_codes = get_name_PEL_codes(font);
300-
assert_eq!(name_PEL_codes, expected_codes);
301-
}
302-
303246
#[test]
304247
fn test_name_entries() {
305248
let config_1 = HashMap::from([

0 commit comments

Comments
 (0)