Skip to content

Commit

Permalink
style: Remove nsCSSValue usage from font code.
Browse files Browse the repository at this point in the history
Really sorry for the size of the patch.

Differential Revision: https://phabricator.services.mozilla.com/D7753
  • Loading branch information
emilio committed Oct 9, 2018
1 parent 7345af6 commit d833754
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 219 deletions.
25 changes: 23 additions & 2 deletions components/style/cbindgen.toml
Expand Up @@ -6,7 +6,17 @@ autogen_warning = """/* DO NOT MODIFY THIS MANUALLY! This file was generated usi
* 1. Get the latest cbindgen using `cargo install --force cbindgen`
* a. Alternatively, you can clone `https://github.com/eqrion/cbindgen` and use a tagged release
* 2. Run `rustup run nightly cbindgen toolkit/library/rust/ --lockfile Cargo.lock --crate style -o layout/style/ServoStyleConsts.h`
*/"""
*/
class nsAtom;
namespace mozilla {
namespace css {
struct URLValue;
}
// Work-around weird cbindgen renaming.
typedef css::URLValue StyleURLValue;
typedef nsAtom StylensAtom;
}
"""
include_guard = "mozilla_ServoStyleConsts_h"
include_version = true
braces = "SameLine"
Expand All @@ -15,6 +25,10 @@ tab_width = 2
language = "C++"
namespaces = ["mozilla"]

[parse]
parse_deps = true
include = ["cssparser"]

[struct]
derive_eq = true

Expand All @@ -25,9 +39,16 @@ derive_helper_methods = true
prefix = "Style"
include = [
"StyleAppearance",
"StyleComputedFontStretchRange",
"StyleComputedFontStyleDescriptor",
"StyleComputedFontWeightRange",
"StyleDisplay",
"StyleDisplayMode",
"StyleFillRule",
"StylePathCommand"
"StyleFontDisplay",
"StyleFontFaceSourceListComponent",
"StyleFontLanguageOverride",
"StylePathCommand",
"StyleUnicodeRange",
]
item_types = ["enums", "structs", "typedefs"]
136 changes: 109 additions & 27 deletions components/style/font_face.rs
Expand Up @@ -24,7 +24,7 @@ use style_traits::values::SequenceWriter;
use values::computed::font::FamilyName;
use values::generics::font::FontStyle as GenericFontStyle;
use values::specified::Angle;
use values::specified::font::{AbsoluteFontWeight, FontStretch as SpecifiedFontStretch};
use values::specified::font::{AbsoluteFontWeight, FontStretch};
#[cfg(feature = "gecko")]
use values::specified::font::{SpecifiedFontFeatureSettings, SpecifiedFontVariationSettings};
use values::specified::font::SpecifiedFontStyle;
Expand All @@ -45,6 +45,19 @@ impl OneOrMoreSeparated for Source {
type S = Comma;
}

/// A POD representation for Gecko. All pointers here are non-owned and as such
/// can't outlive the rule they came from, but we can't enforce that via C++.
///
/// All the strings are of course utf8.
#[cfg(feature = "gecko")]
#[repr(u8)]
#[allow(missing_docs)]
pub enum FontFaceSourceListComponent {
Url(*const ::gecko_bindings::structs::mozilla::css::URLValue),
Local(*mut ::gecko_bindings::structs::nsAtom),
FormatHint { length: usize, utf8_bytes: *const u8 },
}

/// A `UrlSource` represents a font-face source that has been specified with a
/// `url()` function.
///
Expand Down Expand Up @@ -84,6 +97,7 @@ impl ToCss for UrlSource {
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, ToComputedValue, ToCss)]
#[repr(u8)]
pub enum FontDisplay {
Auto,
Block,
Expand All @@ -92,41 +106,83 @@ pub enum FontDisplay {
Optional,
}

macro_rules! impl_range {
($range:ident, $component:ident) => {
impl Parse for $range {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let first = $component::parse(context, input)?;
let second = input
.try(|input| $component::parse(context, input))
.unwrap_or_else(|_| first.clone());
Ok($range(first, second))
}
}
impl ToCss for $range {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: fmt::Write,
{
self.0.to_css(dest)?;
if self.0 != self.1 {
dest.write_str(" ")?;
self.1.to_css(dest)?;
}
Ok(())
}
}
}
}

/// The font-weight descriptor:
///
/// https://drafts.csswg.org/css-fonts-4/#descdef-font-face-font-weight
#[derive(Clone, Debug, PartialEq, ToCss)]
pub struct FontWeight(pub AbsoluteFontWeight, pub Option<AbsoluteFontWeight>);
#[derive(Clone, Debug, PartialEq)]
pub struct FontWeightRange(pub AbsoluteFontWeight, pub AbsoluteFontWeight);
impl_range!(FontWeightRange, AbsoluteFontWeight);

impl Parse for FontWeight {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let first = AbsoluteFontWeight::parse(context, input)?;
let second = input
.try(|input| AbsoluteFontWeight::parse(context, input))
.ok();
Ok(FontWeight(first, second))
/// The computed representation of the above so Gecko can read them easily.
///
/// This one is needed because cbindgen doesn't know how to generate
/// specified::Number.
#[repr(C)]
#[allow(missing_docs)]
pub struct ComputedFontWeightRange(f32, f32);

impl FontWeightRange {
/// Returns a computed font-stretch range.
pub fn compute(&self) -> ComputedFontWeightRange {
ComputedFontWeightRange(self.0.compute().0, self.1.compute().0)
}
}

/// The font-stretch descriptor:
///
/// https://drafts.csswg.org/css-fonts-4/#descdef-font-face-font-stretch
#[derive(Clone, Debug, PartialEq, ToCss)]
pub struct FontStretch(pub SpecifiedFontStretch, pub Option<SpecifiedFontStretch>);
#[derive(Clone, Debug, PartialEq,)]
pub struct FontStretchRange(pub FontStretch, pub FontStretch);
impl_range!(FontStretchRange, FontStretch);

impl Parse for FontStretch {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let first = SpecifiedFontStretch::parse(context, input)?;
let second = input
.try(|input| SpecifiedFontStretch::parse(context, input))
.ok();
Ok(FontStretch(first, second))
/// The computed representation of the above, so that
/// Gecko can read them easily.
#[repr(C)]
#[allow(missing_docs)]
pub struct ComputedFontStretchRange(f32, f32);

impl FontStretchRange {
/// Returns a computed font-stretch range.
pub fn compute(&self) -> ComputedFontStretchRange {
fn compute_stretch(s: &FontStretch) -> f32 {
match *s {
FontStretch::Keyword(ref kw) => kw.compute().0,
FontStretch::Stretch(ref p) => p.get(),
FontStretch::System(..) => unreachable!(),
}
}

ComputedFontStretchRange(compute_stretch(&self.0), compute_stretch(&self.1))
}
}

Expand All @@ -141,6 +197,16 @@ pub enum FontStyle {
Oblique(Angle, Angle),
}

/// The computed representation of the above, with angles in degrees, so that
/// Gecko can read them easily.
#[repr(u8)]
#[allow(missing_docs)]
pub enum ComputedFontStyleDescriptor {
Normal,
Italic,
Oblique(f32, f32),
}

impl Parse for FontStyle {
fn parse<'i, 't>(
context: &ParserContext,
Expand Down Expand Up @@ -185,6 +251,22 @@ impl ToCss for FontStyle {
}
}

impl FontStyle {
/// Returns a computed font-style descriptor.
pub fn compute(&self) -> ComputedFontStyleDescriptor {
match *self {
FontStyle::Normal => ComputedFontStyleDescriptor::Normal,
FontStyle::Italic => ComputedFontStyleDescriptor::Italic,
FontStyle::Oblique(ref first, ref second) => {
ComputedFontStyleDescriptor::Oblique(
SpecifiedFontStyle::compute_angle_degrees(first),
SpecifiedFontStyle::compute_angle_degrees(second),
)
}
}
}
}

/// Parse the block inside a `@font-face` rule.
///
/// Note that the prelude parsing code lives in the `stylesheets` module.
Expand Down Expand Up @@ -459,10 +541,10 @@ font_face_descriptors! {
"font-style" style / mStyle: FontStyle,

/// The weight of this font face.
"font-weight" weight / mWeight: FontWeight,
"font-weight" weight / mWeight: FontWeightRange,

/// The stretch of this font face.
"font-stretch" stretch / mStretch: FontStretch,
"font-stretch" stretch / mStretch: FontStretchRange,

/// The display of this font face.
"font-display" display / mDisplay: FontDisplay,
Expand Down

0 comments on commit d833754

Please sign in to comment.