Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove the infer crate #165

Merged
merged 3 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ crate-type = ["cdylib"]

[dependencies]
env_logger = "0.9.1"
fontdb = "0.9.1"
fontdb = "=0.9.3"
log = "0.4"

serde = { version = "1", features = ["derive"] }
Expand All @@ -22,7 +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"

[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
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
35 changes: 32 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,8 @@ impl Resvg {
fn resolve_image_inner(&self, href: String, buffer: Vec<u8>) -> Result<(), Error> {
let resolver = usvg::ImageHrefResolver::default_data_resolver();
let options = self.js_options.to_usvg_options();
let mime = infer::get(&buffer)
.ok_or_else(|| Error::UnrecognizedBuffer)?
.to_string();
let mime = MimeType::parse(&buffer)?.mime_type().to_string();

for node in self.tree.root.descendants() {
if let NodeKind::Image(i) = &mut *node.borrow_mut() {
let matched = if let ImageKind::RAW(_, _, data) = &mut i.kind {
Expand Down Expand Up @@ -648,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",
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

干嘛把这段代码挪来挪去?增加一个没啥意义的diff

Binary file modified wasm/index_bg.wasm
Binary file not shown.