use std::sync::Arc;
use iced::advanced::graphics::text::Raw as TextRaw;
use iced::advanced::graphics::text::cosmic_text;
use iced::advanced::layout::Layout;
use iced::advanced::mouse;
use iced::advanced::renderer;
use iced::{Color, Point, Rectangle};
use crate::util::UnwrapPoison;
use super::theme;
#[must_use]
pub fn font_metrics() -> cosmic_text::Metrics {
cosmic_text::Metrics::relative(theme::TEXT_14, 1.3)
}
pub(crate) const MAX_HIGHLIGHT_SIZE: usize = 10 * 1024 * 1024;
pub(crate) const GUTTER_FONT_SIZE: f32 = theme::TEXT_11;
pub(crate) const MAX_VISUAL_LINES_PER_SOURCE: usize = 10_000;
pub(crate) fn with_font_system<R>(f: impl FnOnce(&mut cosmic_text::FontSystem) -> R) -> R {
let mut guard = iced::advanced::graphics::text::font_system()
.write()
.unwrap_poison();
f(guard.raw())
}
pub(crate) fn reshape_and_shape(
buffer: &mut cosmic_text::Buffer,
font_sys: &mut cosmic_text::FontSystem,
scroll_y: Option<f32>,
scroll_x: f32,
text_area_width: f32,
text_area_height: f32,
) {
if let Some(scroll_y) = scroll_y {
buffer.set_scroll(cosmic_text::Scroll {
line: 0,
vertical: scroll_y,
horizontal: scroll_x,
});
}
buffer.set_size(font_sys, Some(text_area_width), Some(text_area_height));
buffer.shape_until_scroll(font_sys, false);
}
#[expect(clippy::cast_precision_loss)]
pub(crate) fn compute_total_height(
buffer: &mut cosmic_text::Buffer,
font_sys: &mut cosmic_text::FontSystem,
metrics: cosmic_text::Metrics,
) -> f32 {
let mut total_visual_lines: f32 = 0.0;
for i in 0..buffer.lines.len() {
let visual_count = buffer
.line_layout(font_sys, i)
.map_or(1, |ll| ll.len().min(MAX_VISUAL_LINES_PER_SOURCE));
total_visual_lines += visual_count as f32;
}
total_visual_lines * metrics.line_height
}
pub(crate) fn text_area_rect(
bounds: Rectangle,
h_padding: f32,
v_padding: f32,
gutter_width: f32,
) -> Rectangle {
let x = bounds.x + h_padding + gutter_width + 4.0; let y = bounds.y + v_padding;
let width = (bounds.width - (x - bounds.x) - h_padding).max(0.0);
let height = (bounds.height - v_padding * 2.0).max(0.0);
Rectangle {
x,
y,
width,
height,
}
}
pub(crate) fn byte_line_and_start(text: &str, offset: usize) -> (usize, usize) {
let prefix = &text[..offset];
let line = prefix.bytes().filter(|&b| b == b'\n').count();
let line_start = prefix.rfind('\n').map_or(0, |p| p + 1);
(line, line_start)
}
pub(crate) fn cursor_to_buffer_coords(
layout: Layout<'_>,
cursor: mouse::Cursor,
gutter_width: f32,
h_padding: f32,
v_padding: f32,
) -> Option<(f32, f32)> {
let bounds = layout.bounds();
let pos = cursor.position_in(bounds)?;
let buf_x = pos.x - h_padding - gutter_width - 4.0;
let buf_y = pos.y - v_padding;
if buf_x < 0.0 || buf_y < 0.0 {
None
} else {
Some((buf_x, buf_y))
}
}
pub(crate) fn gutter_clip_rect(
bounds: Rectangle,
h_padding: f32,
v_padding: f32,
gutter_width: f32,
text_area_height: f32,
) -> Rectangle {
Rectangle {
x: bounds.x + h_padding,
y: bounds.y + v_padding,
width: gutter_width,
height: text_area_height,
}
}
#[expect(clippy::too_many_arguments)]
pub(crate) fn draw_highlight_background<Renderer>(
renderer: &mut Renderer,
text_clip: Rectangle,
text_x: f32,
text_y: f32,
run: &cosmic_text::LayoutRun,
x_offset: f32,
width: f32,
color: Color,
) where
Renderer: iced::advanced::Renderer,
{
let rect = Rectangle {
x: text_x + x_offset,
y: text_y + run.line_top,
width,
height: run.line_height,
};
if let Some(clipped) = text_clip.intersection(&rect) {
renderer.fill_quad(
renderer::Quad {
bounds: clipped,
border: iced::Border::default(),
..renderer::Quad::default()
},
color,
);
}
}
pub(crate) fn draw_background<Renderer>(renderer: &mut Renderer, bounds: Rectangle, color: Color)
where
Renderer: iced::advanced::Renderer,
{
renderer.fill_quad(
renderer::Quad {
bounds,
border: iced::Border::default(),
..renderer::Quad::default()
},
color,
);
}
pub(crate) fn draw_buffer_text<Renderer>(
renderer: &mut Renderer,
buffer: &Arc<cosmic_text::Buffer>,
position: Point,
clip_bounds: Rectangle,
) where
Renderer: iced::advanced::graphics::text::Renderer,
{
renderer.fill_raw(TextRaw {
buffer: Arc::downgrade(buffer),
position,
color: Color::WHITE,
clip_bounds,
});
}
#[expect(clippy::too_many_arguments)]
pub(crate) fn draw_run_highlights<Renderer, F>(
renderer: &mut Renderer,
buffer: &cosmic_text::Buffer,
text_clip: Rectangle,
text_x: f32,
text_y: f32,
color: Color,
stop_after_first: bool,
visible_y: Option<(f32, f32)>,
mut filter: F,
) where
Renderer: iced::advanced::Renderer,
F: FnMut(&cosmic_text::LayoutRun) -> Option<((usize, usize), (usize, usize))>,
{
for run in buffer.layout_runs() {
if let Some((top, bottom)) = visible_y {
let run_top = text_y + run.line_top;
let run_bottom = run_top + run.line_height;
if run_bottom <= top {
continue;
}
if run_top >= bottom {
break;
}
}
let Some(((start_line, start_idx), (end_line, end_idx))) = filter(&run) else {
continue;
};
let start = cosmic_text::Cursor {
line: start_line,
index: start_idx,
..cosmic_text::Cursor::default()
};
let end = cosmic_text::Cursor {
line: end_line,
index: end_idx,
..cosmic_text::Cursor::default()
};
if let Some((x_offset, width)) = run.highlight(start, end) {
draw_highlight_background(
renderer, text_clip, text_x, text_y, &run, x_offset, width, color,
);
if stop_after_first {
break;
}
}
}
}
#[expect(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
pub(crate) fn iced_color_to_cosmic(c: Color) -> cosmic_text::Color {
let r = (c.r * 255.0).round() as u8;
let g = (c.g * 255.0).round() as u8;
let b = (c.b * 255.0).round() as u8;
cosmic_text::Color::rgb(r, g, b)
}
fn push_or_merge<'a>(
text: &'a str,
result: &mut Vec<(&'a str, cosmic_text::Attrs<'a>)>,
new_text: &'a str,
new_attrs: cosmic_text::Attrs<'a>,
) {
if let Some(last) = result.last_mut() {
if last.1 == new_attrs {
let start = (last.0.as_ptr() as usize) - (text.as_ptr() as usize);
let last_end = start + last.0.len();
let new_start = (new_text.as_ptr() as usize) - (text.as_ptr() as usize);
if last_end == new_start {
let end = new_start + new_text.len();
last.0 = &text[start..end];
return;
}
}
}
result.push((new_text, new_attrs));
}
pub(crate) fn fill_rich_spans<'a>(
text: &'a str,
spans: impl IntoIterator<Item = (usize, usize, cosmic_text::Attrs<'a>)>,
base_attrs: &cosmic_text::Attrs<'a>,
) -> Vec<(&'a str, cosmic_text::Attrs<'a>)> {
let mut result: Vec<(&str, cosmic_text::Attrs)> = Vec::new();
let mut byte_pos = 0usize;
for (raw_start, raw_end, attrs) in spans {
let start = text.floor_char_boundary(raw_start).max(byte_pos);
let end = text.floor_char_boundary(raw_end);
if start > byte_pos {
push_or_merge(
text,
&mut result,
&text[byte_pos..start],
base_attrs.clone(),
);
byte_pos = start;
}
if end > start {
push_or_merge(text, &mut result, &text[start..end], attrs);
byte_pos = end;
}
}
if byte_pos < text.len() {
push_or_merge(text, &mut result, &text[byte_pos..], base_attrs.clone());
}
result
}
#[cfg(test)]
mod tests {
use super::*;
fn red() -> cosmic_text::Attrs<'static> {
cosmic_text::Attrs::new().color(cosmic_text::Color::rgb(255, 0, 0))
}
fn spans(
ranges: &[(usize, usize)],
attrs: &cosmic_text::Attrs<'static>,
) -> Vec<(usize, usize, cosmic_text::Attrs<'static>)> {
ranges
.iter()
.map(|&(lo, hi)| (lo, hi, attrs.clone()))
.collect()
}
fn assert_rich_spans_cover(case: &str, text: &str, out: &[(&str, cosmic_text::Attrs)]) {
let mut cursor = 0usize;
for (slice, _) in out {
let start = (slice.as_ptr() as usize) - (text.as_ptr() as usize);
let end = start + slice.len();
assert_eq!(start, cursor, "case {case}: gap before byte {cursor}");
assert!(text.is_char_boundary(start), "case {case}: start {start}");
assert!(text.is_char_boundary(end), "case {case}: end {end}");
cursor = end;
}
assert_eq!(cursor, text.len(), "case {case}: spans cover all bytes");
}
type CoverCase<'a> = (&'a str, &'a str, &'a [(usize, usize)]);
#[test]
fn fill_rich_spans_keeps_multibyte_coverage() {
let cases: &[CoverCase] = &[
("spans inside a multibyte char", "ab—cdé", &[(3, 6), (8, 9)]),
(
"collapsed multibyte span keeps coverage",
"ab—cd",
&[(3, 4)],
),
("end inside a multibyte char", "a—", &[(0, 3)]),
(
"fills leading and trailing multibyte gaps",
"—x—",
&[(1, 4)],
),
];
let base = cosmic_text::Attrs::new();
for &(name, text, ranges) in cases {
let out = fill_rich_spans(text, spans(ranges, &red()), &base);
assert_rich_spans_cover(name, text, &out);
}
}
type ClampCase<'a> = (
&'a str,
&'a str,
&'a [(usize, usize)],
Option<&'a [&'a str]>,
);
#[test]
fn fill_rich_spans_clamps_out_of_range_spans_and_leaves_ascii_untouched() {
let cases: &[ClampCase] = &[
(
"clamps out of range spans",
"a—b",
&[(2, 100), (200, 300)],
None,
),
(
"does not change ascii behaviour",
"hello world",
&[(0, 5), (6, 11)],
Some(&["hello", " ", "world"]),
),
];
let base = cosmic_text::Attrs::new();
for &(name, text, ranges, expected) in cases {
let out = fill_rich_spans(text, spans(ranges, &red()), &base);
assert_rich_spans_cover(name, text, &out);
if let Some(expected) = expected {
let slices: Vec<&str> = out.iter().map(|(slice, _)| *slice).collect();
assert_eq!(slices, expected, "case {name}");
}
}
}
}