mirror of
https://github.com/typst/typst
synced 2025-08-15 15:38:33 +08:00
Memoize font creation
This commit is contained in:
parent
fe15f0c4e9
commit
4fdda7d5d0
@ -68,16 +68,7 @@ fn convert_font(
|
|||||||
if let Some(font) = gc.fonts_forward.get(&typst_font) {
|
if let Some(font) = gc.fonts_forward.get(&typst_font) {
|
||||||
Ok(font.clone())
|
Ok(font.clone())
|
||||||
} else {
|
} else {
|
||||||
let font_data: Arc<dyn AsRef<[u8]> + Send + Sync> =
|
let font = build_font(typst_font.clone())?;
|
||||||
Arc::new(typst_font.data().clone());
|
|
||||||
let font =
|
|
||||||
match krilla::text::Font::new(font_data.into(), typst_font.index(), true) {
|
|
||||||
None => {
|
|
||||||
let font_str = display_font(&typst_font);
|
|
||||||
bail!(Span::detached(), "failed to process font {font_str}");
|
|
||||||
}
|
|
||||||
Some(f) => f,
|
|
||||||
};
|
|
||||||
|
|
||||||
gc.fonts_forward.insert(typst_font.clone(), font.clone());
|
gc.fonts_forward.insert(typst_font.clone(), font.clone());
|
||||||
gc.fonts_backward.insert(font.clone(), typst_font.clone());
|
gc.fonts_backward.insert(font.clone(), typst_font.clone());
|
||||||
@ -86,6 +77,20 @@ fn convert_font(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[comemo::memoize]
|
||||||
|
fn build_font(typst_font: Font) -> SourceResult<krilla::text::Font> {
|
||||||
|
let font_data: Arc<dyn AsRef<[u8]> + Send + Sync> =
|
||||||
|
Arc::new(typst_font.data().clone());
|
||||||
|
|
||||||
|
match krilla::text::Font::new(font_data.into(), typst_font.index(), true) {
|
||||||
|
None => {
|
||||||
|
let font_str = display_font(&typst_font);
|
||||||
|
bail!(Span::detached(), "failed to process font {font_str}");
|
||||||
|
}
|
||||||
|
Some(f) => Ok(f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(TransparentWrapper)]
|
#[derive(TransparentWrapper)]
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
struct PdfGlyph(Glyph);
|
struct PdfGlyph(Glyph);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user