use super::*;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FontKey {
pub family: Id,
pub weight: FontWeight,
pub style: fontdb::Style,
pub monospace: bool,
pub tabular: bool,
}
impl FontKey {
pub(super) fn new(family: Id, weight: FontWeight, monospace: bool, tabular: bool) -> Self {
Self {
family,
weight,
style: fontdb::Style::Normal,
monospace,
tabular,
}
}
}
pub const DEFAULT_TAB: u16 = 8;
pub struct CachedLayout {
pub text: String,
pub size: u32,
pub wrap: u32,
pub font: FontKey,
pub tab: u16,
pub layout: Arc<TextLayout>,
pub last_frame: u64,
}
impl TextSystem {
pub fn measure_with_wrap(
&mut self,
text: &str,
size: f32,
font: impl Into<TextFont>,
wrap: f32,
) -> (Vec2, f32) {
self.measure_with_wrap_tab(text, size, font, wrap, DEFAULT_TAB)
}
pub(crate) fn measure_with_wrap_tab(
&mut self,
text: &str,
size: f32,
font: impl Into<TextFont>,
wrap: f32,
tab: u16,
) -> (Vec2, f32) {
let layout = self.layout_with_tab(text, size, font, wrap, tab);
let effective = if layout.width_independent && wrap >= layout.size.x {
f32::INFINITY
} else {
wrap
};
(layout.size, effective)
}
pub fn measure(
&mut self,
text: &str,
size: f32,
font: impl Into<TextFont>,
wrap_width: f32,
) -> Vec2 {
self.layout(text, size, font, wrap_width).size
}
pub fn monospace_metrics(&mut self, size: f32, weight: FontWeight) -> MonospaceMetrics {
let font = TextFont::new(weight, TextFamily::Monospace, false);
let layout = self.layout("0", size, font, f32::INFINITY);
MonospaceMetrics {
cell_width: layout.size.x,
line_height: layout.size.y,
}
}
pub fn carets(
&mut self,
text: &str,
size: f32,
font: impl Into<TextFont>,
) -> Vec<(usize, f32)> {
self.layout(text, size, font, f32::INFINITY).carets.clone()
}
pub fn layout(
&mut self,
text: &str,
size: f32,
font: impl Into<TextFont>,
wrap_width: f32,
) -> Arc<TextLayout> {
self.layout_with_tab(text, size, font, wrap_width, DEFAULT_TAB)
}
pub fn layout_with_tab(
&mut self,
text: &str,
size: f32,
font: impl Into<TextFont>,
wrap_width: f32,
tab: u16,
) -> Arc<TextLayout> {
let spec = font.into();
let font = self.font_key(spec);
let key = Id::new((text, size.to_bits(), wrap_width.to_bits(), font, tab));
if let Some(cached) = self.layouts.get_mut(&key) {
if cached.text == text
&& cached.size == size.to_bits()
&& cached.wrap == wrap_width.to_bits()
&& cached.font == font
&& cached.tab == tab
{
cached.last_frame = self.frame;
return Arc::clone(&cached.layout);
}
}
let text_key = Id::new((text, size.to_bits(), font, tab));
let reusable = self
.layout_keys
.get(&text_key)
.and_then(|key| self.layouts.get(key))
.filter(|c| {
c.text == text
&& c.size == size.to_bits()
&& c.font == font
&& c.tab == tab
&& c.layout.width_independent
&& wrap_width >= c.layout.size.x
})
.map(|c| Arc::clone(&c.layout));
let layout = reusable.unwrap_or_else(|| {
self.builds += 1;
let stop = (font.monospace && text.contains('\t'))
.then(|| self.layout("0", size, spec, f32::INFINITY).size.x * f32::from(tab));
Arc::new(self.build_layout_with_tab(text, size, font, wrap_width, tab, stop))
});
self.layout_keys.insert(text_key, key);
self.layouts.insert(
key,
CachedLayout {
text: text.to_owned(),
size: size.to_bits(),
wrap: wrap_width.to_bits(),
font,
tab,
layout: Arc::clone(&layout),
last_frame: self.frame,
},
);
layout
}
}