From bda35efe8810f7cfab9c56778f9dd7cb066425c2 Mon Sep 17 00:00:00 2001 From: daa Date: Sun, 29 Apr 2018 22:43:35 +0300 Subject: [PATCH] Use font options from gtk widget (#88) --- src/render/context.rs | 16 ++++++---------- src/shell.rs | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/render/context.rs b/src/render/context.rs index bd50c87..3262151 100644 --- a/src/render/context.rs +++ b/src/render/context.rs @@ -1,4 +1,3 @@ -use pangocairo::FontMap; use pango::prelude::*; use pango; @@ -13,14 +12,14 @@ pub struct Context { } impl Context { - pub fn new(font_desc: pango::FontDescription) -> Self { + pub fn new(pango_context: pango::Context) -> Self { Context { - state: ContextState::new(font_desc), + state: ContextState::new(pango_context), } } - pub fn update(&mut self, font_desc: pango::FontDescription) { - self.state = ContextState::new(font_desc); + pub fn update(&mut self, pango_context: pango::Context) { + self.state = ContextState::new(pango_context); } pub fn itemize(&self, line: &StyledLine) -> Vec { @@ -62,12 +61,9 @@ struct ContextState { } impl ContextState { - pub fn new(font_desc: pango::FontDescription) -> Self { - let font_map = FontMap::get_default().unwrap(); - let pango_context = font_map.create_context().unwrap(); - pango_context.set_font_description(&font_desc); - + pub fn new(pango_context: pango::Context) -> Self { let font_metrics = pango_context.get_metrics(None, None).unwrap(); + let font_desc = pango_context.get_font_description().unwrap(); ContextState { pango_context, diff --git a/src/shell.rs b/src/shell.rs index 005eb6f..933fa79 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -7,6 +7,8 @@ use std::thread; use std::collections::HashMap; use std::time::Duration; +use pango; +use pango::prelude::*; use cairo; use cairo::prelude::*; use pango::{FontDescription, LayoutExt}; @@ -91,9 +93,9 @@ pub struct RenderState { } impl RenderState { - pub fn new() -> Self { + pub fn new(pango_context: pango::Context) -> Self { RenderState { - font_ctx: render::Context::new(FontDescription::from_string(DEFAULT_FONT_NAME)), + font_ctx: render::Context::new(pango_context), color_model: ColorModel::new(), mode: mode::Mode::new(), } @@ -139,7 +141,11 @@ pub struct State { impl State { pub fn new(settings: Rc>, options: ShellOptions) -> State { let drawing_area = gtk::DrawingArea::new(); - let render_state = Rc::new(RefCell::new(RenderState::new())); + + let pango_context = drawing_area.create_pango_context().unwrap(); + pango_context.set_font_description(&FontDescription::from_string(DEFAULT_FONT_NAME)); + + let render_state = Rc::new(RefCell::new(RenderState::new(pango_context))); let popup_menu = PopupMenu::new(&drawing_area); let cmd_line = CmdLine::new(&drawing_area, render_state.clone()); @@ -258,10 +264,13 @@ impl State { } pub fn set_font_desc(&mut self, desc: &str) { + let pango_context = self.drawing_area.create_pango_context().unwrap(); + pango_context.set_font_description(&FontDescription::from_string(desc)); + self.render_state .borrow_mut() .font_ctx - .update(FontDescription::from_string(desc)); + .update(pango_context); self.model.clear_glyphs(); self.try_nvim_resize(); self.on_redraw(&RepaintMode::All); @@ -1181,9 +1190,7 @@ impl State { pub fn on_put(&mut self, text: String) -> RepaintMode { let double_width = text.is_empty(); - RepaintMode::Area( - self.model.put(&text, double_width, self.cur_attrs.as_ref()), - ) + RepaintMode::Area(self.model.put(&text, double_width, self.cur_attrs.as_ref())) } pub fn on_clear(&mut self) -> RepaintMode {