Skip to content

Commit

Permalink
More refactoring to new v2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
fschutt committed Oct 24, 2024
1 parent 58cd197 commit 6f9ddd4
Show file tree
Hide file tree
Showing 17 changed files with 3,152 additions and 58 deletions.
8 changes: 7 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 32 additions & 57 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,70 +9,57 @@ repository = "https://github.com/fschutt/printpdf"
homepage = "https://github.com/fschutt/printpdf"
license = "MIT"
readme = "README.md"
description = "Rust library for writing PDF files"
description = "Rust library for reading and writing PDF files"
keywords = ["pdf", "gui", "graphics", "wkhtmltopdf"]
categories = ["gui"]
exclude = ["./assets/*", "./doc/*", "./examples/*"]
autoexamples = false
edition = "2021"

[dependencies]
# minimum dependencies
lopdf = { version = "0.33.0", default-features = false, features = [
"pom_parser",
] }
owned_ttf_parser = { version = "0.24.0", default-features = false, features = [
"std",
] }
lopdf = { version = "0.33.0", default-features = false, features = ["pom_parser"] }
owned_ttf_parser = { version = "0.24.0", default-features = false, features = ["std"] }
time = { version = "0.3.25", default-features = false, features = ["std"] }
# optional: logging
log = { version = "0.4.8", optional = true }
# image reading (png / jpeg)
image = { version = "0.25", optional = true, default-features = false, features = [
"gif",
"jpeg",
"png",
"pnm",
"tiff",
"bmp",
] }
# svg support (svg -> pdf xobject)
allsorts = { version = "0.15", default-features = false, features = ["flate2_rust"] }
pdf-writer = { version = "0.10" }
# optional deps
image = { version = "0.25", optional = true, default-features = false, features = ["gif", "jpeg", "png", "pnm", "tiff", "bmp"] }
svg2pdf = { version = "0.11", optional = true }
pdf-writer = { version = "0.10", optional = true }
usvg = { version = "0.42", optional = true }
allsorts = { version = "0.15", optional = true, default-features = false, features = [
"flate2_rust",
] }
ttf-parser = "0.25.0"

[features]
default = ["js-sys"]
# do not compress PDF streams, useful for debugging
less-optimization = []
# enables logging
logging = ["log"]
# enables image support with some basic formats
embedded_images = ["image"]
# enables extra image formats
ico = ["image/ico", "embedded_images"]
tga = ["image/tga", "embedded_images"]
hdr = ["image/hdr", "embedded_images"]
rayon = ["image/rayon", "embedded_images"]
dds = ["image/dds", "embedded_images"]
webp = ["image/webp", "embedded_images"]
# enables svg
svg = ["svg2pdf", "usvg", "pdf-writer"]
font_subsetting = ["dep:allsorts"]
# enables annotations
annotations = ["pdf-writer"]
# enables js-sys features on wasm
js-sys = ["dep:js-sys"]
less-optimization = [] # do not compress PDF streams, useful for debugging
images = ["image"] # enables image support with some basic formats
ico = ["image/ico", "images"] # enables extra image format .ICO
tga = ["image/tga", "images"] # enables extra image format .TGA
hdr = ["image/hdr", "images"] # enables extra image format .HDR
dds = ["image/dds", "images"] # enables extra image format .DDS
webp = ["image/webp", "images"] # enables extra image format .WEBP
rayon = ["image/rayon", "images"] # enables multithreading for decoding images
svg = ["svg2pdf", "usvg"] # enables SVG embedding
js-sys = ["dep:js-sys"] # enables js-sys features on wasm
rendering = [] # enables experimental rendering module (renders PDF pages to image::DynamicImage)

[package.metadata.docs.rs]
all-features = true

[target.'cfg(all(target_arch="wasm32",target_os="unknown"))'.dependencies]
js-sys = { version = "0.3.40", optional = true }

[[example]]
name = "image"
required-features = ["images"]

[[example]]
name = "image_alpha"
required-features = ["images"]

[[example]]
name = "svg"
required-features = ["svg"]

[[example]]
name = "bookmark"
required-features = []
Expand All @@ -85,14 +72,6 @@ required-features = []
name = "font"
required-features = []

[[example]]
name = "image"
required-features = ["embedded_images"]

[[example]]
name = "image_alpha"
required-features = ["embedded_images"]

[[example]]
name = "no_icc"
required-features = []
Expand All @@ -105,17 +84,13 @@ required-features = []
name = "shape"
required-features = []

[[example]]
name = "svg"
required-features = ["svg"]

[[example]]
name = "annotations"
required-features = []

[[example]]
name = "hyperlink"
required-features = ["annotations"]
required-features = []

[[example]]
name = "rect"
Expand Down
117 changes: 117 additions & 0 deletions src/annotation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//! Bookmarks, page and link annotations
use crate::graphics::Rect;

#[derive(Debug, PartialEq, Clone)]
pub struct PageAnnotation {
/// Name of the bookmark annotation (i.e. "Chapter 5")
pub name: String,
/// Which page to jump to (i.e "page 10" = 10)
pub page: usize,
}

#[derive(Debug, PartialEq, Clone)]
pub struct LinkAnnotation {
pub rect: Rect,
pub border: BorderArray,
pub c: ColorArray,
pub a: Actions,
pub h: HighlightingMode,
}

impl LinkAnnotation {
/// Creates a new LinkAnnotation
pub fn new(
rect: Rect,
border: Option<BorderArray>,
c: Option<ColorArray>,
a: Actions,
h: Option<HighlightingMode>,
) -> Self {
Self {
rect,
border: border.unwrap_or_default(),
c: c.unwrap_or_default(),
a,
h: h.unwrap_or_default(),
}
}
}

#[derive(Debug, PartialEq, Clone)]
pub enum BorderArray {
Solid([f32; 3]),
Dashed([f32; 3], DashPhase),
}

impl Default for BorderArray {
fn default() -> Self {
BorderArray::Solid([0.0, 0.0, 1.0])
}
}

#[derive(Debug, PartialEq, Clone)]
pub struct DashPhase {
pub dash_array: Vec<f32>,
pub phase: f32,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ColorArray {
Transparent,
Gray([f32; 1]),
RGB([f32; 3]),
CMYK([f32; 4]),
}

impl Default for ColorArray {
fn default() -> Self {
ColorArray::RGB([0.0, 1.0, 1.0])
}
}

#[derive(Debug, PartialEq, Clone)]
#[non_exhaustive]
pub enum Destination {
/// Display `page` with coordinates `top` and `left` positioned at the upper-left corner of the
/// window and the contents of the page magnified by `zoom`.
///
/// A value of `None` for any parameter indicates to leave the current value unchanged, and a
/// `zoom` value of 0 has the same meaning as `None`.
XYZ {
page: usize,
left: Option<f32>,
top: Option<f32>,
zoom: Option<f32>,
},
}

#[derive(Debug, PartialEq, Clone)]
pub enum Actions {
GoTo(Destination),
URI(String),
}

impl Actions {
pub fn go_to(destination: Destination) -> Self {
Self::GoTo(destination)
}

pub fn uri(uri: String) -> Self {
Self::URI(uri)
}
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum HighlightingMode {
None,
Invert,
Outline,
Push,
}

impl Default for HighlightingMode {
fn default() -> Self {
HighlightingMode::Invert
}
}
Loading

0 comments on commit 6f9ddd4

Please sign in to comment.