diff --git a/crates/io/Cargo.toml b/crates/io/Cargo.toml index 92468c779..790b37ba8 100644 --- a/crates/io/Cargo.toml +++ b/crates/io/Cargo.toml @@ -39,3 +39,7 @@ rustix = { version = "1", default-features = false, features = ["std", "fs"] } cadmpeg-core = { workspace = true } cadmpeg-ir = { workspace = true } cadmpeg-registry = { workspace = true } + +[dev-dependencies] +# P2 PDF prototype tests only; the reviewed pure-Rust subsetter, no variable-font stack. +subsetter = { version = "=0.2.6", default-features = false } diff --git a/crates/io/src/paper/pdf.rs b/crates/io/src/paper/pdf.rs index 6ee0e8b17..b9bb8d0e9 100644 --- a/crates/io/src/paper/pdf.rs +++ b/crates/io/src/paper/pdf.rs @@ -26,6 +26,19 @@ //! buffer, including inside a path/arc and the final graphics-state restore. Allocation //! failure is an error. The limit is for uncompressed content, not total export memory. +// P2 prototypes are registered only for their unit/acceptance tests. They do not +// participate in paper_pdf or provide B1, page text or production font resources. +#[cfg(test)] +mod font_admission; +#[cfg(test)] +mod font_objects; +#[cfg(test)] +mod glyph_closure; +#[cfg(test)] +mod subset_font; +#[cfg(test)] +mod to_unicode; + use std::f64::consts::FRAC_PI_2; use std::io::Write as _; diff --git a/crates/io/src/paper/pdf/font_admission.rs b/crates/io/src/paper/pdf/font_admission.rs new file mode 100644 index 000000000..632d21b81 --- /dev/null +++ b/crates/io/src/paper/pdf/font_admission.rs @@ -0,0 +1,88 @@ +//! Admission of the one reviewed Liberation Sans resource for future PDF use. +//! This checks resource identity and embedding metadata only, not glyph layout, +//! font validity in general, licence compliance of a subset, or a B1 record. +//! OS/2 reference: https://learn.microsoft.com/en-us/typography/opentype/spec/os2#fstype +use sha2::{Digest, Sha256}; +use crate::{IoError, Result}; + +const FONT_BYTES: usize = 410_820; +const MAX_TABLES: usize = 19; +const FONT_HASH: [u8; 32] = [0x07, 0x5e, 0x71, 0xbe, 0x4c, 0x58, 0xbe, 0xda, 0x17, 0x2e, 0xcb, 0x57, 0x4a, 0x79, 0x93, 0x08, 0xf0, 0x4c, 0x3d, 0x05, 0x4c, 0x6a, 0x5b, 0x74, 0x34, 0x95, 0x4d, 0x3b, 0x0f, 0x1b, 0x28, 0xd8]; + +/// Only the reviewed byte revision, face zero, and its installable embedding +/// flags are admitted. No name/path lookup, fallback, filesystem or cache access. +/// Nonzero flags are outside this single-resource policy, not a declaration +/// that every other OpenType embedding mode is prohibited by the specification. +pub(super) fn check_liberation_sans(bytes: &[u8], face_index: u32) -> Result<()> { + if face_index != 0 || bytes.len() != FONT_BYTES { + return Err(invalid("unapproved bundled font identity")); + } + let hash: [u8; 32] = Sha256::digest(bytes).into(); + if hash != FONT_HASH { + return Err(invalid("unapproved bundled font identity")); + } + reviewed_embedding_metadata(bytes) +} + +fn invalid(message: &str) -> IoError { + IoError::Invalid(format!("PDF bundled font: {message}")) +} + +fn u16_at(bytes: &[u8], at: usize) -> Result { + let end = at.checked_add(2).ok_or_else(|| invalid("table range overflow"))?; + let pair = bytes.get(at..end).and_then(|s| s.try_into().ok()).ok_or_else(|| invalid("truncated table"))?; + Ok(u16::from_be_bytes(pair)) +} + +fn u32_at(bytes: &[u8], at: usize) -> Result { + let end = at.checked_add(4).ok_or_else(|| invalid("table range overflow"))?; + let word = bytes.get(at..end).and_then(|s| s.try_into().ok()).ok_or_else(|| invalid("truncated table"))?; + Ok(u32::from_be_bytes(word)) +} + +// Bounded metadata reader, also tested independently so digest refusal cannot +// hide malformed-range or permission-check defects. No font data is modified. +fn reviewed_embedding_metadata(bytes: &[u8]) -> Result<()> { + if bytes.len() > FONT_BYTES || bytes.get(..4) != Some(&[0, 1, 0, 0]) { + return Err(invalid("unsupported font container")); + } + let count = usize::from(u16_at(bytes, 4)?); + if count == 0 || count > MAX_TABLES { + return Err(invalid("table count exceeds reviewed envelope")); + } + let end = count.checked_mul(16).and_then(|n| n.checked_add(12)).ok_or_else(|| invalid("table range overflow"))?; + if bytes.get(..end).is_none() { + return Err(invalid("truncated table directory")); + } + let mut os2 = None; + for i in 0..count { + // count <= 19 proves these directory offsets cannot overflow. + let record = 12 + 16 * i; + let start = usize::try_from(u32_at(bytes, record + 8)?).map_err(|_| invalid("table offset exceeds platform"))?; + let length = usize::try_from(u32_at(bytes, record + 12)?).map_err(|_| invalid("table length exceeds platform"))?; + let stop = start.checked_add(length).ok_or_else(|| invalid("table range overflow"))?; + if start < end || bytes.get(start..stop).is_none() { + return Err(invalid("table outside font payload")); + } + if bytes.get(record..record + 4) == Some(b"OS/2") { + if os2.is_some() { + return Err(invalid("duplicate OS/2 table")); + } + os2 = bytes.get(start..stop); + } + } + let table = os2.ok_or_else(|| invalid("missing OS/2 table"))?; + // The current approved resource has OS/2 v3, length 96 and fsType zero. + // Legacy flag interpretations and other revisions are not admitted here. + if table.len() != 96 || u16_at(table, 0)? != 3 { + return Err(invalid("unsupported OS/2 revision")); + } + if u16_at(table, 8)? != 0 { + return Err(invalid("embedding flags outside reviewed policy")); + } + Ok(()) +} + +#[cfg(test)] +#[path = "font_admission_tests.rs"] +mod tests; diff --git a/crates/io/src/paper/pdf/font_admission_tests.rs b/crates/io/src/paper/pdf/font_admission_tests.rs new file mode 100644 index 000000000..6b3660590 --- /dev/null +++ b/crates/io/src/paper/pdf/font_admission_tests.rs @@ -0,0 +1,87 @@ +//! Original SFNT metadata fixtures; no system font or external test asset. +use super::*; + +const BUNDLED: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../assets/fonts/liberation-sans/LiberationSans-Regular.ttf")); + +fn metadata_fixture(flags: u16) -> Vec { + let mut bytes = vec![0; 124]; + bytes[..4].copy_from_slice(&[0, 1, 0, 0]); + bytes[4..6].copy_from_slice(&1_u16.to_be_bytes()); + bytes[12..16].copy_from_slice(b"OS/2"); + bytes[20..24].copy_from_slice(&28_u32.to_be_bytes()); + bytes[24..28].copy_from_slice(&96_u32.to_be_bytes()); + bytes[28..30].copy_from_slice(&3_u16.to_be_bytes()); + bytes[36..38].copy_from_slice(&flags.to_be_bytes()); + bytes +} + +#[test] +fn current_bundled_revision_and_face_zero_are_admitted_without_resolution() { + assert_eq!(BUNDLED.len(), 410_820); + assert!(check_liberation_sans(BUNDLED, 0).is_ok()); + assert!(reviewed_embedding_metadata(BUNDLED).is_ok()); + assert!(check_liberation_sans(BUNDLED, 1).is_err()); + assert!(check_liberation_sans(BUNDLED, u32::MAX).is_err()); +} + +#[test] +fn metadata_fixture_is_not_an_approved_font_resource() { + assert!(reviewed_embedding_metadata(&metadata_fixture(0)).is_ok()); + assert!(check_liberation_sans(&metadata_fixture(0), 0).is_err()); + let mut changed = BUNDLED.to_vec(); + let last = changed.len() - 1; + changed[last] ^= 1; + assert!(check_liberation_sans(&changed, 0).is_err()); + assert!(check_liberation_sans(&BUNDLED[..BUNDLED.len() - 1], 0).is_err()); + changed.push(0); + assert!(check_liberation_sans(&changed, 0).is_err()); +} + +#[test] +fn nonzero_permissions_and_restrictions_are_outside_the_one_resource_policy() { + for flags in [0x0001, 0x0002, 0x0004, 0x0008, 0x000c, 0x0010, 0x0100, 0x0200, 0x0300, 0x8000, 0xffff] { + let bytes = metadata_fixture(flags); + let before = bytes.clone(); + assert!(reviewed_embedding_metadata(&bytes).is_err(), "flags {flags:04x}"); + assert_eq!(bytes, before); + } +} + +#[test] +fn missing_truncated_or_out_of_envelope_metadata_is_refused() { + let valid = metadata_fixture(0); + for length in 0..valid.len() { + assert!(reviewed_embedding_metadata(&valid[..length]).is_err(), "length {length}"); + } + for (at, value) in [(0, 0x74), (4, 1), (5, 0), (5, 20), (12, b'X'), (28, 1), (29, 2)] { + let mut bytes = valid.clone(); + bytes[at] = value; + assert!(reviewed_embedding_metadata(&bytes).is_err(), "byte {at} value {value}"); + } + assert!(reviewed_embedding_metadata(&vec![0; FONT_BYTES + 1]).is_err()); +} + +#[test] +fn hostile_directory_offsets_and_lengths_never_become_table_slices() { + for (offset, length) in [(0, 96), (27, 96), (28, 95), (28, 97), (29, 96), (u32::MAX, 96), (28, u32::MAX)] { + let mut bytes = metadata_fixture(0); + bytes[20..24].copy_from_slice(&offset.to_be_bytes()); + bytes[24..28].copy_from_slice(&length.to_be_bytes()); + assert!(reviewed_embedding_metadata(&bytes).is_err(), "offset {offset}, length {length}"); + } +} + +#[test] +fn duplicate_permission_tables_are_refused_instead_of_selecting_one() { + let one = metadata_fixture(0); + let mut bytes = vec![0; 140]; + bytes[..12].copy_from_slice(&one[..12]); + bytes[4..6].copy_from_slice(&2_u16.to_be_bytes()); + for start in [12, 28] { + bytes[start..start + 4].copy_from_slice(b"OS/2"); + bytes[start + 8..start + 12].copy_from_slice(&44_u32.to_be_bytes()); + bytes[start + 12..start + 16].copy_from_slice(&96_u32.to_be_bytes()); + } + bytes[44..].copy_from_slice(&one[28..]); + assert!(reviewed_embedding_metadata(&bytes).is_err()); +} diff --git a/crates/io/src/paper/pdf/font_objects.rs b/crates/io/src/paper/pdf/font_objects.rs new file mode 100644 index 000000000..0c235beba --- /dev/null +++ b/crates/io/src/paper/pdf/font_objects.rs @@ -0,0 +1,822 @@ +//! Private PDF font resources for one admitted subset; no B1 or content writer. +//! +//! Identity-H explicitly maps two-byte codes to CIDs. A separate stream maps +//! CIDs to subset GIDs; logical text remains keyed by code in real ToUnicode. +//! Descriptor metrics must come from the SAME admitted source font. Numeric +//! validation does not prove that provenance, and no metrics are estimated. +//! +//! pdf-writer 0.15.0 allocates internally without fallible allocation controls. +//! This returns a local Chunk, never mutating a caller-owned Pdf on Result errors. +//! A later extend has no rollback or OOM guarantee. Caller owns global Ref/tag +//! reservations, page resources, content, positioning and document budgets. + +use pdf_writer::types::{CidFontType, FontFlags, SystemInfo}; +use pdf_writer::{Chunk, Name, Rect, Ref, Str}; + +use super::subset_font::SubsetFont; +use super::to_unicode::write_to_unicode; +use crate::{IoError, MAX_DRAWING_BYTES, Result}; + +const MAX_REFS_ID: i32 = 100_000; +const MAX_CODES: usize = 65_536; +const MAX_GLYPHS: usize = 16_384; +const MAX_FONT_BYTES: usize = 410_820; +const MAX_MAP_BYTES: usize = 131_072; +const MAX_UTF8_BYTES: usize = 4 << 20; +const MAX_CLUSTER_BYTES: usize = 768; +const MAX_CHUNK_BYTES: usize = 16 << 20; +const MAX_METRIC: f32 = 1_000_000.0; +const ROW_BYTES: usize = 128; +const FIXED_BYTES: usize = 32 << 10; +const POSTSCRIPT_NAME: &[u8] = b"LiberationSans"; + +/// Six already reserved real references. Local validation cannot reserve them +/// globally or discover collisions with future P3/font objects. +pub(super) struct FontObjectRefs { + pub(super) type0: Ref, + pub(super) cid_font: Ref, + pub(super) descriptor: Ref, + pub(super) font_file2: Ref, + pub(super) to_unicode: Ref, + pub(super) cid_to_gid: Ref, +} + +impl FontObjectRefs { + fn all(&self) -> [Ref; 6] { + [self.type0, self.cid_font, self.descriptor, self.font_file2, self.to_unicode, self.cid_to_gid] + } + + fn validate(&self) -> Result<()> { + let ids = self.all(); + for (index, id) in ids.iter().enumerate() { + if !(6..=MAX_REFS_ID).contains(&id.get()) || ids.iter().take(index).any(|prior| prior == id) { + return Err(invalid("font object references must be distinct, outside core 1..5, and at most 100000")); + } + } + Ok(()) + } +} + +/// A caller-assigned two-byte code, interpreted as a CID by Identity-H. +/// Neither code nor logical text is inferred from subset_gid. +pub(super) struct CodeToGlyph<'a> { + pub(super) code: u16, + pub(super) subset_gid: u16, + pub(super) logical: &'a str, +} + +/// Explicit PDF descriptor values, not an existing B1/provider record. +/// Lengths use 1000-unit glyph space; italic angle uses degrees. +/// Caller supplies faithful source metrics; no StemV/ascent defaults exist here. +pub(super) struct DescriptorMetrics { + pub(super) bbox_1000: [f32; 4], + pub(super) ascent_1000: f32, + pub(super) descent_1000: f32, + pub(super) cap_height_1000: f32, + pub(super) italic_angle_degrees: f32, + pub(super) stem_v_1000: f32, + pub(super) flags: u32, +} + +impl DescriptorMetrics { + fn validate(&self) -> Result { + let numbers = [ + self.ascent_1000, self.descent_1000, self.cap_height_1000, + self.italic_angle_degrees, self.stem_v_1000, + ]; + if self.bbox_1000.iter().chain(numbers.iter()).any(|n| { + !n.is_finite() || n.abs() > MAX_METRIC || (*n != 0.0 && n.abs() < f32::MIN_POSITIVE) + }) { + return Err(invalid("font descriptor numbers must be finite and bounded")); + } + let [left, bottom, right, top] = self.bbox_1000; + if left >= right || bottom >= top || self.ascent_1000 <= 0.0 + || self.descent_1000 >= 0.0 || self.cap_height_1000 <= 0.0 + || self.cap_height_1000 > self.ascent_1000 || self.stem_v_1000 <= 0.0 + || self.italic_angle_degrees.abs() > 90.0 + { + return Err(invalid("font descriptor has invalid bounds, signs, cap height or stem")); + } + let flags = FontFlags::from_bits(self.flags).ok_or_else(|| invalid("font descriptor contains reserved flags"))?; + let symbolic = flags.contains(FontFlags::SYMBOLIC); + let nonsymbolic = flags.contains(FontFlags::NON_SYMBOLIC); + if symbolic == nonsymbolic { + return Err(invalid("font descriptor must select exactly one Symbolic or Nonsymbolic flag")); + } + Ok(flags) + } +} + +/// Pure IO parameters, not a fabricated B1 record or global resource allocator. +pub(super) struct FontObjectInput<'a> { + pub(super) subset: &'a SubsetFont, + pub(super) codes: &'a [CodeToGlyph<'a>], + pub(super) descriptor: Option<&'a DescriptorMetrics>, + pub(super) postscript_name: &'a [u8], + pub(super) subset_tag: [u8; 6], + pub(super) max_chunk_bytes: usize, +} + +fn invalid(message: &'static str) -> IoError { + IoError::Invalid(message.into()) +} + +fn part(bytes: &[u8], at: usize, len: usize) -> Result<&[u8]> { + let end = at.checked_add(len).ok_or_else(|| invalid("font metric offset overflow"))?; + bytes.get(at..end).ok_or_else(|| invalid("truncated font metric data")) +} + +fn word(bytes: &[u8], at: usize) -> Result { + Ok(u16::from_be_bytes(part(bytes, at, 2)?.try_into().map_err(|_| invalid("invalid font metric word"))?)) +} + +fn dword(bytes: &[u8], at: usize) -> Result { + let raw = u32::from_be_bytes(part(bytes, at, 4)?.try_into().map_err(|_| invalid("invalid font metric offset"))?); + usize::try_from(raw).map_err(|_| invalid("font metric offset exceeds platform")) +} + +/// Small bounded table view over the already validated, immutable SubsetFont. +/// This does not repeat admission, glyph closure or composite parsing. +fn table<'a>(bytes: &'a [u8], tag: &[u8; 4]) -> Result<&'a [u8]> { + let count = usize::from(word(bytes, 4)?); + if count == 0 || count > 19 { + return Err(invalid("font metric table count exceeds the admitted subset contract")); + } + part(bytes, 0, 12 + count * 16)?; // count <= 19. + for index in 0..count { + let record = 12 + index * 16; + if part(bytes, record, 4)? == tag { + return part(bytes, dword(bytes, record + 8)?, dword(bytes, record + 12)?); + } + } + Err(invalid("subset lacks a required metric table")) +} + +struct MetricView<'a> { + hmtx: &'a [u8], + glyphs: usize, + full_metrics: usize, + units_per_em: u16, +} + +impl<'a> MetricView<'a> { + fn read(subset: &'a SubsetFont) -> Result { + let glyphs = subset.mapping().len(); + if glyphs == 0 || glyphs > MAX_GLYPHS || subset.bytes().len() > MAX_FONT_BYTES { + return Err(invalid("font subset exceeds the admitted fixed-resource contract")); + } + let mut previous = None; + for (expected, &(old, new)) in subset.mapping().iter().enumerate() { + if previous.is_some_and(|prior| old <= prior) || usize::from(new) != expected { + return Err(invalid("font subset mapping is not ordered and dense")); + } + if expected == 0 && old != 0 { + return Err(invalid("font subset lacks original glyph zero")); + } + previous = Some(old); + } + let bytes = subset.bytes(); + let head = table(bytes, b"head")?; + let hhea = table(bytes, b"hhea")?; + let hmtx = table(bytes, b"hmtx")?; + let maxp = table(bytes, b"maxp")?; + if usize::from(word(maxp, 4)?) != glyphs { + return Err(invalid("font subset glyph count differs from its mapping")); + } + let units_per_em = word(head, 18)?; + if units_per_em != 2048 { + return Err(invalid("font subset units per em differ from the one admitted font")); + } + let full_metrics = usize::from(word(hhea, 34)?); + if full_metrics == 0 || full_metrics > glyphs { + return Err(invalid("subset has invalid numberOfHMetrics")); + } + let expected = full_metrics.checked_mul(4) + .and_then(|full| (glyphs - full_metrics).checked_mul(2).and_then(|tail| full.checked_add(tail))) + .ok_or_else(|| invalid("subset metric length overflow"))?; + if hmtx.len() != expected { + return Err(invalid("subset hmtx length does not match its glyph metrics")); + } + Ok(Self { hmtx, glyphs, full_metrics, units_per_em }) + } + + fn advance(&self, gid: u16) -> Result { + let gid = usize::from(gid); + if gid >= self.glyphs { + return Err(invalid("PDF code maps outside the subset glyph range")); + } + let index = gid.min(self.full_metrics - 1); // full_metrics checked > 0. + let advance = word(self.hmtx, index.checked_mul(4).ok_or_else(|| invalid("font advance offset overflow"))?)?; + let width = (f64::from(advance) * 1000.0 / f64::from(self.units_per_em)) as f32; + if !width.is_finite() || width.abs() > MAX_METRIC { + return Err(invalid("PDF glyph width is not finite and bounded")); + } + Ok(width) + } +} + +fn fixed_font_name(name: &[u8], tag: [u8; 6]) -> Result<[u8; 21]> { + if name != POSTSCRIPT_NAME || !tag.iter().all(u8::is_ascii_uppercase) { + return Err(invalid("PDF font requires LiberationSans and a caller-supplied six-uppercase subset tag")); + } + let mut result = *b"AAAAAA+LiberationSans"; + for (slot, byte) in result.iter_mut().take(6).zip(tag) { + *slot = byte; + } + Ok(result) +} + +/// Conservative byte bound for this exact six-object call sequence/settings. +/// W rows: at most 5 CID digits + 64 finite-f32 bytes + punctuation/spaces < 128. +/// Fixed dictionaries/three streams: 31 pairs including nested SystemInfo, +/// keys <= 16, scalar/name/ref tokens <= 64, bbox value < 262. Each fixed +/// pair costs < 512 bytes; all headers/trailers < 4096. Thus fixed bytes +/// < 31*512+4096 = 19968 < 32768, excluding W rows and raw stream payloads. +/// Stream payload bytes are counted separately, exactly once each in Chunk. +/// This bounds serialized bytes, not Vec capacity, transient memory or OOM. +fn byte_bound(font: usize, cmap: usize, map: usize, rows: usize) -> Result { + rows.checked_mul(ROW_BYTES).and_then(|n| n.checked_add(FIXED_BYTES)) + .and_then(|n| n.checked_add(font)).and_then(|n| n.checked_add(cmap)) + .and_then(|n| n.checked_add(map)).ok_or_else(|| invalid("font object byte budget overflow")) +} + +struct Prepared<'a> { + font: &'a [u8], + unicode: Vec, + cid_map: Vec, + widths: Vec<(u16, f32)>, + default_width: f32, + name: [u8; 21], + descriptor: &'a DescriptorMetrics, + flags: FontFlags, + limit: usize, + bound: usize, +} + +fn prepare(input: FontObjectInput<'_>) -> Result> { + let descriptor = input.descriptor.ok_or_else(|| invalid("essential font descriptor metrics are missing"))?; + let flags = descriptor.validate()?; + let name = fixed_font_name(input.postscript_name, input.subset_tag)?; + if input.codes.len() > MAX_CODES { + return Err(invalid("PDF font code count exceeds 65536")); + } + let metrics = MetricView::read(input.subset)?; + let limit = input.max_chunk_bytes.min(MAX_CHUNK_BYTES).min(MAX_DRAWING_BYTES); + let max_cid = input.codes.last().map_or(0, |row| usize::from(row.code)); + let map_len = max_cid.checked_add(1).and_then(|n| n.checked_mul(2)) + .filter(|n| *n <= MAX_MAP_BYTES).ok_or_else(|| invalid("CID to GID map byte limit exceeded"))?; + let without_unicode = byte_bound(input.subset.bytes().len(), 0, map_len, input.codes.len())?; + let unicode_allowance = limit.checked_sub(without_unicode).ok_or_else(|| invalid("font objects exceed their preflight byte allowance"))?; + + let (mut previous, mut utf8) = (None, 0usize); + // Counts/order/ranges/lengths before allocating code-derived buffers. + for row in input.codes { + if previous.is_some_and(|prior| row.code <= prior) { + return Err(invalid("PDF font codes must be strictly increasing and unique")); + } + if row.code == 0 && row.subset_gid != 0 { + return Err(invalid("CID zero must map to subset glyph zero")); + } + metrics.advance(row.subset_gid)?; + if row.logical.is_empty() || row.logical.len() > MAX_CLUSTER_BYTES { + return Err(invalid("PDF font code requires a nonempty bounded literal cluster")); + } + utf8 = utf8.checked_add(row.logical.len()).filter(|n| *n <= MAX_UTF8_BYTES) + .ok_or_else(|| invalid("PDF font logical text input exceeds its byte limit"))?; + previous = Some(row.code); + } + + let mut pairs = Vec::new(); + pairs.try_reserve_exact(input.codes.len()).map_err(|_| invalid("cannot allocate PDF ToUnicode pairs"))?; + let mut widths = Vec::new(); + widths.try_reserve_exact(input.codes.len()).map_err(|_| invalid("cannot allocate PDF CID widths"))?; + let mut cid_map = Vec::new(); + cid_map.try_reserve_exact(map_len).map_err(|_| invalid("cannot allocate PDF CID to GID map"))?; + cid_map.resize(map_len, 0); + for row in input.codes { + pairs.push((row.code, row.logical)); + widths.push((row.code, metrics.advance(row.subset_gid)?)); + let at = usize::from(row.code).checked_mul(2).ok_or_else(|| invalid("CID map offset overflow"))?; + let end = at.checked_add(2).ok_or_else(|| invalid("CID map offset overflow"))?; + cid_map.get_mut(at..end).ok_or_else(|| invalid("CID map range exceeds its allowance"))? + .copy_from_slice(&row.subset_gid.to_be_bytes()); + } + let unicode = write_to_unicode(&pairs, unicode_allowance)?; + let bound = byte_bound(input.subset.bytes().len(), unicode.len(), cid_map.len(), widths.len())?; + if bound > limit { + return Err(invalid("font objects exceed their complete preflight byte allowance")); + } + // Stream::start panics above i32::MAX, so check each real stream explicitly. + for length in [input.subset.bytes().len(), unicode.len(), cid_map.len()] { + i32::try_from(length).map_err(|_| invalid("font stream length exceeds the PDF writer limit"))?; + } + Ok(Prepared { + font: input.subset.bytes(), unicode, cid_map, widths, + default_width: metrics.advance(0)?, name, descriptor, flags, limit, bound, + }) +} + +fn check_chunk(chunk: &Chunk, limit: usize, bound: usize) -> Result<()> { + if chunk.len() > limit || chunk.len() > bound { + return Err(invalid("serialized font objects exceed their checked byte bound")); + } + Ok(()) +} + +/// Prepare six independent resource objects locally. Caller must reserve their +/// Ref IDs/tag globally and later attach the Type0 ref to page resources. +/// No caller Pdf is borrowed, so Result refusals cannot mutate that document. +pub(super) fn font_objects(refs: FontObjectRefs, input: FontObjectInput<'_>) -> Result { + refs.validate()?; + let prepared = prepare(input)?; + let Prepared { font, unicode, cid_map, widths, default_width, name, descriptor, flags, limit, bound } = prepared; + let mut chunk = Chunk::new(); + { + let mut writer = chunk.type0_font(refs.type0); + writer.base_font(Name(&name)).encoding_predefined(Name(b"Identity-H")) + .descendant_font(refs.cid_font).to_unicode(refs.to_unicode); + } + check_chunk(&chunk, limit, bound)?; + { + let mut writer = chunk.cid_font(refs.cid_font); + writer.subtype(CidFontType::Type2).base_font(Name(&name)) + .system_info(SystemInfo { registry: Str(b"Adobe"), ordering: Str(b"Identity"), supplement: 0 }) + .font_descriptor(refs.descriptor).default_width(default_width) + .cid_to_gid_map_stream(refs.cid_to_gid); + let mut array = writer.widths(); + for &(cid, width) in &widths { + array.consecutive(cid, std::iter::once(width)); + } + } + check_chunk(&chunk, limit, bound)?; + { + let [left, bottom, right, top] = descriptor.bbox_1000; + let mut writer = chunk.font_descriptor(refs.descriptor); + writer.name(Name(&name)).flags(flags).bbox(Rect::new(left, bottom, right, top)) + .italic_angle(descriptor.italic_angle_degrees).ascent(descriptor.ascent_1000) + .descent(descriptor.descent_1000).cap_height(descriptor.cap_height_1000) + .stem_v(descriptor.stem_v_1000).font_file2(refs.font_file2); + } + check_chunk(&chunk, limit, bound)?; + { + let length = i32::try_from(font.len()).map_err(|_| invalid("FontFile2 length exceeds i32"))?; + chunk.stream(refs.font_file2, font).pair(Name(b"Length1"), length); + } + check_chunk(&chunk, limit, bound)?; + { + chunk.stream(refs.to_unicode, &unicode); + } + check_chunk(&chunk, limit, bound)?; + { + chunk.stream(refs.cid_to_gid, &cid_map); + } + check_chunk(&chunk, limit, bound)?; + if !chunk.refs().eq(refs.all()) { + return Err(invalid("font object references differ from the six reserved IDs")); + } + Ok(chunk) +} + +#[cfg(test)] +mod tests { + use super::*; + use super::super::subset_font::subset_font; + + const BUNDLED: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../assets/fonts/liberation-sans/LiberationSans-Regular.ttf")); + + // SYNTHETIC descriptor for serialization/refusal tests. These are NOT + // measured Liberation values or a choice of the real provider's policies. + fn synthetic_descriptor() -> DescriptorMetrics { + DescriptorMetrics { + bbox_1000: [-100.0, -250.0, 1100.0, 900.0], + ascent_1000: 800.0, descent_1000: -200.0, cap_height_1000: 700.0, + italic_angle_degrees: 0.0, stem_v_1000: 80.0, + flags: FontFlags::NON_SYMBOLIC.bits(), + } + } + + fn admitted_subset() -> SubsetFont { + // Read-only original metadata: glyph0 metric(1536,205), 2/3=(569,0), + // original IDs0,2,3 become subset IDs0,1,2. No B1/shaping assertion. + subset_font(BUNDLED, 0, &[3, 2, 3], MAX_FONT_BYTES).unwrap() + } + + fn refs(first: i32) -> FontObjectRefs { + FontObjectRefs { + type0: Ref::new(first), cid_font: Ref::new(first + 1), + descriptor: Ref::new(first + 2), font_file2: Ref::new(first + 3), + to_unicode: Ref::new(first + 4), cid_to_gid: Ref::new(first + 5), + } + } + + fn input<'a>(subset: &'a SubsetFont, codes: &'a [CodeToGlyph<'a>], descriptor: &'a DescriptorMetrics) -> FontObjectInput<'a> { + FontObjectInput { + subset, codes, descriptor: Some(descriptor), + postscript_name: POSTSCRIPT_NAME, subset_tag: *b"ABCDEF", + max_chunk_bytes: MAX_CHUNK_BYTES, + } + } + + struct Parsed<'a> { + id: i32, + dict: &'a str, + stream: Option<&'a [u8]>, + } + + fn skip_space(bytes: &[u8], at: &mut usize) { + while bytes.get(*at).is_some_and(u8::is_ascii_whitespace) { *at += 1; } + } + + fn number(dict: &str, key: &str) -> usize { + dict.split_ascii_whitespace().skip_while(|token| *token != key) + .nth(1).unwrap().parse().unwrap() + } + + // Original tiny reader for THIS fixed dictionary/stream grammar, not a + // general PDF parser. Binary font bytes are skipped by declared Length, + // never scanned for endobj/endstream text or used as an expected golden. + fn objects(bytes: &[u8]) -> Vec> { + let mut out = Vec::new(); + let mut at = 0; + while at < bytes.len() { + skip_space(bytes, &mut at); + if at == bytes.len() { break; } + let start = at; + while bytes[at].is_ascii_digit() { at += 1; } + let id = std::str::from_utf8(&bytes[start..at]).unwrap().parse().unwrap(); + assert!(bytes[at..].starts_with(b" 0 obj\n")); + at += b" 0 obj\n".len(); + let start = at; + assert!(bytes[at..].starts_with(b"<<")); + let mut depth = 0; + loop { + if bytes[at..].starts_with(b"<<") { depth += 1; at += 2; } + else if bytes[at..].starts_with(b">>") { + depth -= 1; + at += 2; + if depth == 0 { break; } + } else { at += 1; } + } + let dict = std::str::from_utf8(&bytes[start..at]).unwrap(); + skip_space(bytes, &mut at); + let stream = if bytes[at..].starts_with(b"stream\n") { + at += b"stream\n".len(); + let len = number(dict, "/Length"); + let payload = &bytes[at..at + len]; + at += len; + assert!(bytes[at..].starts_with(b"\nendstream")); + at += b"\nendstream".len(); + skip_space(bytes, &mut at); + Some(payload) + } else { None }; + assert!(bytes[at..].starts_with(b"endobj")); + at += b"endobj".len(); + out.push(Parsed { id, dict, stream }); + } + out + } + + fn object<'a, 'b>(objects: &'b [Parsed<'a>], id: i32) -> &'b Parsed<'a> { + objects.iter().find(|object| object.id == id).unwrap() + } + + #[test] + fn six_real_refs_link_exact_objects_and_preserve_raw_font_bytes() { + let subset = admitted_subset(); + let descriptor = synthetic_descriptor(); + let chunk = font_objects(refs(6), input(&subset, &[], &descriptor)).unwrap(); + let parsed = objects(chunk.as_bytes()); + assert_eq!(parsed.iter().map(|o| o.id).collect::>(), [6, 7, 8, 9, 10, 11]); + let type0 = object(&parsed, 6).dict; + assert!(type0.contains("/Subtype /Type0")); + assert!(type0.contains("/Encoding /Identity-H")); + assert!(type0.contains("/DescendantFonts [7 0 R]")); + assert!(type0.contains("/ToUnicode 10 0 R")); + assert!(type0.contains("/BaseFont /ABCDEF+LiberationSans")); + let cid = object(&parsed, 7).dict; + assert!(cid.contains("/Subtype /CIDFontType2")); + assert!(cid.contains("/CIDToGIDMap 11 0 R")); + assert!(cid.contains("/FontDescriptor 8 0 R")); + assert!(cid.contains("/Registry (Adobe)") && cid.contains("/Ordering (Identity)")); + let desc = object(&parsed, 8).dict; + assert!(desc.contains("/FontName /ABCDEF+LiberationSans")); + assert!(desc.contains("/FontFile2 9 0 R")); + assert!(desc.contains("/StemV 80") && desc.contains("/Ascent 800")); + assert!(desc.contains("/CapHeight 700") && desc.contains("/Flags 32")); + let file = object(&parsed, 9); + assert_eq!(file.stream.unwrap(), subset.bytes()); + assert_eq!(number(file.dict, "/Length1"), subset.bytes().len()); + assert!(!file.dict.contains("/Filter")); + assert_eq!(object(&parsed, 11).stream.unwrap(), [0, 0]); + } + + #[test] + fn distinct_codes_share_a_gid_without_losing_literal_logical_clusters() { + let subset = admitted_subset(); + let descriptor = synthetic_descriptor(); + // Synthetic routing semantics only; no claim that these words are + // shaped or rendered by the space/empty glyph in the fixed resource. + let codes = [ + CodeToGlyph { code: 2, subset_gid: 1, logical: "one" }, + CodeToGlyph { code: 7, subset_gid: 1, logical: "two" }, + CodeToGlyph { code: 9, subset_gid: 0, logical: "notdef" }, + ]; + let chunk = font_objects(refs(6), input(&subset, &codes, &descriptor)).unwrap(); + let parsed = objects(chunk.as_bytes()); + let map = object(&parsed, 11).stream.unwrap(); + let mut expected = vec![0; 20]; + expected[5] = 1; + expected[15] = 1; + assert_eq!(map, expected); + let cmap = std::str::from_utf8(object(&parsed, 10).stream.unwrap()).unwrap(); + assert!(cmap.contains("<0002> <006F006E0065>")); + assert!(cmap.contains("<0007> <00740077006F>")); + assert!(cmap.contains("<0009> <006E006F0074006400650066>")); + assert!(!cmap.contains("<0001> <")); + } + + #[test] + fn sparse_maximum_cid_has_bounded_map_and_zero_stays_notdef() { + let subset = admitted_subset(); + let descriptor = synthetic_descriptor(); + let codes = [CodeToGlyph { code: u16::MAX, subset_gid: 2, logical: "z" }]; + let chunk = font_objects(refs(6), input(&subset, &codes, &descriptor)).unwrap(); + let parsed = objects(chunk.as_bytes()); + let map = object(&parsed, 11).stream.unwrap(); + assert_eq!(map.len(), 131_072); + assert!(map[..131_070].iter().all(|byte| *byte == 0)); + assert_eq!(&map[131_070..], [0, 2]); + let bad = [CodeToGlyph { code: 0, subset_gid: 1, logical: "x" }]; + assert!(font_objects(refs(6), input(&subset, &bad, &descriptor)).is_err()); + let zero = [CodeToGlyph { code: 0, subset_gid: 0, logical: "notdef" }]; + assert!(font_objects(refs(6), input(&subset, &zero, &descriptor)).is_ok()); + } + + // Test-only parser for this oracle's EXACT two-row W shape. It preserves + // the outer array and one-element inner arrays instead of flattening them. + // Bounded input/tokens, no deliberate panic, and no general PDF claim. + struct WidthField<'a> { + bytes: &'a [u8], + at: usize, + } + + impl WidthField<'_> { + fn literal(&mut self, token: &[u8]) -> Result<()> { + skip_space(self.bytes, &mut self.at); + let end = self.at.checked_add(token.len()).ok_or_else(|| invalid("W oracle offset overflow"))?; + if self.bytes.get(self.at..end) != Some(token) { + return Err(invalid("W oracle expected an array delimiter or field key")); + } + self.at = end; + Ok(()) + } + + fn cid(&mut self) -> Result { + skip_space(self.bytes, &mut self.at); + let start = self.at; + while self.bytes.get(self.at).is_some_and(u8::is_ascii_digit) { + if self.at - start == 5 { + return Err(invalid("W oracle CID token exceeds five digits")); + } + self.at += 1; + } + let token = self.bytes.get(start..self.at).ok_or_else(|| invalid("W oracle CID range"))?; + let token = std::str::from_utf8(token).map_err(|_| invalid("W oracle CID encoding"))?; + token.parse::().map_err(|_| invalid("W oracle expected a u16 CID")) + } + + fn width(&mut self) -> Result { + skip_space(self.bytes, &mut self.at); + let start = self.at; + while let Some(byte) = self.bytes.get(self.at) { + if byte.is_ascii_whitespace() || matches!(*byte, b'[' | b']') { + break; + } + if self.at - start == 64 { + return Err(invalid("W oracle width token exceeds its limit")); + } + self.at += 1; + } + let token = self.bytes.get(start..self.at).ok_or_else(|| invalid("W oracle width range"))?; + if !token.iter().any(u8::is_ascii_digit) + || token.iter().any(|byte| !byte.is_ascii_digit() && !matches!(*byte, b'+' | b'-' | b'.')) + { + return Err(invalid("W oracle expected a decimal width token")); + } + let token = std::str::from_utf8(token).map_err(|_| invalid("W oracle width encoding"))?; + let width = token.parse::().map_err(|_| invalid("W oracle decimal width"))?; + if !width.is_finite() || width < 0.0 { + return Err(invalid("W oracle requires a finite nonnegative width")); + } + Ok(width) + } + + fn row(&mut self) -> Result<(u16, [f32; 1])> { + let cid = self.cid()?; + self.literal(b"[")?; + let width = self.width()?; + self.literal(b"]")?; + Ok((cid, [width])) + } + } + + fn parse_two_width_rows(field: &str) -> Result<[(u16, [f32; 1]); 2]> { + if field.len() > 512 { + return Err(invalid("W oracle field exceeds its byte limit")); + } + let mut reader = WidthField { bytes: field.as_bytes(), at: 0 }; + reader.literal(b"/W")?; + reader.literal(b"[")?; + let first = reader.row()?; + let second = reader.row()?; + reader.literal(b"]")?; + skip_space(reader.bytes, &mut reader.at); + if reader.at != reader.bytes.len() { + return Err(invalid("W oracle has trailing data or additional rows")); + } + Ok([first, second]) + } + + #[test] + fn widths_use_the_mapped_subset_gid_but_are_indexed_by_cid() { + let subset = admitted_subset(); + let descriptor = synthetic_descriptor(); + let codes = [ + CodeToGlyph { code: 42, subset_gid: 2, logical: "synthetic" }, + CodeToGlyph { code: 300, subset_gid: 0, logical: "notdef" }, + ]; + let prepared = prepare(input(&subset, &codes, &descriptor)).unwrap(); + // Independent original-font integer metadata, not emitter output. + let empty_width = (569_000.0_f64 / 2048.0) as f32; + assert_eq!(prepared.default_width, 750.0); //1536*1000/2048. + assert_eq!(prepared.widths, [(42, empty_width), (300, 750.0)]); + // Subsetter compresses the repeated569 width of the two empty glyphs. + let view = MetricView::read(&subset).unwrap(); + assert_eq!(view.advance(1).unwrap(), empty_width); + assert_eq!(view.advance(2).unwrap(), empty_width); + let chunk = font_objects(refs(6), input(&subset, &codes, &descriptor)).unwrap(); + let parsed = objects(chunk.as_bytes()); + let cid = object(&parsed, 7).dict; + assert!(cid.contains("/DW 750")); + // W is the last entry of this emitted dictionary; remove ONLY its + // dictionary terminator, then parse all of W including both array levels. + let field = cid.get(cid.find("/W ").unwrap()..).unwrap() + .strip_suffix(">>").unwrap().trim_end(); + let expected = [(42, [empty_width]), (300, [750.0])]; + assert_eq!(parse_two_width_rows(field).unwrap(), expected); + // Independent literals, not emitter-generated goldens. The former + // flattened-token oracle would miss these nesting/trailing mutations. + for malformed in [ + "/W [42 277.83203125 300 750]", + "/W [42 [277.83203125] 300 [750] 999 [123]]", + "/W [42 [277.83203125] 300 [750]] 999 [123]", + "/W [42 [277.83203125] 300 [750]", + "/W [42 [277.83203125 999] 300 [750]]", + ] { + assert!(parse_two_width_rows(malformed).is_err(), "accepted malformed W: {malformed}"); + } + assert_eq!(parse_two_width_rows("/W [42 [277.83203125] 300 [750]]").unwrap(), expected); + } + + #[test] + fn invalid_code_order_gid_and_logical_clusters_refuse() { + let subset = admitted_subset(); + let descriptor = synthetic_descriptor(); + for codes in [ + vec![CodeToGlyph { code: 2, subset_gid: 3, logical: "outside" }], + vec![CodeToGlyph { code: 2, subset_gid: 1, logical: "" }], + vec![CodeToGlyph { code: 2, subset_gid: 1, logical: "a" }, CodeToGlyph { code: 2, subset_gid: 1, logical: "b" }], + vec![CodeToGlyph { code: 2, subset_gid: 1, logical: "a" }, CodeToGlyph { code: 1, subset_gid: 1, logical: "b" }], + ] { + assert!(font_objects(refs(6), input(&subset, &codes, &descriptor)).is_err()); + } + let too_long = "x".repeat(769); + let codes = [CodeToGlyph { code: 1, subset_gid: 1, logical: &too_long }]; + assert!(font_objects(refs(6), input(&subset, &codes, &descriptor)).is_err()); + let too_many_utf16 = "x".repeat(257); // Within UTF8 cap, beyond512UTF16BE bytes. + let codes = [CodeToGlyph { code: 1, subset_gid: 1, logical: &too_many_utf16 }]; + assert!(font_objects(refs(6), input(&subset, &codes, &descriptor)).is_err()); + } + + #[test] + fn essential_descriptor_fields_refuse_missing_invalid_and_underflow_values() { + let subset = admitted_subset(); + let valid = synthetic_descriptor(); + let mut missing = input(&subset, &[], &valid); + missing.descriptor = None; + assert!(font_objects(refs(6), missing).is_err()); + for case in 0..12 { + let mut descriptor = synthetic_descriptor(); + match case { + 0 => descriptor.stem_v_1000 = 0.0, + 1 => descriptor.ascent_1000 = 0.0, + 2 => descriptor.descent_1000 = 0.0, + 3 => descriptor.cap_height_1000 = -1.0, + 4 => descriptor.cap_height_1000 = 900.0, + 5 => descriptor.bbox_1000 = [0.0; 4], + 6 => descriptor.stem_v_1000 = f32::NAN, + 7 => descriptor.bbox_1000[3] = f32::INFINITY, + 8 => descriptor.ascent_1000 = MAX_METRIC + 1.0, + 9 => descriptor.italic_angle_degrees = 91.0, + 10 => descriptor.stem_v_1000 = f32::from_bits(1), + _ => descriptor.bbox_1000 = [10.0, 0.0, 0.0, 900.0], + } + assert!(font_objects(refs(6), input(&subset, &[], &descriptor)).is_err(), "synthetic case{case}"); + } + for flags in [0, 1 << 31, FontFlags::SYMBOLIC.bits() | FontFlags::NON_SYMBOLIC.bits()] { + let mut descriptor = synthetic_descriptor(); + descriptor.flags = flags; + assert!(font_objects(refs(6), input(&subset, &[], &descriptor)).is_err()); + } + } + + #[test] + fn fixed_name_tag_and_real_ref_constraints_are_explicit() { + let subset = admitted_subset(); + let descriptor = synthetic_descriptor(); + let mut wrong_name = input(&subset, &[], &descriptor); + wrong_name.postscript_name = b"OtherFont"; + assert!(font_objects(refs(6), wrong_name).is_err()); + for tag in [*b"abcDEF", *b"ABCDE ", [255; 6]] { + let mut wrong_tag = input(&subset, &[], &descriptor); + wrong_tag.subset_tag = tag; + assert!(font_objects(refs(6), wrong_tag).is_err()); + } + let mut duplicate = refs(6); + duplicate.cid_font = duplicate.type0; + assert!(font_objects(duplicate, input(&subset, &[], &descriptor)).is_err()); + assert!(font_objects(refs(1), input(&subset, &[], &descriptor)).is_err()); + let mut too_high = refs(6); + too_high.cid_to_gid = Ref::new(100_001); + assert!(font_objects(too_high, input(&subset, &[], &descriptor)).is_err()); + let mut inclusive = refs(6); + inclusive.cid_to_gid = Ref::new(100_000); + assert!(font_objects(inclusive, input(&subset, &[], &descriptor)).is_ok()); + } + + #[test] + fn preflight_byte_allowance_is_inclusive_and_actual_serialization_is_below_it() { + let subset = admitted_subset(); + let descriptor = synthetic_descriptor(); + let codes = [CodeToGlyph { code: 42, subset_gid: 1, logical: "x" }]; + let prepared = prepare(input(&subset, &codes, &descriptor)).unwrap(); + let bound = prepared.bound; // Boundary property, not a generated font golden. + let mut exact = input(&subset, &codes, &descriptor); + exact.max_chunk_bytes = bound; + let chunk = font_objects(refs(6), exact).unwrap(); + assert!(chunk.len() <= bound); + let mut short = input(&subset, &codes, &descriptor); + short.max_chunk_bytes = bound - 1; + assert!(font_objects(refs(6), short).is_err()); + let mut zero = input(&subset, &codes, &descriptor); + zero.max_chunk_bytes = 0; + assert!(font_objects(refs(6), zero).is_err()); + assert!(byte_bound(usize::MAX, 0, 0, 0).is_err()); + assert!(byte_bound(0, 0, 0, usize::MAX).is_err()); + } + + #[test] + fn repeated_complete_inputs_are_deterministic_and_leave_source_unchanged() { + let subset = admitted_subset(); + let descriptor = synthetic_descriptor(); + let source = subset.bytes().to_vec(); + let mapping = subset.mapping().to_vec(); + let codes = [CodeToGlyph { code: 10, subset_gid: 1, logical: "x" }]; + let a = font_objects(refs(6), input(&subset, &codes, &descriptor)).unwrap(); + let b = font_objects(refs(6), input(&subset, &codes, &descriptor)).unwrap(); + assert_eq!(a.as_bytes(), b.as_bytes()); + assert_eq!(source, subset.bytes()); + assert_eq!(mapping, subset.mapping()); + } + + #[test] + fn full_u16_code_space_is_inclusive_and_one_extra_record_refuses() { + let subset = admitted_subset(); + let descriptor = synthetic_descriptor(); + let mut codes: Vec<_> = (0..=u16::MAX).map(|code| CodeToGlyph { + code, subset_gid: if code == 0 { 0 } else { 1 }, logical: "x", + }).collect(); + let chunk = font_objects(refs(6), input(&subset, &codes, &descriptor)).unwrap(); + assert_eq!(chunk.refs().count(), 6); + assert!(chunk.len() <= MAX_CHUNK_BYTES); + codes.push(CodeToGlyph { code: 0, subset_gid: 0, logical: "x" }); + assert!(font_objects(refs(6), input(&subset, &codes, &descriptor)).is_err()); + } + + #[test] + fn source_derived_p3_offset_arithmetic_is_a_caller_reservation_not_a_registry() { + let subset = admitted_subset(); + let descriptor = synthetic_descriptor(); + for layers in [0, 5] { + let first = 6 + layers; + let chunk = font_objects(refs(first), input(&subset, &[], &descriptor)).unwrap(); + let ids: Vec<_> = chunk.refs().map(Ref::get).collect(); + assert_eq!(ids, (first..=first + 5).collect::>()); + assert!(ids.iter().all(|id| *id > 5 + layers)); + } + // This only checks the derived offsets with synthetic descriptors. + // The actual P3 allocator/page resource integration is NOT exercised. + } +} + + diff --git a/crates/io/src/paper/pdf/glyph_closure.rs b/crates/io/src/paper/pdf/glyph_closure.rs new file mode 100644 index 000000000..677135bf6 --- /dev/null +++ b/crates/io/src/paper/pdf/glyph_closure.rs @@ -0,0 +1,841 @@ +//! Dependency closure for already chosen TrueType glyph IDs in the one admitted font. +//! +//! No font lookup, text shaping, CID mapping, glyph rendering or font-byte output. +//! The public entry MUST pass font_admission's exact-byte/face policy first. +//! Internal parser tests use original structural fixtures, not approved fonts. +//! Specification: Microsoft OpenType glyf, loca, head, maxp and file organization. + +use super::font_admission::check_liberation_sans; +use crate::{IoError, Result}; + +const MAX_FONT_BYTES: usize = 410_820; +const MAX_TABLES: usize = 19; +const MAX_USED: usize = 65_536; +const MAX_GLYPHS: usize = 16_384; +const MAX_COMPONENTS: usize = 65_536; +const MAX_PER_GLYPH: usize = 1_024; +const MAX_DEPTH: usize = 64; +const MAX_WORK: usize = 4 << 20; +const MAX_STATE_BYTES: usize = 128 << 10; +const MAX_CONTOURS: usize = 4_096; +const MAX_POINTS: usize = 65_536; + +fn invalid(message: &'static str) -> IoError { + IoError::Invalid(message.into()) +} + +fn part(bytes: &[u8], at: usize, len: usize) -> Result<&[u8]> { + let end = at.checked_add(len).ok_or_else(|| invalid("glyph closure offset overflow"))?; + bytes.get(at..end).ok_or_else(|| invalid("truncated glyph closure data")) +} + +fn word(bytes: &[u8], at: usize) -> Result { + let value = part(bytes, at, 2)?.try_into().map_err(|_| invalid("invalid glyph closure word"))?; + Ok(u16::from_be_bytes(value)) +} + +fn dword(bytes: &[u8], at: usize) -> Result { + let value = part(bytes, at, 4)?.try_into().map_err(|_| invalid("invalid glyph closure dword"))?; + Ok(u32::from_be_bytes(value)) +} + +fn count32(value: u32) -> Result { + usize::try_from(value).map_err(|_| invalid("glyph closure value exceeds platform")) +} + +fn charge(value: &mut usize, next: usize, limit: usize, message: &'static str) -> Result<()> { + let sum = value.checked_add(next).filter(|sum| *sum <= limit).ok_or_else(|| invalid(message))?; + *value = sum; + Ok(()) +} + +#[derive(Clone, Copy)] +struct Limits { + glyphs: usize, + components: usize, + depth: usize, + bytes: usize, + work: usize, +} + +impl Limits { + const MAX: Self = Self { + glyphs: MAX_GLYPHS, components: MAX_COMPONENTS, depth: MAX_DEPTH, + bytes: MAX_FONT_BYTES, work: MAX_WORK, + }; + + fn bounded(self) -> Self { + Self { + glyphs: self.glyphs.min(MAX_GLYPHS), components: self.components.min(MAX_COMPONENTS), + depth: self.depth.min(MAX_DEPTH), bytes: self.bytes.min(MAX_FONT_BYTES), + work: self.work.min(MAX_WORK), + } + } +} + +struct Budget { + limits: Limits, + require_final_instructions: bool, + glyphs: usize, + components: usize, + bytes: usize, + work: usize, +} + +impl Budget { + fn new(limits: Limits) -> Self { + Self { limits: limits.bounded(), require_final_instructions: false, glyphs: 0, components: 0, bytes: 0, work: 0 } + } + + fn work(&mut self, count: usize) -> Result<()> { + charge(&mut self.work, count, self.limits.work, "glyph closure work limit exceeded") + } + + fn glyph(&mut self, bytes: usize) -> Result<()> { + charge(&mut self.glyphs, 1, self.limits.glyphs, "glyph closure entry limit exceeded")?; + charge(&mut self.bytes, bytes, self.limits.bytes, "glyph closure byte limit exceeded")?; + self.work(bytes.checked_add(1).ok_or_else(|| invalid("glyph closure work overflow"))?) + } + + fn component(&mut self) -> Result<()> { + charge(&mut self.components, 1, self.limits.components, "glyph closure component limit exceeded")?; + self.work(1) + } +} + +struct Font<'a> { + loca: &'a [u8], + glyf: &'a [u8], + glyphs: usize, + format: u16, +} + +impl<'a> Font<'a> { + fn parse(bytes: &'a [u8], budget: &mut Budget) -> Result { + if bytes.len() > MAX_FONT_BYTES || dword(bytes, 0)? != 0x0001_0000 { + return Err(invalid("unsupported glyph closure SFNT envelope")); + } + let count = usize::from(word(bytes, 4)?); + if count == 0 || count > MAX_TABLES { + return Err(invalid("glyph closure table count exceeds its limit")); + } + let directory_end = count.checked_mul(16).and_then(|n| n.checked_add(12)) + .ok_or_else(|| invalid("glyph closure directory overflow"))?; + part(bytes, 0, directory_end)?; + budget.work(count.checked_mul(count + 1).ok_or_else(|| invalid("glyph closure work overflow"))?)?; + let (mut head, mut maxp, mut loca, mut glyf) = (None, None, None, None); + let mut previous = None; + for index in 0..count { + // count <= 19 bounds these record offsets independently of the font. + let record = 12 + index * 16; + let tag = dword(bytes, record)?; + if previous.is_some_and(|previous| tag <= previous) { + return Err(invalid("glyph closure table tags must be unique and sorted")); + } + previous = Some(tag); + let start = count32(dword(bytes, record + 8)?)?; + let len = count32(dword(bytes, record + 12)?)?; + if start < directory_end || start % 4 != 0 { + return Err(invalid("invalid glyph closure table offset")); + } + let table = part(bytes, start, len)?; + let end = start.checked_add(len).ok_or_else(|| invalid("glyph closure table range overflow"))?; + for prior in 0..index { + let prior_record = 12 + prior * 16; + let low = count32(dword(bytes, prior_record + 8)?)?; + let size = count32(dword(bytes, prior_record + 12)?)?; + let high = low.checked_add(size).ok_or_else(|| invalid("glyph closure table range overflow"))?; + if len != 0 && size != 0 && start < high && low < end { + return Err(invalid("overlapping glyph closure tables")); + } + } + match part(bytes, record, 4)? { + b"head" => head = Some(table), + b"maxp" => maxp = Some(table), + b"loca" => loca = Some(table), + b"glyf" => glyf = Some(table), + _ => {} + } + } + let head = head.ok_or_else(|| invalid("glyph closure requires head"))?; + let maxp = maxp.ok_or_else(|| invalid("glyph closure requires maxp"))?; + let loca = loca.ok_or_else(|| invalid("glyph closure requires loca"))?; + let glyf = glyf.ok_or_else(|| invalid("glyph closure requires glyf"))?; + part(head, 0, 54)?; + part(maxp, 0, 32)?; + if dword(head, 0)? != 0x0001_0000 || dword(head, 12)? != 0x5f0f_3cf5 || word(head, 52)? != 0 { + return Err(invalid("unsupported glyph closure head revision")); + } + if dword(maxp, 0)? != 0x0001_0000 { + return Err(invalid("glyph closure requires TrueType maxp 1.0")); + } + let glyphs = usize::from(word(maxp, 4)?); + if glyphs == 0 { + return Err(invalid("glyph closure font has no glyph zero")); + } + let format = word(head, 50)?; + let width = match format { + 0 => 2, + 1 => 4, + _ => return Err(invalid("unsupported glyph closure loca format")), + }; + let entries = glyphs.checked_add(1).ok_or_else(|| invalid("glyph closure loca count overflow"))?; + let expected = entries.checked_mul(width).ok_or_else(|| invalid("glyph closure loca length overflow"))?; + if loca.len() != expected { + return Err(invalid("glyph closure loca length does not match numGlyphs")); + } + // Admit the complete loca scan before beginning it; no offset array is copied. + budget.work(entries)?; + let font = Self { loca, glyf, glyphs, format }; + let mut prior = 0usize; + for index in 0..entries { + let offset = font.offset(index)?; + if offset < prior || offset > glyf.len() { + return Err(invalid("glyph closure loca offsets are descending or outside glyf")); + } + prior = offset; + } + Ok(font) + } + + fn offset(&self, index: usize) -> Result { + let width = if self.format == 0 { 2 } else { 4 }; + let at = index.checked_mul(width).ok_or_else(|| invalid("glyph closure loca offset overflow"))?; + if self.format == 0 { + usize::from(word(self.loca, at)?).checked_mul(2).ok_or_else(|| invalid("glyph closure short loca overflow")) + } else { + count32(dword(self.loca, at)?) + } + } + + fn glyph(&self, gid: u16) -> Result<&'a [u8]> { + let index = usize::from(gid); + if index >= self.glyphs { + return Err(invalid("glyph closure glyph ID is outside numGlyphs")); + } + let next = index.checked_add(1).ok_or_else(|| invalid("glyph closure glyph index overflow"))?; + let start = self.offset(index)?; + let end = self.offset(next)?; + self.glyf.get(start..end).ok_or_else(|| invalid("glyph closure glyph range is invalid")) + } +} + +struct Cursor<'a> { + bytes: &'a [u8], + at: usize, +} + +impl<'a> Cursor<'a> { + fn take(&mut self, count: usize) -> Result<&'a [u8]> { + let value = part(self.bytes, self.at, count)?; + self.at = self.at.checked_add(count).ok_or_else(|| invalid("glyph closure cursor overflow"))?; + Ok(value) + } + + fn word(&mut self) -> Result { + let value = self.take(2)?.try_into().map_err(|_| invalid("invalid glyph closure component word"))?; + Ok(u16::from_be_bytes(value)) + } + + fn byte(&mut self) -> Result { + self.take(1)?.first().copied().ok_or_else(|| invalid("missing glyph closure flag")) + } + + fn padding(&self) -> Result<()> { + let tail = self.bytes.get(self.at..).ok_or_else(|| invalid("invalid glyph closure padding range"))?; + if tail.iter().any(|byte| *byte != 0) { + return Err(invalid("nonzero trailing glyph closure data")); + } + Ok(()) + } +} + +fn simple(cursor: &mut Cursor<'_>, contours: usize, budget: &mut Budget) -> Result<()> { + if contours > MAX_CONTOURS { + return Err(invalid("glyph closure contour limit exceeded")); + } + // A zero-contour glyph can be exactly its header, with no instructions. + if contours == 0 && cursor.at == cursor.bytes.len() { + return Ok(()); + } + let bytes = contours.checked_mul(2).ok_or_else(|| invalid("glyph closure contour size overflow"))?; + let endpoints = cursor.take(bytes)?; + budget.work(contours)?; + let mut previous = None; + let mut points = 0usize; + for pair in endpoints.chunks_exact(2) { + let end = u16::from_be_bytes(pair.try_into().map_err(|_| invalid("invalid glyph closure endpoint"))?); + if previous.is_some_and(|previous| end <= previous) { + return Err(invalid("glyph closure contour endpoints must increase")); + } + previous = Some(end); + points = usize::from(end).checked_add(1).ok_or_else(|| invalid("glyph closure point count overflow"))?; + } + if points > MAX_POINTS { + return Err(invalid("glyph closure point limit exceeded")); + } + let instructions = usize::from(cursor.word()?); + cursor.take(instructions)?; // Opaque bytes: never executed or rewritten. + let (mut seen, mut x_bytes, mut y_bytes) = (0usize, 0usize, 0usize); + while seen < points { + let flags = cursor.byte()?; + if flags & 0x80 != 0 || (seen != 0 && flags & 0x40 != 0) { + return Err(invalid("unsupported glyph closure simple flags")); + } + let run = if flags & 0x08 != 0 { usize::from(cursor.byte()?) + 1 } else { 1 }; + if run > points - seen { + return Err(invalid("glyph closure repeated flags exceed point count")); + } + budget.work(run)?; + let x = if flags & 0x02 != 0 { 1 } else if flags & 0x10 != 0 { 0 } else { 2 }; + let y = if flags & 0x04 != 0 { 1 } else if flags & 0x20 != 0 { 0 } else { 2 }; + let x = run.checked_mul(x).ok_or_else(|| invalid("glyph closure coordinate byte overflow"))?; + let y = run.checked_mul(y).ok_or_else(|| invalid("glyph closure coordinate byte overflow"))?; + x_bytes = x_bytes.checked_add(x).ok_or_else(|| invalid("glyph closure coordinate byte overflow"))?; + y_bytes = y_bytes.checked_add(y).ok_or_else(|| invalid("glyph closure coordinate byte overflow"))?; + seen += run; // run <= points-seen and points <= 65536. + } + cursor.take(x_bytes.checked_add(y_bytes).ok_or_else(|| invalid("glyph closure coordinate byte overflow"))?)?; + cursor.padding() +} + +fn children(font: &Font<'_>, bytes: &[u8], budget: &mut Budget) -> Result> { + budget.glyph(bytes.len())?; + if bytes.is_empty() { + return Ok(Vec::new()); // Equal consecutive loca offsets: no outline/instructions. + } + let mut cursor = Cursor { bytes, at: 0 }; + let contours = cursor.word()? as i16; + cursor.take(8)?; // Four signed bounding-box coordinates, not evaluated as geometry. + if contours >= 0 { + simple(&mut cursor, contours as usize, budget)?; + return Ok(Vec::new()); + } + let mut out = Vec::new(); + let mut instructions = false; + loop { + if out.len() >= MAX_PER_GLYPH { + return Err(invalid("glyph closure per-glyph component limit exceeded")); + } + budget.component()?; // Before copying or reserving a component entry. + let flags = cursor.word()?; + let child = cursor.word()?; + if flags & 0xe010 != 0 || (flags & 0x00c8).count_ones() > 1 || flags & 0x1800 == 0x1800 { + return Err(invalid("unsupported glyph closure composite flags")); + } + if (out.is_empty() && flags & 0x0002 == 0) || (!out.is_empty() && flags & 0x0400 != 0) { + return Err(invalid("invalid glyph closure first/overlap component flags")); + } + if usize::from(child) >= font.glyphs { + return Err(invalid("glyph closure component ID is outside numGlyphs")); + } + cursor.take(if flags & 0x0001 != 0 { 4 } else { 2 })?; + let transform = if flags & 0x0008 != 0 { 2 } else if flags & 0x0040 != 0 { 4 } else if flags & 0x0080 != 0 { 8 } else { 0 }; + cursor.take(transform)?; + out.try_reserve(1).map_err(|_| invalid("glyph closure component allocation failed"))?; + out.push(child); + instructions |= flags & 0x0100 != 0; // Any component may set this flag. + if flags & 0x0020 == 0 { + if budget.require_final_instructions && instructions && flags & 0x0100 == 0 { + return Err(invalid("subsetter 0.2.6 cannot preserve nonfinal-only composite instructions")); + } + break; + } + } + if instructions { + let length = usize::from(cursor.word()?); + cursor.take(length)?; + } + cursor.padding()?; + Ok(out) +} + +struct Frame { + gid: u16, + children: Vec, + next: usize, + height: usize, +} + +#[derive(Clone, Copy)] +struct State { + colour: u8, + height: u8, +} + +fn include_child_height(frame: &mut Frame, child: usize, limit: usize) -> Result<()> { + let height = child.checked_add(1).filter(|height| *height <= limit) + .ok_or_else(|| invalid("glyph closure depth limit exceeded"))?; + frame.height = frame.height.max(height); + Ok(()) +} + +fn enter(font: &Font<'_>, gid: u16, states: &mut [State], stack: &mut Vec, budget: &mut Budget) -> Result<()> { + if stack.len() >= budget.limits.depth { + return Err(invalid("glyph closure depth limit exceeded")); + } + let children = children(font, font.glyph(gid)?, budget)?; + let state = states.get_mut(usize::from(gid)).ok_or_else(|| invalid("invalid glyph closure state index"))?; + if state.colour != 0 { + return Err(invalid("glyph closure entered a nonwhite node")); + } + stack.try_reserve(1).map_err(|_| invalid("glyph closure stack allocation failed"))?; + state.colour = 1; + stack.push(Frame { gid, children, next: 0, height: 1 }); + Ok(()) +} + +fn collect(font_bytes: &[u8], used: &[u16], limits: Limits) -> Result> { + if used.len() > MAX_USED { + return Err(invalid("glyph closure input ID count exceeds its limit")); + } + let mut budget = Budget::new(limits); + let font = Font::parse(font_bytes, &mut budget)?; + budget.work(used.len().checked_add(font.glyphs).ok_or_else(|| invalid("glyph closure work overflow"))?)?; + for gid in used { + if usize::from(*gid) >= font.glyphs { + return Err(invalid("glyph closure input ID is outside numGlyphs")); + } + } + if font.glyphs.checked_mul(std::mem::size_of::()).is_none_or(|bytes| bytes > MAX_STATE_BYTES) { + return Err(invalid("glyph closure state byte limit exceeded")); + } + let mut states = Vec::new(); + states.try_reserve_exact(font.glyphs).map_err(|_| invalid("glyph closure state allocation failed"))?; + states.resize(font.glyphs, State { colour: 0, height: 0 }); + let mut stack = Vec::new(); + for root in std::iter::once(0u16).chain(used.iter().copied()) { + if states.get(usize::from(root)).is_some_and(|state| state.colour == 2) { + continue; + } + enter(&font, root, &mut states, &mut stack, &mut budget)?; + while !stack.is_empty() { + let next = { + let frame = stack.last_mut().ok_or_else(|| invalid("missing glyph closure frame"))?; + match frame.children.get(frame.next).copied() { + Some(child) => { + frame.next += 1; // children.len <= 1024. + Some(child) + } + None => None, + } + }; + if let Some(child) = next { + match states.get(usize::from(child)).copied() { + Some(State { colour: 0, .. }) => enter(&font, child, &mut states, &mut stack, &mut budget)?, + Some(State { colour: 1, .. }) => return Err(invalid("cyclic glyph closure")), + Some(State { colour: 2, height }) => { + let frame = stack.last_mut().ok_or_else(|| invalid("missing glyph closure parent frame"))?; + include_child_height(frame, usize::from(height), budget.limits.depth)?; + } + _ => return Err(invalid("invalid glyph closure child state")), + } + } else { + let frame = stack.pop().ok_or_else(|| invalid("missing glyph closure closing frame"))?; + let state = states.get_mut(usize::from(frame.gid)).ok_or_else(|| invalid("invalid glyph closure closing state"))?; + state.height = u8::try_from(frame.height).map_err(|_| invalid("glyph closure cached height exceeds its bound"))?; + state.colour = 2; + if let Some(parent) = stack.last_mut() { + include_child_height(parent, frame.height, budget.limits.depth)?; + } + } + } + } + let mut out = Vec::new(); + out.try_reserve_exact(budget.glyphs).map_err(|_| invalid("glyph closure result allocation failed"))?; + for (index, state) in states.into_iter().enumerate() { + if state.colour == 2 { + out.push(u16::try_from(index).map_err(|_| invalid("glyph closure result index exceeds u16"))?); + } else if state.colour != 0 { + return Err(invalid("glyph closure contains an unfinished node")); + } + } + Ok(out) +} + +/// Sorted unique GIDs needed by used, including glyph zero and recursive components. +/// Exact bundled identity/face and the existing metadata guard are mandatory. +/// The caller must obtain used IDs from these SAME font bytes and face. +/// A numerically valid GID cannot establish its source-font identity; this +/// helper checks ranges, not shaping provenance or text-to-glyph correctness. +/// Returned IDs are not remapped and no font bytes, metrics or hint program is emitted. +pub(super) fn glyph_closure(font: &[u8], face_index: u32, used: &[u16]) -> Result> { + check_liberation_sans(font, face_index)?; + collect(font, used, Limits::MAX) +} + +/// Temporary subsetter-0.2.6 policy, not general TrueType validity. +/// Caller MUST first obtain closure from glyph_closure on these same bytes/face. +/// Reuses the original bounded parser; never duplicates font admission or hashing. +pub(super) fn check_subsetter_component_instruction_flags(font: &[u8], closure: &[u16]) -> Result<()> { + if closure.len() > MAX_GLYPHS { + return Err(invalid("subsetter preflight glyph count exceeds its limit")); + } + let mut budget = Budget::new(Limits::MAX); + budget.require_final_instructions = true; + let parsed = Font::parse(font, &mut budget)?; + for gid in closure { + children(&parsed, parsed.glyph(*gid)?, &mut budget)?; + } + Ok(()) +} + +/// Structural read-back of a PDF-only subset, not admission as an input font. +/// Reuses the same parser without applying the original-font identity guard. +pub(super) fn check_subset_program(bytes: &[u8], glyphs: usize) -> Result<()> { + if glyphs == 0 || glyphs > MAX_GLYPHS { + return Err(invalid("invalid subset glyph count")); + } + let mut budget = Budget::new(Limits::MAX); + let parsed = Font::parse(bytes, &mut budget)?; + if parsed.glyphs != glyphs { + return Err(invalid("subset glyph count does not match its mapping")); + } + // Font::parse already checked this directory's bounds, uniqueness and ranges. + // Eleven tables are emitted for this fixed hinted font by subsetter 0.2.6. + let required = [*b"cvt ", *b"fpgm", *b"glyf", *b"head", *b"hhea", *b"hmtx", *b"loca", *b"maxp", *b"name", *b"post", *b"prep"]; + let mut present = 0u16; + let (mut hhea, mut hmtx) = (None, None); + for index in 0..usize::from(word(bytes, 4)?) { + let at = 12 + index * 16; // Parsed table count <= 19. + let tag = part(bytes, at, 4)?; + if tag == b"cmap" { + return Err(invalid("a FontFile2 CID subset must not contain cmap")); + } + if let Some(bit) = required.iter().position(|required| required.as_slice() == tag) { + present |= 1u16 << bit; // Eleven fixed entries; shift is bounded. + } + let table = part(bytes, count32(dword(bytes, at + 8)?)?, count32(dword(bytes, at + 12)?)?)?; + match tag { + b"hhea" => hhea = Some(table), + b"hmtx" => hmtx = Some(table), + _ => {} + } + } + if present != (1u16 << required.len()) - 1 { + return Err(invalid("subset is missing a required fixed-font table")); + } + let hhea = hhea.ok_or_else(|| invalid("subset requires hhea"))?; + part(hhea, 0, 36)?; + let metrics = usize::from(word(hhea, 34)?); + if metrics == 0 || metrics > glyphs { + return Err(invalid("subset numberOfHMetrics is outside its glyph count")); + } + let expected = metrics.checked_mul(4) + .and_then(|full| (glyphs - metrics).checked_mul(2).and_then(|tail| full.checked_add(tail))) + .ok_or_else(|| invalid("subset hmtx length overflow"))?; + if hmtx.ok_or_else(|| invalid("subset requires hmtx"))?.len() != expected { + return Err(invalid("subset hmtx length does not match numberOfHMetrics")); + } + for gid in 0..glyphs { + let gid = u16::try_from(gid).map_err(|_| invalid("subset glyph ID exceeds u16"))?; + children(&parsed, parsed.glyph(gid)?, &mut budget)?; + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + const BUNDLED: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../assets/fonts/liberation-sans/LiberationSans-Regular.ttf")); + + fn put16(bytes: &mut [u8], at: usize, value: u16) { + bytes[at..at + 2].copy_from_slice(&value.to_be_bytes()); + } + + fn put32(bytes: &mut [u8], at: usize, value: u32) { + bytes[at..at + 4].copy_from_slice(&value.to_be_bytes()); + } + + // Original structural SFNT fixtures for the PRIVATE parser, not fonts admitted + // by glyph_closure. No names, cmap, outlines or third-party font program copied. + fn fixture(glyphs: &[Vec], short: bool) -> Vec { + let mut glyf = Vec::new(); + let mut offsets = vec![0u32]; + for glyph in glyphs { + glyf.extend_from_slice(glyph); + if glyf.len() % 2 != 0 { glyf.push(0); } + offsets.push(u32::try_from(glyf.len()).unwrap()); + } + let mut loca = Vec::new(); + for offset in offsets { + if short { + loca.extend_from_slice(&u16::try_from(offset / 2).unwrap().to_be_bytes()); + } else { + loca.extend_from_slice(&offset.to_be_bytes()); + } + } + let mut head = vec![0; 54]; + put32(&mut head, 0, 0x0001_0000); + put32(&mut head, 12, 0x5f0f_3cf5); + put16(&mut head, 50, if short { 0 } else { 1 }); + let mut maxp = vec![0; 32]; + put32(&mut maxp, 0, 0x0001_0000); + put16(&mut maxp, 4, u16::try_from(glyphs.len()).unwrap()); + let tables = [(*b"glyf", glyf), (*b"head", head), (*b"loca", loca), (*b"maxp", maxp)]; + let mut out = vec![0; 12 + 4 * 16]; + put32(&mut out, 0, 0x0001_0000); + put16(&mut out, 4, 4); + for (index, (tag, data)) in tables.into_iter().enumerate() { + while out.len() % 4 != 0 { out.push(0); } + let start = u32::try_from(out.len()).unwrap(); + let len = u32::try_from(data.len()).unwrap(); + let record = 12 + index * 16; + out[record..record + 4].copy_from_slice(&tag); + put32(&mut out, record + 8, start); + put32(&mut out, record + 12, len); + out.extend_from_slice(&data); + } + out + } + + fn simple_glyph() -> Vec { + let mut out = vec![0; 10]; + put16(&mut out, 0, 1); + // One contour ending at point0, no instructions, one on-curve point + // whose x/y equal their previous value, then zero padding. + out.extend_from_slice(&[0, 0, 0, 0, 0x31, 0]); + out + } + + fn composite(records: &[(u16, u16)], instructions: Option<&[u8]>) -> Vec { + let mut out = vec![0; 10]; + put16(&mut out, 0, 0xffff); + for (index, &(flags, gid)) in records.iter().enumerate() { + let flags = flags | if index + 1 < records.len() { 0x0020 } else { 0 }; + out.extend_from_slice(&flags.to_be_bytes()); + out.extend_from_slice(&gid.to_be_bytes()); + out.extend(std::iter::repeat_n(0, if flags & 1 != 0 { 4 } else { 2 })); + let transform = if flags & 0x0008 != 0 { 2 } else if flags & 0x0040 != 0 { 4 } else if flags & 0x0080 != 0 { 8 } else { 0 }; + out.extend(std::iter::repeat_n(0, transform)); + } + if let Some(bytes) = instructions { + out.extend_from_slice(&u16::try_from(bytes.len()).unwrap().to_be_bytes()); + out.extend_from_slice(bytes); + } + out + } + + fn graph() -> Vec> { + vec![Vec::new(), simple_glyph(), composite(&[(3, 1)], None), composite(&[(3, 2), (3, 1)], None)] + } + + fn record(bytes: &[u8], tag: &[u8; 4]) -> usize { + (0..usize::from(word(bytes, 4).unwrap())).map(|index| 12 + index * 16) + .find(|at| &bytes[*at..*at + 4] == tag).unwrap() + } + + fn table_start(bytes: &[u8], tag: &[u8; 4]) -> usize { + usize::try_from(dword(bytes, record(bytes, tag) + 8).unwrap()).unwrap() + } + + #[test] + fn approved_resource_simple_case_is_derived_from_fixed_byte_metadata() { + // SHA075e71..., maxp.numGlyphs2620; long loca[0..2]=[0,76,76]. + // glyph0 has two simple contours; glyph1 has no bytes/components. + assert_eq!(glyph_closure(BUNDLED, 0, &[1, 1, 0]).unwrap(), [0, 1]); + assert!(glyph_closure(BUNDLED, 1, &[0]).is_err()); + assert!(glyph_closure(BUNDLED, u32::MAX, &[0]).is_err()); + assert!(glyph_closure(BUNDLED, 0, &[2620]).is_err()); + let mut changed = BUNDLED.to_vec(); + *changed.last_mut().unwrap() ^= 1; + assert!(glyph_closure(&changed, 0, &[0]).is_err()); + assert!(glyph_closure(&fixture(&[simple_glyph()], false), 0, &[0]).is_err(), "synthetic parser fixtures are not admitted fonts"); + } + + #[test] + fn literal_nested_graph_closure_is_sorted_unique_and_includes_zero() { + for short in [false, true] { + let bytes = fixture(&graph(), short); + assert_eq!(collect(&bytes, &[3, 3, 2], Limits::MAX).unwrap(), [0, 1, 2, 3]); + assert_eq!(collect(&bytes, &[1, 3], Limits::MAX).unwrap(), [0, 1, 2, 3]); + assert_eq!(collect(&bytes, &[], Limits::MAX).unwrap(), [0]); + } + } + + #[test] + fn cycles_refuse_but_shared_finished_components_are_not_cycles() { + for glyphs in [ + vec![simple_glyph(), composite(&[(3, 1)], None)], + vec![simple_glyph(), composite(&[(3, 2)], None), composite(&[(3, 1)], None)], + ] { + assert!(collect(&fixture(&glyphs, false), &[1], Limits::MAX).unwrap_err().to_string().contains("cyclic")); + } + assert_eq!(collect(&fixture(&graph(), false), &[3], Limits::MAX).unwrap(), [0, 1, 2, 3]); + } + + #[test] + fn component_argument_and_transform_widths_and_any_instruction_flag_are_read() { + for flags in [2, 3, 3 | 8, 3 | 0x40, 3 | 0x80] { + let bytes = fixture(&[simple_glyph(), composite(&[(flags, 0)], None)], false); + assert_eq!(collect(&bytes, &[1], Limits::MAX).unwrap(), [0, 1]); + } + // The FIRST component sets instructions, not the last. Both must be read. + let bytes = fixture(&[simple_glyph(), composite(&[(3 | 0x100, 0), (3, 0)], Some(&[0, 1, 2]))], false); + assert_eq!(collect(&bytes, &[1], Limits::MAX).unwrap(), [0, 1]); + // Negative contour counts denote composites; -1 is recommended, not mandatory. + let mut negative = composite(&[(3, 0)], None); + put16(&mut negative, 0, 0xfffe); + assert_eq!(collect(&fixture(&[simple_glyph(), negative], false), &[1], Limits::MAX).unwrap(), [0, 1]); + } + + #[test] + fn malformed_flags_component_ids_and_instruction_ranges_are_not_repaired() { + for flags in [3 | 0x10, 3 | 0x2000, 3 | 8 | 0x40, 3 | 0x1800, 1] { + let bytes = fixture(&[simple_glyph(), composite(&[(flags, 0)], None)], false); + assert!(collect(&bytes, &[1], Limits::MAX).is_err(), "flags{flags:x}"); + } + for bad in [ + composite(&[(3, 2)], None), // Two glyphs only: gid2 does not exist. + composite(&[(3 | 0x20, 0)], None), // MORE without another record. + composite(&[(3 | 0x100, 0)], None), // No instruction length. + vec![0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0], // Partial record. + ] { + assert!(collect(&fixture(&[simple_glyph(), bad], false), &[1], Limits::MAX).is_err()); + } + let mut instructions = composite(&[(3 | 0x100, 0)], Some(&[1])); + put16(&mut instructions, 18, 5); // At most two bytes available after padding. + assert!(collect(&fixture(&[simple_glyph(), instructions], false), &[1], Limits::MAX).is_err()); + let mut trailing = composite(&[(3, 0)], None); + trailing.push(0xff); + assert!(collect(&fixture(&[simple_glyph(), trailing], false), &[1], Limits::MAX).is_err()); + } + + #[test] + fn simple_records_require_complete_flags_coordinates_and_instruction_payload() { + let mut bad_reserved = simple_glyph(); + bad_reserved[14] |= 0x80; + let mut bad_repeat = simple_glyph(); + bad_repeat[14] = 0x39; + bad_repeat[15] = 1; // Two repetitions for one point. + let mut bad_coordinates = simple_glyph(); + bad_coordinates[14] = 1; // Needs four coordinate bytes, absent here. + let mut bad_instructions = simple_glyph(); + put16(&mut bad_instructions, 12, 20); + // The fixture pads odd lengths to even; eight bytes remain truncated. + for bad in [vec![0; 8], bad_reserved, bad_repeat, bad_coordinates, bad_instructions] { + assert!(collect(&fixture(&[bad], false), &[], Limits::MAX).is_err()); + } + assert_eq!(collect(&fixture(&[vec![0; 10]], false), &[], Limits::MAX).unwrap(), [0], "header-only zero contour"); + } + + #[test] + fn missing_duplicate_overlapping_and_out_of_payload_tables_refuse() { + let original = fixture(&[simple_glyph()], false); + for (tag, replacement) in [(*b"glyf", *b"glya"), (*b"head", *b"heae"), (*b"loca", *b"loaa"), (*b"maxp", *b"maxo")] { + let mut bad = original.clone(); + let at = record(&bad, &tag); + bad[at..at + 4].copy_from_slice(&replacement); + assert!(collect(&bad, &[], Limits::MAX).is_err()); + } + let mut duplicate = original.clone(); + duplicate[28..32].copy_from_slice(b"glyf"); + assert!(collect(&duplicate, &[], Limits::MAX).is_err()); + let mut overlap = original.clone(); + let start = table_start(&overlap, b"glyf"); + let r = record(&overlap, b"head"); + put32(&mut overlap, r + 8, start as u32); + assert!(collect(&overlap, &[], Limits::MAX).is_err()); + let mut outside = original.clone(); + put32(&mut outside, 20, u32::MAX); + assert!(collect(&outside, &[], Limits::MAX).is_err()); + for end in 0..76 { + assert!(collect(&original[..end], &[], Limits::MAX).is_err()); + } + } + + #[test] + fn loca_format_length_order_and_bounds_are_checked() { + let original = fixture(&[simple_glyph(), simple_glyph()], false); + let mut format = original.clone(); + let h = table_start(&format, b"head"); + put16(&mut format, h + 50, 2); + assert!(collect(&format, &[], Limits::MAX).is_err()); + let mut length = original.clone(); + let r = record(&length, b"loca"); + put32(&mut length, r + 12, 8); + assert!(collect(&length, &[], Limits::MAX).is_err()); + let mut descending = original.clone(); + let l = table_start(&descending, b"loca"); + put32(&mut descending, l + 4, 32); + put32(&mut descending, l + 8, 16); + assert!(collect(&descending, &[], Limits::MAX).is_err()); + let mut outside = original.clone(); + put32(&mut outside, l + 8, u32::MAX); + assert!(collect(&outside, &[], Limits::MAX).is_err()); + assert!(collect(&original, &[u16::MAX], Limits::MAX).is_err()); + assert!(collect(&fixture(&[], true), &[], Limits::MAX).is_err()); + } + + #[test] + fn table_versions_required_headers_and_directory_caps_refuse_explicitly() { + let original = fixture(&[simple_glyph()], false); + for (tag, offset, value) in [(*b"head", 0, 0), (*b"head", 12, 0), (*b"maxp", 0, 0x0000_5000)] { + let mut bad = original.clone(); + let start = table_start(&bad, &tag); + put32(&mut bad, start + offset, value); + assert!(collect(&bad, &[], Limits::MAX).is_err()); + } + let mut format = original.clone(); + let start = table_start(&format, b"head"); + put16(&mut format, start + 52, 1); + assert!(collect(&format, &[], Limits::MAX).is_err()); + for (tag, len) in [(*b"head", 53), (*b"maxp", 31)] { + let mut bad = original.clone(); + let r = record(&bad, &tag); + put32(&mut bad, r + 12, len); + assert!(collect(&bad, &[], Limits::MAX).is_err()); + } + for count in [0, 20, u16::MAX] { + let mut bad = original.clone(); + put16(&mut bad, 4, count); + assert!(collect(&bad, &[], Limits::MAX).is_err()); + } + } + + #[test] + fn closure_caps_are_inclusive_and_refusal_exposes_no_partial_result() { + let glyphs = graph(); + let bytes = fixture(&glyphs, false); + let exact = Limits { glyphs: 4, components: 3, depth: 3, bytes: 60, work: MAX_WORK }; + assert_eq!(collect(&bytes, &[3], exact).unwrap(), [0, 1, 2, 3]); + for limits in [ + Limits { glyphs: 3, ..exact }, Limits { components: 2, ..exact }, + Limits { depth: 2, ..exact }, Limits { bytes: 59, ..exact }, + Limits { work: 0, ..exact }, + ] { + assert!(collect(&bytes, &[3], limits).is_err()); + } + // Cached finished nodes must not hide a long path when used IDs are + // supplied in leaf-first order. Admission is independent of that order. + assert!(collect(&bytes, &[1, 2, 3], Limits { depth: 2, ..exact }).is_err()); + assert_eq!(collect(&bytes, &[1, 2, 3], exact).unwrap(), [0, 1, 2, 3]); + assert!(collect(&bytes, &vec![0; MAX_USED + 1], Limits::MAX).is_err()); + assert_eq!(glyphs, graph(), "borrowed fixture source unchanged"); + let mut count = usize::MAX; + assert!(charge(&mut count, 1, usize::MAX, "overflow").is_err()); + assert_eq!(count, usize::MAX); + } + + #[test] + fn per_glyph_component_limit_is_inclusive_even_for_repeated_child_ids() { + let records = vec![(3, 0); MAX_PER_GLYPH]; + assert_eq!(collect(&fixture(&[simple_glyph(), composite(&records, None)], false), &[1], Limits::MAX).unwrap(), [0, 1]); + let too_many = vec![(3, 0); MAX_PER_GLYPH + 1]; + let bytes = fixture(&[simple_glyph(), composite(&too_many, None)], false); + assert!(collect(&bytes, &[1], Limits::MAX).unwrap_err().to_string().contains("per-glyph component limit")); + } + + #[test] + fn temporary_subsetter_policy_does_not_redefine_general_closure() { + let bytes = fixture(&[simple_glyph(), composite(&[(3 | 0x100, 0), (3, 0)], Some(&[0, 1, 2]))], false); + assert_eq!(collect(&bytes, &[1], Limits::MAX).unwrap(), [0, 1]); + assert!(check_subsetter_component_instruction_flags(&bytes, &[0, 1]).unwrap_err().to_string().contains("nonfinal-only")); + let last = fixture(&[simple_glyph(), composite(&[(3, 0), (3 | 0x100, 0)], Some(&[0, 1, 2]))], false); + assert!(check_subsetter_component_instruction_flags(&last, &[0, 1]).is_ok()); + let both = fixture(&[simple_glyph(), composite(&[(3 | 0x100, 0), (3 | 0x100, 0)], Some(&[0, 1, 2]))], false); + assert!(check_subsetter_component_instruction_flags(&both, &[0, 1]).is_ok()); + assert!(check_subsetter_component_instruction_flags(&bytes, &vec![0; MAX_GLYPHS + 1]).is_err()); + } +} diff --git a/crates/io/src/paper/pdf/subset_font.rs b/crates/io/src/paper/pdf/subset_font.rs new file mode 100644 index 000000000..cef7bd532 --- /dev/null +++ b/crates/io/src/paper/pdf/subset_font.rs @@ -0,0 +1,194 @@ +//! PDF-only adapter for the explicitly reviewed subsetter 0.2.6 API. +//! +//! Takes original GIDs from the SAME admitted font bytes/face, without shaping, +//! Unicode/CID allocation, text measurement, font lookup or PDF-object emission. +//! The dependency allocates internally without a fallible allocator/budget API; +//! local caps and returned-byte checks do not prove process-OOM or panic safety. + +use subsetter::GlyphRemapper; + +use super::glyph_closure::{ + check_subset_program, check_subsetter_component_instruction_flags, glyph_closure, +}; +use crate::{IoError, MAX_DRAWING_BYTES, Result}; + +/// Fixed-resource returned-byte policy, not a bound on dependency allocations. +const MAX_SUBSET_BYTES: usize = 410_820; +/// Must not loosen the reviewed closure's own cap. +const MAX_SUBSET_GLYPHS: usize = 16_384; + +/// FontFile2 bytes and ORIGINAL font GID -> SUBSET font GID. +/// These IDs are not Unicode scalars, PDF character codes or logical clusters. +#[derive(Debug, PartialEq, Eq)] +pub(super) struct SubsetFont { + bytes: Vec, + mapping: Vec<(u16, u16)>, +} + +impl SubsetFont { + pub(super) fn bytes(&self) -> &[u8] { + &self.bytes + } + + pub(super) fn mapping(&self) -> &[(u16, u16)] { + &self.mapping + } +} + +fn invalid(message: &'static str) -> IoError { + IoError::Invalid(message.into()) +} + +/// The caller must obtain used IDs from these SAME font bytes and face. +/// Admission occurs inside the real glyph_closure; never hash the font twice. +/// Nonfinal-only composite instruction flags are temporarily unsupported by +/// this adapter, without redefining ordinary TrueType glyph-closure validity. +/// This produces a PDF CID font program, not a general installable font. +pub(super) fn subset_font(font: &[u8], face_index: u32, used: &[u16], max_bytes: usize) -> Result { + let closure = glyph_closure(font, face_index, used)?; + check_subsetter_component_instruction_flags(font, &closure)?; + + let max_bytes = max_bytes.min(MAX_SUBSET_BYTES).min(MAX_DRAWING_BYTES); + if max_bytes == 0 { + return Err(invalid("subset font byte allowance is zero")); + } + if closure.is_empty() || closure.len() > MAX_SUBSET_GLYPHS || closure.first().copied() != Some(0) { + return Err(invalid("subset font closure exceeds its contract")); + } + let mut mapping = Vec::new(); + mapping.try_reserve_exact(closure.len()).map_err(|_| invalid("cannot allocate subset font mapping"))?; + + // External API: new/remap/get allocate internally without try_reserve. + // Admission fixes the source to 2620 GIDs; local closure cap is also < u16::MAX. + let mut remapper = GlyphRemapper::new(); + for (index, old) in closure.iter().copied().enumerate() { + let expected = u16::try_from(index).map_err(|_| invalid("subset font index exceeds u16"))?; + let new = remapper.remap(old); + if new != expected || remapper.get(old) != Some(new) { + return Err(invalid("subset font remapper is not dense and ordered")); + } + mapping.push((old, new)); + } + if usize::from(remapper.num_gids()) != closure.len() + || !remapper.remapped_gids().eq(closure.iter().copied()) + { + return Err(invalid("subset font remapper does not match the complete closure")); + } + + // The dependency clones the remapper and closes components again. Passing + // the complete preflighted closure prevents it needing unreported new IDs. + let bytes = subsetter::subset(font, face_index, &remapper) + .map_err(|error| IoError::Invalid(format!("cannot subset the admitted PDF font: {error}")))?; + if bytes.is_empty() || bytes.len() > max_bytes { + return Err(invalid("subset font exceeds its returned-byte allowance")); + } + check_subset_program(&bytes, mapping.len())?; + if mapping.iter().any(|(_, new)| usize::from(*new) >= mapping.len()) { + return Err(invalid("subset font mapping is outside its program")); + } + Ok(SubsetFont { bytes, mapping }) +} + +#[cfg(test)] +mod tests { + use super::*; + + const BUNDLED: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../assets/fonts/liberation-sans/LiberationSans-Regular.ttf")); + + fn table<'a>(bytes: &'a [u8], tag: &[u8; 4]) -> &'a [u8] { + let count = u16::from_be_bytes(bytes[4..6].try_into().unwrap()); + for index in 0..usize::from(count) { + let record = 12 + index * 16; + if &bytes[record..record + 4] == tag { + let start = u32::from_be_bytes(bytes[record + 8..record + 12].try_into().unwrap()) as usize; + let len = u32::from_be_bytes(bytes[record + 12..record + 16].try_into().unwrap()) as usize; + return &bytes[start..start + len]; + } + } + panic!("required test table absent"); + } + + fn metric(bytes: &[u8], gid: usize) -> (u16, i16) { + let hhea = table(bytes, b"hhea"); + let hmtx = table(bytes, b"hmtx"); + let count = usize::from(u16::from_be_bytes(hhea[34..36].try_into().unwrap())); + assert!(count > 0); + let advance_at = gid.min(count - 1) * 4; + let lsb_at = if gid < count { gid * 4 + 2 } else { count * 4 + (gid - count) * 2 }; + ( + u16::from_be_bytes(hmtx[advance_at..advance_at + 2].try_into().unwrap()), + i16::from_be_bytes(hmtx[lsb_at..lsb_at + 2].try_into().unwrap()), + ) + } + + #[test] + fn fixed_simple_and_empty_glyphs_have_literal_original_metrics() { + // Read-only source metadata, not candidate-generated expectations: + // loca[0..2]=[0,76,76]; hmtx0=(1536,205), hmtx1=(0,0). + let result = subset_font(BUNDLED, 0, &[1, 1, 0], MAX_SUBSET_BYTES).unwrap(); + assert_eq!(result.mapping, [(0, 0), (1, 1)]); + assert_eq!(u16::from_be_bytes(table(&result.bytes, b"maxp")[4..6].try_into().unwrap()), 2); + assert_eq!(metric(&result.bytes, 0), (1536, 205)); + assert_eq!(metric(&result.bytes, 1), (0, 0)); + assert_eq!(table(&result.bytes, b"head")[18..20], [8, 0]); // 2048 units/em. + for tag in [b"cvt ", b"fpgm", b"prep"] { + assert_eq!(table(&result.bytes, tag), table(BUNDLED, tag), "opaque hint table"); + } + } + + #[test] + fn original_ids_are_separate_from_dense_subset_ids_and_order_is_stable() { + // Original loca gives empty spans for1,2,3; widths2,3 are both569. + let a = subset_font(BUNDLED, 0, &[3, 2, 3], MAX_SUBSET_BYTES).unwrap(); + let b = subset_font(BUNDLED, 0, &[2, 3], MAX_SUBSET_BYTES).unwrap(); + assert_eq!(a.mapping, [(0, 0), (2, 1), (3, 2)]); + assert_eq!(metric(&a.bytes, 0), (1536, 205)); + assert_eq!(metric(&a.bytes, 1), (569, 0)); + assert_eq!(metric(&a.bytes, 2), (569, 0)); + assert_eq!(a, b); + } + + #[test] + fn empty_request_still_embeds_notdef_without_inventing_text() { + let result = subset_font(BUNDLED, 0, &[], MAX_SUBSET_BYTES).unwrap(); + assert_eq!(result.mapping, [(0, 0)]); + assert_eq!(metric(&result.bytes, 0), (1536, 205)); + } + + #[test] + fn admission_refuses_wrong_face_changed_resource_and_invalid_gids() { + assert!(subset_font(BUNDLED, 1, &[0], MAX_SUBSET_BYTES).is_err()); + assert!(subset_font(BUNDLED, u32::MAX, &[0], MAX_SUBSET_BYTES).is_err()); + assert!(subset_font(BUNDLED, 0, &[2620], MAX_SUBSET_BYTES).is_err()); + assert!(subset_font(BUNDLED, 0, &vec![0; 65_537], MAX_SUBSET_BYTES).is_err()); + let mut changed = BUNDLED.to_vec(); + *changed.last_mut().unwrap() ^= 1; + assert!(subset_font(&changed, 0, &[0], MAX_SUBSET_BYTES).is_err()); + assert!(subset_font(&[0; 32], 0, &[0], MAX_SUBSET_BYTES).is_err()); + } + + #[test] + fn returned_byte_allowance_is_inclusive_and_never_a_preallocation_claim() { + let expected = subset_font(BUNDLED, 0, &[1], MAX_SUBSET_BYTES).unwrap(); + let exact = subset_font(BUNDLED, 0, &[1], expected.bytes.len()).unwrap(); + assert_eq!(exact, expected); + assert!(subset_font(BUNDLED, 0, &[1], expected.bytes.len() - 1).is_err()); + assert!(subset_font(BUNDLED, 0, &[1], 0).is_err()); + assert!(subset_font(BUNDLED, 0, &[1], 1).is_err()); + } + + #[test] + fn program_readback_rejects_count_mismatch_and_truncated_ranges() { + let result = subset_font(BUNDLED, 0, &[1], MAX_SUBSET_BYTES).unwrap(); + assert!(check_subset_program(&result.bytes, 3).is_err()); + assert!(check_subset_program(&result.bytes, 0).is_err()); + for end in [0, 4, 11, 12] { + assert!(check_subset_program(&result.bytes[..end], 2).is_err()); + } + // The original resource has cmap and all original glyphs, so it is not + // accepted as this two-glyph PDF-only subset just because it is admitted. + assert!(check_subset_program(BUNDLED, 2).is_err()); + assert!(check_subset_program(BUNDLED, 2620).unwrap_err().to_string().contains("cmap")); + } +} + diff --git a/crates/io/src/paper/pdf/to_unicode.rs b/crates/io/src/paper/pdf/to_unicode.rs new file mode 100644 index 000000000..b043073e4 --- /dev/null +++ b/crates/io/src/paper/pdf/to_unicode.rs @@ -0,0 +1,345 @@ +//! Deterministic ToUnicode CMap bytes for already assigned two-byte character codes. +//! +//! ISO 32000-1:2008, 9.10.3: UTF-16BE destinations, at most 512 bytes each. +//! Adobe Technical Note 5411, 1.4.1: at most 100 mappings per bfchar block. +//! This does not assign CIDs/GIDs, shape text, subset/embed a font or infer clusters. +//! The caller must use a matching TWO-BYTE font encoding; this is not for simple +//! one-byte fonts. Text is literal: no normalization, replacement or implicit BOM. +//! +//! https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf +//! https://pdfa.org/norm-refs/5411.ToUnicode.pdf + +use crate::{IoError, MAX_DRAWING_BYTES, Result}; + +const MAX_MAPPINGS: usize = 65_536; +const MAX_CLUSTER_UTF8_BYTES: usize = 768; // 256 BMP scalars of three UTF-8 bytes. +const MAX_DESTINATION_UNITS: usize = 256; // The specification's 512 destination bytes. +const MAX_INPUT_BYTES: usize = 4 << 20; +const MAX_UTF16_UNITS: usize = 1 << 20; +const MAX_OUTPUT_BYTES: usize = 8 << 20; +const MAX_WORK: usize = 64 << 20; +const BLOCK_MAPPINGS: usize = 100; + +const PREFIX: &[u8] = b"/CIDInit /ProcSet findresource begin +12 dict begin +begincmap +/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def +/CMapName /SolidCraft-ToUnicode16 def +/CMapType 2 def +1 begincodespacerange +<0000> +endcodespacerange +"; +const SUFFIX: &[u8] = b"endcmap +CMapName currentdict /CMap defineresource pop +end +end +"; +const BLOCK_BEGIN: &[u8] = b" beginbfchar\n"; +const BLOCK_END: &[u8] = b"endbfchar\n"; + +fn invalid(message: &'static str) -> IoError { + IoError::Invalid(message.into()) +} + +#[derive(Clone, Copy)] +struct Limits { + mappings: usize, + input_bytes: usize, + utf16_units: usize, + output_bytes: usize, + work: usize, +} + +impl Limits { + const MAX: Self = Self { + mappings: MAX_MAPPINGS, + input_bytes: MAX_INPUT_BYTES, + utf16_units: MAX_UTF16_UNITS, + output_bytes: MAX_OUTPUT_BYTES, + work: MAX_WORK, + }; + + fn bounded(self) -> Self { + Self { + mappings: self.mappings.min(MAX_MAPPINGS), + input_bytes: self.input_bytes.min(MAX_INPUT_BYTES), + utf16_units: self.utf16_units.min(MAX_UTF16_UNITS), + output_bytes: self.output_bytes.min(MAX_OUTPUT_BYTES).min(MAX_DRAWING_BYTES), + work: self.work.min(MAX_WORK), + } + } +} + +/// Serialize literal Unicode clusters in strictly increasing source-code order. +/// Codes 0 and 65535 are valid; distinct codes may have the same destination. +/// Empty clusters, duplicate/out-of-order codes and over-budget input are refused. +/// max_bytes can only tighten the hard output cap; it cannot increase it. +/// Returns a complete CMap stream payload, not a PDF object/dictionary or font. +pub(super) fn write_to_unicode(mappings: &[(u16, &str)], max_bytes: usize) -> Result> { + serialize(mappings, Limits { output_bytes: max_bytes, ..Limits::MAX }) +} + +/// Checked budget changes are atomic even when the proposed addition fails. +fn charge(total: &mut usize, amount: usize, limit: usize, message: &'static str) -> Result<()> { + let next = total.checked_add(amount).filter(|next| *next <= limit).ok_or_else(|| invalid(message))?; + *total = next; + Ok(()) +} + +fn preflight(mappings: &[(u16, &str)], limits: Limits) -> Result { + if mappings.len() > limits.mappings { + return Err(invalid("ToUnicode mapping count exceeds its limit")); + } + let (mut input, mut work, mut previous) = (0usize, 0usize, None); + // Length/order-only pass before scanning ANY Unicode payload or allocating output. + for &(code, text) in mappings { + if previous.is_some_and(|previous| code <= previous) { + return Err(invalid("ToUnicode codes must be unique and strictly increasing")); + } + previous = Some(code); + if text.is_empty() { + return Err(invalid("ToUnicode requires a nonempty literal Unicode cluster")); + } + if text.len() > MAX_CLUSTER_UTF8_BYTES { + return Err(invalid("ToUnicode cluster exceeds its UTF-8 input bound")); + } + charge(&mut input, text.len(), limits.input_bytes, "ToUnicode UTF-8 input exceeds its limit")?; + // A conservative linear-work budget for validation and both UTF-16 passes, + // including four hexadecimal bytes per unit. Not a wall-clock guarantee. + let cost = text.len().checked_mul(8).and_then(|cost| cost.checked_add(32)) + .ok_or_else(|| invalid("ToUnicode work count overflow"))?; + charge(&mut work, cost, limits.work, "ToUnicode work exceeds its limit")?; + } + let mut bytes = 0usize; + charge(&mut bytes, PREFIX.len(), limits.output_bytes, "ToUnicode output exceeds its limit")?; + charge(&mut bytes, SUFFIX.len(), limits.output_bytes, "ToUnicode output exceeds its limit")?; + let mut total_units = 0usize; + for block in mappings.chunks(BLOCK_MAPPINGS) { + let digits = if block.len() == 100 { 3 } else if block.len() >= 10 { 2 } else { 1 }; + charge(&mut bytes, digits + BLOCK_BEGIN.len() + BLOCK_END.len(), limits.output_bytes, "ToUnicode output exceeds its limit")?; + for &(_, text) in block { + // Bounded even before the cumulative destination budget is checked. + let units = text.encode_utf16().take(MAX_DESTINATION_UNITS + 1).count(); + if units > MAX_DESTINATION_UNITS { + return Err(invalid("ToUnicode destination exceeds 512 UTF-16BE bytes")); + } + charge(&mut total_units, units, limits.utf16_units, "ToUnicode UTF-16 payload exceeds its limit")?; + // < + four ASCII hex bytes per UTF-16 unit + > and newline. + let row_bytes = units.checked_mul(4).and_then(|hex| hex.checked_add(10)) + .ok_or_else(|| invalid("ToUnicode output byte count overflow"))?; + charge(&mut bytes, row_bytes, limits.output_bytes, "ToUnicode output exceeds its limit")?; + } + } + Ok(bytes) +} + +struct Output { + bytes: Vec, + limit: usize, +} + +impl Output { + fn new(limit: usize) -> Result { + if limit > MAX_OUTPUT_BYTES.min(MAX_DRAWING_BYTES) { + return Err(invalid("ToUnicode output exceeds its limit")); + } + let mut bytes = Vec::new(); + bytes.try_reserve_exact(limit).map_err(|_| invalid("ToUnicode output allocation failed"))?; + Ok(Self { bytes, limit }) + } + + fn append(&mut self, text: &[u8]) -> Result<()> { + if self.bytes.len().checked_add(text.len()).is_none_or(|len| len > self.limit) { + return Err(invalid("ToUnicode output exceeds its limit")); + } + self.bytes.try_reserve(text.len()).map_err(|_| invalid("ToUnicode output allocation failed"))?; + self.bytes.extend_from_slice(text); + Ok(()) + } + + fn block_count(&mut self, count: usize) -> Result<()> { + if count == 0 || count > BLOCK_MAPPINGS { + return Err(invalid("ToUnicode bfchar block must contain 1 to 100 mappings")); + } + if count == 100 { + self.append(b"100") + } else { + let count = u8::try_from(count).map_err(|_| invalid("invalid ToUnicode block count"))?; + if count >= 10 { + self.append(&[b'0' + count / 10, b'0' + count % 10]) + } else { + self.append(&[b'0' + count]) + } + } + } +} + +/// The nibbles are masked, so every returned byte is an ASCII hexadecimal digit. +fn hex16(unit: u16) -> [u8; 4] { + [12, 8, 4, 0].map(|shift| { + let nibble = ((unit >> shift) & 15) as u8; + if nibble < 10 { b'0' + nibble } else { b'A' + nibble - 10 } + }) +} + +fn serialize(mappings: &[(u16, &str)], limits: Limits) -> Result> { + let expected = preflight(mappings, limits.bounded())?; + let mut out = Output::new(expected)?; + out.append(PREFIX)?; + for block in mappings.chunks(BLOCK_MAPPINGS) { + out.block_count(block.len())?; + out.append(BLOCK_BEGIN)?; + for &(code, text) in block { + out.append(b"<")?; + out.append(&hex16(code))?; + out.append(b"> <")?; + for unit in text.encode_utf16() { + out.append(&hex16(unit))?; + } + out.append(b">\n")?; + } + out.append(BLOCK_END)?; + } + out.append(SUFFIX)?; + if out.bytes.len() != expected { + return Err(invalid("ToUnicode output size differs from its preflight")); + } + Ok(out.bytes) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ascii_stream_is_literal_complete_and_deterministic() { + let expected = b"/CIDInit /ProcSet findresource begin\n12 dict begin\nbegincmap\n/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n/CMapName /SolidCraft-ToUnicode16 def\n/CMapType 2 def\n1 begincodespacerange\n<0000> \nendcodespacerange\n1 beginbfchar\n<0042> <0041>\nendbfchar\nendcmap\nCMapName currentdict /CMap defineresource pop\nend\nend\n"; + assert_eq!(write_to_unicode(&[(0x42, "A")], MAX_OUTPUT_BYTES).unwrap(), expected); + assert_eq!(write_to_unicode(&[(0x42, "A")], MAX_OUTPUT_BYTES).unwrap(), expected); + } + + #[test] + fn bmp_supplementary_and_literal_clusters_keep_their_sequence() { + let maps = [(1, "Å"), (2, "中"), (3, "🚀"), (4, "e\u{301}"), (5, "ffi"), (6, "\u{feff}\0"), (7, "\u{10ffff}")]; + let bytes = write_to_unicode(&maps, MAX_OUTPUT_BYTES).unwrap(); + let text = std::str::from_utf8(&bytes).unwrap(); + for row in [ + "<0001> <00C5>\n", "<0002> <4E2D>\n", "<0003> \n", + "<0004> <00650301>\n", "<0005> <006600660069>\n", + "<0006> \n", "<0007> \n", + ] { + assert_eq!(text.matches(row).count(), 1, "{row}"); + } + assert!(!text.contains(""), "no implicit BOM"); + assert!(!text.contains("<00E9>"), "no normalization of e + combining acute"); + assert!(bytes.is_ascii()); + } + + #[test] + fn codes_are_not_cids_or_glyph_ids_and_both_u16_extremes_are_allowed() { + let bytes = write_to_unicode(&[(0, "A"), (u16::MAX, "A")], MAX_OUTPUT_BYTES).unwrap(); + let text = std::str::from_utf8(&bytes).unwrap(); + assert!(text.contains("<0000> <0041>\n")); + assert!(text.contains(" <0041>\n")); + assert!(!text.contains("begincidchar")); + for maps in [vec![(7, "A"), (7, "B")], vec![(8, "A"), (7, "B")], vec![(1, "")]] { + assert!(write_to_unicode(&maps, MAX_OUTPUT_BYTES).is_err()); + } + } + + #[test] + fn empty_mapping_table_has_codespace_but_no_empty_bfchar_block() { + let bytes = write_to_unicode(&[], MAX_OUTPUT_BYTES).unwrap(); + let text = std::str::from_utf8(&bytes).unwrap(); + assert!(text.contains("1 begincodespacerange\n<0000> \nendcodespacerange\n")); + assert!(!text.contains("beginbfchar")); + assert!(text.ends_with("end\nend\n")); + } + + #[test] + fn destination_limit_counts_utf16_units_instead_of_scalars_or_utf8_bytes() { + for value in ["A".repeat(256), "中".repeat(256), "🚀".repeat(128)] { + assert!(write_to_unicode(&[(1, &value)], MAX_OUTPUT_BYTES).is_ok()); + } + for value in ["A".repeat(257), "🚀".repeat(129)] { + assert!(write_to_unicode(&[(1, &value)], MAX_OUTPUT_BYTES).unwrap_err().to_string().contains("512 UTF-16BE")); + } + let too_many_bytes = "中".repeat(257); + assert!(write_to_unicode(&[(1, &too_many_bytes)], MAX_OUTPUT_BYTES).unwrap_err().to_string().contains("UTF-8 input bound")); + } + + #[test] + fn bfchar_blocks_end_at_one_hundred_and_never_emit_a_zero_block() { + for (count, expected) in [(99, vec![99]), (100, vec![100]), (101, vec![100, 1]), (200, vec![100, 100]), (201, vec![100, 100, 1])] { + let maps: Vec<_> = (0..count).map(|code| (code as u16, "x")).collect(); + let bytes = write_to_unicode(&maps, MAX_OUTPUT_BYTES).unwrap(); + let text = std::str::from_utf8(&bytes).unwrap(); + let counts: Vec = text.lines().filter_map(|line| line.strip_suffix(" beginbfchar")).map(|n| n.parse().unwrap()).collect(); + assert_eq!(counts, expected); + assert_eq!(text.lines().filter(|line| line.starts_with('<') && line.ends_with("<0078>")).count(), count); + assert_eq!(text.lines().filter(|line| *line == "endbfchar").count(), expected.len()); + } + } + + #[test] + fn literal_operator_text_is_only_hex_data_and_cannot_close_the_cmap() { + let bytes = write_to_unicode(&[(1, "\nendcmap\n%<>")], MAX_OUTPUT_BYTES).unwrap(); + let text = std::str::from_utf8(&bytes).unwrap(); + assert!(text.contains("<0001> <000A0065006E00640063006D00610070000A0025003C003E>\n")); + assert_eq!(text.lines().filter(|line| *line == "endcmap").count(), 1); + assert!(!text.contains("\n%<>")); + } + + #[test] + fn each_budget_accepts_its_inclusive_boundary_and_refuses_one_less() { + let maps = [(1, "A")]; + let expected = write_to_unicode(&maps, MAX_OUTPUT_BYTES).unwrap(); + let exact = Limits { mappings: 1, input_bytes: 1, utf16_units: 1, output_bytes: expected.len(), work: 40 }; + assert_eq!(serialize(&maps, exact).unwrap(), expected); + assert_eq!(write_to_unicode(&maps, expected.len()).unwrap(), expected); + assert!(write_to_unicode(&maps, expected.len() - 1).is_err()); + assert_eq!(write_to_unicode(&maps, usize::MAX).unwrap(), expected, "caller cannot remove hard caps"); + for limits in [ + Limits { mappings: 0, ..exact }, Limits { input_bytes: 0, ..exact }, + Limits { utf16_units: 0, ..exact }, Limits { output_bytes: expected.len() - 1, ..exact }, + Limits { work: 39, ..exact }, + ] { + assert!(serialize(&maps, limits).is_err()); + } + } + + #[test] + fn input_work_admission_precedes_unicode_payload_scanning_and_output() { + // Its 257 UTF-16 units would fail later; length/work admission wins first. + let cluster = "A".repeat(257); + let maps = [(1, cluster.as_str())]; + assert!(serialize(&maps, Limits { input_bytes: 256, ..Limits::MAX }).unwrap_err().to_string().contains("UTF-8 input exceeds")); + assert!(serialize(&maps, Limits { work: 32, ..Limits::MAX }).unwrap_err().to_string().contains("work exceeds")); + assert_eq!(maps[0].1, cluster); + } + + #[test] + fn cumulative_budgets_and_count_are_independent_of_per_cluster_admission() { + let maps = [(1, "A"), (2, "B")]; + assert!(serialize(&maps, Limits { input_bytes: 1, ..Limits::MAX }).is_err()); + assert!(serialize(&maps, Limits { utf16_units: 1, ..Limits::MAX }).is_err()); + let too_many = vec![(0, "A"); MAX_MAPPINGS + 1]; + assert!(write_to_unicode(&too_many, MAX_OUTPUT_BYTES).unwrap_err().to_string().contains("mapping count")); + assert!(serialize(&too_many, Limits { mappings: usize::MAX, ..Limits::MAX }).unwrap_err().to_string().contains("mapping count")); + } + + #[test] + fn checked_budget_and_output_refusals_preserve_their_private_state() { + let mut count = usize::MAX; + assert!(charge(&mut count, 1, usize::MAX, "overflow").is_err()); + assert_eq!(count, usize::MAX); + let mut out = Output::new(2).unwrap(); + out.append(b"AB").unwrap(); + assert!(out.append(b"C").is_err()); + assert_eq!(out.bytes, b"AB"); + assert!(out.block_count(0).is_err()); + assert!(out.block_count(101).is_err()); + assert_eq!(out.bytes, b"AB"); + } +} diff --git a/crates/io/src/paper/pdf_tests.rs b/crates/io/src/paper/pdf_tests.rs index 44a839a0a..39c6a6767 100644 --- a/crates/io/src/paper/pdf_tests.rs +++ b/crates/io/src/paper/pdf_tests.rs @@ -786,3 +786,140 @@ fn merged_pdf_run_limits_charge_transitions_and_the_final_close() { assert!(super::content_with_limit(&grid.groups, prefix + tail.len() - 1).is_err()); } } + +// Independent acceptance of the coordinated PRIVATE ToUnicode helper proposal. +// Pending module hook; no font/CID/GID assignment or PDF extraction claim. +// Byte layout/name below is a proposed project contract, not a universal PDF layout. +const P2_ORACLE_PREFIX: &str = concat!( + "/CIDInit /ProcSet findresource begin\n", + "12 dict begin\nbegincmap\n", + "/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n", + "/CMapName /SolidCraft-ToUnicode16 def\n/CMapType 2 def\n", + "1 begincodespacerange\n<0000> \nendcodespacerange\n", +); +const P2_ORACLE_SUFFIX: &str = "endcmap\nCMapName currentdict /CMap defineresource pop\nend\nend\n"; + +// Constructs only expected fixture text from literal grammar/data. Never calls +// the writer, its constants, hex16, encode_utf16 or a serializer under test. +fn p2_oracle_cmap(body: &str) -> Vec { + [P2_ORACLE_PREFIX.as_bytes(), body.as_bytes(), P2_ORACLE_SUFFIX.as_bytes()].concat() +} + +#[test] +fn p2_tounicode_literal_utf16be_covers_ascii_bmp_supplementary_and_clusters() { + let entries = [ + (0x0000, "\0"), + (0x0001, "A"), + (0x00ff, "\u{00c5}"), + (0x0100, "\u{4e2d}"), + (0x1234, "\u{1f600}"), + (0x7ffe, "e\u{301}\u{327}"), + (0x8000, "ffi"), + (0x8001, "\u{feff}\0"), + (0xffff, "\u{10ffff}"), + ]; + let before = entries; + // U+1F600 - 0x10000 = 0xF600 -> high0x3D/low0x200 -> D83D DE00. + // Combining sequence remains 0065 0301 0327: no NFC or mark reordering. + let expected = p2_oracle_cmap(concat!( + "9 beginbfchar\n", + "<0000> <0000>\n<0001> <0041>\n<00FF> <00C5>\n<0100> <4E2D>\n", + "<1234> \n<7FFE> <006503010327>\n<8000> <006600660069>\n", + "<8001> \n \nendbfchar\n", + )); + let actual = super::to_unicode::write_to_unicode(&entries, expected.len()).unwrap(); + assert_eq!(actual, expected); + assert!(actual.is_ascii()); + assert_eq!(entries, before); + assert_eq!(super::to_unicode::write_to_unicode(&entries, expected.len()).unwrap(), expected); +} + +#[test] +fn p2_tounicode_one_hundred_and_one_mapping_blocks_have_exact_independent_bytes() { + for count in [100_u16, 101_u16] { + let entries: Vec<_> = (0..count).map(|code| (code, "A")).collect(); + let before = entries.clone(); + let mut body = String::from("100 beginbfchar\n"); + // These integer labels are the source data, not a Unicode encoding oracle. + for code in 0..100_u16 { + body.push_str(&format!("<{code:04X}> <0041>\n")); + } + body.push_str("endbfchar\n"); + if count == 101 { + body.push_str("1 beginbfchar\n<0064> <0041>\nendbfchar\n"); + } + let expected = p2_oracle_cmap(&body); + assert_eq!(super::to_unicode::write_to_unicode(&entries, expected.len()).unwrap(), expected); + assert!(super::to_unicode::write_to_unicode(&entries, expected.len() - 1).is_err()); + assert_eq!(entries, before); + } +} + +#[test] +fn p2_tounicode_destination_boundary_is_512_decoded_bytes_not_utf8_or_hex_length() { + for (cluster, literal_hex) in [ + ("A".repeat(256), "0041".repeat(256)), + ("\u{0800}".repeat(256), "0800".repeat(256)), + ("\u{1f600}".repeat(128), "D83DDE00".repeat(128)), + ] { + // Every expected destination has1024 hex characters, representing512 bytes. + // UTF-8 input lengths are256,768,512 respectively, not the encoded limit. + assert_eq!(literal_hex.len(), 1024); + let body = format!("1 beginbfchar\n<0042> <{literal_hex}>\nendbfchar\n"); + let expected = p2_oracle_cmap(&body); + assert_eq!( + super::to_unicode::write_to_unicode(&[(0x42, cluster.as_str())], expected.len()).unwrap(), + expected, + ); + } + //257 BMP units ->514 bytes;129 supplementary scalars ->516 bytes. + // Both inputs fit the separate768-byte UTF-8 per-cluster ceiling. + for cluster in ["A".repeat(257), "\u{1f600}".repeat(129)] { + assert!(super::to_unicode::write_to_unicode(&[(0x42, cluster.as_str())], 4096).is_err()); + } +} + +#[test] +fn p2_tounicode_u16_extremes_repeated_destinations_and_strict_source_order() { + let entries = [(0x0000, "A"), (0x00ff, "A"), (0x0100, "A"), (0xffff, "A")]; + let expected = p2_oracle_cmap(concat!( + "4 beginbfchar\n<0000> <0041>\n<00FF> <0041>\n", + "<0100> <0041>\n <0041>\nendbfchar\n", + )); + assert_eq!(super::to_unicode::write_to_unicode(&entries, expected.len()).unwrap(), expected); + for invalid in [ + vec![(1, "A"), (1, "A")], + vec![(1, "A"), (1, "B")], + vec![(2, "A"), (1, "B")], + vec![(0xffff, "A"), (0x0000, "B")], + vec![(0x0000, "")], + vec![(0x0000, "A"), (0x0001, "")], + ] { + let before = invalid.clone(); + assert!(super::to_unicode::write_to_unicode(&invalid, 4096).is_err()); + assert_eq!(invalid, before); + } +} + +#[test] +fn p2_tounicode_cumulative_output_budget_is_based_on_literal_bytes_including_tail() { + let entries = [(0x42, "A"), (0x43, "\u{1f600}")]; + let expected = p2_oracle_cmap("2 beginbfchar\n<0042> <0041>\n<0043> \nendbfchar\n"); + let complete_length = expected.len(); + assert_eq!(super::to_unicode::write_to_unicode(&entries, complete_length).unwrap(), expected); + assert_eq!(super::to_unicode::write_to_unicode(&entries, complete_length + 1).unwrap(), expected); + for allowance in [0, 1, P2_ORACLE_PREFIX.len(), complete_length - P2_ORACLE_SUFFIX.len(), complete_length - 1] { + assert!(super::to_unicode::write_to_unicode(&entries, allowance).is_err(), "budget{allowance}"); + } + // A legal U+0000 is payload, not a C-string terminator or an empty cluster. + let null_expected = p2_oracle_cmap("1 beginbfchar\n<0042> <0000>\nendbfchar\n"); + assert_eq!(super::to_unicode::write_to_unicode(&[(0x42, "\0")], null_expected.len()).unwrap(), null_expected); +} + +#[test] +fn p2_tounicode_empty_table_has_complete_codespace_and_no_zero_mapping_block() { + let expected = p2_oracle_cmap(""); + assert_eq!(super::to_unicode::write_to_unicode(&[], expected.len()).unwrap(), expected); + assert!(super::to_unicode::write_to_unicode(&[], expected.len() - 1).is_err()); + assert!(super::to_unicode::write_to_unicode(&[], 0).is_err()); +}