neovim-gtk/src/render/mod.rs

185 lines
5.2 KiB
Rust
Raw Normal View History

2017-08-24 14:41:20 +00:00
mod context;
2017-09-11 15:00:51 +00:00
mod itemize;
2017-09-12 15:28:01 +00:00
mod model_clip_iterator;
2017-08-24 14:41:20 +00:00
2017-08-31 15:37:55 +00:00
pub use self::context::Context;
2017-09-05 15:23:46 +00:00
pub use self::context::CellMetrics;
2017-09-12 15:28:01 +00:00
use self::model_clip_iterator::ModelClipIteratorFactory;
2017-08-31 15:37:55 +00:00
2017-09-07 15:51:12 +00:00
use mode;
2017-08-31 15:37:55 +00:00
use color;
use sys::pango::*;
use pango;
use cairo;
2017-09-07 15:51:12 +00:00
use cursor;
2017-08-26 20:17:09 +00:00
use pangocairo::CairoContextExt;
2017-08-24 14:41:20 +00:00
use ui_model;
2017-08-25 15:32:30 +00:00
pub fn render(
ctx: &cairo::Context,
2017-09-07 15:51:12 +00:00
cursor: &cursor::Cursor,
2017-09-06 14:18:30 +00:00
font_ctx: &context::Context,
2017-08-31 15:37:55 +00:00
ui_model: &ui_model::UiModel,
color_model: &color::ColorModel,
2017-09-07 15:51:12 +00:00
mode: &mode::Mode,
2017-08-25 15:32:30 +00:00
) {
2017-08-31 15:37:55 +00:00
ctx.set_source_rgb(
color_model.bg_color.0,
color_model.bg_color.1,
color_model.bg_color.2,
);
ctx.paint();
2017-08-25 15:32:30 +00:00
2017-09-12 20:27:45 +00:00
let cell_metrics = font_ctx.cell_metrics();
2017-09-06 14:18:30 +00:00
let &CellMetrics {
line_height,
char_width,
2017-09-09 20:02:06 +00:00
underline_position,
underline_thickness,
2017-09-06 14:18:30 +00:00
..
2017-09-12 20:27:45 +00:00
} = cell_metrics;
2017-09-07 15:51:12 +00:00
let (cursor_row, cursor_col) = ui_model.get_cursor();
2017-08-26 20:17:09 +00:00
2017-09-12 20:27:45 +00:00
for (row, line) in ui_model.get_clip_iterator(ctx, cell_metrics) {
2017-09-12 15:28:01 +00:00
let line_y = row as f64 * line_height;
2017-08-27 19:29:43 +00:00
let mut line_x = 0.0;
2017-08-31 15:37:55 +00:00
2017-09-07 15:51:12 +00:00
for col in 0..line.line.len() {
let cell = &line.line[col];
2017-09-05 15:23:46 +00:00
2017-09-06 14:18:30 +00:00
2017-09-12 20:27:45 +00:00
draw_cell(
ctx,
cell_metrics,
color_model,
line,
cell,
col,
line_x,
line_y,
);
2017-09-07 15:51:12 +00:00
2017-09-09 20:02:06 +00:00
if cell.attrs.underline || cell.attrs.undercurl {
if cell.attrs.undercurl {
let sp = color_model.actual_cell_sp(cell);
ctx.set_source_rgba(sp.0, sp.1, sp.2, 0.7);
2017-09-12 12:09:17 +00:00
let max_undercurl_height = (line_height - underline_position) * 2.0;
let undercurl_height = (underline_thickness * 4.0).min(max_undercurl_height);
let undercurl_y = line_y + underline_position - undercurl_height / 2.0;
2017-09-12 20:27:45 +00:00
ctx.show_error_underline(line_x, undercurl_y, char_width, undercurl_height);
2017-09-09 20:02:06 +00:00
} else if cell.attrs.underline {
2017-09-12 20:27:45 +00:00
let fg = color_model.actual_cell_fg(cell);
2017-09-09 20:02:06 +00:00
ctx.set_source_rgb(fg.0, fg.1, fg.2);
ctx.set_line_width(underline_thickness);
ctx.move_to(line_x, line_y + underline_position);
ctx.line_to(line_x + char_width, line_y + underline_position);
ctx.stroke();
}
}
2017-09-07 15:51:12 +00:00
if row == cursor_row && col == cursor_col {
2017-09-07 19:53:32 +00:00
ctx.move_to(line_x, line_y);
2017-09-07 15:51:12 +00:00
cursor.draw(
ctx,
font_ctx,
mode,
line_y,
false, //TODO: double_width,
color_model.actual_cell_bg(cell),
);
}
2017-08-27 19:29:43 +00:00
line_x += char_width;
2017-08-25 15:32:30 +00:00
}
}
}
2017-09-12 20:27:45 +00:00
fn draw_cell(
ctx: &cairo::Context,
cell_metrics: &CellMetrics,
color_model: &color::ColorModel,
line: &ui_model::Line,
cell: &ui_model::Cell,
col: usize,
line_x: f64,
line_y: f64,
) {
let &CellMetrics {
char_width,
line_height,
ascent,
..
} = cell_metrics;
let (bg, fg) = color_model.cell_colors(cell);
if let Some(item) = line.item_line[col].as_ref() {
if let Some(bg) = bg {
ctx.set_source_rgb(bg.0, bg.1, bg.2);
ctx.rectangle(
line_x,
line_y,
char_width * line.item_len_from_idx(col) as f64,
line_height,
);
ctx.fill();
}
if let Some(ref glyphs) = item.glyphs {
ctx.move_to(line_x, line_y + ascent);
ctx.set_source_rgb(fg.0, fg.1, fg.2);
ctx.show_glyph_string(item.font(), glyphs);
}
} else if !line.is_binded_to_item(col) {
let bg = color_model.cell_bg(cell);
if let Some(bg) = bg {
ctx.set_source_rgb(bg.0, bg.1, bg.2);
ctx.rectangle(line_x, line_y, char_width, line_height);
ctx.fill();
}
}
}
2017-09-04 15:32:12 +00:00
pub fn shape_dirty(
ctx: &context::Context,
ui_model: &mut ui_model::UiModel,
color_model: &color::ColorModel,
) {
2017-08-25 15:32:30 +00:00
for line in ui_model.model_mut() {
if line.dirty_line {
2017-09-04 15:32:12 +00:00
let styled_line = ui_model::StyledLine::from(line, color_model);
2017-08-25 15:32:30 +00:00
let items = ctx.itemize(&styled_line);
line.merge(&styled_line, &items);
for i in 0..line.line.len() {
if line[i].dirty {
2017-09-11 15:31:15 +00:00
if let Some(item) = line.get_item_mut(i) {
2017-08-27 19:29:43 +00:00
let mut glyphs = pango::GlyphString::new();
{
let analysis = item.analysis();
let (offset, length, _) = item.item.offset();
pango_shape(
&styled_line.line_str,
offset,
length,
&analysis,
&mut glyphs,
);
}
2017-08-25 15:32:30 +00:00
2017-09-08 15:26:16 +00:00
item.set_glyphs(ctx, glyphs);
2017-08-27 19:29:43 +00:00
}
2017-08-25 15:32:30 +00:00
}
line[i].dirty = false;
}
line.dirty_line = false;
}
2017-08-23 09:45:56 +00:00
}
}