Skip to content

Commit

Permalink
Update WASM API
Browse files Browse the repository at this point in the history
  • Loading branch information
fschutt committed Feb 24, 2025
1 parent 4af6a09 commit aff2e06
Show file tree
Hide file tree
Showing 23 changed files with 1,272 additions and 588 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ jobs:
with:
target: wasm32-unknown-unknown
toolchain: ${{ matrix.channel }}

- run: rustup target add wasm32-wasip1
- run: rustup target add wasm32-wasip2
- run: rustup target add wasm32-unknown-unknown
- run: cargo check --verbose --target wasm32-unknown-unknown --examples
- run: cargo check --verbose --target wasm32-unknown-unknown

- run: cargo check --verbose --target wasm32-wasip1 --examples
- run: cargo check --verbose --target wasm32-wasip1
- run: cargo check --verbose --target wasm32-wasip2 --examples
- run: cargo check --verbose --target wasm32-wasip2
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ edition = "2021"
lopdf = { git = "https://github.com/J-F-Liu/lopdf", rev = "80a6c505bf8303c74b3057c8e53d4f791e92e126", default-features = false, features = [
"nom_parser",
] }
time = { version = "0.3.25", default-features = false, features = ["std"] }
time = { version = "0.3.25", default-features = false, features = ["std", "serde", "serde-human-readable"] }
allsorts = { version = "0.15", git = "https://github.com/fschutt/allsorts", branch = "optional-brotli", default-features = false, features = ["flate2_rust"] }
image = { version = "0.25", default-features = false }
svg2pdf = { version = "0.12.0" }
Expand All @@ -40,7 +40,7 @@ base64 = "0.22.1"
flate2 = "1.0.35"

[features]
default = ["js-sys"]
default = []
wasm = ["wasm-bindgen"]
gif = ["image/gif"]
jpeg = ["image/jpeg"]
Expand Down
6 changes: 5 additions & 1 deletion examples/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ fn main() {
let page = PdfPage::new(Mm(100.0), Mm(100.0), ops);
let svg = page.to_svg(&doc.resources, &PdfToSvgOptions::web());
std::fs::write("./helloworld.svg", svg).unwrap();
std::fs::write("./helloworld.pdf", doc.with_pages(vec![page]).save(&PdfSaveOptions::default())).unwrap();
std::fs::write(
"./helloworld.pdf",
doc.with_pages(vec![page]).save(&PdfSaveOptions::default()),
)
.unwrap();
}
42 changes: 21 additions & 21 deletions src/annotation.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
//! Bookmarks, page and link annotations
use serde_derive::{Deserialize, Serialize};

use crate::graphics::Rect;

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
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)]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct LinkAnnotation {
pub rect: Rect,
pub actions: Actions,

#[serde(default)]
pub border: BorderArray,
#[serde(default)]
pub color: ColorArray,
pub actions: Actions,
#[serde(default)]
pub highlighting: HighlightingMode,
}

Expand All @@ -38,7 +44,8 @@ impl LinkAnnotation {
}
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum BorderArray {
Solid([f32; 3]),
Dashed([f32; 3], DashPhase),
Expand All @@ -57,31 +64,20 @@ impl BorderArray {
}
}

/*
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::Real(self.phase.into()),
])
}
}
*/

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

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

#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum ColorArray {
Transparent,
Gray([f32; 1]),
Expand All @@ -95,8 +91,9 @@ impl Default for ColorArray {
}
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[non_exhaustive]
#[serde(tag = "type", content = "data")]
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`.
Expand Down Expand Up @@ -131,7 +128,9 @@ pub enum Destination {
Trans (PDF 1.5) Updates the display of a document, using a transition dictionary. “Transition Actions” on page 670
GoTo3DView (PDF 1.6) Set the current view of a 3D annotation “Go-To-3D-View Actions” on page 670
*/
#[derive(Debug, PartialEq, Clone)]

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", tag = "type", content = "data")]
pub enum Actions {
GoTo(Destination),
URI(String),
Expand All @@ -158,7 +157,8 @@ impl Actions {
}
}

#[derive(Debug, PartialEq, Clone, Copy, Default)]
#[derive(Debug, PartialEq, Clone, Copy, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum HighlightingMode {
None,
#[default]
Expand Down
22 changes: 15 additions & 7 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use serde_derive::{Deserialize, Serialize};

use crate::IccProfileId;

/// Color space (enum for marking the number of bits a color has)
#[derive(Debug, Copy, PartialEq, Clone)]
#[derive(Debug, Copy, PartialEq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ColorSpace {
Rgb,
Rgba,
Expand Down Expand Up @@ -38,7 +41,8 @@ impl From<image::ColorType> for ColorSpace {
}

/// How many bits does a color have?
#[derive(Debug, Copy, PartialEq, Clone)]
#[derive(Debug, Copy, PartialEq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ColorBits {
Bit1,
Bit8,
Expand All @@ -56,7 +60,8 @@ impl ColorBits {
}

/// Wrapper for Rgb, Cmyk and other color types
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum Color {
Rgb(Rgb),
Cmyk(Cmyk),
Expand Down Expand Up @@ -95,11 +100,12 @@ impl Color {
}

/// RGB color
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Rgb {
pub r: f32,
pub g: f32,
pub b: f32,
#[serde(default)]
pub icc_profile: Option<IccProfileId>,
}

Expand All @@ -115,12 +121,13 @@ impl Rgb {
}

/// CMYK color
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Cmyk {
pub c: f32,
pub m: f32,
pub y: f32,
pub k: f32,
#[serde(default)]
pub icc_profile: Option<IccProfileId>,
}

Expand All @@ -138,9 +145,10 @@ impl Cmyk {
}

/// Greyscale color
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Greyscale {
pub percent: f32,
#[serde(default)]
pub icc_profile: Option<IccProfileId>,
}

Expand All @@ -155,7 +163,7 @@ impl Greyscale {

/// Spot colors are like Cmyk, but without color space. They are essentially "named" colors
/// from specific vendors - currently they are the same as a CMYK color.
#[derive(Debug, Copy, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
pub struct SpotColor {
pub c: f32,
pub m: f32,
Expand Down
7 changes: 5 additions & 2 deletions src/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
//!
//! [PDF/A Versions](https://en.wikipedia.org/wiki/PDF/A)
use serde_derive::{Deserialize, Serialize};

/// List of (relevant) PDF versions
/// Please note the difference between **PDF/A** (archiving), **PDF/UA** (universal acessibility),
/// **PDF/X** (printing), **PDF/E** (engineering / CAD), **PDF/VT** (large volume transactions with
/// repeated content)
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[allow(non_camel_case_types)]
#[serde(rename_all = "kebab-case", tag = "type", content = "data")]
pub enum PdfConformance {
/// `PDF/A-1b` basic PDF, many features restricted
A1B_2005_PDF_1_4,
Expand Down Expand Up @@ -74,7 +77,7 @@ impl Default for PdfConformance {

/// Allows building custom conformance profiles. This is useful if you want very small documents for
/// example and you don't __need__ conformance with any PDF standard, you just want a PDF file.
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct CustomPdfConformance {
/// Identifier for this conformance
///
Expand Down
Loading

0 comments on commit aff2e06

Please sign in to comment.