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:
parent
ac2a1ece9e
commit
7fc50f6690
|
@ -57,8 +57,8 @@ font:
|
||||||
# Offset is the extra space around each character. offset.y can be thought of
|
# 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.
|
# as modifying the linespacing, and offset.x as modifying the letter spacing.
|
||||||
offset:
|
offset:
|
||||||
x: 2.0
|
x: 0.0
|
||||||
y: -7.0
|
y: 0.0
|
||||||
|
|
||||||
# OS X only: use thin stroke font rendering. Thin strokes are suitable
|
# 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
|
# for retina displays, but for non-retina you probably want this set to
|
||||||
|
|
|
@ -59,18 +59,15 @@ impl ::Rasterize for FreeTypeRasterizer {
|
||||||
.get(&key)
|
.get(&key)
|
||||||
.ok_or(Error::FontNotLoaded)?;
|
.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 width = (size_metrics.max_advance / 64) as f64;
|
||||||
let w = face.max_advance_width() as f64;
|
let height = (size_metrics.height / 64) 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;
|
|
||||||
|
|
||||||
Ok(Metrics {
|
Ok(Metrics {
|
||||||
average_advance: w_scale,
|
average_advance: width,
|
||||||
line_height: h_scale,
|
line_height: height,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,6 +275,9 @@ pub enum Error {
|
||||||
/// Couldn't find font matching description
|
/// Couldn't find font matching description
|
||||||
MissingFont(FontDesc),
|
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
|
/// Requested an operation with a FontKey that isn't known to the rasterizer
|
||||||
FontNotLoaded,
|
FontNotLoaded,
|
||||||
}
|
}
|
||||||
|
@ -295,6 +295,7 @@ impl ::std::error::Error for Error {
|
||||||
Error::FreeType(ref err) => err.description(),
|
Error::FreeType(ref err) => err.description(),
|
||||||
Error::MissingFont(ref _desc) => "couldn't find the requested font",
|
Error::MissingFont(ref _desc) => "couldn't find the requested font",
|
||||||
Error::FontNotLoaded => "tried to operate on font that hasn't been loaded",
|
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 => {
|
Error::FontNotLoaded => {
|
||||||
f.write_str("Tried to use a font that hasn't been loaded")
|
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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1189,6 +1189,12 @@ impl FontOffset {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for FontOffset {
|
||||||
|
fn default() -> FontOffset {
|
||||||
|
FontOffset { x: 0.0, y: 0.0 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
trait DeserializeFromF32 : Sized {
|
trait DeserializeFromF32 : Sized {
|
||||||
fn deserialize_from_f32<D>(D) -> ::std::result::Result<Self, D::Error>
|
fn deserialize_from_f32<D>(D) -> ::std::result::Result<Self, D::Error>
|
||||||
where D: serde::de::Deserializer;
|
where D: serde::de::Deserializer;
|
||||||
|
@ -1301,10 +1307,7 @@ impl Default for Font {
|
||||||
italic: FontDescription::new_with_family("Menlo"),
|
italic: FontDescription::new_with_family("Menlo"),
|
||||||
size: Size::new(11.0),
|
size: Size::new(11.0),
|
||||||
use_thin_strokes: true,
|
use_thin_strokes: true,
|
||||||
offset: FontOffset {
|
offset: Default::default()
|
||||||
x: 0.0,
|
|
||||||
y: 0.0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1318,12 +1321,7 @@ impl Default for Font {
|
||||||
italic: FontDescription::new_with_family("monospace"),
|
italic: FontDescription::new_with_family("monospace"),
|
||||||
size: Size::new(11.0),
|
size: Size::new(11.0),
|
||||||
use_thin_strokes: false,
|
use_thin_strokes: false,
|
||||||
offset: FontOffset {
|
offset: Default::default()
|
||||||
// TODO should improve freetype metrics... shouldn't need such
|
|
||||||
// drastic offsets for the default!
|
|
||||||
x: 2.0,
|
|
||||||
y: -7.0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue