perf(image): lazy load the Fontdb Database

This commit is contained in:
LongYinan 2023-03-12 15:26:36 +08:00
parent 5a8a6e0fd0
commit 68400a9707
No known key found for this signature in database
GPG Key ID: C3666B7FC82ADAD7
2 changed files with 9 additions and 4 deletions

View File

@ -35,7 +35,7 @@ image = { version = "0.24", default-features = false, features = [
"openexr",
] }
jpeg-decoder = "0.3"
libavif = { version = "=0.10.1", default-features = false, features = [
libavif = { version = "0.11", default-features = false, features = [
"codec-aom",
] }
libc = "0.2"
@ -47,6 +47,7 @@ napi-derive = { version = "2", default-features = false, features = [
] }
num-complex = "0.4"
num_cpus = "1"
once_cell = "1"
png = "0.17"
rexif = "0.7"
rgb = "0.8"

View File

@ -20,6 +20,12 @@ use crate::{
png::PngEncodeOptions,
};
static FONT_DB: once_cell::sync::Lazy<Database> = once_cell::sync::Lazy::new(|| {
let mut fontdb = Database::new();
fontdb.load_system_fonts();
fontdb
});
pub enum EncodeOptions {
Png(PngEncodeOptions),
Jpeg(u32),
@ -669,8 +675,6 @@ impl Transformer {
input: Either<String, Buffer>,
background: Option<String>,
) -> Result<Transformer> {
let mut fontdb = Database::new();
fontdb.load_system_fonts();
let options = Options::default();
let mut tree = match input {
@ -678,7 +682,7 @@ impl Transformer {
Either::B(b) => usvg::Tree::from_data(b.as_ref(), &options),
}
.map_err(|err| Error::from_reason(format!("{err}")))?;
tree.convert_text(&fontdb);
tree.convert_text(&*FONT_DB);
let mut size = tree.size.to_screen_size();
let min_svg_size = 1000;