From aad5dc2112c89131b8fca12c24b67df654405e06 Mon Sep 17 00:00:00 2001 From: daa84 Date: Wed, 23 Aug 2017 12:45:56 +0300 Subject: [PATCH] Progress... --- src/color.rs | 19 ++++++ src/cursor.rs | 2 +- src/main.rs | 1 + src/render/mod.rs | 35 +++++----- src/shell.rs | 3 +- src/ui_model/cell.rs | 63 ++++++++++++++++++ src/ui_model/line.rs | 23 +++++++ src/{ui_model.rs => ui_model/mod.rs} | 98 +++------------------------- 8 files changed, 137 insertions(+), 107 deletions(-) create mode 100644 src/color.rs create mode 100644 src/ui_model/cell.rs create mode 100644 src/ui_model/line.rs rename src/{ui_model.rs => ui_model/mod.rs} (88%) diff --git a/src/color.rs b/src/color.rs new file mode 100644 index 0000000..b6257ee --- /dev/null +++ b/src/color.rs @@ -0,0 +1,19 @@ +use gdk; + +#[derive(Clone, PartialEq)] +pub struct Color(pub f64, pub f64, pub f64); + +pub const COLOR_BLACK: Color = Color(0.0, 0.0, 0.0); +pub const COLOR_WHITE: Color = Color(1.0, 1.0, 1.0); +pub const COLOR_RED: Color = Color(1.0, 0.0, 0.0); + +impl <'a> Into for &'a Color { + fn into(self) -> gdk::RGBA { + gdk::RGBA { + red: self.0, + green: self.1, + blue: self.2, + alpha: 1.0, + } + } +} diff --git a/src/cursor.rs b/src/cursor.rs index e97a2c2..2d534ac 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -1,5 +1,5 @@ use cairo; -use ui_model::Color; +use color::Color; use ui::UiMutex; use shell; use mode; diff --git a/src/main.rs b/src/main.rs index d8303e8..84c49c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,7 @@ extern crate toml; mod sys; +mod color; mod value; mod mode; mod ui_model; diff --git a/src/render/mod.rs b/src/render/mod.rs index c214611..8ba40db 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -2,26 +2,27 @@ use sys::pango::*; use pango; use pango::prelude::*; use cairo; -use pangocairo::CairoContextExt; +use pangocairo::{CairoContextExt, FontMap}; use std::ffi::CString; pub fn render(ctx: &cairo::Context, font_desc: pango::FontDescription) { - let pango_context = ctx.create_pango_context(); - pango_context.set_font_description(&font_desc); + 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().into_bytes(); - let len = text.len(); - let text = CString::new(text).unwrap(); - let attr_list = pango::AttrList::new(); + let text = "TEST String".to_owned().into_bytes(); + let len = text.len(); + let text = CString::new(text).unwrap(); + let attr_list = pango::AttrList::new(); - ctx.move_to(0.0, 50.0); - let items = pango_itemize(&pango_context, &text, 0, len, &attr_list); - for item in items { - let mut glyphs = pango::GlyphString::new(); - let analysis = item.analysis(); - pango_shape(&text, len, &analysis, &mut glyphs); - let font = analysis.font(); - let (ink, logical) = glyphs.extents(&font); - ctx.show_glyph_string(&font, &glyphs); - } + ctx.move_to(0.0, 50.0); + let items = pango_itemize(&pango_context, &text, 0, len, &attr_list); + for item in items { + let mut glyphs = pango::GlyphString::new(); + let analysis = item.analysis(); + pango_shape(&text, len, &analysis, &mut glyphs); + let font = analysis.font(); + let (ink, logical) = glyphs.extents(&font); + ctx.show_glyph_string(&font, &glyphs); + } } diff --git a/src/shell.rs b/src/shell.rs index ed0af3a..c79fd08 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -19,7 +19,8 @@ use neovim_lib::{Neovim, NeovimApi, Value}; use neovim_lib::neovim_api::Tabpage; use settings::{Settings, FontSource}; -use ui_model::{UiModel, Cell, Attrs, Color, ModelRect, COLOR_BLACK, COLOR_WHITE, COLOR_RED}; +use ui_model::{UiModel, Cell, Attrs, ModelRect}; +use color::{Color, COLOR_BLACK, COLOR_WHITE, COLOR_RED}; use nvim; use nvim::{RedrawEvents, GuiApi, RepaintMode, ErrorReport, NeovimClient}; use input; diff --git a/src/ui_model/cell.rs b/src/ui_model/cell.rs new file mode 100644 index 0000000..1825ed2 --- /dev/null +++ b/src/ui_model/cell.rs @@ -0,0 +1,63 @@ +use color::Color; + +#[derive(Clone)] +pub struct Attrs { + pub italic: bool, + pub bold: bool, + pub underline: bool, + pub undercurl: bool, + pub foreground: Option, + pub background: Option, + pub special: Option, + pub reverse: bool, + pub double_width: bool, +} + +impl Attrs { + pub fn new() -> Attrs { + Attrs { + foreground: None, + background: None, + special: None, + italic: false, + bold: false, + underline: false, + undercurl: false, + reverse: false, + double_width: false, + } + } + + fn clear(&mut self) { + self.italic = false; + self.bold = false; + self.underline = false; + self.undercurl = false; + self.reverse = false; + self.foreground = None; + self.background = None; + self.special = None; + self.double_width = false; + } +} + +#[derive(Clone)] +pub struct Cell { + pub ch: char, + pub attrs: Attrs, +} + +impl Cell { + pub fn new(ch: char) -> Cell { + Cell { + ch: ch, + attrs: Attrs::new(), + } + } + + pub fn clear(&mut self) { + self.ch = ' '; + self.attrs.clear(); + } +} + diff --git a/src/ui_model/line.rs b/src/ui_model/line.rs new file mode 100644 index 0000000..6fd4fee --- /dev/null +++ b/src/ui_model/line.rs @@ -0,0 +1,23 @@ +use super::cell::Cell; +use pango::Item; + +pub struct Line { + line: Box<[Cell]>, + item_line: Option>, + cell_to_item: Box<[usize]>, +} + +impl Line { + pub fn new(columns: usize) -> Self { + let line = Vec::with_capacity(columns); + for _ in 0..columns { + line.push(Cell::new(' ')); + } + + Line { + cell_to_item: Vec::with_capacity(line.len()).into_boxed_slice(), + line: line.into_boxed_slice(), + item_line: None, + } + } +} diff --git a/src/ui_model.rs b/src/ui_model/mod.rs similarity index 88% rename from src/ui_model.rs rename to src/ui_model/mod.rs index e8967ea..7017179 100644 --- a/src/ui_model.rs +++ b/src/ui_model/mod.rs @@ -1,92 +1,17 @@ +mod cell; +mod line; + +pub use self::cell::{Cell, Attrs}; +use self::line::Line; + use std::slice::Iter; -use gdk; - -#[derive(Clone, PartialEq)] -pub struct Color(pub f64, pub f64, pub f64); - -pub const COLOR_BLACK: Color = Color(0.0, 0.0, 0.0); -pub const COLOR_WHITE: Color = Color(1.0, 1.0, 1.0); -pub const COLOR_RED: Color = Color(1.0, 0.0, 0.0); - -impl <'a> Into for &'a Color { - fn into(self) -> gdk::RGBA { - gdk::RGBA { - red: self.0, - green: self.1, - blue: self.2, - alpha: 1.0, - } - } -} - -#[derive(Clone)] -pub struct Attrs { - pub italic: bool, - pub bold: bool, - pub underline: bool, - pub undercurl: bool, - pub foreground: Option, - pub background: Option, - pub special: Option, - pub reverse: bool, - pub double_width: bool, -} - -impl Attrs { - pub fn new() -> Attrs { - Attrs { - foreground: None, - background: None, - special: None, - italic: false, - bold: false, - underline: false, - undercurl: false, - reverse: false, - double_width: false, - } - } - - fn clear(&mut self) { - self.italic = false; - self.bold = false; - self.underline = false; - self.undercurl = false; - self.reverse = false; - self.foreground = None; - self.background = None; - self.special = None; - self.double_width = false; - } -} - -#[derive(Clone)] -pub struct Cell { - pub ch: char, - pub attrs: Attrs, -} - -impl Cell { - pub fn new(ch: char) -> Cell { - Cell { - ch: ch, - attrs: Attrs::new(), - } - } - - fn clear(&mut self) { - self.ch = ' '; - self.attrs.clear(); - } -} - pub struct UiModel { pub columns: usize, pub rows: usize, cur_row: usize, cur_col: usize, - model: Vec>, + model: Box<[Line]>, top: usize, bot: usize, left: usize, @@ -97,10 +22,7 @@ impl UiModel { pub fn new(rows: u64, columns: u64) -> UiModel { let mut model = Vec::with_capacity(rows as usize); for i in 0..rows as usize { - model.push(Vec::with_capacity(columns as usize)); - for _ in 0..columns as usize { - model[i].push(Cell::new(' ')); - } + model.push(Line::new(columns as usize)); } UiModel { @@ -108,7 +30,7 @@ impl UiModel { rows: rows as usize, cur_row: 0, cur_col: 0, - model: model, + model: model.into_boxed_slice(), top: 0, bot: (rows - 1) as usize, left: 0, @@ -116,7 +38,7 @@ impl UiModel { } } - pub fn model(&self) -> &Vec> { + pub fn model(&self) -> &[Line] { &self.model }