use std::{cell::RefCell, collections::HashSet, sync::Arc};
use gpui::{
App, Font, FontFallbacks, FontWeight, Global, Hsla, ParentElement, Pixels, Point, ShapedLine,
SharedString, Styled, TextAlign, TextRun, Window, div, px,
};
use gpui_material_theme::{Direction, FontFamilies, LayoutPolicy, TypeStyle, Typography};
pub fn wrapped_text(
text: impl Into<SharedString>,
style: TypeStyle,
font_scale: f32,
tint: Hsla,
window: &Window,
cx: &App,
) -> gpui::Div {
text_style(div().min_w_0(), style, font_scale, window, cx)
.text_color(tint)
.whitespace_normal()
.child(text.into())
}
pub fn text_style<E: Styled>(
element: E,
style: TypeStyle,
font_scale: f32,
window: &Window,
cx: &App,
) -> E {
let theme = crate::current_material_theme(window, cx);
let style = LayoutPolicy::scale_text(theme.typography.scale_style(style), font_scale);
let font = font_for_style(&theme.typography, style, cx);
element
.font(font)
.text_size(px(style.size))
.line_height(px(style.line_height))
.text_align(if theme.layout.direction == Direction::Rtl {
TextAlign::Right
} else {
TextAlign::Left
})
}
type ResolvedFontEntry = (FontFamilies, SharedString, Option<FontFallbacks>);
struct InstalledFonts {
families: HashSet<String>,
revision: u64,
resolved: RefCell<Vec<ResolvedFontEntry>>,
}
impl Global for InstalledFonts {}
impl InstalledFonts {
fn enumerate(cx: &App) -> Self {
Self {
families: cx
.text_system()
.all_font_names()
.into_iter()
.map(|name| name.to_ascii_lowercase())
.collect(),
revision: font_cache_revision(cx).wrapping_add(1),
resolved: RefCell::new(Vec::new()),
}
}
fn resolve_family(&self, families: &FontFamilies) -> (SharedString, Option<FontFallbacks>) {
if let Some((_, family, fallbacks)) = self
.resolved
.borrow()
.iter()
.find(|(cached, _, _)| cached == families)
{
return (family.clone(), fallbacks.clone());
}
let family = std::iter::once(&families.primary)
.chain(&families.fallback)
.find(|name| {
name.as_str() == ".SystemUIFont"
|| self.families.contains(&name.to_ascii_lowercase())
})
.map_or_else(
|| SharedString::new_static(".SystemUIFont"),
|name| SharedString::from(name.clone()),
);
let fallbacks = Some(FontFallbacks::from_fonts(families.fallback.clone()));
let mut resolved = self.resolved.borrow_mut();
if resolved.len() < 32 {
resolved.push((families.clone(), family.clone(), fallbacks.clone()));
}
(family, fallbacks)
}
}
pub(crate) fn refresh_installed_fonts(cx: &mut App) {
cx.set_global(InstalledFonts::enumerate(cx));
}
#[doc(hidden)]
pub fn font_cache_revision(cx: &App) -> u64 {
cx.try_global::<InstalledFonts>()
.map_or(0, |fonts| fonts.revision)
}
pub fn font_for_style(typography: &Typography, style: TypeStyle, cx: &App) -> Font {
let families = typography.families(style.typeface);
let (family, fallbacks) = match cx.try_global::<InstalledFonts>() {
Some(installed) => installed.resolve_family(families),
None => InstalledFonts::enumerate(cx).resolve_family(families),
};
Font {
family,
features: Default::default(),
fallbacks,
weight: FontWeight(f32::from(style.weight)),
style: Default::default(),
}
}
pub struct ShapedText {
line: ShapedLine,
line_height: Pixels,
}
impl ShapedText {
pub fn line(&self) -> &ShapedLine {
&self.line
}
pub fn line_height(&self) -> Pixels {
self.line_height
}
pub fn paint(
&self,
origin: Point<Pixels>,
align: TextAlign,
align_width: Option<Pixels>,
window: &mut Window,
cx: &mut App,
) -> gpui::Result<()> {
self.line
.paint(origin, self.line_height, align, align_width, window, cx)
}
}
const TRACKED_LAYOUT_CACHE_LEN: usize = 512;
type TrackedLayoutSlot = Option<(Arc<gpui::LineLayout>, u32, Arc<gpui::LineLayout>)>;
thread_local! {
static TRACKED_LAYOUT_CACHE: RefCell<Vec<TrackedLayoutSlot>> =
RefCell::new(vec![None; TRACKED_LAYOUT_CACHE_LEN]);
}
pub fn shape_text(
text: impl Into<SharedString>,
style: TypeStyle,
typography: &Typography,
font_scale: f32,
color: Hsla,
window: &Window,
cx: &App,
) -> Option<ShapedText> {
let text = text.into();
if text.contains(['\r', '\n']) {
return None;
}
let style = LayoutPolicy::scale_text(typography.scale_style(style), font_scale);
let run = TextRun {
len: text.len(),
font: font_for_style(typography, style, cx),
color,
..Default::default()
};
let mut line = window
.text_system()
.shape_line(text, px(style.size), &[run], None);
if style.tracking != 0. && line.len() > 1 {
let tracking_bits = style.tracking.to_bits();
let raw_ptr = Arc::as_ptr(&*line) as usize;
let hash =
(raw_ptr >> 4) ^ (raw_ptr >> 13) ^ (tracking_bits as usize).wrapping_mul(0x9e37_79b9);
let slot_index = hash & (TRACKED_LAYOUT_CACHE_LEN - 1);
let cached = TRACKED_LAYOUT_CACHE.with(|cache| {
let cache = cache.borrow();
cache[slot_index]
.as_ref()
.and_then(|(untracked, bits, tracked)| {
(*bits == tracking_bits && Arc::ptr_eq(untracked, &*line))
.then(|| tracked.clone())
})
});
if let Some(tracked) = cached {
*line = tracked;
} else {
let mut first_cluster = None;
let has_multiple_clusters =
line.runs
.iter()
.flat_map(|run| &run.glyphs)
.any(|glyph| match first_cluster {
None => {
first_cluster = Some(glyph.index);
false
}
Some(first) => glyph.index != first,
});
let tracked = if has_multiple_clusters {
let mut layout = gpui::LineLayout {
font_size: line.font_size,
width: line.width,
ascent: line.ascent,
descent: line.descent,
runs: line.runs.clone(),
len: line.len(),
};
let mut previous_cluster = None;
let mut offset = px(0.);
for glyph in layout.runs.iter_mut().flat_map(|run| &mut run.glyphs) {
if previous_cluster.is_some_and(|index| index != glyph.index) {
offset += px(style.tracking);
}
glyph.position.x += offset;
previous_cluster = Some(glyph.index);
}
layout.width = (layout.width + offset).max(px(0.));
Arc::new(layout)
} else {
(*line).clone()
};
let untracked = std::mem::replace(&mut *line, tracked.clone());
TRACKED_LAYOUT_CACHE.with(|cache| {
cache.borrow_mut()[slot_index] = Some((untracked, tracking_bits, tracked));
});
}
}
Some(ShapedText {
line,
line_height: px(style.line_height),
})
}