PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
refactor(engine): move the page filters to page_filter.rs
alehel8ca4bf641f05
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex 25428ebf6..825494f26 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -38,6 +38,7 @@ pub mod links;pub mod ocr;mod open;pub mod optimizer;+mod page_filter;mod pictures;pub mod print_raster;pub mod printcheck;@@ -162,6 +163,7 @@ pub use fill_sign::{displayed_offset, text_box_at, typed_signature_graphic, typepub use history::Label;use layers::{apply_layer_state, set_default_layer_state, use_layer_choices};pub use open::{OpenContext, PreparedDocument};+pub use page_filter::{PageOrientation, PageParity, filter_pages};pub use pictures::MarkFile;use pictures::{embed_image, fit_picture, mark_source};use protection::unrestricted;@@ -269,43 +271,3 @@ pub struct OptimizeReport {#[cfg(test)]mod tests;--/// Page filters of Acrobat's Rotate Pages and page selection: even/odd page numbers and-/// orientation (as displayed).-#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]-pub enum PageParity {- #[default]- Both,- Even,- Odd,-}--#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]-pub enum PageOrientation {- #[default]- Both,- Landscape,- Portrait,-}--/// The pages of `pages` (0-based) that pass the filters.-pub fn filter_pages(info: &pdfcraft_render::DocInfo, pages: &[usize], parity: PageParity, orientation: PageOrientation) -> Vec<usize> {- pages- .iter()- .copied()- .filter(|p| match parity {- PageParity::Both => true,- // Page numbers are 1-based: index 1 is page 2, an even page.- PageParity::Even => p % 2 == 1,- PageParity::Odd => p % 2 == 0,- })- .filter(|p| {- let Some(pg) = info.pages.get(*p) else { return false };- match orientation {- PageOrientation::Both => true,- PageOrientation::Landscape => pg.width > pg.height,- PageOrientation::Portrait => pg.width <= pg.height,- }- })- .collect()-}diff --git a/crates/engine/src/page_filter.rs b/crates/engine/src/page_filter.rsnew file mode 100644index 000000000..aaedfbd65--- /dev/null+++ b/crates/engine/src/page_filter.rs@@ -0,0 +1,41 @@+//! Page filters: even or odd page numbers, and orientation as displayed.++/// Page filters of Acrobat's Rotate Pages and page selection: even/odd page numbers and+/// orientation (as displayed).+#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]+pub enum PageParity {+ #[default]+ Both,+ Even,+ Odd,+}++#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]+pub enum PageOrientation {+ #[default]+ Both,+ Landscape,+ Portrait,+}++/// The pages of `pages` (0-based) that pass the filters.+pub fn filter_pages(info: &pdfcraft_render::DocInfo, pages: &[usize], parity: PageParity, orientation: PageOrientation) -> Vec<usize> {+ pages+ .iter()+ .copied()+ .filter(|p| match parity {+ PageParity::Both => true,+ // Page numbers are 1-based: index 1 is page 2, an even page.+ PageParity::Even => p % 2 == 1,+ PageParity::Odd => p % 2 == 0,+ })+ .filter(|p| {+ let Some(pg) = info.pages.get(*p) else { return false };+ match orientation {+ PageOrientation::Both => true,+ PageOrientation::Landscape => pg.width > pg.height,+ PageOrientation::Portrait => pg.width <= pg.height,+ }+ })+ .collect()+}