PDF import: forget lost fills at flush points (#8549 follow-up)

Since #8549 the glyphs of lines finished past MAX_PARKED were kept
until the next stroke and, past 4096 of them, matched every stroke.
A `2 Tr` word after a dense page of filled-only type then came in as a
filled object and a stroked one.

- flush_text forgets them: nothing is drawn between an operator's fill
  pass and its stroke pass. end_text keeps them, since it also runs
  mid fill pass for a glyph kept as an outline.
- LostFills keeps the first 4096 glyphs and matches only those; there
  is no overflow that matches every stroke.
- replayed counts the lines its `left` check looks at toward
  MAX_REPLAY_WORK.
This commit is contained in:
HassanKhan
2026-11-19 11:06:23 +05:00
parent 8fcadbc3c1
commit 2113b214f1
7 changed files with 89 additions and 23 deletions
+29 -8
View File
@@ -479,15 +479,26 @@ impl<'p> Builder<'p> {
self.push_node(n);
}
/// Finish the open line of type ([`Self::end_text`]) and the outlines it was kept as, before
/// something else is drawn: a path, an image, a Type 3 glyph's art, or a change of blend mode
/// or soft mask. Nothing is drawn between a text-showing operator's fill pass and its stroke
/// pass, so no stroke drawn after this goes over a glyph finished before it either: the
/// glyphs of lines finished before their stroke pass could end are forgotten
/// ([`Self::park_text`]). It also finishes a line no stroke can go over as the next line
/// starts ([`Self::park_text`]): a fill pass whose glyphs a stroke can go over parks each of
/// its lines instead, so that is never in the middle of one that left such glyphs behind.
pub(super) fn flush_text(&mut self) {
if self.end_text() {
self.flush_glyphs();
}
self.lost = None;
}
/// Finish the open line of type, after the lines parked before it ([`Self::park_text`]).
/// `true`: the open line was kept as outlines instead ([`reshaped`]), which are left open as
/// the glyph run for an outline drawn next to join.
/// the glyph run for an outline drawn next to join. It runs in the middle of a fill pass when
/// one of its glyphs is kept as an outline: the glyphs of lines finished past the most kept
/// before it are not forgotten there, as they are by [`Self::flush_text`].
pub(super) fn end_text(&mut self) -> bool {
self.settle_strokes();
self.release_parked(usize::MAX);
@@ -665,21 +676,31 @@ impl<'p> Builder<'p> {
fn replayed(&self, held: &[HeldStroke]) -> Option<Vec<(usize, usize)>> {
let (head, rest) = held.split_first()?;
let mut work = 0usize;
// The glyphs filled from `at` on, up to as many as there are strokes held.
let left = |(slot, i): (usize, usize)| {
(slot..=self.parked.len())
.filter_map(|s| self.line_at(s).map(|line| line.filled_len().saturating_sub(if s == slot { i } else { 0 })))
.fold(0usize, |n, len| n.saturating_add(len).min(held.len()))
// The glyphs filled from `at` on, up to as many as there are strokes held, each line
// looked at counted as work.
let left = |(slot, i): (usize, usize), work: &mut usize| {
let mut n = 0usize;
for s in slot..=self.parked.len() {
if n >= held.len() {
break;
}
*work = work.saturating_add(1);
if let Some(line) = self.line_at(s) {
n = n.saturating_add(line.filled_len().saturating_sub(if s == slot { i } else { 0 }));
}
}
n.min(held.len())
};
let mut start = self.last_fill(None, head);
while let Some(first) = start {
start = self.last_fill(Some(first), head);
work = work.saturating_add(1);
// Too few glyphs filled after it for the strokes held: not where the pass starts.
let short = left(first, &mut work) < held.len();
if work > MAX_REPLAY_WORK {
return None;
}
// Too few glyphs filled after it for the strokes held: not where the pass starts.
if left(first) < held.len() {
if short {
continue;
}
let mut places = Vec::with_capacity(held.len());
+2 -1
View File
@@ -123,7 +123,8 @@ pub(super) struct Builder<'p> {
pub(super) held: Vec<HeldStroke>,
/// The glyphs of the lines a fill pass finished beyond the most kept for their stroke pass that
/// a stroke may still have gone over, until the first stroke after them
/// ([`Builder::hold_stroke`]) or a boundary of stroke passes ([`Builder::end_fills`]).
/// ([`Builder::hold_stroke`]), a boundary of stroke passes ([`Builder::end_fills`]) or
/// anything else drawn ([`Builder::flush_text`]).
pub(super) lost: Option<crate::import_text::LostFills>,
pub(super) text_as: TextAs,
pub(super) actual: super::actual_text::ActualText,
+13 -10
View File
@@ -309,27 +309,31 @@ fn may_stroke(fill: (u128, Placement, bool, Option<&str>), opaque: bool, font: u
/// The glyphs a stroke could still go over on lines of type finished before their stroke pass
/// could end: lines beyond the most a fill pass keeps for its stroke pass. The device isn't told
/// where an operator starts, so a stroke pass over such a line can't be told from one over the
/// glyphs kept: its first stroke, over a glyph finished so, may match one of them instead. Kept up
/// to [`MAX_LOST_FILLS`](Self::MAX_LOST_FILLS), beyond which any stroke may be over one.
/// glyphs kept: its first stroke, over a glyph finished so, may match one of them instead. The
/// first [`MAX_LOST_FILLS`](Self::MAX_LOST_FILLS) are kept, those finished first: a stroke pass
/// starts over its operator's first glyph, which is among them unless more glyphs than that were
/// finished so before the operator, since the last stroke or anything else drawn (#8549). A
/// stroke over a glyph not kept matches none, so type filled and stroked after a page of type
/// filled only stays filled and stroked; an operator whose first glyph isn't kept and that draws
/// it again where it drew it may have its first stroke matched to the copy, as well as it can be,
/// not exactly.
#[derive(Clone, Debug, Default)]
pub(crate) struct LostFills {
/// Each glyph's font, placement, whether it is a glyph of an ActualText span, its text and
/// whether its line is opaque.
fills: Vec<(u128, Placement, bool, Option<String>, bool)>,
/// More glyphs than [`MAX_LOST_FILLS`](Self::MAX_LOST_FILLS) were finished so.
overflowed: bool,
}
impl LostFills {
/// The most glyphs kept, for bounded work and memory on hostile input.
/// The most glyphs kept, for bounded work and memory on hostile input: those after them
/// are not kept.
pub const MAX_LOST_FILLS: usize = 4096;
/// Add the glyphs `line` filled that a stroke may still go over.
/// Add the glyphs `line` filled that a stroke may still go over, while there is room.
pub fn add(&mut self, line: &TextLine) {
let opaque = line.opacity >= 0.999;
for g in line.filled.get(line.stroked..).unwrap_or_default() {
if self.fills.len() >= Self::MAX_LOST_FILLS {
self.overflowed = true;
return;
}
self.fills.push((g.font, g.at, g.actual.is_some(), line.glyph_text(g).map(str::to_string), opaque));
@@ -337,10 +341,9 @@ impl LostFills {
}
/// Whether a stroke over glyph `text` drawn at `at` in `font` at `opacity` may be drawn over
/// one of the glyphs ([`TextLine::fills_at`]).
/// one of the glyphs kept ([`TextLine::fills_at`]).
pub fn may_stroke(&self, font: u128, at: Placement, opacity: f32, text: &str, actual: bool) -> bool {
self.overflowed
|| self.fills.iter().any(|(f, a, act, t, opaque)| may_stroke((*f, *a, *act, t.as_deref()), *opaque, font, at, opacity, text, actual))
self.fills.iter().any(|(f, a, act, t, opaque)| may_stroke((*f, *a, *act, t.as_deref()), *opaque, font, at, opacity, text, actual))
}
}
+38
View File
@@ -568,3 +568,41 @@ fn a_long_stroke_pass_does_not_take_the_same_glyphs_filled_by_an_operator_before
let want = [vec![(format!("A{before}"), true, false)], vec![(format!("A{now}"), true, true)]];
assert!(looks == want, "{:?}", looks.iter().map(|t| t.iter().map(|r| (r.0.len(), r.1, r.2)).collect::<Vec<_>>()).collect::<Vec<_>>());
}
/// A page drawing `first`, then `lines` lines of type filled only, each of `glyphs` `A`s, then
/// `Title` filled and stroked by one `Tj` (mode 2) at 40, 150 in 24-point type, after `between`.
fn dense_then_title(first: &str, lines: usize, glyphs: usize, between: &str) -> String {
let line = format!("({}) Tj 0 -1 Td", "A".repeat(glyphs));
let dense = vec![line; lines].join(" ");
let title = "/F1 24 Tf 1 0 0 1 40 150 Tm (Title) Tj";
format!("BT 0 Tr {first} /F1 1 Tf 1 0 0 1 10 190 Tm {dense} ET {between} BT 2 Tr {title} 0 Tr ET").replace("{title}", title)
}
/// Type filled and stroked after a page of type filled only, more lines than a fill pass keeps
/// for its stroke pass, comes in as one type object filled and stroked, with or without a path
/// drawn between them: neither the lines finished past the 64 kept nor more than 4,096 glyphs'
/// worth of them make the title's first stroke the start of a stroke pass over them (#8549).
#[test]
fn type_filled_and_stroked_after_a_dense_page_of_filled_type_is_filled_and_stroked() {
let title = vec![("Title".to_string(), true, true)];
for (lines, glyphs) in [(150, 50), (70, 1)] {
for between in ["", "0 0 m 10 10 l S"] {
let types = type_runs(&dense_then_title("", lines, glyphs, between));
assert_eq!(types.len(), lines + 1, "{lines} lines of {glyphs}, {between:?}");
assert_eq!(types.last(), Some(&title), "{lines} lines of {glyphs}, {between:?}");
}
}
}
/// Type filled and stroked where a glyph filled only stood, on a line finished past the 64 kept,
/// comes in as one type object filled and stroked when a path is drawn between them: nothing is
/// drawn between an operator's fill pass and its stroke pass, so the path ends the stroke pass
/// of every glyph filled before it (#8549).
#[test]
fn type_filled_and_stroked_over_a_finished_glyph_after_a_path_is_filled_and_stroked() {
let content = dense_then_title("{title}", 70, 1, "0 0 m 10 10 l S");
let types = type_runs(&content);
assert_eq!(types.len(), 72, "{content}");
assert_eq!(types.first(), Some(&vec![("Title".to_string(), true, false)]), "{content}");
assert_eq!(types.last(), Some(&vec![("Title".to_string(), true, true)]), "{content}");
}
File diff suppressed because one or more lines are too long
+2 -1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long