draw_rect refactor.

This commit is contained in:
Mark Tomlin 2023-12-25 14:58:29 -05:00 committed by Jeremy Soller
parent aecb80eaed
commit 47aad59fe4

View file

@ -98,15 +98,22 @@ where
TextBox::new(editor, metrics) TextBox::new(editor, metrics)
} }
struct Canvas {
w: i32,
h: i32,
}
struct Offset {
x: i32,
y: i32,
}
//TODO: improve performance //TODO: improve performance
fn draw_rect( fn draw_rect(
buffer: &mut [u32], buffer: &mut [u32],
image_w: i32, canvas: Canvas,
image_h: i32, offset: Canvas,
start_x: i32, start: Offset,
start_y: i32,
w: i32,
h: i32,
cosmic_color: cosmic_text::Color, cosmic_color: cosmic_text::Color,
) { ) {
// Grab alpha channel and green channel // Grab alpha channel and green channel
@ -121,15 +128,15 @@ fn draw_rect(
// Do not draw if alpha is zero // Do not draw if alpha is zero
} else if alpha >= 255 { } else if alpha >= 255 {
// Handle overwrite // Handle overwrite
for y in start_y..start_y + h { for y in start.y..start.y + offset.h {
if y < 0 || y >= image_h { if y < 0 || y >= canvas.h {
// Skip if y out of bounds // Skip if y out of bounds
continue; continue;
} }
let line_offset = y as usize * image_w as usize; let line_offset = y as usize * canvas.w as usize;
for x in start_x..start_x + w { for x in start.x..start.x + offset.w {
if x < 0 || x >= image_w { if x < 0 || x >= canvas.w {
// Skip if x out of bounds // Skip if x out of bounds
continue; continue;
} }
@ -140,15 +147,15 @@ fn draw_rect(
} }
} else { } else {
let n_alpha = 255 - alpha; let n_alpha = 255 - alpha;
for y in start_y..start_y + h { for y in start.y..start.y + offset.h {
if y < 0 || y >= image_h { if y < 0 || y >= canvas.h {
// Skip if y out of bounds // Skip if y out of bounds
continue; continue;
} }
let line_offset = y as usize * image_w as usize; let line_offset = y as usize * canvas.w as usize;
for x in start_x..start_x + w { for x in start.x..start.x + offset.w {
if x < 0 || x >= image_w { if x < 0 || x >= canvas.w {
// Skip if x out of bounds // Skip if x out of bounds
continue; continue;
} }
@ -384,12 +391,15 @@ where
// Ensure fill with gutter color // Ensure fill with gutter color
draw_rect( draw_rect(
pixels, pixels,
image_w, Canvas {
image_h, w: image_w,
0, h: image_h,
0, },
editor_offset_x, Canvas {
image_h, w: editor_offset_x,
h: image_h,
},
Offset { x: 0, y: 0 },
gutter, gutter,
); );
@ -437,12 +447,15 @@ where
|x, y, color| { |x, y, color| {
draw_rect( draw_rect(
pixels, pixels,
image_w, Canvas {
image_h, w: image_w,
physical_glyph.x + x, h: image_h,
physical_glyph.y + y, },
1, Canvas { w: 1, h: 1 },
1, Offset {
x: physical_glyph.x + x,
y: physical_glyph.y + y,
},
color, color,
); );
}, },
@ -457,12 +470,18 @@ where
editor.draw(&mut font_system, &mut swash_cache, |x, y, w, h, color| { editor.draw(&mut font_system, &mut swash_cache, |x, y, w, h, color| {
draw_rect( draw_rect(
pixels, pixels,
image_w, Canvas {
image_h, w: image_w,
editor_offset_x + x, h: image_h,
y, },
w as i32, Canvas {
h as i32, w: w as i32,
h: h as i32,
},
Offset {
x: editor_offset_x + x,
y,
},
color, color,
); );
}); });