#[cfg(target_os = "linux")]
use fontique as _;
use std::collections::HashMap;
use std::sync::Arc;
mod glyph_run;
use parley::{FontContext, FontWeight, LayoutContext, PositionedLayoutItem, StyleProperty};
use serge_core::{FontHandle, Glyph, Point, ScenePrimitive};
use serge_style::Color;
pub use parley::FontData as Font;
#[derive(Clone, Debug)]
pub struct Run {
pub font: FontHandle,
pub size: f32,
pub glyphs: Vec<Glyph>,
}
#[derive(Clone, Debug, Default)]
pub struct TextLayout {
pub width: f32,
pub height: f32,
pub runs: Vec<Run>,
}
impl TextLayout {
pub fn primitives(
&self,
origin: Point,
color: Color,
) -> impl Iterator<Item = ScenePrimitive> + '_ {
self.runs.iter().map(move |r| ScenePrimitive::Glyphs {
origin,
font: r.font.clone(),
size: r.size,
color,
glyphs: r.glyphs.clone(),
})
}
}
#[derive(Hash, PartialEq, Eq, Clone)]
struct Key {
size: u32,
weight: u16,
max_width: Option<u32>,
}
pub struct TextSystem {
fonts: FontContext,
layouts: LayoutContext<()>,
cache: HashMap<Box<str>, HashMap<Key, TextLayout>>,
cached_layouts: usize,
handles: HashMap<(u64, u32), FontHandle>,
}
impl Default for TextSystem {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Debug for TextSystem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TextSystem").finish_non_exhaustive()
}
}
impl TextSystem {
pub fn new() -> TextSystem {
TextSystem {
fonts: FontContext::new(),
layouts: LayoutContext::new(),
cache: HashMap::new(),
cached_layouts: 0,
handles: HashMap::new(),
}
}
pub fn layout(
&mut self,
text: &str,
size: f32,
weight: u16,
max_width: Option<f32>,
) -> TextLayout {
self.layout_ref(text, size, weight, max_width).clone()
}
pub fn layout_ref(
&mut self,
text: &str,
size: f32,
weight: u16,
max_width: Option<f32>,
) -> &TextLayout {
let wrap = max_width.map(|w| (w.max(1.0) + 0.5).ceil());
let key = Key {
size: size.to_bits(),
weight,
max_width: wrap.map(f32::to_bits),
};
if self
.cache
.get(text)
.is_some_and(|styles| styles.contains_key(&key))
{
return &self.cache[text][&key];
}
if self.cached_layouts >= 2048 {
self.cache.clear();
self.cached_layouts = 0;
}
let mut b = self
.layouts
.ranged_builder(&mut self.fonts, text, 1.0, true);
b.push_default(StyleProperty::FontSize(size));
b.push_default(StyleProperty::FontWeight(FontWeight::new(f32::from(
weight,
))));
b.push_default(StyleProperty::LineHeight(
parley::LineHeight::FontSizeRelative(1.25),
));
let mut layout: parley::Layout<()> = b.build(text);
layout.break_all_lines(wrap);
let mut runs = Vec::new();
for line in layout.lines() {
for item in line.items() {
if let PositionedLayoutItem::GlyphRun(g) = item {
let run = g.run();
let font = run.font();
let handle = self
.handles
.entry((font.data.id(), font.index))
.or_insert_with(|| FontHandle(Arc::new(font.clone())))
.clone();
runs.push(Run {
font: handle,
size: run.font_size(),
glyphs: glyph_run::positioned(&g),
});
}
}
}
let out = TextLayout {
width: layout.width(),
height: layout.height().max(size * 1.2),
runs,
};
self.cached_layouts += 1;
self.cache
.entry(text.into())
.or_default()
.entry(key)
.or_insert(out)
}
pub fn measure(
&mut self,
text: &str,
size: f32,
weight: u16,
max_width: Option<f32>,
) -> (f32, f32) {
let l = self.layout_ref(text, size, weight, max_width);
(l.width.ceil(), l.height.ceil())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wider_text_measures_wider_and_wraps() {
let mut t = TextSystem::new();
let (w1, h1) = t.measure("Count: 0", 16.0, 400, None);
let (w2, _) = t.measure("Count: 0 and more words here", 16.0, 400, None);
assert!(w1 > 0.0 && h1 > 0.0 && w2 > w1, "{w1} {w2}");
let (_, wrapped) = t.measure("Count: 0 and more words here", 16.0, 400, Some(60.0));
assert!(wrapped > h1, "wrapping adds lines");
}
}