neovim-gtk/src/render/mod.rs

94 lines
2.8 KiB
Rust
Raw Normal View History

2017-08-24 14:41:20 +00:00
mod context;
use sys::pango::*;
use pango;
use cairo;
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,
font_desc: pango::FontDescription,
2017-08-26 20:17:09 +00:00
line_height: f64,
char_width: f64,
2017-08-25 15:32:30 +00:00
ui_model: &mut ui_model::UiModel,
) {
let font_ctx = context::Context::new(&font_desc);
shape_dirty(&font_ctx, ui_model);
2017-08-26 20:17:09 +00:00
let mut line_y = line_height;
2017-08-25 15:32:30 +00:00
for line in ui_model.model_mut() {
2017-08-27 19:29:43 +00:00
let mut line_x = 0.0;
2017-08-25 15:32:30 +00:00
for i in 0..line.line.len() {
2017-08-27 19:29:43 +00:00
ctx.move_to(line_x, line_y);
2017-08-25 15:32:30 +00:00
let item = line.item_line[i].as_ref();
if let Some(item) = item {
if let Some(ref glyphs) = item.glyphs {
2017-08-26 20:17:09 +00:00
ctx.show_glyph_string(item.font(), glyphs);
2017-08-25 15:32:30 +00:00
}
}
2017-08-27 19:29:43 +00:00
line_x += char_width;
2017-08-25 15:32:30 +00:00
}
2017-08-26 20:17:09 +00:00
line_y += line_height;
2017-08-25 15:32:30 +00:00
}
}
fn shape_dirty(ctx: &context::Context, ui_model: &mut ui_model::UiModel) {
for line in ui_model.model_mut() {
if line.dirty_line {
let styled_line = ui_model::StyledLine::from(line);
let items = ctx.itemize(&styled_line);
line.merge(&styled_line, &items);
for i in 0..line.line.len() {
if line[i].dirty {
2017-08-28 15:05:58 +00:00
// FIXME: dont shape/render empty items(space cells)
2017-08-27 19:29:43 +00:00
if let Some(mut item) = line.get_item_mut(i) {
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-08-27 19:29:43 +00:00
item.set_glyphs(glyphs);
}
2017-08-25 15:32:30 +00:00
}
line[i].dirty = false;
}
line.dirty_line = false;
}
2017-08-23 09:45:56 +00:00
}
}
2017-08-24 14:41:20 +00:00
2017-08-25 15:32:30 +00:00
//pub fn render_test(ctx: &cairo::Context, font_desc: pango::FontDescription) {
//let font_map = FontMap::get_default();
//let pango_context = font_map.create_context().unwrap();
//pango_context.set_font_description(&font_desc);
//let text = "TEST String".to_owned();
//let attr_list = pango::AttrList::new();
//ctx.move_to(0.0, 50.0);
//let items = pango_itemize(&pango_context, &text, &attr_list);
//for item in items {
//let mut glyphs = pango::GlyphString::new();
//let analysis = item.analysis();
//pango_shape(&text, &analysis, &mut glyphs);
//let font = analysis.font();
//let (ink, logical) = glyphs.extents(&font);
//ctx.show_glyph_string(&font, &glyphs);
//}
//}