Merge pull request #8375 from dotCooCoo/perf/pdf-type3-color-scan-once

PDF import: a Type 3 font's glyph procedures are checked for color once per document
This commit is contained in:
BFlat
2026-11-18 12:57:14 +07:00
committed by GitHub
3 changed files with 96 additions and 39 deletions
+45 -39
View File
@@ -663,47 +663,11 @@ impl Walker<'_, '_> {
self.out.type3_cache_map.insert(hash, (f_key, to_unicode));
}
}
let is_colour = if is_type3 {
let mut colour = false;
let mut seen_streams = HashSet::new();
let mut decoded_bytes = 0usize;
const MAX_CHARPROC_BYTES: usize = 16 << 20;
if let Some(char_procs) = f.get::<Dict<'_>>(b"CharProcs") {
for key in char_procs.keys() {
if let Some(stream) = char_procs.get::<Stream<'_>>(key.as_ref()) {
if !seen_streams.insert(stream.cache_key()) {
continue;
}
if decoded_bytes >= MAX_CHARPROC_BYTES {
colour = true;
break;
}
if let Ok(data) = stream.decoded_within(MAX_CHARPROC_BYTES.saturating_sub(decoded_bytes)) {
decoded_bytes = decoded_bytes.saturating_add(data.len());
let mut iter = TypedIter::new(data.as_ref());
while let Some(op) = iter.next() {
match op {
TypedInstruction::ColorGlyph(_) => {
colour = true;
break;
}
TypedInstruction::ShapeGlyph(_) => break,
_ => {}
}
}
if colour {
break;
}
}
}
}
}
colour
} else {
false
};
// A font is noted once for the document, so its glyph procedures are read only the
// first time any page or form lists it.
self.fonts.entry(f_key).or_insert_with(|| {
let embedded = embeds_glyphs(&f);
let is_colour = is_type3 && sets_glyph_colors(&f);
FontNote { name, embedded, vertical: vertical_metrics(&f, embedded), is_type3, to_unicode, is_colour }
});
}
@@ -1108,6 +1072,48 @@ fn font_name(f: &Dict<'_>) -> Option<String> {
Some(stripped.unwrap_or(base))
}
/// The most bytes of a Type 3 font's glyph procedures decoded to look for colored glyphs.
const MAX_CHARPROC_BYTES: usize = 16 << 20;
// The glyph procedures decoded by `sets_glyph_colors` on this thread.
#[cfg(test)]
thread_local! {
pub(crate) static TEST_CHARPROC_DECODES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
/// Whether Type 3 font `f` has a glyph procedure that sets its own color (`d0`, ISO 32000-1
/// §9.6.5): its procedures are read until one starts with `d0`, each up to its `d0` or `d1`. Past
/// [`MAX_CHARPROC_BYTES`] of them the font is taken to have one.
fn sets_glyph_colors(f: &Dict<'_>) -> bool {
let Some(char_procs) = f.get::<Dict<'_>>(b"CharProcs") else { return false };
let mut seen_streams = HashSet::new();
let mut decoded_bytes = 0usize;
for key in char_procs.keys() {
if let Some(stream) = char_procs.get::<Stream<'_>>(key.as_ref()) {
if !seen_streams.insert(stream.cache_key()) {
continue;
}
if decoded_bytes >= MAX_CHARPROC_BYTES {
return true;
}
#[cfg(test)]
TEST_CHARPROC_DECODES.with(|n| n.set(n.get() + 1));
if let Ok(data) = stream.decoded_within(MAX_CHARPROC_BYTES.saturating_sub(decoded_bytes)) {
decoded_bytes = decoded_bytes.saturating_add(data.len());
let mut iter = TypedIter::new(data.as_ref());
while let Some(op) = iter.next() {
match op {
TypedInstruction::ColorGlyph(_) => return true,
TypedInstruction::ShapeGlyph(_) => break,
_ => {}
}
}
}
}
}
false
}
/// Whether font `f` carries its own glyphs: a font program in its descriptor (a composite font's in
/// its descendant's), or a Type 3 font's glyph procedures. A font that doesn't is drawn with a
/// stand-in, whose glyphs say nothing about which installed face the document means.
+50
View File
@@ -507,6 +507,56 @@ fn a_form_drawn_many_times_reads_its_font_resources_once() {
assert_eq!(walk.fonts.len(), 1);
}
/// A Type 3 font that every page lists in a font dictionary of its own, and a form lists in its
/// resources, has its glyph procedures read once for the document. Each page still maps the font's
/// glyphs, and the font whose glyphs set their own color (`d0`) is told from the one whose glyphs
/// don't (`d1`).
#[test]
fn a_type3_font_on_every_page_has_its_glyph_procedures_read_once() {
const PAGES: usize = 6;
let n = first_extra(PAGES);
let (shape, color, shape_a, shape_b, color_a, form) = (n, n + 1, n + 2, n + 3, n + 4, n + 5);
let font = |name: &str, procs: &str| {
format!(
"<< /Type /Font /Subtype /Type3 /Name /{name} /FontBBox [0 0 1000 1000] /FontMatrix [0.001 0 0 0.001 0 0] /CharProcs << {procs} >> /Encoding << /Type /Encoding /Differences [97 /a /b] >> /FirstChar 97 /LastChar 98 /Widths [1000 1000] /Resources << >> >>"
)
};
let objects = [
font("Vcshape", &format!("/a {shape_a} 0 R /b {shape_b} 0 R")),
font("Vccolor", &format!("/a {color_a} 0 R /b {color_a} 0 R")),
stream("", "1000 0 0 0 1000 1000 d1 0 0 1000 1000 re f"),
stream("", "1000 0 0 0 1000 1000 d1 0 0 500 500 re f"),
stream("", "1000 0 d0 1 0 0 rg 0 0 1000 1000 re f"),
stream(
&format!("/Type /XObject /Subtype /Form /BBox [0 0 100 100] /Resources << /Font << /C {color} 0 R /S {shape} 0 R >> >>"),
"BT /S 12 Tf (ab) Tj /C 12 Tf (ab) Tj ET",
),
];
let pages: Vec<PdfPage> = (0..PAGES)
.map(|_| PdfPage {
resources: format!("/Font << /S {shape} 0 R /C {color} 0 R >> /XObject << /X0 {form} 0 R >>"),
..PdfPage::new(100.0, 100.0, "BT /S 24 Tf 10 30 Td (ab) Tj /C 24 Tf (ab) Tj ET /X0 Do")
})
.collect();
let extra: Vec<&str> = objects.iter().map(String::as_str).collect();
let pdf = craft_pdf::syntax::Pdf::new(pdf_with(&pages, &extra, None)).unwrap();
let mut fonts = crate::import_scan::FontNames::new();
let mut ocgs = crate::import_scan::Ocgs::read(&pdf);
crate::import_scan::TEST_CHARPROC_DECODES.with(|d| d.set(0));
for page in pdf.pages().iter() {
let scan = crate::import_scan::scan_page(page, &mut ocgs, false, &mut fonts, pdf.decode_limits().max_decoded_stream()).unwrap();
let mapped: std::collections::HashSet<(u128, bool)> = scan.type3_cache_map.values().copied().collect();
let noted: std::collections::HashSet<(u128, bool)> = fonts.keys().map(|k| (*k, false)).collect();
assert_eq!(scan.type3_cache_map.len(), 2 * 257);
assert_eq!(mapped, noted);
let mut colors: Vec<(String, bool)> = fonts.values().map(|f| (f.name.clone(), f.is_colour)).collect();
colors.sort();
assert_eq!(colors, [("Vccolor".to_string(), true), ("Vcshape".to_string(), false)]);
}
// The `d1` font's two procedures and the `d0` font's one.
assert_eq!(crate::import_scan::TEST_CHARPROC_DECODES.with(std::cell::Cell::get), 3);
}
/// A value of the page's font dictionary that the reader can't parse (a name with a broken `#`
/// escape, a number with a stray `-`) is left out when the page's fonts are noted, and the fonts
/// after it are noted.
+1
View File
@@ -682,6 +682,7 @@ since 2026-10-01.
- **EPS writes a repeated freeform gradient's image once:** a freeform gradient whose image samples the same on more than one fill (copies of a shape moved with their points, or one shape in a symbol's instances) has its samples written once in the setup, as a repeated image's are, and a fill whose image differs (other colors, another size) gets its own. A freeform fill drawn once is written where it is drawn, as before, and a fill whose placement cannot be drawn, or that arrives when the page is already at the 256 MB limit, is no longer sampled first (eps `tests::a_freeform_gradients_image_drawn_twice_writes_its_samples_once`, `tests::freeform_gradients_sampled_differently_dont_share_an_image`, `tests::a_field_taken_back_at_the_page_limit_leaves_no_image_behind`). Scores and hours unchanged.
- **EPS releases the images of art left out at the page limit:** art taken back because it passes the 256 MB limit on the page no longer keeps the encoded samples of the placed images, colorized images and freeform fills it drew, and an image whose first placement does not fit under the limit is not kept after it is encoded. Later placements of that image are left out with the same warning without encoding it again while it still does not fit; once art taken back leaves room, it is encoded again and written where it is drawn. The page the EPS file draws is the same as before; a repeated image or field written after art taken back can take a lower resource number in the setup (eps `tests::a_rolled_back_node_frees_its_image_codes`, `tests::a_rolled_back_image_does_not_pin_an_earlier_field_code`, `tests::an_image_rejected_by_the_page_limit_keeps_no_unused_code`, `tests::an_image_rejected_by_the_page_limit_is_encoded_once`, `tests::an_image_rejected_in_art_taken_back_is_encoded_again_when_it_fits`). Scores and hours unchanged.
- **PDF writes a warped image once:** with downsampling on, the pieces of an image bent by a Warp effect are resampled for the widest and the highest size any piece places the image at, so every piece paints one image XObject (a colorized image: one colorized image and its soft mask), instead of one image for each size a piece is placed at. Another placement of the same image is still resampled for its own size. Without downsampling, a colorized image and a CMYK image with a grayscale image mask on its grid are decoded and encoded once for all the sizes they are placed at (pdf `tests::compression::a_warped_image_is_written_once_for_all_its_pieces`, `tests::colorize::a_colorized_warped_image_is_written_once_for_all_its_pieces`, `export::images::tests::colorized_and_grid_masked_images_are_made_once_for_every_size_without_downsampling`). Scores and hours unchanged.
- **PDF import reads a Type 3 font's glyph procedures once:** a Type 3 font that many pages or forms list, each in a font dictionary of its own, has its glyph procedures checked for glyphs that set their own color once for the document, when the font is first noted, instead of again on every page and form. The pages open as before (pdf `tests::import_fidelity::a_type3_font_on_every_page_has_its_glyph_procedures_read_once`). Scores and hours unchanged.
- **Distort Appearance with Transform effect options:** with Envelope Options › Distort Appearance on, a path whose Transform effect has Scale Strokes & Effects or Transform Patterns on, or copies it with a gradient, is bent as the same per-copy art it draws and expands as outside the envelope: each copy's own stroke weight, dashes, pattern placement and gradient (effects `bake::tests`). Scores and hours unchanged.
- **Fitted gradients turn with the Transform effect:** a gradient fitted to the object (not placed with the Gradient tool) turns, reflects and stretches with the object when its Transform effect rotates, reflects or scales it unevenly, as Object › Transform already does: the gradient is fixed to its fit on the geometry the effect takes, then follows the effect and the effects after it as a placed gradient does. Under moves and even scales it draws as a refit does, and a Transform effect that scales the object to nothing across leaves it fitted, with Scale Strokes & Effects or Transform Patterns on too, and on each copy. This holds on the canvas and in Object › Expand of the fill, for an object's own effects, a fill's or stroke's own, and a group's, where the gradients fitted to its paths, to its own fill, to the groups inside it and to its images' and symbol instances' own fills each fix to their own object's fit. Existing documents with such a Transform effect on fitted-gradient art draw differently: before, the gradient refit to the turned outline. The Gradient tool still shows a fitted gradient's vector at its unturned fit. How gradients follow the effect is VectorCraft's own rule; what the effect does to the object is described in [Distort and transform effects](https://helpx.adobe.com/illustrator/desktop/manage-objects/reshape-transform-objects/distort-and-transform-effects-in-illustrator.html) (effects `bake::tests::a_fitted_gradient_turns_reflects_and_stretches_with_a_transform_effect`, `bake::tests::a_transform_effect_that_scales_to_nothing_leaves_a_fitted_gradient_fitted`, `group::tests::a_groups_transform_turns_the_gradients_fitted_to_its_members_and_its_own_fill`; render `objectfx::a_fitted_gradient_turns_and_reflects_with_its_transform_on_the_canvas`; engine `objexpand::a_fitted_gradient_turned_by_a_transform_expands_as_the_canvas_draws_it`). Scores and hours unchanged.
- **Placed gradients follow Transform and Free Distort effects:** a gradient placed with the Gradient tool moves, turns and scales with the object when the object's Transform effect moves its outline, and each copy of the Transform effect takes it as many times as it takes the transform. Under Free Distort it moves and scales from the path's bounds before the effect to its bounds after it. This holds on the canvas, in export, in Expand Appearance, in Pathfinder and compound shapes, and in Object › Expand of the fill, for an object's own effects, a group's (the own fills of the group, of the groups inside it, of a symbol's art, and of the images and symbol instances it moves, included) and a fill's or stroke's own. A gradient on a path inside a group with a Transform effect now moves with the path (#2214 moved only the gradients of type), and so does a gradient on an image's or symbol instance's own fill. When a path keeps its shape because the effect would pass the geometry budget, the group's own gradients stay in place; a hidden path in a layer, which the layer's bounds leave out, does not hold them. A freeform gradient whose points were edited moves its points in the same way. How gradients follow each effect is VectorCraft's own rule; what each effect does to the object is described in [Distort and transform effects](https://helpx.adobe.com/illustrator/desktop/manage-objects/reshape-transform-objects/distort-and-transform-effects-in-illustrator.html) (effects `bake::tests::a_baked_transform_moves_a_placed_gradient_with_the_path`, `bake::tests::a_strokes_own_transform_and_free_distort_move_its_placed_gradient`, `bake::tests::a_fills_gradient_follows_the_effects_around_its_transform_with_options`, `bake::tests::baked_free_distort_moves_placed_gradients_from_the_bounds_to_the_distorted_bounds`, `group::tests::a_groups_transform_and_free_distort_move_the_gradients_placed_on_its_paths`, `group::tests::a_groups_transform_moves_the_gradient_of_its_own_fill`, `group::tests::a_nested_groups_own_gradient_follows_the_outer_groups_transform_and_free_distort`, `group::tests::a_groups_transform_moves_the_gradients_placed_on_an_images_own_fills`, `group::tests::shape_art_moves_gradients_with_a_transform_effect`, `group::tests::shape_art_moves_the_gradient_of_a_symbol_instances_art_with_its_transform`, `tests::art_past_the_budget_keeps_its_own_gradients_in_place`, `tests::a_hidden_path_past_the_budget_leaves_a_layers_gradients_free_to_move`, `tests::edited_freeform_points_follow_a_transform_effect_and_free_distort`; render `objectfx::a_paths_gradient_moves_with_its_transform_and_free_distort`; engine `objexpand::a_placed_gradient_moved_by_a_transform_or_free_distort_expands_as_the_canvas_draws_it`). Scores and hours unchanged.