Skip to content

Commit

Permalink
Merge branch 'main' into upgrade-resvg-34
Browse files Browse the repository at this point in the history
  • Loading branch information
zimond authored Sep 26, 2023
2 parents 08d7271 + 24b9f07 commit e2c0d54
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 51 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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"
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" }
Expand Down Expand Up @@ -54,3 +55,4 @@ codegen-units = 1

[patch.crates-io]
resvg = { git = "https://github.com/zimond/resvg", rev = "3495d870" }
woff2 = { git="https://github.com/yisibl/woff2-rs", branch="fix-total-compressed-size" }
21 changes: 21 additions & 0 deletions __test__/wasm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,27 @@ test('should be load custom multiple fontBuffers', async (t) => {
t.is(originPixels.join(',').match(/0,0,255/g)?.length, 8911)
})

test('should be load custom WOFF2 font', async (t) => {
const svg = `
<svg xmlns="http://www.w3.org/2000/svg" width="500" height="200" viewBox="0 0 500 200">
<text fill="blue" font-size="60">
<tspan x="40" y="100">Hello resvg-js</tspan>
</text>
</svg>
`
const pacificoBuffer = await fs.readFile(join(__dirname, '../wasm/fonts/', 'Pacifico-Regular.woff2'))
const resvg = new Resvg(svg, {
font: {
fontBuffers: [pacificoBuffer],
},
})
const pngData = resvg.render()
const originPixels = Array.from(pngData.pixels)

// Find the number of blue `rgb(0,255,255)`pixels
t.is(originPixels.join(',').match(/0,0,255/g)?.length, 6067)
})

test('should generate a 80x80 png and opaque', async (t) => {
const svg = `<svg width="200px" height="200px" viewBox="0 0 200 200" version="1.1" xmlns="http://www.w3.org/2000/svg">
<rect fill="green" x="0" y="0" width="100" height="100"></rect>
Expand Down
11 changes: 10 additions & 1 deletion src/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use resvg::usvg::fontdb::{Family, Query, Source};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::JsCast;

#[cfg(target_arch = "wasm32")]
use woff2::decode::{convert_woff2_to_ttf, is_woff2};

/// Loads fonts.
#[cfg(not(target_arch = "wasm32"))]
pub fn load_fonts(font_options: &JsFontOptions) -> Database {
Expand Down Expand Up @@ -62,7 +65,13 @@ pub fn load_wasm_fonts(
for font in font_buffers.values().into_iter() {
let raw_font = font?;
let font_data = raw_font.dyn_into::<js_sys::Uint8Array>()?.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);
}
}

Expand Down
Binary file added wasm/fonts/Pacifico-Regular.woff2
Binary file not shown.
Loading

0 comments on commit e2c0d54

Please sign in to comment.