From bad1e6c76c0373668f8d2b0b37bf5a1b31ef844b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E4=B8=9D?= Date: Tue, 14 Feb 2023 21:23:59 +0800 Subject: [PATCH] feat: improve custom loaded fonts --- Cargo.toml | 2 ++ example/text.js | 32 ++++++++++++++++++++++++++++++++ src/fonts.rs | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 77b50ce9..ef92967a 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" } +# 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" } @@ -57,3 +58,4 @@ codegen-units = 1 [patch.crates-io] resvg = { git = "https://github.com/zimond/resvg", rev = "6201182c" } +# resvg = { path = "../resvg" } diff --git a/example/text.js b/example/text.js index 29104e98..2e37515c 100644 --- a/example/text.js +++ b/example/text.js @@ -30,3 +30,35 @@ async function main() { } main() +const { promises } = require('fs') +const { join } = require('path') +const { performance } = require('perf_hooks') + +const { Resvg } = require('../index') + +async function main() { + const 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() diff --git a/src/fonts.rs b/src/fonts.rs index 52210acc..7d525434 100644 --- a/src/fonts.rs +++ b/src/fonts.rs @@ -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"))] @@ -62,6 +62,40 @@ pub fn load_fonts(font_options: &JsFontOptions) -> Database { 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;