Allow linear values for text edges

This commit is contained in:
Laurenz 2021-09-28 09:42:04 +02:00
parent f1ab290572
commit 0e89facb53
5 changed files with 31 additions and 21 deletions

View File

@ -8,7 +8,7 @@ use std::rc::Rc;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use ttf_parser::{name_id, GlyphId, PlatformId}; use ttf_parser::{name_id, GlyphId, PlatformId};
use crate::geom::Em; use crate::geom::{Em, Length, Linear};
use crate::loading::{FileHash, Loader}; use crate::loading::{FileHash, Loader};
use crate::util::decode_mac_roman; use crate::util::decode_mac_roman;
@ -269,20 +269,21 @@ impl Face {
.map(|units| self.to_em(units)) .map(|units| self.to_em(units))
} }
/// Look up a vertical metric. /// Look up a vertical metric at the given font size.
pub fn vertical_metric(&self, metric: VerticalFontMetric) -> Em { pub fn vertical_metric(&self, metric: VerticalFontMetric, size: Length) -> Length {
match metric { match metric {
VerticalFontMetric::Ascender => self.ascender, VerticalFontMetric::Ascender => self.ascender.to_length(size),
VerticalFontMetric::CapHeight => self.cap_height, VerticalFontMetric::CapHeight => self.cap_height.to_length(size),
VerticalFontMetric::XHeight => self.x_height, VerticalFontMetric::XHeight => self.x_height.to_length(size),
VerticalFontMetric::Baseline => Em::zero(), VerticalFontMetric::Baseline => Length::zero(),
VerticalFontMetric::Descender => self.descender, VerticalFontMetric::Descender => self.descender.to_length(size),
VerticalFontMetric::Linear(v) => v.resolve(size),
} }
} }
} }
/// Identifies a vertical metric of a font. /// Identifies a vertical metric of a font.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] #[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub enum VerticalFontMetric { pub enum VerticalFontMetric {
/// The distance from the baseline to the typographic ascender. /// The distance from the baseline to the typographic ascender.
/// ///
@ -300,17 +301,21 @@ pub enum VerticalFontMetric {
/// Corresponds to the typographic descender from the `OS/2` table if /// Corresponds to the typographic descender from the `OS/2` table if
/// present and falls back to the descender from the `hhea` table otherwise. /// present and falls back to the descender from the `hhea` table otherwise.
Descender, Descender,
/// An font-size dependent distance from the baseline (positive goes up, negative
/// down).
Linear(Linear),
} }
impl Debug for VerticalFontMetric { impl Debug for VerticalFontMetric {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(match self { match self {
Self::Ascender => "ascender", Self::Ascender => f.pad("ascender"),
Self::CapHeight => "cap-height", Self::CapHeight => f.pad("cap-height"),
Self::XHeight => "x-height", Self::XHeight => f.pad("x-height"),
Self::Baseline => "baseline", Self::Baseline => f.pad("baseline"),
Self::Descender => "descender", Self::Descender => f.pad("descender"),
}) Self::Linear(v) => v.fmt(f),
}
} }
} }

View File

@ -342,8 +342,8 @@ fn measure(
// Expand top and bottom by reading the face's vertical metrics. // Expand top and bottom by reading the face's vertical metrics.
let mut expand = |face: &Face| { let mut expand = |face: &Face| {
top.set_max(face.vertical_metric(state.top_edge).to_length(state.size)); top.set_max(face.vertical_metric(state.top_edge, state.size));
bottom.set_max(-face.vertical_metric(state.bottom_edge).to_length(state.size)); bottom.set_max(-face.vertical_metric(state.bottom_edge, state.size));
}; };
if glyphs.is_empty() { if glyphs.is_empty() {

View File

@ -127,16 +127,19 @@ dynamic! {
dynamic! { dynamic! {
FontWeight: "font weight", FontWeight: "font weight",
Value::Int(number) => { Value::Int(v) => {
u16::try_from(number).map_or(Self::BLACK, Self::from_number) u16::try_from(v).map_or(Self::BLACK, Self::from_number)
}, },
} }
dynamic! { dynamic! {
FontStretch: "font stretch", FontStretch: "font stretch",
Value::Relative(relative) => Self::from_ratio(relative.get() as f32), Value::Relative(v) => Self::from_ratio(v.get() as f32),
} }
dynamic! { dynamic! {
VerticalFontMetric: "vertical font metric", VerticalFontMetric: "vertical font metric",
Value::Length(v) => Self::Linear(v.into()),
Value::Relative(v) => Self::Linear(v.into()),
Value::Linear(v) => Self::Linear(v),
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 24 KiB

View File

@ -58,6 +58,8 @@ Emoji: 🐪, 🌋, 🏞
#try(ascender, baseline) #try(ascender, baseline)
#try(cap-height, baseline) #try(cap-height, baseline)
#try(x-height, baseline) #try(x-height, baseline)
#try(4pt, -2pt)
#try(1pt + 27%, -18%)
--- ---
// Error: 7-12 unexpected argument // Error: 7-12 unexpected argument