Skip to content

Commit

Permalink
Add Image::load_from_svg_data. (slint-ui#2638)
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft authored Apr 26, 2023
1 parent d253b47 commit fa690fe
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions internal/core/graphics/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,15 @@ impl Image {
})
}

/// Creates a new Image from the specified buffer, which contains SVG raw data.
#[cfg(feature = "svg")]
pub fn load_from_svg_data(buffer: &[u8]) -> Result<Self, LoadImageError> {
let cache_key = ImageCacheKey::Invalid;
Ok(Image(ImageInner::Svg(vtable::VRc::new(
svg::load_from_data(buffer, cache_key).map_err(|_| LoadImageError(()))?,
))))
}

/// Returns the size of the Image in pixels.
pub fn size(&self) -> IntSize {
match &self.0 {
Expand Down Expand Up @@ -665,6 +674,22 @@ fn test_image_size_from_buffer_without_backend() {
}
}

#[cfg(feature = "svg")]
#[test]
fn test_image_size_from_svg() {
let simple_svg = r#"<svg width="320" height="200" xmlns="http://www.w3.org/2000/svg"></svg>"#;
let image = Image::load_from_svg_data(simple_svg.as_bytes()).unwrap();
assert_eq!(image.size(), [320, 200].into());
}

#[cfg(feature = "svg")]
#[test]
fn test_image_invalid_svg() {
let invalid_svg = r#"AaBbCcDd"#;
let result = Image::load_from_svg_data(invalid_svg.as_bytes());
assert!(result.is_err());
}

/// Return an size that can be used to render an image in a buffer that matches a given ImageFit
pub fn fit_size(
image_fit: ImageFit,
Expand Down

0 comments on commit fa690fe

Please sign in to comment.