From 7e41bb27d478ca1fa61f61ecca707b4e22554b3d Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 23 Jan 2018 23:12:30 -0800 Subject: [PATCH] Add basic attribute setting and rendering --- src/main.rs | 32 ++++++++++++++++++++++++-------- src/term/color.rs | 2 +- src/term/mod.rs | 39 ++++++++++++++++++++++++++++++++++++--- src/xgfx.rs | 9 +++++++++ 4 files changed, 70 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 84187bd33..dec9b7a7b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -129,19 +129,35 @@ impl<'a> TerminalWindow<'a> { debug!("paint"); self.need_paint = false; - let message = "x_advance != foo->bar(); ❤ 😍🤢"; - let cells = term::Line::from_text(message); - println!("cells {:?}", cells); + let palette = term::color::ColorPalette::default(); - self.buffer_image.clear(xgfx::Color::rgb(0, 0, 0)); + let message = "x_advance != foo->bar(); ❤ 😍🤢"; + let mut line_attr = term::CellAttributes::default(); + line_attr.foreground = + term::color::ColorAttribute::PaletteIndex(term::color::AnsiColor::Teal as u8); + let line = term::Line::from_text(message, &line_attr); + + self.buffer_image.clear(xgfx::Color::rgb(0, 0, 0x55)); + let cell_height = self.cell_height.ceil() as usize; let mut x = 0 as isize; - let mut y = self.cell_height.ceil() as isize; - let glyph_info = self.font.shape(0, &cells.as_str())?; - for info in glyph_info { + let mut y = cell_height as isize; + let glyph_info = self.font.shape(0, &line.as_str())?; + for (cell_idx, info) in glyph_info.iter().enumerate() { let has_color = self.font.has_color(info.font_idx)?; let ft_glyph = self.font.load_glyph(info.font_idx, info.glyph_pos)?; + let attrs = &line.cells[cell_idx].attrs; + + // Render the cell background color + self.buffer_image.clear_rect( + x, + y - cell_height as isize, + info.num_cells as usize * self.cell_width as usize, + cell_height, + palette.resolve(&attrs.background).into(), + ); + let scale = if (info.x_advance / info.num_cells as f64).floor() > self.cell_width { info.num_cells as f64 * (self.cell_width / info.x_advance) } else if ft_glyph.bitmap.rows as f64 > self.cell_height { @@ -230,7 +246,7 @@ impl<'a> TerminalWindow<'a> { let operator = if has_color { xgfx::Operator::Over } else { - xgfx::Operator::MultiplyThenOver(xgfx::Color::rgb(0xb3, 0xb3, 0xb3)) + xgfx::Operator::MultiplyThenOver(palette.resolve(&attrs.foreground).into()) }; self.buffer_image.draw_image( x + x_offset as isize + bearing_x, diff --git a/src/term/color.rs b/src/term/color.rs index 65565874f..a939748b4 100644 --- a/src/term/color.rs +++ b/src/term/color.rs @@ -32,7 +32,7 @@ pub struct RgbColor { impl RgbColor {} -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub enum ColorAttribute { Foreground, Background, diff --git a/src/term/mod.rs b/src/term/mod.rs index a066c7c07..34c3ed822 100644 --- a/src/term/mod.rs +++ b/src/term/mod.rs @@ -5,9 +5,39 @@ use unicode_segmentation; pub mod color; +#[derive(Debug, Clone, Copy)] +pub struct CellAttributes { + pub bold: bool, + pub underline: bool, + pub italic: bool, + pub blink: bool, + pub reverse: bool, + pub strikethrough: bool, + pub font: u8, + pub foreground: color::ColorAttribute, + pub background: color::ColorAttribute, +} + +impl Default for CellAttributes { + fn default() -> CellAttributes { + CellAttributes { + bold: false, + underline: false, + italic: false, + blink: false, + reverse: false, + strikethrough: false, + font: 0, + foreground: color::ColorAttribute::Foreground, + background: color::ColorAttribute::Background, + } + } +} + #[derive(Debug, Clone)] pub struct Cell { chars: [u8; 8], + pub attrs: CellAttributes, } impl Cell { @@ -23,7 +53,7 @@ impl Cell { #[derive(Debug)] pub struct Line { - cells: Vec, + pub cells: Vec, } impl Line { @@ -38,7 +68,7 @@ impl Line { s } - pub fn from_text(s: &str) -> Line { + pub fn from_text(s: &str, attrs: &CellAttributes) -> Line { let mut cells = Vec::new(); for (_, sub) in unicode_segmentation::UnicodeSegmentation::grapheme_indices(s, true) { @@ -46,7 +76,10 @@ impl Line { let len = sub.len().min(8); chars[0..len].copy_from_slice(sub.as_bytes()); - cells.push(Cell { chars }); + cells.push(Cell { + chars, + attrs: *attrs, + }); } Line { cells } diff --git a/src/xgfx.rs b/src/xgfx.rs index c9e0e6390..6c71e04b7 100644 --- a/src/xgfx.rs +++ b/src/xgfx.rs @@ -1,4 +1,5 @@ use resize; +use std::convert::From; use std::result; use xcb; use xcb_util; @@ -6,6 +7,8 @@ use xcb_util; use failure::{self, Error}; pub type Result = result::Result; +use super::term::color::RgbColor; + /// The X protocol allows referencing a number of drawable /// objects. This trait marks those objects here in code. pub trait Drawable { @@ -266,6 +269,12 @@ impl Color { } } +impl From for Color { + fn from(color: RgbColor) -> Self { + Color::rgb(color.red, color.green, color.blue) + } +} + /// Compositing operator. /// We implement a small subset of possible compositing operators. /// More information on these and their temrinology can be found