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
This commit is contained in:
Jonathan Kew 2022-10-07 23:00:44 +00:00 committed by Martin Robinson
parent 38caec4f89
commit 6a98b777e0
6 changed files with 83 additions and 10 deletions

View file

@ -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",

View file

@ -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:
{

View file

@ -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;

View file

@ -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};

View file

@ -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<FontSynthesis, ParseError<'i>> {
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<FontPalette, ParseError<'i>> {
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<W>(&self, dest: &mut CssWriter<W>) -> 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<VariationValue<Number>>;

View file

@ -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};