Same attribute for same properties

This commit is contained in:
daa84 2017-09-06 12:05:12 +03:00
parent 26493977be
commit 2f1f4e02c3

View File

@ -267,7 +267,7 @@ impl StyledLine {
let mut cell_to_byte = Vec::new(); let mut cell_to_byte = Vec::new();
let attr_list = pango::AttrList::new(); let attr_list = pango::AttrList::new();
let mut byte_offset = 0; let mut byte_offset = 0;
//FIXME: generate single attr for same atttributes values let mut style_attr = StyleAttr::new();
for (cell_idx, cell) in line.line.iter().enumerate() { for (cell_idx, cell) in line.line.iter().enumerate() {
if cell.attrs.double_width { if cell.attrs.double_width {
@ -281,19 +281,17 @@ impl StyledLine {
cell_to_byte.push(cell_idx); cell_to_byte.push(cell_idx);
} }
if !cell.ch.is_whitespace() { let next = style_attr.next(byte_offset, byte_offset + len, cell, color_model);
insert_attrs( if let Some(next) = next {
cell, style_attr.insert(&attr_list);
color_model, style_attr = next;
&attr_list,
byte_offset as u32,
(byte_offset + len) as u32,
);
} }
byte_offset += len; byte_offset += len;
} }
style_attr.insert(&attr_list);
StyledLine { StyledLine {
line_str, line_str,
cell_to_byte: cell_to_byte.into_boxed_slice(), cell_to_byte: cell_to_byte.into_boxed_slice(),
@ -302,36 +300,106 @@ impl StyledLine {
} }
} }
fn insert_attrs( struct StyleAttr<'c> {
cell: &Cell, italic: bool,
color_model: &color::ColorModel, bold: bool,
attr_list: &pango::AttrList, foreground: Option<&'c color::Color>,
start_idx: u32, empty: bool,
end_idx: u32,
) { start_idx: usize,
if cell.attrs.italic { end_idx: usize,
let mut attr = pango::Attribute::new_style(pango::Style::Italic).unwrap(); }
attr.set_start_index(start_idx);
attr.set_end_index(end_idx); impl<'c> StyleAttr<'c> {
attr_list.insert(attr); fn new() -> Self {
StyleAttr {
italic: false,
bold: false,
foreground: None,
empty: true,
start_idx: 0,
end_idx: 0,
}
} }
if cell.attrs.bold { fn from(
let mut attr = pango::Attribute::new_weight(pango::Weight::Bold).unwrap(); start_idx: usize,
attr.set_start_index(start_idx); end_idx: usize,
attr.set_end_index(end_idx); cell: &'c Cell,
attr_list.insert(attr); color_model: &'c color::ColorModel,
) -> Self {
StyleAttr {
italic: cell.attrs.italic,
bold: cell.attrs.bold,
foreground: color_model.cell_fg(cell),
empty: false,
start_idx,
end_idx,
}
} }
if let Some(fg) = color_model.cell_fg(cell) { fn next(
let (r, g, b) = fg.to_u16(); &mut self,
let mut attr = pango::Attribute::new_foreground(r, g, b).unwrap(); start_idx: usize,
attr.set_start_index(start_idx); end_idx: usize,
attr.set_end_index(end_idx); cell: &'c Cell,
color_model: &'c color::ColorModel,
) -> Option<StyleAttr<'c>> {
let style_attr = Self::from(start_idx, end_idx, cell, color_model);
if self != &style_attr {
Some(style_attr)
} else {
self.end_idx = end_idx;
None
}
}
fn insert(&self, attr_list: &pango::AttrList) {
if self.empty {
return;
}
if self.italic {
self.insert_attr(
attr_list,
pango::Attribute::new_style(pango::Style::Italic).unwrap(),
);
}
if self.bold {
self.insert_attr(
attr_list,
pango::Attribute::new_weight(pango::Weight::Bold).unwrap(),
);
}
if let Some(fg) = self.foreground {
let (r, g, b) = fg.to_u16();
self.insert_attr(
attr_list,
pango::Attribute::new_foreground(r, g, b).unwrap(),
);
}
}
#[inline]
fn insert_attr(&self, attr_list: &pango::AttrList, mut attr: pango::Attribute) {
attr.set_start_index(self.start_idx as u32);
attr.set_end_index(self.end_idx as u32);
attr_list.insert(attr); attr_list.insert(attr);
} }
} }
impl<'c> PartialEq for StyleAttr<'c> {
fn eq(&self, other: &Self) -> bool {
self.italic == other.italic && self.bold == other.bold &&
self.foreground == other.foreground && self.empty == other.empty
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;