Embed table-cell images at their grid rectangles

This commit is contained in:
gloamxun committed 2026-10-20 02:28:45 +08:00
1 parent d50b4bf560
commit 87a81f16cd
7 files changed
+61 -11

No files matched your search

+1
View File
@@ -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<String>,
}
/// Paragraph-level alignment requested by `\raggedright` or `\raggedleft`.
+4 -2
View File
@@ -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;
+1 -1
View File
@@ -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.
+1
View File
@@ -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
+19
View File
@@ -533,6 +533,7 @@ fn append_list_layout_tokens(
#[allow(clippy::too_many_arguments)]
fn layout_table(
source_dir: &Path,
rows: &[Vec<Vec<Run>>],
alignments: &[ColumnAlignment],
rules: &[TableRule],
@@ -548,6 +549,18 @@ fn layout_table(
cache: &mut ParagraphLayoutCache,
font_size: u8,
) -> Option<PositionedTable> {
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<Vec<Run>>, styles: Option<&Vec<TableCellStyle>>| {
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,
},
+31 -8
View File
@@ -33,6 +33,7 @@ pub(super) enum LineItem {
raise: Scaled,
height: Scaled,
y_offset: Scaled,
image: Option<String>,
},
Frame {
width: Scaled,
@@ -99,6 +100,7 @@ enum TextLineItem {
raise: Mm,
height: Mm,
y_offset: Mm,
image: Option<String>,
},
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<String>,
}
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,
"<rect x=\"{:.4}\" y=\"{:.4}\" width=\"{:.4}\" height=\"{:.4}\" fill=\"#000000\"/>",
"<rect x=\"{:.4}\" y=\"{:.4}\" width=\"{:.4}\" height=\"{:.4}\" fill=\"{}\"/>",
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());
+4
View File
@@ -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");