Fix cursor bold and italic rendering (#1136)

* Fix cursor bold and italic rendering

* Rename character to grid cell

* Rename character -> grid_cell in Cursor struct

* Rename get_cursor_character -> get_cursor_grid_cell
macos-click-through
Eric Pfister 3 years ago committed by GitHub
parent 224f1f2083
commit f5bb8cc89b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,6 +4,8 @@ use skia_safe::Color4f;
use crate::editor::style::{Colors, Style}; use crate::editor::style::{Colors, Style};
use super::grid::GridCell;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum CursorShape { pub enum CursorShape {
Block, Block,
@ -44,7 +46,7 @@ pub struct Cursor {
pub style: Option<Arc<Style>>, pub style: Option<Arc<Style>>,
pub enabled: bool, pub enabled: bool,
pub double_width: bool, pub double_width: bool,
pub character: String, pub grid_cell: GridCell,
} }
impl Cursor { impl Cursor {
@ -60,7 +62,7 @@ impl Cursor {
blinkoff: None, blinkoff: None,
enabled: true, enabled: true,
double_width: false, double_width: false,
character: " ".to_string(), grid_cell: (" ".to_string(), None),
} }
} }

@ -387,12 +387,12 @@ impl Editor {
fn send_cursor_info(&mut self) { fn send_cursor_info(&mut self) {
let (grid_left, grid_top) = self.cursor.grid_position; let (grid_left, grid_top) = self.cursor.grid_position;
if let Some(window) = self.windows.get(&self.cursor.parent_window_id) { if let Some(window) = self.windows.get(&self.cursor.parent_window_id) {
let (character, double_width) = window.get_cursor_character(grid_left, grid_top); let (character, style, double_width) = window.get_cursor_grid_cell(grid_left, grid_top);
self.cursor.character = character; self.cursor.grid_cell = (character, style);
self.cursor.double_width = double_width; self.cursor.double_width = double_width;
} else { } else {
self.cursor.double_width = false; self.cursor.double_width = false;
self.cursor.character = " ".to_string(); self.cursor.grid_cell = (" ".to_string(), None);
} }
self.draw_command_batcher self.draw_command_batcher
.queue(DrawCommand::UpdateCursor(self.cursor.clone())) .queue(DrawCommand::UpdateCursor(self.cursor.clone()))

@ -63,10 +63,14 @@ impl Window {
}); });
} }
pub fn get_cursor_character(&self, window_left: u64, window_top: u64) -> (String, bool) { pub fn get_cursor_grid_cell(
let character = match self.grid.get_cell(window_left, window_top) { &self,
Some((character, _)) => character.clone(), window_left: u64,
_ => ' '.to_string(), window_top: u64,
) -> (String, Option<Arc<Style>>, bool) {
let grid_cell = match self.grid.get_cell(window_left, window_top) {
Some((character, style)) => (character.clone(), style.clone()),
_ => (' '.to_string(), None),
}; };
let double_width = match self.grid.get_cell(window_left + 1, window_top) { let double_width = match self.grid.get_cell(window_left + 1, window_top) {
@ -74,7 +78,7 @@ impl Window {
_ => false, _ => false,
}; };
(character, double_width) (grid_cell.0, grid_cell.1, double_width)
} }
pub fn get_width(&self) -> u64 { pub fn get_width(&self) -> u64 {

@ -273,7 +273,7 @@ impl CursorRenderer {
let mut paint = Paint::new(skia_safe::colors::WHITE, None); let mut paint = Paint::new(skia_safe::colors::WHITE, None);
paint.set_anti_alias(settings.antialiasing); paint.set_anti_alias(settings.antialiasing);
let character = self.cursor.character.clone(); let character = self.cursor.grid_cell.0.clone();
let mut cursor_width = grid_renderer.font_dimensions.width; let mut cursor_width = grid_renderer.font_dimensions.width;
if self.cursor.double_width && self.cursor.shape == CursorShape::Block { if self.cursor.double_width && self.cursor.shape == CursorShape::Block {
@ -341,52 +341,58 @@ impl CursorRenderer {
self.previous_editor_mode = current_mode.clone(); self.previous_editor_mode = current_mode.clone();
} }
if self.cursor.enabled && render { if !(self.cursor.enabled && render) {
// Draw Background return;
let background_color = self }
.cursor // Draw Background
.background(&grid_renderer.default_style.colors) let background_color = self
.to_color(); .cursor
paint.set_color(background_color); .background(&grid_renderer.default_style.colors)
.to_color();
// The cursor is made up of four points, so I create a path with each of the four paint.set_color(background_color);
// corners.
let mut path = Path::new(); // The cursor is made up of four points, so I create a path with each of the four
// corners.
path.move_to(self.corners[0].current_position); let mut path = Path::new();
path.line_to(self.corners[1].current_position);
path.line_to(self.corners[2].current_position); path.move_to(self.corners[0].current_position);
path.line_to(self.corners[3].current_position); path.line_to(self.corners[1].current_position);
path.close(); path.line_to(self.corners[2].current_position);
path.line_to(self.corners[3].current_position);
canvas.draw_path(&path, &paint); path.close();
// Draw foreground canvas.draw_path(&path, &paint);
let foreground_color = self
.cursor // Draw foreground
.foreground(&grid_renderer.default_style.colors) let foreground_color = self
.to_color(); .cursor
paint.set_color(foreground_color); .foreground(&grid_renderer.default_style.colors)
.to_color();
canvas.save(); paint.set_color(foreground_color);
canvas.clip_path(&path, None, Some(false));
canvas.save();
let y_adjustment = grid_renderer.shaper.y_adjustment(); canvas.clip_path(&path, None, Some(false));
let blobs = &grid_renderer.shaper.shape_cached(character, false, false);
let y_adjustment = grid_renderer.shaper.y_adjustment();
for blob in blobs.iter() { let style = &self.cursor.grid_cell.1;
canvas.draw_text_blob(
&blob, let bold = style.as_ref().map(|x| x.bold).unwrap_or(false);
(self.destination.x, self.destination.y + y_adjustment as f32), let italic = style.as_ref().map(|x| x.italic).unwrap_or(false);
&paint,
); let blobs = &grid_renderer.shaper.shape_cached(character, bold, italic);
}
for blob in blobs.iter() {
canvas.draw_text_blob(
&blob,
(self.destination.x, self.destination.y + y_adjustment as f32),
&paint,
);
}
canvas.restore(); canvas.restore();
if let Some(vfx) = self.cursor_vfx.as_ref() { if let Some(vfx) = self.cursor_vfx.as_ref() {
vfx.render(&settings, canvas, grid_renderer, &self.cursor); vfx.render(&settings, canvas, grid_renderer, &self.cursor);
}
} }
} }
} }

Loading…
Cancel
Save