diff --git a/Cargo.lock b/Cargo.lock index b53b5f3..6eb925b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "adler" @@ -108,14 +108,14 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a595cb550439a117696039dfc69830492058211b771a2a165379f2a1a53d84d" dependencies = [ - "roxmltree", + "roxmltree 0.19.0", ] [[package]] name = "fontdb" -version = "0.16.2" +version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0299020c3ef3f60f526a4f64ab4a3d4ce116b1acbf24cdd22da0068e5d81dc3" +checksum = "e32eac81c1135c1df01d4e6d4233c47ba11f6a6d07f33e0bba09d18797077770" dependencies = [ "fontconfig-parser", "log", @@ -177,9 +177,9 @@ checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "log" -version = "0.4.22" +version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "memmap2" @@ -242,18 +242,18 @@ checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" [[package]] name = "proc-macro2" -version = "1.0.86" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ "unicode-ident", ] [[package]] name = "pyo3" -version = "0.23.2" +version = "0.23.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f54b3d09cbdd1f8c20650b28e7b09e338881482f4aa908a5f61a00c98fba2690" +checksum = "e484fd2c8b4cb67ab05a318f1fd6fa8f199fcc30819f08f07d200809dba26c15" dependencies = [ "cfg-if", "indoc", @@ -269,9 +269,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.23.2" +version = "0.23.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3015cf985888fe66cfb63ce0e321c603706cd541b7aec7ddd35c281390af45d8" +checksum = "dc0e0469a84f208e20044b98965e1561028180219e35352a2afaf2b942beff3b" dependencies = [ "once_cell", "target-lexicon", @@ -279,9 +279,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.23.2" +version = "0.23.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fca7cd8fd809b5ac4eefb89c1f98f7a7651d3739dfb341ca6980090f554c270" +checksum = "eb1547a7f9966f6f1a0f0227564a9945fe36b90da5a93b3933fc3dc03fae372d" dependencies = [ "libc", "pyo3-build-config", @@ -289,9 +289,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.23.2" +version = "0.23.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34e657fa5379a79151b6ff5328d9216a84f55dc93b17b08e7c3609a969b73aa0" +checksum = "fdb6da8ec6fa5cedd1626c886fc8749bdcbb09424a86461eb8cdf096b7c33257" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -301,9 +301,9 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.23.2" +version = "0.23.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "295548d5ffd95fd1981d2d3cf4458831b21d60af046b729b6fd143b0ba7aee2f" +checksum = "38a385202ff5a92791168b1136afae5059d3ac118457bb7bc304c197c2d33e7d" dependencies = [ "heck", "proc-macro2", @@ -323,15 +323,14 @@ dependencies = [ [[package]] name = "resvg" -version = "0.41.0" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2327ced609dadeed3e9702fec3e6b2ddd208758a9268d13e06566c6101ba533" +checksum = "944d052815156ac8fa77eaac055220e95ba0b01fa8887108ca710c03805d9051" dependencies = [ "gif", "jpeg-decoder", "log", "pico-args", - "png", "rgb", "svgtypes", "tiny-skia", @@ -340,7 +339,7 @@ dependencies = [ [[package]] name = "resvg_py" -version = "0.1.6" +version = "0.1.5" dependencies = [ "log", "pyo3", @@ -363,11 +362,17 @@ version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3cd14fd5e3b777a7422cca79358c57a8f6e3a703d9ac187448d0daf220c2407f" +[[package]] +name = "roxmltree" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97" + [[package]] name = "rustybuzz" -version = "0.13.0" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88117946aa1bfb53c2ae0643ceac6506337f44887f8c9fbfb43587b1cc52ba49" +checksum = "7730060ad401b0d1807c904ea56735288af101430aa0d2ab8358b789f5f37002" dependencies = [ "bitflags 2.5.0", "bytemuck", @@ -426,9 +431,9 @@ dependencies = [ [[package]] name = "svgtypes" -version = "0.15.2" +version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "794de53cc48eaabeed0ab6a3404a65f40b3e38c067e4435883a65d2aa4ca000e" +checksum = "fae3064df9b89391c9a76a0425a69d124aee9c5c28455204709e72c39868a43c" dependencies = [ "kurbo", "siphasher", @@ -436,9 +441,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.68" +version = "2.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" dependencies = [ "proc-macro2", "quote", @@ -494,9 +499,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "ttf-parser" -version = "0.20.0" +version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17f77d76d837a7830fe1d4f12b7b4ba4192c1888001c7164257e4bc6d21d96b4" +checksum = "2c591d83f69777866b9126b24c6dd9a18351f177e49d625920d19f989fd31cf8" [[package]] name = "unicode-bidi" @@ -548,9 +553,9 @@ checksum = "c7de7d73e1754487cb58364ee906a499937a0dfabd86bcb980fa99ec8c8fa2ce" [[package]] name = "usvg" -version = "0.41.0" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c704361d822337cfc00387672c7b59eaa72a1f0744f62b2a68aa228a0c6927d" +checksum = "b84ea542ae85c715f07b082438a4231c3760539d902e11d093847a0b22963032" dependencies = [ "base64", "data-url", @@ -560,7 +565,7 @@ dependencies = [ "kurbo", "log", "pico-args", - "roxmltree", + "roxmltree 0.20.0", "rustybuzz", "simplecss", "siphasher", diff --git a/Cargo.toml b/Cargo.toml index 60166d6..9935011 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "resvg_py" -version = "0.1.6" +version = "0.1.5" edition = "2021" authors = ['baseplate-admin'] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -10,20 +10,20 @@ path = "src/rust/lib.rs" crate-type = ["cdylib"] [dependencies] -log = "0.4.22" -pyo3 = "0.23.2" -resvg = { version = "0.41.0", features = ["raster-images","text"] } -svgtypes = "0.15.2" +log = "0.4.21" +pyo3 = "0.23.0" +resvg = { version = "0.42.0", features = ["raster-images", "text"] } +svgtypes = "0.15.1" [profile.release.package."*"] codegen-units = 1 -opt-level ='z' +opt-level = 'z' strip = true [profile.release] panic = "abort" codegen-units = 1 lto = "fat" -opt-level ='z' +opt-level = 'z' strip = true diff --git a/src/rust/lib.rs b/src/rust/lib.rs index c248edd..b8e15b3 100644 --- a/src/rust/lib.rs +++ b/src/rust/lib.rs @@ -5,7 +5,7 @@ Based on */ use pyo3::prelude::*; -use resvg; +use resvg::{self, usvg::{FontResolver}}; #[derive(Clone, Copy, PartialEq, Debug)] @@ -77,7 +77,7 @@ impl FitTo { ) } } -struct Opts { +struct Opts<'a> { // font_size: u32, serif_family: Option, sans_serif_family: Option, @@ -89,12 +89,14 @@ struct Opts { font_dirs: Option>, // Abstract Classes fit_to: FitTo, - usvg_opt: resvg::usvg::Options, + usvg_opt: resvg::usvg::Options<'a>, // Renderers skip_system_fonts: bool, } -fn load_fonts(options: &mut Opts, fontdb: &mut resvg::usvg::fontdb::Database) { + +fn load_fonts(options: &mut Opts,fontdb: &mut resvg::usvg::fontdb::Database) { + if let Some(font_files) = &options.font_files { for path in font_files { if let Err(e) = fontdb.load_font_file(path) { @@ -112,6 +114,7 @@ fn load_fonts(options: &mut Opts, fontdb: &mut resvg::usvg::fontdb::Database) { let take_or = |family: Option, fallback: &str| family.unwrap_or_else(|| fallback.to_string()); + // Use lock to modify the fontdb mutably fontdb.set_serif_family(take_or(options.serif_family.take(), "Times New Roman")); fontdb.set_sans_serif_family(take_or(options.sans_serif_family.take(), "Arial")); fontdb.set_cursive_family(take_or(options.cursive_family.take(), "Comic Sans MS")); @@ -138,7 +141,7 @@ fn render_svg(options: Opts, tree: &resvg::usvg::Tree) -> Result Result, String> { +fn resvg_magic(mut options: Opts, svg_string: String,fontdb: &mut resvg::usvg::fontdb::Database ) -> Result, String> { let xml_tree = { let xml_opt = resvg::usvg::roxmltree::ParsingOptions { allow_dtd: true, @@ -151,17 +154,17 @@ fn resvg_magic(mut options: Opts, svg_string: String) -> Result, String> .descendants() .any(|n| n.has_tag_name(("http://www.w3.org/2000/svg", "text"))); - let mut fontdb = resvg::usvg::fontdb::Database::new(); + if !options.skip_system_fonts { fontdb.load_system_fonts(); } if has_text_nodes { - load_fonts(&mut options, &mut fontdb); + load_fonts(&mut options,fontdb); } let tree = { - resvg::usvg::Tree::from_xmltree(&xml_tree, &options.usvg_opt, &fontdb) + resvg::usvg::Tree::from_xmltree(&xml_tree, &options.usvg_opt) .map_err(|e| e.to_string()) }?; Ok(render_svg(options, &tree)?.encode_png().unwrap()) @@ -227,6 +230,7 @@ fn svg_to_bytes( image_rendering: Option, ) -> PyResult> { + if log_information.unwrap_or(false) { if let Ok(()) = log::set_logger(&LOGGER) { log::set_max_level(log::LevelFilter::Warn); @@ -234,7 +238,6 @@ fn svg_to_bytes( } let mut _svg_string = String::new(); - if let Some(svg_string) = svg_string { _svg_string = svg_string; } @@ -318,6 +321,8 @@ fn svg_to_bytes( None => None, }; + let mut fontdb = resvg::usvg::fontdb::Database::new(); + let usvg_options = resvg::usvg::Options { resources_dir: _resources_dir, dpi: dpi.unwrap_or(0) as f32, @@ -329,6 +334,8 @@ fn svg_to_bytes( image_rendering: _image_rendering, default_size, image_href_resolver: resvg::usvg::ImageHrefResolver::default(), + fontdb:fontdb.clone().into(), + font_resolver:FontResolver::default() }; @@ -346,7 +353,7 @@ fn svg_to_bytes( font_files, font_dirs, }; - let pixmap = resvg_magic(options, _svg_string.trim().to_owned()).unwrap(); + let pixmap = resvg_magic(options, _svg_string.trim().to_owned(),&mut fontdb).unwrap(); Ok(pixmap) }