Skip to content

Commit

Permalink
Try to add font support (#27)
Browse files Browse the repository at this point in the history
* add

* code improvements

* add

* add

* Fiinal fix

* Remove Test Variables

* Revert "Remove Test Variables"

This reverts commit 378d23d.

* add

* fix

* remove placeholders

* allow auto version
  • Loading branch information
baseplate-admin authored May 9, 2024
1 parent 345ef36 commit 09fc53a
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 10 deletions.
3 changes: 2 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
import datetime
import resvg_py

project = "resvg_py"
author = "baseplate-admin"
copyright = f"2024-{datetime.date.today().year}, {author}"
release = "0.1.3"
release = resvg_py.version()

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ Documentation = "https://resvg-py.readthedocs.io/"

[tool.maturin]
features = ["pyo3/extension-module"]
python-source = "src/python"
3 changes: 3 additions & 0 deletions resvg_py.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def svg_to_bytes(
"optimize_quality"
],
background: str | None = None,
skip_system_fonts: bool | None = None,
) -> list[bytes]:
"""
:param svg_str: A string containing valid svg.
Expand All @@ -52,3 +53,5 @@ def svg_to_bytes(
"""

...

def version() -> str: ...
28 changes: 20 additions & 8 deletions src/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ struct Opts {
background: Option<svgtypes::Color>,
font_files: Option<Vec<String>>,
font_dirs: Option<Vec<String>>,
// skip_system_fonts: bool,
// Abstract Classes
fit_to: FitTo,
usvg_opt: resvg::usvg::Options,
// Renderers
skip_system_fonts: bool,
}

fn load_fonts(options: &mut Opts, fontdb: &mut resvg::usvg::fontdb::Database) {
Expand Down Expand Up @@ -88,6 +88,7 @@ fn load_fonts(options: &mut Opts, fontdb: &mut resvg::usvg::fontdb::Database) {
fn svg_to_skia_color(color: svgtypes::Color) -> resvg::tiny_skia::Color {
resvg::tiny_skia::Color::from_rgba8(color.red, color.green, color.blue, color.alpha)
}

fn render_svg(options: Opts, tree: &resvg::usvg::Tree) -> Result<resvg::tiny_skia::Pixmap, String> {
let mut pixmap = resvg::tiny_skia::Pixmap::new(
tree.size().to_int_size().width(),
Expand All @@ -112,23 +113,25 @@ fn resvg_magic(mut options: Opts, svg_string: String) -> Result<Vec<u8>, String>
};
resvg::usvg::roxmltree::Document::parse_with_options(&svg_string, xml_opt)
.map_err(|e| e.to_string())
}
.unwrap();
}?;
let has_text_nodes = xml_tree
.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);
}

let tree = {
resvg::usvg::Tree::from_xmltree(&xml_tree, &options.usvg_opt, &fontdb)
.map_err(|e| e.to_string())
}
.unwrap();
let img: Vec<u8> = render_svg(options, &tree).unwrap().encode_png().unwrap();
Ok(img)
}?;
Ok(render_svg(options, &tree)?.encode_png().unwrap())
}

#[pyfunction]
Expand Down Expand Up @@ -160,6 +163,8 @@ fn svg_to_bytes(
image_rendering: Option<String>,
// Background
background: Option<String>,
// Skip System Fonts
skip_system_fonts: Option<bool>,
) -> PyResult<Vec<u8>> {
let mut _svg_string = String::new();

Expand Down Expand Up @@ -234,7 +239,7 @@ fn svg_to_bytes(
};

let _resources_dir = match resources_dir {
Some(value) => Some(std::fs::canonicalize(value).unwrap()),
Some(value) => Some(std::fs::canonicalize(value)?),
None => None,
};

Expand Down Expand Up @@ -262,6 +267,7 @@ fn svg_to_bytes(
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,
Expand All @@ -275,9 +281,15 @@ fn svg_to_bytes(
Ok(pixmap)
}

#[pyfunction]
#[pyo3(name = "version")]
fn version() -> PyResult<String> {
Ok(env!("CARGO_PKG_VERSION").to_owned())
}
/// A Python module implemented in Rust.
#[pymodule]
fn resvg_py(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(svg_to_bytes, m)?)?;
m.add_function(wrap_pyfunction!(version, m)?)?;
Ok(())
}
Binary file added tests/font/Kokoro-Regular.ttf
Binary file not shown.
174 changes: 174 additions & 0 deletions tests/font/ink.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions tests/font/test_font_render.py

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 09fc53a

Please sign in to comment.