From 43b14172105adc4c3ef144e86a572894f9cbe656 Mon Sep 17 00:00:00 2001 From: Velli20 Date: Sat, 31 Oct 2026 20:11:34 +0200 Subject: [PATCH] Add Rect origin and size accessors Replace hand-built top-left points and sizes in annotation projection with Rect::origin() and Rect::size(). Co-Authored-By: Claude Opus 5.5 --- crates/pdf-annotation-core/src/projection.rs | 6 +++--- crates/pdf-annotation-core/src/source_kind.rs | 2 +- crates/pdf-annotation-core/src/source_properties.rs | 3 +-- crates/pdf-graphics/src/rect.rs | 12 +++++++++++- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/crates/pdf-annotation-core/src/projection.rs b/crates/pdf-annotation-core/src/projection.rs index 66414941..4d08e063 100644 --- a/crates/pdf-annotation-core/src/projection.rs +++ b/crates/pdf-annotation-core/src/projection.rs @@ -10,7 +10,7 @@ use crate::{ pdf_data::AnnotationAction, shapes, widget_control, }; -use pdf_graphics::{point::Point, rect::Rect, size::Size, viewport::PageViewport}; +use pdf_graphics::{point::Point, rect::Rect, viewport::PageViewport}; use pdf_optional_content::OptionalContentState; /// The shared field a widget is bound to; `None` for other kinds. @@ -90,8 +90,8 @@ pub(crate) fn project( subtype: annotation.content.subtype_name().into_owned(), unsupported: unsupported_reason(annotation, field), bounds: device_regions(annotation, &bounds, viewport)?, - origin: Point::new(bounds.left, bounds.top), - size: Size::new(bounds.width(), bounds.height()), + origin: bounds.origin(), + size: bounds.size(), transform: viewport.local_frame(Point::new(bounds.left, bounds.bottom))?, no_zoom: annotation.metadata.no_zoom(), no_rotate: annotation.metadata.no_rotate(), diff --git a/crates/pdf-annotation-core/src/source_kind.rs b/crates/pdf-annotation-core/src/source_kind.rs index f9ea375a..2d0b5f08 100644 --- a/crates/pdf-annotation-core/src/source_kind.rs +++ b/crates/pdf-annotation-core/src/source_kind.rs @@ -104,7 +104,7 @@ fn text_note( Ok(TextNote { properties: Some(Box::new(v.clone())), style: Some(style.clone()), - anchor: Point::new(rect.left, rect.top), + anchor: rect.origin(), text: source.contents()?, color: style.rgba_color()?, }) diff --git a/crates/pdf-annotation-core/src/source_properties.rs b/crates/pdf-annotation-core/src/source_properties.rs index 90b17fea..d685c2cb 100644 --- a/crates/pdf-annotation-core/src/source_properties.rs +++ b/crates/pdf-annotation-core/src/source_properties.rs @@ -2,7 +2,6 @@ use crate::error::ValidationError; use crate::fields::TextMode; use crate::kind::SourceLayout; -use crate::models::Point; use crate::pdf_data::*; use pdf_graphics::rect::Rect; @@ -94,7 +93,7 @@ impl SourceAnnotation { .copied(), _ => None, }; - let anchor = first_ink_point.unwrap_or_else(|| Point::new(rect.left, rect.top)); + let anchor = first_ink_point.unwrap_or_else(|| rect.origin()); Some(SourceLayout { rect, anchor }) } diff --git a/crates/pdf-graphics/src/rect.rs b/crates/pdf-graphics/src/rect.rs index eec600c5..96b79971 100644 --- a/crates/pdf-graphics/src/rect.rs +++ b/crates/pdf-graphics/src/rect.rs @@ -1,4 +1,4 @@ -use crate::{point::Point, transform::Transform}; +use crate::{point::Point, size::Size, transform::Transform}; /// An axis-aligned rectangle whose edge coordinate type defaults to `f32`. #[derive(Debug, Default, Clone, Copy, PartialEq)] @@ -74,6 +74,16 @@ impl Rect { self.bottom - self.top } + /// Returns the top-left corner `(left, top)`. + pub fn origin(&self) -> Point { + Point::new(self.left, self.top) + } + + /// Returns the width and height as a [`Size`]. + pub fn size(&self) -> Size { + Size::new(self.width(), self.height()) + } + /// Returns whether this rectangle has finite edges and finite, positive dimensions. pub fn is_valid(&self) -> bool { [