Get sizes from FontMetrics

This commit is contained in:
daa84 2017-09-05 18:23:46 +03:00
parent bc31984362
commit 05dee3251f
4 changed files with 177 additions and 163 deletions

View File

@ -52,15 +52,15 @@ impl ColorModel {
} }
} }
pub fn cell_colors<'a>(&'a self, cell: &'a Cell) -> (&'a Color, &'a Color) { pub fn cell_colors<'a>(&'a self, cell: &'a Cell) -> (Option<&'a Color>, &'a Color) {
if !cell.attrs.reverse { if !cell.attrs.reverse {
( (
cell.attrs.background.as_ref().unwrap_or(&self.bg_color), cell.attrs.background.as_ref(),
cell.attrs.foreground.as_ref().unwrap_or(&self.fg_color), cell.attrs.foreground.as_ref().unwrap_or(&self.fg_color),
) )
} else { } else {
( (
cell.attrs.foreground.as_ref().unwrap_or(&self.fg_color), cell.attrs.foreground.as_ref().or(Some(&self.fg_color)),
cell.attrs.background.as_ref().unwrap_or(&self.bg_color), cell.attrs.background.as_ref().unwrap_or(&self.bg_color),
) )
} }

View File

@ -1,5 +1,3 @@
use std::ffi::CString;
use pangocairo::FontMap; use pangocairo::FontMap;
use pango::prelude::*; use pango::prelude::*;
use pango; use pango;
@ -9,31 +7,77 @@ use sys::pango as sys_pango;
use ui_model::StyledLine; use ui_model::StyledLine;
pub struct Context { pub struct Context {
pango_context: pango::Context, state: ContextState,
} }
impl Context { impl Context {
pub fn new(font_desc: &pango::FontDescription) -> Self { pub fn new(font_desc: pango::FontDescription) -> Self {
Context { pango_context: create_pango_context(font_desc) } Context { state: ContextState::new(font_desc) }
} }
pub fn update(&mut self, font_desc: &pango::FontDescription) { pub fn update(&mut self, font_desc: pango::FontDescription) {
self.pango_context = create_pango_context(font_desc); self.state = ContextState::new(font_desc);
} }
pub fn itemize(&self, line: &StyledLine) -> Vec<sys_pango::Item> { pub fn itemize(&self, line: &StyledLine) -> Vec<sys_pango::Item> {
sys_pango::pango_itemize( sys_pango::pango_itemize(
&self.pango_context, &self.state.pango_context,
line.line_str.trim_right(), line.line_str.trim_right(),
&line.attr_list, &line.attr_list,
) )
} }
#[inline]
pub fn font_description(&self) -> &pango::FontDescription {
&self.state.font_desc
}
#[inline]
pub fn ascent(&self) -> f64 {
self.state.cell_metrics.ascent
}
#[inline]
pub fn cell_metrics(&self) -> &CellMetrics {
&self.state.cell_metrics
}
} }
fn create_pango_context(font_desc: &pango::FontDescription) -> pango::Context { struct ContextState {
let font_map = FontMap::get_default(); pango_context: pango::Context,
let pango_context = font_map.create_context().unwrap(); cell_metrics: CellMetrics,
pango_context.set_font_description(&font_desc); font_desc: pango::FontDescription,
}
pango_context
impl ContextState {
pub fn new(font_desc: pango::FontDescription) -> Self {
let font_map = FontMap::get_default();
let pango_context = font_map.create_context().unwrap();
pango_context.set_font_description(&font_desc);
let font_metrics = pango_context.get_metrics(None, None).unwrap();
ContextState {
pango_context,
cell_metrics: CellMetrics::new(&font_metrics),
font_desc,
}
}
}
pub struct CellMetrics {
pub line_height: f64,
pub char_width: f64,
pub ascent: f64,
}
impl CellMetrics {
fn new(font_metrics: &pango::FontMetrics) -> Self {
CellMetrics {
ascent: font_metrics.get_ascent() as f64 / pango::SCALE as f64,
line_height: (font_metrics.get_ascent() + font_metrics.get_descent()) as f64 /
pango::SCALE as f64,
char_width: font_metrics.get_approximate_digit_width() as f64 / pango::SCALE as f64,
}
}
} }

View File

@ -1,6 +1,7 @@
mod context; mod context;
pub use self::context::Context; pub use self::context::Context;
pub use self::context::CellMetrics;
use color; use color;
use sys::pango::*; use sys::pango::*;
@ -11,10 +12,9 @@ use ui_model;
pub fn render( pub fn render(
ctx: &cairo::Context, ctx: &cairo::Context,
font_ctx: &context::Context,
ui_model: &ui_model::UiModel, ui_model: &ui_model::UiModel,
color_model: &color::ColorModel, color_model: &color::ColorModel,
line_height: f64,
char_width: f64,
) { ) {
ctx.set_source_rgb( ctx.set_source_rgb(
color_model.bg_color.0, color_model.bg_color.0,
@ -23,16 +23,25 @@ pub fn render(
); );
ctx.paint(); ctx.paint();
let mut line_y = line_height; let &CellMetrics {line_height, char_width, ..} = font_ctx.cell_metrics();
let mut line_y = 0.0;
let ascent = font_ctx.ascent();
for line in ui_model.model() { for line in ui_model.model() {
let mut line_x = 0.0; let mut line_x = 0.0;
for i in 0..line.line.len() { for i in 0..line.line.len() {
ctx.move_to(line_x, line_y); let (bg, fg) = color_model.cell_colors(&line.line[i]);
if let Some(bg) = bg {
ctx.set_source_rgb(bg.0, bg.1, bg.2);
ctx.rectangle(line_x, line_y, char_width, line_height);
ctx.fill();
}
if let Some(item) = line.item_line[i].as_ref() { if let Some(item) = line.item_line[i].as_ref() {
if let Some(ref glyphs) = item.glyphs { if let Some(ref glyphs) = item.glyphs {
let (_, fg) = color_model.cell_colors(&line.line[i]); ctx.move_to(line_x, line_y + ascent);
ctx.set_source_rgb(fg.0, fg.1, fg.2); ctx.set_source_rgb(fg.0, fg.1, fg.2);
ctx.show_glyph_string(item.font(), glyphs); ctx.show_glyph_string(item.font(), glyphs);
} }

View File

@ -32,6 +32,7 @@ use tabline::Tabline;
use error; use error;
use mode; use mode;
use render; use render;
use render::CellMetrics;
const DEFAULT_FONT_NAME: &str = "DejaVu Sans Mono 12"; const DEFAULT_FONT_NAME: &str = "DejaVu Sans Mono 12";
pub const MINIMUM_SUPPORTED_NVIM_VERSION: &str = "0.2"; pub const MINIMUM_SUPPORTED_NVIM_VERSION: &str = "0.2";
@ -54,7 +55,7 @@ pub struct State {
cur_attrs: Option<Attrs>, cur_attrs: Option<Attrs>,
mouse_enabled: bool, mouse_enabled: bool,
nvim: Rc<RefCell<NeovimClient>>, nvim: Rc<RefCell<NeovimClient>>,
font_desc: FontDescription, font_ctx: render::Context,
cursor: Option<Cursor>, cursor: Option<Cursor>,
popup_menu: RefCell<PopupMenu>, popup_menu: RefCell<PopupMenu>,
settings: Rc<RefCell<Settings>>, settings: Rc<RefCell<Settings>>,
@ -67,8 +68,6 @@ pub struct State {
im_context: gtk::IMMulticontext, im_context: gtk::IMMulticontext,
error_area: error::ErrorArea, error_area: error::ErrorArea,
line_height: Option<f64>,
char_width: Option<f64>,
request_resize: bool, request_resize: bool,
request_nvim_resize: bool, request_nvim_resize: bool,
resize_timer: Option<glib::SourceId>, resize_timer: Option<glib::SourceId>,
@ -82,6 +81,7 @@ impl State {
pub fn new(settings: Rc<RefCell<Settings>>, options: ShellOptions) -> State { pub fn new(settings: Rc<RefCell<Settings>>, options: ShellOptions) -> State {
let drawing_area = gtk::DrawingArea::new(); let drawing_area = gtk::DrawingArea::new();
let popup_menu = RefCell::new(PopupMenu::new(&drawing_area)); let popup_menu = RefCell::new(PopupMenu::new(&drawing_area));
let font_ctx = render::Context::new(FontDescription::from_string(DEFAULT_FONT_NAME));
State { State {
model: UiModel::new(1, 1), model: UiModel::new(1, 1),
@ -89,10 +89,10 @@ impl State {
nvim: Rc::new(RefCell::new(NeovimClient::new())), nvim: Rc::new(RefCell::new(NeovimClient::new())),
cur_attrs: None, cur_attrs: None,
mouse_enabled: true, mouse_enabled: true,
font_desc: FontDescription::from_string(DEFAULT_FONT_NAME), font_ctx,
cursor: None, cursor: None,
popup_menu, popup_menu,
settings: settings, settings,
mode: mode::Mode::new(), mode: mode::Mode::new(),
@ -103,8 +103,6 @@ impl State {
im_context: gtk::IMMulticontext::new(), im_context: gtk::IMMulticontext::new(),
error_area: error::ErrorArea::new(), error_area: error::ErrorArea::new(),
line_height: None,
char_width: None,
resize_timer: None, resize_timer: None,
request_resize: false, request_resize: false,
request_nvim_resize: false, request_nvim_resize: false,
@ -142,18 +140,12 @@ impl State {
} }
} }
fn create_pango_font(&self) -> FontDescription {
self.font_desc.clone()
}
pub fn get_font_desc(&self) -> &FontDescription { pub fn get_font_desc(&self) -> &FontDescription {
&self.font_desc self.font_ctx.font_description()
} }
pub fn set_font_desc(&mut self, desc: &str) { pub fn set_font_desc(&mut self, desc: &str) {
self.font_desc = FontDescription::from_string(desc); self.font_ctx.update(FontDescription::from_string(desc));
self.line_height = None;
self.char_width = None;
self.model.clear_draw_cache(); self.model.clear_draw_cache();
} }
@ -183,19 +175,19 @@ impl State {
} }
fn queue_draw_area<M: AsRef<ModelRect>>(&self, rect_list: &[M]) { fn queue_draw_area<M: AsRef<ModelRect>>(&self, rect_list: &[M]) {
match (&self.line_height, &self.char_width) { let &CellMetrics {
(&Some(line_height), &Some(char_width)) => { line_height,
for rect in rect_list { char_width,
let mut rect = rect.as_ref().clone(); ..
// this need to repain also line under curren line } = self.font_ctx.cell_metrics();
// in case underscore or 'g' symbol is go here for rect in rect_list {
// right one for italic symbol let mut rect = rect.as_ref().clone();
rect.extend(0, 1, 0, 1); // this need to repain also line under curren line
let (x, y, width, height) = rect.to_area(line_height, char_width); // in case underscore or 'g' symbol is go here
self.drawing_area.queue_draw_area(x, y, width, height); // right one for italic symbol
} rect.extend(0, 1, 0, 1);
} let (x, y, width, height) = rect.to_area(line_height, char_width);
_ => self.drawing_area.queue_draw(), self.drawing_area.queue_draw_area(x, y, width, height);
} }
} }
@ -203,36 +195,17 @@ impl State {
input::im_input(&mut self.nvim.borrow_mut(), ch); input::im_input(&mut self.nvim.borrow_mut(), ch);
} }
fn calc_char_bounds(&self, ctx: &cairo::Context) -> (i32, i32) { fn calc_nvim_size(&self) -> (usize, usize) {
let layout = ctx.create_pango_layout(); let &CellMetrics {
line_height,
let desc = self.create_pango_font(); char_width,
layout.set_font_description(Some(&desc)); ..
layout.set_text("A"); } = self.font_ctx.cell_metrics();
let alloc = self.drawing_area.get_allocation();
layout.get_pixel_size() (
} (alloc.width as f64 / char_width).trunc() as usize,
(alloc.height as f64 / line_height).trunc() as usize,
fn calc_line_metrics(&mut self, ctx: &cairo::Context) { )
if self.line_height.is_none() {
let (width, height) = self.calc_char_bounds(ctx);
self.line_height = Some(height as f64);
self.char_width = Some(width as f64);
}
}
fn calc_nvim_size(&self) -> Option<(usize, usize)> {
if let Some(line_height) = self.line_height {
if let Some(char_width) = self.char_width {
let alloc = self.drawing_area.get_allocation();
return Some((
(alloc.width as f64 / char_width).trunc() as usize,
(alloc.height as f64 / line_height).trunc() as usize,
));
}
}
None
} }
fn show_error_area(&self) { fn show_error_area(&self) {
@ -244,21 +217,21 @@ impl State {
} }
fn set_im_location(&self) { fn set_im_location(&self) {
if let Some(line_height) = self.line_height { let &CellMetrics {
if let Some(char_width) = self.char_width { line_height,
let (row, col) = self.model.get_cursor(); char_width,
..
} = self.font_ctx.cell_metrics();
let (row, col) = self.model.get_cursor();
let (x, y, width, height) = let (x, y, width, height) = ModelRect::point(col, row).to_area(line_height, char_width);
ModelRect::point(col, row).to_area(line_height, char_width);
self.im_context.set_cursor_location(&gdk::Rectangle { self.im_context.set_cursor_location(&gdk::Rectangle {
x, x,
y, y,
width, width,
height, height,
}); });
}
}
self.im_context.reset(); self.im_context.reset();
} }
@ -562,19 +535,19 @@ fn gtk_button_press(shell: &mut State, ui_state: &mut UiState, ev: &EventButton)
} }
fn mouse_input(shell: &mut State, input: &str, state: ModifierType, position: (f64, f64)) { fn mouse_input(shell: &mut State, input: &str, state: ModifierType, position: (f64, f64)) {
if let Some(line_height) = shell.line_height { let &CellMetrics {
if let Some(char_width) = shell.char_width { line_height,
char_width,
let mut nvim = shell.nvim(); ..
let (x, y) = position; } = shell.font_ctx.cell_metrics();
let col = (x / char_width).trunc() as u64; let mut nvim = shell.nvim();
let row = (y / line_height).trunc() as u64; let (x, y) = position;
let input_str = format!("{}<{},{}>", keyval_to_input_string(input, state), col, row); let col = (x / char_width).trunc() as u64;
nvim.input(&input_str).expect( let row = (y / line_height).trunc() as u64;
"Can't send mouse input event", let input_str = format!("{}<{},{}>", keyval_to_input_string(input, state), col, row);
); nvim.input(&input_str).expect(
} "Can't send mouse input event",
} );
} }
fn gtk_button_release(ui_state: &mut UiState) -> Inhibit { fn gtk_button_release(ui_state: &mut UiState) -> Inhibit {
@ -589,18 +562,7 @@ fn gtk_motion_notify(shell: &mut State, ui_state: &mut UiState, ev: &EventMotion
Inhibit(false) Inhibit(false)
} }
#[inline]
fn update_line_metrics(state_arc: &Arc<UiMutex<State>>, ctx: &cairo::Context) {
let mut state = state_arc.borrow_mut();
if state.line_height.is_none() {
state.calc_line_metrics(ctx);
}
}
fn gtk_draw(state_arc: &Arc<UiMutex<State>>, ctx: &cairo::Context) -> Inhibit { fn gtk_draw(state_arc: &Arc<UiMutex<State>>, ctx: &cairo::Context) -> Inhibit {
update_line_metrics(state_arc, ctx);
if state_arc.borrow_mut().request_nvim_resize { if state_arc.borrow_mut().request_nvim_resize {
try_nvim_resize(state_arc); try_nvim_resize(state_arc);
} }
@ -619,20 +581,8 @@ fn gtk_draw(state_arc: &Arc<UiMutex<State>>, ctx: &cairo::Context) -> Inhibit {
} }
fn render(state: &mut State, ctx: &cairo::Context) { fn render(state: &mut State, ctx: &cairo::Context) {
let font_desc = state.create_pango_font(); render::shape_dirty(&state.font_ctx, &mut state.model, &state.color_model);
let line_height = state.line_height.unwrap(); render::render(ctx, &state.font_ctx, &state.model, &state.color_model);
let char_width = state.char_width.unwrap();
let font_ctx = render::Context::new(&font_desc);
render::shape_dirty(&font_ctx, &mut state.model, &state.color_model);
render::render(
ctx,
&state.model,
&state.color_model,
line_height,
char_width,
);
} }
fn show_nvim_start_error(err: nvim::NvimInitError, state_arc: Arc<UiMutex<State>>) { fn show_nvim_start_error(err: nvim::NvimInitError, state_arc: Arc<UiMutex<State>>) {
@ -715,7 +665,7 @@ fn init_nvim(state_arc: &Arc<UiMutex<State>>) {
if nvim.is_uninitialized() { if nvim.is_uninitialized() {
nvim.set_in_progress(); nvim.set_in_progress();
let (cols, rows) = state.calc_nvim_size().unwrap(); let (cols, rows) = state.calc_nvim_size();
let state_arc = state_arc.clone(); let state_arc = state_arc.clone();
let options = state.options.clone(); let options = state.options.clone();
@ -779,10 +729,13 @@ fn get_model_clip(
fn draw_initializing(state: &State, ctx: &cairo::Context) { fn draw_initializing(state: &State, ctx: &cairo::Context) {
let layout = ctx.create_pango_layout(); let layout = ctx.create_pango_layout();
let desc = state.create_pango_font(); let desc = state.get_font_desc();
let alloc = state.drawing_area.get_allocation(); let alloc = state.drawing_area.get_allocation();
let line_height = state.line_height.unwrap(); let &CellMetrics {
let char_width = state.char_width.unwrap(); line_height,
char_width,
..
} = state.font_ctx.cell_metrics();
ctx.set_source_rgb( ctx.set_source_rgb(
state.color_model.bg_color.0, state.color_model.bg_color.0,
@ -791,7 +744,7 @@ fn draw_initializing(state: &State, ctx: &cairo::Context) {
); );
ctx.paint(); ctx.paint();
layout.set_font_description(&desc); layout.set_font_description(desc);
layout.set_text("Loading->"); layout.set_text("Loading->");
let (width, height) = layout.get_pixel_size(); let (width, height) = layout.get_pixel_size();
@ -964,12 +917,17 @@ fn request_window_resize(state: &mut State) {
return; return;
} }
let &CellMetrics {
line_height,
char_width,
..
} = state.font_ctx.cell_metrics();
state.request_resize = false; state.request_resize = false;
let width = state.drawing_area.get_allocated_width(); let width = state.drawing_area.get_allocated_width();
let height = state.drawing_area.get_allocated_height(); let height = state.drawing_area.get_allocated_height();
let request_height = (state.model.rows as f64 * state.line_height.unwrap()) as i32; let request_height = (state.model.rows as f64 * line_height) as i32;
let request_width = (state.model.columns as f64 * state.char_width.unwrap()) as i32; let request_width = (state.model.columns as f64 * char_width) as i32;
if width != request_width || height != request_height { if width != request_width || height != request_height {
let window: gtk::Window = state let window: gtk::Window = state
@ -998,21 +956,20 @@ fn try_nvim_resize(state: &Arc<UiMutex<State>>) {
return; return;
} }
if let Some((columns, rows)) = state_ref.calc_nvim_size() { let (columns, rows) = state_ref.calc_nvim_size();
let state = state.clone(); let state = state.clone();
state_ref.resize_timer = Some(glib::timeout_add(250, move || { state_ref.resize_timer = Some(glib::timeout_add(250, move || {
let mut state_ref = state.borrow_mut(); let mut state_ref = state.borrow_mut();
state_ref.resize_timer = None; state_ref.resize_timer = None;
if state_ref.model.rows != rows || state_ref.model.columns != columns { if state_ref.model.rows != rows || state_ref.model.columns != columns {
if let Err(err) = state_ref.nvim().ui_try_resize(columns as u64, rows as u64) { if let Err(err) = state_ref.nvim().ui_try_resize(columns as u64, rows as u64) {
error!("Error trying resize nvim {}", err); error!("Error trying resize nvim {}", err);
}
} }
Continue(false) }
})); Continue(false)
} }));
} }
impl RedrawEvents for State { impl RedrawEvents for State {
@ -1149,20 +1106,24 @@ impl RedrawEvents for State {
row: u64, row: u64,
col: u64, col: u64,
) -> RepaintMode { ) -> RepaintMode {
if let (&Some(line_height), &Some(char_width)) = (&self.line_height, &self.char_width) {
let point = ModelRect::point(col as usize, row as usize);
let (x, y, width, height) = point.to_area(line_height, char_width);
self.popup_menu.borrow_mut().show( let &CellMetrics {
self, line_height,
menu, char_width,
selected, ..
x, } = self.font_ctx.cell_metrics();
y, let point = ModelRect::point(col as usize, row as usize);
width, let (x, y, width, height) = point.to_area(line_height, char_width);
height,
); self.popup_menu.borrow_mut().show(
} self,
menu,
selected,
x,
y,
width,
height,
);
RepaintMode::Nothing RepaintMode::Nothing
} }