1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-26 08:25:50 +03:00

Add basic attribute setting and rendering

This commit is contained in:
Wez Furlong 2018-01-23 23:12:30 -08:00
parent 641e85650d
commit 7e41bb27d4
4 changed files with 70 additions and 12 deletions

View File

@ -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,

View File

@ -32,7 +32,7 @@ pub struct RgbColor {
impl RgbColor {}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub enum ColorAttribute {
Foreground,
Background,

View File

@ -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<Cell>,
pub cells: Vec<Cell>,
}
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 }

View File

@ -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<T> = result::Result<T, Error>;
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<RgbColor> 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