Embed project images in route-D previews and PDFs

This commit is contained in:
GloamXun committed 2026-10-24 23:29:15 +08:00
1 parent ae69eba64d
commit 0f8aff7290
8 files changed
+407 -18

No files matched your search

+101
View File
@@ -124,6 +124,12 @@ pub enum VerticalItem {
height: i64,
depth: i64,
},
/// A raster image painted into the vertical list.
Image {
width: i64,
height: i64,
image: crate::render::RasterImage,
},
}
impl Halign {
@@ -1824,6 +1830,10 @@ fn document_nodes(
height: *height,
depth: *depth,
},
VerticalItem::Image { height, .. } => VerticalNode::Box {
height: *height,
depth: 0,
},
VerticalItem::Paragraph(paragraph) => VerticalNode::Box {
height: paragraph.height,
depth: if paragraph.height > paragraph.depth {
@@ -1883,6 +1893,7 @@ fn item_height(item: &VerticalItem) -> i64 {
VerticalItem::Halign(halign) => halign.height,
VerticalItem::Paragraph(paragraph) => paragraph.height,
VerticalItem::Math { height, depth, .. } => height + depth,
VerticalItem::Image { height, .. } => *height,
VerticalItem::Mark(_) | VerticalItem::Penalty(_) => 0,
}
}
@@ -2629,6 +2640,31 @@ impl Engine {
}
current.items.push(item.clone());
}
VerticalItem::Image { height, .. } => {
if top_float_exceeds_page_fraction(
&current,
document.page_goal,
document.top_fraction_percent,
) {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
if current.items.is_empty() && !page_has_top_float_material(&current) {
used = document.top_skip.max(*height);
} else if used + *height
> document.page_goal
- insertion_height
- bottom_float_area(&current, document.text_float_sep)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip.max(*height);
insertion_height = 0;
} else {
used += *height;
}
current.items.push(item.clone());
}
VerticalItem::Box(vertical_box) => {
if top_float_exceeds_page_fraction(
&current,
@@ -3685,6 +3721,7 @@ impl Engine {
VerticalItem::Halign(halign) => halign.height,
VerticalItem::Paragraph(paragraph) => paragraph.height,
VerticalItem::Math { height, depth, .. } => height + depth,
VerticalItem::Image { height, .. } => *height,
})
.sum();
// A paragraph is rebuilt line by line, so it may fill the remaining
@@ -3790,6 +3827,31 @@ impl Engine {
}
current.items.push(item.clone());
}
VerticalItem::Image { height, .. } => {
if top_float_exceeds_page_fraction(
&current,
next.page_goal,
next.top_fraction_percent,
) {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
}
if current.items.is_empty() && !page_has_top_float_material(&current) {
used = next.top_skip.max(*height);
} else if used + *height
> next.page_goal
- insertion_height
- bottom_float_area(&current, next.text_float_sep)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip.max(*height);
insertion_height = 0;
} else {
used += *height;
}
current.items.push(item.clone());
}
VerticalItem::Box(vertical_box) => {
if top_float_exceeds_page_fraction(
&current,
@@ -8800,6 +8862,45 @@ b & 2 \\
assert_eq!(hashes, fresh_hashes);
}
#[test]
fn image_items_paint_rasters() {
let image = crate::render::RasterImage {
width: 2,
height: 2,
rgb: vec![255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 0],
};
let document = Document::new(
1000,
vec![Unit::vertical(
1,
vec![VerticalItem::Image {
width: 50,
height: 40,
image,
}],
)],
);
let layout = Engine::layout_paged(&document).expect("images are page-buildable");
let display = layout.pages()[0].display_list();
assert_eq!(display.images.len(), 1);
assert!(display.items.iter().any(|item| matches!(
item,
crate::render::DisplayItem::Image {
width,
height,
..
} if *width == 50 && *height == 40
)));
let pdf = crate::pdf::display_lists_to_pdf_with_cm_type1(
&[display],
1000,
1000,
crate::pdf::PageOrigin::default(),
);
let text = String::from_utf8_lossy(&pdf);
assert!(text.contains("/Subtype /Image"));
}
#[test]
fn paged_layout_places_top_floats() {
let document = Document::new(
+27
View File
@@ -79,6 +79,14 @@ pub enum DisplayItem {
Special(String),
}
/// A raster image carried by a vertical item, with its pixel data.
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct RasterImage {
pub width: u32,
pub height: u32,
pub rgb: Vec<u8>,
}
/// A raw RGB image that can be painted as a PDF XObject.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PdfImage {
@@ -471,6 +479,25 @@ impl Page {
VerticalItem::Box(box_) => {
y += box_.height;
}
VerticalItem::Image {
width,
height,
image,
} => {
let index = list.add_image(PdfImage {
width: (*width).max(0) as u32,
height: (*height).max(0) as u32,
rgb: image.rgb.clone(),
});
list.push(DisplayItem::Image {
x: 0,
y,
width: *width,
height: *height,
index,
});
y += *height;
}
VerticalItem::Rule { width, height } => {
list.push(DisplayItem::Rule {
x: 0,