mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
- A font file that grew past the limits after the installed fonts were listed counts as skipped, and the bytes read from it count toward the 2 GiB a search reads. A suitcase font's resource fork is read the same way, no further than one byte past the limit. - The 500,000-glyph budget counts the glyphs an index draws (each matched glyph once), not the face's mapped characters. - The Retype panel's background search hands the command the loaded fonts that match as well as the installed fonts it found, so a loaded font that matched stays listed after the fonts found are loaded. - A shape that no glyph's key fits (more than 48 contours) no longer keeps type.retype reading installed files after the other shapes have matched.
176 lines
6.8 KiB
Rust
176 lines
6.8 KiB
Rust
//! Suitcase fonts: classic Mac OS font files whose data fork is empty and whose fonts are `sfnt`
|
|
//! resources in the resource fork (Microsoft Office installs its fonts this way in
|
|
//! `/Library/Fonts/Microsoft`). macOS lists them as installed; a reader of the file's data finds
|
|
//! nothing, so the scan reads the resource fork. The fork's layout is Apple's published resource
|
|
//! file format (Inside Macintosh: More Macintosh Toolbox, "Resource Manager").
|
|
|
|
use std::path::Path;
|
|
|
|
/// Caps on what a (possibly damaged) fork can make the scan read.
|
|
const MAX_FORK: u64 = 256 << 20;
|
|
const MAX_TYPES: usize = 1 << 12;
|
|
const MAX_FONTS: usize = 256;
|
|
|
|
fn be16(b: &[u8], at: usize) -> Option<usize> {
|
|
b.get(at..at.checked_add(2)?).and_then(|s| s.try_into().ok()).map(|s| u16::from_be_bytes(s) as usize)
|
|
}
|
|
|
|
fn be32(b: &[u8], at: usize) -> Option<usize> {
|
|
b.get(at..at.checked_add(4)?).and_then(|s| s.try_into().ok()).map(|s| u32::from_be_bytes(s) as usize)
|
|
}
|
|
|
|
/// The font files (`sfnt` resources) of a resource fork, in the order its map lists them. A
|
|
/// fork that isn't well formed gives the fonts read before the damage, or none.
|
|
pub(crate) fn sfnt_resources(fork: &[u8]) -> Vec<&[u8]> {
|
|
let mut fonts = Vec::new();
|
|
let _ = collect(fork, &mut fonts);
|
|
fonts
|
|
}
|
|
|
|
fn collect<'a>(fork: &'a [u8], fonts: &mut Vec<&'a [u8]>) -> Option<()> {
|
|
// The header: where the data and the map start.
|
|
let data = be32(fork, 0)?;
|
|
let map = fork.get(be32(fork, 4)?..)?;
|
|
// The map: its type list starts at the offset kept 24 bytes in, and counts from the map.
|
|
let types = be16(map, 24)?;
|
|
// Counts are stored less one, so a list of none is 0xFFFF.
|
|
let n_types = (be16(map, types)? + 1) & 0xFFFF;
|
|
let mut total = 0usize;
|
|
for t in 0..n_types.min(MAX_TYPES) {
|
|
let at = types + 2 + t * 8;
|
|
if map.get(at..at + 4)? != b"sfnt" {
|
|
continue;
|
|
}
|
|
let count = (be16(map, at + 4)? + 1) & 0xFFFF;
|
|
// Each reference: its data's offset is the low three bytes of the word 4 bytes in.
|
|
let refs = types + be16(map, at + 6)?;
|
|
for r in 0..count {
|
|
let start = data.checked_add(be32(map, refs + r * 12 + 4)? & 0x00FF_FFFF)?;
|
|
let len = be32(fork, start)?;
|
|
// References can share data: cap the total too, as every font is copied out.
|
|
total = total.checked_add(len)?;
|
|
if fonts.len() >= MAX_FONTS || total as u64 > MAX_FORK {
|
|
return Some(());
|
|
}
|
|
fonts.push(fork.get(start + 4..start.checked_add(4)?.checked_add(len)?)?);
|
|
}
|
|
}
|
|
Some(())
|
|
}
|
|
|
|
/// The fonts of the suitcase font at `path`, read from its resource fork; none for a file
|
|
/// without one, and off macOS, the only place such a fork is a part of a file.
|
|
pub(crate) fn read_fonts(path: &Path) -> Vec<Vec<u8>> {
|
|
let Some(fork) = read_fork(path) else { return vec![] };
|
|
sfnt_resources(&fork).into_iter().map(<[u8]>::to_vec).collect()
|
|
}
|
|
|
|
/// [`read_fonts`] reading the fork no further than one byte past `max_len` (at most the longest
|
|
/// fork read), with the bytes read: no fonts, and true, when the fork is longer.
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
pub(crate) fn read_fonts_within(path: &Path, max_len: u64) -> (Vec<Vec<u8>>, u64, bool) {
|
|
let max_len = max_len.min(MAX_FORK);
|
|
let Some(fork) = read_fork_within(path, max_len.saturating_add(1)) else { return (vec![], 0, false) };
|
|
let n = fork.len() as u64;
|
|
if n > max_len {
|
|
return (vec![], n, true);
|
|
}
|
|
(sfnt_resources(&fork).into_iter().map(<[u8]>::to_vec).collect(), n, false)
|
|
}
|
|
|
|
/// Whether the file at `path` is a suitcase font: nothing in its data fork and something in its
|
|
/// resource fork.
|
|
pub(crate) fn is_suitcase(path: &Path) -> bool {
|
|
// Off macOS no file has a fork: don't look at every font file twice.
|
|
cfg!(target_os = "macos") && std::fs::metadata(path).is_ok_and(|m| m.is_file() && m.len() == 0) && fork_len(path) > 0
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
fn fork_path(path: &Path) -> std::path::PathBuf {
|
|
path.join("..namedfork/rsrc")
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
pub(crate) fn fork_len(path: &Path) -> u64 {
|
|
std::fs::metadata(fork_path(path)).map_or(0, |m| m.len())
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
fn read_fork(path: &Path) -> Option<Vec<u8>> {
|
|
read_fork_within(path, MAX_FORK)
|
|
}
|
|
|
|
/// The first `max` bytes of the resource fork of the file at `path`.
|
|
#[cfg(target_os = "macos")]
|
|
fn read_fork_within(path: &Path, max: u64) -> Option<Vec<u8>> {
|
|
use std::io::Read;
|
|
let mut fork = Vec::new();
|
|
std::fs::File::open(fork_path(path)).ok()?.take(max).read_to_end(&mut fork).ok()?;
|
|
Some(fork)
|
|
}
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
pub(crate) fn fork_len(_: &Path) -> u64 {
|
|
0
|
|
}
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
fn read_fork(_: &Path) -> Option<Vec<u8>> {
|
|
let _ = MAX_FORK;
|
|
None
|
|
}
|
|
|
|
#[cfg(all(not(target_os = "macos"), not(target_arch = "wasm32")))]
|
|
fn read_fork_within(_: &Path, _: u64) -> Option<Vec<u8>> {
|
|
None
|
|
}
|
|
|
|
/// A resource fork holding `fonts` as `sfnt` resources and one other resource (a `vers`), as
|
|
/// a font utility writes them.
|
|
#[cfg(test)]
|
|
pub(crate) fn fork_of(fonts: &[Vec<u8>]) -> Vec<u8> {
|
|
let mut data = Vec::new();
|
|
let mut offsets = Vec::new();
|
|
for body in fonts.iter().map(Vec::as_slice).chain([&b"1.0"[..]]) {
|
|
offsets.push(data.len() as u32);
|
|
data.extend_from_slice(&(body.len() as u32).to_be_bytes());
|
|
data.extend_from_slice(body);
|
|
}
|
|
let n = fonts.len();
|
|
// The type list: `vers` (1 resource) then `sfnt`, each entry pointing at its references.
|
|
let list = 2 + 2 * 8;
|
|
let mut types = Vec::new();
|
|
types.extend_from_slice(&1u16.to_be_bytes());
|
|
types.extend_from_slice(b"vers");
|
|
types.extend_from_slice(&0u16.to_be_bytes());
|
|
types.extend_from_slice(&(list as u16).to_be_bytes());
|
|
types.extend_from_slice(b"sfnt");
|
|
types.extend_from_slice(&((n - 1) as u16).to_be_bytes());
|
|
types.extend_from_slice(&((list + 12) as u16).to_be_bytes());
|
|
let reference = |id: i16, offset: u32| {
|
|
let mut r = Vec::new();
|
|
r.extend_from_slice(&id.to_be_bytes());
|
|
r.extend_from_slice(&0xFFFFu16.to_be_bytes());
|
|
r.extend_from_slice(&offset.to_be_bytes());
|
|
r.extend_from_slice(&[0; 4]);
|
|
r
|
|
};
|
|
types.extend(reference(1, offsets[n]));
|
|
for (i, o) in offsets[..n].iter().enumerate() {
|
|
types.extend(reference(5000 + i as i16, *o));
|
|
}
|
|
// The map: a copy of the header, a handle, attributes, then the offsets of the lists.
|
|
let mut map = vec![0; 24];
|
|
map.extend_from_slice(&28u16.to_be_bytes());
|
|
map.extend_from_slice(&((28 + types.len()) as u16).to_be_bytes());
|
|
map.extend(types);
|
|
let mut fork = Vec::new();
|
|
for v in [256u32, 256 + data.len() as u32, data.len() as u32, map.len() as u32] {
|
|
fork.extend_from_slice(&v.to_be_bytes());
|
|
}
|
|
fork.resize(256, 0);
|
|
fork.extend(data);
|
|
fork.extend(map);
|
|
fork
|
|
}
|