Preserve pdfTeX text positioning across direct color changes

This commit is contained in:
gloamxun committed 2026-11-26 15:19:17 +08:00
1 parent 0f6aa1f33d
commit f88a16d101
15 files changed
+193 -44

No files matched your search

@@ -354,3 +354,50 @@ fn colors_follow_actual_glyphs_and_survive_retained_execution() {
"{report:?}: PDF glyph raster differs"
);
}
#[test]
fn color_stack_modes_preserve_or_reset_the_text_cursor() {
let directory = std::env::temp_dir().join(format!("texrra-color-modes-{}", std::process::id()));
let data = br"\catcode123=1 \catcode125=2
\pdfoutput=1 \pdfcompresslevel=0 \pdfobjcompresslevel=0
\pdfpagewidth=210pt \pdfpageheight=120pt
\font\sample=cmr10 \sample
\edef\onpage{\pdfcolorstackinit page page{}}
\edef\atpoint{\pdfcolorstackinit{}}
\edef\intext{\pdfcolorstackinit direct{}}
\shipout\hbox{\kern131sp A\pdfcolorstack0 push{.21 g}\kern2.26pt B%
\pdfcolorstack\onpage set{.32 g}C\pdfcolorstack\atpoint set{.43 g}D%
\pdfcolorstack\intext set{.54 g}E}
\end
";
let original = reference_pdf(&directory, "colored-text-modes", data);
let mut host = Host::default();
let execution = run(data.to_vec(), &mut host).unwrap();
let restored: Execution =
serde_json::from_slice(&serde_json::to_vec(&execution).unwrap()).unwrap();
let mut pages = restored.shipout_display_lists().unwrap();
texrra_tex_core::native_font::resolve_native_fonts(&mut pages, &mut host).unwrap();
let native =
pdf::shipout_lists_to_native_pdf(&pages, restored.state.pdf_final_options()).unwrap();
for pdf in [fs::read(&original).unwrap(), native] {
let streams = streams(&pdf);
let page = streams.iter().find(|stream| stream.contains("\n.21 g\n")).unwrap();
let tokens: Vec<_> = page.split_ascii_whitespace().collect();
for (color, in_text, moves_origin) in [
(".21", true, false),
(".32", false, false),
(".43", false, true),
(".54", true, false),
] {
let at = tokens.windows(2).position(|w| w == [color, "g"]).unwrap();
let state = tokens[..at]
.iter()
.rev()
.find(|&&token| matches!(token, "BT" | "ET"))
.unwrap();
assert_eq!(*state == "BT", in_text, "color {color}");
assert_eq!(tokens[at - 1] == "cm", moves_origin, "color {color}");
}
}
}