2017-08-24 14:41:20 +00:00
|
|
|
use pangocairo::FontMap;
|
|
|
|
use pango::prelude::*;
|
|
|
|
use pango;
|
|
|
|
|
2017-08-26 20:17:09 +00:00
|
|
|
use sys::pango as sys_pango;
|
2017-09-11 15:00:51 +00:00
|
|
|
use sys::pango::AttrIteratorFactory;
|
2017-08-24 14:41:20 +00:00
|
|
|
|
|
|
|
use ui_model::StyledLine;
|
2017-09-11 15:00:51 +00:00
|
|
|
use super::itemize::ItemizeIterator;
|
2017-08-24 14:41:20 +00:00
|
|
|
|
|
|
|
pub struct Context {
|
2017-09-05 15:23:46 +00:00
|
|
|
state: ContextState,
|
2017-08-24 14:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
2017-09-05 15:23:46 +00:00
|
|
|
pub fn new(font_desc: pango::FontDescription) -> Self {
|
2018-01-21 20:19:00 +00:00
|
|
|
Context {
|
|
|
|
state: ContextState::new(font_desc),
|
|
|
|
}
|
2017-08-24 14:41:20 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 15:23:46 +00:00
|
|
|
pub fn update(&mut self, font_desc: pango::FontDescription) {
|
|
|
|
self.state = ContextState::new(font_desc);
|
2017-08-24 14:41:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 20:17:09 +00:00
|
|
|
pub fn itemize(&self, line: &StyledLine) -> Vec<sys_pango::Item> {
|
2017-09-11 15:00:51 +00:00
|
|
|
let mut attr_iter = line.attr_list.get_iterator();
|
|
|
|
|
|
|
|
ItemizeIterator::new(&line.line_str)
|
|
|
|
.flat_map(|(offset, len)| {
|
|
|
|
sys_pango::pango_itemize(
|
|
|
|
&self.state.pango_context,
|
|
|
|
&line.line_str,
|
|
|
|
offset,
|
|
|
|
len,
|
|
|
|
&line.attr_list,
|
|
|
|
Some(&mut attr_iter),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect()
|
2017-08-24 14:41:20 +00:00
|
|
|
}
|
2017-09-05 15:23:46 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn font_description(&self) -> &pango::FontDescription {
|
|
|
|
&self.state.font_desc
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn cell_metrics(&self) -> &CellMetrics {
|
|
|
|
&self.state.cell_metrics
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ContextState {
|
|
|
|
pango_context: pango::Context,
|
|
|
|
cell_metrics: CellMetrics,
|
|
|
|
font_desc: pango::FontDescription,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ContextState {
|
|
|
|
pub fn new(font_desc: pango::FontDescription) -> Self {
|
2017-12-31 09:47:50 +00:00
|
|
|
let font_map = FontMap::get_default().unwrap();
|
2017-09-05 15:23:46 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
2017-08-24 14:41:20 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 15:23:46 +00:00
|
|
|
pub struct CellMetrics {
|
|
|
|
pub line_height: f64,
|
|
|
|
pub char_width: f64,
|
|
|
|
pub ascent: f64,
|
2017-09-09 20:02:06 +00:00
|
|
|
pub underline_position: f64,
|
|
|
|
pub underline_thickness: f64,
|
2017-09-08 15:26:16 +00:00
|
|
|
pub pango_ascent: i32,
|
|
|
|
pub pango_descent: i32,
|
|
|
|
pub pango_char_width: i32,
|
2017-09-05 15:23:46 +00:00
|
|
|
}
|
2017-08-24 14:41:20 +00:00
|
|
|
|
2017-09-05 15:23:46 +00:00
|
|
|
impl CellMetrics {
|
|
|
|
fn new(font_metrics: &pango::FontMetrics) -> Self {
|
|
|
|
CellMetrics {
|
2017-09-08 15:26:16 +00:00
|
|
|
pango_ascent: font_metrics.get_ascent(),
|
|
|
|
pango_descent: font_metrics.get_descent(),
|
|
|
|
pango_char_width: font_metrics.get_approximate_digit_width(),
|
2017-09-05 15:23:46 +00:00
|
|
|
ascent: font_metrics.get_ascent() as f64 / pango::SCALE as f64,
|
2018-01-21 20:19:00 +00:00
|
|
|
line_height: (font_metrics.get_ascent() + font_metrics.get_descent()) as f64
|
|
|
|
/ pango::SCALE as f64,
|
2017-09-05 15:23:46 +00:00
|
|
|
char_width: font_metrics.get_approximate_digit_width() as f64 / pango::SCALE as f64,
|
2018-01-21 20:19:00 +00:00
|
|
|
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,
|
2017-09-05 15:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-11 15:00:51 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn new_hw(line_height: f64, char_width: f64) -> Self {
|
|
|
|
CellMetrics {
|
|
|
|
pango_ascent: 0,
|
|
|
|
pango_descent: 0,
|
|
|
|
pango_char_width: 0,
|
|
|
|
ascent: 0.0,
|
|
|
|
line_height,
|
|
|
|
char_width,
|
|
|
|
underline_position: 0.0,
|
|
|
|
underline_thickness: 0.0,
|
|
|
|
}
|
|
|
|
}
|
2017-08-24 14:41:20 +00:00
|
|
|
}
|