PDF import: translucent fill-and-stroke type stays two objects (#8015)

A fill and a stroke at the same opacity below 1 were joined into one
type object whose opacity then applies to both together, losing the
darker overlap the file composites. stroke_filled now takes a stroke
only when the fill and the stroke are opaque.

An upright glyph filled and stroked was kept as an outline for the
reshaped-outline fallback while upright glyphs filled only were not;
it is no longer kept either, so such a line can't come in as the
outlines of its stroked glyphs alone.

Docs: the #8015 entries say what is left (translucent type, and a TJ
that starts a new line part way through).
This commit is contained in:
HassanKhan
2026-11-17 11:59:20 +05:00
parent d3640da834
commit 971b13843b
8 changed files with 83 additions and 17 deletions
+5 -1
View File
@@ -1155,7 +1155,11 @@ impl<'p> Builder<'p> {
&& look.fill.is_none()
&& line.stroke_filled(key, at, opacity, &text, s.clone(), overprint)
{
self.keep_glyph(o, m, drawn, opacity, scale, overprint);
// Upright glyphs aren't kept as outlines (nor, below, are those filled only): a
// vertical line that came in as its outlines would lose the ones filled only.
if top.is_none() {
self.keep_glyph(o, m, drawn, opacity, scale, overprint);
}
return true;
}
if !self.text.as_mut().is_some_and(place) {
+11 -4
View File
@@ -611,9 +611,16 @@ impl TextLine {
/// glyph's text takes the stroke; strokes over glyphs next to each other in a run gather and
/// restyle their text as one run, so a word filled whole and stroked whole stays one run. The
/// glyphs around them keep their own stroke or none (#7741).
///
/// Only opaque type takes its stroke: the file composites a translucent fill and stroke
/// apiece, darker where they overlap, while one object's opacity applies to its fill and
/// stroke together. Translucent fill and stroke stay type objects of their own, as do a fill
/// and stroke at different opacities.
pub fn stroke_filled(&mut self, font: u128, at: Placement, opacity: f32, text: &str, stroke: TypeStroke, overprint: bool) -> bool {
// A stroke at another opacity than the fill's (`CA` and `ca` differ) is type of its own.
let rest = if opacity == self.opacity { self.filled.get(self.stroked..) } else { None };
// A stroke at another opacity than the fill's (`CA` and `ca` differ), or over a
// translucent fill, is type of its own.
let opaque = opacity >= 0.999 && self.opacity >= 0.999;
let rest = if opaque { self.filled.get(self.stroked..) } else { None };
let found = rest.and_then(|rest| {
rest.iter().position(|g| {
g.font == font
@@ -625,8 +632,8 @@ impl TextLine {
// Stroked text of its own (stroke rendering): no later stroke goes back over the
// glyphs filled before it, so no stroke looks through them again.
self.stroked = self.filled.len();
// Nor does a stroke at another opacity join the glyph just filled: the line has one.
return opacity == self.opacity && self.stroke_last(font, at, text, stroke, overprint);
// Nor does a translucent stroke join the glyph just filled.
return opaque && self.stroke_last(font, at, text, stroke, overprint);
};
let continues = self
.pending
+6 -1
View File
@@ -81,8 +81,13 @@ fn form_resource_scopes_keep_scanned_overprint_in_step_with_interpretation() {
}
pub(crate) fn text_pdf(content: &str) -> Vec<u8> {
text_pdf_with("/ca 0.25 /CA 0.75", content)
}
/// [`text_pdf`] with graphics state `/G` set by `state` (its entries, such as `/ca 0.5 /CA 0.5`).
pub(crate) fn text_pdf_with(state: &str, content: &str) -> Vec<u8> {
page(
"/Font << /F1 5 0 R >> /ExtGState << /G << /ca 0.25 /CA 0.75 >> >>",
&format!("/Font << /F1 5 0 R >> /ExtGState << /G << {state} >> >>"),
content.as_bytes(),
&[
b"<< /Type /Font /Subtype /TrueType /BaseFont /Inter-Regular /Encoding /WinAnsiEncoding /FirstChar 65 /LastChar 67 /Widths [600 600 600] /FontDescriptor 6 0 R >>".to_vec(),
+25 -1
View File
@@ -38,6 +38,11 @@ fn page(drawn: &[char], sign_width: Option<f64>) -> Vec<u8> {
/// graphics state, to set between glyphs or before them, with `objects` it refers to (the first
/// is object `first_extra(1) + 4`).
fn page_with(ops: impl Fn(&dyn Fn(char) -> String) -> String, extra: &str, objects: &[String], sign_width: Option<f64>) -> Vec<u8> {
page_in("Identity-H", ops, extra, objects, sign_width)
}
/// [`page_with`] with the Type 0 font's encoding `encoding` (`Identity-V`: vertical writing).
fn page_in(encoding: &str, ops: impl Fn(&dyn Fn(char) -> String) -> String, extra: &str, objects: &[String], sign_width: Option<f64>) -> Vec<u8> {
FontDb::global().add_font(matra_font().expect("the test font builds"));
let program = matra_font().unwrap();
let hex: String = program.iter().map(|b| format!("{b:02X}")).collect::<String>() + ">";
@@ -66,7 +71,7 @@ fn page_with(ops: impl Fn(&dyn Fn(char) -> String) -> String, extra: &str, objec
];
let refs: Vec<&str> = objs.iter().chain(objects).map(String::as_str).collect();
let resources = format!(
"/Font << /F1 << /Type /Font /Subtype /Type0 /BaseFont /MatratestSans-Regular /Encoding /Identity-H /DescendantFonts [{} 0 R] /ToUnicode {} 0 R >> >> {extra}",
"/Font << /F1 << /Type /Font /Subtype /Type0 /BaseFont /MatratestSans-Regular /Encoding /{encoding} /DescendantFonts [{} 0 R] /ToUnicode {} 0 R >> >> {extra}",
n + 2,
n + 3
);
@@ -218,3 +223,22 @@ fn outlined_glyphs_under_a_varying_soft_mask_are_masked_one_by_one() {
assert_eq!(outlines(&report.document), 3, "a path each");
assert_eq!(masked_outlines(&report.document), 3, "each path has its own mask");
}
/// A vertical line (upright glyphs) with one glyph filled and stroked: its glyphs aren't kept as
/// outlines, so it can't come in as the outlines of that glyph alone, losing the glyph filled
/// only. Every glyph comes in filled (#8015).
#[test]
fn a_vertical_line_with_one_glyph_filled_and_stroked_keeps_every_glyph_filled() {
let bytes = page_in("Identity-V", |id| format!("1 0 0 rg 0 0 1 RG 2 w {} Tj 2 Tr {} Tj 0 Tr", id(KA), id(SIGN_I)), "", &[], None);
let report = open(&bytes);
let mut runs = vec![];
report.document.walk(|n| {
if let NodeKind::Text(t) = &n.kind {
runs.extend(t.runs.iter().map(|r| (r.text.clone(), !r.style.fill.is_none(), !r.style.stroke.is_none())));
}
});
let filled: String = runs.iter().filter(|r| r.1).map(|r| r.0.as_str()).collect();
assert_eq!(filled, format!("{KA}{SIGN_I}"), "every glyph filled: {runs:?}");
let stroked: String = runs.iter().filter(|r| r.2).map(|r| r.0.as_str()).collect();
assert_eq!(stroked, SIGN_I.to_string(), "{runs:?}");
}
+22
View File
@@ -267,6 +267,28 @@ fn a_glyph_stroked_at_another_opacity_than_its_fill_stays_its_own_type() {
}
}
/// Type filled and stroked at the same opacity below 1 stays a filled object and a stroked one:
/// the file composites the fill and the stroke apiece (darker where they overlap), while an
/// object's opacity applies to its fill and stroke together. Opaque, it is one object (#8015).
#[test]
fn translucent_fill_and_stroke_text_stays_two_objects() {
let half = |fill, stroke| (128.0 / 255.0, fill, stroke);
for show in ["(A) Tj", "(AB) Tj"] {
let content = format!("/G gs 1 0 0 rg 0 0 1 RG 2 w BT /F1 20 Tf 2 Tr 1 0 0 1 10 20 Tm {show} 0 Tr ET");
for (state, want) in [("/ca 0.5 /CA 0.5", vec![half(true, false), half(false, true)]), ("/ca 1 /CA 1", vec![(1.0, true, true)])] {
let d = import(&crate::tests::reader_compat::text_pdf_with(state, &content)).unwrap();
let mut texts = vec![];
d.walk(|n| {
if let NodeKind::Text(t) = &n.kind {
let st = t.first_style();
texts.push((n.opacity, !st.fill.is_none(), !st.stroke.is_none()));
}
});
assert_eq!(texts, want, "{state}: {content}");
}
}
}
/// A stroke goes only over a glyph filled with the same text: "AB" filled, its "A" stroked, then
/// an "X" stroked where the "B" stands is type of its own, and the "B" stays unstroked (#8015).
#[test]
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