From aa2f3cb0afaa60ad9d0f00a6b905770d9943b1bc Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 27 Oct 2026 23:37:54 +0800 Subject: [PATCH] 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. --- crates/texrra-tex-core/src/lib.rs | 5 +++++ crates/texrra-tex-core/src/render.rs | 3 ++- docs/TEX_FIDELITY_ROADMAP.md | 6 +++--- src/route_d.rs | 2 ++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index f03c062..2352e9d 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -207,6 +207,9 @@ pub enum VerticalItem { }, /// A raster image painted into the vertical list. Image { + /// Horizontal offset inside the float/list. + #[serde(default)] + x: i64, width: i64, height: i64, image: crate::render::RasterImage, @@ -7924,6 +7927,7 @@ mod tests { 100, 5, vec![VerticalItem::Image { + x: 0, width: 100, height: 30, image, @@ -9679,6 +9683,7 @@ b & 2 \\ vec![Unit::vertical( 1, vec![VerticalItem::Image { + x: 0, width: 50, height: 40, image, diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index 338186e..d8de2d9 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -617,6 +617,7 @@ impl Page { y += box_.height; } VerticalItem::Image { + x: image_x, width, height, image, @@ -627,7 +628,7 @@ impl Page { rgb: image.rgb.clone(), }); list.push(DisplayItem::Image { - x: 0, + x: *image_x, y, width: *width, height: *height, diff --git a/docs/TEX_FIDELITY_ROADMAP.md b/docs/TEX_FIDELITY_ROADMAP.md index c3e49bc..ae987be 100644 --- a/docs/TEX_FIDELITY_ROADMAP.md +++ b/docs/TEX_FIDELITY_ROADMAP.md @@ -48,9 +48,9 @@ execution is not a goal. parenthetical citations; inline `thebibliography` without labels remains numeric, and the full natbib option surface is not implemented; - display math is approximate; - - PNG/JPEG/BMP floats now reserve their image height and embed the raster - with its real pixel dimensions; the fbox frame and vector/PDF images are - still missing, and float placement differs. + - PNG/JPEG/BMP floats now reserve their image height, embed the raster with + its real pixel dimensions, and centre the image within the float; the fbox + frame and vector/PDF images are still missing, and float placement differs. - Incremental foundation exists: - `Engine::edit` + page hashes; - project-scoped sessions and changed-page reporting; diff --git a/src/route_d.rs b/src/route_d.rs index d96f913..94699d4 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -13744,6 +13744,7 @@ fn build_document_with_labels( image_height = (natural_height as f64 * scale).round() as i64; } content.push(VerticalItem::Image { + x: ((width - image_width) / 2).max(0), width: image_width.max(1), height: image_height.max(1), image: image.clone(), @@ -15482,6 +15483,7 @@ mod source_tests { width, height, image, + .. } => Some((*width, *height, image.clone())), _ => None, })