Skip to content

Commit

Permalink
Resolves #141
Browse files Browse the repository at this point in the history
  • Loading branch information
Alastair Carey committed Jun 16, 2024
1 parent 4b2441c commit 52be6e8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,12 @@ pub trait PdfiumLibraryBindings {
fn FPDF_GetPageBoundingBox(&self, page: FPDF_PAGE, rect: *mut FS_RECTF) -> FPDF_BOOL;

#[allow(non_snake_case)]
fn FPDF_GetPageSizeByIndexF(&self, document: FPDF_DOCUMENT, page_index: c_int, size: *mut FS_SIZEF) -> FPDF_BOOL;
fn FPDF_GetPageSizeByIndexF(
&self,
document: FPDF_DOCUMENT,
page_index: c_int,
size: *mut FS_SIZEF,
) -> FPDF_BOOL;

#[allow(non_snake_case)]
fn FPDFPage_GetMediaBox(
Expand Down Expand Up @@ -648,6 +653,7 @@ pub trait PdfiumLibraryBindings {
fn FPDFPage_GenerateContent(&self, page: FPDF_PAGE) -> FPDF_BOOL;

#[allow(non_snake_case)]
#[allow(clippy::too_many_arguments)]
fn FPDFPage_TransformAnnots(
&self,
page: FPDF_PAGE,
Expand Down
1 change: 1 addition & 0 deletions src/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub type Pixels = i32;

/// The pixel format of the rendered image data in the backing buffer of a [PdfBitmap].
#[derive(Copy, Clone, Debug, PartialEq)]
#[allow(clippy::manual_non_exhaustive)] // triggered by deprecation below, can be removed in 0.9.0
pub enum PdfBitmapFormat {
Gray = FPDFBitmap_Gray as isize,
BGR = FPDFBitmap_BGR as isize,
Expand Down
2 changes: 1 addition & 1 deletion src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,7 @@ impl DynamicPdfiumBindings {
extern_FPDFAttachment_SetFile: *(library.get(b"FPDFAttachment_SetFile\0")?),
extern_FPDFAttachment_GetFile: *(library.get(b"FPDFAttachment_GetFile\0")?),
extern_FPDFCatalog_IsTagged: *(library.get(b"FPDFCatalog_IsTagged\0")?),
library: library,
library,
}
};

Expand Down
14 changes: 14 additions & 0 deletions src/page_size.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Defines the [PdfPagePaperSize] enum, a set of common ANSI and ISO paper sizes.

use crate::points::PdfPoints;
use crate::rect::PdfRect;

/// A standardized paper size.
#[derive(Debug, Copy, Clone, PartialEq)]
Expand Down Expand Up @@ -490,6 +491,7 @@ impl PdfPagePaperSize {
}

/// Returns the width of this [PdfPagePaperSize].
#[inline]
pub fn width(&self) -> PdfPoints {
match self {
PdfPagePaperSize::Portrait(size) => size.width(),
Expand All @@ -499,11 +501,23 @@ impl PdfPagePaperSize {
}

/// Returns the height of this [PdfPagePaperSize].
#[inline]
pub fn height(&self) -> PdfPoints {
match self {
PdfPagePaperSize::Portrait(size) => size.height(),
PdfPagePaperSize::Landscape(size) => size.width(),
PdfPagePaperSize::Custom(_, height) => *height,
}
}

/// Returns the dimensions of this [PdfPagePaperSize] as a [PdfRect].
#[inline]
pub fn as_rect(&self) -> PdfRect {
PdfRect::new(
PdfPoints::ZERO,
PdfPoints::ZERO,
self.height(),
self.width(),
)
}
}
50 changes: 47 additions & 3 deletions src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,13 +665,57 @@ mod tests {

let pdfium = test_bind_to_pdfium();

let document = pdfium.load_pdf_from_file("./test/export-test.pdf", None)?;
let document = pdfium.load_pdf_from_file("./test/page-sizes-test.pdf", None)?;

assert_eq!(document.pages().page_size(0)?, expected_page_0_size());
assert_eq!(document.pages().page_size(1)?, expected_page_1_size());
assert_eq!(document.pages().page_size(2)?, expected_page_2_size());
assert_eq!(document.pages().page_size(3)?, expected_page_3_size());
assert_eq!(document.pages().page_size(4)?, expected_page_4_size());
assert!(document.pages().page_size(5).is_err());

Ok(())
}

#[test]
fn test_page_sizes() -> Result<(), PdfiumError> {
// Tests the dimensions of all pages in a sample file.

let pdfium = test_bind_to_pdfium();

let document = pdfium.load_pdf_from_file("./test/page-sizes-test.pdf", None)?;

assert_eq!(
document.pages().page_size(0).unwrap(),
PdfRect::new_from_values(0.0, 0.0, 841.8897, 595.3039),
document.pages().page_sizes()?,
vec!(
expected_page_0_size(),
expected_page_1_size(),
expected_page_2_size(),
expected_page_3_size(),
expected_page_4_size(),
),
);

Ok(())
}

const fn expected_page_0_size() -> PdfRect {
PdfRect::new_from_values(0.0, 0.0, 841.8897, 595.3039)
}

const fn expected_page_1_size() -> PdfRect {
PdfRect::new_from_values(0.0, 0.0, 595.3039, 841.8897)
}

const fn expected_page_2_size() -> PdfRect {
PdfRect::new_from_values(0.0, 0.0, 1190.5513, 841.8897)
}

const fn expected_page_3_size() -> PdfRect {
PdfRect::new_from_values(0.0, 0.0, 419.55588, 595.3039)
}

const fn expected_page_4_size() -> PdfRect {
expected_page_0_size()
}
}
Binary file added test/page-sizes-test.pdf
Binary file not shown.

0 comments on commit 52be6e8

Please sign in to comment.