diff --git a/crates/engine/src/cmd/create.rs b/crates/engine/src/cmd/create.rs index 6ec7d4e59..3a9f6d67f 100644 --- a/crates/engine/src/cmd/create.rs +++ b/crates/engine/src/cmd/create.rs @@ -362,15 +362,13 @@ fn free_spot(s: &Session, w: f64, h: f64) -> Result<(f64, f64)> { }) } -/// The bytes of the file at `path`, within the session's allowed file roots. #[cfg(not(target_arch = "wasm32"))] -pub(super) fn read_path(path: &str) -> Result> { +fn read_path(path: &str) -> Result> { crate::file_access::read(path) } -/// The browser has no files to read: always an error. #[cfg(target_arch = "wasm32")] -pub(super) fn read_path(path: &str) -> Result> { +fn read_path(path: &str) -> Result> { Err(crate::EngineError::Other(format!("{path}: files can't be read in the browser"))) } diff --git a/crates/engine/src/cmd/text.rs b/crates/engine/src/cmd/text.rs index 0f0f003de..6e1f5c3db 100644 --- a/crates/engine/src/cmd/text.rs +++ b/crates/engine/src/cmd/text.rs @@ -497,7 +497,7 @@ pub fn specs() -> Vec { let b64 = b64.split_once(',').map(|(_, d)| d.to_string()).unwrap_or(b64); base64::engine::general_purpose::STANDARD.decode(b64.trim()).map_err(|e| bad(cmd, format!("`data`: {e}")))? } else if let Some(path) = string(p, "path") { - super::create::read_path(&path)? + super::file::read_capped(&path, figcraft_text::fontdb::MAX_FONT_FILE)? } else { return Err(bad(cmd, "pass `path` or `data` (base64)")); }; diff --git a/crates/text/src/fontdb.rs b/crates/text/src/fontdb.rs index 814d3a2f7..604f3eee5 100644 --- a/crates/text/src/fontdb.rs +++ b/crates/text/src/fontdb.rs @@ -819,13 +819,19 @@ fn scan_dir(dir: &std::path::Path, depth: usize, out: &mut BTreeMap MAX_FONT_FILE { + continue; + } + let Some(bytes) = usize::try_from(meta.len()).ok().and_then(|l| index_bytes(&path, l)) else { continue }; for (index, family, style) in faces_in(&bytes) { out.entry(family.to_lowercase()).or_insert_with(|| (family.clone(), Vec::new())).1.push(Pending { path: path.clone(), index, style }); } } } +/// The largest font file read (CJK collections run to ~120 MB, Apple Color Emoji ~180 MB). +pub const MAX_FONT_FILE: u64 = 256 * 1024 * 1024; + /// The parts of a font file the scan reads: headers, table directories and `name` tables, at their /// offsets in a zeroed buffer of the file's length (reading whole files was most of the scan). fn index_bytes(path: &std::path::Path, len: usize) -> Option> {