feat: render interactive manuscript from Endor through generic graphics
This commit is contained in:
@@ -17,6 +17,8 @@ use crate::display_list::Color;
|
||||
pub enum TextFamily {
|
||||
Interface,
|
||||
Code,
|
||||
/// A registered family loaded through the generic font-resource API.
|
||||
Custom(u32),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
@@ -82,6 +84,7 @@ struct PreparedText {
|
||||
/// 共享字体系统、字形缓存和整形缓存;调用通过全局锁串行访问。
|
||||
struct TextEngine {
|
||||
fonts: FontSystem,
|
||||
custom: Vec<(String, u64)>,
|
||||
code_family: Option<String>,
|
||||
swash: SwashCache,
|
||||
buffers: HashMap<TextKey, PreparedText>,
|
||||
@@ -121,6 +124,7 @@ impl TextEngine {
|
||||
}
|
||||
Self {
|
||||
fonts,
|
||||
custom: Vec::new(),
|
||||
code_family: mono,
|
||||
swash: SwashCache::new(),
|
||||
buffers: HashMap::new(),
|
||||
@@ -152,6 +156,7 @@ impl TextEngine {
|
||||
}
|
||||
let family = match style.family {
|
||||
TextFamily::Interface => Family::SansSerif,
|
||||
TextFamily::Custom(id) => Family::Name(&self.custom[id as usize].0),
|
||||
// Keep Latin source on the selected code face, and allow ordinary
|
||||
// script fallback for CJK instead of searching all monospace faces.
|
||||
TextFamily::Code => self
|
||||
@@ -365,6 +370,37 @@ pub fn fit(value: &str, width: f32, style: TextStyle) -> String {
|
||||
format!("{}{ellipsis}", &value[..end])
|
||||
}
|
||||
|
||||
/// Register an explicit face for graphics resources. Repeated identical loads
|
||||
/// reuse the family; conflicting content and unbounded global growth fail.
|
||||
pub fn register_font(bytes: Vec<u8>, family: &str) -> anyhow::Result<TextFamily> {
|
||||
use std::hash::{Hash, Hasher};
|
||||
let mut hash = std::collections::hash_map::DefaultHasher::new();
|
||||
bytes.hash(&mut hash);
|
||||
let hash = hash.finish();
|
||||
let mut e = engine().lock().expect("text engine lock");
|
||||
if let Some((id, (_, old))) = e.custom.iter().enumerate().find(|(_, v)| v.0 == family) {
|
||||
anyhow::ensure!(*old == hash, "font family already has different content");
|
||||
return Ok(TextFamily::Custom(id as u32));
|
||||
}
|
||||
anyhow::ensure!(e.custom.len() < 32, "font registry exceeds 32 families");
|
||||
let previous = e.fonts.db().clone();
|
||||
e.fonts.db_mut().load_font_data(bytes);
|
||||
if !e
|
||||
.fonts
|
||||
.db()
|
||||
.faces()
|
||||
.any(|f| f.families.iter().any(|(n, _)| n == family))
|
||||
{
|
||||
*e.fonts.db_mut() = previous;
|
||||
anyhow::bail!("font data does not contain requested family");
|
||||
}
|
||||
let id = e.custom.len() as u32;
|
||||
e.custom.push((family.into(), hash));
|
||||
e.buffers.clear();
|
||||
e.source_bytes = 0;
|
||||
Ok(TextFamily::Custom(id))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user