Underline

This commit is contained in:
daa 2017-09-09 23:02:06 +03:00
parent 47ccbdcec5
commit ab6052705a
3 changed files with 36 additions and 3 deletions

View File

@ -89,4 +89,9 @@ impl ColorModel {
cell.attrs.foreground.as_ref().unwrap_or(&self.fg_color) cell.attrs.foreground.as_ref().unwrap_or(&self.fg_color)
} }
} }
#[inline]
pub fn actual_cell_sp<'a>(&'a self, cell: &'a Cell) -> &'a Color {
cell.attrs.special.as_ref().unwrap_or(&self.sp_color)
}
} }

View File

@ -69,6 +69,8 @@ pub struct CellMetrics {
pub line_height: f64, pub line_height: f64,
pub char_width: f64, pub char_width: f64,
pub ascent: f64, pub ascent: f64,
pub underline_position: f64,
pub underline_thickness: f64,
pub pango_ascent: i32, pub pango_ascent: i32,
pub pango_descent: i32, pub pango_descent: i32,
pub pango_char_width: i32, pub pango_char_width: i32,
@ -76,6 +78,7 @@ pub struct CellMetrics {
impl CellMetrics { impl CellMetrics {
fn new(font_metrics: &pango::FontMetrics) -> Self { fn new(font_metrics: &pango::FontMetrics) -> Self {
CellMetrics { CellMetrics {
pango_ascent: font_metrics.get_ascent(), pango_ascent: font_metrics.get_ascent(),
pango_descent: font_metrics.get_descent(), pango_descent: font_metrics.get_descent(),
@ -84,6 +87,11 @@ impl CellMetrics {
line_height: (font_metrics.get_ascent() + font_metrics.get_descent()) as f64 / line_height: (font_metrics.get_ascent() + font_metrics.get_descent()) as f64 /
pango::SCALE as f64, pango::SCALE as f64,
char_width: font_metrics.get_approximate_digit_width() as f64 / pango::SCALE as f64, char_width: font_metrics.get_approximate_digit_width() as f64 / pango::SCALE as f64,
underline_position: (font_metrics.get_ascent() -
font_metrics.get_underline_position()) as
f64 / pango::SCALE as f64,
underline_thickness: font_metrics.get_underline_thickness() as f64 /
pango::SCALE as f64,
} }
} }
} }

View File

@ -20,8 +20,6 @@ pub fn render(
color_model: &color::ColorModel, color_model: &color::ColorModel,
mode: &mode::Mode, mode: &mode::Mode,
) { ) {
// TODO: underline
// TODO: undercurl
ctx.set_source_rgb( ctx.set_source_rgb(
color_model.bg_color.0, color_model.bg_color.0,
color_model.bg_color.1, color_model.bg_color.1,
@ -32,6 +30,8 @@ pub fn render(
let &CellMetrics { let &CellMetrics {
line_height, line_height,
char_width, char_width,
underline_position,
underline_thickness,
.. ..
} = font_ctx.cell_metrics(); } = font_ctx.cell_metrics();
let mut line_y = 0.0; let mut line_y = 0.0;
@ -44,9 +44,10 @@ pub fn render(
for col in 0..line.line.len() { for col in 0..line.line.len() {
let cell = &line.line[col]; let cell = &line.line[col];
let (bg, fg) = color_model.cell_colors(cell);
// draw cell // draw cell
if let Some(item) = line.item_line[col].as_ref() { if let Some(item) = line.item_line[col].as_ref() {
let (bg, fg) = color_model.cell_colors(cell);
if let Some(bg) = bg { if let Some(bg) = bg {
ctx.set_source_rgb(bg.0, bg.1, bg.2); ctx.set_source_rgb(bg.0, bg.1, bg.2);
@ -74,6 +75,25 @@ pub fn render(
} }
} }
if cell.attrs.underline || cell.attrs.undercurl {
if cell.attrs.undercurl {
let sp = color_model.actual_cell_sp(cell);
ctx.set_source_rgba(sp.0, sp.1, sp.2, 0.7);
ctx.set_dash(&[4.0, 2.0], 0.0);
ctx.set_line_width(underline_thickness);
ctx.move_to(line_x, line_y + underline_position);
ctx.line_to(line_x + char_width, line_y + underline_position);
ctx.stroke();
ctx.set_dash(&[], 0.0);
} else if cell.attrs.underline {
ctx.set_source_rgb(fg.0, fg.1, fg.2);
ctx.set_line_width(underline_thickness);
ctx.move_to(line_x, line_y + underline_position);
ctx.line_to(line_x + char_width, line_y + underline_position);
ctx.stroke();
}
}
if row == cursor_row && col == cursor_col { if row == cursor_row && col == cursor_col {
ctx.move_to(line_x, line_y); ctx.move_to(line_x, line_y);
cursor.draw( cursor.draw(