# Conflicts:
#	docs/file-format-parity.md
#	docs/roadmap.md
#	docs/target-app-parity.md
This commit is contained in:
bflatastic
2026-11-19 01:07:40 +07:00
9 changed files with 818 additions and 73 deletions
+32 -10
View File
@@ -1,6 +1,6 @@
//! Bounded ownership of authored replacement text (ISO 32000-1, 14.9.4).
use super::state::Builder;
use super::state::{Builder, HeldStroke};
use super::{Affine, Look, Placement, Shape, TextLine, Upright};
use crate::import_text::TextContent;
@@ -21,13 +21,16 @@ impl ActualText {
self.depth = self.depth.saturating_add(1);
}
pub(super) fn end(&mut self) {
if self.owner == Some(self.depth) {
/// `true`: the owner's span ended.
pub(super) fn end(&mut self) -> bool {
let ended = self.owner == Some(self.depth);
if ended {
self.owner = None;
self.pending = None;
self.empty = false;
}
self.depth = self.depth.saturating_sub(1);
ended
}
pub(super) fn active(&self) -> bool {
@@ -62,13 +65,32 @@ impl Builder<'_> {
/// against the glyph's original Unicode, not against this replacement's characters.
pub(super) fn actual_glyph(&mut self, o: &craft_pdf::interpret::font::OutlineGlyph, m: Affine, at: Placement, look: Look, opacity: f32) -> bool {
let key = o.font_cache_key();
self.flush_glyphs();
// A fill and stroke callback for the same glyph consumes its text only once.
if let (Some(stroke), Some(line)) = (&look.stroke, &mut self.text)
&& look.fill.is_none()
&& line.stroke_actual(key, at, stroke.clone(), look.overprint_stroke)
// A fill and stroke callback for the same glyph consumes its text only once: the stroke
// goes over the glyph of the span filled, matched as the stroke pass ends as any other
// (`Builder::hold_stroke`), on a line the operator's kerning finished too.
let stroking = look.fill.is_none() && look.stroke.is_some();
if let Some(stroke) = &look.stroke
&& stroking
{
return true;
let h = HeldStroke {
font: key,
at,
opacity,
text: String::new(),
stroke: stroke.clone(),
overprint: look.overprint_stroke,
glyph: None,
actual: true,
first: (0, 0),
};
if self.hold_stroke(h) {
return true;
}
}
self.flush_glyphs();
// Stroked type of its own ends the stroke pass of the glyphs filled before it.
if stroking {
self.end_fills();
}
let width = o.advance_width().filter(|w| w.is_finite());
let advance = width.map_or(at.size * 0.5, |w| w as f64 / 1000.0 * at.size * at.h_scale / 100.0);
@@ -87,7 +109,7 @@ impl Builder<'_> {
None => line.push_content(&look, at, opacity, advance, content),
};
if !self.text.as_mut().is_some_and(place) {
self.flush_text();
self.park_text(!stroking);
let mut line = TextLine::new(at, opacity);
if !place(&mut line) {
self.actual.pending = pending;
+341 -12
View File
@@ -4,7 +4,9 @@ use super::entry::{
UNNAMED_CHARACTERS_NOTE, max_objects,
};
use super::reshaped;
use super::state::{Builder, Frame, FrameKind, GlyphRun, Saved, bounds, grouped, mean_scale, paints, round3, same_stroke, stroke_layer, type_stroke};
use super::state::{
Builder, Frame, FrameKind, GlyphRun, HeldStroke, Saved, bounds, grouped, mean_scale, paints, round3, same_stroke, stroke_layer, type_stroke,
};
use super::{
Affine, Appearance, AppearanceItem, Arc, BezPath, BfString, BlendMode, CacheKey, Color, Colors, Families, FillLayer, FillRule, FunctionBudget,
FunctionPaint, HashMap, HashSet, ImageBlob, ImageObject, Knockout, Look, MaskSpec, MaskType, Native, Node, NodeId, NodeKind, OpacityMask, Paint,
@@ -12,6 +14,30 @@ use super::{
Upright, Walked, clipped, contains, fold_stop_opacity, function_paint, gradient_shape, is_rectangle, luminance, mask_spec, shading_gradient,
};
/// The most lines of type kept for a text-showing operator's stroke pass ([`Builder::park_text`]):
/// beyond them the oldest is finished, and strokes over its glyphs come in as type of their own.
/// They are the lines its fill pass finished, besides the open one: a `TJ` over up to 65 lines is
/// filled and stroked, one over more is a filled object and a stroked one for each part. The
/// pass's first stroke goes over a glyph finished so, which can match a glyph kept (a `TJ` that
/// draws it again where it began): a first stroke that may go over one of them is type of its own
/// too, and ends the pass ([`Builder::hold_stroke`]).
const MAX_PARKED: usize = 64;
/// The most strokes of a stroke pass held to be matched to the glyphs they go over
/// ([`Builder::hold_stroke`]): beyond them, those held are matched to the last glyphs filled one
/// after another that they go over, with as many glyphs filled from there on as strokes held
/// ([`Builder::replayed`]), and the pass goes on from there. The reader tells no operator's
/// start, so the last such run is taken: the operator's fill pass is the one right before its
/// stroke pass. An operator that fills the same run of more than this many glyphs twice at the
/// same places may have its first strokes matched to its second copy; such a pass is matched as
/// well as it can be, not exactly.
const MAX_HELD: usize = 4096;
/// The most glyphs [`Builder::replayed`] compares to the strokes held, for bounded work on
/// hostile input: beyond them, each stroke goes over the first glyph it may go over after the one
/// the stroke before it goes over.
const MAX_REPLAY_WORK: usize = 1 << 20;
fn is_colour_face(face: &vectorcraft_text::FontFace) -> bool {
use skrifa::Tag;
use skrifa::raw::TableProvider;
@@ -37,6 +63,9 @@ impl<'p> Builder<'p> {
type3_run: None,
text: None,
text_glyphs: vec![],
parked: vec![],
held: vec![],
lost: None,
text_as,
actual: Default::default(),
images: HashMap::new(),
@@ -119,6 +148,9 @@ impl<'p> Builder<'p> {
self.type3_run = None;
self.text = None;
self.text_glyphs.clear();
self.parked.clear();
self.held.clear();
self.lost = None;
self.mask = None;
self.masked.clear();
self.marked.clear();
@@ -420,7 +452,10 @@ impl<'p> Builder<'p> {
}
}
/// Finish the open run of glyphs kept as outlines, once the strokes held are matched
/// ([`Self::settle_strokes`]): whatever is drawn next ends their stroke pass.
pub(super) fn flush_glyphs(&mut self) {
self.settle_strokes();
let Some(run) = self.glyphs.take() else { return };
if run.path.elements().is_empty() {
return;
@@ -450,11 +485,274 @@ impl<'p> Builder<'p> {
}
}
/// Finish the open line of type. `true`: it was kept as outlines instead ([`reshaped`]), which
/// are left open as the glyph run for an outline drawn next to join.
/// 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.
pub(super) fn end_text(&mut self) -> bool {
self.settle_strokes();
self.release_parked(usize::MAX);
let glyphs = std::mem::take(&mut self.text_glyphs);
let Some(mut line) = self.text.take() else { return false };
let Some(line) = self.text.take() else { return false };
self.end_line(line, glyphs)
}
/// Finish the first `n` parked lines ([`Self::park_text`]), in the order they were drawn.
pub(super) fn release_parked(&mut self, n: usize) {
let n = n.min(self.parked.len());
if n == 0 {
return;
}
let lines: Vec<_> = self.parked.drain(..n).collect();
for (line, glyphs) in lines {
if self.end_line(line, glyphs) {
self.flush_glyphs();
}
}
}
/// The open line ends where glyph drawn next starts another. The reader fills every glyph of
/// a text-showing operator before it strokes them (fill and stroke rendering): a line that a
/// stroke may still be drawn over is parked instead of finished, so that the operator's
/// stroke pass still finds the glyphs it filled when its kerning started a new line part way
/// through (#8543). Any other line, or with `park` false, is finished with the lines parked
/// before it.
///
/// Parked lines are finished, oldest first, when the stroke pass goes past them (a stroke is
/// drawn over a later line or the open one: [`Self::settle_strokes`]; or over none of them:
/// [`Self::end_fills`]), when anything else ends the open line ([`Self::end_text`]), and
/// beyond [`MAX_PARKED`] lines: the glyphs of those are kept for the stroke after them
/// ([`Self::hold_stroke`]).
pub(super) fn park_text(&mut self, park: bool) {
match self.text.take() {
Some(line) if park && line.awaits_stroke() => {
let glyphs = std::mem::take(&mut self.text_glyphs);
self.parked.push((line, glyphs));
let over = self.parked.len().saturating_sub(MAX_PARKED);
if over > 0 {
let lost = self.lost.get_or_insert_default();
for (line, _) in self.parked.iter().take(over) {
lost.add(line);
}
}
self.release_parked(over);
}
line => {
self.text = line;
self.flush_text();
}
}
}
/// A boundary of stroke passes: no stroke drawn from now on goes over a glyph filled before
/// it. The strokes held are matched ([`Self::settle_strokes`]), the parked lines finished, and
/// the open line's glyphs filled so far closed to strokes ([`TextLine::end_fills`]). A stroke
/// that goes over no glyph filled (stroked type of its own) is one: a stroke pass replays the
/// glyphs its operator filled last, so a stroke after it isn't part of a pass over glyphs
/// filled before it, and stroking those would draw it under the type of its own. The start
/// and end of an ActualText span are too: a span's glyphs are stroked within it (#8543).
pub(super) fn end_fills(&mut self) {
self.lost = None;
self.settle_strokes();
self.release_parked(usize::MAX);
if let Some(line) = &mut self.text {
line.end_fills();
}
}
/// The parked line at `slot` ([`Self::park_text`]), or the open line at the slot after them.
fn line_at(&self, slot: usize) -> Option<&TextLine> {
match self.parked.get(slot) {
Some((line, _)) => Some(line),
None if slot == self.parked.len() => self.text.as_ref(),
None => None,
}
}
/// The glyph filled right after the one at `at` (a line's slot and its index among the line's
/// filled glyphs, as [`Self::next_fill`]): the next on its line, else the first of the line
/// after it.
fn after_fill(&self, (slot, i): (usize, usize)) -> (usize, usize) {
match self.line_at(slot) {
Some(line) if i.saturating_add(1) < line.filled_len() => (slot, i.saturating_add(1)),
_ => (slot.saturating_add(1), 0),
}
}
/// The first glyph filled after `after` that stroke `h` may go over
/// ([`TextLine::next_filled`]), as its line's slot ([`Self::line_at`]) and its index among
/// the line's filled glyphs: the lines in the order they were drawn, each line's glyphs in
/// the order they were filled. From the first glyph with no `after`.
fn next_fill(&self, after: Option<(usize, usize)>, h: &HeldStroke) -> Option<(usize, usize)> {
let first = after.map_or(0, |(slot, _)| slot);
(first..=self.parked.len()).find_map(|slot| {
let from = match after {
Some((s, i)) if s == slot => i.saturating_add(1),
_ => 0,
};
let i = self.line_at(slot)?.next_filled(from, h.font, h.at, h.opacity, &h.text, h.actual)?;
Some((slot, i))
})
}
/// The last glyph filled before `before` that stroke `h` may go over ([`Self::next_fill`]).
/// From the last glyph with no `before`.
fn last_fill(&self, before: Option<(usize, usize)>, h: &HeldStroke) -> Option<(usize, usize)> {
let last = before.map_or(self.parked.len(), |(slot, _)| slot);
(0..=last).rev().find_map(|slot| {
let until = match before {
Some((s, i)) if s == slot => i,
_ => usize::MAX,
};
let i = self.line_at(slot)?.last_filled(until, h.font, h.at, h.opacity, &h.text, h.actual)?;
Some((slot, i))
})
}
/// Hold stroke `h`, over a glyph filled on the open line or a parked one, until its stroke
/// pass ends ([`Self::settle_strokes`]). `false`: no glyph filled after those the strokes
/// held go over is one it may go over (the strokes held are matched first, and if it may go
/// over none then, it isn't held either: it goes over the line as it stands).
///
/// The reader fills all the glyphs of a text-showing operator, then strokes the same glyphs
/// in the same order (fill and stroke rendering, #8015). Which glyph a stroke goes over is
/// only told once the pass has ended: a `TJ` can draw the same glyph at the same place twice
/// (its kerning goes back), and an operator can fill and stroke a glyph where one filled
/// before it stands, which the device can't tell from a glyph the operator filled (#8543).
///
/// The first stroke after lines were finished beyond [`MAX_PARKED`] ([`Self::park_text`])
/// that may go over a glyph of theirs may be the start of their stroke pass, which no longer
/// finds its first glyphs: it ends the pass ([`Self::end_fills`]) and is type of its own, as
/// are the pass's strokes after it. Any other first stroke there is matched as ever: no
/// stroke pass is due over those lines once another stroke is drawn.
pub(super) fn hold_stroke(&mut self, mut h: HeldStroke) -> bool {
if self.lost.take().is_some_and(|lost| lost.may_stroke(h.font, h.at, h.opacity, &h.text, h.actual)) {
self.end_fills();
return false;
}
if self.held.len() >= MAX_HELD {
self.settle(false);
}
let mut first = self.next_fill(self.held.last().map(|l| l.first), &h);
if first.is_none() && !self.held.is_empty() {
self.settle_strokes();
first = self.next_fill(None, &h);
}
let Some(first) = first else { return false };
h.first = first;
self.held.push(h);
true
}
/// Match the strokes held ([`Self::hold_stroke`]) to the glyphs they go over, now that their
/// stroke pass has ended, and stroke them. The pass strokes the glyphs filled last, in the
/// order they were filled: each stroke goes over the last glyph it may go over that is filled
/// before the one the stroke after it goes over. So a `TJ` that draws a glyph twice at the
/// same place strokes each where it filled it, and a glyph filled and stroked where one
/// filled before it stands strokes the one filled last (#8015, #8543).
pub(super) fn settle_strokes(&mut self) {
self.settle(true);
}
/// The glyphs the strokes `held` go over when they are the start of a stroke pass that goes
/// on after them ([`MAX_HELD`]): the last glyphs filled one right after another that they
/// may go over, in order, with at least as many glyphs filled from the first of them on as
/// there are strokes held. The pass replays its operator's fill pass, the glyphs filled
/// last, one right after another, right before the pass, so the strokes don't go over a
/// glyph filled before those, nor skip one. The device isn't told where an operator starts:
/// an operator before it that filled the same glyphs at the same places stays unstroked, as
/// the first glyphs of the pass are the last such run the pass still has room for. `None`
/// when there are none, or beyond [`MAX_REPLAY_WORK`].
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()))
};
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);
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() {
continue;
}
let mut places = Vec::with_capacity(held.len());
places.push(first);
let mut at = first;
for h in rest {
work = work.saturating_add(1);
if work > MAX_REPLAY_WORK {
return None;
}
at = self.after_fill(at);
if !self.line_at(at.0).is_some_and(|line| line.fills_at(at.1, h.font, h.at, h.opacity, &h.text, h.actual)) {
break;
}
places.push(at);
}
if places.len() == held.len() {
return Some(places);
}
}
None
}
/// [`Self::settle_strokes`], each stroke matched to the last glyph it may go over (`last`),
/// or, when too many are held ([`Self::hold_stroke`]), as the start of a pass that goes on
/// ([`Self::replayed`]).
fn settle(&mut self, last: bool) {
if self.held.is_empty() {
return;
}
let held = std::mem::take(&mut self.held);
let mut places = vec![];
if last {
let mut before = None;
for h in held.iter().rev() {
let Some(p) = self.last_fill(before, h) else { break };
places.push(p);
before = Some(p);
}
places.reverse();
} else if let Some(p) = self.replayed(&held) {
places = p;
}
// Each held stroke goes over the first glyph it may go over after the one before it
// does ([`Self::hold_stroke`]), so the last ones are found too: this is only a guard.
if places.len() != held.len() {
places = held.iter().map(|h| h.first).collect();
}
// The slots of the parked lines finished so far, which the later places count in.
let mut finished = 0usize;
for (h, (slot, index)) in held.into_iter().zip(places) {
let slot = slot.saturating_sub(finished);
let parked = self.parked.len();
// The lines before the one stroked are past the stroke pass: they are finished.
self.release_parked(slot);
finished = finished.saturating_add(slot.min(parked));
let target = match self.parked.first_mut() {
Some((line, glyphs)) if slot < parked => Some((line, glyphs)),
_ => self.text.as_mut().map(|line| (line, &mut self.text_glyphs)),
};
if let Some((line, glyphs)) = target
&& line.stroke_filled_at(index, h.stroke, h.overprint)
{
// Upright glyphs aren't kept as outlines (nor are those filled only): a vertical
// line that came in as its outlines would lose the ones filled only.
glyphs.extend(h.glyph);
}
}
}
/// Finish `line` of type, whose `glyphs` are its outlines ([`Self::keep_glyph`]). `true`:
/// it was kept as them ([`Self::end_text`]).
fn end_line(&mut self, mut line: TextLine, glyphs: Vec<GlyphRun>) -> bool {
let held = std::mem::take(&mut line.held);
// Type the file names itself (ActualText) is in reading order as it stands.
let checked = !glyphs.is_empty() && line.mapped() && !line.has_type3;
@@ -880,13 +1178,9 @@ impl<'p> Builder<'p> {
scale: f64,
overprint: bool,
) {
let Some((paint, stroke)) = drawn else { return };
let mut path = o.outline();
if path.elements().is_empty() {
return;
if let Some(g) = glyph_run(o, m, drawn, opacity, scale, overprint) {
self.text_glyphs.push(g);
}
path.apply_affine(m);
self.text_glyphs.push(GlyphRun { path, paint, opacity, stroke, scale, overprint });
}
fn type3_font_name(&mut self, key: u128) -> FontInfo {
@@ -1150,10 +1444,22 @@ impl<'p> Builder<'p> {
Some(top) => line.push_upright(&look, at, opacity, Upright { top }, &text),
None => line.push(&look, at, opacity, advance, &text),
};
let stroking = look.fill.is_none() && look.stroke.is_some();
// A stroke over a glyph filled before it, on the open line or one the same operator's
// kerning finished: matched once its stroke pass ends.
if let Some(s) = &look.stroke
&& stroking
{
let glyph = if top.is_none() { glyph_run(o, m, drawn.clone(), opacity, scale, overprint) } else { None };
let h = HeldStroke { font: key, at, opacity, text: text.clone(), stroke: s.clone(), overprint, glyph, actual: false, first: (0, 0) };
if self.hold_stroke(h) {
return true;
}
}
self.flush_glyphs();
// A stroke over the glyph just filled (fill and stroke rendering).
if let (Some(s), Some(line)) = (&look.stroke, &mut self.text)
&& look.fill.is_none()
&& stroking
&& line.stroke_filled(key, at, opacity, &text, s.clone(), overprint)
{
// Upright glyphs aren't kept as outlines (nor, below, are those filled only): a
@@ -1163,8 +1469,12 @@ impl<'p> Builder<'p> {
}
return true;
}
// Stroked type of its own ends the stroke pass of the glyphs filled before it.
if stroking {
self.end_fills();
}
if !self.text.as_mut().is_some_and(place) {
self.flush_text();
self.park_text(!stroking);
let mut line = TextLine::new(at, opacity);
place(&mut line);
self.text = Some(line);
@@ -1184,6 +1494,25 @@ impl<'p> Builder<'p> {
}
}
/// Glyph `o` placed by `m` and drawn with `drawn` as the outline it is kept as
/// ([`Builder::keep_glyph`]). Nothing for a glyph that shows nothing.
fn glyph_run(
o: &craft_pdf::interpret::font::OutlineGlyph,
m: Affine,
drawn: Option<(Paint, Option<StrokeProps>)>,
opacity: f32,
scale: f64,
overprint: bool,
) -> Option<GlyphRun> {
let (paint, stroke) = drawn?;
let mut path = o.outline();
if path.elements().is_empty() {
return None;
}
path.apply_affine(m);
Some(GlyphRun { path, paint, opacity, stroke, scale, overprint })
}
/// The characters of one glyph in the order they are drawn: a PDF names a ligature's characters
/// in logical order (lam then alef for لا), while a horizontal line of type is gathered glyph by
/// glyph in visual order and only then put back in logical order. Right-to-left characters are
+9 -1
View File
@@ -453,12 +453,20 @@ impl<'a> Device<'a> for Builder<'_> {
self.flush();
}
self.marked.pop();
self.actual.end();
// A span's glyphs are stroked within it: a stroke after it goes over none of them.
if self.actual.end() {
self.end_fills();
}
}
fn actual_text(&mut self, text: &[u8]) {
let active = self.actual.active();
if self.nested == 0 && self.text_as == crate::TextAs::Text && !self.actual.set(text) {
self.warn("ActualText beyond the import size or nesting limit was left to glyph mappings");
}
// Nor does a stroke in a span go over a glyph filled before it.
if !active && self.actual.active() {
self.end_fills();
}
}
}
+31
View File
@@ -58,6 +58,24 @@ pub(super) struct GlyphRun {
pub(super) overprint: bool,
}
/// A stroke over a glyph filled before it (fill and stroke rendering), held with the stroke pass
/// it is part of ([`Builder::hold_stroke`]): the glyph's font, placement, opacity and text, the
/// stroke, whether it overprints, the glyph's outline as drawn (kept as for a line of type kept
/// as outlines; none for a glyph set upright or one of an ActualText span), whether it is one of
/// an ActualText span, and the first glyph filled so it may go over, as its line's place among
/// the parked lines and the open one and its index in the line's filled glyphs.
pub(super) struct HeldStroke {
pub(super) font: u128,
pub(super) at: super::Placement,
pub(super) opacity: f32,
pub(super) text: String,
pub(super) stroke: TypeStroke,
pub(super) overprint: bool,
pub(super) glyph: Option<GlyphRun>,
pub(super) actual: bool,
pub(super) first: (usize, usize),
}
/// How often a tiling pattern was read, and the art (as its paints) each reading drew with the
/// swatch made for it.
pub(super) type PatternReadings = (u32, Vec<(Vec<Paint>, String)>);
@@ -94,6 +112,19 @@ pub(super) struct Builder<'p> {
/// kept as: what the line becomes when its type wouldn't draw as the file does
/// ([`super::reshaped`]).
pub(super) text_glyphs: Vec<GlyphRun>,
/// Lines of type a text-showing operator's fill pass finished (a `TJ`'s kerning starts a new
/// line part way through) that its stroke pass may still go over, oldest first, each with
/// its [`text_glyphs`](Self::text_glyphs): written before the open line, in this order
/// ([`Builder::park_text`]).
pub(super) parked: Vec<(TextLine, Vec<GlyphRun>)>,
/// The strokes of a text-showing operator's stroke pass over glyphs it filled on the
/// [`parked`](Self::parked) lines and the open one, held until the pass ends to be matched to
/// the glyphs it strokes ([`Builder::settle_strokes`]).
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`]).
pub(super) lost: Option<crate::import_text::LostFills>,
pub(super) text_as: TextAs,
pub(super) actual: super::actual_text::ActualText,
pub(super) images: HashMap<String, ImageBlob>,
+171 -40
View File
@@ -233,10 +233,8 @@ pub(crate) struct TextLine {
slots: Vec<Option<Slot>>,
/// Per character: authored logical order must survive visual-to-logical reconstruction.
authored: Vec<bool>,
/// Fill callbacks in an authored span, for the reader's later stroke pass.
actual_fills: HashMap<(u128, u64, u64), Option<usize>>,
/// The glyphs filled without a stroke, in drawing order, for a stroke drawn over them later
/// ([`stroke_filled`](Self::stroke_filled)).
/// ([`stroke_filled`](Self::stroke_filled)), those of an ActualText span among them.
filled: Vec<Filled>,
/// The first of [`filled`](Self::filled) a stroke may still be drawn over: a stroke matches
/// glyphs in the order they were filled, and each at most once.
@@ -246,6 +244,10 @@ pub(crate) struct TextLine {
pending: Option<PendingStroke>,
/// The last glyph's placement.
last_at: Placement,
/// The last glyph was drawn before a boundary of stroke passes ([`end_fills`](Self::end_fills)):
/// no stroke drawn since goes over it, not even as the glyph just drawn
/// ([`stroke_last`](Self::stroke_last)).
last_closed: bool,
/// The first glyph was set vertically ([`Upright`]).
upright: Option<Upright>,
/// The glyphs make a column (vertical type), each below the last.
@@ -279,7 +281,9 @@ struct Slot {
}
/// A glyph filled without a stroke: its font and placement, and where its text is in the line's
/// runs (the run's index, the text's byte offset in it and its length).
/// runs (the run's index, the text's byte offset in it and its length). A glyph of an ActualText
/// span has `actual`: `Some(true)` for the glyph whose run holds the replacement text,
/// `Some(false)` for the glyphs after it, which add no text (`len` 0 for both).
#[derive(Clone, Copy, Debug)]
struct Filled {
font: u128,
@@ -287,6 +291,57 @@ struct Filled {
run: usize,
start: usize,
len: usize,
actual: Option<bool>,
}
/// Whether a stroke over glyph `text` drawn at `at` in `font` at `opacity` may be drawn over a
/// glyph `fill` filled (its font, placement, whether it is a glyph of an ActualText span, and its
/// text) on a line that is `opaque` or not ([`TextLine::fills_at`]).
fn may_stroke(fill: (u128, Placement, bool, Option<&str>), opaque: bool, font: u128, at: Placement, opacity: f32, text: &str, actual: bool) -> bool {
let (g_font, g_at, g_actual, g_text) = fill;
if actual {
let same = |a: f64, b: f64| a.to_bits() == b.to_bits();
return g_actual && g_font == font && same(g_at.origin.x, at.origin.x) && same(g_at.origin.y, at.origin.y);
}
!g_actual && opacity >= 0.999 && opaque && g_font == font && g_at.same_as(&at) && g_text == Some(text)
}
/// 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.
#[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.
pub const MAX_LOST_FILLS: usize = 4096;
/// Add the glyphs `line` filled that a stroke may still go over.
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));
}
}
/// Whether a stroke over glyph `text` drawn at `at` in `font` at `opacity` may be drawn over
/// one of the glyphs ([`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))
}
}
/// Glyphs next to each other in one run that a stroke is drawn over, not yet restyled
@@ -331,11 +386,11 @@ impl TextLine {
text_bytes: 0,
slots: vec![],
authored: vec![],
actual_fills: HashMap::new(),
filled: vec![],
stroked: 0,
pending: None,
last_at: at,
last_closed: false,
upright: None,
column: false,
ink: vec![],
@@ -382,10 +437,10 @@ impl TextLine {
let Some((last, _, run)) = self.runs.last_mut() else {
self.runs.push((look.clone(), 0.0, text.to_string()));
self.last_start = 0;
self.record_actual_fill(look, at, content);
self.record_filled(look, at, content);
self.authored.extend(text.chars().map(|_| content.authored()));
(self.upright, self.next, self.last, self.last_at) = (Some(upright), at.origin + down * at.size, at.origin, at);
self.last_closed = false;
return true;
};
let across = (at.origin - self.at.origin).dot(self.at.dir);
@@ -420,12 +475,12 @@ impl TextLine {
self.runs.push((look.clone(), 0.0, text.to_string()));
self.last_start = 0;
}
self.record_actual_fill(look, at, content);
self.record_filled(look, at, content);
self.authored.extend(text.chars().map(|_| content.authored()));
self.next = at.origin + down * at.size;
self.last = at.origin;
self.last_at = at;
self.last_closed = false;
true
}
@@ -522,7 +577,6 @@ impl TextLine {
self.runs.push((look.clone(), 0.0, text.to_string()));
self.last_start = 0;
}
self.record_actual_fill(look, at, content);
self.record_filled(look, at, content);
self.text_bytes = self.text_bytes.saturating_add(text.len());
if !pen {
@@ -539,41 +593,38 @@ impl TextLine {
}
self.last = at.origin;
self.last_at = at;
self.last_closed = false;
self.last_dir = at.dir;
self.last_size = at.size;
self.baseline.push(at.origin);
true
}
fn record_actual_fill(&mut self, look: &Look, at: Placement, content: TextContent<'_>) {
if content.authored() && look.fill.is_some() {
let run = matches!(content, TextContent::Actual(_)).then(|| self.runs.len().saturating_sub(1));
self.actual_fills.insert((look.font, at.origin.x.to_bits(), at.origin.y.to_bits()), run);
}
}
/// Note glyph `content` just added to the last run, filled without a stroke, for a stroke
/// drawn over it later.
/// drawn over it later: a glyph of an ActualText span too, whose text is the replacement's
/// (the first glyph's) or none.
fn record_filled(&mut self, look: &Look, at: Placement, content: TextContent<'_>) {
let len = content.text().len();
if content.authored() || len == 0 || look.fill.is_none() || look.stroke.is_some() {
if look.fill.is_none() || look.stroke.is_some() {
return;
}
let run = self.runs.len().saturating_sub(1);
self.filled.push(Filled { font: look.font, at, run, start: self.last_start, len });
let (run, start) = (self.runs.len().saturating_sub(1), self.last_start);
let (start, len, actual) = match content {
TextContent::Mapped(text) if !text.is_empty() => (start, text.len(), None),
TextContent::Mapped(_) => return,
TextContent::Actual(_) => (0, 0, Some(true)),
TextContent::Pen => (0, 0, Some(false)),
};
self.filled.push(Filled { font: look.font, at, run, start, len, actual });
}
/// The reader fills a whole Tj/TJ before replaying its glyphs as strokes. A replay
/// must neither consume the replacement nor rewind the pen. Only the first glyph's
/// style belongs to the replacement; suppressed glyphs have no text to restyle.
/// style belongs to the replacement; suppressed glyphs have no text to restyle. The stroke
/// goes over the next glyph of the span filled in `font` at `at`, not stroked yet
/// ([`stroke_filled_at`](Self::stroke_filled_at)).
pub fn stroke_actual(&mut self, font: u128, at: Placement, stroke: TypeStroke, overprint: bool) -> bool {
self.flush_stroke();
let key = (font, at.origin.x.to_bits(), at.origin.y.to_bits());
let Some(run) = self.actual_fills.remove(&key) else { return false };
if let Some((look, ..)) = run.and_then(|i| self.runs.get_mut(i)) {
(look.stroke, look.overprint_stroke) = (Some(stroke), overprint);
}
true
let Some(index) = self.next_filled(0, font, at, 1.0, "", true) else { return false };
self.stroke_filled_at(index, stroke, overprint)
}
/// Has the baseline turned (more than about 3° from the first glyph to the last)?
@@ -625,21 +676,35 @@ impl TextLine {
// 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
&& g.at.same_as(&at)
&& self.runs.get(g.run).and_then(|(.., t)| t.get(g.start..g.start.saturating_add(g.len))) == Some(text)
})
});
let Some(index) = found.map(|i| self.stroked.saturating_add(i)) else {
let Some(index) = self.next_filled(0, font, at, opacity, text, false) else {
// 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 translucent stroke join the glyph just filled.
return opaque && self.stroke_last(font, at, text, stroke, overprint);
};
self.stroke_filled_at(index, stroke, overprint)
}
/// A stroke over the glyph filled at `index` in [`filled`](Self::filled), not stroked yet
/// ([`next_filled`](Self::next_filled), [`last_filled`](Self::last_filled)): that glyph is
/// stroked as [`stroke_filled`](Self::stroke_filled) strokes the glyph it finds, and no later
/// stroke goes over the glyphs filled before it. `false`: there is no such glyph.
/// A glyph of an ActualText span restyles its replacement's run at once, if it holds it.
pub fn stroke_filled_at(&mut self, index: usize, stroke: TypeStroke, overprint: bool) -> bool {
if index < self.stroked || index >= self.filled.len() {
return false;
}
if let Some(g) = self.filled.get(index).copied()
&& let Some(owner) = g.actual
{
self.flush_stroke();
if let Some((look, ..)) = self.runs.get_mut(g.run).filter(|_| owner) {
(look.stroke, look.overprint_stroke) = (Some(stroke), overprint);
}
self.stroked = index.saturating_add(1);
return true;
}
let continues = self
.pending
.as_ref()
@@ -661,6 +726,68 @@ impl TextLine {
true
}
/// The first glyph from index `from` of [`filled`](Self::filled) on, not stroked yet, that a
/// stroke over glyph `text` drawn at `at` in `font` at `opacity` may be drawn over
/// ([`fills_at`](Self::fills_at)). `None` for a translucent fill or stroke.
pub fn next_filled(&self, from: usize, font: u128, at: Placement, opacity: f32, text: &str, actual: bool) -> Option<usize> {
let from = from.max(self.stroked);
let i = self.filled.get(from..)?.iter().position(|g| self.fills(g, font, at, opacity, text, actual))?;
Some(from.saturating_add(i))
}
/// The last glyph before index `before` of [`filled`](Self::filled), not stroked yet, that a
/// stroke over glyph `text` drawn at `at` in `font` at `opacity` may be drawn over
/// ([`fills_at`](Self::fills_at)).
pub fn last_filled(&self, before: usize, font: u128, at: Placement, opacity: f32, text: &str, actual: bool) -> Option<usize> {
let rest = self.filled.get(self.stroked..before.min(self.filled.len()))?;
let i = rest.iter().rposition(|g| self.fills(g, font, at, opacity, text, actual))?;
Some(self.stroked.saturating_add(i))
}
/// Whether the glyph filled at `index` in [`filled`](Self::filled), not stroked yet, is one a
/// stroke over glyph `text` drawn at `at` in `font` at `opacity` may be drawn over: opaque,
/// in the same font, with the same placement and text. With `actual`, a stroke over a glyph
/// of an ActualText span instead: a glyph of the span filled in the same font at the same
/// origin, at any opacity ([`stroke_actual`](Self::stroke_actual)): the span's replacement
/// text goes with the glyph filled first, so a translucent stroke kept apart from its fill
/// would be type with no text, and come in as nothing.
pub fn fills_at(&self, index: usize, font: u128, at: Placement, opacity: f32, text: &str, actual: bool) -> bool {
index >= self.stroked && self.filled.get(index).is_some_and(|g| self.fills(g, font, at, opacity, text, actual))
}
/// How many glyphs the line filled for a stroke drawn over them later, stroked or not
/// ([`filled`](Self::filled)).
pub fn filled_len(&self) -> usize {
self.filled.len()
}
/// [`fills_at`](Self::fills_at) for glyph `g`.
fn fills(&self, g: &Filled, font: u128, at: Placement, opacity: f32, text: &str, actual: bool) -> bool {
let (fill, opaque) = ((g.font, g.at, g.actual.is_some(), self.glyph_text(g)), self.opacity >= 0.999);
may_stroke(fill, opaque, font, at, opacity, text, actual)
}
/// The text of filled glyph `g` in the line's runs.
fn glyph_text(&self, g: &Filled) -> Option<&str> {
self.runs.get(g.run).and_then(|(.., t)| t.get(g.start..g.start.saturating_add(g.len)))
}
/// Whether a stroke drawn later may still be drawn over a glyph of the line (fill and stroke
/// rendering): an opaque glyph filled and not stroked yet, or a filled glyph of an
/// ActualText span ([`stroke_actual`](Self::stroke_actual)).
pub fn awaits_stroke(&self) -> bool {
self.filled.get(self.stroked..).is_some_and(|rest| rest.iter().any(|g| g.actual.is_some() || self.opacity >= 0.999))
}
/// No stroke drawn from now on goes over a glyph the line filled so far: a stroke pass does
/// not go back past a stroke of its own (one over no glyph filled) or the start or end of an
/// ActualText span ([`stroke_filled`](Self::stroke_filled)). Nor does one restyle the glyph
/// drawn last ([`stroke_last`](Self::stroke_last)).
pub fn end_fills(&mut self) {
self.stroked = self.filled.len();
self.last_closed = true;
}
/// 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. Text that comes right after a run
@@ -729,9 +856,6 @@ impl TextLine {
for g in self.filled.iter_mut().skip(from) {
(g.run, g.start) = moved(g.run, g.start);
}
for i in self.actual_fills.values_mut().flatten() {
*i = moved(*i, 0).0;
}
if last {
self.last_start = moved(run, self.last_start).1;
}
@@ -744,7 +868,14 @@ impl TextLine {
/// ([`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.
///
/// Not a glyph drawn before a boundary of stroke passes ([`end_fills`](Self::end_fills)):
/// a stroke after the end of an ActualText span would otherwise stroke the span's replacement
/// text, and one after its start a glyph filled before the span (#8543).
fn stroke_last(&mut self, font: u128, at: Placement, glyph: &str, stroke: TypeStroke, overprint: bool) -> bool {
if self.last_closed {
return false;
}
self.flush_stroke();
let start = self.last_start;
let last_at = self.last_at;
+222
View File
@@ -346,3 +346,225 @@ fn glyphs_filled_and_stroked_one_by_one_stay_one_run() {
assert_eq!(type_runs(content), [vec![("ABC".to_string(), true, true)]], "{content}");
}
}
/// Fill and stroke rendering of a `TJ` whose kerning starts a new line part way through: the
/// reader fills every glyph of the operator before it strokes them, and each part comes in as
/// type filled and stroked, in the order drawn, not as each part filled and then each part
/// stroked over it (#8543). So too a `TJ` that jumps back to start a line before it and one whose
/// kerning starts two new lines. In a span with replacement text (`ActualText`) it stays one type
/// object, on the first glyph's line and in its style, filled and stroked; small kerning keeps
/// one line as before.
#[test]
fn a_tj_whose_kerning_starts_a_new_line_is_filled_and_stroked() {
// Each type object as its text, and whether all of it is filled and all of it stroked.
let objects = |content: &str| -> Vec<(String, bool, bool)> {
type_runs(content)
.into_iter()
.map(|runs| (runs.iter().map(|r| r.0.as_str()).collect(), runs.iter().all(|r| r.1), runs.iter().all(|r| r.2)))
.collect()
};
let both = |text: &str| (text.to_string(), true, true);
for mode in ["2", "6"] {
let cases = [
("/F1 48 Tf 20 80 Td [(AB) -3500 (CD)] TJ", vec![both("AB"), both("CD")]),
("/F1 24 Tf 20 80 Td [(AB) -3500 (CD) -3500 (EF)] TJ", vec![both("AB"), both("CD"), both("EF")]),
("/F1 24 Tf 200 80 Td [(AB) 6000 (CD)] TJ", vec![both("AB"), both("CD")]),
("/F1 48 Tf 20 80 Td [(AB) -100 (CD)] TJ", vec![both("ABCD")]),
// In a span with replacement text, which places all of its text with the first glyph.
("/F1 48 Tf 20 80 Td /Span << /ActualText (ABCD) >> BDC [(AB) -3500 (CD)] TJ EMC", vec![both("ABCD")]),
];
for (show, want) in cases {
let content = format!("BT {mode} Tr {show} 0 Tr ET");
assert_eq!(objects(&content), want, "{content}: {:?}", type_runs(&content));
}
}
}
/// Stroke rendering (modes 1 and 5) of a `TJ` whose kerning starts a new line stays stroke only,
/// one type object per line (#8543).
#[test]
fn a_stroked_tj_whose_kerning_starts_a_new_line_stays_stroke_only() {
for mode in ["1", "5"] {
let content = format!("BT {mode} Tr /F1 48 Tf 20 80 Td [(AB) -3500 (CD)] TJ 0 Tr ET");
let want = [vec![("AB".to_string(), false, true)], vec![("CD".to_string(), false, true)]];
assert_eq!(type_runs(&content), want, "{content}");
}
}
/// Translucent type filled and stroked by a `TJ` whose kerning starts a new line stays a filled
/// object and a stroked one for each part, in the order the file draws them (#8015, #8543).
#[test]
fn a_translucent_tj_whose_kerning_starts_a_new_line_stays_fill_and_stroke_apart() {
use vectorcraft_testkit::pdf::{PdfPage, pdf};
let resources = "/Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Helvetica >> >> /ExtGState << /G << /ca 0.5 /CA 0.5 >> >>";
let content = "/G gs 0 0 0 RG 1 1 0.8 rg 3 w BT 2 Tr /F1 48 Tf 20 80 Td [(AB) -3500 (CD)] TJ 0 Tr ET";
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 {
let st = t.first_style();
types.push((t.plain_text(), n.opacity < 1.0, !st.fill.is_none(), !st.stroke.is_none()));
}
});
let part = |text: &str, fill: bool| (text.to_string(), true, fill, !fill);
assert_eq!(types, [part("AB", true), part("CD", true), part("AB", false), part("CD", false)], "{content}");
}
/// Fill and stroke rendering of a `TJ` whose kerning goes back to draw a glyph again where it
/// drew one before, on a line of its own: the reader strokes the glyphs in the order it filled
/// them, so each copy is filled and stroked, whether the copy drawn last ends the operator (the
/// open line) or a line after it does (a finished one), and however often it comes back
/// (#8543). A glyph filled and stroked by an operator of its own where one filled before stands
/// still strokes only the one filled last (#8015).
#[test]
fn a_tj_that_draws_a_glyph_again_where_it_drew_one_strokes_each_copy() {
let both = |text: &str| vec![(text.to_string(), true, true)];
// Helvetica at 48 points: A and B are 667 units wide, so 4834 back after B (3500 on from A)
// draws A again where the first A stands.
for mode in ["2", "6"] {
let cases = [
("[(A) -3500 (B) 4834 (A)] TJ", vec![both("A"), both("B"), both("A")]),
("[(A) -3500 (B) 4834 (A) -6000 (C)] TJ", vec![both("A"), both("B"), both("A"), both("C")]),
("[(A) -3500 (B) 4834 (A) -3500 (B)] TJ", vec![both("A"), both("B"), both("A"), both("B")]),
("[(A) -3500 (B) 4834 (A) -3500 (B) 4834 (A)] TJ", vec![both("A"), both("B"), both("A"), both("B"), both("A")]),
];
for (show, want) in cases {
let content = format!("BT {mode} Tr /F1 48 Tf 20 80 Td {show} 0 Tr ET");
assert_eq!(type_runs(&content), want, "{content}");
}
}
let content = "BT 0 Tr /F1 48 Tf 20 80 Td [(A) -3500 (B) 4834] TJ 2 Tr (A) Tj 0 Tr ET";
let want = [vec![("A".to_string(), true, false)], vec![("B".to_string(), true, false)], both("A")];
assert_eq!(type_runs(content), want, "{content}");
}
/// A stroke pass longer than the importer holds at once ([`MAX_HELD`] strokes) is matched to its
/// own operator's glyphs, in the order filled: an `A` filled by an operator of its own, then `A`
/// and 4,096 `B`s filled and stroked from the same place, strokes the second `A`, not the first
/// (#8543).
#[test]
fn a_long_stroke_pass_strokes_its_own_glyphs_not_one_filled_before() {
let bs = "B".repeat(4096);
let content = format!("BT /F1 2 Tf 1 0 0 1 20 80 Tm 0 Tr (A) Tj 1 0 0 1 20 80 Tm 2 Tr (A{bs}) Tj 0 Tr ET");
let looks: Vec<Vec<(String, bool, bool)>> = type_runs(&content);
let want = [vec![("A".to_string(), true, false)], vec![(format!("A{bs}"), true, true)]];
assert!(looks == want, "{:?}", looks.iter().map(|t| t.iter().map(|r| (r.0.len(), r.1, r.2)).collect::<Vec<_>>()).collect::<Vec<_>>());
}
/// Stroked type of its own (a stroke over no glyph filled) ends the stroke pass of the glyphs
/// filled before it: an `A` filled, a `B` filled on a line of its own, a `C` stroked after the
/// `B`, then a stroke over the `A` is type of its own, drawn last, not a stroke joined to the
/// `A` drawn under the `B` and `C` (#8543).
#[test]
fn a_stroke_after_stroked_type_of_its_own_does_not_join_a_glyph_filled_before() {
let content = "BT /F1 48 Tf 1 0 0 1 20 80 Tm 0 Tr (A) Tj 1 0 0 1 20 140 Tm (B) Tj 1 Tr (C) Tj 1 0 0 1 20 80 Tm (A) Tj 0 Tr ET";
let run = |text: &str, fill: bool, stroke: bool| (text.to_string(), fill, stroke);
let want = [vec![run("A", true, false)], vec![run("B", true, false), run("C", false, true)], vec![run("A", false, true)]];
assert_eq!(type_runs(content), want, "{content}");
}
/// A stroke in an ActualText span goes over a glyph that span filled, not one an earlier span
/// filled at the same place: each span's replacement text comes in (#8543).
#[test]
fn a_stroke_in_an_actual_text_span_does_not_take_an_earlier_spans_glyph() {
let content = "BT /F1 48 Tf 0 Tr /Span << /ActualText (First) >> BDC 1 0 0 1 20 80 Tm (A) Tj EMC 1 0 0 1 20 140 Tm (B) Tj \
/Span << /ActualText (Second) >> BDC 1 Tr 1 0 0 1 20 80 Tm (A) Tj EMC 0 Tr ET";
let run = |text: &str, fill: bool, stroke: bool| vec![(text.to_string(), fill, stroke)];
assert_eq!(type_runs(content), [run("First", true, false), run("B", true, false), run("Second", false, true)], "{content}");
}
/// Fill and stroke rendering of a `TJ` that draws a glyph again where it drew one, in an
/// ActualText span: the strokes go over the glyphs in the order filled, so the replacement text,
/// on the first glyph's line and in its style, is filled and stroked, not left to the copy drawn
/// last, which shows no text (#8543).
#[test]
fn a_tj_that_draws_a_glyph_again_in_an_actual_text_span_strokes_the_replacement() {
for mode in ["2", "6"] {
for show in ["[(A) 667 (A)] TJ", "[(A) -500 (B) 1834 (A)] TJ", "[(A) -3500 (B) 4834 (A)] TJ"] {
let content = format!("BT {mode} Tr /F1 48 Tf 20 80 Td /Span << /ActualText (ABA) >> BDC {show} EMC 0 Tr ET");
assert_eq!(type_runs(&content), [vec![("ABA".to_string(), true, true)]], "{content}");
}
}
}
/// The lines of type one operator's fill pass finishes are kept for its stroke pass up to 64 at
/// a time: a `TJ` whose kerning starts 65 lines comes in as 65 type objects filled and stroked,
/// and one that starts 66 as a filled object and a stroked one for each part, the fills first,
/// as the file draws them (#8543).
#[test]
fn a_tj_over_more_lines_than_are_kept_comes_in_as_fill_and_stroke_apart() {
let content = |n: usize| format!("BT 2 Tr /F1 4 Tf 20 80 Td [{}] TJ 0 Tr ET", vec!["(A)"; n].join(" -3500 "));
let both = vec![("A".to_string(), true, true)];
assert_eq!(type_runs(&content(65)), vec![both; 65]);
let (fill, stroke) = (vec![("A".to_string(), true, false)], vec![("A".to_string(), false, true)]);
let want: Vec<_> = std::iter::repeat_n(fill, 66).chain(std::iter::repeat_n(stroke, 66)).collect();
assert_eq!(type_runs(&content(66)), want);
}
/// A `TJ` over more lines than are kept whose last part draws its first glyph again at the first
/// glyph's origin still comes in as a filled object and a stroked one for each part: the stroke
/// over the first glyph, whose line was finished, doesn't take the last part's fill (#8543).
#[test]
fn a_tj_over_more_lines_than_are_kept_that_ends_where_it_began_stays_fill_and_stroke_apart() {
// Helvetica's A is 667 units wide: 65 As and 64 kerns of 3,500 back to the first A's origin.
let parts = vec!["(A)"; 65].join(" -3500 ");
let content = format!("BT 2 Tr /F1 4 Tf 20 80 Td [{parts} 267355 (A)] TJ 0 Tr ET");
let (fill, stroke) = (vec![("A".to_string(), true, false)], vec![("A".to_string(), false, true)]);
let want: Vec<_> = std::iter::repeat_n(fill, 66).chain(std::iter::repeat_n(stroke, 66)).collect();
assert_eq!(type_runs(&content), want);
}
/// Type filled and stroked right after a `TJ` over more lines than are kept is filled and
/// stroked as before, whether the `TJ` was filled and stroked or filled only (#8543).
#[test]
fn type_after_a_tj_over_more_lines_than_are_kept_is_filled_and_stroked() {
let parts = vec!["(A)"; 66].join(" -3500 ");
for mode in ["2", "0"] {
let content = format!("BT /F1 4 Tf 20 80 Td {mode} Tr [{parts}] TJ 2 Tr /F1 48 Tf 1 0 0 1 40 150 Tm (B) Tj 0 Tr ET");
let types = type_runs(&content);
assert_eq!(types.len(), if mode == "2" { 133 } else { 67 }, "{content}");
assert_eq!(types.last(), Some(&vec![("B".to_string(), true, true)]), "{content}");
}
}
/// The start and end of an ActualText span end the stroke pass of the glyphs filled before them,
/// for the stroke over the glyph filled last too: a stroke after the span over the glyph its
/// replacement text came with, or over a glyph filled before an empty span, is type of its own,
/// and the glyph keeps its fill only (#8543).
#[test]
fn a_stroke_after_an_actual_text_span_does_not_take_a_glyph_filled_before_it() {
let run = |text: &str, fill: bool, stroke: bool| vec![(text.to_string(), fill, stroke)];
let cases = [
"/Span << /ActualText (A) >> BDC 1 0 0 1 20 80 Tm (A) Tj EMC 1 Tr 1 0 0 1 20 80 Tm (A) Tj",
"1 0 0 1 20 80 Tm (A) Tj /Span << /ActualText (B) >> BDC EMC 1 Tr 1 0 0 1 20 80 Tm (A) Tj",
];
for show in cases {
let content = format!("BT /F1 48 Tf 0 Tr {show} 0 Tr ET");
assert_eq!(type_runs(&content), [run("A", true, false), run("A", false, true)], "{content}");
}
}
/// A glyph stroked twice where it was drawn, by stroke rendering or by fill and stroke rendering
/// then stroke rendering, is one type object, as before: a stroke over the glyph drawn last
/// restyles it when nothing ended its stroke pass (#7741, #8015).
#[test]
fn a_glyph_stroked_again_where_it_was_drawn_stays_one_type_object() {
for (show, fill) in [("1 Tr (A) Tj", false), ("2 Tr (A) Tj", true)] {
let content = format!("BT /F1 48 Tf {show} 1 Tr 1 0 0 1 20 80 Tm (A) Tj 0 Tr ET").replace("Tf ", "Tf 1 0 0 1 20 80 Tm ");
assert_eq!(type_runs(&content), [vec![("A".to_string(), fill, true)]], "{content}");
}
}
/// A stroke pass longer than the importer holds at once ([`MAX_HELD`] strokes) goes over its own
/// operator's glyphs, filled right before it, also when an operator before it filled the same
/// glyphs at the same places: `A` and 4,095 `B`s filled, then `A` and 4,096 `B`s filled and
/// stroked from the same place strokes the second operator's glyphs, not the first one's (#8543).
#[test]
fn a_long_stroke_pass_does_not_take_the_same_glyphs_filled_by_an_operator_before() {
let (before, now) = ("B".repeat(4095), "B".repeat(4096));
let content = format!("BT /F1 2 Tf 1 0 0 1 20 80 Tm 0 Tr (A{before}) Tj 1 0 0 1 20 80 Tm 2 Tr (A{now}) Tj 0 Tr ET");
let looks: Vec<Vec<(String, bool, bool)>> = type_runs(&content);
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<_>>());
}
File diff suppressed because one or more lines are too long
+2 -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