Skip to content

Commit

Permalink
style: Use horizontal metrics for ch in vertical mixed/sideways writi…
Browse files Browse the repository at this point in the history
…ng modes and for ex always.

Differential Revision: https://phabricator.services.mozilla.com/D23426
  • Loading branch information
heycam authored and emilio committed Mar 27, 2019
1 parent d5f208e commit d4635f1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
13 changes: 12 additions & 1 deletion components/style/font_metrics.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,14 +20,25 @@ pub struct FontMetrics {
pub zero_advance_measure: Option<Au>, pub zero_advance_measure: Option<Au>,
} }


/// Type of font metrics to retrieve.
#[derive(Clone, Debug, PartialEq)]
pub enum FontMetricsOrientation {
/// Get metrics for horizontal or vertical according to the Context's
/// writing mode.
MatchContext,
/// Force getting horizontal metrics.
Horizontal,
}

/// A trait used to represent something capable of providing us font metrics. /// A trait used to represent something capable of providing us font metrics.
pub trait FontMetricsProvider { pub trait FontMetricsProvider {
/// Obtain the metrics for given font family. /// Obtain the metrics for given font family.
fn query( fn query(
&self, &self,
_context: &crate::values::computed::Context, _context: &crate::values::computed::Context,
_base_size: crate::values::specified::length::FontBaseSize, _base_size: crate::values::specified::length::FontBaseSize,
) -> FontMetricsQueryResult { _orientation: FontMetricsOrientation,
) -> FontMetrics {
Default::default() Default::default()
} }


Expand Down
9 changes: 7 additions & 2 deletions components/style/gecko/wrapper.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::context::{PostAnimationTasks, QuirksMode, SharedStyleContext, UpdateA
use crate::data::ElementData; use crate::data::ElementData;
use crate::dom::{LayoutIterator, NodeInfo, OpaqueNode, TDocument, TElement, TNode, TShadowRoot}; use crate::dom::{LayoutIterator, NodeInfo, OpaqueNode, TDocument, TElement, TNode, TShadowRoot};
use crate::element_state::{DocumentState, ElementState}; use crate::element_state::{DocumentState, ElementState};
use crate::font_metrics::{FontMetrics, FontMetricsProvider}; use crate::font_metrics::{FontMetrics, FontMetricsOrientation, FontMetricsProvider};
use crate::gecko::data::GeckoStyleSheet; use crate::gecko::data::GeckoStyleSheet;
use crate::gecko::selector_parser::{NonTSPseudoClass, PseudoElement, SelectorImpl}; use crate::gecko::selector_parser::{NonTSPseudoClass, PseudoElement, SelectorImpl};
use crate::gecko::snapshot_helpers; use crate::gecko::snapshot_helpers;
Expand Down Expand Up @@ -1035,6 +1035,7 @@ impl FontMetricsProvider for GeckoFontMetricsProvider {
&self, &self,
context: &crate::values::computed::Context, context: &crate::values::computed::Context,
base_size: FontBaseSize, base_size: FontBaseSize,
orientation: FontMetricsOrientation,
) -> FontMetrics { ) -> FontMetrics {
let pc = match context.device().pres_context() { let pc = match context.device().pres_context() {
Some(pc) => pc, Some(pc) => pc,
Expand All @@ -1056,10 +1057,14 @@ impl FontMetricsProvider for GeckoFontMetricsProvider {
}, },
}; };


let vertical_metrics = match orientation {
FontMetricsOrientation::MatchContext => wm.is_vertical() && wm.is_upright(),
FontMetricsOrientation::Horizontal => false,
};
let gecko_metrics = unsafe { let gecko_metrics = unsafe {
bindings::Gecko_GetFontMetrics( bindings::Gecko_GetFontMetrics(
pc, pc,
wm.is_vertical() && !wm.is_sideways(), vertical_metrics,
font.gecko(), font.gecko(),
size.0, size.0,
// we don't use the user font set in a media query // we don't use the user font set in a media query
Expand Down
28 changes: 23 additions & 5 deletions components/style/values/specified/length.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! [length]: https://drafts.csswg.org/css-values/#lengths //! [length]: https://drafts.csswg.org/css-values/#lengths


use super::{AllowQuirks, Number, Percentage, ToComputedValue}; use super::{AllowQuirks, Number, Percentage, ToComputedValue};
use crate::font_metrics::FontMetrics; use crate::font_metrics::{FontMetrics, FontMetricsOrientation};
use crate::parser::{Parse, ParserContext}; use crate::parser::{Parse, ParserContext};
use crate::properties::computed_value_flags::ComputedValueFlags; use crate::properties::computed_value_flags::ComputedValueFlags;
use crate::values::computed::{self, CSSPixelLength, Context}; use crate::values::computed::{self, CSSPixelLength, Context};
Expand Down Expand Up @@ -133,8 +133,9 @@ impl FontRelativeLength {
fn query_font_metrics( fn query_font_metrics(
context: &Context, context: &Context,
base_size: FontBaseSize, base_size: FontBaseSize,
orientation: FontMetricsOrientation,
) -> FontMetrics { ) -> FontMetrics {
context.font_metrics_provider.query(context, base_size) context.font_metrics_provider.query(context, base_size, orientation)
} }


let reference_font_size = base_size.resolve(context); let reference_font_size = base_size.resolve(context);
Expand All @@ -160,7 +161,12 @@ impl FontRelativeLength {
context.rule_cache_conditions.borrow_mut().set_uncacheable(); context.rule_cache_conditions.borrow_mut().set_uncacheable();
} }
context.builder.add_flags(ComputedValueFlags::DEPENDS_ON_FONT_METRICS); context.builder.add_flags(ComputedValueFlags::DEPENDS_ON_FONT_METRICS);
let metrics = query_font_metrics(context, base_size); // The x-height is an intrinsically horizontal metric.
let metrics = query_font_metrics(
context,
base_size,
FontMetricsOrientation::Horizontal
);
let reference_size = metrics.x_height.unwrap_or_else(|| { let reference_size = metrics.x_height.unwrap_or_else(|| {
// https://drafts.csswg.org/css-values/#ex // https://drafts.csswg.org/css-values/#ex
// //
Expand All @@ -177,7 +183,18 @@ impl FontRelativeLength {
context.rule_cache_conditions.borrow_mut().set_uncacheable(); context.rule_cache_conditions.borrow_mut().set_uncacheable();
} }
context.builder.add_flags(ComputedValueFlags::DEPENDS_ON_FONT_METRICS); context.builder.add_flags(ComputedValueFlags::DEPENDS_ON_FONT_METRICS);
let metrics = query_font_metrics(context, base_size); // https://drafts.csswg.org/css-values/#ch:
//
// Equal to the used advance measure of the “0” (ZERO,
// U+0030) glyph in the font used to render it. (The advance
// measure of a glyph is its advance width or height,
// whichever is in the inline axis of the element.)
//
let metrics = query_font_metrics(
context,
base_size,
FontMetricsOrientation::MatchContext,
);
let reference_size = metrics.zero_advance_measure.unwrap_or_else(|| { let reference_size = metrics.zero_advance_measure.unwrap_or_else(|| {
// https://drafts.csswg.org/css-values/#ch // https://drafts.csswg.org/css-values/#ch
// //
Expand All @@ -189,7 +206,8 @@ impl FontRelativeLength {
// writing-mode is vertical-rl or vertical-lr and // writing-mode is vertical-rl or vertical-lr and
// text-orientation is upright). // text-orientation is upright).
// //
if context.style().writing_mode.is_vertical() { let wm = context.style().writing_mode;
if wm.is_vertical() && wm.is_upright() {
reference_font_size reference_font_size
} else { } else {
reference_font_size.scale_by(0.5) reference_font_size.scale_by(0.5)
Expand Down

0 comments on commit d4635f1

Please sign in to comment.