mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 04:40:03 -04:00
Support SVG Text Rendering in egui_extras (#5979)
**Added** * Create `svg_text` feature flag to support text rendering & loading of system fonts. **Changed** * Updates `resvg` to `0.45`. * Adds `usvg::Options` field to the `SvgLoader` structure. * Change function signatures to support passing `usvg::Options` to downstream `load_svg_bytes_with_size`. **Additional Info** * I used this PR as a reference: https://github.com/emilk/egui/pull/4659. @xNWP can you see if this adequately resolves your concern from your original PR? * Closes https://github.com/emilk/egui/issues/5977 (we may want to open another issue for my other thoughts in this issue) * Also, I would like to thank @xNWP and their original PR for being a good reference for this one. * [x] I have followed the instructions in the PR template
This commit is contained in:
committed by
GitHub
parent
a8e0c56a8f
commit
0f1d6c2818
@@ -10,9 +10,9 @@ use egui::{
|
||||
|
||||
type Entry = Result<Arc<ColorImage>, String>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct SvgLoader {
|
||||
cache: Mutex<HashMap<(Cow<'static, str>, SizeHint), Entry>>,
|
||||
options: resvg::usvg::Options<'static>,
|
||||
}
|
||||
|
||||
impl SvgLoader {
|
||||
@@ -27,6 +27,22 @@ fn is_supported(uri: &str) -> bool {
|
||||
ext == "svg"
|
||||
}
|
||||
|
||||
impl Default for SvgLoader {
|
||||
fn default() -> Self {
|
||||
// opt is mutated when `svg_text` feature flag is enabled
|
||||
#[allow(unused_mut)]
|
||||
let mut options = resvg::usvg::Options::default();
|
||||
|
||||
#[cfg(feature = "svg_text")]
|
||||
options.fontdb_mut().load_system_fonts();
|
||||
|
||||
Self {
|
||||
cache: Mutex::new(HashMap::default()),
|
||||
options,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ImageLoader for SvgLoader {
|
||||
fn id(&self) -> &str {
|
||||
Self::ID
|
||||
@@ -48,8 +64,12 @@ impl ImageLoader for SvgLoader {
|
||||
match ctx.try_load_bytes(uri) {
|
||||
Ok(BytesPoll::Ready { bytes, .. }) => {
|
||||
log::trace!("started loading {uri:?}");
|
||||
let result = crate::image::load_svg_bytes_with_size(&bytes, Some(size_hint))
|
||||
.map(Arc::new);
|
||||
let result = crate::image::load_svg_bytes_with_size(
|
||||
&bytes,
|
||||
Some(size_hint),
|
||||
&self.options,
|
||||
)
|
||||
.map(Arc::new);
|
||||
log::trace!("finished loading {uri:?}");
|
||||
cache.insert((Cow::Owned(uri.to_owned()), size_hint), result.clone());
|
||||
match result {
|
||||
|
||||
Reference in New Issue
Block a user