Move root_font_size to the device

This commit is contained in:
Manish Goregaokar 2017-05-25 11:56:47 -07:00
parent af124f2d89
commit ce2237e123
5 changed files with 53 additions and 24 deletions

View file

@ -17,7 +17,9 @@ use gecko_bindings::structs::RawGeckoPresContextOwned;
use media_queries::MediaType; use media_queries::MediaType;
use parser::ParserContext; use parser::ParserContext;
use properties::{ComputedValues, StyleBuilder}; use properties::{ComputedValues, StyleBuilder};
use properties::longhands::font_size;
use std::fmt::{self, Write}; use std::fmt::{self, Write};
use std::sync::atomic::{AtomicIsize, Ordering};
use str::starts_with_ignore_ascii_case; use str::starts_with_ignore_ascii_case;
use string_cache::Atom; use string_cache::Atom;
use style_traits::ToCss; use style_traits::ToCss;
@ -35,8 +37,20 @@ pub struct Device {
pub pres_context: RawGeckoPresContextOwned, pub pres_context: RawGeckoPresContextOwned,
default_values: Arc<ComputedValues>, default_values: Arc<ComputedValues>,
viewport_override: Option<ViewportConstraints>, viewport_override: Option<ViewportConstraints>,
/// The font size of the root element
/// This is set when computing the style of the root
/// element, and used for rem units in other elements.
///
/// When computing the style of the root element, there can't be any
/// other style being computed at the same time, given we need the style of
/// the parent to compute everything else. So it is correct to just use
/// a relaxed atomic here.
root_font_size: AtomicIsize,
} }
unsafe impl Sync for Device {}
unsafe impl Send for Device {}
impl Device { impl Device {
/// Trivially constructs a new `Device`. /// Trivially constructs a new `Device`.
pub fn new(pres_context: RawGeckoPresContextOwned) -> Self { pub fn new(pres_context: RawGeckoPresContextOwned) -> Self {
@ -45,6 +59,7 @@ impl Device {
pres_context: pres_context, pres_context: pres_context,
default_values: ComputedValues::default_values(unsafe { &*pres_context }), default_values: ComputedValues::default_values(unsafe { &*pres_context }),
viewport_override: None, viewport_override: None,
root_font_size: AtomicIsize::new(font_size::get_initial_value().0 as isize), // FIXME(bz): Seems dubious?
} }
} }
@ -73,6 +88,16 @@ impl Device {
&self.default_values &self.default_values
} }
/// Get the font size of the root element (for rem)
pub fn root_font_size(&self) -> Au {
Au::new(self.root_font_size.load(Ordering::Relaxed) as i32)
}
/// Set the font size of the root element (for rem)
pub fn set_root_font_size(&self, size: Au) {
self.root_font_size.store(size.0 as isize, Ordering::Relaxed)
}
/// Recreates all the temporary state that the `Device` stores. /// Recreates all the temporary state that the `Device` stores.
/// ///
/// This includes the viewport override from `@viewport` rules, and also the /// This includes the viewport override from `@viewport` rules, and also the
@ -111,9 +136,6 @@ impl Device {
} }
} }
unsafe impl Sync for Device {}
unsafe impl Send for Device {}
/// A expression for gecko contains a reference to the media feature, the value /// A expression for gecko contains a reference to the media feature, the value
/// the media query contained, and the range to evaluate. /// the media query contained, and the range to evaluate.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View file

@ -84,7 +84,6 @@ pub struct ComputedValues {
custom_properties: Option<Arc<ComputedValuesMap>>, custom_properties: Option<Arc<ComputedValuesMap>>,
pub writing_mode: WritingMode, pub writing_mode: WritingMode,
pub root_font_size: Au,
pub font_computation_data: FontComputationData, pub font_computation_data: FontComputationData,
/// The element's computed values if visited, only computed if there's a /// The element's computed values if visited, only computed if there's a
@ -96,7 +95,6 @@ pub struct ComputedValues {
impl ComputedValues { impl ComputedValues {
pub fn new(custom_properties: Option<Arc<ComputedValuesMap>>, pub fn new(custom_properties: Option<Arc<ComputedValuesMap>>,
writing_mode: WritingMode, writing_mode: WritingMode,
root_font_size: Au,
font_size_keyword: Option<(longhands::font_size::KeywordSize, f32)>, font_size_keyword: Option<(longhands::font_size::KeywordSize, f32)>,
visited_style: Option<Arc<ComputedValues>>, visited_style: Option<Arc<ComputedValues>>,
% for style_struct in data.style_structs: % for style_struct in data.style_structs:
@ -106,7 +104,6 @@ impl ComputedValues {
ComputedValues { ComputedValues {
custom_properties: custom_properties, custom_properties: custom_properties,
writing_mode: writing_mode, writing_mode: writing_mode,
root_font_size: root_font_size,
font_computation_data: FontComputationData::new(font_size_keyword), font_computation_data: FontComputationData::new(font_size_keyword),
visited_style: visited_style, visited_style: visited_style,
% for style_struct in data.style_structs: % for style_struct in data.style_structs:
@ -119,7 +116,6 @@ impl ComputedValues {
Arc::new(ComputedValues { Arc::new(ComputedValues {
custom_properties: None, custom_properties: None,
writing_mode: WritingMode::empty(), // FIXME(bz): This seems dubious writing_mode: WritingMode::empty(), // FIXME(bz): This seems dubious
root_font_size: longhands::font_size::get_initial_value(), // FIXME(bz): Also seems dubious?
font_computation_data: FontComputationData::default_values(), font_computation_data: FontComputationData::default_values(),
visited_style: None, visited_style: None,
% for style_struct in data.style_structs: % for style_struct in data.style_structs:

View file

@ -1828,8 +1828,6 @@ pub struct ComputedValues {
custom_properties: Option<Arc<::custom_properties::ComputedValuesMap>>, custom_properties: Option<Arc<::custom_properties::ComputedValuesMap>>,
/// The writing mode of this computed values struct. /// The writing mode of this computed values struct.
pub writing_mode: WritingMode, pub writing_mode: WritingMode,
/// The root element's computed font size.
pub root_font_size: Au,
/// The keyword behind the current font-size property, if any /// The keyword behind the current font-size property, if any
pub font_computation_data: FontComputationData, pub font_computation_data: FontComputationData,
@ -1844,7 +1842,6 @@ impl ComputedValues {
/// Construct a `ComputedValues` instance. /// Construct a `ComputedValues` instance.
pub fn new(custom_properties: Option<Arc<::custom_properties::ComputedValuesMap>>, pub fn new(custom_properties: Option<Arc<::custom_properties::ComputedValuesMap>>,
writing_mode: WritingMode, writing_mode: WritingMode,
root_font_size: Au,
font_size_keyword: Option<(longhands::font_size::KeywordSize, f32)>, font_size_keyword: Option<(longhands::font_size::KeywordSize, f32)>,
visited_style: Option<Arc<ComputedValues>>, visited_style: Option<Arc<ComputedValues>>,
% for style_struct in data.active_style_structs(): % for style_struct in data.active_style_structs():
@ -1854,7 +1851,6 @@ impl ComputedValues {
ComputedValues { ComputedValues {
custom_properties: custom_properties, custom_properties: custom_properties,
writing_mode: writing_mode, writing_mode: writing_mode,
root_font_size: root_font_size,
font_computation_data: FontComputationData::new(font_size_keyword), font_computation_data: FontComputationData::new(font_size_keyword),
visited_style: visited_style, visited_style: visited_style,
% for style_struct in data.active_style_structs(): % for style_struct in data.active_style_structs():
@ -2292,8 +2288,6 @@ pub struct StyleBuilder<'a> {
/// ///
/// TODO(emilio): Make private. /// TODO(emilio): Make private.
pub writing_mode: WritingMode, pub writing_mode: WritingMode,
/// The font size of the root element.
pub root_font_size: Au,
/// The keyword behind the current font-size property, if any. /// The keyword behind the current font-size property, if any.
pub font_size_keyword: Option<(longhands::font_size::KeywordSize, f32)>, pub font_size_keyword: Option<(longhands::font_size::KeywordSize, f32)>,
/// The element's style if visited, only computed if there's a relevant link /// The element's style if visited, only computed if there's a relevant link
@ -2310,7 +2304,6 @@ impl<'a> StyleBuilder<'a> {
pub fn new( pub fn new(
custom_properties: Option<Arc<::custom_properties::ComputedValuesMap>>, custom_properties: Option<Arc<::custom_properties::ComputedValuesMap>>,
writing_mode: WritingMode, writing_mode: WritingMode,
root_font_size: Au,
font_size_keyword: Option<(longhands::font_size::KeywordSize, f32)>, font_size_keyword: Option<(longhands::font_size::KeywordSize, f32)>,
visited_style: Option<Arc<ComputedValues>>, visited_style: Option<Arc<ComputedValues>>,
% for style_struct in data.active_style_structs(): % for style_struct in data.active_style_structs():
@ -2320,7 +2313,6 @@ impl<'a> StyleBuilder<'a> {
StyleBuilder { StyleBuilder {
custom_properties: custom_properties, custom_properties: custom_properties,
writing_mode: writing_mode, writing_mode: writing_mode,
root_font_size: root_font_size,
font_size_keyword: font_size_keyword, font_size_keyword: font_size_keyword,
visited_style: visited_style, visited_style: visited_style,
% for style_struct in data.active_style_structs(): % for style_struct in data.active_style_structs():
@ -2340,7 +2332,6 @@ impl<'a> StyleBuilder<'a> {
pub fn for_inheritance(parent: &'a ComputedValues, default: &'a ComputedValues) -> Self { pub fn for_inheritance(parent: &'a ComputedValues, default: &'a ComputedValues) -> Self {
Self::new(parent.custom_properties(), Self::new(parent.custom_properties(),
parent.writing_mode, parent.writing_mode,
parent.root_font_size,
parent.font_computation_data.font_size_keyword, parent.font_computation_data.font_size_keyword,
parent.clone_visited_style(), parent.clone_visited_style(),
% for style_struct in data.active_style_structs(): % for style_struct in data.active_style_structs():
@ -2414,7 +2405,6 @@ impl<'a> StyleBuilder<'a> {
pub fn build(self) -> ComputedValues { pub fn build(self) -> ComputedValues {
ComputedValues::new(self.custom_properties, ComputedValues::new(self.custom_properties,
self.writing_mode, self.writing_mode,
self.root_font_size,
self.font_size_keyword, self.font_size_keyword,
self.visited_style, self.visited_style,
% for style_struct in data.active_style_structs(): % for style_struct in data.active_style_structs():
@ -2458,8 +2448,7 @@ mod lazy_static_module {
% endfor % endfor
custom_properties: None, custom_properties: None,
writing_mode: WritingMode::empty(), writing_mode: WritingMode::empty(),
root_font_size: longhands::font_size::get_initial_value(), font_computation_data: FontComputationData::default_values(),
font_computation_data: FontComputationData::default_values()
visited_style: None, visited_style: None,
}; };
} }
@ -2608,7 +2597,6 @@ pub fn apply_declarations<'a, F, I>(device: &Device,
let builder = if !flags.contains(INHERIT_ALL) { let builder = if !flags.contains(INHERIT_ALL) {
StyleBuilder::new(custom_properties, StyleBuilder::new(custom_properties,
WritingMode::empty(), WritingMode::empty(),
inherited_style.root_font_size,
inherited_style.font_computation_data.font_size_keyword, inherited_style.font_computation_data.font_size_keyword,
visited_style, visited_style,
% for style_struct in data.active_style_structs(): % for style_struct in data.active_style_structs():
@ -2622,7 +2610,6 @@ pub fn apply_declarations<'a, F, I>(device: &Device,
} else { } else {
StyleBuilder::new(custom_properties, StyleBuilder::new(custom_properties,
WritingMode::empty(), WritingMode::empty(),
inherited_style.root_font_size,
inherited_style.font_computation_data.font_size_keyword, inherited_style.font_computation_data.font_size_keyword,
visited_style, visited_style,
% for style_struct in data.active_style_structs(): % for style_struct in data.active_style_structs():
@ -2788,7 +2775,7 @@ pub fn apply_declarations<'a, F, I>(device: &Device,
if is_root_element { if is_root_element {
let s = context.style.get_font().clone_font_size(); let s = context.style.get_font().clone_font_size();
context.style.root_font_size = s; context.device.set_root_font_size(s);
} }
% endif % endif
% endfor % endfor

View file

@ -12,7 +12,9 @@ use font_metrics::ServoMetricsProvider;
use media_queries::MediaType; use media_queries::MediaType;
use parser::ParserContext; use parser::ParserContext;
use properties::{ComputedValues, StyleBuilder}; use properties::{ComputedValues, StyleBuilder};
use properties::longhands::font_size;
use std::fmt; use std::fmt;
use std::sync::atomic::{AtomicIsize, Ordering};
use style_traits::{CSSPixel, ToCss}; use style_traits::{CSSPixel, ToCss};
use style_traits::viewport::ViewportConstraints; use style_traits::viewport::ViewportConstraints;
use values::computed::{self, ToComputedValue}; use values::computed::{self, ToComputedValue};
@ -22,12 +24,23 @@ use values::specified;
/// is displayed in. /// is displayed in.
/// ///
/// This is the struct against which media queries are evaluated. /// This is the struct against which media queries are evaluated.
#[derive(Debug, HeapSizeOf)] #[derive(HeapSizeOf)]
pub struct Device { pub struct Device {
/// The current media type used by de device. /// The current media type used by de device.
media_type: MediaType, media_type: MediaType,
/// The current viewport size, in CSS pixels. /// The current viewport size, in CSS pixels.
viewport_size: TypedSize2D<f32, CSSPixel>, viewport_size: TypedSize2D<f32, CSSPixel>,
/// The font size of the root element
/// This is set when computing the style of the root
/// element, and used for rem units in other elements
///
/// When computing the style of the root element, there can't be any
/// other style being computed at the same time, given we need the style of
/// the parent to compute everything else. So it is correct to just use
/// a relaxed atomic here.
#[ignore_heap_size_of = "Pure stack type"]
root_font_size: AtomicIsize,
} }
impl Device { impl Device {
@ -38,6 +51,7 @@ impl Device {
Device { Device {
media_type: media_type, media_type: media_type,
viewport_size: viewport_size, viewport_size: viewport_size,
root_font_size: AtomicIsize::new(font_size::get_initial_value().0 as isize), // FIXME(bz): Seems dubious?
} }
} }
@ -49,6 +63,16 @@ impl Device {
ComputedValues::initial_values() ComputedValues::initial_values()
} }
/// Get the font size of the root element (for rem)
pub fn root_font_size(&self) -> Au {
Au::new(self.root_font_size.load(Ordering::Relaxed) as i32)
}
/// Set the font size of the root element (for rem)
pub fn set_root_font_size(&self, size: Au) {
self.root_font_size.store(size.0 as isize, Ordering::Relaxed)
}
/// Returns the viewport size of the current device in app units, needed, /// Returns the viewport size of the current device in app units, needed,
/// among other things, to resolve viewport units. /// among other things, to resolve viewport units.
#[inline] #[inline]

View file

@ -112,7 +112,7 @@ impl FontRelativeLength {
let reference_font_size = base_size.resolve(context); let reference_font_size = base_size.resolve(context);
let root_font_size = context.style().root_font_size; let root_font_size = context.device.root_font_size();
match *self { match *self {
FontRelativeLength::Em(length) => reference_font_size.scale_by(length), FontRelativeLength::Em(length) => reference_font_size.scale_by(length),
FontRelativeLength::Ex(length) => { FontRelativeLength::Ex(length) => {