mirror of
https://github.com/servo/servo.git
synced 2025-08-04 13:10:20 +01:00
style: Use less Au in font code.
Font code is the only thing that was using Au in the style system without interfacing with Gecko, and there was no real reason for it to do so. This slightly simplifies the code. Differential Revision: https://phabricator.services.mozilla.com/D57248
This commit is contained in:
parent
4cd8813a81
commit
a541046147
13 changed files with 62 additions and 89 deletions
|
@ -8,16 +8,16 @@
|
||||||
|
|
||||||
use crate::context::SharedStyleContext;
|
use crate::context::SharedStyleContext;
|
||||||
use crate::Atom;
|
use crate::Atom;
|
||||||
use app_units::Au;
|
use crate::values::computed::Length;
|
||||||
|
|
||||||
/// Represents the font metrics that style needs from a font to compute the
|
/// Represents the font metrics that style needs from a font to compute the
|
||||||
/// value of certain CSS units like `ex`.
|
/// value of certain CSS units like `ex`.
|
||||||
#[derive(Clone, Debug, Default, PartialEq)]
|
#[derive(Clone, Debug, Default, PartialEq)]
|
||||||
pub struct FontMetrics {
|
pub struct FontMetrics {
|
||||||
/// The x-height of the font.
|
/// The x-height of the font.
|
||||||
pub x_height: Option<Au>,
|
pub x_height: Option<Length>,
|
||||||
/// The zero advance. This is usually writing mode dependent
|
/// The zero advance. This is usually writing mode dependent
|
||||||
pub zero_advance_measure: Option<Au>,
|
pub zero_advance_measure: Option<Length>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Type of font metrics to retrieve.
|
/// Type of font metrics to retrieve.
|
||||||
|
@ -47,7 +47,7 @@ pub trait FontMetricsProvider {
|
||||||
&self,
|
&self,
|
||||||
font_name: &Atom,
|
font_name: &Atom,
|
||||||
font_family: crate::values::computed::font::GenericFontFamily,
|
font_family: crate::values::computed::font::GenericFontFamily,
|
||||||
) -> Au;
|
) -> Length;
|
||||||
|
|
||||||
/// Construct from a shared style context
|
/// Construct from a shared style context
|
||||||
fn create_from(context: &SharedStyleContext) -> Self
|
fn create_from(context: &SharedStyleContext) -> Self
|
||||||
|
@ -70,7 +70,7 @@ impl FontMetricsProvider for ServoMetricsProvider {
|
||||||
ServoMetricsProvider
|
ServoMetricsProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_size(&self, _: &Atom, _: crate::values::computed::font::GenericFontFamily) -> Au {
|
fn get_size(&self, _: &Atom, _: crate::values::computed::font::GenericFontFamily) -> Length {
|
||||||
unreachable!("Dummy provider should never be used to compute font size")
|
unreachable!("Dummy provider should never be used to compute font size")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,10 +11,9 @@ use crate::gecko_bindings::structs;
|
||||||
use crate::media_queries::MediaType;
|
use crate::media_queries::MediaType;
|
||||||
use crate::properties::ComputedValues;
|
use crate::properties::ComputedValues;
|
||||||
use crate::string_cache::Atom;
|
use crate::string_cache::Atom;
|
||||||
use crate::values::computed::font::FontSize;
|
use crate::values::specified::font::FONT_MEDIUM_PX;
|
||||||
use crate::values::{CustomIdent, KeyframesName};
|
use crate::values::{CustomIdent, KeyframesName};
|
||||||
use app_units::Au;
|
use app_units::{Au, AU_PER_PX};
|
||||||
use app_units::AU_PER_PX;
|
|
||||||
use cssparser::RGBA;
|
use cssparser::RGBA;
|
||||||
use euclid::default::Size2D;
|
use euclid::default::Size2D;
|
||||||
use euclid::Scale;
|
use euclid::Scale;
|
||||||
|
@ -87,7 +86,7 @@ impl Device {
|
||||||
document,
|
document,
|
||||||
default_values: ComputedValues::default_values(doc),
|
default_values: ComputedValues::default_values(doc),
|
||||||
// FIXME(bz): Seems dubious?
|
// FIXME(bz): Seems dubious?
|
||||||
root_font_size: AtomicIsize::new(FontSize::medium().size().0 as isize),
|
root_font_size: AtomicIsize::new(Au::from_px(FONT_MEDIUM_PX as i32).0 as isize),
|
||||||
body_text_color: AtomicUsize::new(prefs.mDefaultColor as usize),
|
body_text_color: AtomicUsize::new(prefs.mDefaultColor as usize),
|
||||||
used_root_font_size: AtomicBool::new(false),
|
used_root_font_size: AtomicBool::new(false),
|
||||||
used_viewport_size: AtomicBool::new(false),
|
used_viewport_size: AtomicBool::new(false),
|
||||||
|
@ -139,8 +138,7 @@ impl Device {
|
||||||
|
|
||||||
/// Set the font size of the root element (for rem)
|
/// Set the font size of the root element (for rem)
|
||||||
pub fn set_root_font_size(&self, size: Au) {
|
pub fn set_root_font_size(&self, size: Au) {
|
||||||
self.root_font_size
|
self.root_font_size.store(size.0 as isize, Ordering::Relaxed)
|
||||||
.store(size.0 as isize, Ordering::Relaxed)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the body text color for the "inherit color from body" quirk.
|
/// Sets the body text color for the "inherit color from body" quirk.
|
||||||
|
|
|
@ -68,6 +68,7 @@ use crate::shared_lock::Locked;
|
||||||
use crate::string_cache::{Atom, Namespace, WeakAtom, WeakNamespace};
|
use crate::string_cache::{Atom, Namespace, WeakAtom, WeakNamespace};
|
||||||
use crate::stylist::CascadeData;
|
use crate::stylist::CascadeData;
|
||||||
use crate::values::computed::font::GenericFontFamily;
|
use crate::values::computed::font::GenericFontFamily;
|
||||||
|
use crate::values::computed::Length;
|
||||||
use crate::values::specified::length::FontBaseSize;
|
use crate::values::specified::length::FontBaseSize;
|
||||||
use crate::CaseSensitivityExt;
|
use crate::CaseSensitivityExt;
|
||||||
use app_units::Au;
|
use app_units::Au;
|
||||||
|
@ -929,7 +930,7 @@ impl FontMetricsProvider for GeckoFontMetricsProvider {
|
||||||
GeckoFontMetricsProvider::new()
|
GeckoFontMetricsProvider::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_size(&self, font_name: &Atom, font_family: GenericFontFamily) -> Au {
|
fn get_size(&self, font_name: &Atom, font_family: GenericFontFamily) -> Length {
|
||||||
let mut cache = self.font_size_cache.borrow_mut();
|
let mut cache = self.font_size_cache.borrow_mut();
|
||||||
if let Some(sizes) = cache.iter().find(|el| el.0 == *font_name) {
|
if let Some(sizes) = cache.iter().find(|el| el.0 == *font_name) {
|
||||||
return sizes.1.size_for_generic(font_family);
|
return sizes.1.size_for_generic(font_family);
|
||||||
|
@ -950,7 +951,7 @@ impl FontMetricsProvider for GeckoFontMetricsProvider {
|
||||||
None => return Default::default(),
|
None => return Default::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let size = base_size.resolve(context);
|
let size = Au::from(base_size.resolve(context));
|
||||||
let style = context.style();
|
let style = context.style();
|
||||||
|
|
||||||
let (wm, font) = match base_size {
|
let (wm, font) = match base_size {
|
||||||
|
@ -977,9 +978,9 @@ impl FontMetricsProvider for GeckoFontMetricsProvider {
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
FontMetrics {
|
FontMetrics {
|
||||||
x_height: Some(Au(gecko_metrics.mXSize)),
|
x_height: Some(Au(gecko_metrics.mXSize).into()),
|
||||||
zero_advance_measure: if gecko_metrics.mChSize >= 0 {
|
zero_advance_measure: if gecko_metrics.mChSize >= 0 {
|
||||||
Some(Au(gecko_metrics.mChSize))
|
Some(Au(gecko_metrics.mChSize).into())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
|
@ -988,7 +989,7 @@ impl FontMetricsProvider for GeckoFontMetricsProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl structs::FontSizePrefs {
|
impl structs::FontSizePrefs {
|
||||||
fn size_for_generic(&self, font_family: GenericFontFamily) -> Au {
|
fn size_for_generic(&self, font_family: GenericFontFamily) -> Length {
|
||||||
Au(match font_family {
|
Au(match font_family {
|
||||||
GenericFontFamily::None => self.mDefaultVariableSize,
|
GenericFontFamily::None => self.mDefaultVariableSize,
|
||||||
GenericFontFamily::Serif => self.mDefaultSerifSize,
|
GenericFontFamily::Serif => self.mDefaultSerifSize,
|
||||||
|
@ -999,7 +1000,7 @@ impl structs::FontSizePrefs {
|
||||||
GenericFontFamily::MozEmoji => unreachable!(
|
GenericFontFamily::MozEmoji => unreachable!(
|
||||||
"Should never get here, since this doesn't (yet) appear on font family"
|
"Should never get here, since this doesn't (yet) appear on font family"
|
||||||
),
|
),
|
||||||
})
|
}).into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -715,7 +715,7 @@ pub trait MatchMethods: TElement {
|
||||||
.map_or(true, |s| s.get_font().clone_font_size() != new_font_size)
|
.map_or(true, |s| s.get_font().clone_font_size() != new_font_size)
|
||||||
{
|
{
|
||||||
debug_assert!(self.owner_doc_matches_for_testing(device));
|
debug_assert!(self.owner_doc_matches_for_testing(device));
|
||||||
device.set_root_font_size(new_font_size.size());
|
device.set_root_font_size(new_font_size.size().into());
|
||||||
// If the root font-size changed since last time, and something
|
// If the root font-size changed since last time, and something
|
||||||
// in the document did use rem units, ensure we recascade the
|
// in the document did use rem units, ensure we recascade the
|
||||||
// entire tree.
|
// entire tree.
|
||||||
|
|
|
@ -743,6 +743,7 @@ impl<'a, 'b: 'a> Cascade<'a, 'b> {
|
||||||
fn recompute_keyword_font_size_if_needed(&mut self) {
|
fn recompute_keyword_font_size_if_needed(&mut self) {
|
||||||
use crate::values::computed::ToComputedValue;
|
use crate::values::computed::ToComputedValue;
|
||||||
use crate::values::specified;
|
use crate::values::specified;
|
||||||
|
use app_units::Au;
|
||||||
|
|
||||||
if !self.seen.contains(LonghandId::XLang) &&
|
if !self.seen.contains(LonghandId::XLang) &&
|
||||||
!self.seen.contains(LonghandId::FontFamily) {
|
!self.seen.contains(LonghandId::FontFamily) {
|
||||||
|
@ -759,7 +760,7 @@ impl<'a, 'b: 'a> Cascade<'a, 'b> {
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
if font.gecko().mScriptUnconstrainedSize == new_size.size().0 {
|
if font.gecko().mScriptUnconstrainedSize == Au::from(new_size.size()).0 {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1134,7 +1134,7 @@ fn static_assert() {
|
||||||
pub fn set_font_size(&mut self, v: FontSize) {
|
pub fn set_font_size(&mut self, v: FontSize) {
|
||||||
use crate::values::specified::font::KeywordSize;
|
use crate::values::specified::font::KeywordSize;
|
||||||
|
|
||||||
let size = v.size();
|
let size = Au::from(v.size());
|
||||||
self.gecko.mScriptUnconstrainedSize = size.0;
|
self.gecko.mScriptUnconstrainedSize = size.0;
|
||||||
|
|
||||||
// These two may be changed from Cascade::fixup_font_stuff.
|
// These two may be changed from Cascade::fixup_font_stuff.
|
||||||
|
|
|
@ -10,9 +10,9 @@ use crate::media_queries::media_feature::{Evaluator, MediaFeatureDescription};
|
||||||
use crate::media_queries::media_feature_expression::RangeOrOperator;
|
use crate::media_queries::media_feature_expression::RangeOrOperator;
|
||||||
use crate::media_queries::MediaType;
|
use crate::media_queries::MediaType;
|
||||||
use crate::properties::ComputedValues;
|
use crate::properties::ComputedValues;
|
||||||
use crate::values::computed::font::FontSize;
|
|
||||||
use crate::values::computed::CSSPixelLength;
|
use crate::values::computed::CSSPixelLength;
|
||||||
use crate::values::KeyframesName;
|
use crate::values::KeyframesName;
|
||||||
|
use crate::values::specified::font::FONT_MEDIUM_PX;
|
||||||
use app_units::Au;
|
use app_units::Au;
|
||||||
use cssparser::RGBA;
|
use cssparser::RGBA;
|
||||||
use euclid::default::Size2D as UntypedSize2D;
|
use euclid::default::Size2D as UntypedSize2D;
|
||||||
|
@ -68,7 +68,7 @@ impl Device {
|
||||||
viewport_size,
|
viewport_size,
|
||||||
device_pixel_ratio,
|
device_pixel_ratio,
|
||||||
// FIXME(bz): Seems dubious?
|
// FIXME(bz): Seems dubious?
|
||||||
root_font_size: AtomicIsize::new(FontSize::medium().size().0 as isize),
|
root_font_size: AtomicIsize::new(Au::from_px(FONT_MEDIUM_PX).0 as isize),
|
||||||
used_root_font_size: AtomicBool::new(false),
|
used_root_font_size: AtomicBool::new(false),
|
||||||
used_viewport_units: AtomicBool::new(false),
|
used_viewport_units: AtomicBool::new(false),
|
||||||
environment: CssEnvironment,
|
environment: CssEnvironment,
|
||||||
|
|
|
@ -21,7 +21,6 @@ use crate::values::specified::font::{
|
||||||
use crate::values::specified::length::{FontBaseSize, NoCalcLength};
|
use crate::values::specified::length::{FontBaseSize, NoCalcLength};
|
||||||
use crate::values::CSSFloat;
|
use crate::values::CSSFloat;
|
||||||
use crate::Atom;
|
use crate::Atom;
|
||||||
use app_units::Au;
|
|
||||||
use byteorder::{BigEndian, ByteOrder};
|
use byteorder::{BigEndian, ByteOrder};
|
||||||
use cssparser::{serialize_identifier, CssStringWriter, Parser};
|
use cssparser::{serialize_identifier, CssStringWriter, Parser};
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
|
@ -148,15 +147,16 @@ impl FontWeight {
|
||||||
|
|
||||||
impl FontSize {
|
impl FontSize {
|
||||||
/// The actual computed font size.
|
/// The actual computed font size.
|
||||||
pub fn size(self) -> Au {
|
#[inline]
|
||||||
self.size.into()
|
pub fn size(&self) -> Length {
|
||||||
|
self.size.0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
/// Get default value of font size.
|
/// Get default value of font size.
|
||||||
pub fn medium() -> Self {
|
pub fn medium() -> Self {
|
||||||
Self {
|
Self {
|
||||||
size: Au::from_px(specified::FONT_MEDIUM_PX).into(),
|
size: NonNegative(Length::new(specified::FONT_MEDIUM_PX as CSSFloat)),
|
||||||
keyword_info: Some(KeywordInfo::medium()),
|
keyword_info: Some(KeywordInfo::medium()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ pub use crate::values::specified::url::UrlOrNone;
|
||||||
pub use crate::values::specified::{Angle, BorderStyle, Time};
|
pub use crate::values::specified::{Angle, BorderStyle, Time};
|
||||||
|
|
||||||
impl ToComputedValue for specified::NoCalcLength {
|
impl ToComputedValue for specified::NoCalcLength {
|
||||||
type ComputedValue = CSSPixelLength;
|
type ComputedValue = Length;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
|
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
|
||||||
|
|
|
@ -20,7 +20,6 @@ use crate::values::specified::{AllowQuirks, Angle, Integer, LengthPercentage};
|
||||||
use crate::values::specified::{NoCalcLength, NonNegativeNumber, Number, Percentage};
|
use crate::values::specified::{NoCalcLength, NonNegativeNumber, Number, Percentage};
|
||||||
use crate::values::CustomIdent;
|
use crate::values::CustomIdent;
|
||||||
use crate::Atom;
|
use crate::Atom;
|
||||||
use app_units::Au;
|
|
||||||
use byteorder::{BigEndian, ByteOrder};
|
use byteorder::{BigEndian, ByteOrder};
|
||||||
use cssparser::{Parser, Token};
|
use cssparser::{Parser, Token};
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
|
@ -773,18 +772,18 @@ impl ToComputedValue for KeywordSize {
|
||||||
type ComputedValue = NonNegativeLength;
|
type ComputedValue = NonNegativeLength;
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_computed_value(&self, _: &Context) -> NonNegativeLength {
|
fn to_computed_value(&self, _: &Context) -> NonNegativeLength {
|
||||||
|
let medium = Length::new(FONT_MEDIUM_PX as f32);
|
||||||
// https://drafts.csswg.org/css-fonts-3/#font-size-prop
|
// https://drafts.csswg.org/css-fonts-3/#font-size-prop
|
||||||
match *self {
|
NonNegative(match *self {
|
||||||
KeywordSize::XXSmall => Au::from_px(FONT_MEDIUM_PX) * 3 / 5,
|
KeywordSize::XXSmall => medium * 3.0 / 5.0,
|
||||||
KeywordSize::XSmall => Au::from_px(FONT_MEDIUM_PX) * 3 / 4,
|
KeywordSize::XSmall => medium * 3.0 / 4.0,
|
||||||
KeywordSize::Small => Au::from_px(FONT_MEDIUM_PX) * 8 / 9,
|
KeywordSize::Small => medium * 8.0 / 9.0,
|
||||||
KeywordSize::Medium => Au::from_px(FONT_MEDIUM_PX),
|
KeywordSize::Medium => medium,
|
||||||
KeywordSize::Large => Au::from_px(FONT_MEDIUM_PX) * 6 / 5,
|
KeywordSize::Large => medium * 6.0 / 5.0,
|
||||||
KeywordSize::XLarge => Au::from_px(FONT_MEDIUM_PX) * 3 / 2,
|
KeywordSize::XLarge => medium * 3.0 / 2.0,
|
||||||
KeywordSize::XXLarge => Au::from_px(FONT_MEDIUM_PX) * 2,
|
KeywordSize::XXLarge => medium * 2.0,
|
||||||
KeywordSize::XXXLarge => Au::from_px(FONT_MEDIUM_PX) * 3,
|
KeywordSize::XXXLarge => medium * 3.0,
|
||||||
}
|
})
|
||||||
.into()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -799,7 +798,6 @@ impl ToComputedValue for KeywordSize {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_computed_value(&self, cx: &Context) -> NonNegativeLength {
|
fn to_computed_value(&self, cx: &Context) -> NonNegativeLength {
|
||||||
use crate::context::QuirksMode;
|
use crate::context::QuirksMode;
|
||||||
use crate::values::specified::length::au_to_int_px;
|
|
||||||
|
|
||||||
// The tables in this function are originally from
|
// The tables in this function are originally from
|
||||||
// nsRuleNode::CalcFontPointSize in Gecko:
|
// nsRuleNode::CalcFontPointSize in Gecko:
|
||||||
|
@ -850,22 +848,21 @@ impl ToComputedValue for KeywordSize {
|
||||||
Atom::with(gecko_font.mLanguage.mRawPtr, |atom| {
|
Atom::with(gecko_font.mLanguage.mRawPtr, |atom| {
|
||||||
cx.font_metrics_provider
|
cx.font_metrics_provider
|
||||||
.get_size(atom, gecko_font.mGenericID)
|
.get_size(atom, gecko_font.mGenericID)
|
||||||
.0
|
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
let base_size_px = au_to_int_px(base_size as f32);
|
let base_size_px = base_size.px().round() as i32;
|
||||||
let html_size = self.html_size() as usize;
|
let html_size = self.html_size() as usize;
|
||||||
if base_size_px >= 9 && base_size_px <= 16 {
|
NonNegative(if base_size_px >= 9 && base_size_px <= 16 {
|
||||||
let mapping = if cx.quirks_mode == QuirksMode::Quirks {
|
let mapping = if cx.quirks_mode == QuirksMode::Quirks {
|
||||||
QUIRKS_FONT_SIZE_MAPPING
|
QUIRKS_FONT_SIZE_MAPPING
|
||||||
} else {
|
} else {
|
||||||
FONT_SIZE_MAPPING
|
FONT_SIZE_MAPPING
|
||||||
};
|
};
|
||||||
Au::from_px(mapping[(base_size_px - 9) as usize][html_size]).into()
|
Length::new(mapping[(base_size_px - 9) as usize][html_size] as f32)
|
||||||
} else {
|
} else {
|
||||||
Au(FONT_SIZE_FACTORS[html_size] * base_size / 100).into()
|
base_size * FONT_SIZE_FACTORS[html_size] as f32 / 100.0
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -927,7 +924,7 @@ impl FontSize {
|
||||||
// If the parent font was keyword-derived, this is too.
|
// If the parent font was keyword-derived, this is too.
|
||||||
// Tack the % onto the factor
|
// Tack the % onto the factor
|
||||||
info = compose_keyword(pc.0);
|
info = compose_keyword(pc.0);
|
||||||
base_size.resolve(context).scale_by(pc.0).into()
|
base_size.resolve(context) * pc.0
|
||||||
},
|
},
|
||||||
FontSize::Length(LengthPercentage::Calc(ref calc)) => {
|
FontSize::Length(LengthPercentage::Calc(ref calc)) => {
|
||||||
let parent = context.style().get_parent_font().clone_font_size();
|
let parent = context.style().get_parent_font().clone_font_size();
|
||||||
|
@ -964,8 +961,7 @@ impl FontSize {
|
||||||
// others should reject negatives during parsing. But SMIL
|
// others should reject negatives during parsing. But SMIL
|
||||||
// allows parsing negatives, and relies on us _not_ doing that
|
// allows parsing negatives, and relies on us _not_ doing that
|
||||||
// clamping. That's so bonkers :(
|
// clamping. That's so bonkers :(
|
||||||
CSSPixelLength::from(calc.to_used_value(base_size.resolve(context)))
|
calc.percentage_relative_to(base_size.resolve(context)).clamp_to_non_negative()
|
||||||
.clamp_to_non_negative()
|
|
||||||
},
|
},
|
||||||
FontSize::Keyword(i) => {
|
FontSize::Keyword(i) => {
|
||||||
// As a specified keyword, this is keyword derived
|
// As a specified keyword, this is keyword derived
|
||||||
|
|
|
@ -5,8 +5,7 @@
|
||||||
//! Specified types for legacy Gecko-only properties.
|
//! Specified types for legacy Gecko-only properties.
|
||||||
|
|
||||||
use crate::parser::{Parse, ParserContext};
|
use crate::parser::{Parse, ParserContext};
|
||||||
use crate::values::computed::length::CSSPixelLength;
|
use crate::values::computed::{self, LengthPercentage, Length};
|
||||||
use crate::values::computed::{self, LengthPercentage};
|
|
||||||
use crate::values::generics::rect::Rect;
|
use crate::values::generics::rect::Rect;
|
||||||
use cssparser::{Parser, Token};
|
use cssparser::{Parser, Token};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
@ -24,7 +23,7 @@ fn parse_pixel_or_percent<'i, 't>(
|
||||||
value, ref unit, ..
|
value, ref unit, ..
|
||||||
} => {
|
} => {
|
||||||
match_ignore_ascii_case! { unit,
|
match_ignore_ascii_case! { unit,
|
||||||
"px" => Ok(LengthPercentage::new(CSSPixelLength::new(value), None)),
|
"px" => Ok(LengthPercentage::new(Length::new(value), None)),
|
||||||
_ => Err(()),
|
_ => Err(()),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -47,14 +47,6 @@ pub const AU_PER_PT: CSSFloat = AU_PER_IN / 72.;
|
||||||
/// Number of app units per pica
|
/// Number of app units per pica
|
||||||
pub const AU_PER_PC: CSSFloat = AU_PER_PT * 12.;
|
pub const AU_PER_PC: CSSFloat = AU_PER_PT * 12.;
|
||||||
|
|
||||||
/// Same as Gecko's AppUnitsToIntCSSPixels
|
|
||||||
///
|
|
||||||
/// Converts app units to integer pixel values,
|
|
||||||
/// rounding during the conversion
|
|
||||||
pub fn au_to_int_px(au: f32) -> i32 {
|
|
||||||
(au / AU_PER_PX).round() as i32
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A font relative length.
|
/// A font relative length.
|
||||||
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToCss, ToShmem)]
|
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToCss, ToShmem)]
|
||||||
pub enum FontRelativeLength {
|
pub enum FontRelativeLength {
|
||||||
|
@ -87,7 +79,7 @@ pub enum FontBaseSize {
|
||||||
|
|
||||||
impl FontBaseSize {
|
impl FontBaseSize {
|
||||||
/// Calculate the actual size for a given context
|
/// Calculate the actual size for a given context
|
||||||
pub fn resolve(&self, context: &Context) -> Au {
|
pub fn resolve(&self, context: &Context) -> computed::Length {
|
||||||
match *self {
|
match *self {
|
||||||
FontBaseSize::CurrentStyle => context.style().get_font().clone_font_size().size(),
|
FontBaseSize::CurrentStyle => context.style().get_font().clone_font_size().size(),
|
||||||
FontBaseSize::InheritedStyleButStripEmUnits | FontBaseSize::InheritedStyle => {
|
FontBaseSize::InheritedStyleButStripEmUnits | FontBaseSize::InheritedStyle => {
|
||||||
|
@ -109,13 +101,9 @@ impl FontRelativeLength {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes the font-relative length.
|
/// Computes the font-relative length.
|
||||||
pub fn to_computed_value(&self, context: &Context, base_size: FontBaseSize) -> CSSPixelLength {
|
pub fn to_computed_value(&self, context: &Context, base_size: FontBaseSize) -> computed::Length {
|
||||||
use std::f32;
|
|
||||||
let (reference_size, length) = self.reference_font_size_and_length(context, base_size);
|
let (reference_size, length) = self.reference_font_size_and_length(context, base_size);
|
||||||
let pixel = (length * reference_size.to_f32_px())
|
reference_size * length
|
||||||
.min(f32::MAX)
|
|
||||||
.max(f32::MIN);
|
|
||||||
CSSPixelLength::new(pixel)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return reference font size.
|
/// Return reference font size.
|
||||||
|
@ -129,7 +117,7 @@ impl FontRelativeLength {
|
||||||
&self,
|
&self,
|
||||||
context: &Context,
|
context: &Context,
|
||||||
base_size: FontBaseSize,
|
base_size: FontBaseSize,
|
||||||
) -> (Au, CSSFloat) {
|
) -> (computed::Length, CSSFloat) {
|
||||||
fn query_font_metrics(
|
fn query_font_metrics(
|
||||||
context: &Context,
|
context: &Context,
|
||||||
base_size: FontBaseSize,
|
base_size: FontBaseSize,
|
||||||
|
@ -153,7 +141,7 @@ impl FontRelativeLength {
|
||||||
}
|
}
|
||||||
|
|
||||||
if base_size == FontBaseSize::InheritedStyleButStripEmUnits {
|
if base_size == FontBaseSize::InheritedStyleButStripEmUnits {
|
||||||
(Au(0), length)
|
(Zero::zero(), length)
|
||||||
} else {
|
} else {
|
||||||
(reference_font_size, length)
|
(reference_font_size, length)
|
||||||
}
|
}
|
||||||
|
@ -175,7 +163,7 @@ impl FontRelativeLength {
|
||||||
// determine the x-height, a value of 0.5em must be
|
// determine the x-height, a value of 0.5em must be
|
||||||
// assumed.
|
// assumed.
|
||||||
//
|
//
|
||||||
reference_font_size.scale_by(0.5)
|
reference_font_size * 0.5
|
||||||
});
|
});
|
||||||
(reference_size, length)
|
(reference_size, length)
|
||||||
},
|
},
|
||||||
|
@ -210,7 +198,7 @@ impl FontRelativeLength {
|
||||||
if wm.is_vertical() && wm.is_upright() {
|
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 * 0.5
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
(reference_size, length)
|
(reference_size, length)
|
||||||
|
@ -225,7 +213,7 @@ impl FontRelativeLength {
|
||||||
let reference_size = if context.is_root_element || context.in_media_query {
|
let reference_size = if context.is_root_element || context.in_media_query {
|
||||||
reference_font_size
|
reference_font_size
|
||||||
} else {
|
} else {
|
||||||
context.device().root_font_size()
|
computed::Length::new(context.device().root_font_size().to_f32_px())
|
||||||
};
|
};
|
||||||
(reference_size, length)
|
(reference_size, length)
|
||||||
},
|
},
|
||||||
|
@ -290,15 +278,14 @@ pub struct CharacterWidth(pub i32);
|
||||||
|
|
||||||
impl CharacterWidth {
|
impl CharacterWidth {
|
||||||
/// Computes the given character width.
|
/// Computes the given character width.
|
||||||
pub fn to_computed_value(&self, reference_font_size: Au) -> CSSPixelLength {
|
pub fn to_computed_value(&self, reference_font_size: computed::Length) -> computed::Length {
|
||||||
// This applies the *converting a character width to pixels* algorithm as specified
|
// This applies the *converting a character width to pixels* algorithm
|
||||||
// in HTML5 § 14.5.4.
|
// as specified in HTML5 § 14.5.4.
|
||||||
//
|
//
|
||||||
// TODO(pcwalton): Find these from the font.
|
// TODO(pcwalton): Find these from the font.
|
||||||
let average_advance = reference_font_size.scale_by(0.5);
|
let average_advance = reference_font_size * 0.5;
|
||||||
let max_advance = reference_font_size;
|
let max_advance = reference_font_size;
|
||||||
let au = average_advance.scale_by(self.0 as CSSFloat - 1.0) + max_advance;
|
average_advance * (self.0 as CSSFloat - 1.0) + max_advance
|
||||||
au.into()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,6 @@ impl ToComputedValue for LineHeight {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
|
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
|
||||||
use crate::values::computed::Length as ComputedLength;
|
|
||||||
use crate::values::specified::length::FontBaseSize;
|
use crate::values::specified::length::FontBaseSize;
|
||||||
match *self {
|
match *self {
|
||||||
GenericLineHeight::Normal => GenericLineHeight::Normal,
|
GenericLineHeight::Normal => GenericLineHeight::Normal,
|
||||||
|
@ -97,16 +96,8 @@ impl ToComputedValue for LineHeight {
|
||||||
LengthPercentage::Calc(ref calc) => {
|
LengthPercentage::Calc(ref calc) => {
|
||||||
let computed_calc =
|
let computed_calc =
|
||||||
calc.to_computed_value_zoomed(context, FontBaseSize::CurrentStyle);
|
calc.to_computed_value_zoomed(context, FontBaseSize::CurrentStyle);
|
||||||
let font_relative_length =
|
let base = context.style().get_font().clone_font_size().size();
|
||||||
FontRelativeLength::Em(computed_calc.percentage())
|
computed_calc.percentage_relative_to(base)
|
||||||
.to_computed_value(context, FontBaseSize::CurrentStyle)
|
|
||||||
.px();
|
|
||||||
|
|
||||||
let absolute_length = computed_calc.unclamped_length().px();
|
|
||||||
let pixel = computed_calc
|
|
||||||
.clamping_mode
|
|
||||||
.clamp(absolute_length + font_relative_length);
|
|
||||||
ComputedLength::new(pixel)
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
GenericLineHeight::Length(result.into())
|
GenericLineHeight::Length(result.into())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue