neovim-gtk/src/ui_model/line.rs

242 lines
6.4 KiB
Rust
Raw Normal View History

2017-08-27 19:29:43 +00:00
use std;
2017-08-24 14:41:20 +00:00
use std::ops::{Index, IndexMut};
2017-08-23 09:45:56 +00:00
use super::cell::Cell;
2017-08-26 20:17:09 +00:00
use sys::pango as sys_pango;
2017-08-24 14:41:20 +00:00
use pango;
pub struct Item {
2017-08-25 15:32:30 +00:00
pub item: sys_pango::Item,
pub glyphs: Option<pango::GlyphString>,
2017-08-26 20:17:09 +00:00
pub ink_rect: Option<pango::Rectangle>,
font: pango::Font,
2017-08-24 14:41:20 +00:00
}
impl Item {
2017-08-25 15:32:30 +00:00
pub fn new(item: sys_pango::Item) -> Self {
2017-08-24 14:41:20 +00:00
Item {
2017-08-26 20:17:09 +00:00
font: item.analysis().font(),
2017-08-24 14:41:20 +00:00
item,
2017-08-25 15:32:30 +00:00
glyphs: None,
2017-08-26 20:17:09 +00:00
ink_rect: None,
2017-08-24 14:41:20 +00:00
}
}
2017-08-25 15:32:30 +00:00
pub fn update(&mut self, item: sys_pango::Item) {
2017-08-26 20:17:09 +00:00
self.font = item.analysis().font();
2017-08-25 15:32:30 +00:00
self.item = item;
self.glyphs = None;
2017-08-26 20:17:09 +00:00
self.ink_rect = None;
2017-08-25 15:32:30 +00:00
}
pub fn set_glyphs(&mut self, glyphs: pango::GlyphString) {
2017-08-26 20:17:09 +00:00
let mut glyphs = glyphs;
let (ink_rect, _) = glyphs.extents(&self.font);
self.ink_rect = Some(ink_rect);
2017-08-25 15:32:30 +00:00
self.glyphs = Some(glyphs);
}
2017-08-26 20:17:09 +00:00
pub fn font(&self) -> &pango::Font {
&self.font
}
pub fn analysis(&self) -> sys_pango::Analysis {
self.item.analysis()
}
2017-08-24 14:41:20 +00:00
}
2017-08-23 09:45:56 +00:00
pub struct Line {
2017-08-24 14:41:20 +00:00
pub line: Box<[Cell]>,
2017-08-25 15:32:30 +00:00
// format of item line is
// [Item1, Item2, None, None, Item3]
// Item2 take 3 cells and renders as one
pub item_line: Box<[Option<Item>]>,
2017-08-27 19:29:43 +00:00
cell_to_item: Box<[i32]>,
2017-08-25 15:32:30 +00:00
item_line_empty: bool,
pub dirty_line: bool,
2017-08-23 09:45:56 +00:00
}
impl Line {
pub fn new(columns: usize) -> Self {
2017-08-24 14:41:20 +00:00
let mut line = Vec::with_capacity(columns);
2017-08-23 09:45:56 +00:00
for _ in 0..columns {
line.push(Cell::new(' '));
}
2017-08-25 15:32:30 +00:00
let mut item_line = Vec::with_capacity(columns);
for _ in 0..columns {
item_line.push(None);
}
2017-08-23 09:45:56 +00:00
2017-08-24 14:41:20 +00:00
Line {
2017-08-23 09:45:56 +00:00
line: line.into_boxed_slice(),
2017-08-25 15:32:30 +00:00
item_line: item_line.into_boxed_slice(),
2017-08-27 19:29:43 +00:00
cell_to_item: vec![-1; columns].into_boxed_slice(),
2017-08-25 15:32:30 +00:00
dirty_line: false,
item_line_empty: true,
2017-08-23 09:45:56 +00:00
}
}
2017-08-24 14:41:20 +00:00
pub fn clear(&mut self, left: usize, right: usize) {
for cell in &mut self.line[left..right + 1] {
cell.clear();
}
2017-08-25 15:32:30 +00:00
}
pub fn merge(&mut self, old_items: &StyledLine, new_items: &[sys_pango::Item]) {
for new_item in new_items {
2017-08-27 19:29:43 +00:00
//FIXME: clear empty cells
2017-08-25 15:32:30 +00:00
let (offset, length, _) = new_item.offset();
let start_cell = old_items.cell_to_byte[offset];
let end_cell = old_items.cell_to_byte[offset + length - 1];
// first time initialization
2017-08-27 19:29:43 +00:00
// as cell_to_item points to wrong values
2017-08-25 15:32:30 +00:00
if !self.item_line_empty {
let start_item = self.cell_to_item(start_cell);
let end_item = self.cell_to_item(end_cell);
// in case different item length was in previous iteration
// mark all item as dirty
if start_item != end_item {
2017-08-26 16:53:37 +00:00
self.initialize_cell_item(start_cell, end_cell, new_item);
2017-08-25 15:32:30 +00:00
} else {
2017-08-27 19:29:43 +00:00
self.item_line[start_cell].as_mut().unwrap().update(
2017-08-25 15:32:30 +00:00
new_item.clone(),
);
}
} else {
2017-08-26 16:53:37 +00:00
self.initialize_cell_item(start_cell, end_cell, new_item);
2017-08-25 15:32:30 +00:00
}
}
self.item_line_empty = false;
}
2017-08-27 19:29:43 +00:00
fn initialize_cell_item(
&mut self,
start_cell: usize,
end_cell: usize,
new_item: &sys_pango::Item,
) {
for i in start_cell..end_cell + 1 {
2017-08-25 15:32:30 +00:00
self.line[i].dirty = true;
2017-08-27 19:29:43 +00:00
self.cell_to_item[i] = start_cell as i32;
2017-08-25 15:32:30 +00:00
}
for i in start_cell + 1..end_cell {
self.item_line[i] = None;
}
self.item_line[start_cell] = Some(Item::new(new_item.clone()));
}
pub fn mark_dirty_cell(&mut self, idx: usize) {
self.line[idx].dirty = true;
}
pub fn get_item_mut(&mut self, cell_idx: usize) -> Option<&mut Item> {
2017-08-27 19:29:43 +00:00
let item_idx = self.cell_to_item(cell_idx);
if item_idx >= 0 {
self.item_line[item_idx as usize].as_mut()
} else {
None
2017-08-25 15:32:30 +00:00
}
2017-08-27 19:29:43 +00:00
}
2017-08-24 14:41:20 +00:00
2017-08-27 19:29:43 +00:00
fn cell_to_item(&self, cell_idx: usize) -> i32 {
self.cell_to_item[cell_idx]
2017-08-24 14:41:20 +00:00
}
}
impl Index<usize> for Line {
type Output = Cell;
fn index(&self, index: usize) -> &Cell {
&self.line[index]
}
}
impl IndexMut<usize> for Line {
fn index_mut(&mut self, index: usize) -> &mut Cell {
&mut self.line[index]
}
}
pub struct StyledLine {
pub line_str: String,
cell_to_byte: Box<[usize]>,
pub attr_list: pango::AttrList,
}
impl StyledLine {
pub fn from(line: &Line) -> Self {
let mut line_str = String::new();
let mut cell_to_byte = Vec::new();
let attr_list = pango::AttrList::new();
let mut byte_offset = 0;
for (cell_idx, cell) in line.line.iter().enumerate() {
2017-08-27 19:29:43 +00:00
if cell.attrs.double_width || cell.ch.is_whitespace() {
2017-08-24 14:41:20 +00:00
continue;
}
line_str.push(cell.ch);
2017-08-26 16:53:37 +00:00
let len = line_str.len() - byte_offset;
2017-08-24 14:41:20 +00:00
2017-08-26 16:53:37 +00:00
for _ in 0..len {
2017-08-24 14:41:20 +00:00
cell_to_byte.push(cell_idx);
}
2017-08-25 15:32:30 +00:00
insert_attrs(
cell,
&attr_list,
byte_offset as u32,
(byte_offset + len) as u32,
);
2017-08-24 14:41:20 +00:00
byte_offset += len;
}
StyledLine {
line_str,
cell_to_byte: cell_to_byte.into_boxed_slice(),
attr_list,
}
}
}
fn insert_attrs(cell: &Cell, attr_list: &pango::AttrList, start_idx: u32, end_idx: u32) {
if cell.attrs.italic {
let mut attr = pango::Attribute::new_style(pango::Style::Italic).unwrap();
attr.set_start_index(start_idx);
attr.set_end_index(end_idx);
attr_list.insert(attr);
}
if cell.attrs.bold {
let mut attr = pango::Attribute::new_weight(pango::Weight::Bold).unwrap();
attr.set_start_index(start_idx);
attr.set_end_index(end_idx);
attr_list.insert(attr);
}
2017-08-23 09:45:56 +00:00
}
2017-08-26 16:53:37 +00:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_styled_line() {
let mut line = Line::new(3);
line[0].ch = 'a';
line[1].ch = 'b';
line[2].ch = 'c';
let styled_line = StyledLine::from(&line);
assert_eq!("abc", styled_line.line_str);
assert_eq!(3, styled_line.cell_to_byte.len());
assert_eq!(0, styled_line.cell_to_byte[0]);
assert_eq!(1, styled_line.cell_to_byte[1]);
assert_eq!(2, styled_line.cell_to_byte[2]);
}
}