From d4f0eb40f7db70a71ca61f8ca117a6b4bddf4b88 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 9 Dec 2019 12:58:03 -0800 Subject: [PATCH] fonts: add a metics method to the shaper --- src/font/shaper/harfbuzz.rs | 19 ++++++++++++++++++- src/font/shaper/mod.rs | 6 +++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/font/shaper/harfbuzz.rs b/src/font/shaper/harfbuzz.rs index 2df8350ee..123598e53 100644 --- a/src/font/shaper/harfbuzz.rs +++ b/src/font/shaper/harfbuzz.rs @@ -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> { self.do_shape(0, text, size, dpi) } + + fn metrics(&self, size: f64, dpi: u32) -> Fallible { + 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., + }) + } } diff --git a/src/font/shaper/mod.rs b/src/font/shaper/mod.rs index 2842f3b27..d9a623016 100644 --- a/src/font/shaper/mod.rs +++ b/src/font/shaper/mod.rs @@ -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>; + + /// Compute the font metrics for the preferred font + /// at the specified size. + fn metrics(&self, size: f64, dpi: u32) -> Fallible; } #[derive(Debug, Deserialize, Clone, Copy)]