EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust
Merge main into #1478; keep both sides of crates/pdf/src/cff.rs
bflatastic9bdf05f34b89
diff --cc crates/pdf/src/cff.rsindex 59b49ec03,275b75aa4..61bb7f9eb--- a/crates/pdf/src/cff.rs+++ b/crates/pdf/src/cff.rs@@@ -1051,45 -1062,87 +1064,129 @@@ mod testsassert_eq!(p.bounding_box(), kurbo::Rect::new(50.0, 0.0, 550.0, 500.0));}+ /// An `endchar` seac whose base and accent name its own glyph used to recurse until the+ /// stack overflowed and aborted the app; the depth limit cuts it off like the Type 1+ /// interpreter's.+ #[test]+ fn seac_cycles_are_cut_off_not_a_stack_overflow() {+ let enc = |v: i32| -> Vec<u8> {+ if (-107..=107).contains(&v) {+ vec![(v + 139) as u8]+ } else {+ let mut o = vec![28];+ o.extend_from_slice(&(v as i16).to_be_bytes());+ o+ }+ };+ // `w adx ady bchar achar endchar`: both components are 'A' (standard code 65), this glyph.+ let mut cs = vec![];+ for v in [500, 0, 0, 65, 65] {+ cs.extend(enc(v));+ }+ cs.push(14);+ let font = write_test_cff(&[(".notdef", vec![14]), ("A", cs)]);+ let cff = Cff::parse(&font).unwrap();+ let (p, _) = cff.outline(cff.gid_by_name("A").unwrap()).unwrap();+ assert!(p.elements().is_empty());+ }++ /// A CFF whose top DICT points its Private DICT at offsets near `usize::MAX`: `size` and `off`+ /// come from the file, and reading the Private DICT must not overflow `off + size`.+ #[test]+ fn huge_private_dict_offsets_do_not_overflow() {+ fn idx(items: &[Vec<u8>]) -> Vec<u8> {+ let mut out = (items.len() as u16).to_be_bytes().to_vec();+ if items.is_empty() {+ return out;+ }+ out.push(4);+ let mut off = 1u32;+ out.extend_from_slice(&off.to_be_bytes());+ for it in items {+ off += it.len() as u32;+ out.extend_from_slice(&off.to_be_bytes());+ }+ for it in items {+ out.extend_from_slice(it);+ }+ out+ }+ let int = |v: i32| {+ let mut o = vec![29];+ o.extend_from_slice(&v.to_be_bytes());+ o+ };+ // The BCD real "1E300": a size and an offset that both saturate at `usize::MAX`.+ let huge = vec![0x1E, 0x1B, 0x30, 0x0F];+ let header = [1u8, 0, 4, 4];+ let names = idx(&[b"Test".to_vec()]);+ let strings = idx(&[]);+ let gsubrs = idx(&[]);+ let charset = vec![0u8];+ let cs = idx(&[vec![14]]); // one `.notdef` charstring: endchar+ let top_len = 5 + 1 + 5 + 1 + huge.len() * 2 + 1;+ let top_index_len = 2 + 1 + 8 + top_len;+ let base = header.len() + names.len() + top_index_len + strings.len() + gsubrs.len();+ let cs_off = base + charset.len();+ let mut top = vec![];+ top.extend(int(0));+ top.push(15); // charset: ISOAdobe+ top.extend(int(cs_off as i32));+ top.push(17); // CharStrings+ top.extend(&huge);+ top.extend(&huge);+ top.push(18); // Private: [size, offset]+ assert_eq!(top.len(), top_len);+ let mut out = header.to_vec();+ out.extend(names);+ out.extend(idx(&[top]));+ out.extend(strings);+ out.extend(gsubrs);+ out.extend(charset);+ out.extend(cs);+ let cff = Cff::parse(&out).expect("the damaged Private DICT is skipped, the font parses");+ assert_eq!(cff.glyph_count(), 1);+ }+++ /// A four-operand `endchar` (`adx ady bchar achar`, no width) is a seac like the Type 1+ /// interpreter's: `endchar` must not eat `adx` as a width, and a five-operand one reads its+ /// width first.+ #[test]+ fn endchar_reads_its_width_by_operand_count() {+ let enc = |v: i32| -> Vec<u8> {+ if (-107..=107).contains(&v) {+ vec![(v + 139) as u8]+ } else {+ let mut o = vec![28];+ o.extend_from_slice(&(v as i16).to_be_bytes());+ o+ }+ };+ // The base glyph: a square drawn without a width (`500 500 -500 hlineto endchar`).+ let mut square = vec![];+ for v in [500, 500, -500] {+ square.extend(enc(v));+ }+ square.extend([6, 14]);+ // Four operands: `adx ady bchar achar endchar`, no width to take.+ let mut seac = vec![];+ for v in [10, 20, 65, 65] {+ seac.extend(enc(v));+ }+ seac.push(14);+ let font = write_test_cff(&[(".notdef", vec![14]), ("A", square.clone()), ("Aacute", seac)]);+ let cff = Cff::parse(&font).unwrap();+ let (p, _) = cff.outline(cff.gid_by_name("Aacute").unwrap()).unwrap();+ assert!(!p.elements().is_empty(), "the base glyph of the seac is part of the outline");+ // Five operands: the first is the width, the last four the seac.+ let mut wide = vec![];+ for v in [600, 0, 0, 65, 65] {+ wide.extend(enc(v));+ }+ wide.push(14);+ let font = write_test_cff(&[(".notdef", vec![14]), ("A", square), ("Awide", wide)]);+ let cff = Cff::parse(&font).unwrap();+ let (_, w) = cff.outline(cff.gid_by_name("Awide").unwrap()).unwrap();+ assert_eq!(w, 600.0);+ }}