Centre raster images inside floats

Add an x offset to VerticalItem::Image and use it when placing a decoded image
in a float, so a 0.4\textwidth plot is centred while its caption remains
centred across the float. Other image uses keep x=0.

Test count stays 1074.
This commit is contained in:
gloamxun committed 2026-10-27 23:37:54 +08:00
1 parent bbedf7676c
commit aa2f3cb0af
4 files changed
+12 -4

No files matched your search

+5
View File
@@ -207,6 +207,9 @@ pub enum VerticalItem {
}, },
/// A raster image painted into the vertical list. /// A raster image painted into the vertical list.
Image { Image {
/// Horizontal offset inside the float/list.
#[serde(default)]
x: i64,
width: i64, width: i64,
height: i64, height: i64,
image: crate::render::RasterImage, image: crate::render::RasterImage,
@@ -7924,6 +7927,7 @@ mod tests {
100, 100,
5, 5,
vec![VerticalItem::Image { vec![VerticalItem::Image {
x: 0,
width: 100, width: 100,
height: 30, height: 30,
image, image,
@@ -9679,6 +9683,7 @@ b & 2 \\
vec![Unit::vertical( vec![Unit::vertical(
1, 1,
vec![VerticalItem::Image { vec![VerticalItem::Image {
x: 0,
width: 50, width: 50,
height: 40, height: 40,
image, image,
+2 -1
View File
@@ -617,6 +617,7 @@ impl Page {
y += box_.height; y += box_.height;
} }
VerticalItem::Image { VerticalItem::Image {
x: image_x,
width, width,
height, height,
image, image,
@@ -627,7 +628,7 @@ impl Page {
rgb: image.rgb.clone(), rgb: image.rgb.clone(),
}); });
list.push(DisplayItem::Image { list.push(DisplayItem::Image {
x: 0, x: *image_x,
y, y,
width: *width, width: *width,
height: *height, height: *height,
+3 -3
View File
@@ -48,9 +48,9 @@ execution is not a goal.
parenthetical citations; inline `thebibliography` without labels remains parenthetical citations; inline `thebibliography` without labels remains
numeric, and the full natbib option surface is not implemented; numeric, and the full natbib option surface is not implemented;
- display math is approximate; - display math is approximate;
- PNG/JPEG/BMP floats now reserve their image height and embed the raster - PNG/JPEG/BMP floats now reserve their image height, embed the raster with
with its real pixel dimensions; the fbox frame and vector/PDF images are its real pixel dimensions, and centre the image within the float; the fbox
still missing, and float placement differs. frame and vector/PDF images are still missing, and float placement differs.
- Incremental foundation exists: - Incremental foundation exists:
- `Engine::edit` + page hashes; - `Engine::edit` + page hashes;
- project-scoped sessions and changed-page reporting; - project-scoped sessions and changed-page reporting;
+2
View File
@@ -13744,6 +13744,7 @@ fn build_document_with_labels(
image_height = (natural_height as f64 * scale).round() as i64; image_height = (natural_height as f64 * scale).round() as i64;
} }
content.push(VerticalItem::Image { content.push(VerticalItem::Image {
x: ((width - image_width) / 2).max(0),
width: image_width.max(1), width: image_width.max(1),
height: image_height.max(1), height: image_height.max(1),
image: image.clone(), image: image.clone(),
@@ -15482,6 +15483,7 @@ mod source_tests {
width, width,
height, height,
image, image,
..
} => Some((*width, *height, image.clone())), } => Some((*width, *height, image.clone())),
_ => None, _ => None,
}) })