|
|
|
@ -1,3 +1,5 @@
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
use skulpin::CoordinateSystemHelper;
|
|
|
|
|
use skulpin::skia_safe::{Canvas, Paint, Surface, Budgeted, Rect, colors};
|
|
|
|
|
use skulpin::skia_safe::gpu::SurfaceOrigin;
|
|
|
|
@ -42,44 +44,44 @@ impl Renderer {
|
|
|
|
|
self.font_height = font_height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn compute_text_region(&self, text: &str, grid_pos: (u64, u64), size: u16) -> Rect {
|
|
|
|
|
fn compute_text_region(&self, text: &str, grid_pos: (u64, u64), cell_width: u64, size: u16) -> Rect {
|
|
|
|
|
let (grid_x, grid_y) = grid_pos;
|
|
|
|
|
let x = grid_x as f32 * self.font_width;
|
|
|
|
|
let y = grid_y as f32 * self.font_height;
|
|
|
|
|
let width = text.graphemes(true).count() as f32 * self.font_width * size as f32;
|
|
|
|
|
let width = cell_width as f32 * self.font_width * size as f32;
|
|
|
|
|
let height = self.font_height * size as f32;
|
|
|
|
|
Rect::new(x, y, x + width, y + height)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw_background(&mut self, canvas: &mut Canvas, text: &str, grid_pos: (u64, u64), size: u16, style: &Option<Style>, default_colors: &Colors) {
|
|
|
|
|
let region = self.compute_text_region(text, grid_pos, size);
|
|
|
|
|
let style = style.clone().unwrap_or_else(|| Style::new(default_colors.clone()));
|
|
|
|
|
fn draw_background(&mut self, canvas: &mut Canvas, text: &str, grid_pos: (u64, u64), cell_width:u64, size: u16, style: &Option<Arc<Style>>, default_style: &Arc<Style>) {
|
|
|
|
|
let region = self.compute_text_region(text, grid_pos, cell_width, size);
|
|
|
|
|
let style = style.as_ref().unwrap_or(default_style);
|
|
|
|
|
|
|
|
|
|
self.paint.set_color(style.background(default_colors).to_color());
|
|
|
|
|
self.paint.set_color(style.background(&default_style.colors).to_color());
|
|
|
|
|
canvas.draw_rect(region, &self.paint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw_foreground(&mut self, canvas: &mut Canvas, text: &str, grid_pos: (u64, u64), size: u16, style: &Option<Style>, default_colors: &Colors) {
|
|
|
|
|
fn draw_foreground(&mut self, canvas: &mut Canvas, text: &str, grid_pos: (u64, u64), cell_width: u64, size: u16, style: &Option<Arc<Style>>, default_style: &Arc<Style>) {
|
|
|
|
|
let (grid_x, grid_y) = grid_pos;
|
|
|
|
|
let x = grid_x as f32 * self.font_width;
|
|
|
|
|
let y = grid_y as f32 * self.font_height;
|
|
|
|
|
let width = text.graphemes(true).count() as f32 * self.font_width;
|
|
|
|
|
let width = cell_width as f32 * self.font_width;
|
|
|
|
|
|
|
|
|
|
let style = style.clone().unwrap_or_else(|| Style::new(default_colors.clone()));
|
|
|
|
|
let style = style.as_ref().unwrap_or(default_style);
|
|
|
|
|
|
|
|
|
|
canvas.save();
|
|
|
|
|
|
|
|
|
|
let region = self.compute_text_region(text, grid_pos, size);
|
|
|
|
|
let region = self.compute_text_region(text, grid_pos, cell_width, size);
|
|
|
|
|
|
|
|
|
|
canvas.clip_rect(region, None, Some(false));
|
|
|
|
|
|
|
|
|
|
if style.underline || style.undercurl {
|
|
|
|
|
let line_position = self.shaper.underline_position();
|
|
|
|
|
self.paint.set_color(style.special(&default_colors).to_color());
|
|
|
|
|
self.paint.set_color(style.special(&default_style.colors).to_color());
|
|
|
|
|
canvas.draw_line((x, y - line_position + self.font_height), (x + width, y - line_position + self.font_height), &self.paint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.paint.set_color(style.foreground(&default_colors).to_color());
|
|
|
|
|
self.paint.set_color(style.foreground(&default_style.colors).to_color());
|
|
|
|
|
let text = text.trim_end();
|
|
|
|
|
if !text.is_empty() {
|
|
|
|
|
for blob in self.shaper.shape_cached(text, style.bold, style.italic).iter() {
|
|
|
|
@ -89,7 +91,7 @@ impl Renderer {
|
|
|
|
|
|
|
|
|
|
if style.strikethrough {
|
|
|
|
|
let line_position = region.center_y();
|
|
|
|
|
self.paint.set_color(style.special(&default_colors).to_color());
|
|
|
|
|
self.paint.set_color(style.special(&default_style.colors).to_color());
|
|
|
|
|
canvas.draw_line((x, line_position), (x + width, line_position), &self.paint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -97,11 +99,11 @@ impl Renderer {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn draw(&mut self, gpu_canvas: &mut Canvas, coordinate_system_helper: &CoordinateSystemHelper) -> bool {
|
|
|
|
|
let ((draw_commands, should_clear), default_colors, cursor, font_name, font_size) = {
|
|
|
|
|
let ((draw_commands, should_clear), default_style, cursor, font_name, font_size) = {
|
|
|
|
|
let mut editor = EDITOR.lock().unwrap();
|
|
|
|
|
(
|
|
|
|
|
editor.build_draw_commands(),
|
|
|
|
|
editor.default_colors.clone(),
|
|
|
|
|
editor.default_style.clone(),
|
|
|
|
|
editor.cursor.clone(),
|
|
|
|
|
editor.font_name.clone(),
|
|
|
|
|
editor.font_size
|
|
|
|
@ -126,7 +128,7 @@ impl Renderer {
|
|
|
|
|
let surface_origin = SurfaceOrigin::TopLeft;
|
|
|
|
|
let mut surface = Surface::new_render_target(&mut context, budgeted, &image_info, None, surface_origin, None, None).expect("Could not create surface");
|
|
|
|
|
let canvas = surface.canvas();
|
|
|
|
|
canvas.clear(default_colors.background.clone().unwrap().to_color());
|
|
|
|
|
canvas.clear(default_style.colors.background.clone().unwrap().to_color());
|
|
|
|
|
surface
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -134,10 +136,10 @@ impl Renderer {
|
|
|
|
|
coordinate_system_helper.use_logical_coordinates(&mut canvas);
|
|
|
|
|
|
|
|
|
|
for command in draw_commands.iter() {
|
|
|
|
|
self.draw_background(&mut canvas, &command.text, command.grid_position.clone(), command.scale, &command.style, &default_colors);
|
|
|
|
|
self.draw_background(&mut canvas, &command.text, command.grid_position.clone(), command.cell_width, command.scale, &command.style, &default_style);
|
|
|
|
|
}
|
|
|
|
|
for command in draw_commands.iter() {
|
|
|
|
|
self.draw_foreground(&mut canvas, &command.text, command.grid_position.clone(), command.scale, &command.style, &default_colors);
|
|
|
|
|
self.draw_foreground(&mut canvas, &command.text, command.grid_position.clone(), command.cell_width, command.scale, &command.style, &default_style);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let image = surface.image_snapshot();
|
|
|
|
@ -148,7 +150,7 @@ impl Renderer {
|
|
|
|
|
self.surface = Some(surface);
|
|
|
|
|
|
|
|
|
|
self.cursor_renderer.draw(
|
|
|
|
|
cursor, &default_colors,
|
|
|
|
|
cursor, &default_style.colors,
|
|
|
|
|
self.font_width, self.font_height,
|
|
|
|
|
&mut self.paint, &mut self.shaper,
|
|
|
|
|
gpu_canvas);
|
|
|
|
|