Support png image converter (#494)

* fix

* 0.0.266

* 0.0.267

* update

Co-authored-by: bokuweb <bokuweb@bokuwebnombp.lan>
main
bokuweb 2022-06-14 18:11:52 +09:00 committed by GitHub
parent 1f0877bbad
commit fc2760c31a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 6 deletions

12
Cargo.lock generated
View File

@ -220,6 +220,7 @@ dependencies = [
"num-rational",
"num-traits",
"png",
"tiff",
]
[[package]]
@ -497,6 +498,17 @@ dependencies = [
"syn",
]
[[package]]
name = "tiff"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cfada0986f446a770eca461e8c6566cb879682f7d687c8348aa0c857bd52286"
dependencies = [
"flate2",
"jpeg-decoder",
"weezl",
]
[[package]]
name = "ts-rs"
version = "6.1.1"

View File

@ -27,7 +27,7 @@ zip = { version = "0.6.2", default-features = false, features = ["deflate"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
base64 = "0.13.0"
image = { version = "0.24.2", default-features = false, features=["gif", "jpeg", "png", "bmp"] }
image = { version = "0.24.2", default-features = false, features=["gif", "jpeg", "png", "bmp", "tiff"] }
ts-rs = "6.1"
[dev-dependencies]

View File

@ -36,6 +36,7 @@ mod xml_docx;
pub(crate) use build_xml::BuildXML;
pub(crate) use history_id::HistoryId;
use image::ImageFormat;
pub(crate) use paragraph_id::*;
pub(crate) use paragraph_property_change_id::ParagraphPropertyChangeId;
pub(crate) use pic_id::*;
@ -73,6 +74,9 @@ use serde::{ser, Serialize};
#[derive(Debug, Clone)]
pub struct Image(pub Vec<u8>);
#[derive(Debug, Clone)]
pub struct Png(pub Vec<u8>);
pub type ImageIdAndPath = (String, String);
pub type ImageIdAndBuf = (String, Vec<u8>);
@ -86,6 +90,16 @@ impl ser::Serialize for Image {
}
}
impl ser::Serialize for Png {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
let base64 = base64::display::Base64Display::with_config(&*self.0, base64::STANDARD);
serializer.collect_str(&base64)
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Docx {
@ -111,7 +125,7 @@ pub struct Docx {
// reader only
pub themes: Vec<Theme>,
// reader only
pub images: Vec<(String, String, Image)>,
pub images: Vec<(String, String, Image, Png)>,
}
impl Default for Docx {
@ -214,7 +228,14 @@ impl Docx {
path: impl Into<String>,
buf: Vec<u8>,
) -> Self {
self.images.push((id.into(), path.into(), Image(buf)));
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");
self.images
.push((id.into(), path.into(), Image(buf), Png(png.into_inner())));
self
}

View File

@ -47,8 +47,8 @@ export type DocxJSON = {
webSettings: WebSettingsJSON;
fontTable: {};
themes: ThemeJSON[];
//(id, path, base64 encoded image data)
images: [string, string, string][];
//(id, path, base64 encoded original image data, base64 encoded png image data)
images: [string, string, string, string][];
};
export type SettingsJSON = {

View File

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

File diff suppressed because one or more lines are too long