Skip to content

Commit

Permalink
Merge pull request #179 from wyatt-herkamp/depend-updates
Browse files Browse the repository at this point in the history
Update Dependencies
  • Loading branch information
fschutt authored Aug 5, 2024
2 parents fbc7d69 + 7936425 commit 8e3b072
Show file tree
Hide file tree
Showing 15 changed files with 529 additions and 399 deletions.
703 changes: 416 additions & 287 deletions Cargo.lock

Large diffs are not rendered by default.

26 changes: 16 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[package]
name = "printpdf"
version = "0.7.0"
authors = ["Felix Schütt <felix.schuett@maps4print.com>", "Julien Schminke <julien.schminke@web.de"]
authors = [
"Felix Schütt <felix.schuett@maps4print.com>",
"Julien Schminke <julien.schminke@web.de",
]
repository = "https://github.com/fschutt/printpdf"
homepage = "https://github.com/fschutt/printpdf"
license = "MIT"
Expand All @@ -15,15 +18,17 @@ edition = "2021"

[dependencies]
# minimum dependencies
lopdf = { version = "0.31.0", default-features = false, features = [
lopdf = { version = "0.33.0", default-features = false, features = [
"pom_parser",
] }
owned_ttf_parser = { version = "0.19.0", default-features = false }
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.24.3", optional = true, default-features = false, features = [
image = { version = "0.25", optional = true, default-features = false, features = [
"gif",
"jpeg",
"png",
Expand All @@ -32,10 +37,12 @@ image = { version = "0.24.3", optional = true, default-features = false, feature
"bmp",
] }
# svg support (svg -> pdf xobject)
svg2pdf = { version = "0.8", optional = true }
pdf-writer = { version = "0.9", optional = true }
usvg = { version = "0.35.0", optional = true }
allsorts = { version = "0.14", optional = true, default-features = false, features = ["flate2_rust"] }
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",
] }

[features]
default = ["js-sys"]
Expand All @@ -49,8 +56,7 @@ embedded_images = ["image"]
ico = ["image/ico", "embedded_images"]
tga = ["image/tga", "embedded_images"]
hdr = ["image/hdr", "embedded_images"]
jpeg_rayon = ["image/jpeg_rayon", "embedded_images"]
dxt = ["image/dxt", "embedded_images"]
rayon = ["image/rayon", "embedded_images"]
dds = ["image/dds", "embedded_images"]
webp = ["image/webp", "embedded_images"]
# enables svg
Expand Down
31 changes: 17 additions & 14 deletions examples/annotations.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
//! Uses the extend_with feature to add a link to the page.
extern crate printpdf;

use lopdf::{Dictionary, Object, StringFormat::Literal};
use printpdf::*;
use std::fs::File;
use std::io::BufWriter;
use lopdf::{
StringFormat::Literal,
Dictionary,
Object
};

fn main() {
let (doc, page1, layer1) =
Expand All @@ -17,30 +13,37 @@ fn main() {
let current_layer = page.get_layer(layer1);

let action = Dictionary::from_iter(vec![
("Type", "Action".into()),
("S", Object::Name(b"URI".to_vec())),
("URI", Object::String(b"https://github.com/fschutt/printpdf".to_vec(), Literal)),
("Type", "Action".into()),
("S", Object::Name(b"URI".to_vec())),
(
"URI",
Object::String(b"https://github.com/fschutt/printpdf".to_vec(), Literal),
),
]);

let annotation = Dictionary::from_iter(vec![
("Type", "Annot".into()),
("Subtype", Object::Name(b"Link".to_vec())),
("Rect", vec![20.into(), 580.into(), 300.into(), 560.into()].into()),
(
"Rect",
vec![20.into(), 580.into(), 300.into(), 560.into()].into(),
),
("C", vec![].into()),
("Contents", Object::String("Hello World".into(), Literal)),
("A", action.into()),
]);

let annotations = Dictionary::from_iter(vec![
("Annots", Object::Array(vec![annotation.into()]))
]);
let annotations =
Dictionary::from_iter(vec![("Annots", Object::Array(vec![annotation.into()]))]);

page.extend_with(annotations);

let text = "There's an invisible annotation with a link covering this text.";
let font = doc.add_builtin_font(BuiltinFont::Helvetica).unwrap();
current_layer.use_text(text, 10.0, Mm(10.0), Mm(200.0), &font);

doc.save(&mut BufWriter::new(File::create("test_annotations.pdf").unwrap()))
.unwrap();
doc.save(&mut BufWriter::new(
File::create("test_annotations.pdf").unwrap(),
))
.unwrap();
}
9 changes: 1 addition & 8 deletions src/ctm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,7 @@ fn test_ctm_translate() {
let ctm_rot = CurTransMat::Rotate(30.0);
let ctm_rot_arr: [f32; 6] = ctm_rot.into();
assert_eq!(
[
0.8660253,
0.5000002,
-0.5000002,
0.8660253,
0.0,
0.0
],
[0.8660253, 0.5000002, -0.5000002, 0.8660253, 0.0, 0.0],
ctm_rot_arr
);
}
6 changes: 4 additions & 2 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,8 +757,10 @@ impl FontData for TtfFace {
fn glyph_ids(&self) -> HashMap<u16, char> {
let subtables = self
.face()
.tables().cmap.map(|cmap| cmap.subtables.into_iter().filter(|v| v.is_unicode()));
let Some(subtables) = subtables else{
.tables()
.cmap
.map(|cmap| cmap.subtables.into_iter().filter(|v| v.is_unicode()));
let Some(subtables) = subtables else {
return HashMap::new();
};
let mut map = HashMap::with_capacity(self.face().number_of_glyphs().into());
Expand Down
7 changes: 2 additions & 5 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ pub struct Image {

impl From<ImageXObject> for Image {
fn from(image: ImageXObject) -> Self {
Self {
image,
smask: None,
}
Self { image, smask: None }
}
}

#[cfg(feature = "embedded_images")]
impl<'a> Image {
pub fn try_from<T: ImageDecoder<'a>>(image: T) -> Result<Self, image_crate::ImageError> {
pub fn try_from<T: ImageDecoder>(image: T) -> Result<Self, image_crate::ImageError> {
let (image, smask) = ImageXObject::try_from(image)?;
Ok(Self { image, smask })
}
Expand Down
1 change: 0 additions & 1 deletion src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,3 @@ impl Polygon {
operations
}
}

44 changes: 25 additions & 19 deletions src/link_annotation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use lopdf::{self, Object};
use std::collections::HashMap;
use crate::Rect;
use lopdf::{self, Dictionary, Object};
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub struct LinkAnnotation {
Expand Down Expand Up @@ -32,12 +32,12 @@ impl LinkAnnotation {

impl Into<Object> for LinkAnnotation {
fn into(self) -> Object {
let mut dict = lopdf::Dictionary::new();
dict.set("Type", lopdf::Object::Name("Annot".as_bytes().to_vec()));
dict.set("Subtype", lopdf::Object::Name("Link".as_bytes().to_vec()));
let mut dict = Dictionary::new();
dict.set("Type", Object::Name("Annot".as_bytes().to_vec()));
dict.set("Subtype", Object::Name("Link".as_bytes().to_vec()));
dict.set(
"Rect",
lopdf::Object::Array(vec![
Object::Array(vec![
self.rect.ll.x.into(),
self.rect.ll.y.into(),
self.rect.ur.x.into(),
Expand Down Expand Up @@ -93,7 +93,12 @@ pub struct DashPhase {
impl Into<Object> for DashPhase {
fn into(self) -> Object {
Object::Array(vec![
Object::Array(self.dash_array.into_iter().map(|x| Object::Real(x.into())).collect()),
Object::Array(
self.dash_array
.into_iter()
.map(|x| Object::Real(x.into()))
.collect(),
),
Object::Real(self.phase.into()),
])
}
Expand All @@ -117,9 +122,7 @@ impl Into<Object> for ColorArray {
fn into(self) -> Object {
match self {
ColorArray::Transparent => Object::Array(vec![]),
ColorArray::Gray(arr) => Object::Array(vec![
Object::Real(arr[0].into()),
]),
ColorArray::Gray(arr) => Object::Array(vec![Object::Real(arr[0].into())]),
ColorArray::RGB(arr) => Object::Array(vec![
Object::Real(arr[0].into()),
Object::Real(arr[1].into()),
Expand Down Expand Up @@ -152,9 +155,12 @@ impl Actions {

impl Into<Object> for Actions {
fn into(self) -> Object {
let mut dict = lopdf::Dictionary::new();
let mut dict = Dictionary::new();
dict.set("S", Object::Name(self.s.into_bytes().to_vec()));
dict.set("URI", Object::String(self.uri.into_bytes().to_vec(), lopdf::StringFormat::Literal));
dict.set(
"URI",
Object::String(self.uri.into_bytes().to_vec(), lopdf::StringFormat::Literal),
);
Object::Dictionary(dict)
}
}
Expand Down Expand Up @@ -230,18 +236,18 @@ impl LinkAnnotationList {
}
}

impl From<LinkAnnotationList> for lopdf::Dictionary {
impl From<LinkAnnotationList> for Dictionary {
fn from(_val: LinkAnnotationList) -> Self {
if _val.link_annotations.is_empty() {
return lopdf::Dictionary::new();
return Dictionary::new();
}
let mut dict = lopdf::Dictionary::new();
dict.set("Type", lopdf::Object::Name("Annot".as_bytes().to_vec()));
dict.set("Subtype", lopdf::Object::Name("Link".as_bytes().to_vec()));

let mut dict = Dictionary::new();
dict.set("Type", Object::Name("Annot".as_bytes().to_vec()));
dict.set("Subtype", Object::Name("Link".as_bytes().to_vec()));
dict.set(
"Rect",
lopdf::Object::Array(vec![
Object::Array(vec![
_val.link_annotations["PT0"].rect.ll.x.into(),
_val.link_annotations["PT0"].rect.ll.y.into(),
_val.link_annotations["PT0"].rect.ur.x.into(),
Expand Down
6 changes: 3 additions & 3 deletions src/pdf_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,9 @@ impl PdfDocumentReference {
]);

if let Some(extension) = &page.extend_with {
for (key, value) in extension.iter() {
p.set(key.to_vec(), value.clone())
}
for (key, value) in extension.iter() {
p.set(key.to_vec(), value.clone())
}
}

// this will collect the resources needed for rendering this page
Expand Down
10 changes: 5 additions & 5 deletions src/pdf_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl PdfLayerReference {
// add gs operator to stream
page_mut.layers[self.layer.0]
.operations
.push(lopdf::content::Operation::new(
.push(Operation::new(
"gs",
vec![lopdf::Object::Name(new_ref.gs_name.as_bytes().to_vec())],
));
Expand All @@ -212,7 +212,7 @@ impl PdfLayerReference {
let new_ref = page_mut.add_graphics_state(new_overprint_state);
page_mut.layers[self.layer.0]
.operations
.push(lopdf::content::Operation::new(
.push(Operation::new(
"gs",
vec![lopdf::Object::Name(new_ref.gs_name.as_bytes().to_vec())],
));
Expand All @@ -234,7 +234,7 @@ impl PdfLayerReference {

page_mut.layers[self.layer.0]
.operations
.push(lopdf::content::Operation::new(
.push(Operation::new(
"gs",
vec![lopdf::Object::Name(new_ref.gs_name.as_bytes().to_vec())],
));
Expand Down Expand Up @@ -343,7 +343,7 @@ impl PdfLayerReference {
self.add_operation(Operation::new("Tz", vec![lopdf::Object::Real(scaling)]));
}

/// Offsets the current text positon (used for superscript
/// Offsets the current text position (used for superscript
/// and subscript). To reset the superscript / subscript, call this
/// function with 0 as the offset. For superscript, use a positive
/// number, for subscript, use a negative number. This does not
Expand Down Expand Up @@ -543,7 +543,7 @@ impl PdfLayerReference {

page_mut.layers[self.layer.0]
.operations
.push(lopdf::content::Operation::new(
.push(Operation::new(
"Do",
vec![lopdf::Object::Name(name.as_bytes().to_vec())],
));
Expand Down
9 changes: 4 additions & 5 deletions src/pdf_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use std::rc::Weak;

use crate::indices::{PdfLayerIndex, PdfPageIndex};
use crate::{
ExtendedGraphicsState, ExtendedGraphicsStateRef, Mm, Pattern, PatternRef, PdfDocument,
PdfLayer, PdfLayerReference, PdfResources, Pt, XObject, XObjectRef, LinkAnnotation, LinkAnnotationRef,
ExtendedGraphicsState, ExtendedGraphicsStateRef, LinkAnnotation, LinkAnnotationRef, Mm,
Pattern, PatternRef, PdfDocument, PdfLayer, PdfLayerReference, PdfResources, Pt, XObject,
XObjectRef,
};

/// PDF page
Expand Down Expand Up @@ -52,7 +53,7 @@ impl PdfPage {
height: height.into(),
layers: Vec::new(),
resources: PdfResources::new(),
extend_with: None
extend_with: None,
};

let initial_layer = PdfLayer::new(layer_name);
Expand Down Expand Up @@ -152,8 +153,6 @@ impl PdfPage {
pub fn add_link_annotation(&mut self, annotation: LinkAnnotation) -> LinkAnnotationRef {
self.resources.add_link_annotation(annotation)
}


}

impl PdfPageReference {
Expand Down
11 changes: 7 additions & 4 deletions src/scale.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
//! Scaling types for reducing errors between conversions between point (pt) and millimeter (mm)
use std::num::FpCategory;
use std::cmp::Ordering;
use std::num::FpCategory;

macro_rules! impl_partialeq {
($t:ty) => {
impl PartialEq for $t {
// custom compare function because of floating point inaccuracy
fn eq(&self, other: &$t) -> bool {
if (self.0.classify() == FpCategory::Zero || self.0.classify() == FpCategory::Normal) && (other.0.classify() == FpCategory::Zero || other.0.classify() == FpCategory::Normal) {
if (self.0.classify() == FpCategory::Zero
|| self.0.classify() == FpCategory::Normal)
&& (other.0.classify() == FpCategory::Zero
|| other.0.classify() == FpCategory::Normal)
{
// four floating point numbers have to match
(self.0 * 1000.0).round() == (other.0 * 1000.0).round()
} else {
Expand Down Expand Up @@ -251,7 +255,6 @@ fn min_mm() {
assert_eq!(mm_vector.iter().min().unwrap(), &Mm(0.0));
}


#[test]
fn pt_eq_zero_check() {
let pt1: Pt = Pt(0.0).into();
Expand All @@ -271,4 +274,4 @@ fn max_pt() {
fn min_pt() {
let pt_vector = vec![Pt(0.0), Pt(1.0), Pt(2.0)];
assert_eq!(pt_vector.iter().min().unwrap(), &Pt(0.0));
}
}
Loading

0 comments on commit 8e3b072

Please sign in to comment.