use std::cell::RefCell;
use std::rc::Rc;
use cosmic_text::{
Attrs, Buffer, Color as CosmicColor, Family, FontSystem, Metrics, Shaping, SwashCache, Weight,
};
use glyphon::{
Cache, ColorMode, Resolution, TextArea, TextAtlas, TextBounds, TextRenderer, Viewport,
};
use crate::core::error::{panic_to_string, push_error, UiError};
use crate::style::{Color, Rect, TextAlign};
#[derive(Clone)]
pub struct TextAreaDesc {
pub buffer: Rc<RefCell<Buffer>>,
pub element_id: crate::core::ElementId,
pub generation: u64,
pub left: f32,
pub top: f32,
pub scale: f32,
pub color: Color,
pub scroll_x: f32,
pub scroll_y: f32,
pub clip_rect: Option<Rect>,
pub z_index: i32,
}
impl TextAreaDesc {
pub fn offset(&self, dx: f32, dy: f32) -> Self {
Self {
buffer: self.buffer.clone(),
element_id: self.element_id,
generation: self.generation,
left: self.left + dx,
top: self.top + dy,
scale: self.scale,
color: self.color,
scroll_x: self.scroll_x,
scroll_y: self.scroll_y,
clip_rect: self.clip_rect.map(|r| r.offset(dx, dy)),
z_index: self.z_index,
}
}
}
pub struct GlyphonBridge {
#[allow(dead_code)]
cache: Cache,
atlas: TextAtlas,
viewport: Viewport,
renderers: Vec<TextRenderer>,
next_renderer: usize,
multisample: wgpu::MultisampleState,
pub font_system: Option<FontSystem>,
pub font_system_ok: bool,
swash_cache: SwashCache,
}
impl GlyphonBridge {
pub fn new(
device: &wgpu::Device,
queue: &wgpu::Queue,
format: wgpu::TextureFormat,
width: u32,
height: u32,
multisample: wgpu::MultisampleState,
) -> Self {
let cache = Cache::new(device);
let mut viewport = Viewport::new(device, &cache);
viewport.update(queue, Resolution { width, height });
let mut atlas =
TextAtlas::with_color_mode(device, queue, &cache, format, ColorMode::Accurate);
let renderer = TextRenderer::new(&mut atlas, device, multisample, None);
let (font_system, font_system_ok) = match std::panic::catch_unwind(FontSystem::new) {
Ok(fs) => (Some(fs), true),
Err(panic) => {
let msg = panic_to_string(&panic);
push_error(UiError::FontLoad(format!(
"GlyphonBridge FontSystem::new panicked: {msg}"
)));
(None, false)
}
};
let swash_cache = SwashCache::new();
Self {
cache,
atlas,
viewport,
renderers: vec![renderer],
next_renderer: 0,
multisample,
font_system,
font_system_ok,
swash_cache,
}
}
pub fn resize(&mut self, queue: &wgpu::Queue, width: u32, height: u32) {
self.viewport.update(queue, Resolution { width, height });
}
pub fn trim(&mut self) {
self.atlas.trim();
self.next_renderer = 0;
}
pub fn invalidate_cache(&mut self) {
self.atlas.trim();
self.renderers.clear();
self.next_renderer = 0;
}
pub fn prepare_layer(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
descs: &[TextAreaDesc],
) -> Option<usize> {
if !self.font_system_ok {
return None;
}
if descs.is_empty() {
return None;
}
let mut descs_sorted: Vec<&TextAreaDesc> = descs.iter().collect();
descs_sorted.sort_by_key(|d| d.z_index);
let bufs: Vec<std::cell::Ref<cosmic_text::Buffer>> =
descs_sorted.iter().map(|d| d.buffer.borrow()).collect();
let build_text_areas = || -> Vec<TextArea> {
descs_sorted
.iter()
.zip(bufs.iter())
.filter_map(|(d, buf)| {
if buf.layout_runs().count() == 0 {
return None;
}
let left = (d.left - d.scroll_x) * d.scale;
let top = (d.top - d.scroll_y) * d.scale;
let (mut l, mut t, mut r, b) = d
.clip_rect
.map(|c| {
let sf = d.scale;
let l = (c.x * sf).round() as i32;
let t = (c.y * sf).round() as i32;
let r = ((c.x + c.width) * sf).round() as i32;
let b = ((c.y + c.height) * sf).round() as i32;
(l, t, r, b)
})
.unwrap_or((i32::MIN, i32::MIN, i32::MAX, i32::MAX));
if r <= 0 || b <= 0 {
return None;
}
l += 1;
t += 1;
r = (r - 1).max(l);
if r <= l || b <= t {
return None;
}
Some(TextArea {
buffer: buf,
left,
top,
scale: d.scale,
bounds: TextBounds {
left: l,
top: t,
right: r,
bottom: b,
},
default_color: CosmicColor::rgba(
(d.color.r * 255.0) as u8,
(d.color.g * 255.0) as u8,
(d.color.b * 255.0) as u8,
(d.color.a * 255.0) as u8,
),
custom_glyphs: &[],
})
})
.collect()
};
let text_areas = build_text_areas();
if text_areas.is_empty() {
return None;
}
let idx = self.next_renderer;
if idx >= self.renderers.len() {
let r = TextRenderer::new(&mut self.atlas, device, self.multisample, None);
self.renderers.push(r);
}
self.next_renderer += 1;
let Self {
renderers,
atlas,
viewport,
font_system,
swash_cache,
..
} = self;
let fs = font_system
.as_mut()
.expect("font_system_ok guard ensures Some");
let result =
renderers[idx].prepare(device, queue, fs, atlas, viewport, text_areas, swash_cache);
match result {
Ok(()) => Some(idx),
Err(glyphon::PrepareError::AtlasFull) => {
eprintln!("[GLYPHON:ATLAS] atlas full — trimming + retry");
atlas.trim();
let mut new_r = TextRenderer::new(atlas, device, self.multisample, None);
let retry_areas = build_text_areas();
if retry_areas.is_empty() {
return None;
}
match new_r.prepare(device, queue, fs, atlas, viewport, retry_areas, swash_cache) {
Ok(()) => {
renderers[idx] = new_r;
Some(idx)
}
Err(_) => {
eprintln!("[GLYPHON:ATLAS] retry also failed — skipping layer");
None
}
}
}
}
}
pub fn render_layer(&self, idx: usize, pass: &mut wgpu::RenderPass<'_>) {
let Some(renderer) = self.renderers.get(idx) else {
return;
};
match renderer.render(&self.atlas, &self.viewport, pass) {
Ok(()) => {}
Err(glyphon::RenderError::RemovedFromAtlas) => {
#[cfg(feature = "tracing")]
tracing::warn!("glyphon: glyph removed from atlas during render");
}
Err(glyphon::RenderError::ScreenResolutionChanged) => {
#[cfg(feature = "tracing")]
tracing::debug!("glyphon: screen resolution changed");
}
}
}
}
pub fn create_buffer(
text: &str,
font_size: f32,
line_height: f32,
font_weight: u16,
font_family: Option<&str>,
max_width: Option<f32>,
text_align: TextAlign,
) -> Buffer {
ensure_font_system();
FONT_SYSTEM.with(|fs| {
let mut guard = fs.borrow_mut();
let fs = guard.as_mut().expect(
"ensure_font_system guarantees Some; if the panic-once fallback also \
failed the process has deeper font issues and should not continue",
);
let metrics = Metrics::new(font_size, font_size * line_height);
let mut buffer = Buffer::new(&mut *fs, metrics);
let mut buf = buffer.borrow_with(&mut *fs);
buf.set_size(max_width, Some(font_size * line_height * 100.0));
let mut attrs = Attrs::new().weight(Weight(font_weight));
if let Some(f) = font_family {
attrs = attrs.family(Family::Name(f));
} else {
attrs = attrs.family(Family::SansSerif);
}
buf.set_text(text, &attrs, Shaping::Advanced, None);
drop(buf);
buffer.shape_until_scroll(&mut *fs, false);
let cosmic_align = match text_align {
TextAlign::Left => Some(cosmic_text::Align::Left),
TextAlign::Center if text.chars().any(|c| c as u32 > 0x1F000) => None,
TextAlign::Center => Some(cosmic_text::Align::Center),
TextAlign::Right => Some(cosmic_text::Align::Right),
TextAlign::Justify => Some(cosmic_text::Align::Justified),
TextAlign::End => Some(cosmic_text::Align::End),
TextAlign::Start => None,
};
for line in &mut buffer.lines {
line.set_align(cosmic_align);
}
buffer.shape_until_scroll(&mut *fs, false);
buffer
})
}
pub fn reuse_buffer(
buffer: &mut Buffer,
text: &str,
font_size: f32,
line_height: f32,
font_weight: u16,
font_family: Option<&str>,
max_width: Option<f32>,
text_align: TextAlign,
) {
if !ensure_font_system() {
return;
}
FONT_SYSTEM.with(|fs| {
let mut guard = fs.borrow_mut();
let fs = guard.as_mut().unwrap();
let metrics = Metrics::new(font_size, font_size * line_height);
let mut buf = buffer.borrow_with(fs);
buf.set_metrics(metrics);
buf.set_size(max_width, Some(font_size * line_height * 100.0));
let mut attrs = Attrs::new().weight(Weight(font_weight));
if let Some(f) = font_family {
attrs = attrs.family(Family::Name(f));
} else {
attrs = attrs.family(Family::SansSerif);
}
buf.set_text(text, &attrs, Shaping::Advanced, None);
drop(buf);
let cosmic_align = match text_align {
TextAlign::Left => Some(cosmic_text::Align::Left),
TextAlign::Center if text.chars().any(|c| c as u32 > 0x1F000) => None,
TextAlign::Center => Some(cosmic_text::Align::Center),
TextAlign::Right => Some(cosmic_text::Align::Right),
TextAlign::Justify => Some(cosmic_text::Align::Justified),
TextAlign::End => Some(cosmic_text::Align::End),
TextAlign::Start => None,
};
for line in &mut buffer.lines {
line.set_align(cosmic_align);
}
buffer.shape_until_scroll(&mut *fs, false);
})
}
pub(crate) fn ensure_font_system() -> bool {
FONT_SYSTEM.with(|cell| {
if cell.borrow().is_none() {
let init = std::panic::catch_unwind(FontSystem::new);
match init {
Ok(fs) => *cell.borrow_mut() = Some(fs),
Err(p) => {
let msg = panic_to_string(&p);
push_error(UiError::FontLoad(format!(
"Thread-local FontSystem::new panicked: {msg}"
)));
let fallback = std::panic::catch_unwind(|| {
FontSystem::new_with_locale_and_db("en".into(), fontdb::Database::new())
});
if let Ok(fs) = fallback {
*cell.borrow_mut() = Some(fs);
}
}
}
}
cell.borrow().is_some()
})
}
thread_local! {
pub static FONT_SYSTEM: RefCell<Option<FontSystem>> = const { RefCell::new(None) };
}
pub(crate) fn intrinsic_width_from_buffer(buffer: &Buffer, font_size: f32) -> Option<f32> {
let mut runs = buffer.layout_runs();
let run = runs.next()?;
if runs.next().is_some() {
return None;
}
let (mut min_x, mut max_x) = (f32::MAX, 0.0f32);
for g in run.glyphs.iter() {
min_x = min_x.min(g.x);
max_x = max_x.max(g.x + g.w);
}
if min_x < max_x && max_x > 0.0 {
Some((max_x - min_x) + 2.0)
} else {
Some(font_size + 2.0)
}
}
pub(crate) fn trim_shape_run_cache(keep_ages: u64) {
if !ensure_font_system() {
return;
}
FONT_SYSTEM.with(|fs| {
if let Some(fs) = fs.borrow_mut().as_mut() {
fs.shape_run_cache.trim(keep_ages);
}
});
}