diff --git a/Cargo.toml b/Cargo.toml index 5ddcde28..d8a5a2e8 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/__test__/index.spec.ts b/__test__/index.spec.ts index 3ba77607..8a5e089d 100755 --- a/__test__/index.spec.ts +++ b/__test__/index.spec.ts @@ -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) => { diff --git a/src/error.rs b/src/error.rs index 309a0d7f..d0a24a2a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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"))] diff --git a/src/lib.rs b/src/lib.rs index 779201b3..c085e33e 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::{ @@ -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 { - 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 { let transform = node.borrow().transform(); @@ -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() { @@ -692,3 +647,33 @@ fn points_to_rect(min: usvg::Point, max: usvg::Point) -> 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 { + 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", + } + } +} diff --git a/wasm/index_bg.wasm b/wasm/index_bg.wasm index 3d665316..dcacc7e7 100644 Binary files a/wasm/index_bg.wasm and b/wasm/index_bg.wasm differ