diff --git a/src/rust/lib.rs b/src/rust/lib.rs index 0c07ac6..8849da7 100644 --- a/src/rust/lib.rs +++ b/src/rust/lib.rs @@ -167,38 +167,64 @@ fn resvg_magic(mut options: Opts, svg_string: String) -> Result, String> } #[pyfunction] -#[pyo3(name = "svg_to_bytes")] +#[pyo3(signature = ( + svg_string= None, + svg_path = None, + background = None, + skip_system_fonts= false, + log_information = false, + width = None, + height= None, + zoom = None, + dpi = 0, + resources_dir = None, + languages = vec![], + font_size = 16, + font_family = "Times New Roman", + serif_family = "Times New Roman", + sans_serif_family = "Arial", + cursive_family = "Comic Sans MS", + fantasy_family = "Impact", + monospace_family = "Courier New", + font_files = None, + font_dirs = None, + shape_rendering = "geometric_precision", + text_rendering = "optimize_legibility", + image_rendering = "optimize_quality", + ))] fn svg_to_bytes( svg_string: Option, svg_path: Option, + // Background + background: Option, + // Skip System Fonts + skip_system_fonts: Option, + // Log informations + log_information: Option, // Control width, height, zoom, dpi width: Option, height: Option, zoom: Option, dpi: Option, - // Log informations - log_information: Option, + // Resource Directory resources_dir: Option, // Fonts languages: Option>, font_size: Option, - font_family: Option, - serif_family: Option, - sans_serif_family: Option, - cursive_family: Option, - fantasy_family: Option, - monospace_family: Option, + font_family: Option<&str>, + serif_family: Option<&str>, + sans_serif_family: Option<&str>, + cursive_family: Option<&str>, + fantasy_family: Option<&str>, + monospace_family: Option<&str>, font_files: Option>, font_dirs: Option>, // Effects based - shape_rendering: Option, - text_rendering: Option, - image_rendering: Option, - // Background - background: Option, - // Skip System Fonts - skip_system_fonts: Option, + shape_rendering: Option<&str>, + text_rendering: Option<&str>, + image_rendering: Option<&str>, + ) -> PyResult> { if log_information.unwrap_or(false) { if let Ok(()) = log::set_logger(&LOGGER) { @@ -250,7 +276,7 @@ fn svg_to_bytes( } let _shape_rendering = match shape_rendering - .unwrap_or("geometric_precision".to_string()) + .unwrap_or("geometric_precision") .as_ref() { "optimize_speed" => resvg::usvg::ShapeRendering::OptimizeSpeed, @@ -260,7 +286,7 @@ fn svg_to_bytes( }; let _text_rendering = match text_rendering - .unwrap_or("optimize_legibility".to_string()) + .unwrap_or("optimize_legibility") .as_ref() { "optimize_speed" => resvg::usvg::TextRendering::OptimizeSpeed, @@ -270,7 +296,7 @@ fn svg_to_bytes( }; let _image_rendering = match image_rendering - .unwrap_or("optimize_quality".to_string()) + .unwrap_or("optimize_quality") .as_ref() { "optimize_quality" => resvg::usvg::ImageRendering::OptimizeQuality, @@ -294,7 +320,7 @@ fn svg_to_bytes( let usvg_options = resvg::usvg::Options { resources_dir: _resources_dir, dpi: dpi.unwrap_or(0) as f32, - font_family: font_family.unwrap_or("Times New Roman".to_owned()), + font_family: font_family.unwrap_or("Times New Roman").to_string(), font_size: font_size.unwrap_or(16) as f32, languages: languages.unwrap_or(vec![]), shape_rendering: _shape_rendering, @@ -304,16 +330,18 @@ fn svg_to_bytes( image_href_resolver: resvg::usvg::ImageHrefResolver::default(), }; + + let options = Opts { usvg_opt: usvg_options, background: _background, skip_system_fonts: skip_system_fonts.unwrap_or(false), fit_to, - serif_family, - sans_serif_family, - cursive_family, - fantasy_family, - monospace_family, + serif_family:serif_family.map(|s|s.to_string()), + sans_serif_family:sans_serif_family.map(|s|s.to_string()), + cursive_family:cursive_family.map(|s|s.to_string()), + fantasy_family:fantasy_family.map(|s|s.to_string()), + monospace_family:monospace_family.map(|s|s.to_string()), font_files, font_dirs, };