Skip to content

Commit

Permalink
Fix incorrect size calculation for MacRoman strings
Browse files Browse the repository at this point in the history
We were counting bytes and not chars.
  • Loading branch information
cmyr committed Dec 16, 2022
1 parent 258e542 commit 8ab86d1
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion write-fonts/src/tables/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl NameStringWriter<'_> {
match self.encoding {
Encoding::Utf16Be => self.string.chars().map(|c| c.len_utf16() as u16 * 2).sum(),
// this will be correct assuming we pass validation
Encoding::MacRoman => self.string.len().try_into().unwrap(),
Encoding::MacRoman => self.string.chars().count().try_into().unwrap(),
Encoding::Unknown => 0,
}
}
Expand Down Expand Up @@ -222,6 +222,19 @@ mod tests {
assert_eq!(loaded.name_record()[2].name_id, 1030);
}

/// ensure we are counting characters and not bytes
#[test]
fn mac_str_length() {
let name = NameRecord::new(1, 0, 0, 9, String::from("cé").into());
let mut table = Name::default();
table.name_record.insert(name);
let bytes = crate::dump_table(&table).unwrap();
let load = crate::read::tables::name::Name::read(FontData::new(&bytes)).unwrap();

let data = load.name_record()[0].string(load.string_data()).unwrap();
assert_eq!(data.chars().collect::<String>(), "cé");
}

#[test]
fn roundtrip() {
#[rustfmt::skip]
Expand Down

0 comments on commit 8ab86d1

Please # to comment.