/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ //! The [`@font-face`][ff] at-rule. //! //! [ff]: https://drafts.csswg.org/css-fonts/#at-font-face-rule use crate::derives::*; use crate::error_reporting::ContextualParseError; use crate::parser::{Parse, ParserContext}; use crate::shared_lock::{SharedRwLockReadGuard, ToCssWithGuard}; use crate::values::computed::FontWeight; use crate::values::computed::font::FontFamilyNameSyntax; use crate::values::generics::font::FontStyle as GenericFontStyle; use crate::values::specified::{Angle, url::SpecifiedUrl}; use cssparser::{Parser, RuleBodyParser, SourceLocation}; use std::fmt::{self, Write}; use style_traits::{CssStringWriter, CssWriter, ParseError, StyleParseErrorKind, ToCss}; pub use crate::properties::font_face::{DescriptorId, DescriptorParser, Descriptors}; pub use crate::values::computed::font::{FamilyName, FontStyle, FontWidth}; pub use crate::values::specified::font::{ AbsoluteFontWeight, FontFeatureSettings, FontLanguageOverride, FontVariationSettings, FontWidth as SpecifiedFontWidth, MetricsOverride, SpecifiedFontStyle, }; /// A source for a font-face rule. #[cfg_attr(feature = "servo", derive(Deserialize, Serialize))] #[derive(Clone, Debug, Eq, MallocSizeOf, PartialEq, ToCss, ToShmem)] pub enum Source { /// A `url()` source. Url(UrlSource), /// A `local()` source. #[css(function)] Local(FamilyName), } /// A list of sources for the font-face src descriptor. #[derive(Clone, Debug, Eq, MallocSizeOf, PartialEq, ToCss, ToShmem)] #[css(comma)] pub struct SourceList(#[css(iterable)] pub Vec); // We can't just use OneOrMoreSeparated to derive Parse for the Source list, // because we want to filter out components that parsed as None, then fail if no // valid components remain. So we provide our own implementation here. impl Parse for SourceList { fn parse(context: &ParserContext, input: &mut Parser) -> Result { // Parse the comma-separated list, then let filter_map discard any None items. let list = input .parse_comma_separated(|input| { let s = input.parse_entirely(|input| Source::parse(context, input)); while input.next().is_ok() {} Ok(s.ok()) })? .into_iter() .flatten() .collect::>(); if list.is_empty() { Err(ParseError::custom(StyleParseErrorKind::UnspecifiedError)) } else { Ok(SourceList(list)) } } } /// Keywords for the font-face src descriptor's format() function. /// ('None' and 'Unknown' are for internal use in gfx, not exposed to CSS.) #[derive( Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, Parse, PartialEq, Serialize, ToCss, ToShmem, )] #[repr(u8)] #[allow(missing_docs)] pub enum FontFaceSourceFormatKeyword { #[css(skip)] None, Collection, EmbeddedOpentype, Opentype, Svg, Truetype, Woff, Woff2, #[css(skip)] Unknown, } /// Flags for the @font-face tech() function, indicating font technologies /// required by the resource. #[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize, ToShmem)] #[repr(C)] pub struct FontFaceSourceTechFlags(u16); bitflags! { impl FontFaceSourceTechFlags: u16 { /// Font requires OpenType feature support. const FEATURES_OPENTYPE = 1 << 0; /// Font requires Apple Advanced Typography support. const FEATURES_AAT = 1 << 1; /// Font requires Graphite shaping support. const FEATURES_GRAPHITE = 1 << 2; /// Font requires COLRv0 rendering support (simple list of colored layers). const COLOR_COLRV0 = 1 << 3; /// Font requires COLRv1 rendering support (graph of paint operations). const COLOR_COLRV1 = 1 << 4; /// Font requires SVG glyph rendering support. const COLOR_SVG = 1 << 5; /// Font has bitmap glyphs in 'sbix' format. const COLOR_SBIX = 1 << 6; /// Font has bitmap glyphs in 'CBDT' format. const COLOR_CBDT = 1 << 7; /// Font requires OpenType Variations support. const VARIATIONS = 1 << 8; /// Font requires CPAL palette selection support. const PALETTES = 1 << 9; /// Font requires support for incremental downloading. const INCREMENTAL = 1 << 10; } } impl FontFaceSourceTechFlags { /// Parse a single font-technology keyword and return its flag. pub fn parse_one(input: &mut Parser) -> Result { Ok(try_match_ident_ignore_ascii_case! { input, "features-opentype" => Self::FEATURES_OPENTYPE, "features-aat" => Self::FEATURES_AAT, "features-graphite" => Self::FEATURES_GRAPHITE, "color-colrv0" => Self::COLOR_COLRV0, "color-colrv1" => Self::COLOR_COLRV1, "color-svg" => Self::COLOR_SVG, "color-sbix" => Self::COLOR_SBIX, "color-cbdt" => Self::COLOR_CBDT, "variations" => Self::VARIATIONS, "palettes" => Self::PALETTES, "incremental" => Self::INCREMENTAL, }) } } impl Parse for FontFaceSourceTechFlags { fn parse(_context: &ParserContext, input: &mut Parser) -> Result { // We don't actually care about the return value of parse_comma_separated, // because we insert the flags into result as we go. let mut result = Self::empty(); input.parse_comma_separated(|input| { let flag = Self::parse_one(input)?; result.insert(flag); Ok(()) })?; if !result.is_empty() { Ok(result) } else { Err(ParseError::custom(StyleParseErrorKind::UnspecifiedError)) } } } #[allow(unused_assignments)] impl ToCss for FontFaceSourceTechFlags { fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write, { let mut first = true; macro_rules! write_if_flag { ($s:expr => $f:ident) => { if self.contains(Self::$f) { if first { first = false; } else { dest.write_str(", ")?; } dest.write_str($s)?; } }; } write_if_flag!("features-opentype" => FEATURES_OPENTYPE); write_if_flag!("features-aat" => FEATURES_AAT); write_if_flag!("features-graphite" => FEATURES_GRAPHITE); write_if_flag!("color-colrv0" => COLOR_COLRV0); write_if_flag!("color-colrv1" => COLOR_COLRV1); write_if_flag!("color-svg" => COLOR_SVG); write_if_flag!("color-sbix" => COLOR_SBIX); write_if_flag!("color-cbdt" => COLOR_CBDT); write_if_flag!("variations" => VARIATIONS); write_if_flag!("palettes" => PALETTES); write_if_flag!("incremental" => INCREMENTAL); Ok(()) } } /// #[derive(Clone, Debug, ToShmem, PartialEq)] pub struct FontFaceRule { /// The descriptors of the @font-face rule. pub descriptors: Descriptors, /// The parser location of the rule. pub source_location: SourceLocation, } impl FontFaceRule { /// Returns an empty rule. pub fn empty(source_location: SourceLocation) -> Self { Self { descriptors: Default::default(), source_location, } } } /// 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")] #[derive(Clone, Copy, Debug, Eq, PartialEq)] #[repr(u8)] #[allow(missing_docs)] pub enum FontFaceSourceListComponent { Url(*const crate::url::CssUrl), Local(*mut crate::gecko_bindings::structs::nsAtom), FormatHintKeyword(FontFaceSourceFormatKeyword), FormatHintString { length: usize, utf8_bytes: *const u8, }, TechFlags(FontFaceSourceTechFlags), } #[derive(Clone, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize, ToCss, ToShmem)] #[repr(u8)] #[allow(missing_docs)] pub enum FontFaceSourceFormat { Keyword(FontFaceSourceFormatKeyword), String(String), } /// A `UrlSource` represents a font-face source that has been specified with a /// `url()` function. /// /// #[cfg_attr(feature = "servo", derive(Deserialize, Serialize))] #[derive(Clone, Debug, Eq, MallocSizeOf, PartialEq, ToShmem)] pub struct UrlSource { /// The specified url. pub url: SpecifiedUrl, /// The format hint specified with the `format()` function, if present. pub format_hint: Option, /// The font technology flags specified with the `tech()` function, if any. pub tech_flags: FontFaceSourceTechFlags, } impl ToCss for UrlSource { fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write, { self.url.to_css(dest)?; if let Some(hint) = &self.format_hint { dest.write_str(" format(")?; hint.to_css(dest)?; dest.write_char(')')?; } if !self.tech_flags.is_empty() { dest.write_str(" tech(")?; self.tech_flags.to_css(dest)?; dest.write_char(')')?; } Ok(()) } } /// A font-display value for a @font-face rule. /// The font-display descriptor determines how a font face is displayed based /// on whether and when it is downloaded and ready to use. #[allow(missing_docs)] #[derive( Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, Parse, PartialEq, Serialize, ToComputedValue, ToCss, ToShmem, )] #[repr(u8)] pub enum FontDisplay { Auto, Block, Swap, Fallback, Optional, } macro_rules! impl_range { ($range:ident, $component:ident) => { impl Parse for $range { fn parse(context: &ParserContext, input: &mut Parser) -> Result { let first = $component::parse(context, input)?; let second = input .try_parse(|input| $component::parse(context, input)) .unwrap_or_else(|_| first.clone()); Ok($range(first, second)) } } impl ToCss for $range { fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write, { self.0.to_css(dest)?; if self.0 != self.1 { dest.write_char(' ')?; 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, MallocSizeOf, PartialEq, ToShmem)] pub struct FontWeightRange(pub AbsoluteFontWeight, pub AbsoluteFontWeight); impl_range!(FontWeightRange, AbsoluteFontWeight); /// The computed representation of the above so Gecko and Servo can read them easily. /// /// This one is needed because cbindgen doesn't know how to generate /// specified::Number. #[repr(C)] #[allow(missing_docs)] #[derive(Clone, Debug, Deserialize, Hash, MallocSizeOf, PartialEq, Serialize)] pub struct ComputedFontWeightRange(pub FontWeight, pub FontWeight); #[inline] fn sort_range(a: T, b: T) -> (T, T) { if a > b { (b, a) } else { (a, b) } } impl FontWeightRange { /// Returns a computed font-weight range, or None if either bound is an unresolvable calc. pub fn compute(&self) -> Option { let (min, max) = sort_range(self.0.compute()?, self.1.compute()?); Some(ComputedFontWeightRange(min, max)) } } /// The font-width descriptor: /// /// https://drafts.csswg.org/css-fonts-4/#descdef-font-face-font-width #[derive(Clone, Debug, MallocSizeOf, PartialEq, ToShmem)] pub struct FontWidthRange(pub SpecifiedFontWidth, pub SpecifiedFontWidth); impl_range!(FontWidthRange, SpecifiedFontWidth); /// The computed representation of the above, so that Gecko and Servo can read them /// easily. #[repr(C)] #[allow(missing_docs)] #[derive(Clone, Debug, Deserialize, Hash, MallocSizeOf, PartialEq, Serialize)] pub struct ComputedFontWidthRange(pub FontWidth, pub FontWidth); impl FontWidthRange { /// Returns a computed font-width range, or None if any value contains a calc /// expression that cannot be resolved at parse time. pub fn compute(&self) -> Option { fn compute_width(s: &SpecifiedFontWidth) -> Option { match *s { SpecifiedFontWidth::Keyword(ref kw) => Some(kw.compute()), SpecifiedFontWidth::Width(ref p) => { Some(FontWidth::from_percentage(p.compute()?.0)) }, SpecifiedFontWidth::System(..) => unreachable!(), } } let (min, max) = sort_range(compute_width(&self.0)?, compute_width(&self.1)?); Some(ComputedFontWidthRange(min, max)) } } /// The font-style descriptor: /// /// https://drafts.csswg.org/css-fonts-4/#descdef-font-face-font-style #[derive(Clone, Debug, MallocSizeOf, PartialEq, ToShmem)] #[allow(missing_docs)] pub enum FontStyleRange { Italic, Oblique(Angle, Angle), } /// The computed representation of the above, with angles in degrees stored as /// signed 8.8 fixed-point values, so that Gecko and Servo can read them easily. #[repr(C)] #[allow(missing_docs)] #[derive(Clone, Debug, Deserialize, Hash, MallocSizeOf, PartialEq, Serialize)] pub struct ComputedFontStyleRange(pub FontStyle, pub FontStyle); impl Parse for FontStyleRange { fn parse(context: &ParserContext, input: &mut Parser) -> Result { // We parse 'normal' explicitly here to distinguish it from 'oblique 0deg', // because we must not accept a following angle. if input .try_parse(|i| i.expect_ident_matching("normal")) .is_ok() { return Ok(Self::Oblique(Angle::zero(), Angle::zero())); } let style = SpecifiedFontStyle::parse(context, input)?; Ok(match style { GenericFontStyle::Italic => Self::Italic, GenericFontStyle::Oblique(angle) => { let second_angle = input .try_parse(|input| SpecifiedFontStyle::parse_angle(context, input)) .unwrap_or_else(|_| angle.clone()); Self::Oblique(angle, second_angle) }, }) } } impl ToCss for FontStyleRange { fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write, { match *self { Self::Italic => dest.write_str("italic"), Self::Oblique(ref first, ref second) => { // Not first.is_zero() because we don't want to serialize // `oblique calc(0deg)` as `normal`. if *first == Angle::zero() && first == second { return dest.write_str("normal"); } dest.write_str("oblique")?; if *first != SpecifiedFontStyle::default_angle() || first != second { dest.write_char(' ')?; first.to_css(dest)?; } if first != second { dest.write_char(' ')?; second.to_css(dest)?; } Ok(()) }, } } } impl FontStyleRange { /// Returns a computed font-style descriptor. pub fn compute(&self) -> Option { Some(match *self { Self::Italic => ComputedFontStyleRange(FontStyle::ITALIC, FontStyle::ITALIC), Self::Oblique(ref first, ref second) => { let (min, max) = sort_range(first.degrees()?, second.degrees()?); ComputedFontStyleRange(FontStyle::oblique(min), FontStyle::oblique(max)) }, }) } } /// Parse the block inside a `@font-face` rule. /// /// Note that the prelude parsing code lives in the `stylesheets` module. pub fn parse_font_face_block( context: &ParserContext, input: &mut Parser, source_location: SourceLocation, ) -> FontFaceRule { let mut rule = FontFaceRule::empty(source_location); { let mut parser = DescriptorParser { context, descriptors: &mut rule.descriptors, }; let iter = RuleBodyParser::new(input, &mut parser); for declaration in iter { if let Err((error, slice, location)) = declaration { let error = ContextualParseError::UnsupportedFontFaceDescriptor(slice, error); context.log_css_error(location, error) } } } rule } impl Parse for Source { fn parse(context: &ParserContext, input: &mut Parser) -> Result { if input .try_parse(|input| input.expect_function_matching("local")) .is_ok() { let mut family_name = input.parse_nested_block(|input| FamilyName::parse(context, input))?; // Force src:local() names to always serialize as quoted strings. family_name.syntax = FontFamilyNameSyntax::Quoted; return Ok(Source::Local(family_name)); } let url = SpecifiedUrl::parse(context, input)?; // Parsing optional format() let format_hint = if input .try_parse(|input| input.expect_function_matching("format")) .is_ok() { input.parse_nested_block(|input| { if let Ok(kw) = input.try_parse(FontFaceSourceFormatKeyword::parse) { Ok(Some(FontFaceSourceFormat::Keyword(kw))) } else { let s = input.expect_string()?.as_ref().to_owned(); Ok(Some(FontFaceSourceFormat::String(s))) } })? } else { None }; // Parse optional tech() let tech_flags = if crate::pref!("layout.css.font-tech.enabled", gecko = true) && input .try_parse(|input| input.expect_function_matching("tech")) .is_ok() { input.parse_nested_block(|input| FontFaceSourceTechFlags::parse(context, input))? } else { FontFaceSourceTechFlags::empty() }; Ok(Source::Url(UrlSource { url, format_hint, tech_flags, })) } } impl ToCssWithGuard for FontFaceRule { // Serialization of FontFaceRule is not specced. fn to_css(&self, _guard: &SharedRwLockReadGuard, dest: &mut CssStringWriter) -> fmt::Result { dest.write_str("@font-face { ")?; self.descriptors.to_css(&mut CssWriter::new(dest))?; dest.write_char('}') } }