Skip to content

Commit

Permalink
Tweaks to Svg
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Feb 7, 2025
1 parent 7032c39 commit 02c9047
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions crates/kas-resvg/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,33 +153,31 @@ impl_scope! {
pub fn new(data: &'static [u8]) -> Result<Self, impl std::error::Error> {
let mut svg = Svg::default();
let source = Source::Static(data, None);
svg.load_source(source).map(|_action| svg)
svg.load_source(source).map(|_| svg)
}

/// Construct from a path
pub fn new_path<P: AsRef<Path>>(path: P) -> Result<Self, impl std::error::Error> {
let mut svg = Svg::default();
let _action = svg.load_path_(path.as_ref())?;
svg._load_path(path.as_ref())?;
Result::<Self, LoadError>::Ok(svg)
}

/// Load from `data`
///
/// Replaces existing data, but does not re-render until a resize
/// happens (hence returning [`Action::RESIZE`]).
///
/// This sets [`PixmapScaling::size`] from the SVG.
/// Replaces existing data and request a resize. The sizing policy is
/// set to [`PixmapScaling::size`] using dimensions from the SVG.
pub fn load(
&mut self,
cx: &mut EventState,
data: &'static [u8],
resources_dir: Option<&Path>,
) -> Result<(), impl std::error::Error> {
let source = Source::Static(data, resources_dir.map(|p| p.to_owned()));
self.load_source(source).map(|act| cx.action(self, act))
self.load_source(source).map(|_| cx.resize(self))
}

fn load_source(&mut self, source: Source) -> Result<Action, usvg::Error> {
fn load_source(&mut self, source: Source) -> Result<(), usvg::Error> {
// Set scaling size. TODO: this is useless if Self::with_size is called after.
let size = source.tree()?.size();
self.scaling.size = LogicalSize(size.width(), size.height());
Expand All @@ -188,7 +186,7 @@ impl_scope! {
State::Ready(_, px) => State::Ready(source, px),
_ => State::Initial(source),
};
Ok(Action::RESIZE)
Ok(())
}

/// Load from a path
Expand All @@ -199,10 +197,10 @@ impl_scope! {
cx: &mut EventState,
path: P,
) -> Result<(), impl std::error::Error> {
self.load_path_(path.as_ref()).map(|act| cx.action(self, act))
self._load_path(path.as_ref()).map(|_| cx.resize(self))
}

fn load_path_(&mut self, path: &Path) -> Result<Action, LoadError> {
fn _load_path(&mut self, path: &Path) -> Result<(), LoadError> {
let buf = std::fs::read(path)?;
let rd = path.parent().map(|path| path.to_owned());
let source = Source::Heap(buf.into(), rd);
Expand Down

0 comments on commit 02c9047

Please sign in to comment.