Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide from linguist #11

Merged
merged 8 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/*.py linguist-vendored
83 changes: 62 additions & 21 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ crate-type = ["cdylib"]
[dependencies]
base64 = "0.22.0"
pyo3 = "0.21.1"
resvg = { version = "0.41.0", features = ["raster-images","text"] }
tiny-skia = "0.11.4"
resvg = { version = "0.40.0", features = ["raster-images","text"] }
svgtypes = "0.15.0"
37 changes: 27 additions & 10 deletions src/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ enum FitTo {
}

impl FitTo {
fn fit_to_size(&self, size: tiny_skia::IntSize) -> Option<tiny_skia::IntSize> {
fn fit_to_size(&self, size: resvg::tiny_skia::IntSize) -> Option<resvg::tiny_skia::IntSize> {
match *self {
FitTo::Original => Some(size),
FitTo::Width(w) => size.scale_to_width(w),
FitTo::Height(h) => size.scale_to_height(h),
FitTo::Size(w, h) => tiny_skia::IntSize::from_wh(w, h).map(|s| size.scale_to(s)),
FitTo::Size(w, h) => resvg::tiny_skia::IntSize::from_wh(w, h).map(|s| size.scale_to(s)),
FitTo::Zoom(z) => size.scale_by(z),
}
}

fn fit_to_transform(&self, size: tiny_skia::IntSize) -> tiny_skia::Transform {
fn fit_to_transform(&self, size: resvg::tiny_skia::IntSize) -> resvg::tiny_skia::Transform {
let size1 = size.to_size();
let size2 = match self.fit_to_size(size) {
Some(v) => v.to_size(),
None => return tiny_skia::Transform::default(),
None => return resvg::tiny_skia::Transform::default(),
};
tiny_skia::Transform::from_scale(
resvg::tiny_skia::Transform::from_scale(
size2.width() / size1.width(),
size2.height() / size1.height(),
)
Expand All @@ -52,7 +52,7 @@ struct Opts {
cursive_family: Option<String>,
fantasy_family: Option<String>,
monospace_family: Option<String>,

background: Option<svgtypes::Color>,
font_files: Option<Vec<String>>,
font_dirs: Option<Vec<String>>,
// skip_system_fonts: bool,
Expand Down Expand Up @@ -86,13 +86,19 @@ fn load_fonts(options: &mut Opts, fontdb: &mut resvg::usvg::fontdb::Database) {
fontdb.set_fantasy_family(take_or(options.fantasy_family.take(), "Impact"));
fontdb.set_monospace_family(take_or(options.monospace_family.take(), "Courier New"));
}

fn render_svg(options: Opts, tree: &resvg::usvg::Tree) -> Result<tiny_skia::Pixmap, String> {
let mut pixmap = tiny_skia::Pixmap::new(
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(),
tree.size().to_int_size().height(),
)
.unwrap();

if let Some(background) = options.background {
pixmap.fill(svg_to_skia_color(background));
}
let ts = options.fit_to.fit_to_transform(tree.size().to_int_size());
resvg::render(tree, ts, &mut pixmap.as_mut());

Expand Down Expand Up @@ -151,6 +157,8 @@ fn svg_to_base64(
shape_rendering: Option<String>,
text_rendering: Option<String>,
image_rendering: Option<String>,
// Background
background: Option<String>,
) -> PyResult<String> {
let mut fit_to = FitTo::Original;
let mut default_size = resvg::usvg::Size::from_wh(100.0, 100.0).unwrap();
Expand Down Expand Up @@ -202,6 +210,14 @@ fn svg_to_base64(
None => None,
};

let _background = match background {
Some(color_str) => match color_str.parse::<svgtypes::Color>() {
Ok(color) => Some(color),
Err(error) => panic!("Error background: {}", error),
},
None => None,
};

let usvg_options = resvg::usvg::Options {
resources_dir: _resources_dir,
dpi: dpi.unwrap_or(0) as f32,
Expand All @@ -217,6 +233,7 @@ fn svg_to_base64(

let options = Opts {
usvg_opt: usvg_options,
background: _background,
fit_to,
serif_family,
sans_serif_family,
Expand All @@ -232,7 +249,7 @@ fn svg_to_base64(

/// A Python module implemented in Rust.
#[pymodule]
fn resvg_py(_py: Python, m: &PyModule) -> PyResult<()> {
fn resvg_py(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(svg_to_base64, m)?)?;
Ok(())
}
Loading