preparation

This commit is contained in:
2025-09-09 21:53:32 -04:00
parent 15cc91d92a
commit 709a2d0e17
14 changed files with 292 additions and 220 deletions

View File

@@ -28,6 +28,16 @@ pub struct TextAttrs {
pub family: Family<'static>,
}
#[derive(Default)]
pub enum Cursor {
#[default]
None,
Select {
line: usize,
idx: usize,
},
}
pub type TextBuffer = Buffer;
impl Default for TextAttrs {
@@ -48,6 +58,7 @@ impl TextData {
buffer: &mut TextBuffer,
content: &str,
attrs: &TextAttrs,
cursor: &Cursor,
textures: &mut Textures,
) -> (TextureHandle, TextOffset) {
buffer.set_metrics(
@@ -67,7 +78,14 @@ impl TextData {
let mut max_y = 0;
let c = attrs.color;
let mut max_width = 0.0f32;
for run in buffer.layout_runs() {
let mut i = 0;
let mut cursor_x = 0;
for (run_i, run) in buffer.layout_runs().enumerate() {
if let Cursor::Select { line, .. } = cursor
&& *line == run_i
{
cursor_x = run.line_w as i32;
}
for glyph in run.glyphs.iter() {
let physical_glyph = glyph.physical((0., 0.), 1.0);
@@ -76,6 +94,14 @@ impl TextData {
None => cosmic_text::Color::rgba(c.r, c.g, c.b, c.a),
};
if let Cursor::Select { idx, line } = cursor
&& *line == run_i
&& *idx == i
{
cursor_x = physical_glyph.x;
}
i += 1;
self.swash_cache.with_pixels(
&mut self.font_system,
physical_glyph.cache_key,
@@ -93,6 +119,11 @@ impl TextData {
}
max_width = max_width.max(run.line_w);
}
if let &Cursor::Select { line, .. } = cursor {
let y = (attrs.line_height * (line + 1) as f32) as i32 - 1;
max_y = max_y.max(y);
max_x = max_x.max(cursor_x);
}
let width = (max_x - min_x + 1) as u32;
let height = (max_y - min_y + 1) as u32;
let mut image = RgbaImage::new(width, height);
@@ -101,6 +132,15 @@ impl TextData {
let y = (y - min_y) as u32;
image.put_pixel(x, y, color);
}
if let &Cursor::Select { line, .. } = cursor {
for y in 0..attrs.line_height as u32 {
// no clue if this is good or bad for non integer values
// depends on how the layouting is actually done
let x = (cursor_x - min_x) as u32;
let y = (y as f32 + attrs.line_height * line as f32 - min_y as f32) as u32;
image.put_pixel(x, y, Rgba(c.as_arr()));
}
}
let offset = TextOffset {
top_left: Vec2::new(min_x as f32, min_y as f32),
bot_right: Vec2::new(