1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::collections::HashSet;

use pango;
use pango::prelude::*;

use crate::sys::pango as sys_pango;

use super::itemize::ItemizeIterator;
use crate::ui_model::StyledLine;

pub struct Context {
    font_metrics: FontMetrix,
    font_features: FontFeatures,
    line_space: i32,
}

impl Context {
    pub fn new(pango_context: pango::Context) -> Self {
        Context {
            line_space: 0,
            font_metrics: FontMetrix::new(pango_context, 0),
            font_features: FontFeatures::new(),
        }
    }

    pub fn update(&mut self, pango_context: pango::Context) {
        self.font_metrics = FontMetrix::new(pango_context, self.line_space);
    }

    pub fn update_font_features(&mut self, font_features: FontFeatures) {
        self.font_features = font_features;
    }

    pub fn update_line_space(&mut self, line_space: i32) {
        self.line_space = line_space;
        let pango_context = self.font_metrics.pango_context.clone();
        self.font_metrics = FontMetrix::new(pango_context, self.line_space);
    }

    pub fn itemize(&self, line: &StyledLine) -> Vec<pango::Item> {
        let attr_iter = line.attr_list.get_iterator();

        ItemizeIterator::new(&line.line_str)
            .flat_map(|(offset, len)| {
                pango::itemize(
                    &self.font_metrics.pango_context,
                    &line.line_str,
                    offset as i32,
                    len as i32,
                    &line.attr_list,
                    attr_iter.as_ref(),
                )
            }).collect()
    }

    pub fn create_layout(&self) -> pango::Layout {
        pango::Layout::new(&self.font_metrics.pango_context)
    }

    pub fn font_description(&self) -> &pango::FontDescription {
        &self.font_metrics.font_desc
    }

    pub fn cell_metrics(&self) -> &CellMetrics {
        &self.font_metrics.cell_metrics
    }

    pub fn font_features(&self) -> &FontFeatures {
        &self.font_features
    }

    pub fn font_families(&self) -> HashSet<glib::GString> {
        self.font_metrics
            .pango_context
            .list_families()
            .iter()
            .filter_map(pango::FontFamilyExt::get_name)
            .collect()
    }
}

struct FontMetrix {
    pango_context: pango::Context,
    cell_metrics: CellMetrics,
    font_desc: pango::FontDescription,
}

impl FontMetrix {
    pub fn new(pango_context: pango::Context, line_space: i32) -> Self {
        let font_metrics = pango_context.get_metrics(None, None).unwrap();
        let font_desc = pango_context.get_font_description().unwrap();

        FontMetrix {
            pango_context,
            cell_metrics: CellMetrics::new(&font_metrics, line_space),
            font_desc,
        }
    }
}

pub struct CellMetrics {
    pub line_height: f64,
    pub char_width: f64,
    pub ascent: f64,
    pub underline_position: f64,
    pub underline_thickness: f64,
    pub pango_ascent: i32,
    pub pango_descent: i32,
    pub pango_char_width: i32,
}

impl CellMetrics {
    fn new(font_metrics: &pango::FontMetrics, line_space: i32) -> Self {
        CellMetrics {
            pango_ascent: font_metrics.get_ascent(),
            pango_descent: font_metrics.get_descent(),
            pango_char_width: font_metrics.get_approximate_digit_width(),
            ascent: f64::from(font_metrics.get_ascent()) / f64::from(pango::SCALE),
            line_height: f64::from(font_metrics.get_ascent() + font_metrics.get_descent())
                / f64::from(pango::SCALE)
                + f64::from(line_space),
            char_width: f64::from(font_metrics.get_approximate_digit_width())
                / f64::from(pango::SCALE),
            underline_position: f64::from(
                font_metrics.get_ascent() - font_metrics.get_underline_position(),
            ) / f64::from(pango::SCALE),
            underline_thickness: f64::from(font_metrics.get_underline_thickness())
                / f64::from(pango::SCALE),
        }
    }

    #[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,
        }
    }
}

pub struct FontFeatures {
    attr: Option<pango::Attribute>,
}

impl FontFeatures {
    pub fn new() -> Self {
        FontFeatures { attr: None }
    }

    pub fn from(font_features: String) -> Self {
        if font_features.trim().is_empty() {
            return Self::new();
        }

        FontFeatures {
            attr: sys_pango::attribute::new_features(&font_features),
        }
    }

    pub fn insert_into(&self, attr_list: &pango::AttrList) {
        if let Some(ref attr) = self.attr {
            attr_list.insert(attr.clone());
        }
    }
}