Skip to content

Commit

Permalink
feat: Wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed Aug 6, 2023
1 parent 381e44f commit ce3a162
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
3 changes: 1 addition & 2 deletions __test__/wasm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ test('should return undefined if bbox is invalid', (t) => {

test('should render using font buffer provided by options', async (t) => {
const svg = `<svg width='480' height='150' viewBox='-20 -80 550 100' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>
<text x='0' y='0' font-size='100' fill='#000000'>Font Buffer</text>
<text x='0' y='0' font-size='100' fill='#000'>Font Buffer</text>
</svg>`

const pacificoBuffer = await fs.readFile(join(__dirname, './Pacifico-Regular.ttf'))
Expand All @@ -339,7 +339,6 @@ test('should render using font buffer provided by options', async (t) => {
const options = {
font: {
fontsBuffers: [pacificoBuffer],
loadSystemFonts: false,
defaultFontFamily: 'Pacifico',
},
}
Expand Down
71 changes: 61 additions & 10 deletions src/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use crate::options::*;
use resvg::usvg_text_layout::fontdb::{Database, Language};


#[cfg(not(target_arch = "wasm32"))]
use log::{debug, warn};

Expand Down Expand Up @@ -34,7 +33,7 @@ pub fn load_fonts(font_options: &JsFontOptions) -> Database {
fontdb.load_fonts_dir(path);
}

// 加载系统字体,Wasm 不支持
// 加载系统字体
// https://github.com/RazrFalcon/fontdb/blob/052d74b9eb45f2c4f446846a53f33bd965e2662d/src/lib.rs#L261
if font_options.load_system_fonts {
fontdb.load_system_fonts();
Expand All @@ -51,32 +50,38 @@ pub fn load_fonts(font_options: &JsFontOptions) -> Database {
fontdb
}

/// Loads fonts.
/// Loads fonts in Wasm.
#[cfg(target_arch = "wasm32")]
pub fn load_wasm_fonts(
font_options: &JsFontOptions,
fonts_buffers: Option<js_sys::Array>,
fontdb: &mut Database,
) -> Result<(), js_sys::Error> {
if let Some(fonts_buffers) = fonts_buffers {
if let Some(ref fonts_buffers) = fonts_buffers {
for font in fonts_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);
}
}

set_font_families(font_options, fontdb);
set_wasm_font_families(font_options, fontdb, fonts_buffers);

Ok(())
}

#[cfg(not(target_arch = "wasm32"))]
fn set_font_families(font_options: &JsFontOptions, fontdb: &mut Database) {
let mut default_font_family = font_options.default_font_family.clone();

// 当默认字体为空时,尝试直接从 font_files 中加载读取字体名称,然后设置到默认的 font-family 中
if font_options.default_font_family.to_string().trim().is_empty() {
if font_options.font_files.len() > 0 || font_options.font_dirs.len() > 0 {
if font_options
.default_font_family
.to_string()
.trim()
.is_empty()
{
if font_options.font_files.len() > 0 || font_options.font_dirs.len() > 0 {
for face in fontdb.faces() {
// debug!("font_id = {}, post_script_name = {} ", face.id, face.post_script_name);

Expand All @@ -91,12 +96,12 @@ fn set_font_families(font_options: &JsFontOptions, fontdb: &mut Database) {
break;
}


// 遍历所有加载的字体
// for face in fontdb.faces() {
// if let Source::File(ref path) = &face.source {
// // 如果 path.display() 中包含了 font_options.font_files 中的字体路径,则设置为默认字体,并打印出 font_files 中的路径
// // debug!("font_id = {}, post_script_name = {} ", face.id, face.post_script_name);
// // 如果 path.display() 中包含了 font_options.font_files
// 中的字体路径,则设置为默认字体,并打印出 font_files 中的路径 //
// debug!("font_id = {}, post_script_name = {} ", face.id, face.post_script_name);

// // 匹配到 font_files 中的字体后,设置默认字体
// for font_file in &font_options.font_files {
Expand Down Expand Up @@ -126,7 +131,53 @@ fn set_font_families(font_options: &JsFontOptions, fontdb: &mut Database) {
// debug!("默认字体匹配到了 = {} ", default_font_family);
// }
// });
} else {
default_font_family = "Arial".to_string();
}

fontdb.set_serif_family(&default_font_family);
fontdb.set_sans_serif_family(&default_font_family);
fontdb.set_cursive_family(&default_font_family);
fontdb.set_fantasy_family(&default_font_family);
fontdb.set_monospace_family(&default_font_family);
} else {
fontdb.set_serif_family(&font_options.default_font_family);
fontdb.set_sans_serif_family(&font_options.default_font_family);
fontdb.set_cursive_family(&font_options.default_font_family);
fontdb.set_fantasy_family(&font_options.default_font_family);
fontdb.set_monospace_family(&font_options.default_font_family);
}

#[cfg(not(target_arch = "wasm32"))]
find_and_debug_font_path(fontdb, default_font_family.as_str())
}

#[cfg(target_arch = "wasm32")]
fn set_wasm_font_families(
font_options: &JsFontOptions,
fontdb: &mut Database,
fonts_buffers: Option<js_sys::Array>,
) {
let mut default_font_family = font_options.default_font_family.clone();

// 当默认字体为空时,尝试直接从 font_files 中加载读取字体名称,然后设置到默认的 font-family 中
if font_options
.default_font_family
.to_string()
.trim()
.is_empty()
{
if let Some(_fonts_buffers) = fonts_buffers {
for face in fontdb.faces() {
let new_family = face
.families
.iter()
.find(|f| f.1 == Language::English_UnitedStates)
.unwrap_or(&face.families[0]);

default_font_family = new_family.0.clone();
break;
}
} else {
default_font_family = "Arial".to_string();
}
Expand Down
Binary file modified wasm/index_bg.wasm
Binary file not shown.

0 comments on commit ce3a162

Please sign in to comment.