Skip to content

Commit

Permalink
gpui: Add support for text in svgs
Browse files Browse the repository at this point in the history
  • Loading branch information
SethDusek committed Mar 9, 2025
1 parent 22d9b5d commit f6c634e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 28 deletions.
122 changes: 98 additions & 24 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions crates/gpui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ profiling.workspace = true
rand = { optional = true, workspace = true }
raw-window-handle = "0.6"
refineable.workspace = true
resvg = { version = "0.44.0", default-features = false }
usvg = { version = "0.44.0", default-features = false }
resvg = { version = "0.45.0", default-features = false, features = ["text"] }
usvg = { version = "0.45.0", default-features = false }
schemars.workspace = true
seahash = "4.1"
semantic_version.workspace = true
Expand All @@ -117,6 +117,7 @@ util.workspace = true
uuid.workspace = true
waker-fn = "1.2.0"
lyon = "1.0"
fontdb = "0.23.0"

[target.'cfg(target_os = "macos")'.dependencies]
block = "0.1"
Expand Down
14 changes: 12 additions & 2 deletions crates/gpui/src/svg_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub(crate) struct RenderSvgParams {
#[derive(Clone)]
pub struct SvgRenderer {
asset_source: Arc<dyn AssetSource>,
usvg_options: Arc<usvg::Options<'static>>,
}

pub enum SvgSize {
Expand All @@ -24,7 +25,16 @@ pub enum SvgSize {

impl SvgRenderer {
pub fn new(asset_source: Arc<dyn AssetSource>) -> Self {
Self { asset_source }
let mut font_db = fontdb::Database::new();
font_db.load_system_fonts();
let options = usvg::Options {
fontdb: Arc::new(font_db),
..Default::default()
};
Self {
asset_source,
usvg_options: Arc::new(options),
}
}

pub(crate) fn render(&self, params: &RenderSvgParams) -> Result<Option<Vec<u8>>> {
Expand All @@ -49,7 +59,7 @@ impl SvgRenderer {
}

pub fn render_pixmap(&self, bytes: &[u8], size: SvgSize) -> Result<Pixmap, usvg::Error> {
let tree = usvg::Tree::from_data(bytes, &usvg::Options::default())?;
let tree = usvg::Tree::from_data(bytes, &self.usvg_options)?;

let size = match size {
SvgSize::Size(size) => size,
Expand Down

0 comments on commit f6c634e

Please sign in to comment.