gpui: Add SVG rendering to img
element and generic asset cache (#9931)
This is a follow up to #9436 . It has a cleaner API and generalized the image_cache to be a generic asset cache, that all GPUI elements can make use off. The changes have been discussed with @mikayla-maki on Discord. --------- Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
parent
ed5bfcdddc
commit
f9becbd3d1
11 changed files with 392 additions and 237 deletions
|
@ -1,5 +1,6 @@
|
|||
use crate::{AssetSource, DevicePixels, IsZero, Result, SharedString, Size};
|
||||
use anyhow::anyhow;
|
||||
use resvg::tiny_skia::Pixmap;
|
||||
use std::{
|
||||
hash::Hash,
|
||||
sync::{Arc, OnceLock},
|
||||
|
@ -11,10 +12,16 @@ pub(crate) struct RenderSvgParams {
|
|||
pub(crate) size: Size<DevicePixels>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct SvgRenderer {
|
||||
asset_source: Arc<dyn AssetSource>,
|
||||
}
|
||||
|
||||
pub enum SvgSize {
|
||||
Size(Size<DevicePixels>),
|
||||
ScaleFactor(f32),
|
||||
}
|
||||
|
||||
impl SvgRenderer {
|
||||
pub fn new(asset_source: Arc<dyn AssetSource>) -> Self {
|
||||
Self { asset_source }
|
||||
|
@ -27,20 +34,8 @@ impl SvgRenderer {
|
|||
|
||||
// Load the tree.
|
||||
let bytes = self.asset_source.load(¶ms.path)?;
|
||||
let tree =
|
||||
resvg::usvg::Tree::from_data(&bytes, &resvg::usvg::Options::default(), svg_fontdb())?;
|
||||
|
||||
// Render the SVG to a pixmap with the specified width and height.
|
||||
let mut pixmap =
|
||||
resvg::tiny_skia::Pixmap::new(params.size.width.into(), params.size.height.into())
|
||||
.unwrap();
|
||||
|
||||
let ratio = params.size.width.0 as f32 / tree.size().width();
|
||||
resvg::render(
|
||||
&tree,
|
||||
resvg::tiny_skia::Transform::from_scale(ratio, ratio),
|
||||
&mut pixmap.as_mut(),
|
||||
);
|
||||
let pixmap = self.render_pixmap(&bytes, SvgSize::Size(params.size))?;
|
||||
|
||||
// Convert the pixmap's pixels into an alpha mask.
|
||||
let alpha_mask = pixmap
|
||||
|
@ -50,10 +45,37 @@ impl SvgRenderer {
|
|||
.collect::<Vec<_>>();
|
||||
Ok(alpha_mask)
|
||||
}
|
||||
|
||||
pub fn render_pixmap(&self, bytes: &[u8], size: SvgSize) -> Result<Pixmap, resvg::usvg::Error> {
|
||||
let tree =
|
||||
resvg::usvg::Tree::from_data(&bytes, &resvg::usvg::Options::default(), svg_fontdb())?;
|
||||
|
||||
let size = match size {
|
||||
SvgSize::Size(size) => size,
|
||||
SvgSize::ScaleFactor(scale) => crate::size(
|
||||
DevicePixels((tree.size().width() * scale) as i32),
|
||||
DevicePixels((tree.size().height() * scale) as i32),
|
||||
),
|
||||
};
|
||||
|
||||
// Render the SVG to a pixmap with the specified width and height.
|
||||
let mut pixmap =
|
||||
resvg::tiny_skia::Pixmap::new(size.width.into(), size.height.into()).unwrap();
|
||||
|
||||
let ratio = size.width.0 as f32 / tree.size().width();
|
||||
|
||||
resvg::render(
|
||||
&tree,
|
||||
resvg::tiny_skia::Transform::from_scale(ratio, ratio),
|
||||
&mut pixmap.as_mut(),
|
||||
);
|
||||
|
||||
Ok(pixmap)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the global font database used for SVG rendering.
|
||||
fn svg_fontdb() -> &'static resvg::usvg::fontdb::Database {
|
||||
pub(crate) fn svg_fontdb() -> &'static resvg::usvg::fontdb::Database {
|
||||
static FONTDB: OnceLock<resvg::usvg::fontdb::Database> = OnceLock::new();
|
||||
FONTDB.get_or_init(|| {
|
||||
let mut fontdb = resvg::usvg::fontdb::Database::new();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue