Skip to content

Commit

Permalink
feat: error code UnrecognizedBuffer changed to UnsupportedImage
Browse files Browse the repository at this point in the history
Explicitly return which formats are currently supported by resvg.
  • Loading branch information
yisibl committed Nov 16, 2022
1 parent 903e48a commit 78a30d9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 50 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ pathfinder_geometry = "0.5.1"
pathfinder_content = { version = "0.5.0", default-features = false }
pathfinder_simd = { version = "0.5.1", features = ["pf-no-simd"] }
futures = "0.3.21"
# infer = "0.11.0"
# strum_macros = "0.24"

[target.'cfg(all(not(all(target_os = "linux", target_arch = "aarch64", target_env = "musl")), not(all(target_os = "windows", target_arch = "aarch64")), not(target_arch = "wasm32")))'.dependencies]
mimalloc-rust = { version = "0.2" }
Expand Down
1 change: 0 additions & 1 deletion __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ test('Use href to load a JPG image without alpha', async (t) => {
font: {
loadSystemFonts: false,
},
logLevel: 'debug',
})
const resolved = await Promise.all(
resvg.imagesToResolve().map(async (url) => {
Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub enum Error {
ZeroSized,
#[error("Input must be string or Uint8Array")]
InvalidInput,
#[error("Unrecognized image buffer")]
UnrecognizedBuffer,
#[error("Unsupported image types (currently resvg only supports PNG, JPEG and GIF)")]
UnsupportedImage,
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down
75 changes: 30 additions & 45 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::sync::Arc;
// use strum_macros::Display;

#[cfg(not(target_arch = "wasm32"))]
use napi::bindgen_prelude::{
Expand Down Expand Up @@ -395,49 +394,6 @@ impl Resvg {
}
}

// #[derive(Display, Debug)]
// pub enum MimeType {
// #[strum(serialize = "image/png")]
// Png,

// #[strum(serialize = "image/jpeg")]
// Jpeg,

// #[strum(serialize = "image/gif")]
// Gif,

// #[strum(serialize = "Not Support")]
// NotSupported,
// }

pub enum MimeType {
Png,
Jpeg,
Gif,
}

impl MimeType {
pub fn parse(buffer: &[u8]) -> Result<Self, Error> {
if buffer.len() < 4 {
return Err(Error::UnrecognizedBuffer);
}
Ok(match &buffer[0..4] {
[0x89, 0x50, 0x4E, 0x47] => MimeType::Png,
[0xFF, 0xD8, 0xFF, _] => MimeType::Jpeg,
[0x47, 0x49, 0x46, _] => MimeType::Gif,
_ => return Err(Error::UnrecognizedBuffer),
})
}

pub fn mime_type(&self) -> &'static str {
match self {
MimeType::Png => "image/png",
MimeType::Gif => "image/gif",
_ => "image/jpeg",
}
}
}

impl Resvg {
fn node_bbox(&self, node: usvg::Node) -> Option<RectF> {
let transform = node.borrow().transform();
Expand Down Expand Up @@ -626,7 +582,6 @@ impl Resvg {
let resolver = usvg::ImageHrefResolver::default_data_resolver();
let options = self.js_options.to_usvg_options();
let mime = MimeType::parse(&buffer)?.mime_type().to_string();
// debug!("🤓 Image mime is: '{}' .", mime);

for node in self.tree.root.descendants() {
if let NodeKind::Image(i) = &mut *node.borrow_mut() {
Expand Down Expand Up @@ -692,3 +647,33 @@ fn points_to_rect(min: usvg::Point<f64>, max: usvg::Point<f64>) -> RectF {
let max = Vector2F::new(max.x as f32, max.y as f32);
RectF::new(min, max - min)
}

// Detects the file type by magic number.
// Currently resvg only supports the following types of files.
pub enum MimeType {
Png,
Jpeg,
Gif,
}

impl MimeType {
pub fn parse(buffer: &[u8]) -> Result<Self, Error> {
if buffer.len() < 4 {
return Err(Error::UnsupportedImage);
}
Ok(match &buffer[0..4] {
[0x89, 0x50, 0x4E, 0x47] => MimeType::Png,
[0xFF, 0xD8, 0xFF, _] => MimeType::Jpeg,
[0x47, 0x49, 0x46, _] => MimeType::Gif,
_ => return Err(Error::UnsupportedImage),
})
}

pub fn mime_type(&self) -> &'static str {
match self {
MimeType::Png => "image/png",
MimeType::Gif => "image/gif",
_ => "image/jpeg",
}
}
}
Binary file modified wasm/index_bg.wasm
Binary file not shown.

0 comments on commit 78a30d9

Please sign in to comment.