Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions crates/pdf-annotation-core/src/source_decode_subtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::pdf_data::{
StampAnnotation, StrikeOutAnnotation, TextAnnotation, UnderlineAnnotation, WidgetAnnotation,
};
use crate::source_decode::{DecodeResult, color};
use pdf_graphics::{polyline::Polyline, rect::Rect};
use pdf_graphics::polyline::Polyline;
use pdf_object_reader::{
dictionary::Dictionary, object_lookup::ObjectLookupExt, object_resolver::ObjectResolver,
object_variant::ObjectVariant,
Expand Down Expand Up @@ -141,9 +141,7 @@ impl FreeTextAnnotation {
.get(b"CL")
.map(|value| callout_line(value, objects))
.transpose()?;
let difference_rect = dictionary
.optional_array_of::<f32, 4>(b"RD", objects)?
.map(Rect::from);
let difference_rect = dictionary.optional_rect(b"RD", objects)?;
let intent = dictionary.optional_bytes(b"IT", objects)?.map(Vec::from);
let border_effect = BorderEffect::from_dictionary(dictionary, b"BE", objects)?;

Expand Down Expand Up @@ -197,9 +195,7 @@ impl SquareAnnotation {
let border_style = BorderStyle::from_dictionary(dictionary, b"BS", objects)?;
let interior_color = color(dictionary, b"IC", objects)?;
let border_effect = BorderEffect::from_dictionary(dictionary, b"BE", objects)?;
let difference_rect = dictionary
.optional_array_of::<f32, 4>(b"RD", objects)?
.map(Rect::from);
let difference_rect = dictionary.optional_rect(b"RD", objects)?;

Ok(Self {
border_style,
Expand All @@ -218,9 +214,7 @@ impl CircleAnnotation {
let border_style = BorderStyle::from_dictionary(dictionary, b"BS", objects)?;
let interior_color = color(dictionary, b"IC", objects)?;
let border_effect = BorderEffect::from_dictionary(dictionary, b"BE", objects)?;
let difference_rect = dictionary
.optional_array_of::<f32, 4>(b"RD", objects)?
.map(Rect::from);
let difference_rect = dictionary.optional_rect(b"RD", objects)?;

Ok(Self {
border_style,
Expand Down Expand Up @@ -362,9 +356,7 @@ impl CaretAnnotation {
dictionary: &Dictionary,
objects: &dyn ObjectResolver,
) -> DecodeResult<Self> {
let difference_rect = dictionary
.optional_array_of::<f32, 4>(b"RD", objects)?
.map(Rect::from);
let difference_rect = dictionary.optional_rect(b"RD", objects)?;
let style = dictionary
.get(b"Sy")
.map(|value| value.try_bytes(objects).map(CaretSymbolStyle::from))
Expand Down
3 changes: 1 addition & 2 deletions crates/pdf-document/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ impl FromPdfObject for PdfDocument {
let media_box = context.dictionary().optional_media_box(context.source())?;
let crop_box = context
.dictionary()
.optional_array_of::<f32, 4>(b"CropBox", context.source())?
.map(Rect::from);
.optional_rect(b"CropBox", context.source())?;
let rotation = context
.dictionary()
.optional_number::<i32>(b"Rotate", context.source())?;
Expand Down
4 changes: 1 addition & 3 deletions crates/pdf-document/src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ impl FromPdfObject for PdfPage {
context.optional::<ContentStream>(b"Contents")?
};
let media_box = dictionary.optional_media_box(context.source())?;
let crop_box = dictionary
.optional_array_of::<f32, 4>(b"CropBox", context.source())?
.map(Rect::from);
let crop_box = dictionary.optional_rect(b"CropBox", context.source())?;
let rotation = dictionary.optional_number::<i32>(b"Rotate", context.source())?;
let resources = context
.optional_shared::<Resources>(b"Resources")?
Expand Down
38 changes: 31 additions & 7 deletions crates/pdf-object-reader/src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,48 @@ impl Dictionary {
})
}

/// Reads the optional rectangle entry with the given key.
///
/// Missing entries and explicit PDF `null` values are treated as absent.
/// The coordinates are returned in their original order.
pub fn optional_rect(
&self,
key: &[u8],
objects: &dyn ObjectResolver,
) -> Result<Option<Rect>, ObjectError> {
Ok(self
.optional_array_of::<f32, 4>(key, objects)?
.map(Rect::from))
}

/// Reads the required rectangle entry with the given key.
///
/// The coordinates are returned in their original order.
pub fn required_rect(
&self,
key: &[u8],
objects: &dyn ObjectResolver,
) -> Result<Rect, ObjectError> {
self.required_array_of::<f32, 4>(key, objects)
.map(Rect::from)
}

/// Reads the optional `/BBox` entry as a rectangle.
///
/// Missing entries and explicit PDF `null` values are treated as absent.
/// The rectangle is normalized so that `left <= right` and `top <= bottom`.
pub fn optional_bbox(&self, objects: &dyn ObjectResolver) -> Result<Option<Rect>, ObjectError> {
Ok(self
.optional_array_of::<f32, 4>(b"BBox", objects)?
.map(|bbox| Rect::from(bbox).normalized()))
.optional_rect(b"BBox", objects)?
.map(|bbox| bbox.normalized()))
}

/// Reads the required `/BBox` entry as a rectangle.
///
/// The rectangle is normalized so that `left <= right` and `top <= bottom`.
pub fn required_bbox(&self, objects: &dyn ObjectResolver) -> Result<Rect, ObjectError> {
self.required_array_of::<f32, 4>(b"BBox", objects)
.map(|bbox| Rect::from(bbox).normalized())
self.required_rect(b"BBox", objects)
.map(|bbox| bbox.normalized())
}

/// Reads optional `/Width` and `/Height` entries as an origin-based integer rectangle.
Expand Down Expand Up @@ -175,9 +201,7 @@ impl Dictionary {
&self,
objects: &dyn ObjectResolver,
) -> Result<Option<Rect>, ObjectError> {
Ok(self
.optional_array_of::<f32, 4>(b"MediaBox", objects)?
.map(Rect::from))
self.optional_rect(b"MediaBox", objects)
}

/// Reads the optional `/Matrix` entry as an affine transform.
Expand Down
Loading