Embed project images in route-D previews and PDFs
This commit is contained in:
1 parent
ae69eba64d
commit
0f8aff7290
8 files changed
+407
-18
No files matched your search
@@ -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(
|
||||
¤t,
|
||||
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(¤t) {
|
||||
used = document.top_skip.max(*height);
|
||||
} else if used + *height
|
||||
> document.page_goal
|
||||
- insertion_height
|
||||
- bottom_float_area(¤t, 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(
|
||||
¤t,
|
||||
@@ -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(
|
||||
¤t,
|
||||
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(¤t) {
|
||||
used = next.top_skip.max(*height);
|
||||
} else if used + *height
|
||||
> next.page_goal
|
||||
- insertion_height
|
||||
- bottom_float_area(¤t, 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(
|
||||
¤t,
|
||||
@@ -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(
|
||||
|
||||
Reference in new issue
Block a user