Fix image reader (#512)

* ignore table

* ignore unsupported image

* fix

* fix

* fix

* fix

Co-authored-by: bokuweb <bokuweb@bokuwebnombp.lan>
main
bokuweb 2022-07-22 11:46:07 +09:00 committed by GitHub
parent 3a875e1d40
commit 2e8a7731d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 10 deletions

11
Cargo.lock generated
View File

@ -102,6 +102,16 @@ dependencies = [
"winapi",
]
[[package]]
name = "console_error_panic_hook"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
dependencies = [
"cfg-if 1.0.0",
"wasm-bindgen",
]
[[package]]
name = "crc32fast"
version = "1.3.2"
@ -167,6 +177,7 @@ dependencies = [
name = "docx-wasm"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"docx-rs",
"wasm-bindgen",
]

View File

@ -230,14 +230,15 @@ impl Docx {
path: impl Into<String>,
buf: Vec<u8>,
) -> Self {
let dimg = image::load_from_memory(&buf).expect("Should load image from memory.");
let mut png = std::io::Cursor::new(vec![]);
// For now only png supported
dimg.write_to(&mut png, ImageFormat::Png)
.expect("Unable to write dynamic image");
if let Ok(dimg) = image::load_from_memory(&buf) {
let mut png = std::io::Cursor::new(vec![]);
// For now only png supported
dimg.write_to(&mut png, ImageFormat::Png)
.expect("Unable to write dynamic image");
self.images
.push((id.into(), path.into(), Image(buf), Png(png.into_inner())));
self.images
.push((id.into(), path.into(), Image(buf), Png(png.into_inner())));
}
self
}
@ -987,8 +988,9 @@ fn push_comment_and_comment_extended(
comments.push(c.get_comment());
let comment_extended = CommentExtended::new(para_id);
if let Some(parent_comment_id) = comment.parent_comment_id {
let parent_para_id = comment_map.get(&parent_comment_id).unwrap().clone();
comments_extended.push(comment_extended.parent_paragraph_id(parent_para_id));
if let Some(parent_para_id) = comment_map.get(&parent_comment_id) {
comments_extended.push(comment_extended.parent_paragraph_id(parent_para_id.clone()));
}
} else {
comments_extended.push(comment_extended);
}

View File

@ -11,6 +11,7 @@ impl ElementReader for TableCell {
let mut cell = TableCell::new();
loop {
let e = r.next();
match e {
Ok(XmlEvent::StartElement {
attributes, name, ..

View File

@ -11,4 +11,5 @@ crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2.78"
console_error_panic_hook = "0.1.7"
docx-rs= { path = "../docx-core" }

View File

@ -1,6 +1,6 @@
{
"name": "docx-wasm",
"version": "0.0.273",
"version": "0.0.274-image-test8",
"main": "dist/node/index.js",
"browser": "dist/web/index.js",
"author": "bokuweb <bokuweb12@gmail.com>",

View File

@ -1,6 +1,8 @@
use super::*;
use wasm_bindgen::prelude::*;
extern crate console_error_panic_hook;
#[wasm_bindgen]
#[derive(Debug)]
pub struct Docx(docx_rs::Docx);
@ -8,6 +10,8 @@ pub struct Docx(docx_rs::Docx);
#[wasm_bindgen]
#[allow(non_snake_case)]
pub fn createDocx() -> Docx {
use std::panic;
panic::set_hook(Box::new(console_error_panic_hook::hook));
Docx(docx_rs::Docx::new())
}