From 6a98b777e0f31f534d328181147ca4e4303ccb58 Mon Sep 17 00:00:00 2001 From: Jonathan Kew Date: Fri, 7 Oct 2022 23:00:44 +0000 Subject: [PATCH] style: CSS support for the font-palette property This is just the CSS parsing, not yet connected to the rendering back-end. Differential Revision: https://phabricator.services.mozilla.com/D157957 --- .../style/properties/longhands/font.mako.rs | 12 ++++ .../style/properties/shorthands/font.mako.rs | 18 ++++-- components/style/values/computed/font.rs | 1 + components/style/values/computed/mod.rs | 2 +- components/style/values/specified/font.rs | 58 ++++++++++++++++++- components/style/values/specified/mod.rs | 2 +- 6 files changed, 83 insertions(+), 10 deletions(-) diff --git a/components/style/properties/longhands/font.mako.rs b/components/style/properties/longhands/font.mako.rs index 322a82e00a4..aea9a91d758 100644 --- a/components/style/properties/longhands/font.mako.rs +++ b/components/style/properties/longhands/font.mako.rs @@ -206,6 +206,18 @@ ${helpers.single_keyword_system( spec="https://www.w3.org/TR/css-fonts-4/#font-optical-sizing-def", )} +${helpers.predefined_type( + "font-palette", + "FontPalette", + engines="gecko", + initial_value="computed::FontPalette::normal()", + initial_specified_value="specified::FontPalette::normal()", + animation_value_type="discrete", + gecko_pref="layout.css.font-palette.enabled", + has_effect_on_gecko_scrollbars=False, + spec="https://drafts.csswg.org/css-fonts/#font-palette-prop", +)} + ${helpers.predefined_type( "-x-lang", "XLang", diff --git a/components/style/properties/shorthands/font.mako.rs b/components/style/properties/shorthands/font.mako.rs index 45c2c59e234..a58f5e57a14 100644 --- a/components/style/properties/shorthands/font.mako.rs +++ b/components/style/properties/shorthands/font.mako.rs @@ -27,6 +27,7 @@ ${'font-language-override' if engine == 'gecko' else ''} ${'font-feature-settings' if engine == 'gecko' else ''} ${'font-variation-settings' if engine == 'gecko' else ''} + ${'font-palette' if engine == 'gecko' else ''} " derive_value_info="False" spec="https://drafts.csswg.org/css-fonts-3/#propdef-font" @@ -37,7 +38,7 @@ use crate::properties::longhands::font_variant_caps; use crate::values::specified::text::LineHeight; use crate::values::specified::FontSize; - use crate::values::specified::font::{FontStretch, FontStretchKeyword}; + use crate::values::specified::font::{FontPalette, FontStretch, FontStretchKeyword}; #[cfg(feature = "gecko")] use crate::values::specified::font::SystemFont; @@ -46,7 +47,8 @@ variant_alternates variant_east_asian \ variant_ligatures variant_numeric \ variant_position feature_settings \ - variation_settings optical_sizing".split() + variation_settings optical_sizing \ + palette".split() %> % if engine == "gecko": % for prop in gecko_sub_properties: @@ -75,8 +77,9 @@ ${name}: ${name}::SpecifiedValue::system_font(sys), % endif % endfor - // line-height is just reset to initial + // line-height and palette are just reset to initial line_height: LineHeight::normal(), + font_palette: FontPalette::normal(), }) } % endif @@ -186,9 +189,14 @@ return Ok(()); } } + if let Some(v) = self.font_palette { + if v != &font_palette::get_initial_specified_value() { + return Ok(()); + } + } % for name in gecko_sub_properties: - % if name != "optical_sizing" and name != "variation_settings": + % if name != "optical_sizing" and name != "variation_settings" and name != "palette": if self.font_${name} != &font_${name}::get_initial_specified_value() { return Ok(()); } @@ -251,7 +259,7 @@ let mut all = true; % for prop in SYSTEM_FONT_LONGHANDS: - % if prop == "font_optical_sizing" or prop == "font_variation_settings": + % if prop == "font_optical_sizing" or prop == "font_variation_settings" or prop == "font_palette": if let Some(value) = self.${prop} { % else: { diff --git a/components/style/values/computed/font.rs b/components/style/values/computed/font.rs index ef06334ef25..009ac2427a5 100644 --- a/components/style/values/computed/font.rs +++ b/components/style/values/computed/font.rs @@ -24,6 +24,7 @@ use std::fmt::{self, Write}; use style_traits::{CssWriter, ParseError, ToCss}; pub use crate::values::computed::Length as MozScriptMinSize; +pub use crate::values::specified::font::FontPalette; pub use crate::values::specified::font::{FontSynthesis, MozScriptSizeMultiplier}; pub use crate::values::specified::font::{XLang, XTextZoom}; pub use crate::values::specified::Integer as SpecifiedInteger; diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index 729f535b79f..7a5d2a35838 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -63,7 +63,7 @@ pub use self::counters::{Content, ContentItem, CounterIncrement, CounterReset, C pub use self::easing::TimingFunction; pub use self::effects::{BoxShadow, Filter, SimpleShadow}; pub use self::flex::FlexBasis; -pub use self::font::{FontFamily, FontLanguageOverride, FontStyle}; +pub use self::font::{FontFamily, FontLanguageOverride, FontStyle, FontPalette}; pub use self::font::{FontFeatureSettings, FontVariantLigatures, FontVariantNumeric}; pub use self::font::{FontSize, FontSizeAdjust, FontStretch, FontSynthesis}; pub use self::font::{FontVariantAlternates, FontWeight}; diff --git a/components/style/values/specified/font.rs b/components/style/values/specified/font.rs index 4279f5148f3..4cf5b0807a9 100644 --- a/components/style/values/specified/font.rs +++ b/components/style/values/specified/font.rs @@ -20,7 +20,7 @@ use crate::values::generics::NonNegative; use crate::values::specified::length::{FontBaseSize, PX_PER_PT}; use crate::values::specified::{AllowQuirks, Angle, Integer, LengthPercentage}; use crate::values::specified::{NoCalcLength, NonNegativeNumber, NonNegativePercentage, Number}; -use crate::values::CustomIdent; +use crate::values::{CustomIdent, SelectorParseErrorKind, serialize_atom_identifier}; use crate::Atom; use cssparser::{Parser, Token}; #[cfg(feature = "gecko")] @@ -743,7 +743,7 @@ impl Parse for FontSizeAdjust { "ic-height" if basis_enabled => GenericFontSizeAdjust::IcHeight, // Unknown (or disabled) keyword. _ => return Err(location.new_custom_error( - ::selectors::parser::SelectorParseErrorKind::UnexpectedIdent(ident) + SelectorParseErrorKind::UnexpectedIdent(ident) )), }; let value = NonNegativeNumber::parse(context, input)?; @@ -1991,7 +1991,6 @@ impl Parse for FontSynthesis { _: &ParserContext, input: &mut Parser<'i, 't>, ) -> Result> { - use crate::values::SelectorParseErrorKind; let mut result = Self::none(); while let Ok(ident) = input.try_parse(|i| i.expect_ident_cloned()) { match_ignore_ascii_case! { &ident, @@ -2162,6 +2161,59 @@ impl Parse for FontLanguageOverride { } } +#[derive( + Clone, + Debug, + Eq, + MallocSizeOf, + PartialEq, + SpecifiedValueInfo, + ToComputedValue, + ToResolvedValue, + ToShmem, +)] +#[repr(C)] +/// Allows authors to choose a palette from those supported by a color font +/// (and potentially @font-palette-values overrides). +pub struct FontPalette(Atom); + +#[allow(missing_docs)] +impl FontPalette { + pub fn normal() -> Self { Self(atom!("normal")) } + pub fn light() -> Self { Self(atom!("light")) } + pub fn dark() -> Self { Self(atom!("dark")) } +} + +impl Parse for FontPalette { + /// normal | light | dark | dashed-ident + fn parse<'i, 't>( + _context: &ParserContext, + input: &mut Parser<'i, 't>, + ) -> Result> { + let location = input.current_source_location(); + let ident = input.expect_ident()?; + match_ignore_ascii_case! { &ident, + "normal" => Ok(Self::normal()), + "light" => Ok(Self::light()), + "dark" => Ok(Self::dark()), + _ => if ident.starts_with("--") { + Ok(Self(Atom::from(ident.as_ref()))) + } else { + Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(ident.clone()))) + }, + } + } +} + +impl ToCss for FontPalette { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { + serialize_atom_identifier(&self.0, dest) + } +} + /// This property provides low-level control over OpenType or TrueType font /// variations. pub type SpecifiedFontVariationSettings = FontSettings>; diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index e157f59d993..7fa2c57549a 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -51,7 +51,7 @@ pub use self::counters::{Content, ContentItem, CounterIncrement, CounterReset, C pub use self::easing::TimingFunction; pub use self::effects::{BoxShadow, Filter, SimpleShadow}; pub use self::flex::FlexBasis; -pub use self::font::{FontFamily, FontLanguageOverride, FontStyle}; +pub use self::font::{FontFamily, FontLanguageOverride, FontStyle, FontPalette}; pub use self::font::{FontFeatureSettings, FontVariantLigatures, FontVariantNumeric}; pub use self::font::{FontSize, FontSizeAdjust, FontSizeKeyword, FontStretch, FontSynthesis}; pub use self::font::{FontVariantAlternates, FontWeight};