Skip to content

Commit

Permalink
Pass in font directory or use default font directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
StarArawn authored and Weasy666 committed Apr 23, 2023
1 parent cd50a98 commit 2bb8fe1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl AssetLoader for SvgAssetLoader {
) -> BoxedFuture<'a, Result<(), anyhow::Error>> {
Box::pin(async move {
debug!("Parsing SVG: {} ...", load_context.path().display());
let mut svg = Svg::from_bytes(bytes, load_context.path())?;
let mut svg = Svg::from_bytes(bytes, load_context.path(), None::<&std::path::Path>)?;
let name = &load_context
.path()
.file_name()
Expand Down
13 changes: 9 additions & 4 deletions src/svg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::Path;
use std::path::PathBuf;

use bevy::{
asset::Handle,
Expand Down Expand Up @@ -53,15 +53,20 @@ impl Default for Svg {

impl Svg {
/// Loads an SVG from bytes
pub fn from_bytes(bytes: &[u8], path: &Path) -> Result<Svg, FileSvgError> {
pub fn from_bytes(
bytes: &[u8],
path: impl Into<PathBuf>,
fonts: Option<impl Into<PathBuf>>,
) -> Result<Svg, FileSvgError> {
let mut opts = usvg::Options::default();
opts.fontdb.load_system_fonts();
opts.fontdb.load_fonts_dir("./assets");
opts.fontdb
.load_fonts_dir(fonts.map(|p| p.into()).unwrap_or("./assets".into()));

let svg_tree =
usvg::Tree::from_data(&bytes, &opts.to_ref()).map_err(|err| FileSvgError {
error: err.into(),
path: format!("{}", path.display()),
path: format!("{}", path.into().display()),
})?;

Ok(Svg::from_tree(svg_tree))
Expand Down

0 comments on commit 2bb8fe1

Please sign in to comment.