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
@@ -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());