Skip to content

Commit

Permalink
feat: improve custom loaded fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed Feb 14, 2023
1 parent 70dd96b commit 3a1d771
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
# usvg-writer = { path = "../resvg/usvg-writer" }

[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 @@ -57,3 +58,4 @@ codegen-units = 1

[patch.crates-io]
resvg = { git = "https://github.com/zimond/resvg", rev = "6201182c" }
# resvg = { path = "../resvg" }
32 changes: 32 additions & 0 deletions example/text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { promises } = require('fs')
const { join } = require('path')
const { performance } = require('perf_hooks')

const { Resvg } = require('../index')

async function main() {
const svg = `
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200">
<text fill="blue" font-family="serif" font-size="120">
<tspan x="40" y="143">水</tspan>
</text>
</svg>
`
const t = performance.now()
const resvg = new Resvg(svg, {
// background: 'pink',
font: {
fontFiles: ['./example/SourceHanSerifCN-Light-subset.ttf'], // Load custom fonts.
loadSystemFonts: false, // It will be faster to disable loading system fonts.
// defaultFontFamily: 'SourceHanSerifCN-Light',
},
logLevel: 'debug', // Default Value: error
})
const pngData = resvg.render()
const pngBuffer = pngData.asPng()
console.info('✨ Done in', performance.now() - t, 'ms')

await promises.writeFile(join(__dirname, './text2-out.png'), pngBuffer)
}

main()
Binary file added example/text2-out.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 35 additions & 17 deletions src/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::options::*;
use log::{debug, warn};

#[cfg(not(target_arch = "wasm32"))]
use resvg::usvg_text_layout::fontdb::{Database, Family, Query, Source};
use resvg::usvg_text_layout::fontdb::{Database, Family, Language, Query, Source};

/// Loads fonts.
#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -36,22 +36,40 @@ pub fn load_fonts(font_options: &JsFontOptions) -> Database {
fontdb.load_fonts_dir(path);
}

// Set generic font families
// - `serif` - Times New Roman
// - `sans-serif` - Arial
// - `cursive` - Comic Sans MS
// - `fantasy` - Impact (Papyrus on macOS)
// - `monospace` - Courier New
fontdb.set_serif_family(&font_options.serif_family);
fontdb.set_sans_serif_family(&font_options.sans_serif_family);
fontdb.set_cursive_family(&font_options.cursive_family);
fontdb.set_fantasy_family(&font_options.fantasy_family);
fontdb.set_monospace_family(&font_options.monospace_family);
debug!(
"Loaded {} font faces in {}ms.",
fontdb.len(),
now.elapsed().as_micros() as f64 / 1000.0
);
let mut default_font_family = font_options.default_font_family.clone();
// 当默认字体为空时,尝试直接从 font_files 中加载读取字体名称,然后设置到默认的 font-family 中
// TODO: 判断只有 font_files 没有 default_font_family 选项的情况
if !font_options.default_font_family.is_empty() && font_options.font_files.len() > 0 {
fontdb.faces().into_iter().for_each(|face| {
let new_family = face
.families
.iter()
.find(|f| f.1 == Language::English_UnitedStates)
.unwrap();
default_font_family = new_family.0.clone();
});

// If a default font family exists, set all other families to that family.
// This prevents fonts from not being rendered in SVG.
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 {
// Set generic font families
// - `serif` - Times New Roman
// - `sans-serif` - Arial
// - `cursive` - Comic Sans MS
// - `fantasy` - Impact (Papyrus on macOS)
// - `monospace` - Courier New
fontdb.set_serif_family(&font_options.serif_family);
fontdb.set_sans_serif_family(&font_options.sans_serif_family);
fontdb.set_cursive_family(&font_options.cursive_family);
fontdb.set_fantasy_family(&font_options.fantasy_family);
fontdb.set_monospace_family(&font_options.monospace_family);
}
debug!("默认字体 = {} ", font_options.default_font_family,);

// 查找指定字体的路径
let font_family: &str = &font_options.default_font_family;
Expand Down

0 comments on commit 3a1d771

Please sign in to comment.