Skip to content

Commit cc053e1

Browse files
committed
Respect ignore-invalid-name-records in writing as well
1 parent 23bd46c commit cc053e1

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "truetype"
3-
version = "0.48.1"
3+
version = "0.48.2"
44
edition = "2021"
55
license = "Apache-2.0/MIT"
66
authors = [

src/tables/names/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,25 +160,34 @@ impl Names {
160160
let mut data = vec![];
161161
let records = records
162162
.into_iter()
163-
.map(
163+
.filter_map(
164164
|((platform_id, encoding_id, language_id, name_id), value)| {
165165
let offset = data.len();
166-
encode(
166+
let result = encode(
167167
platform_id,
168168
encoding_id,
169169
language_id,
170170
value.as_ref(),
171171
&mut data,
172172
context,
173-
)?;
174-
Ok(Record {
173+
);
174+
if cfg!(feature = "ignore-invalid-name-records") {
175+
if let Err(_) = result {
176+
return None;
177+
}
178+
} else {
179+
if let Err(error) = result {
180+
return Some(Err(error));
181+
}
182+
}
183+
Some(Ok(Record {
175184
platform_id,
176185
encoding_id,
177186
language_id,
178187
name_id,
179188
size: (data.len() - offset) as _,
180189
offset: offset as _,
181-
})
190+
}))
182191
},
183192
)
184193
.collect::<Result<Vec<_>>>()?;
115 KB
Binary file not shown.

tests/names.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,46 @@
11
#[macro_use]
22
mod support;
33

4+
mod aoboshi_one {
5+
use std::fs::File;
6+
use std::io::Cursor;
7+
8+
use truetype::tables::names::Names;
9+
use truetype::tape::Read as TapeRead;
10+
use truetype::tape::Write;
11+
use truetype::value::Read as ValueRead;
12+
13+
#[test]
14+
#[cfg_attr(not(feature = "ignore-invalid-name-records"), should_panic)]
15+
fn write() {
16+
let path = crate::support::Fixture::AoboshiOne.path();
17+
let offset = crate::support::Fixture::AoboshiOne.offset("name");
18+
let mut file = ok!(File::open(path));
19+
20+
ok!(file.jump(offset));
21+
let table = ok!(Names::read(&mut file));
22+
23+
let records = table
24+
.iter()
25+
.map(|(ids, value)| (ids, value.unwrap_or_default()));
26+
let language_tags = table.language_tags().map(Option::unwrap_or_default);
27+
let table = ok!(Names::from_iter(
28+
records,
29+
language_tags,
30+
&mut Default::default(),
31+
));
32+
33+
let mut cursor = Cursor::new(vec![]);
34+
ok!(cursor.give(&table));
35+
}
36+
}
37+
438
mod css_test {
539
use truetype::tables::names::Names;
640
use truetype::value::Read;
741

8-
#[cfg_attr(not(feature = "ignore-invalid-language-ids"), should_panic)]
942
#[test]
43+
#[cfg_attr(not(feature = "ignore-invalid-language-ids"), should_panic)]
1044
fn read() {
1145
let table = ok!(Names::read(&mut setup!(CSSTest, "name")));
1246
let _: Vec<_> = table.iter().collect();
@@ -25,7 +59,7 @@ mod open_sans {
2559
#[test]
2660
fn read() {
2761
let table = ok!(Names::read(&mut setup!(OpenSans, "name")));
28-
test(&table);
62+
assert(&table);
2963
}
3064

3165
#[test]
@@ -45,7 +79,7 @@ mod open_sans {
4579
language_tags,
4680
&mut Default::default(),
4781
));
48-
test(&other);
82+
assert(&other);
4983
match (&one, &other) {
5084
(Names::Format0(one), Names::Format0(other)) => {
5185
assert_eq!(one.data.len(), other.data.len() - 48);
@@ -58,7 +92,7 @@ mod open_sans {
5892
assert_eq!(size, cursor.into_inner().len() - 48);
5993
}
6094

61-
fn test(table: &Names) {
95+
fn assert(table: &Names) {
6296
let records = table.iter().collect::<Vec<_>>();
6397
let name_ids = records
6498
.iter()

tests/support/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ macro_rules! setup(
1717
#[allow(dead_code)]
1818
#[derive(Clone, Copy, Debug)]
1919
pub enum Fixture {
20+
AoboshiOne,
2021
BungeeColor,
2122
CSSTest,
2223
Englebert,
@@ -32,6 +33,7 @@ pub enum Fixture {
3233
impl Fixture {
3334
pub fn file_name(&self) -> &'static str {
3435
match *self {
36+
Fixture::AoboshiOne => "AoboshiOne-Regular.ttf",
3537
Fixture::BungeeColor => "BungeeColor-Regular.ttf",
3638
Fixture::CSSTest => "csstest-basic-regular.ttf",
3739
Fixture::Englebert => "Englebert-Regular.ttf",
@@ -52,6 +54,10 @@ impl Fixture {
5254

5355
pub fn offset(&self, table: &str) -> u64 {
5456
match *self {
57+
Fixture::AoboshiOne => match table {
58+
"name" => 105912,
59+
_ => unreachable!(),
60+
},
5561
Fixture::BungeeColor => match table {
5662
"OS/2" => 376,
5763
_ => unreachable!(),

0 commit comments

Comments
 (0)