This commit is contained in:
daa84
2017-08-24 17:41:20 +03:00
parent aad5dc2112
commit 17063d5216
5 changed files with 155 additions and 30 deletions

View File

@@ -1,23 +1,113 @@
use std::ops::{Index, IndexMut};
use super::cell::Cell;
use pango::Item;
use pango;
pub struct Item {
item: pango::Item,
glyph_string: Option<pango::GlyphString>,
}
impl Item {
pub fn new(item: pango::Item) -> Self {
Item {
item,
glyph_string: None,
}
}
}
pub struct Line {
line: Box<[Cell]>,
pub line: Box<[Cell]>,
item_line: Option<Box<[Item]>>,
cell_to_item: Box<[usize]>,
}
impl Line {
pub fn new(columns: usize) -> Self {
let line = Vec::with_capacity(columns);
let mut line = Vec::with_capacity(columns);
for _ in 0..columns {
line.push(Cell::new(' '));
}
Line {
Line {
cell_to_item: Vec::with_capacity(line.len()).into_boxed_slice(),
line: line.into_boxed_slice(),
item_line: None,
}
}
pub fn clear(&mut self, left: usize, right: usize) {
for cell in &mut self.line[left..right + 1] {
cell.clear();
}
}
}
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() {
if cell.attrs.double_width {
continue;
}
line_str.push(cell.ch);
let len = line_str.len();
for i in byte_offset..byte_offset + len {
cell_to_byte.push(cell_idx);
}
insert_attrs(cell, &attr_list, byte_offset as u32, (byte_offset + len) as u32);
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);
}
}

View File

@@ -2,6 +2,7 @@ mod cell;
mod line;
pub use self::cell::{Cell, Attrs};
pub use self::line::{Item, StyledLine};
use self::line::Line;
use std::slice::Iter;
@@ -158,9 +159,7 @@ impl UiModel {
fn clear_region(&mut self, top: usize, bot: usize, left: usize, right: usize) {
for row in &mut self.model[top..bot + 1] {
for cell in &mut row[left..right + 1] {
cell.clear();
}
row.clear(left, right);
}
}
}
@@ -321,7 +320,7 @@ impl AsRef<ModelRect> for ModelRect {
pub struct ClipRowIterator<'a> {
rect: &'a ModelRect,
pos: usize,
iter: Iter<'a, Vec<Cell>>,
iter: Iter<'a, Line>,
}
impl<'a> ClipRowIterator<'a> {
@@ -347,11 +346,11 @@ impl<'a> Iterator for ClipRowIterator<'a> {
pub struct ClipLine<'a> {
rect: &'a ModelRect,
line: &'a [Cell],
line: &'a Line,
}
impl<'a> ClipLine<'a> {
pub fn new(model: &'a [Cell], rect: &'a ModelRect) -> ClipLine<'a> {
pub fn new(model: &'a Line, rect: &'a ModelRect) -> ClipLine<'a> {
ClipLine {
line: model,
rect: rect,
@@ -366,7 +365,7 @@ impl<'a> ClipLine<'a> {
}
pub fn get(&self, idx: usize) -> Option<&Cell> {
self.line.get(idx)
self.line.line.get(idx)
}
pub fn iter(&self) -> ClipColIterator<'a> {
@@ -381,11 +380,11 @@ pub struct ClipColIterator<'a> {
}
impl<'a> ClipColIterator<'a> {
pub fn new(model: &'a [Cell], rect: &'a ModelRect) -> ClipColIterator<'a> {
pub fn new(model: &'a Line, rect: &'a ModelRect) -> ClipColIterator<'a> {
ClipColIterator {
rect: rect,
pos: 0,
iter: model[rect.left..rect.right + 1].iter(),
iter: model.line[rect.left..rect.right + 1].iter(),
}
}
}