Files
vectorcraft/crates/text/src/fontdb/cache.rs
T
BFlat 6f37bd839c VectorCraft on the craft UI framework (#8800)
VectorCraft's UI moves onto the shared craft UI framework in storytold/craft-libs (`next`; the framework side is storytold/craft-libs#2809), as PhotoCraft's did. The app keeps its engine, document model, file formats, commands and automation; its windows, panels, bars, dialogs and canvas are now built and driven by the framework: shell, docking and panels, hosted dialogs, keyboard, focus, IME and pointer routing, and theme tokens.

About 264,000 lines of VectorCraft's own egui UI code are removed and about 154,000 added. craft-libs is pinned at bf62d509f (in `next`), the egui fork at f5025284.

Tests that drove the old UI were rewritten against the port where only their premise changed, with their assertions kept. The UI tests still failing at merge, and their follow-ups, are listed in the pull request.
2026-11-24 08:25:24 +07:00

72 lines
2.5 KiB
Rust

use super::{Arc, ColourContent, GlyphImage, HashMap, IMAGE_CACHE_BYTES, IMAGE_CACHE_MAX};
/// Maps the family and style [`FontDb::canonical`] was asked for to its answer, for names that
/// are neither a loaded nor an installed family.
pub(super) type CanonicalNames = HashMap<(String, String), (String, String)>;
/// [`FontDb::glyph_image`]'s answers by face and glyph.
pub(super) type GlyphImages = GlyphCache<(u32, u32), Arc<GlyphImage>>;
/// [`FontDb::glyph_colour`]'s answers by face and glyph, and the text's fill and stroke colours for
/// glyphs that paint with them.
pub(super) type GlyphColours = GlyphCache<(u32, u32, Option<TextColours>), ColourContent>;
/// The text's fill and stroke colours, as a cache key ([`Color::key`]).
pub(super) type TextColours = ([u32; 5], Option<[u32; 5]>);
/// What a glyph's colour content (an image, layers) takes of memory, in bytes.
pub(super) trait Weight {
fn bytes(&self) -> usize;
}
impl Weight for Arc<GlyphImage> {
fn bytes(&self) -> usize {
self.blob.bytes.len()
}
}
impl Weight for ColourContent {
fn bytes(&self) -> usize {
match self {
ColourContent::Image(i) => i.bytes(),
// Roughly what a path node with its outline and fill takes.
ColourContent::Art(art) => {
let mut nodes = 0;
art.walk(&mut |_| nodes += 1);
nodes * 512
}
}
}
}
/// The colour content of glyphs ([`FontDb::glyph_image`], [`FontDb::glyph_colour`]) kept by
/// face, glyph and what else it depends on, and its bytes in all.
pub(super) struct GlyphCache<K, V> {
pub(super) kept: HashMap<K, Option<V>>,
pub(super) bytes: usize,
}
impl<K, V> Default for GlyphCache<K, V> {
fn default() -> Self {
Self { kept: HashMap::new(), bytes: 0 }
}
}
impl<K: std::hash::Hash + Eq, V: Weight> GlyphCache<K, V> {
/// Keeps `value` (or that there is none), first letting go of all it keeps when that would
/// take more than [`IMAGE_CACHE_MAX`] glyphs or [`IMAGE_CACHE_BYTES`]. A glyph it keeps
/// already, which another thread looked up meanwhile, stays as it is.
pub(super) fn keep(&mut self, key: K, value: Option<V>) {
if self.kept.contains_key(&key) {
return;
}
let bytes = value.as_ref().map_or(0, Weight::bytes);
if self.kept.len() >= IMAGE_CACHE_MAX || self.bytes.saturating_add(bytes) > IMAGE_CACHE_BYTES {
self.kept.clear();
self.bytes = 0;
}
self.bytes += bytes;
self.kept.insert(key, value);
}
}