mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
stylo: Use gecko's font metrics
This commit is contained in:
parent
1154600dd4
commit
25667d0321
10 changed files with 72 additions and 58 deletions
|
@ -9,7 +9,7 @@
|
|||
use app_units::Au;
|
||||
use cssparser::{Parser, Token};
|
||||
use euclid::size::Size2D;
|
||||
use font_metrics::FontMetrics;
|
||||
use font_metrics::FontMetricsQueryResult;
|
||||
use parser::{Parse, ParserContext};
|
||||
use std::{cmp, fmt, mem};
|
||||
use std::ascii::AsciiExt;
|
||||
|
@ -69,22 +69,17 @@ impl ToCss for FontRelativeLength {
|
|||
}
|
||||
|
||||
impl FontRelativeLength {
|
||||
/// Gets the first available font metrics from the current context's
|
||||
/// font-family list.
|
||||
pub fn find_first_available_font_metrics(context: &Context) -> Option<FontMetrics> {
|
||||
use font_metrics::FontMetricsQueryResult::*;
|
||||
for family in context.style().get_font().font_family_iter() {
|
||||
if let Available(metrics) = context.font_metrics_provider.query(family.atom()) {
|
||||
return metrics;
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Computes the font-relative length. We use the use_inherited flag to
|
||||
/// special-case the computation of font-size.
|
||||
pub fn to_computed_value(&self, context: &Context, use_inherited: bool) -> Au {
|
||||
fn query_font_metrics(context: &Context, reference_font_size: Au) -> FontMetricsQueryResult {
|
||||
context.font_metrics_provider.query(context.style().get_font(),
|
||||
reference_font_size,
|
||||
context.style().writing_mode,
|
||||
context.in_media_query,
|
||||
context.device)
|
||||
}
|
||||
|
||||
let reference_font_size = if use_inherited {
|
||||
context.inherited_style().get_font().clone_font_size()
|
||||
} else {
|
||||
|
@ -95,33 +90,20 @@ impl FontRelativeLength {
|
|||
match *self {
|
||||
FontRelativeLength::Em(length) => reference_font_size.scale_by(length),
|
||||
FontRelativeLength::Ex(length) => {
|
||||
match Self::find_first_available_font_metrics(context) {
|
||||
Some(metrics) => metrics.x_height,
|
||||
match query_font_metrics(context, reference_font_size) {
|
||||
FontMetricsQueryResult::Available(metrics) => metrics.x_height.scale_by(length),
|
||||
// https://drafts.csswg.org/css-values/#ex
|
||||
//
|
||||
// In the cases where it is impossible or impractical to
|
||||
// determine the x-height, a value of 0.5em must be
|
||||
// assumed.
|
||||
//
|
||||
None => reference_font_size.scale_by(0.5 * length),
|
||||
FontMetricsQueryResult::NotAvailable => reference_font_size.scale_by(0.5 * length),
|
||||
}
|
||||
},
|
||||
FontRelativeLength::Ch(length) => {
|
||||
let wm = context.style().writing_mode;
|
||||
|
||||
// TODO(emilio, #14144): Compute this properly once we support
|
||||
// all the relevant writing-mode related properties, this should
|
||||
// be equivalent to "is the text in the block direction?".
|
||||
let vertical = wm.is_vertical();
|
||||
|
||||
match Self::find_first_available_font_metrics(context) {
|
||||
Some(metrics) => {
|
||||
if vertical {
|
||||
metrics.zero_advance_measure.height
|
||||
} else {
|
||||
metrics.zero_advance_measure.width
|
||||
}
|
||||
}
|
||||
match query_font_metrics(context, reference_font_size) {
|
||||
FontMetricsQueryResult::Available(metrics) => metrics.zero_advance_measure.scale_by(length),
|
||||
// https://drafts.csswg.org/css-values/#ch
|
||||
//
|
||||
// In the cases where it is impossible or impractical to
|
||||
|
@ -132,8 +114,8 @@ impl FontRelativeLength {
|
|||
// writing-mode is vertical-rl or vertical-lr and
|
||||
// text-orientation is upright).
|
||||
//
|
||||
None => {
|
||||
if vertical {
|
||||
FontMetricsQueryResult::NotAvailable => {
|
||||
if context.style().writing_mode.is_vertical() {
|
||||
reference_font_size.scale_by(length)
|
||||
} else {
|
||||
reference_font_size.scale_by(0.5 * length)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue