Skip to content

Commit

Permalink
Version bump + improvements to svg2pdf (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV authored Jan 24, 2024
1 parent 327e886 commit 49891ef
Show file tree
Hide file tree
Showing 807 changed files with 6,413 additions and 354 deletions.
69 changes: 48 additions & 21 deletions Cargo.lock

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

12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ pdf-writer = "0.9"
pdfium-render = "0.8.6"
regex = "1.8.4"
termcolor = "1.2"
usvg = { version = "0.37.0", default-features = false }
usvg = { version = "0.38.0", default-features = false, features = ["text"] }
walkdir = "2.3.3"
tiny-skia = "0.11.3"
resvg = { version = "0.38.0" }

[package]
name = "svg2pdf"
Expand All @@ -45,15 +47,15 @@ license = { workspace = true }
bench = false

[features]
default = ["image"]
default = ["image", "filters"]
image = ["dep:image"]
filters = ["dep:tiny-skia", "dep:resvg"]

[dependencies]
miniz_oxide = { workspace = true }
once_cell = { workspace = true }
pdf-writer = { workspace = true }
usvg = { workspace = true }
image = { workspace = true, optional = true }

[dev-dependencies]
usvg = { workspace = true, features = ["text", "system-fonts"] }
tiny-skia = {workspace = true, optional = true}
resvg = {workspace = true, optional = true}
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ This crate allows to convert static (i.e. non-interactive) SVG files to
either standalone PDF files or Form XObjects that can be embedded in another
PDF file and used just like images.

The conversion will translate the SVG content to PDF without rasterizing it,
so no quality is lost.
Apart from groups with filters on them, the conversion will translate
the SVG content to PDF without rasterizing it, so no quality is lost.

## Example

Expand Down Expand Up @@ -52,26 +52,24 @@ features like:
- Patterns
- Clip paths
- Masks
- Filters
- Transformation matrices
- Respecting the `keepAspectRatio` attribute
- Raster images and nested SVGs

## Unsupported features
Among the unsupported features are currently:
- The `spreadMethod` attribute of gradients
- Filters
- Text will be converted into shapes before converting to PDF
- Text will be converted into shapes before converting to PDF. It is planned
to add support for text preservation in a future update.
- Raster images are not color managed but use PDF's DeviceRGB color space
- A number of features that were added in SVG2
- A number of features that were added in SVG2
(see [here](https://github.com/RazrFalcon/resvg/blob/master/docs/svg2-changelog.md))

## Contributing
We are looking forward to receiving your bugs and feature requests in the Issues
tab. We would also be very happy to accept PRs for bug fixes, features, or
refactorings!

If you want to contribute but are uncertain where to start, yo could look into
filters like `feBlend` and `feColorMatrix` that can be implemented with
transparency groups and color spaces, respectively. We'd be happy to assist you
refactorings! We'd be happy to assist you
with your PR's, so feel free to post Work in Progress PRs if marked as such.
Please be kind to the maintainers and other contributors. If you feel that there
are any problems, please feel free to reach out to us privately.
Expand Down
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ miniz_oxide = { workspace = true }
pdf-writer = { workspace = true }
svg2pdf = { workspace = true }
termcolor = { workspace = true }
usvg = { workspace = true, features = ["text"] }
usvg = { workspace = true}

[build-dependencies]
clap = { workspace = true, features = ["string"] }
Expand Down
3 changes: 3 additions & 0 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub struct ConvertCommand {
/// The number of SVG pixels per PDF points.
#[clap(long, default_value = "72.0")]
pub dpi: f32,
// How much rasterized effects should be scaled up.
#[clap(long, default_value = "1.0")]
pub raster_scale: f32,
}

/// Lists all discovered fonts in system.
Expand Down
4 changes: 2 additions & 2 deletions cli/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::args::ConvertCommand;
use std::path::{Path, PathBuf};
use svg2pdf::Options;
use usvg::{TreeParsing, TreeTextToPath};
use usvg::{PostProcessingSteps, TreeParsing, TreePostProc};

/// Execute a font listing command.
pub fn _convert(command: ConvertCommand) -> Result<(), String> {
Expand All @@ -26,7 +26,7 @@ pub fn convert_(
let options = usvg::Options::default();

let mut tree = usvg::Tree::from_str(&svg, &options).map_err(|err| err.to_string())?;
tree.convert_text(&fontdb);
tree.postprocess(PostProcessingSteps::default(), &fontdb);

let pdf = svg2pdf::convert_tree(&tree, Options { dpi, ..Options::default() });

Expand Down
16 changes: 13 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ pub struct Options {
///
/// _Default:_ `true`.
pub compress: bool,

/// How much raster images of rasterized effects should be scaled up.
///
/// Higher values will lead to better quality, but will increase the size of
/// the pdf.
///
/// _Default:_ 1
pub raster_scale: f32,
}

impl Default for Options {
Expand All @@ -117,6 +125,7 @@ impl Default for Options {
viewport: None,
aspect: None,
compress: true,
raster_scale: 1.0,
}
}
}
Expand All @@ -126,7 +135,7 @@ impl Default for Options {
/// Does not load any fonts and consequently cannot convert `text` elements. To
/// convert text, you should convert your source string to a
/// [`usvg` tree](Tree) manually,
/// [convert text with usvg](usvg::TreeTextToPath::convert_text) and then use
/// [convert text with usvg](usvg::TreePostProc::postprocess) and then use
/// [`convert_tree`].
///
/// Returns an error if the SVG string is malformed.
Expand All @@ -147,7 +156,7 @@ pub fn convert_str(src: &str, options: Options) -> Result<Vec<u8>, usvg::Error>
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use usvg::{fontdb, TreeParsing, TreeTextToPath};
/// use usvg::{fontdb, PostProcessingSteps, TreeParsing, TreePostProc};
/// use svg2pdf::Options;
///
/// let input = "tests/svg/custom/integration/matplotlib/step.svg";
Expand All @@ -159,7 +168,8 @@ pub fn convert_str(src: &str, options: Options) -> Result<Vec<u8>, usvg::Error>
///
/// let mut db = fontdb::Database::new();
/// db.load_system_fonts();
/// tree.convert_text(&db);
/// tree.postprocess(PostProcessingSteps::default(), &db);
///
///
/// let pdf = svg2pdf::convert_tree(&tree, Options::default());
/// std::fs::write(output, pdf)?;
Expand Down
Loading

0 comments on commit 49891ef

Please sign in to comment.