mirror of
https://github.com/typst/typst
synced 2025-05-19 03:25:27 +08:00
Don't try to reload faces over and over
This commit is contained in:
parent
642e149464
commit
ed1197a3db
13
src/font.rs
13
src/font.rs
@ -34,6 +34,7 @@ impl FaceId {
|
|||||||
/// Storage for loaded and parsed font faces.
|
/// Storage for loaded and parsed font faces.
|
||||||
pub struct FontStore {
|
pub struct FontStore {
|
||||||
loader: Arc<dyn Loader>,
|
loader: Arc<dyn Loader>,
|
||||||
|
failed: Vec<bool>,
|
||||||
faces: Vec<Option<Face>>,
|
faces: Vec<Option<Face>>,
|
||||||
families: BTreeMap<String, Vec<FaceId>>,
|
families: BTreeMap<String, Vec<FaceId>>,
|
||||||
buffers: HashMap<FileHash, Arc<Vec<u8>>>,
|
buffers: HashMap<FileHash, Arc<Vec<u8>>>,
|
||||||
@ -43,17 +44,20 @@ impl FontStore {
|
|||||||
/// Create a new, empty font store.
|
/// Create a new, empty font store.
|
||||||
pub fn new(loader: Arc<dyn Loader>) -> Self {
|
pub fn new(loader: Arc<dyn Loader>) -> Self {
|
||||||
let mut faces = vec![];
|
let mut faces = vec![];
|
||||||
|
let mut failed = vec![];
|
||||||
let mut families = BTreeMap::<String, Vec<FaceId>>::new();
|
let mut families = BTreeMap::<String, Vec<FaceId>>::new();
|
||||||
|
|
||||||
for (i, info) in loader.faces().iter().enumerate() {
|
for (i, info) in loader.faces().iter().enumerate() {
|
||||||
let id = FaceId(i as u32);
|
let id = FaceId(i as u32);
|
||||||
faces.push(None);
|
faces.push(None);
|
||||||
|
failed.push(false);
|
||||||
families.entry(info.family.to_lowercase()).or_default().push(id);
|
families.entry(info.family.to_lowercase()).or_default().push(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
loader,
|
loader,
|
||||||
faces,
|
faces,
|
||||||
|
failed,
|
||||||
families,
|
families,
|
||||||
buffers: HashMap::new(),
|
buffers: HashMap::new(),
|
||||||
}
|
}
|
||||||
@ -95,12 +99,16 @@ impl FontStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let id = best?;
|
let id = best?;
|
||||||
|
|
||||||
// Load the face if it's not already loaded.
|
|
||||||
let idx = id.0 as usize;
|
let idx = id.0 as usize;
|
||||||
let slot = &mut self.faces[idx];
|
let slot = &mut self.faces[idx];
|
||||||
|
if self.failed[idx] {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load the face if it's not already loaded.
|
||||||
if slot.is_none() {
|
if slot.is_none() {
|
||||||
let FaceInfo { ref path, index, .. } = infos[idx];
|
let FaceInfo { ref path, index, .. } = infos[idx];
|
||||||
|
self.failed[idx] = true;
|
||||||
|
|
||||||
// Check the buffer cache since multiple faces may
|
// Check the buffer cache since multiple faces may
|
||||||
// refer to the same data (font collection).
|
// refer to the same data (font collection).
|
||||||
@ -115,6 +123,7 @@ impl FontStore {
|
|||||||
|
|
||||||
let face = Face::new(Arc::clone(buffer), index)?;
|
let face = Face::new(Arc::clone(buffer), index)?;
|
||||||
*slot = Some(face);
|
*slot = Some(face);
|
||||||
|
self.failed[idx] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(id)
|
Some(id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user