mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
ps: preserve reencoded glyph names in fallback fonts
This commit is contained in:
1 parent
7d39dc4df2
commit
808730f49b
1 file changed
+65
-2
@@ -43,9 +43,27 @@ fn substitute_type42(ip: &mut Interp, font_name: &[u8], bytes: &[u8]) -> R<Optio
|
||||
let Ok(font) = skrifa::FontRef::new(bytes) else { return Ok(None) };
|
||||
let cmap = font.charmap();
|
||||
let charstrings = PDict::new(256);
|
||||
for glyph_name in crate::read::encodings::standard() {
|
||||
// A reencoded font may name any glyph in its bundled substitute, not only
|
||||
// StandardEncoding. Prefer the font's own names; cmap aliases also support
|
||||
// fonts whose post table deliberately omits glyph names.
|
||||
for (gid, glyph_name) in font.glyph_names().iter() {
|
||||
ip.charge(1)?;
|
||||
if glyph_name.is_synthesized() {
|
||||
continue;
|
||||
}
|
||||
let bytes = glyph_name.as_str().as_bytes();
|
||||
ip.alloc(bytes.len().saturating_add(128))?;
|
||||
charstrings.put(Obj::lit(bytes), Obj::Int(i64::from(gid.to_u32())));
|
||||
}
|
||||
for glyph_name in crate::read::encodings::standard().into_iter().chain(crate::read::encodings::iso_latin1()) {
|
||||
ip.charge(1)?;
|
||||
let key = Key::Name(name(glyph_name.as_bytes()));
|
||||
if charstrings.known(&key) {
|
||||
continue;
|
||||
}
|
||||
let Some(ch) = standard_glyph_char(glyph_name.as_bytes()) else { continue };
|
||||
let Some(gid) = cmap.map(ch) else { continue };
|
||||
ip.alloc(glyph_name.len().saturating_add(128))?;
|
||||
charstrings.put(Obj::lit(glyph_name.as_bytes()), Obj::Int(i64::from(gid.to_u32())));
|
||||
}
|
||||
ip.alloc(bytes.len().saturating_add(bytes.len() % 2))?;
|
||||
@@ -171,7 +189,14 @@ fn standard_glyph_char(name: &[u8]) -> Option<char> {
|
||||
b"oslash" => 'ø',
|
||||
b"oe" => 'œ',
|
||||
b"germandbls" => 'ß',
|
||||
_ => return None,
|
||||
_ => {
|
||||
return crate::read::encodings::iso_latin1()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.skip(160)
|
||||
.find(|(_, glyph)| glyph.as_bytes() == name)
|
||||
.and_then(|(code, _)| char::from_u32(code as u32));
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -747,5 +772,43 @@ mod tests {
|
||||
assert_eq!(standard_glyph_char(b"quoteleft"), Some('‘'));
|
||||
assert_eq!(standard_glyph_char(b"quoteright"), Some('’'));
|
||||
assert_eq!(standard_glyph_char(b"unmapped.custom"), None);
|
||||
assert_eq!(standard_glyph_char(b"eacute"), Some('é'));
|
||||
assert_eq!(standard_glyph_char(b"Adieresis"), Some('Ä'));
|
||||
assert_eq!(standard_glyph_char(b"space"), Some(' '));
|
||||
}
|
||||
/// A tiny generated SFNT directory with named glyphs, not a copied font asset.
|
||||
fn named_font() -> Vec<u8> {
|
||||
let maxp = [0, 0, 0x50, 0, 0, 3];
|
||||
let mut post = vec![0u8; 32];
|
||||
post[..4].copy_from_slice(&[0, 2, 0, 0]);
|
||||
post.extend_from_slice(&[0, 3, 0, 0, 1, 2, 1, 3]);
|
||||
for n in [b"eacute".as_slice(), b"custom.accent"] {
|
||||
post.push(n.len() as u8);
|
||||
post.extend_from_slice(n);
|
||||
}
|
||||
let mut data = vec![0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0];
|
||||
for (tag, offset, len) in [(b"maxp", 44u32, maxp.len()), (b"post", 52u32, post.len())] {
|
||||
data.extend_from_slice(tag);
|
||||
data.extend_from_slice(&0u32.to_be_bytes());
|
||||
data.extend_from_slice(&offset.to_be_bytes());
|
||||
data.extend_from_slice(&(len as u32).to_be_bytes());
|
||||
}
|
||||
data.extend_from_slice(&maxp);
|
||||
data.resize(52, 0);
|
||||
data.extend_from_slice(&post);
|
||||
data
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn substitute_resolves_reencoded_names_and_charges_budget() -> Result<(), String> {
|
||||
let bytes = named_font();
|
||||
let mut ip = Interp::new(vec![], (0.0, 0.0), Default::default());
|
||||
let font = substitute_type42(&mut ip, b"Fallback", &bytes).map_err(|e| format!("{e:?}"))?.ok_or("fixture font rejected")?;
|
||||
assert_eq!(crate::read::type42::glyph_index(&font, b"eacute"), 1);
|
||||
assert_eq!(crate::read::type42::glyph_index(&font, b"custom.accent"), 2);
|
||||
assert_eq!(crate::read::type42::glyph_index(&font, b"unknown"), 0);
|
||||
let mut limited = Interp::new(vec![], (0.0, 0.0), crate::read::Limits { steps: 0, ..Default::default() });
|
||||
assert!(substitute_type42(&mut limited, b"Fallback", &bytes).is_err());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user