use std::collections::HashSet;
use super::{Property, PropertyId};
use crate::compat::Feature;
use crate::context::PropertyHandlerContext;
use crate::declaration::{DeclarationBlock, DeclarationList};
use crate::error::{ParserError, PrinterError};
use crate::macros::*;
use crate::printer::Printer;
use crate::targets::should_compile;
use crate::traits::{IsCompatible, Parse, PropertyHandler, Shorthand, ToCss};
use crate::values::length::LengthValue;
use crate::values::number::CSSNumber;
use crate::values::string::CowArcStr;
use crate::values::{angle::Angle, length::LengthPercentage, percentage::Percentage};
#[cfg(feature = "visitor")]
use crate::visitor::Visit;
use cssparser::*;
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(tag = "type", content = "value", rename_all = "kebab-case")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub enum FontWeight {
Absolute(AbsoluteFontWeight),
Bolder,
Lighter,
}
impl Default for FontWeight {
fn default() -> FontWeight {
FontWeight::Absolute(AbsoluteFontWeight::default())
}
}
impl<'i> Parse<'i> for FontWeight {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
if let Ok(val) = input.try_parse(AbsoluteFontWeight::parse) {
return Ok(FontWeight::Absolute(val));
}
let location = input.current_source_location();
let ident = input.expect_ident()?;
match_ignore_ascii_case! { &*ident,
"bolder" => Ok(FontWeight::Bolder),
"lighter" => Ok(FontWeight::Lighter),
_ => Err(location.new_unexpected_token_error(
cssparser::Token::Ident(ident.clone())
))
}
}
}
impl ToCss for FontWeight {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
use FontWeight::*;
match self {
Absolute(val) => val.to_css(dest),
Bolder => dest.write_str("bolder"),
Lighter => dest.write_str("lighter"),
}
}
}
impl IsCompatible for FontWeight {
fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
match self {
FontWeight::Absolute(a) => a.is_compatible(browsers),
FontWeight::Bolder | FontWeight::Lighter => true,
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(tag = "type", content = "value", rename_all = "kebab-case")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub enum AbsoluteFontWeight {
Weight(CSSNumber),
Normal,
Bold,
}
impl Default for AbsoluteFontWeight {
fn default() -> AbsoluteFontWeight {
AbsoluteFontWeight::Normal
}
}
impl<'i> Parse<'i> for AbsoluteFontWeight {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
if let Ok(val) = input.try_parse(CSSNumber::parse) {
return Ok(AbsoluteFontWeight::Weight(val));
}
let location = input.current_source_location();
let ident = input.expect_ident()?;
match_ignore_ascii_case! { &*ident,
"normal" => Ok(AbsoluteFontWeight::Normal),
"bold" => Ok(AbsoluteFontWeight::Bold),
_ => Err(location.new_unexpected_token_error(
cssparser::Token::Ident(ident.clone())
))
}
}
}
impl ToCss for AbsoluteFontWeight {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
use AbsoluteFontWeight::*;
match self {
Weight(val) => val.to_css(dest),
Normal => dest.write_str(if dest.minify { "400" } else { "normal" }),
Bold => dest.write_str(if dest.minify { "700" } else { "bold" }),
}
}
}
impl IsCompatible for AbsoluteFontWeight {
fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
match self {
AbsoluteFontWeight::Weight(val) if !(*val >= 100.0 && *val <= 900.0 && *val % 100.0 == 0.0) => {
Feature::FontWeightNumber.is_compatible(browsers)
}
_ => true,
}
}
}
enum_property! {
#[allow(missing_docs)]
pub enum AbsoluteFontSize {
"xx-small": XXSmall,
"x-small": XSmall,
"small": Small,
"medium": Medium,
"large": Large,
"x-large": XLarge,
"xx-large": XXLarge,
"xxx-large": XXXLarge,
}
}
impl IsCompatible for AbsoluteFontSize {
fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
use AbsoluteFontSize::*;
match self {
XXXLarge => Feature::FontSizeXXXLarge.is_compatible(browsers),
_ => true,
}
}
}
enum_property! {
#[allow(missing_docs)]
pub enum RelativeFontSize {
Smaller,
Larger,
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(tag = "type", content = "value", rename_all = "kebab-case")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub enum FontSize {
Length(LengthPercentage),
Absolute(AbsoluteFontSize),
Relative(RelativeFontSize),
}
impl<'i> Parse<'i> for FontSize {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
if let Ok(val) = input.try_parse(LengthPercentage::parse) {
return Ok(FontSize::Length(val));
}
if let Ok(val) = input.try_parse(AbsoluteFontSize::parse) {
return Ok(FontSize::Absolute(val));
}
let val = RelativeFontSize::parse(input)?;
Ok(FontSize::Relative(val))
}
}
impl ToCss for FontSize {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
use FontSize::*;
match self {
Absolute(val) => val.to_css(dest),
Length(val) => val.to_css(dest),
Relative(val) => val.to_css(dest),
}
}
}
impl IsCompatible for FontSize {
fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
match self {
FontSize::Length(LengthPercentage::Dimension(LengthValue::Rem(..))) => {
Feature::FontSizeRem.is_compatible(browsers)
}
FontSize::Length(l) => l.is_compatible(browsers),
FontSize::Absolute(a) => a.is_compatible(browsers),
FontSize::Relative(..) => true,
}
}
}
enum_property! {
pub enum FontStretchKeyword {
"normal": Normal,
"ultra-condensed": UltraCondensed,
"extra-condensed": ExtraCondensed,
"condensed": Condensed,
"semi-condensed": SemiCondensed,
"semi-expanded": SemiExpanded,
"expanded": Expanded,
"extra-expanded": ExtraExpanded,
"ultra-expanded": UltraExpanded,
}
}
impl Default for FontStretchKeyword {
fn default() -> FontStretchKeyword {
FontStretchKeyword::Normal
}
}
impl Into<Percentage> for &FontStretchKeyword {
fn into(self) -> Percentage {
use FontStretchKeyword::*;
let val = match self {
UltraCondensed => 0.5,
ExtraCondensed => 0.625,
Condensed => 0.75,
SemiCondensed => 0.875,
Normal => 1.0,
SemiExpanded => 1.125,
Expanded => 1.25,
ExtraExpanded => 1.5,
UltraExpanded => 2.0,
};
Percentage(val)
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(tag = "type", content = "value", rename_all = "kebab-case")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub enum FontStretch {
Keyword(FontStretchKeyword),
Percentage(Percentage),
}
impl Default for FontStretch {
fn default() -> FontStretch {
FontStretch::Keyword(FontStretchKeyword::default())
}
}
impl<'i> Parse<'i> for FontStretch {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
if let Ok(val) = input.try_parse(Percentage::parse) {
return Ok(FontStretch::Percentage(val));
}
let keyword = FontStretchKeyword::parse(input)?;
Ok(FontStretch::Keyword(keyword))
}
}
impl Into<Percentage> for &FontStretch {
fn into(self) -> Percentage {
match self {
FontStretch::Percentage(val) => val.clone(),
FontStretch::Keyword(keyword) => keyword.into(),
}
}
}
impl ToCss for FontStretch {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
if dest.minify {
let percentage: Percentage = self.into();
return percentage.to_css(dest);
}
match self {
FontStretch::Percentage(val) => val.to_css(dest),
FontStretch::Keyword(val) => val.to_css(dest),
}
}
}
impl IsCompatible for FontStretch {
fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
match self {
FontStretch::Percentage(..) => Feature::FontStretchPercentage.is_compatible(browsers),
FontStretch::Keyword(..) => true,
}
}
}
enum_property! {
#[allow(missing_docs)]
#[derive(Eq, Hash)]
pub enum GenericFontFamily {
"serif": Serif,
"sans-serif": SansSerif,
"cursive": Cursive,
"fantasy": Fantasy,
"monospace": Monospace,
"system-ui": SystemUI,
"emoji": Emoji,
"math": Math,
"fangsong": FangSong,
"ui-serif": UISerif,
"ui-sans-serif": UISansSerif,
"ui-monospace": UIMonospace,
"ui-rounded": UIRounded,
"initial": Initial,
"inherit": Inherit,
"unset": Unset,
"default": Default,
"revert": Revert,
"revert-layer": RevertLayer,
}
}
impl IsCompatible for GenericFontFamily {
fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
use GenericFontFamily::*;
match self {
SystemUI => Feature::FontFamilySystemUi.is_compatible(browsers),
UISerif | UISansSerif | UIMonospace | UIRounded => Feature::ExtendedSystemFonts.is_compatible(browsers),
_ => true,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "into_owned", derive(lightningcss_derive::IntoOwned))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(untagged))]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub enum FontFamily<'i> {
Generic(GenericFontFamily),
#[cfg_attr(feature = "serde", serde(borrow))]
FamilyName(CowArcStr<'i>),
}
impl<'i> Parse<'i> for FontFamily<'i> {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
if let Ok(value) = input.try_parse(|i| i.expect_string_cloned()) {
return Ok(FontFamily::FamilyName(value.into()));
}
if let Ok(value) = input.try_parse(GenericFontFamily::parse) {
return Ok(FontFamily::Generic(value));
}
let value: CowArcStr<'i> = input.expect_ident()?.into();
let mut string = None;
while let Ok(ident) = input.try_parse(|i| i.expect_ident_cloned()) {
if string.is_none() {
string = Some(value.to_string());
}
if let Some(string) = &mut string {
string.push(' ');
string.push_str(&ident);
}
}
let value = if let Some(string) = string {
string.into()
} else {
value
};
Ok(FontFamily::FamilyName(value))
}
}
impl<'i> ToCss for FontFamily<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
match self {
FontFamily::Generic(val) => val.to_css(dest),
FontFamily::FamilyName(val) => {
if !val.is_empty() && !GenericFontFamily::parse_string(val).is_ok() {
let mut id = String::new();
let mut first = true;
for slice in val.split(' ') {
if first {
first = false;
} else {
id.push(' ');
}
serialize_identifier(slice, &mut id)?;
}
if id.len() < val.len() + 2 {
return dest.write_str(&id);
}
}
serialize_string(&val, dest)?;
Ok(())
}
}
}
}
impl IsCompatible for FontFamily<'_> {
fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
match self {
FontFamily::Generic(g) => g.is_compatible(browsers),
FontFamily::FamilyName(..) => true,
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(tag = "type", content = "value", rename_all = "kebab-case")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub enum FontStyle {
Normal,
Italic,
Oblique(#[cfg_attr(feature = "serde", serde(default = "FontStyle::default_oblique_angle"))] Angle),
}
impl Default for FontStyle {
fn default() -> FontStyle {
FontStyle::Normal
}
}
impl FontStyle {
#[inline]
pub(crate) fn default_oblique_angle() -> Angle {
Angle::Deg(14.0)
}
}
impl<'i> Parse<'i> for FontStyle {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let location = input.current_source_location();
let ident = input.expect_ident()?;
match_ignore_ascii_case! { &*ident,
"normal" => Ok(FontStyle::Normal),
"italic" => Ok(FontStyle::Italic),
"oblique" => {
let angle = input.try_parse(Angle::parse).unwrap_or(FontStyle::default_oblique_angle());
Ok(FontStyle::Oblique(angle))
},
_ => Err(location.new_unexpected_token_error(
cssparser::Token::Ident(ident.clone())
))
}
}
}
impl ToCss for FontStyle {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
match self {
FontStyle::Normal => dest.write_str("normal"),
FontStyle::Italic => dest.write_str("italic"),
FontStyle::Oblique(angle) => {
dest.write_str("oblique")?;
if *angle != FontStyle::default_oblique_angle() {
dest.write_char(' ')?;
angle.to_css(dest)?;
}
Ok(())
}
}
}
}
impl IsCompatible for FontStyle {
fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
match self {
FontStyle::Oblique(angle) if *angle != FontStyle::default_oblique_angle() => {
Feature::FontStyleObliqueAngle.is_compatible(browsers)
}
FontStyle::Normal | FontStyle::Italic | FontStyle::Oblique(..) => true,
}
}
}
enum_property! {
pub enum FontVariantCaps {
"normal": Normal,
"small-caps": SmallCaps,
"all-small-caps": AllSmallCaps,
"petite-caps": PetiteCaps,
"all-petite-caps": AllPetiteCaps,
"unicase": Unicase,
"titling-caps": TitlingCaps,
}
}
impl Default for FontVariantCaps {
fn default() -> FontVariantCaps {
FontVariantCaps::Normal
}
}
impl FontVariantCaps {
fn is_css2(&self) -> bool {
matches!(self, FontVariantCaps::Normal | FontVariantCaps::SmallCaps)
}
fn parse_css2<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let value = Self::parse(input)?;
if !value.is_css2() {
return Err(input.new_custom_error(ParserError::InvalidValue));
}
Ok(value)
}
}
impl IsCompatible for FontVariantCaps {
fn is_compatible(&self, _browsers: crate::targets::Browsers) -> bool {
true
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(tag = "type", content = "value", rename_all = "kebab-case")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub enum LineHeight {
Normal,
Number(CSSNumber),
Length(LengthPercentage),
}
impl Default for LineHeight {
fn default() -> LineHeight {
LineHeight::Normal
}
}
impl<'i> Parse<'i> for LineHeight {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
if input.try_parse(|input| input.expect_ident_matching("normal")).is_ok() {
return Ok(LineHeight::Normal);
}
if let Ok(val) = input.try_parse(CSSNumber::parse) {
return Ok(LineHeight::Number(val));
}
Ok(LineHeight::Length(LengthPercentage::parse(input)?))
}
}
impl ToCss for LineHeight {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
match self {
LineHeight::Normal => dest.write_str("normal"),
LineHeight::Number(val) => val.to_css(dest),
LineHeight::Length(val) => val.to_css(dest),
}
}
}
impl IsCompatible for LineHeight {
fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
match self {
LineHeight::Length(l) => l.is_compatible(browsers),
LineHeight::Normal | LineHeight::Number(..) => true,
}
}
}
enum_property! {
pub enum VerticalAlignKeyword {
"baseline": Baseline,
"sub": Sub,
"super": Super,
"top": Top,
"text-top": TextTop,
"middle": Middle,
"bottom": Bottom,
"text-bottom": TextBottom,
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(tag = "type", content = "value", rename_all = "kebab-case")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub enum VerticalAlign {
Keyword(VerticalAlignKeyword),
Length(LengthPercentage),
}
impl<'i> Parse<'i> for VerticalAlign {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
if let Ok(len) = input.try_parse(LengthPercentage::parse) {
return Ok(VerticalAlign::Length(len));
}
let kw = VerticalAlignKeyword::parse(input)?;
Ok(VerticalAlign::Keyword(kw))
}
}
impl ToCss for VerticalAlign {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
match self {
VerticalAlign::Keyword(kw) => kw.to_css(dest),
VerticalAlign::Length(len) => len.to_css(dest),
}
}
}
define_shorthand! {
#[cfg_attr(feature = "into_owned", derive(lightningcss_derive::IntoOwned))]
pub struct Font<'i> {
#[cfg_attr(feature = "serde", serde(borrow))]
family: FontFamily(Vec<FontFamily<'i>>),
size: FontSize(FontSize),
style: FontStyle(FontStyle),
weight: FontWeight(FontWeight),
stretch: FontStretch(FontStretch),
line_height: LineHeight(LineHeight),
variant_caps: FontVariantCaps(FontVariantCaps),
}
}
impl<'i> Parse<'i> for Font<'i> {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let mut style = None;
let mut weight = None;
let mut stretch = None;
let size;
let mut variant_caps = None;
let mut count = 0;
loop {
if input.try_parse(|input| input.expect_ident_matching("normal")).is_ok() {
count += 1;
continue;
}
if style.is_none() {
if let Ok(value) = input.try_parse(FontStyle::parse) {
style = Some(value);
count += 1;
continue;
}
}
if weight.is_none() {
if let Ok(value) = input.try_parse(FontWeight::parse) {
weight = Some(value);
count += 1;
continue;
}
}
if variant_caps.is_none() {
if let Ok(value) = input.try_parse(FontVariantCaps::parse_css2) {
variant_caps = Some(value);
count += 1;
continue;
}
}
if stretch.is_none() {
if let Ok(value) = input.try_parse(FontStretchKeyword::parse) {
stretch = Some(FontStretch::Keyword(value));
count += 1;
continue;
}
}
size = Some(FontSize::parse(input)?);
break;
}
if count > 4 {
return Err(input.new_custom_error(ParserError::InvalidDeclaration));
}
let size = match size {
Some(s) => s,
None => return Err(input.new_custom_error(ParserError::InvalidDeclaration)),
};
let line_height = if input.try_parse(|input| input.expect_delim('/')).is_ok() {
Some(LineHeight::parse(input)?)
} else {
None
};
let family = input.parse_comma_separated(FontFamily::parse)?;
Ok(Font {
family,
size,
style: style.unwrap_or_default(),
weight: weight.unwrap_or_default(),
stretch: stretch.unwrap_or_default(),
line_height: line_height.unwrap_or_default(),
variant_caps: variant_caps.unwrap_or_default(),
})
}
}
impl<'i> ToCss for Font<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
if self.style != FontStyle::default() {
self.style.to_css(dest)?;
dest.write_char(' ')?;
}
if self.variant_caps != FontVariantCaps::default() {
self.variant_caps.to_css(dest)?;
dest.write_char(' ')?;
}
if self.weight != FontWeight::default() {
self.weight.to_css(dest)?;
dest.write_char(' ')?;
}
if self.stretch != FontStretch::default() {
self.stretch.to_css(dest)?;
dest.write_char(' ')?;
}
self.size.to_css(dest)?;
if self.line_height != LineHeight::default() {
dest.delim('/', true)?;
self.line_height.to_css(dest)?;
}
dest.write_char(' ')?;
let len = self.family.len();
for (idx, val) in self.family.iter().enumerate() {
val.to_css(dest)?;
if idx < len - 1 {
dest.delim(',', false)?;
}
}
Ok(())
}
}
property_bitflags! {
#[derive(Default, Debug)]
struct FontProperty: u8 {
const FontFamily = 1 << 0;
const FontSize = 1 << 1;
const FontStyle = 1 << 2;
const FontWeight = 1 << 3;
const FontStretch = 1 << 4;
const LineHeight = 1 << 5;
const FontVariantCaps = 1 << 6;
const Font = Self::FontFamily.bits() | Self::FontSize.bits() | Self::FontStyle.bits() | Self::FontWeight.bits() | Self::FontStretch.bits() | Self::LineHeight.bits() | Self::FontVariantCaps.bits();
}
}
#[derive(Default, Debug)]
pub(crate) struct FontHandler<'i> {
family: Option<Vec<FontFamily<'i>>>,
size: Option<FontSize>,
style: Option<FontStyle>,
weight: Option<FontWeight>,
stretch: Option<FontStretch>,
line_height: Option<LineHeight>,
variant_caps: Option<FontVariantCaps>,
flushed_properties: FontProperty,
has_any: bool,
}
impl<'i> PropertyHandler<'i> for FontHandler<'i> {
fn handle_property(
&mut self,
property: &Property<'i>,
dest: &mut DeclarationList<'i>,
context: &mut PropertyHandlerContext<'i, '_>,
) -> bool {
use Property::*;
macro_rules! flush {
($prop: ident, $val: expr) => {{
if self.$prop.is_some() && matches!(context.targets.browsers, Some(targets) if !$val.is_compatible(targets)) {
self.flush(dest, context);
}
}};
}
macro_rules! property {
($prop: ident, $val: ident) => {{
flush!($prop, $val);
self.$prop = Some($val.clone());
self.has_any = true;
}};
}
match property {
FontFamily(val) => property!(family, val),
FontSize(val) => property!(size, val),
FontStyle(val) => property!(style, val),
FontWeight(val) => property!(weight, val),
FontStretch(val) => property!(stretch, val),
FontVariantCaps(val) => property!(variant_caps, val),
LineHeight(val) => property!(line_height, val),
Font(val) => {
flush!(family, val.family);
flush!(size, val.size);
flush!(style, val.style);
flush!(weight, val.weight);
flush!(stretch, val.stretch);
flush!(line_height, val.line_height);
flush!(variant_caps, val.variant_caps);
self.family = Some(val.family.clone());
self.size = Some(val.size.clone());
self.style = Some(val.style.clone());
self.weight = Some(val.weight.clone());
self.stretch = Some(val.stretch.clone());
self.line_height = Some(val.line_height.clone());
self.variant_caps = Some(val.variant_caps.clone());
self.has_any = true;
}
Unparsed(val) if is_font_property(&val.property_id) => {
self.flush(dest, context);
self
.flushed_properties
.insert(FontProperty::try_from(&val.property_id).unwrap());
dest.push(property.clone());
}
_ => return false,
}
true
}
fn finalize(&mut self, decls: &mut DeclarationList<'i>, context: &mut PropertyHandlerContext<'i, '_>) {
self.flush(decls, context);
self.flushed_properties = FontProperty::empty();
}
}
impl<'i> FontHandler<'i> {
fn flush(&mut self, decls: &mut DeclarationList<'i>, context: &mut PropertyHandlerContext<'i, '_>) {
if !self.has_any {
return;
}
self.has_any = false;
macro_rules! push {
($prop: ident, $val: expr) => {
decls.push(Property::$prop($val));
self.flushed_properties.insert(FontProperty::$prop);
};
}
let mut family = std::mem::take(&mut self.family);
if !self.flushed_properties.contains(FontProperty::FontFamily) {
family = compatible_font_family(family, !should_compile!(context.targets, FontFamilySystemUi));
}
let size = std::mem::take(&mut self.size);
let style = std::mem::take(&mut self.style);
let weight = std::mem::take(&mut self.weight);
let stretch = std::mem::take(&mut self.stretch);
let line_height = std::mem::take(&mut self.line_height);
let variant_caps = std::mem::take(&mut self.variant_caps);
if let Some(family) = &mut family {
if family.len() > 1 {
let mut seen = HashSet::new();
family.retain(|f| seen.insert(f.clone()));
}
}
if family.is_some()
&& size.is_some()
&& style.is_some()
&& weight.is_some()
&& stretch.is_some()
&& line_height.is_some()
&& variant_caps.is_some()
{
let caps = variant_caps.unwrap();
push!(
Font,
Font {
family: family.unwrap(),
size: size.unwrap(),
style: style.unwrap(),
weight: weight.unwrap(),
stretch: stretch.unwrap(),
line_height: line_height.unwrap(),
variant_caps: if caps.is_css2() {
caps
} else {
FontVariantCaps::default()
},
}
);
if !caps.is_css2() {
push!(FontVariantCaps, variant_caps.unwrap());
}
} else {
if let Some(val) = family {
push!(FontFamily, val);
}
if let Some(val) = size {
push!(FontSize, val);
}
if let Some(val) = style {
push!(FontStyle, val);
}
if let Some(val) = variant_caps {
push!(FontVariantCaps, val);
}
if let Some(val) = weight {
push!(FontWeight, val);
}
if let Some(val) = stretch {
push!(FontStretch, val);
}
if let Some(val) = line_height {
push!(LineHeight, val);
}
}
}
}
const SYSTEM_UI: FontFamily = FontFamily::Generic(GenericFontFamily::SystemUI);
const DEFAULT_SYSTEM_FONTS: &[&str] = &[
"-apple-system",
"BlinkMacSystemFont",
"Segoe UI", "Roboto", "Noto Sans", "Ubuntu", "Cantarell", "Helvetica Neue",
];
#[inline]
fn compatible_font_family(mut family: Option<Vec<FontFamily>>, is_supported: bool) -> Option<Vec<FontFamily>> {
if is_supported {
return family;
}
if let Some(families) = &mut family {
if let Some(position) = families.iter().position(|v| *v == SYSTEM_UI) {
families.splice(
(position + 1)..(position + 1),
DEFAULT_SYSTEM_FONTS
.iter()
.map(|name| FontFamily::FamilyName(CowArcStr::from(*name))),
);
}
}
return family;
}
#[inline]
fn is_font_property(property_id: &PropertyId) -> bool {
match property_id {
PropertyId::FontFamily
| PropertyId::FontSize
| PropertyId::FontStyle
| PropertyId::FontWeight
| PropertyId::FontStretch
| PropertyId::FontVariantCaps
| PropertyId::LineHeight
| PropertyId::Font => true,
_ => false,
}
}