Skip to content

Commit

Permalink
remove SVG helper struct
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyDM committed Jul 7, 2024
1 parent e9d5503 commit 6ec1c6d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 60 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repository = "https://github.com/grovesNL/glyphon"
license = "MIT OR Apache-2.0 OR Zlib"

[features]
default = ["svg-icons"]
svg-icons = ["dep:resvg"]

[dependencies]
Expand Down
8 changes: 3 additions & 5 deletions examples/svg-icons.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use glyphon::{
icon::{IconDesc, IconRenderer, IconSourceID, IconSystem, SvgSource},
icon::{IconDesc, IconRenderer, IconSourceID, IconSystem},
Attrs, Buffer, Cache, Color, Family, FontSystem, Metrics, Resolution, Shaping, SwashCache,
TextArea, TextAtlas, TextBounds, TextRenderer, Viewport,
};
Expand Down Expand Up @@ -96,14 +96,12 @@ async fn run() {
// Add SVG sources to the icon system.
icon_system.add_svg(
IconSourceID(0),
SvgSource::Data(LION_SVG).load(&Default::default()).unwrap(),
resvg::usvg::Tree::from_data(LION_SVG, &Default::default()).unwrap(),
true,
);
icon_system.add_svg(
IconSourceID(1),
SvgSource::Data(EAGLE_SVG)
.load(&Default::default())
.unwrap(),
resvg::usvg::Tree::from_data(EAGLE_SVG, &Default::default()).unwrap(),
false,
);

Expand Down
55 changes: 0 additions & 55 deletions src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,58 +425,3 @@ impl IconSystem {
fn zero_depth(_: usize) -> f32 {
0f32
}

/// A helper struct to load SVG data.
pub enum SvgSource<'a> {
Data(&'a [u8]),
String(&'a str),
Path(&'a Path),
}

impl<'a> SvgSource<'a> {
/// Load and parse the SVG data.
pub fn load(self, opt: &usvg::Options<'_>) -> Result<usvg::Tree, SvgSourceError> {
let tree = match self {
Self::Data(data) => usvg::Tree::from_data(data, opt)?,
Self::String(text) => usvg::Tree::from_str(text, opt)?,
Self::Path(path) => {
let data = std::fs::read(path)?;
usvg::Tree::from_data(&data, opt)?
}
};

Ok(tree)
}
}

/// An error that occured while loading and parsing an [`SvgSource`].
#[derive(Debug)]
pub enum SvgSourceError {
/// An error occured while parsing the SVG data.
SvgError(usvg::Error),
/// An error occured while loading the SVG file.
IoError(std::io::Error),
}

impl std::error::Error for SvgSourceError {}

impl std::fmt::Display for SvgSourceError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::SvgError(e) => write!(f, "Could not load svg source: vg error: {}", e),
Self::IoError(e) => write!(f, "Could not load svg source: io error: {}", e),
}
}
}

impl From<usvg::Error> for SvgSourceError {
fn from(e: usvg::Error) -> Self {
Self::SvgError(e)
}
}

impl From<std::io::Error> for SvgSourceError {
fn from(e: std::io::Error) -> Self {
Self::IoError(e)
}
}

0 comments on commit 6ec1c6d

Please sign in to comment.