Skip to content

Commit

Permalink
Update deps, including resvg
Browse files Browse the repository at this point in the history
  • Loading branch information
ogoffart committed Feb 21, 2024
1 parent fac0921 commit 2ab71db
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 45 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ fontdb = { version = "0.16.0", default-features = false }
fontdue = { version = "0.8.0" }
glutin = { version = "0.31.1", default-features = false }
itertools = { version = "0.12" }
resvg = { version= "0.38.0", default-features = false, features = ["text"] }
resvg = { version= "0.40.0", default-features = false, features = ["text"] }
send_wrapper = { version = "0.6.0" }
softbuffer = { version = "0.3.3", default-features = false }
strum = { version = "0.26.1", default-features = false, features = ["derive"] }
toml_edit = { version = "0.21" }
toml_edit = { version = "0.22" }
cfg_aliases = { version = "0.2.0" }

[profile.release]
Expand Down
33 changes: 16 additions & 17 deletions internal/compiler/passes/embed_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,30 +362,29 @@ fn load_image(
) -> image::ImageResult<(image::RgbaImage, SourceFormat, Size)> {
use resvg::{tiny_skia, usvg};
use std::ffi::OsStr;
use usvg::{TreeParsing, TreePostProc as _};
if file.canon_path.extension() == Some(OsStr::new("svg"))
|| file.canon_path.extension() == Some(OsStr::new("svgz"))
{
let options = usvg::Options::default();
let mut tree = match file.builtin_contents {
Some(data) => usvg::Tree::from_data(data, &options),
None => usvg::Tree::from_data(
std::fs::read(&file.canon_path).map_err(image::ImageError::IoError)?.as_slice(),
&options,
),
}
.map_err(|e| {
image::ImageError::Decoding(image::error::DecodingError::new(
image::error::ImageFormatHint::Name("svg".into()),
e,
))
let tree = i_slint_common::sharedfontdb::FONT_DB.with(|db| {
match file.builtin_contents {
Some(data) => usvg::Tree::from_data(data, &options, &db.borrow()),
None => usvg::Tree::from_data(
std::fs::read(&file.canon_path).map_err(image::ImageError::IoError)?.as_slice(),
&options,
&db.borrow(),
),
}
.map_err(|e| {
image::ImageError::Decoding(image::error::DecodingError::new(
image::error::ImageFormatHint::Name("svg".into()),
e,
))
})
})?;
i_slint_common::sharedfontdb::FONT_DB.with(|db| {
tree.postprocess(Default::default(), &db.borrow());
});
let scale_factor = scale_factor as f32;
// TODO: ideally we should find the size used for that `Image`
let original_size = tree.size;
let original_size = tree.size();
let width = original_size.width() * scale_factor;
let height = original_size.height() * scale_factor;

Expand Down
4 changes: 2 additions & 2 deletions internal/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ web-sys = { version = "0.3", features = [ "HtmlImageElement" ] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
fontdb = { workspace = true, optional = true, default-features = true }
rustybuzz = { version = "0.12.0", optional = true }
rustybuzz = { version = "0.13.0", optional = true }
fontdue = { workspace = true, optional = true }

[dev-dependencies]
slint = { path = "../../api/rs/slint", default-features = false, features = ["std", "compat-1-2"] }
i-slint-backend-testing = { path="../backends/testing" }
rustybuzz = "0.12.0"
rustybuzz = "0.13.0"
ttf-parser = "0.20.0"
fontdb = { workspace = true, default-features = true }
serde_json = "1.0.96"
Expand Down
33 changes: 9 additions & 24 deletions internal/core/graphics/image/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::lengths::PhysicalPx;
#[cfg(not(target_arch = "wasm32"))]
use crate::SharedString;
use resvg::{tiny_skia, usvg};
use usvg::{TreeParsing, TreePostProc};

pub struct ParsedSVG {
svg_tree: usvg::Tree,
Expand All @@ -32,7 +31,7 @@ impl core::fmt::Debug for ParsedSVG {

impl ParsedSVG {
pub fn size(&self) -> crate::graphics::IntSize {
let size = self.svg_tree.size.to_int_size();
let size = self.svg_tree.size().to_int_size();
[size.width(), size.height()].into()
}

Expand All @@ -52,16 +51,16 @@ impl ParsedSVG {
Some(size) => {
let target_size = tiny_skia::IntSize::from_wh(size.width, size.height)
.ok_or(usvg::Error::InvalidSize)?;
let target_size = tree.size.to_int_size().scale_to(target_size);
let target_size = tree.size().to_int_size().scale_to(target_size);
let target_size_f = target_size.to_size();

let transform = tiny_skia::Transform::from_scale(
target_size_f.width() as f32 / tree.size.width() as f32,
target_size_f.height() as f32 / tree.size.height() as f32,
target_size_f.width() as f32 / tree.size().width() as f32,
target_size_f.height() as f32 / tree.size().height() as f32,
);
(target_size, transform)
}
None => (tree.size.to_int_size(), tiny_skia::Transform::default()),
None => (tree.size().to_int_size(), tiny_skia::Transform::default()),
};

let mut buffer = SharedPixelBuffer::new(target_size.width(), target_size.height());
Expand All @@ -77,37 +76,23 @@ impl ParsedSVG {
}
}

fn with_svg_options<T>(callback: impl FnOnce(&usvg::Options) -> T) -> T {
let options = usvg::Options::default();
callback(&options)
}

fn fixup_text(mut tree: usvg::Tree) -> usvg::Tree {
i_slint_common::sharedfontdb::FONT_DB.with(|db| {
tree.postprocess(Default::default(), &db.borrow());
});
tree
}

#[cfg(not(target_arch = "wasm32"))]
pub fn load_from_path(
path: &SharedString,
cache_key: ImageCacheKey,
) -> Result<ParsedSVG, std::io::Error> {
let svg_data = std::fs::read(std::path::Path::new(&path.as_str()))?;

with_svg_options(|options| {
usvg::Tree::from_data(&svg_data, options)
.map(fixup_text)
i_slint_common::sharedfontdb::FONT_DB.with(|db| {
usvg::Tree::from_data(&svg_data, &Default::default(), &db.borrow())
.map(|svg| ParsedSVG { svg_tree: svg, cache_key })
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
})
}

pub fn load_from_data(slice: &[u8], cache_key: ImageCacheKey) -> Result<ParsedSVG, usvg::Error> {
with_svg_options(|options| {
usvg::Tree::from_data(slice, options)
.map(fixup_text)
i_slint_common::sharedfontdb::FONT_DB.with(|db| {
usvg::Tree::from_data(slice, &Default::default(), &db.borrow())
.map(|svg| ParsedSVG { svg_tree: svg, cache_key })
})
}

0 comments on commit 2ab71db

Please sign in to comment.