PDF import: a glyph filled only after one filled and stroked stays unstroked (#8300)

Look::takes let a glyph filled only join a run whose glyphs were also
stroked, so after mode 2 text a mode 0 glyph took the run's stroke:
`2 Tr (B) Tj 0 Tr (C) Tj` came in as "BC" stroked throughout. A glyph
now joins only a run drawn alike (fill, stroke, overprints), so a glyph
filled only starts a run of its own and takes a stroke only when the
matched-stroke path (#8039) strokes it.

flush_stroke no longer strokes a whole run in place: a whole run goes
through the split and join code, so fill and stroke rendering glyph by
glyph still builds one stroked run.
This commit is contained in:
HassanKhan
2026-11-17 15:59:49 +05:00
parent 6d5767bf10
commit 007180e2f3
5 changed files with 75 additions and 22 deletions
+16 -15
View File
@@ -111,16 +111,21 @@ pub(crate) struct Look {
}
impl Look {
/// Can a glyph drawn with `other` join a run drawn with this? A fill joins a run whose
/// glyphs were also stroked over (fill and stroke rendering draws each glyph twice); invisible
/// text (neither fill nor stroke) joins only invisible text.
/// Can a glyph drawn with `other` join a run drawn with this? Only a run drawn alike: fill,
/// stroke and overprints. A glyph filled only never joins a run whose glyphs were also stroked
/// over: it takes a stroke only when one is drawn over it later
/// ([`TextLine::stroke_filled`]), which then joins it to the run before if that is stroked
/// alike (fill and stroke rendering draws each glyph twice), and a glyph filled only after
/// one filled and stroked stays unstroked (#8300). Invisible text (neither fill nor stroke)
/// joins only invisible text.
fn takes(&self, other: &Look) -> bool {
self.font == other.font
&& (self.size - other.size).abs() <= self.size * 0.01
&& (self.h_scale - other.h_scale).abs() < 0.5
&& self.fill == other.fill
&& self.overprint_fill == other.overprint_fill
&& ((other.stroke.is_none() && other.fill.is_some()) || (self.stroke == other.stroke && self.overprint_stroke == other.overprint_stroke))
&& self.stroke == other.stroke
&& self.overprint_stroke == other.overprint_stroke
}
fn style(&self) -> CharStyle {
@@ -658,8 +663,9 @@ impl TextLine {
/// Restyle the glyphs gathered for a stroke ([`stroke_filled`](Self::stroke_filled)): their
/// text becomes a run of its own with the stroke, unless the run is stroked so already, and the
/// text before and after it in the run keeps its stroke. A run they make up whole is stroked
/// whole; text that comes right after a run drawn alike joins it.
/// text before and after it in the run keeps its stroke. Text that comes right after a run
/// drawn alike joins it, a run made up of it whole too: fill and stroke rendering glyph by
/// glyph fills each glyph as a run of its own ([`Look::takes`]) before its stroke (#8300).
fn flush_stroke(&mut self) {
let Some(PendingStroke { run, start, end, stroke, overprint }) = self.pending.take() else { return };
let last = run.saturating_add(1) == self.runs.len();
@@ -671,10 +677,6 @@ impl TextLine {
if !(start < end && end <= text.len() && text.is_char_boundary(start) && text.is_char_boundary(end)) {
return;
}
if start == 0 && end == text.len() {
(look.stroke, look.overprint_stroke) = (Some(stroke), overprint);
return;
}
let tail = text.split_off(end);
let glyphs = text.split_off(start);
let head = std::mem::take(text);
@@ -738,11 +740,10 @@ impl TextLine {
/// A stroke over glyph `glyph` just drawn at `at` in `font`, which `overprint`s or not: that
/// glyph is stroked. `false`: it isn't that glyph (another place, font or text).
///
/// A filled glyph joins a run of the same fill whatever its stroke ([`Look::takes`]), so the
/// run can hold glyphs before it that are stroked otherwise or not at all: then the glyph's
/// text becomes a run of its own with this stroke, and the glyphs before it keep theirs
/// (#7741). The line's text is the same either way, so the characters' slots, gaps and
/// authored flags, kept along the whole line, still line up.
/// The glyph's run can hold glyphs before it, filled alike and not stroked
/// ([`Look::takes`]): then the glyph's text becomes a run of its own with this stroke, and
/// the glyphs before it stay unstroked (#7741). The line's text is the same either way, so
/// the characters' slots, gaps and authored flags, kept along the whole line, still line up.
fn stroke_last(&mut self, font: u128, at: Placement, glyph: &str, stroke: TypeStroke, overprint: bool) -> bool {
self.flush_stroke();
let start = self.last_start;
+49
View File
@@ -297,3 +297,52 @@ fn a_stroke_of_another_glyph_where_one_was_filled_stays_its_own_type() {
let want = [("A".to_string(), 60.0, true, true), ("B".to_string(), 60.0, true, false), ("X".to_string(), 60.0, false, true)];
assert_eq!(word_runs(content), want, "{content}");
}
/// Helvetica PDF page drawing `content` (fill cream, stroke black, 3 points wide), imported: each
/// type object's runs in drawing order, as their text and whether they are filled and stroked.
fn type_runs(content: &str) -> Vec<Vec<(String, bool, bool)>> {
use vectorcraft_testkit::pdf::{PdfPage, pdf};
let resources = "/Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Helvetica >> >>";
let content = format!("0 0 0 RG 1 1 0.8 rg 3 w {content}");
let bytes = pdf(&[PdfPage { resources: resources.into(), ..PdfPage::new(300.0, 200.0, &content) }], None);
let d = import(&bytes).unwrap();
let mut types = vec![];
d.walk(|n| {
if let NodeKind::Text(t) = &n.kind {
types.push(t.runs.iter().map(|r| (r.text.clone(), !r.style.fill.is_none(), !r.style.stroke.is_none())).collect());
}
});
types
}
/// A glyph filled only (mode 0) right after one filled and stroked (mode 2) is filled only: it
/// joins the same type object, but not the stroked run, whether the glyphs before were filled
/// only, filled and stroked, or both, and a glyph filled and stroked after it is stroked again
/// (#8300).
#[test]
fn a_glyph_filled_only_after_one_filled_and_stroked_is_not_stroked() {
let run = |text: &str, stroked: bool| (text.to_string(), true, stroked);
let cases = [
("2 Tr (B) Tj 0 Tr (C) Tj", vec![run("B", true), run("C", false)]),
("0 Tr (A) Tj 2 Tr (B) Tj 0 Tr (C) Tj", vec![run("A", false), run("B", true), run("C", false)]),
("2 Tr (B) Tj 0 Tr (C) Tj 2 Tr (D) Tj", vec![run("B", true), run("C", false), run("D", true)]),
("2 Tr (AB) Tj 0 Tr (CD) Tj", vec![run("AB", true), run("CD", false)]),
("0 Tr (A) Tj 2 Tr (BC) Tj", vec![run("A", false), run("BC", true)]),
];
for (show, want) in cases {
let content = format!("BT /F1 48 Tf 20 80 Td {show} 0 Tr ET");
assert_eq!(type_runs(&content), [want], "{content}");
}
}
/// Fill and stroke rendering glyph by glyph, and a word filled whole then stroked whole at the
/// same place, still come in as one run filled and stroked (#8300).
#[test]
fn glyphs_filled_and_stroked_one_by_one_stay_one_run() {
for content in [
"BT /F1 48 Tf 20 80 Td 2 Tr (A) Tj (B) Tj (C) Tj 0 Tr ET",
"BT /F1 48 Tf 1 0 0 1 20 80 Tm 0 Tr (ABC) Tj 1 Tr 1 0 0 1 20 80 Tm (ABC) Tj 0 Tr ET",
] {
assert_eq!(type_runs(content), [vec![("ABC".to_string(), true, true)]], "{content}");
}
}
File diff suppressed because one or more lines are too long
+3 -2
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long