tests(fonts,render): add custom CMap wire and shared RasterOptions max_coverage parity tests (#7968)

* tests(fonts,render): add custom CMap wire and shared RasterOptions max_coverage parity tests (#778)

* fonts: expose Metrics::cid as pub(crate) for embedding test
This commit is contained in:
Dylan M. Taylor
2026-11-20 12:22:26 -05:00
committed by GitHub
parent 888135baec
commit df18640982
3 changed files with 80 additions and 1 deletions
+47
View File
@@ -468,4 +468,51 @@ mod tests {
assert_eq!(programs, 1, "one program for both pages");
assert!(doc.scan_objects().count() > objects, "the second page gets its own resource");
}
#[test]
fn custom_cmap_wire_builders_round_trip_through_metrics_across_chunks_and_surrogates() {
let mut doc = Document::new_empty();
let mut cff_widths = std::collections::BTreeMap::new();
let mut entries: Vec<(u16, u16, f64, String)> =
vec![(1, 301, 512.0, "A".into()), (2, 302, 625.0, "é".into()), (3, 303, 750.0, "fi".into()), (4, 304, 880.0, "𠀋".into())];
// Exceed the 100-entry CMap chunk size so multi-block begincidchar/beginbfchar output is exercised.
for i in 5..=125u16 {
let ch = char::from_u32(0x4E00 + u32::from(i)).unwrap();
entries.push((i, 300 + i, 500.0 + f64::from(i), ch.to_string()));
}
for &(_, glyph, width, _) in &entries {
cff_widths.insert(glyph, width);
}
let cid_mappings: Vec<_> = entries
.iter()
.map(|(cid, glyph, width, unicode)| CidMapping { cid: *cid, glyph: *glyph, width: *width, unicode: unicode.as_str() })
.collect();
let cid_encoding = build_cid_encoding_cmap(&cid_mappings);
let to_unicode = build_to_unicode_cmap(&cid_mappings);
let program = font_program_stream(&mut doc, b"OTTO", true);
let descriptor = build_font_descriptor(&FontDescriptorSpec {
font_name: "CustomCffTest",
family: "CustomCffTest",
italic_angle: 0.0,
ascent: 800.0,
descent: -200.0,
cap_height: 700.0,
bbox: [0.0, -200.0, 1000.0, 800.0],
cff: true,
program,
});
let desc = Object::Ref(doc.add(Object::Dict(descriptor)));
let cid = build_cid_font_dict(&mut doc, "CustomCffTest", desc, true, cff_widths, Vec::new(), Vec::new());
let descendant = Object::Ref(doc.add(Object::Dict(cid)));
let top = build_type0_font_dict(&mut doc, "CustomCffTest", descendant, Some(cid_encoding.into_bytes()), to_unicode.into_bytes());
let m = Metrics::from_dict(&doc, &top);
for &(cid_code, glyph, width, ref expected) in &entries {
let code = u32::from(cid_code);
let bytes = cid_code.to_be_bytes();
assert_eq!(m.decode(&bytes), *expected, "cid {cid_code} ToUnicode");
assert_eq!(m.cid(code), u32::from(glyph), "cid {cid_code} PCGlyphs mapping");
assert!((m.width(code) - width / 1000.0).abs() < 1e-8, "cid {cid_code} width");
}
}
}
+1 -1
View File
@@ -1116,7 +1116,7 @@ impl Metrics {
out
}
fn cid(&self, code: u32) -> u32 {
pub(crate) fn cid(&self, code: u32) -> u32 {
if self.cid_map.is_empty() {
return code;
}
@@ -181,3 +181,35 @@ fn a_layer_clip_past_the_coverage_is_itself_left_out() {
assert!(ctx.coverage_full(), "the clip, past the limit, is left out and that is said");
ctx.pop_layer();
}
#[test]
fn shared_raster_options_enforces_max_coverage() {
let pdf = doubling_forms(10);
let parsed = Pdf::new(Arc::new(pdf)).unwrap();
let render_with = |max_coverage: Option<usize>| {
let truncated = Arc::new(Mutex::new(false));
let seen = truncated.clone();
let settings = InterpreterSettings {
warning_sink: Arc::new(move |w| {
if matches!(w, InterpreterWarning::ContentTruncated) {
*seen.lock().unwrap() = true;
}
}),
..Default::default()
};
let options = RasterOptions { max_coverage, ..Default::default() };
let [w, h] = shared_raster::dimensions(&parsed.pages()[0], &options).unwrap();
let mut buf = vec![0u8; (w as usize) * (h as usize) * 4];
shared_raster::render_into(&parsed.pages()[0], &RenderCache::new(), &settings, &options, &mut buf).unwrap();
let cut = *truncated.lock().unwrap();
(buf, cut)
};
let (whole, whole_cut) = render_with(None);
assert!(!whole_cut, "unlimited render leaves nothing out");
let (kept, kept_cut) = render_with(Some(200_000));
assert!(kept_cut, "shared RasterOptions::max_coverage truncates and warns");
assert_eq!(kept.len(), whole.len());
let blue = |px: &[u8]| px.as_chunks::<4>().0.iter().filter(|p| p[2] > 200 && p[0] < 50).count();
assert!(blue(&kept) > 10_000, "circles drawn before the limit stay: {}", blue(&kept));
}