diff --git a/Cargo.toml b/Cargo.toml index 7d26e480..c70ef844 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ pathfinder_content = { version = "0.5.0", default-features = false } pathfinder_simd = { version = "0.5.1", features = ["pf-no-simd"] } futures = "0.3.21" usvg-writer = { git = "https://github.com/zimond/resvg", rev = "6201182c" } +woff2 = "0.3.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" } @@ -57,3 +58,4 @@ codegen-units = 1 [patch.crates-io] resvg = { git = "https://github.com/zimond/resvg", rev = "6201182c" } +woff2 = { git="https://github.com/yisibl/woff2-rs", branch="fix-total-compressed-size" } diff --git a/example/SourceHanSerifSC-Regular.otf b/example/SourceHanSerifSC-Regular.otf new file mode 100644 index 00000000..0dd496aa Binary files /dev/null and b/example/SourceHanSerifSC-Regular.otf differ diff --git a/example/SourceHanSerifSC-Regular.woff2 b/example/SourceHanSerifSC-Regular.woff2 new file mode 100644 index 00000000..9a559efc Binary files /dev/null and b/example/SourceHanSerifSC-Regular.woff2 differ diff --git a/example/text2-out.png b/example/text2-out.png index 0fed6e5e..38045c9b 100644 Binary files a/example/text2-out.png and b/example/text2-out.png differ diff --git a/src/fonts.rs b/src/fonts.rs index c7bfa46c..1e48c265 100644 --- a/src/fonts.rs +++ b/src/fonts.rs @@ -13,6 +13,7 @@ use resvg::usvg_text_layout::fontdb::{Family, Query, Source}; #[cfg(target_arch = "wasm32")] use wasm_bindgen::JsCast; +use woff2::decode::{convert_woff2_to_ttf, is_woff2}; /// Loads fonts. #[cfg(not(target_arch = "wasm32"))] @@ -87,7 +88,13 @@ pub fn load_fonts( for font in fonts_buffers.values().into_iter() { let raw_font = font?; let font_data = raw_font.dyn_into::()?.to_vec(); - fontdb.load_font_data(font_data); + + let font_buffer = if is_woff2(&font_data) { + convert_woff2_to_ttf(&mut std::io::Cursor::new(font_data)).unwrap() + } else { + font_data + }; + fontdb.load_font_data(font_buffer); } } diff --git a/wasm/index.html b/wasm/index.html index b910dc0b..9c3c2db7 100644 --- a/wasm/index.html +++ b/wasm/index.html @@ -162,7 +162,18 @@ const output = document.getElementById('output') const svgElement = document.querySelector('#input-svg') const inputSVG = svgElement.value.trim() - let resvgOpts = {} + + const font = await fetch('http://192.168.50.114:8000/example/SourceHanSerifSC-Regular.woff2') + const buffer = new Uint8Array(await font.arrayBuffer()) + console.log('font buffer size', buffer.length) + + let resvgOpts = { + font: { + fontsBuffers: [buffer], + // OpenType 中定义的 Font Family name,也就是 nameID=1 + defaultFontFamily: 'Source Han Serif SC', + }, + } await svg2png(inputSVG, resvgOpts) async function svg2png(svgString, opts, hasCrop) { @@ -296,25 +307,20 @@

resvg-js playground

-
diff --git a/wasm/index_bg.wasm b/wasm/index_bg.wasm index 8b87810b..a0383094 100644 Binary files a/wasm/index_bg.wasm and b/wasm/index_bg.wasm differ