1use std::collections::HashSet;
4
5use super::{Property, PropertyId};
6use crate::compat::Feature;
7use crate::context::PropertyHandlerContext;
8use crate::declaration::{DeclarationBlock, DeclarationList};
9use crate::error::{ParserError, PrinterError};
10use crate::macros::*;
11use crate::printer::Printer;
12use crate::targets::should_compile;
13use crate::traits::{IsCompatible, Parse, PropertyHandler, Shorthand, ToCss};
14use crate::values::length::LengthValue;
15use crate::values::number::CSSNumber;
16use crate::values::string::CowArcStr;
17use crate::values::{angle::Angle, length::LengthPercentage, percentage::Percentage};
18#[cfg(feature = "visitor")]
19use crate::visitor::Visit;
20use cssparser::*;
21
22#[derive(Debug, Clone, PartialEq, Parse, ToCss)]
24#[cfg_attr(feature = "visitor", derive(Visit))]
25#[cfg_attr(
26 feature = "serde",
27 derive(serde::Serialize, serde::Deserialize),
28 serde(tag = "type", content = "value", rename_all = "kebab-case")
29)]
30#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
31#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
32pub enum FontWeight {
33 Absolute(AbsoluteFontWeight),
35 Bolder,
37 Lighter,
39}
40
41impl Default for FontWeight {
42 fn default() -> FontWeight {
43 FontWeight::Absolute(AbsoluteFontWeight::default())
44 }
45}
46
47impl IsCompatible for FontWeight {
48 fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
49 match self {
50 FontWeight::Absolute(a) => a.is_compatible(browsers),
51 FontWeight::Bolder | FontWeight::Lighter => true,
52 }
53 }
54}
55
56#[derive(Debug, Clone, PartialEq, Parse)]
61#[cfg_attr(feature = "visitor", derive(Visit))]
62#[cfg_attr(
63 feature = "serde",
64 derive(serde::Serialize, serde::Deserialize),
65 serde(tag = "type", content = "value", rename_all = "kebab-case")
66)]
67#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
68pub enum AbsoluteFontWeight {
69 Weight(CSSNumber),
71 Normal,
73 Bold,
75}
76
77impl Default for AbsoluteFontWeight {
78 fn default() -> AbsoluteFontWeight {
79 AbsoluteFontWeight::Normal
80 }
81}
82
83impl ToCss for AbsoluteFontWeight {
84 fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
85 where
86 W: std::fmt::Write,
87 {
88 use AbsoluteFontWeight::*;
89 match self {
90 Weight(val) => val.to_css(dest),
91 Normal => dest.write_str(if dest.minify { "400" } else { "normal" }),
92 Bold => dest.write_str(if dest.minify { "700" } else { "bold" }),
93 }
94 }
95}
96
97impl IsCompatible for AbsoluteFontWeight {
98 fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
99 match self {
100 AbsoluteFontWeight::Weight(val) if !(*val >= 100.0 && *val <= 900.0 && *val % 100.0 == 0.0) => {
102 Feature::FontWeightNumber.is_compatible(browsers)
103 }
104 _ => true,
105 }
106 }
107}
108
109enum_property! {
110 #[allow(missing_docs)]
115 pub enum AbsoluteFontSize {
116 "xx-small": XXSmall,
117 "x-small": XSmall,
118 "small": Small,
119 "medium": Medium,
120 "large": Large,
121 "x-large": XLarge,
122 "xx-large": XXLarge,
123 "xxx-large": XXXLarge,
124 }
125}
126
127impl IsCompatible for AbsoluteFontSize {
128 fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
129 use AbsoluteFontSize::*;
130 match self {
131 XXXLarge => Feature::FontSizeXXXLarge.is_compatible(browsers),
132 _ => true,
133 }
134 }
135}
136
137enum_property! {
138 #[allow(missing_docs)]
143 pub enum RelativeFontSize {
144 Smaller,
145 Larger,
146 }
147}
148
149#[derive(Debug, Clone, PartialEq, Parse, ToCss)]
151#[cfg_attr(feature = "visitor", derive(Visit))]
152#[cfg_attr(
153 feature = "serde",
154 derive(serde::Serialize, serde::Deserialize),
155 serde(tag = "type", content = "value", rename_all = "kebab-case")
156)]
157#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
158#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
159pub enum FontSize {
160 Length(LengthPercentage),
162 Absolute(AbsoluteFontSize),
164 Relative(RelativeFontSize),
166}
167
168impl IsCompatible for FontSize {
169 fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
170 match self {
171 FontSize::Length(LengthPercentage::Dimension(LengthValue::Rem(..))) => {
172 Feature::FontSizeRem.is_compatible(browsers)
173 }
174 FontSize::Length(l) => l.is_compatible(browsers),
175 FontSize::Absolute(a) => a.is_compatible(browsers),
176 FontSize::Relative(..) => true,
177 }
178 }
179}
180
181enum_property! {
182 pub enum FontStretchKeyword {
187 "normal": Normal,
189 "ultra-condensed": UltraCondensed,
191 "extra-condensed": ExtraCondensed,
193 "condensed": Condensed,
195 "semi-condensed": SemiCondensed,
197 "semi-expanded": SemiExpanded,
199 "expanded": Expanded,
201 "extra-expanded": ExtraExpanded,
203 "ultra-expanded": UltraExpanded,
205 }
206}
207
208impl Default for FontStretchKeyword {
209 fn default() -> FontStretchKeyword {
210 FontStretchKeyword::Normal
211 }
212}
213
214impl Into<Percentage> for &FontStretchKeyword {
215 fn into(self) -> Percentage {
216 use FontStretchKeyword::*;
217 let val = match self {
218 UltraCondensed => 0.5,
219 ExtraCondensed => 0.625,
220 Condensed => 0.75,
221 SemiCondensed => 0.875,
222 Normal => 1.0,
223 SemiExpanded => 1.125,
224 Expanded => 1.25,
225 ExtraExpanded => 1.5,
226 UltraExpanded => 2.0,
227 };
228 Percentage(val)
229 }
230}
231
232#[derive(Debug, Clone, PartialEq, Parse)]
234#[cfg_attr(feature = "visitor", derive(Visit))]
235#[cfg_attr(
236 feature = "serde",
237 derive(serde::Serialize, serde::Deserialize),
238 serde(tag = "type", content = "value", rename_all = "kebab-case")
239)]
240#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
241#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
242pub enum FontStretch {
243 Keyword(FontStretchKeyword),
245 Percentage(Percentage),
247}
248
249impl Default for FontStretch {
250 fn default() -> FontStretch {
251 FontStretch::Keyword(FontStretchKeyword::default())
252 }
253}
254
255impl Into<Percentage> for &FontStretch {
256 fn into(self) -> Percentage {
257 match self {
258 FontStretch::Percentage(val) => val.clone(),
259 FontStretch::Keyword(keyword) => keyword.into(),
260 }
261 }
262}
263
264impl ToCss for FontStretch {
265 fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
266 where
267 W: std::fmt::Write,
268 {
269 if dest.minify {
270 let percentage: Percentage = self.into();
271 return percentage.to_css(dest);
272 }
273
274 match self {
275 FontStretch::Percentage(val) => val.to_css(dest),
276 FontStretch::Keyword(val) => val.to_css(dest),
277 }
278 }
279}
280
281impl IsCompatible for FontStretch {
282 fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
283 match self {
284 FontStretch::Percentage(..) => Feature::FontStretchPercentage.is_compatible(browsers),
285 FontStretch::Keyword(..) => true,
286 }
287 }
288}
289
290enum_property! {
291 #[allow(missing_docs)]
296 #[derive(Eq, Hash)]
297 pub enum GenericFontFamily {
298 "serif": Serif,
299 "sans-serif": SansSerif,
300 "cursive": Cursive,
301 "fantasy": Fantasy,
302 "monospace": Monospace,
303 "system-ui": SystemUI,
304 "emoji": Emoji,
305 "math": Math,
306 "fangsong": FangSong,
307 "ui-serif": UISerif,
308 "ui-sans-serif": UISansSerif,
309 "ui-monospace": UIMonospace,
310 "ui-rounded": UIRounded,
311
312 "initial": Initial,
316 "inherit": Inherit,
317 "unset": Unset,
318 "default": Default,
321
322 "revert": Revert,
325 "revert-layer": RevertLayer,
326 }
327}
328
329impl IsCompatible for GenericFontFamily {
330 fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
331 use GenericFontFamily::*;
332 match self {
333 SystemUI => Feature::FontFamilySystemUi.is_compatible(browsers),
334 UISerif | UISansSerif | UIMonospace | UIRounded => Feature::ExtendedSystemFonts.is_compatible(browsers),
335 _ => true,
336 }
337 }
338}
339
340#[derive(Debug, Clone, PartialEq, Eq, Hash)]
342#[cfg_attr(feature = "visitor", derive(Visit))]
343#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
344#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(untagged))]
345#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
346pub enum FontFamily<'i> {
347 Generic(GenericFontFamily),
349 #[cfg_attr(feature = "serde", serde(borrow))]
351 FamilyName(CowArcStr<'i>),
352}
353
354impl<'i> Parse<'i> for FontFamily<'i> {
355 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
356 if let Ok(value) = input.try_parse(|i| i.expect_string_cloned()) {
357 return Ok(FontFamily::FamilyName(value.into()));
358 }
359
360 if let Ok(value) = input.try_parse(GenericFontFamily::parse) {
361 return Ok(FontFamily::Generic(value));
362 }
363
364 let value: CowArcStr<'i> = input.expect_ident()?.into();
365 let mut string = None;
366 while let Ok(ident) = input.try_parse(|i| i.expect_ident_cloned()) {
367 if string.is_none() {
368 string = Some(value.to_string());
369 }
370
371 if let Some(string) = &mut string {
372 string.push(' ');
373 string.push_str(&ident);
374 }
375 }
376
377 let value = if let Some(string) = string {
378 string.into()
379 } else {
380 value
381 };
382
383 Ok(FontFamily::FamilyName(value))
384 }
385}
386
387impl<'i> ToCss for FontFamily<'i> {
388 fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
389 where
390 W: std::fmt::Write,
391 {
392 match self {
393 FontFamily::Generic(val) => val.to_css(dest),
394 FontFamily::FamilyName(val) => {
395 if !val.is_empty() && !GenericFontFamily::parse_string(val).is_ok() {
399 let mut id = String::new();
400 let mut first = true;
401 for slice in val.split(' ') {
402 if first {
403 first = false;
404 } else {
405 id.push(' ');
406 }
407 serialize_identifier(slice, &mut id)?;
408 }
409 if id.len() < val.len() + 2 {
410 return dest.write_str(&id);
411 }
412 }
413 serialize_string(&val, dest)?;
414 Ok(())
415 }
416 }
417 }
418}
419
420impl IsCompatible for FontFamily<'_> {
421 fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
422 match self {
423 FontFamily::Generic(g) => g.is_compatible(browsers),
424 FontFamily::FamilyName(..) => true,
425 }
426 }
427}
428
429#[derive(Debug, Clone, PartialEq)]
431#[cfg_attr(feature = "visitor", derive(Visit))]
432#[cfg_attr(
433 feature = "serde",
434 derive(serde::Serialize, serde::Deserialize),
435 serde(tag = "type", content = "value", rename_all = "kebab-case")
436)]
437#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
438#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
439pub enum FontStyle {
440 Normal,
442 Italic,
444 Oblique(#[cfg_attr(feature = "serde", serde(default = "FontStyle::default_oblique_angle"))] Angle),
446}
447
448impl Default for FontStyle {
449 fn default() -> FontStyle {
450 FontStyle::Normal
451 }
452}
453
454impl FontStyle {
455 #[inline]
456 pub(crate) fn default_oblique_angle() -> Angle {
457 Angle::Deg(14.0)
458 }
459}
460
461impl<'i> Parse<'i> for FontStyle {
462 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
463 let location = input.current_source_location();
464 let ident = input.expect_ident()?;
465 match_ignore_ascii_case! { &*ident,
466 "normal" => Ok(FontStyle::Normal),
467 "italic" => Ok(FontStyle::Italic),
468 "oblique" => {
469 let angle = input.try_parse(Angle::parse).unwrap_or(FontStyle::default_oblique_angle());
470 Ok(FontStyle::Oblique(angle))
471 },
472 _ => Err(location.new_unexpected_token_error(
473 cssparser::Token::Ident(ident.clone())
474 ))
475 }
476 }
477}
478
479impl ToCss for FontStyle {
480 fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
481 where
482 W: std::fmt::Write,
483 {
484 match self {
485 FontStyle::Normal => dest.write_str("normal"),
486 FontStyle::Italic => dest.write_str("italic"),
487 FontStyle::Oblique(angle) => {
488 dest.write_str("oblique")?;
489 if *angle != FontStyle::default_oblique_angle() {
490 dest.write_char(' ')?;
491 angle.to_css(dest)?;
492 }
493 Ok(())
494 }
495 }
496 }
497}
498
499impl IsCompatible for FontStyle {
500 fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
501 match self {
502 FontStyle::Oblique(angle) if *angle != FontStyle::default_oblique_angle() => {
503 Feature::FontStyleObliqueAngle.is_compatible(browsers)
504 }
505 FontStyle::Normal | FontStyle::Italic | FontStyle::Oblique(..) => true,
506 }
507 }
508}
509
510enum_property! {
511 pub enum FontVariantCaps {
513 Normal,
515 SmallCaps,
517 AllSmallCaps,
519 PetiteCaps,
521 AllPetiteCaps,
523 Unicase,
525 TitlingCaps,
527 }
528}
529
530impl Default for FontVariantCaps {
531 fn default() -> FontVariantCaps {
532 FontVariantCaps::Normal
533 }
534}
535
536impl FontVariantCaps {
537 fn is_css2(&self) -> bool {
538 matches!(self, FontVariantCaps::Normal | FontVariantCaps::SmallCaps)
539 }
540
541 fn parse_css2<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
542 let value = Self::parse(input)?;
543 if !value.is_css2() {
544 return Err(input.new_custom_error(ParserError::InvalidValue));
545 }
546 Ok(value)
547 }
548}
549
550impl IsCompatible for FontVariantCaps {
551 fn is_compatible(&self, _browsers: crate::targets::Browsers) -> bool {
552 true
553 }
554}
555
556#[derive(Debug, Clone, PartialEq, Parse, ToCss)]
558#[cfg_attr(feature = "visitor", derive(Visit))]
559#[cfg_attr(
560 feature = "serde",
561 derive(serde::Serialize, serde::Deserialize),
562 serde(tag = "type", content = "value", rename_all = "kebab-case")
563)]
564#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
565#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
566pub enum LineHeight {
567 Normal,
569 Number(CSSNumber),
571 Length(LengthPercentage),
573}
574
575impl Default for LineHeight {
576 fn default() -> LineHeight {
577 LineHeight::Normal
578 }
579}
580
581impl IsCompatible for LineHeight {
582 fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
583 match self {
584 LineHeight::Length(l) => l.is_compatible(browsers),
585 LineHeight::Normal | LineHeight::Number(..) => true,
586 }
587 }
588}
589
590enum_property! {
591 pub enum VerticalAlignKeyword {
593 Baseline,
595 Sub,
597 Super,
599 Top,
601 TextTop,
603 Middle,
605 Bottom,
607 TextBottom,
609 }
610}
611
612#[derive(Debug, Clone, PartialEq, Parse, ToCss)]
615#[cfg_attr(feature = "visitor", derive(Visit))]
616#[cfg_attr(
617 feature = "serde",
618 derive(serde::Serialize, serde::Deserialize),
619 serde(tag = "type", content = "value", rename_all = "kebab-case")
620)]
621#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
622#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
623pub enum VerticalAlign {
624 Keyword(VerticalAlignKeyword),
626 Length(LengthPercentage),
628}
629
630define_shorthand! {
631 pub struct Font<'i> {
633 #[cfg_attr(feature = "serde", serde(borrow))]
635 family: FontFamily(Vec<FontFamily<'i>>),
636 size: FontSize(FontSize),
638 style: FontStyle(FontStyle),
640 weight: FontWeight(FontWeight),
642 stretch: FontStretch(FontStretch),
644 line_height: LineHeight(LineHeight),
646 variant_caps: FontVariantCaps(FontVariantCaps),
648 }
649}
650
651impl<'i> Parse<'i> for Font<'i> {
652 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
653 let mut style = None;
654 let mut weight = None;
655 let mut stretch = None;
656 let size;
657 let mut variant_caps = None;
658 let mut count = 0;
659
660 loop {
661 if input.try_parse(|input| input.expect_ident_matching("normal")).is_ok() {
663 count += 1;
664 continue;
665 }
666 if style.is_none() {
667 if let Ok(value) = input.try_parse(FontStyle::parse) {
668 style = Some(value);
669 count += 1;
670 continue;
671 }
672 }
673 if weight.is_none() {
674 if let Ok(value) = input.try_parse(FontWeight::parse) {
675 weight = Some(value);
676 count += 1;
677 continue;
678 }
679 }
680 if variant_caps.is_none() {
681 if let Ok(value) = input.try_parse(FontVariantCaps::parse_css2) {
682 variant_caps = Some(value);
683 count += 1;
684 continue;
685 }
686 }
687
688 if stretch.is_none() {
689 if let Ok(value) = input.try_parse(FontStretchKeyword::parse) {
690 stretch = Some(FontStretch::Keyword(value));
691 count += 1;
692 continue;
693 }
694 }
695 size = Some(FontSize::parse(input)?);
696 break;
697 }
698
699 if count > 4 {
700 return Err(input.new_custom_error(ParserError::InvalidDeclaration));
701 }
702
703 let size = match size {
704 Some(s) => s,
705 None => return Err(input.new_custom_error(ParserError::InvalidDeclaration)),
706 };
707
708 let line_height = if input.try_parse(|input| input.expect_delim('/')).is_ok() {
709 Some(LineHeight::parse(input)?)
710 } else {
711 None
712 };
713
714 let family = input.parse_comma_separated(FontFamily::parse)?;
715 Ok(Font {
716 family,
717 size,
718 style: style.unwrap_or_default(),
719 weight: weight.unwrap_or_default(),
720 stretch: stretch.unwrap_or_default(),
721 line_height: line_height.unwrap_or_default(),
722 variant_caps: variant_caps.unwrap_or_default(),
723 })
724 }
725}
726
727impl<'i> ToCss for Font<'i> {
728 fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
729 where
730 W: std::fmt::Write,
731 {
732 if self.style != FontStyle::default() {
733 self.style.to_css(dest)?;
734 dest.write_char(' ')?;
735 }
736
737 if self.variant_caps != FontVariantCaps::default() {
738 self.variant_caps.to_css(dest)?;
739 dest.write_char(' ')?;
740 }
741
742 if self.weight != FontWeight::default() {
743 self.weight.to_css(dest)?;
744 dest.write_char(' ')?;
745 }
746
747 if self.stretch != FontStretch::default() {
748 self.stretch.to_css(dest)?;
749 dest.write_char(' ')?;
750 }
751
752 self.size.to_css(dest)?;
753
754 if self.line_height != LineHeight::default() {
755 dest.delim('/', true)?;
756 self.line_height.to_css(dest)?;
757 }
758
759 dest.write_char(' ')?;
760
761 let len = self.family.len();
762 for (idx, val) in self.family.iter().enumerate() {
763 val.to_css(dest)?;
764 if idx < len - 1 {
765 dest.delim(',', false)?;
766 }
767 }
768
769 Ok(())
770 }
771}
772
773property_bitflags! {
774 #[derive(Default, Debug)]
775 struct FontProperty: u8 {
776 const FontFamily = 1 << 0;
777 const FontSize = 1 << 1;
778 const FontStyle = 1 << 2;
779 const FontWeight = 1 << 3;
780 const FontStretch = 1 << 4;
781 const LineHeight = 1 << 5;
782 const FontVariantCaps = 1 << 6;
783 const Font = Self::FontFamily.bits() | Self::FontSize.bits() | Self::FontStyle.bits() | Self::FontWeight.bits() | Self::FontStretch.bits() | Self::LineHeight.bits() | Self::FontVariantCaps.bits();
784 }
785}
786
787#[derive(Default, Debug)]
788pub(crate) struct FontHandler<'i> {
789 family: Option<Vec<FontFamily<'i>>>,
790 size: Option<FontSize>,
791 style: Option<FontStyle>,
792 weight: Option<FontWeight>,
793 stretch: Option<FontStretch>,
794 line_height: Option<LineHeight>,
795 variant_caps: Option<FontVariantCaps>,
796 flushed_properties: FontProperty,
797 has_any: bool,
798}
799
800impl<'i> PropertyHandler<'i> for FontHandler<'i> {
801 fn handle_property(
802 &mut self,
803 property: &Property<'i>,
804 dest: &mut DeclarationList<'i>,
805 context: &mut PropertyHandlerContext<'i, '_>,
806 ) -> bool {
807 use Property::*;
808
809 macro_rules! flush {
810 ($prop: ident, $val: expr) => {{
811 if self.$prop.is_some() && self.$prop.as_ref().unwrap() != $val && matches!(context.targets.browsers, Some(targets) if !$val.is_compatible(targets)) {
812 self.flush(dest, context);
813 }
814 }};
815 }
816
817 macro_rules! property {
818 ($prop: ident, $val: ident) => {{
819 flush!($prop, $val);
820 self.$prop = Some($val.clone());
821 self.has_any = true;
822 }};
823 }
824
825 match property {
826 FontFamily(val) => property!(family, val),
827 FontSize(val) => property!(size, val),
828 FontStyle(val) => property!(style, val),
829 FontWeight(val) => property!(weight, val),
830 FontStretch(val) => property!(stretch, val),
831 FontVariantCaps(val) => property!(variant_caps, val),
832 LineHeight(val) => property!(line_height, val),
833 Font(val) => {
834 flush!(family, &val.family);
835 flush!(size, &val.size);
836 flush!(style, &val.style);
837 flush!(weight, &val.weight);
838 flush!(stretch, &val.stretch);
839 flush!(line_height, &val.line_height);
840 flush!(variant_caps, &val.variant_caps);
841 self.family = Some(val.family.clone());
842 self.size = Some(val.size.clone());
843 self.style = Some(val.style.clone());
844 self.weight = Some(val.weight.clone());
845 self.stretch = Some(val.stretch.clone());
846 self.line_height = Some(val.line_height.clone());
847 self.variant_caps = Some(val.variant_caps.clone());
848 self.has_any = true;
849 }
851 Unparsed(val) if is_font_property(&val.property_id) => {
852 self.flush(dest, context);
853 self
854 .flushed_properties
855 .insert(FontProperty::try_from(&val.property_id).unwrap());
856 dest.push(property.clone());
857 }
858 _ => return false,
859 }
860
861 true
862 }
863
864 fn finalize(&mut self, decls: &mut DeclarationList<'i>, context: &mut PropertyHandlerContext<'i, '_>) {
865 self.flush(decls, context);
866 self.flushed_properties = FontProperty::empty();
867 }
868}
869
870impl<'i> FontHandler<'i> {
871 fn flush(&mut self, decls: &mut DeclarationList<'i>, context: &mut PropertyHandlerContext<'i, '_>) {
872 if !self.has_any {
873 return;
874 }
875
876 self.has_any = false;
877
878 macro_rules! push {
879 ($prop: ident, $val: expr) => {
880 decls.push(Property::$prop($val));
881 self.flushed_properties.insert(FontProperty::$prop);
882 };
883 }
884
885 let mut family = std::mem::take(&mut self.family);
886 if !self.flushed_properties.contains(FontProperty::FontFamily) {
887 family = compatible_font_family(family, !should_compile!(context.targets, FontFamilySystemUi));
888 }
889 let size = std::mem::take(&mut self.size);
890 let style = std::mem::take(&mut self.style);
891 let weight = std::mem::take(&mut self.weight);
892 let stretch = std::mem::take(&mut self.stretch);
893 let line_height = std::mem::take(&mut self.line_height);
894 let variant_caps = std::mem::take(&mut self.variant_caps);
895
896 if let Some(family) = &mut family {
897 if family.len() > 1 {
898 let mut seen = HashSet::new();
900 family.retain(|f| seen.insert(f.clone()));
901 }
902 }
903
904 if family.is_some()
905 && size.is_some()
906 && style.is_some()
907 && weight.is_some()
908 && stretch.is_some()
909 && line_height.is_some()
910 && variant_caps.is_some()
911 {
912 let caps = variant_caps.unwrap();
913 push!(
914 Font,
915 Font {
916 family: family.unwrap(),
917 size: size.unwrap(),
918 style: style.unwrap(),
919 weight: weight.unwrap(),
920 stretch: stretch.unwrap(),
921 line_height: line_height.unwrap(),
922 variant_caps: if caps.is_css2() {
923 caps
924 } else {
925 FontVariantCaps::default()
926 },
927 }
928 );
929
930 if !caps.is_css2() {
933 push!(FontVariantCaps, variant_caps.unwrap());
934 }
935 } else {
936 if let Some(val) = family {
937 push!(FontFamily, val);
938 }
939
940 if let Some(val) = size {
941 push!(FontSize, val);
942 }
943
944 if let Some(val) = style {
945 push!(FontStyle, val);
946 }
947
948 if let Some(val) = variant_caps {
949 push!(FontVariantCaps, val);
950 }
951
952 if let Some(val) = weight {
953 push!(FontWeight, val);
954 }
955
956 if let Some(val) = stretch {
957 push!(FontStretch, val);
958 }
959
960 if let Some(val) = line_height {
961 push!(LineHeight, val);
962 }
963 }
964 }
965}
966
967const SYSTEM_UI: FontFamily = FontFamily::Generic(GenericFontFamily::SystemUI);
968
969const DEFAULT_SYSTEM_FONTS: &[&str] = &[
970 "-apple-system",
972 "BlinkMacSystemFont",
974 "Segoe UI", "Roboto", "Noto Sans", "Ubuntu", "Cantarell", "Helvetica Neue",
980];
981
982#[inline]
986fn compatible_font_family(mut family: Option<Vec<FontFamily>>, is_supported: bool) -> Option<Vec<FontFamily>> {
987 if is_supported {
988 return family;
989 }
990
991 if let Some(families) = &mut family {
992 if let Some(position) = families.iter().position(|v| *v == SYSTEM_UI) {
993 families.splice(
994 (position + 1)..(position + 1),
995 DEFAULT_SYSTEM_FONTS
996 .iter()
997 .map(|name| FontFamily::FamilyName(CowArcStr::from(*name))),
998 );
999 }
1000 }
1001
1002 return family;
1003}
1004
1005#[inline]
1006fn is_font_property(property_id: &PropertyId) -> bool {
1007 match property_id {
1008 PropertyId::FontFamily
1009 | PropertyId::FontSize
1010 | PropertyId::FontStyle
1011 | PropertyId::FontWeight
1012 | PropertyId::FontStretch
1013 | PropertyId::FontVariantCaps
1014 | PropertyId::LineHeight
1015 | PropertyId::Font => true,
1016 _ => false,
1017 }
1018}