fonts: add a metics method to the shaper

This commit is contained in:
Wez Furlong committed 2019-12-09 12:58:03 -08:00
1 parent c91345cbd6
commit d4f0eb40f7
2 files changed
+23 -2

No files matched your search

+18 -1
View File
@@ -2,7 +2,7 @@ use crate::font::ftwrap;
use crate::font::hbwrap as harfbuzz;
use crate::font::loader::FontDataHandle;
use crate::font::shaper::FontShaper;
use crate::font::system::GlyphInfo;
use crate::font::system::{FontMetrics, GlyphInfo};
use failure::{bail, Fallible};
use log::{debug, error};
use std::cell::RefCell;
@@ -196,4 +196,21 @@ impl FontShaper for HarfbuzzShaper {
fn shape(&self, text: &str, size: f64, dpi: u32) -> Fallible<Vec<GlyphInfo>> {
self.do_shape(0, text, size, dpi)
}
fn metrics(&self, size: f64, dpi: u32) -> Fallible<FontMetrics> {
let mut pair = self.fonts[0].borrow_mut();
let (cell_width, cell_height) = pair.face.set_font_size(size, dpi)?;
let y_scale = unsafe { (*(*pair.face.face).size).metrics.y_scale as f64 / 65536.0 };
Ok(FontMetrics {
cell_height,
cell_width,
// Note: face.face.descender is useless, we have to go through
// face.face.size.metrics to get to the real descender!
descender: unsafe { (*(*pair.face.face).size).metrics.descender as f64 } / 64.0,
underline_thickness: unsafe { (*pair.face.face).underline_thickness as f64 } * y_scale
/ 64.,
underline_position: unsafe { (*pair.face.face).underline_position as f64 } * y_scale
/ 64.,
})
}
}
+5 -1
View File
@@ -1,5 +1,5 @@
use crate::font::loader::FontDataHandle;
use crate::font::system::GlyphInfo;
use crate::font::system::{FontMetrics, GlyphInfo};
use failure::{format_err, Error, Fallible};
use serde_derive::*;
use std::sync::Mutex;
@@ -9,6 +9,10 @@ pub mod harfbuzz;
pub trait FontShaper {
/// Shape text and return a vector of GlyphInfo
fn shape(&self, text: &str, size: f64, dpi: u32) -> Fallible<Vec<GlyphInfo>>;
/// Compute the font metrics for the preferred font
/// at the specified size.
fn metrics(&self, size: f64, dpi: u32) -> Fallible<FontMetrics>;
}
#[derive(Debug, Deserialize, Clone, Copy)]