Fill the page background with pagecolor

This commit is contained in:
gloamxun committed 2026-10-26 02:31:50 +08:00
1 parent 4cb456743a
commit 1ab2a29bfa
6 files changed
+56 -5

No files matched your search

+30
View File
@@ -187,6 +187,8 @@ struct PageGeometry {
lengths: Vec<(String, i64)>,
/// Colors declared with `\definecolor` and their RGB values.
colors: Vec<(String, [u8; 3])>,
/// Background colour painted behind every page.
pub page_color: Option<[u8; 3]>,
/// Integer registers declared with `\newcount` and their current values.
count_registers: Vec<(String, i64)>,
/// Dimension registers declared with `\newdimen`, in scaled points.
@@ -1855,6 +1857,16 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
.extend(graphics_path_groups(trimmed));
continue;
}
if trimmed.starts_with("\\pagecolor") {
let rest = trimmed
.strip_prefix("\\pagecolor")
.map(str::trim_start)
.unwrap_or("");
if let Some((name, _)) = braced_group(rest) {
geometry.page_color = named_color(name.trim(), &geometry.colors);
}
continue;
}
if let Some((name, rgb)) = define_color(trimmed) {
geometry.colors.retain(|(known, _)| known != &name);
geometry.colors.push((name, rgb));
@@ -3271,6 +3283,10 @@ pub fn pdf_bytes_with_images(
page.display_list_with_layout(document.page_goal, document.text_float_sep);
list.width = list.width.max(document.paper_width);
list.height = list.height.max(document.paper_height);
if let Some(color) = geometry.page_color {
list.items
.insert(0, texrra_tex_core::DisplayItem::Background(color));
}
list
})
.collect();
@@ -8171,6 +8187,10 @@ fn render_json(
page.display_list_with_layout(document.page_goal, document.text_float_sep);
list.width = list.width.max(document.paper_width);
list.height = list.height.max(document.paper_height);
if let Some(color) = geometry.page_color {
list.items
.insert(0, texrra_tex_core::DisplayItem::Background(color));
}
list
})
.collect();
@@ -11091,6 +11111,16 @@ A definition.
assert!(boxes.iter().any(|box_| box_.character == Some('$')));
}
#[test]
fn pagecolor_fills_the_page_background() {
let json = preview_json("\\pagecolor{yellow}\n\nBody.").expect("preview");
assert!(json.contains(r##"fill=\"#ffff00\""##), "{json}");
assert!(!json.contains("pagecolor"));
let pdf = pdf_bytes("\\pagecolor{yellow}\n\nBody.").expect("pdf");
let text = String::from_utf8_lossy(&pdf);
assert!(text.contains("1.000 1.000 0.000 rg"), "{text}");
}
#[test]
fn textcolor_paints_red_glyphs() {
let source = "Body \\textcolor{red}{red} word.";