Get sizes from FontMetrics
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
use std::ffi::CString;
|
||||
|
||||
use pangocairo::FontMap;
|
||||
use pango::prelude::*;
|
||||
use pango;
|
||||
@@ -9,31 +7,77 @@ use sys::pango as sys_pango;
|
||||
use ui_model::StyledLine;
|
||||
|
||||
pub struct Context {
|
||||
pango_context: pango::Context,
|
||||
state: ContextState,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub fn new(font_desc: &pango::FontDescription) -> Self {
|
||||
Context { pango_context: create_pango_context(font_desc) }
|
||||
pub fn new(font_desc: pango::FontDescription) -> Self {
|
||||
Context { state: ContextState::new(font_desc) }
|
||||
}
|
||||
|
||||
pub fn update(&mut self, font_desc: &pango::FontDescription) {
|
||||
self.pango_context = create_pango_context(font_desc);
|
||||
pub fn update(&mut self, font_desc: pango::FontDescription) {
|
||||
self.state = ContextState::new(font_desc);
|
||||
}
|
||||
|
||||
pub fn itemize(&self, line: &StyledLine) -> Vec<sys_pango::Item> {
|
||||
sys_pango::pango_itemize(
|
||||
&self.pango_context,
|
||||
&self.state.pango_context,
|
||||
line.line_str.trim_right(),
|
||||
&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 {
|
||||
let font_map = FontMap::get_default();
|
||||
let pango_context = font_map.create_context().unwrap();
|
||||
pango_context.set_font_description(&font_desc);
|
||||
|
||||
pango_context
|
||||
struct ContextState {
|
||||
pango_context: pango::Context,
|
||||
cell_metrics: CellMetrics,
|
||||
font_desc: pango::FontDescription,
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
mod context;
|
||||
|
||||
pub use self::context::Context;
|
||||
pub use self::context::CellMetrics;
|
||||
|
||||
use color;
|
||||
use sys::pango::*;
|
||||
@@ -11,10 +12,9 @@ use ui_model;
|
||||
|
||||
pub fn render(
|
||||
ctx: &cairo::Context,
|
||||
font_ctx: &context::Context,
|
||||
ui_model: &ui_model::UiModel,
|
||||
color_model: &color::ColorModel,
|
||||
line_height: f64,
|
||||
char_width: f64,
|
||||
) {
|
||||
ctx.set_source_rgb(
|
||||
color_model.bg_color.0,
|
||||
@@ -23,16 +23,25 @@ pub fn render(
|
||||
);
|
||||
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() {
|
||||
let mut line_x = 0.0;
|
||||
|
||||
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(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.show_glyph_string(item.font(), glyphs);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user