Skip to content

Commit

Permalink
Try newest resvg version
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Jun 1, 2024
1 parent 09be9e7 commit 8fa5928
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 65 deletions.
21 changes: 11 additions & 10 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ svg2pdf = { path = ".", default-features = false, version = "0.9.1" }
clap = { version = "4.4.2", features = ["derive"] }
clap_complete = "4.4.3"
clap_mangen = "0.2.14"
fontdb = {git = "https://github.com/LaurenzV/fontdb", branch = "custom"}
fontdb = "0.18.0"
image = { version = "0.25.1", default-features = false, features = ["jpeg", "png", "gif"] }
miniz_oxide = "0.7"
once_cell = "1.18.0"
oxipng = { version = "9", default-features = false, features = ["filetime", "parallel", "zopfli"] }
pdf-writer = "0.9"
pdfium-render = "0.8.6"
termcolor = "1.2"
usvg = { git = "https://github.com/LaurenzV/resvg", branch = "new_fontdb", default-features = false }
usvg = { git = "https://github.com/RazrFalcon/resvg", default-features = false }
tiny-skia = "0.11.4"
unicode-properties = "0.1.1"
resvg = { git = "https://github.com/LaurenzV/resvg", branch = "new_fontdb", default-features = false }
resvg = { git = "https://github.com/RazrFalcon/resvg", default-features = false }
subsetter = "0.1.1"
ttf-parser = { version = "0.20.0" }
ttf-parser = { version = "0.21.1" }
siphasher = { version = "1.0.1"}

[package]
Expand Down
42 changes: 18 additions & 24 deletions cli/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,33 @@ pub fn convert_(
log::set_max_level(log::LevelFilter::Warn);
}

// Prepare the font database.
let mut fontdb = fontdb::Database::new();
fontdb.load_system_fonts();
#[cfg(feature = "text")]
let mut options = usvg::Options::default();

fontdb.set_serif_family("Times New Roman");
fontdb.set_sans_serif_family("Arial");
fontdb.set_cursive_family("Comic Sans MS");
fontdb.set_fantasy_family("Impact");
fontdb.set_monospace_family("Courier New");
#[cfg(not(feature = "text"))]
let options = usvg::Options::default();

#[cfg(feature = "text")]
{
options.fontdb_mut().load_system_fonts();

options.fontdb_mut().set_serif_family("Times New Roman");
options.fontdb_mut().set_sans_serif_family("Arial");
options.fontdb_mut().set_cursive_family("Comic Sans MS");
options.fontdb_mut().set_fantasy_family("Impact");
options.fontdb_mut().set_monospace_family("Courier New");
}

// Convert the file.
let name = Path::new(input.file_name().ok_or("Input path does not point to a file")?);
let output = output.unwrap_or_else(|| name.with_extension("pdf"));

let svg = std::fs::read_to_string(input).map_err(|_| "Failed to load SVG file")?;

let options = usvg::Options::default();
let tree = usvg::Tree::from_str(&svg, &options).map_err(|err| err.to_string())?;

let tree = usvg::Tree::from_str(
&svg,
&options,
#[cfg(feature = "text")]
&fontdb,
)
.map_err(|err| err.to_string())?;

let pdf = svg2pdf::to_pdf(
&tree,
conversion_options,
page_options,
#[cfg(feature = "text")]
&fontdb,
);
let pdf =
svg2pdf::to_pdf(&tree, conversion_options, page_options, options.fontdb.as_ref());

std::fs::write(output, pdf).map_err(|_| "Failed to write PDF file")?;

Expand Down
26 changes: 21 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ This example reads an SVG file and writes the corresponding PDF back to the disk
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use svg2pdf::usvg::fontdb;
use svg2pdf::{ConversionOptions, PageOptions};
use std::sync::Arc;
let input = "tests/svg/custom/integration/matplotlib/stairs.svg";
let output = "target/stairs.pdf";
let svg = std::fs::read_to_string(input)?;
let options = svg2pdf::usvg::Options::default();
let mut db = fontdb::Database::new();
db.load_system_fonts();
let tree = svg2pdf::usvg::Tree::from_str(&svg, &options, &db)?;
let db = Arc::new(db);
let options = svg2pdf::usvg::Options {
fontdb: db.clone(),
..svg2pdf::usvg::Options::default()
};
let tree = svg2pdf::usvg::Tree::from_str(&svg, &options)?;
let pdf = svg2pdf::to_pdf(&tree, ConversionOptions::default(), PageOptions::default(), &db);
std::fs::write(output, pdf)?;
Expand Down Expand Up @@ -140,15 +145,20 @@ impl Default for ConversionOptions {
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use svg2pdf::usvg::fontdb;
/// use svg2pdf::{ConversionOptions, PageOptions};
/// use std::sync::Arc;
///
/// let input = "tests/svg/custom/integration/matplotlib/stairs.svg";
/// let output = "target/stairs.pdf";
///
/// let svg = std::fs::read_to_string(input)?;
/// let options = svg2pdf::usvg::Options::default();
/// let mut db = fontdb::Database::new();
/// db.load_system_fonts();
/// let mut tree = svg2pdf::usvg::Tree::from_str(&svg, &options, &db)?;
/// let db = Arc::new(db);
/// let options = svg2pdf::usvg::Options {
/// fontdb: db.clone(),
/// ..svg2pdf::usvg::Options::default()
/// };
/// let mut tree = svg2pdf::usvg::Tree::from_str(&svg, &options)?;
///
///
/// let pdf = svg2pdf::to_pdf(&tree, ConversionOptions::default(), PageOptions::default(), &db);
Expand Down Expand Up @@ -245,6 +255,7 @@ pub fn to_pdf(
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use std::collections::HashMap;
/// use std::sync::Arc;
/// use svg2pdf;
/// use pdf_writer::{Content, Finish, Name, Pdf, Rect, Ref, Str};
/// use svg2pdf::usvg::fontdb;
Expand All @@ -264,7 +275,12 @@ pub fn to_pdf(
/// let svg = std::fs::read_to_string(path)?;
/// let mut db = fontdb::Database::new();
/// db.load_system_fonts();
/// let tree = svg2pdf::usvg::Tree::from_str(&svg, &svg2pdf::usvg::Options::default(), &db)?;
/// let db = Arc::new(db);
/// let options = svg2pdf::usvg::Options {
/// fontdb: db.clone(),
/// ..svg2pdf::usvg::Options::default()
/// };
/// let tree = svg2pdf::usvg::Tree::from_str(&svg, &options)?;
/// let (mut svg_chunk, svg_id) = svg2pdf::to_chunk(&tree, svg2pdf::ConversionOptions::default(), &db);
///
/// // Renumber the chunk so that we can embed it into our existing workflow, and also make sure
Expand Down
15 changes: 5 additions & 10 deletions tests/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::create_fontdb;
use once_cell::sync::Lazy;
#[allow(unused_imports)]
use {
crate::FONTDB,
crate::render_pdf,
crate::{convert_svg, run_test_impl},
pdf_writer::{Content, Finish, Name, Pdf, Rect, Ref, Str},
Expand All @@ -11,9 +10,6 @@ use {
svg2pdf::PageOptions,
};

#[allow(dead_code)]
static FONTDB3: Lazy<std::sync::Mutex<fontdb::Database>> = Lazy::new(|| create_fontdb());

#[test]
fn text_to_paths() {
let options = ConversionOptions { embed_text: false, ..ConversionOptions::default() };
Expand Down Expand Up @@ -51,12 +47,11 @@ fn to_chunk() {
let path =
"svg/custom/integration/wikimedia/coat_of_the_arms_of_edinburgh_city_council.svg";
let svg = std::fs::read_to_string(path).unwrap();
let db = FONTDB3.lock().unwrap();
let tree =
svg2pdf::usvg::Tree::from_str(&svg, &svg2pdf::usvg::Options::default(), &db)
.unwrap();
let mut options = svg2pdf::usvg::Options::default();
options.fontdb = FONTDB.clone();
let tree = svg2pdf::usvg::Tree::from_str(&svg, &options).unwrap();
let (svg_chunk, svg_id) =
svg2pdf::to_chunk(&tree, svg2pdf::ConversionOptions::default(), &db);
svg2pdf::to_chunk(&tree, svg2pdf::ConversionOptions::default(), &FONTDB.as_ref());

let mut map = HashMap::new();
let svg_chunk =
Expand Down
20 changes: 8 additions & 12 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod api;
use std::cmp::max;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use image::io::Reader;
use image::{Rgba, RgbaImage};
Expand All @@ -16,8 +17,7 @@ use usvg::Tree;

use svg2pdf::{ConversionOptions, PageOptions};

static FONTDB1: Lazy<std::sync::Mutex<fontdb::Database>> = Lazy::new(|| create_fontdb());
static FONTDB2: Lazy<std::sync::Mutex<fontdb::Database>> = Lazy::new(|| create_fontdb());
static FONTDB: Lazy<Arc<fontdb::Database>> = Lazy::new(|| create_fontdb());

/// The global pdfium instance.
static PDFIUM: Lazy<std::sync::Mutex<Pdfium>> = Lazy::new(|| {
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn render_pdf(pdf: &[u8]) -> RgbaImage {
result
}

pub fn create_fontdb() -> std::sync::Mutex<fontdb::Database> {
pub fn create_fontdb() -> Arc<fontdb::Database> {
let mut fontdb = fontdb::Database::new();
fontdb.load_fonts_dir("fonts");

Expand All @@ -61,13 +61,14 @@ pub fn create_fontdb() -> std::sync::Mutex<fontdb::Database> {
fontdb.set_fantasy_family("Sedgwick Ave Display");
fontdb.set_monospace_family("Noto Mono");

std::sync::Mutex::new(fontdb)
Arc::new(fontdb)
}

/// Converts an SVG string into a usvg Tree
pub fn read_svg(svg_string: &str) -> Tree {
let options = usvg::Options::default();
Tree::from_str(svg_string, &options, &FONTDB1.lock().unwrap()).unwrap()
let mut options = usvg::Options::default();
options.fontdb = FONTDB.clone();
Tree::from_str(svg_string, &options).unwrap()
}

/// Converts an image into a PDF and returns the PDF as well as a rendered version
Expand All @@ -79,12 +80,7 @@ pub fn convert_svg(
) -> (Vec<u8>, RgbaImage) {
let svg = fs::read_to_string(svg_path).unwrap();
let tree = read_svg(&svg);
let pdf = svg2pdf::to_pdf(
&tree,
conversion_options,
page_options,
&FONTDB2.lock().unwrap(),
);
let pdf = svg2pdf::to_pdf(&tree, conversion_options, page_options, &FONTDB.as_ref());
let image = render_pdf(pdf.as_slice());
(pdf, image)
}
Expand Down

0 comments on commit 8fa5928

Please sign in to comment.