Improve freetype metric usage

The font metrics function was using freetype metrics in an ineffective
way, improve the use of those metrics and remove the now unnecessary
separate default values for font offset in linux.
This commit is contained in:
Aaron Williamson 2017-01-28 19:30:47 -07:00 committed by Joe Wilm
parent ac2a1ece9e
commit 7fc50f6690
3 changed files with 23 additions and 21 deletions

View File

@ -57,8 +57,8 @@ font:
# Offset is the extra space around each character. offset.y can be thought of
# as modifying the linespacing, and offset.x as modifying the letter spacing.
offset:
x: 2.0
y: -7.0
x: 0.0
y: 0.0
# OS X only: use thin stroke font rendering. Thin strokes are suitable
# for retina displays, but for non-retina you probably want this set to

View File

@ -59,18 +59,15 @@ impl ::Rasterize for FreeTypeRasterizer {
.get(&key)
.ok_or(Error::FontNotLoaded)?;
let scale_size = self.dpr as f64 * size.as_f32_pts() as f64;
let size_metrics = face.size_metrics()
.ok_or(Error::MissingSizeMetrics)?;
let em_size = face.em_size() as f64;
let w = face.max_advance_width() as f64;
let h = (face.ascender() - face.descender() + face.height()) as f64;
let w_scale = w * scale_size / em_size;
let h_scale = h * scale_size / em_size;
let width = (size_metrics.max_advance / 64) as f64;
let height = (size_metrics.height / 64) as f64;
Ok(Metrics {
average_advance: w_scale,
line_height: h_scale,
average_advance: width,
line_height: height,
})
}
@ -278,6 +275,9 @@ pub enum Error {
/// Couldn't find font matching description
MissingFont(FontDesc),
/// Tried to get size metrics from a Face that didn't have a size
MissingSizeMetrics,
/// Requested an operation with a FontKey that isn't known to the rasterizer
FontNotLoaded,
}
@ -295,6 +295,7 @@ impl ::std::error::Error for Error {
Error::FreeType(ref err) => err.description(),
Error::MissingFont(ref _desc) => "couldn't find the requested font",
Error::FontNotLoaded => "tried to operate on font that hasn't been loaded",
Error::MissingSizeMetrics => "tried to get size metrics from a face without a size",
}
}
}
@ -311,6 +312,9 @@ impl ::std::fmt::Display for Error {
},
Error::FontNotLoaded => {
f.write_str("Tried to use a font that hasn't been loaded")
},
Error::MissingSizeMetrics => {
f.write_str("Tried to get size metrics from a face without a size")
}
}
}

View File

@ -1189,6 +1189,12 @@ impl FontOffset {
}
}
impl Default for FontOffset {
fn default() -> FontOffset {
FontOffset { x: 0.0, y: 0.0 }
}
}
trait DeserializeFromF32 : Sized {
fn deserialize_from_f32<D>(D) -> ::std::result::Result<Self, D::Error>
where D: serde::de::Deserializer;
@ -1301,10 +1307,7 @@ impl Default for Font {
italic: FontDescription::new_with_family("Menlo"),
size: Size::new(11.0),
use_thin_strokes: true,
offset: FontOffset {
x: 0.0,
y: 0.0
}
offset: Default::default()
}
}
}
@ -1318,12 +1321,7 @@ impl Default for Font {
italic: FontDescription::new_with_family("monospace"),
size: Size::new(11.0),
use_thin_strokes: false,
offset: FontOffset {
// TODO should improve freetype metrics... shouldn't need such
// drastic offsets for the default!
x: 2.0,
y: -7.0
}
offset: Default::default()
}
}
}