From 87a81f16cd4a65ac6acddca4ec7e0115b9374c6b Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 02:28:45 +0800 Subject: [PATCH] Embed table-cell images at their grid rectangles --- crates/texrra-core/src/document.rs | 1 + crates/texrra-core/src/document_parser.rs | 6 ++-- docs/STATUS.md | 2 +- src/rust_typeset/pdf.rs | 1 + src/rust_typeset/pdf/layout.rs | 19 +++++++++++ src/rust_typeset/text_layout.rs | 39 ++++++++++++++++++----- tests/typeset.rs | 4 +++ 7 files changed, 61 insertions(+), 11 deletions(-) diff --git a/crates/texrra-core/src/document.rs b/crates/texrra-core/src/document.rs index 2de2b5b..ddc5f92 100644 --- a/crates/texrra-core/src/document.rs +++ b/crates/texrra-core/src/document.rs @@ -433,6 +433,7 @@ pub struct RuleBox { pub height: common::Scaled, /// Optional fill colour; means the regular black rule. pub color: Option<[u8; 3]>, + pub image: Option, } /// Paragraph-level alignment requested by `\raggedright` or `\raggedleft`. diff --git a/crates/texrra-core/src/document_parser.rs b/crates/texrra-core/src/document_parser.rs index b51f1ba..8c5eb8e 100644 --- a/crates/texrra-core/src/document_parser.rs +++ b/crates/texrra-core/src/document_parser.rs @@ -1719,6 +1719,7 @@ impl Parser { raise, height, color: None, + image: None, }), box_run: None, unresolved_reference: None, @@ -2312,8 +2313,8 @@ impl Parser { return; } if let Some(table) = &mut self.table { - // Table-cell images are represented as grey boxes until the cell - // image renderer lands. This still preserves the reference grid. + // Table-cell images reuse the rule box so table layout can keep the + // reference grid; the PDF renderer embeds the source image at this rect. let width = (0.138_f64 * 160.0 * 72.27 / 25.4 * 65536.0).round() as i32; let height = (width as f64 * 1.35).round() as i32; let mut run = Run::plain(String::new()); @@ -2322,6 +2323,7 @@ impl Parser { raise: common::Scaled(0), height: common::Scaled(height), color: Some([170, 170, 170]), + image: Some(path.clone()), }); table.cell.push(run); return; diff --git a/docs/STATUS.md b/docs/STATUS.md index af74781..d84bb47 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -150,7 +150,7 @@ See `docs/ROADMAP.md` for concrete implementation plans. since printpdf 0.3.4 has no annotation API. - Fonts: bundled reserved-glyph subsets in `assets/fonts-subset/` are used when the document charset is covered; arXiv PDF is about 0.29 MB. -- New-template compatibility: the SIVP class file loads fully and `sn-article.tex` previews and exports a 14-page PDF; PDF figures render as placeholders and table-cell images are emitted as placeholder blocks that preserve the reference grid geometry instead of becoming page-sized strips. Its journal title block captures title, author/affiliation, corresponding-author stars, both e-mails, abstract, and keywords; class declaration option leakage is consumed. The Huawei E fragment compiles to 16 A4 pages. Generic support added for TeX `\if`/`\ifx`, `\newcount`, `\newdimen`, `\char`, `tabular*`/`tabularx`, math accents/operators/arrows, a 64-bit dimension scanner, and `geometry` `text={width,height}` blocks. Both fixtures are regression tests (`sivp_project_previews_and_exports_a_pdf`, `e_fixture_compiles`). +- New-template compatibility: the SIVP class file loads fully and `sn-article.tex` previews and exports a 14-page PDF; PDF figures render as placeholders and table-cell images are embedded at fixed grid rectangles instead of becoming page-sized strips. Its journal title block captures title, author/affiliation, corresponding-author stars, both e-mails, abstract, and keywords; class declaration option leakage is consumed. The Huawei E fragment compiles to 16 A4 pages. Generic support added for TeX `\if`/`\ifx`, `\newcount`, `\newdimen`, `\char`, `tabular*`/`tabularx`, math accents/operators/arrows, a 64-bit dimension scanner, and `geometry` `text={width,height}` blocks. Both fixtures are regression tests (`sivp_project_previews_and_exports_a_pdf`, `e_fixture_compiles`). - Lines whose justification ratio exceeds 3 fall back to natural spacing instead of stretching inter-word gaps across the measure. diff --git a/src/rust_typeset/pdf.rs b/src/rust_typeset/pdf.rs index 97dd7f6..773e4c6 100644 --- a/src/rust_typeset/pdf.rs +++ b/src/rust_typeset/pdf.rs @@ -1785,6 +1785,7 @@ fn boxworks_line_break( raise: rule.raise, height: rule.height, y_offset, + image: rule.image.clone(), }); } else { let (bold, italic, monospace, sans, glyph_size) = style_by_font diff --git a/src/rust_typeset/pdf/layout.rs b/src/rust_typeset/pdf/layout.rs index cdcbaec..d2282d8 100644 --- a/src/rust_typeset/pdf/layout.rs +++ b/src/rust_typeset/pdf/layout.rs @@ -533,6 +533,7 @@ fn append_list_layout_tokens( #[allow(clippy::too_many_arguments)] fn layout_table( + source_dir: &Path, rows: &[Vec>], alignments: &[ColumnAlignment], rules: &[TableRule], @@ -548,6 +549,18 @@ fn layout_table( cache: &mut ParagraphLayoutCache, font_size: u8, ) -> Option { + let mut rows = rows.to_vec(); + for row in &mut rows { + for cell in row { + for run in cell { + if let Some(rule) = run.rule.as_mut() + && let Some(image) = rule.image.as_mut() + { + *image = source_dir.join(&*image).to_string_lossy().into_owned(); + } + } + } + } let row_column_count = |row: &Vec>, styles: Option<&Vec>| { let mut columns = 0; for index in 0..row.len() { @@ -1007,6 +1020,7 @@ fn layout_framed_runs( raise: rule.raise, height: rule.height, y_offset: run.y_offset.unwrap_or_default(), + image: rule.image.clone(), }); width += Mm::from(scaled_units_to_mm(i64::from(rule.width.0))); let above = i64::from(rule.raise.0) + i64::from(rule.height.0); @@ -1515,6 +1529,7 @@ fn append_page_footnotes( raise: common::Scaled::ZERO, height: common::Scaled(separator as i32), y_offset: common::Scaled::ZERO, + image: None, }], pre_display_size: None, }; @@ -1679,6 +1694,7 @@ fn push_title_rule( raise: common::Scaled::ZERO, height: common::Scaled(height as i32), y_offset: common::Scaled::ZERO, + image: None, }], pre_display_size: None, }; @@ -2659,6 +2675,7 @@ pub(super) fn build_positioned_pages( raise: common::Scaled::ZERO, height: *height, y_offset: common::Scaled::ZERO, + image: None, }], pre_display_size: None, }; @@ -2891,6 +2908,7 @@ pub(super) fn build_positioned_pages( ) }); let table = layout_table( + source_dir, rows, alignments, rules, @@ -3731,6 +3749,7 @@ fn append_header_rule(page: &mut PositionedPage, page_number: usize, y: i64, wid raise: common::Scaled::ZERO, height: common::Scaled(rule_height as i32), y_offset: common::Scaled::ZERO, + image: None, }], pre_display_size: None, }, diff --git a/src/rust_typeset/text_layout.rs b/src/rust_typeset/text_layout.rs index 6769775..e11bc3a 100644 --- a/src/rust_typeset/text_layout.rs +++ b/src/rust_typeset/text_layout.rs @@ -33,6 +33,7 @@ pub(super) enum LineItem { raise: Scaled, height: Scaled, y_offset: Scaled, + image: Option, }, Frame { width: Scaled, @@ -99,6 +100,7 @@ enum TextLineItem { raise: Mm, height: Mm, y_offset: Mm, + image: Option, }, Frame { width: Mm, @@ -118,6 +120,7 @@ pub(super) struct PositionedRule { pub(super) raise: Mm, pub(super) height: Mm, pub(super) y_offset: Mm, + pub(super) image: Option, } impl TextLine { @@ -233,6 +236,7 @@ impl TextLine { raise, height, y_offset, + image, } => { flush_text( &mut items, @@ -248,6 +252,7 @@ impl TextLine { raise: scaled_to_mm(*raise), height: scaled_to_mm(*height), y_offset: scaled_to_mm(*y_offset), + image: image.clone(), }); } LineItem::Glue(width) => { @@ -388,6 +393,7 @@ impl TextLine { raise, height, y_offset, + image, } => { if *height > Mm::from(0.0) { ascent = ascent.max(*raise + *height + *y_offset); @@ -398,6 +404,7 @@ impl TextLine { raise: *raise, height: *height, y_offset: *y_offset, + image: image.clone(), }); } } @@ -493,13 +500,19 @@ impl TextLine { ); } for rule in &layout.rules { + let fill = if rule.image.is_some() { + "#aaaaaa" + } else { + "#000000" + }; let _ = write!( output, - "", + "", f64::from(x + horizontal_offset + rule.x), f64::from(baseline - rule.y_offset - rule.raise - rule.height), f64::from(rule.width), f64::from(rule.height), + fill, ); } for span in &layout.spans { @@ -639,14 +652,24 @@ impl Element for TextLine { ); } for rule in &layout.rules { - area.fill_rect( - Position::new( - rule.x + horizontal_offset, - layout.ascent - rule.y_offset - rule.raise - rule.height, - ), - Size::new(rule.width, rule.height), - Style::new().with_color(genpdf::style::Color::Rgb(0, 0, 0)), + let position = Position::new( + rule.x + horizontal_offset, + layout.ascent - rule.y_offset - rule.raise - rule.height, ); + if let Some(path) = &rule.image { + let mut image = genpdf::elements::Image::from_path(path)?; + let mut image_area = area.clone(); + image_area.add_offset(position); + image_area.set_width(rule.width); + image_area.set_height(rule.height); + image.render(context, image_area, inherited_style)?; + } else { + area.fill_rect( + position, + Size::new(rule.width, rule.height), + Style::new().with_color(genpdf::style::Color::Rgb(0, 0, 0)), + ); + } } let mut placements = Vec::with_capacity(layout.spans.len()); diff --git a/tests/typeset.rs b/tests/typeset.rs index 1f1a0d1..556108f 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -1941,6 +1941,10 @@ fn sivp_project_previews_and_exports_a_pdf() { "missing table image label {needle}" ); } + assert!( + all_svg.contains("fill=\"#aaaaaa\""), + "table image cells should stay image-backed" + ); let pdf = CompilerSession::default() .compile(&source, "projects/_sivp__/sn-article.tex") .expect("SIVP PDF");