1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//! The `@font-palette-values` rule.

use super::supports::SupportsRule;
use super::{CssRule, CssRuleList, Location, MinifyContext};
use crate::error::{ParserError, PrinterError};
use crate::printer::Printer;
use crate::properties::custom::CustomProperty;
use crate::properties::font::FontFamily;
use crate::targets::Browsers;
use crate::traits::{Parse, ToCss};
use crate::values::color::{ColorFallbackKind, CssColor};
use crate::values::ident::DashedIdent;
use crate::values::number::CSSInteger;
use cssparser::*;

/// A [@font-palette-values](https://drafts.csswg.org/css-fonts-4/#font-palette-values) rule.
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FontPaletteValuesRule<'i> {
  /// The name of the font palette.
  pub name: DashedIdent<'i>,
  /// Declarations in the `@font-palette-values` rule.
  #[cfg_attr(feature = "serde", serde(borrow))]
  pub properties: Vec<FontPaletteValuesProperty<'i>>,
  /// The location of the rule in the source file.
  pub loc: Location,
}

/// A property within an `@font-palette-values` rule.
///
///  See [FontPaletteValuesRule](FontPaletteValuesRule).
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
  feature = "serde",
  derive(serde::Serialize, serde::Deserialize),
  serde(tag = "type", content = "value", rename_all = "kebab-case")
)]
pub enum FontPaletteValuesProperty<'i> {
  /// The `font-family` property.
  #[cfg_attr(feature = "serde", serde(borrow))]
  FontFamily(FontFamily<'i>),
  /// The `base-palette` property.
  BasePalette(BasePalette),
  /// The `override-colors` property.
  OverrideColors(Vec<OverrideColors>),
  /// An unknown or unsupported property.
  Custom(CustomProperty<'i>),
}

/// A value for the [base-palette](https://drafts.csswg.org/css-fonts-4/#base-palette-desc)
/// property in an `@font-palette-values` rule.
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(
  feature = "serde",
  derive(serde::Serialize, serde::Deserialize),
  serde(tag = "type", content = "value", rename_all = "kebab-case")
)]
pub enum BasePalette {
  /// A light color palette as defined within the font.
  Light,
  /// A dark color palette as defined within the font.
  Dark,
  /// A palette index within the font.
  Integer(u16),
}

/// A value for the [override-colors](https://drafts.csswg.org/css-fonts-4/#override-color)
/// property in an `@font-palette-values` rule.
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OverrideColors {
  /// The index of the color within the palette to override.
  index: u16,
  /// The replacement color.
  color: CssColor,
}

pub(crate) struct FontPaletteValuesDeclarationParser;

impl<'i> cssparser::DeclarationParser<'i> for FontPaletteValuesDeclarationParser {
  type Declaration = FontPaletteValuesProperty<'i>;
  type Error = ParserError<'i>;

  fn parse_value<'t>(
    &mut self,
    name: CowRcStr<'i>,
    input: &mut cssparser::Parser<'i, 't>,
  ) -> Result<Self::Declaration, cssparser::ParseError<'i, Self::Error>> {
    let state = input.state();
    match_ignore_ascii_case! { &name,
      "font-family" => {
        // https://drafts.csswg.org/css-fonts-4/#font-family-2-desc
        if let Ok(font_family) = FontFamily::parse(input) {
          return match font_family {
            FontFamily::Generic(_) => Err(input.new_custom_error(ParserError::InvalidDeclaration)),
            _ => Ok(FontPaletteValuesProperty::FontFamily(font_family))
          }
        }
      },
      "base-palette" => {
        // https://drafts.csswg.org/css-fonts-4/#base-palette-desc
        if let Ok(base_palette) = BasePalette::parse(input) {
          return Ok(FontPaletteValuesProperty::BasePalette(base_palette))
        }
      },
      "override-colors" => {
        // https://drafts.csswg.org/css-fonts-4/#override-color
        if let Ok(override_colors) = input.parse_comma_separated(OverrideColors::parse) {
          return Ok(FontPaletteValuesProperty::OverrideColors(override_colors))
        }
      },
      _ => return Err(input.new_custom_error(ParserError::InvalidDeclaration))
    }

    input.reset(&state);
    return Ok(FontPaletteValuesProperty::Custom(CustomProperty::parse(
      name.into(),
      input,
      &Default::default(),
    )?));
  }
}

/// Default methods reject all at rules.
impl<'i> AtRuleParser<'i> for FontPaletteValuesDeclarationParser {
  type Prelude = ();
  type AtRule = FontPaletteValuesProperty<'i>;
  type Error = ParserError<'i>;
}

impl<'i> FontPaletteValuesRule<'i> {
  pub(crate) fn parse<'t>(
    name: DashedIdent<'i>,
    input: &mut Parser<'i, 't>,
    loc: Location,
  ) -> Result<Self, ParseError<'i, ParserError<'i>>> {
    let mut parser = DeclarationListParser::new(input, FontPaletteValuesDeclarationParser);
    let mut properties = vec![];
    while let Some(decl) = parser.next() {
      if let Ok(decl) = decl {
        properties.push(decl);
      }
    }

    Ok(FontPaletteValuesRule { name, properties, loc })
  }
}

impl<'i> Parse<'i> for BasePalette {
  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
    if let Ok(i) = input.try_parse(CSSInteger::parse) {
      if i.is_negative() {
        return Err(input.new_custom_error(ParserError::InvalidValue));
      }
      return Ok(BasePalette::Integer(i as u16));
    }

    let location = input.current_source_location();
    let ident = input.expect_ident()?;
    match_ignore_ascii_case! { &*ident,
      "light" => Ok(BasePalette::Light),
      "dark" => Ok(BasePalette::Dark),
      _ => Err(location.new_unexpected_token_error(Token::Ident(ident.clone())))
    }
  }
}

impl ToCss for BasePalette {
  fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
  where
    W: std::fmt::Write,
  {
    match self {
      BasePalette::Light => dest.write_str("light"),
      BasePalette::Dark => dest.write_str("dark"),
      BasePalette::Integer(i) => (*i as CSSInteger).to_css(dest),
    }
  }
}

impl<'i> Parse<'i> for OverrideColors {
  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
    let index = CSSInteger::parse(input)?;
    if index.is_negative() {
      return Err(input.new_custom_error(ParserError::InvalidValue));
    }

    let color = CssColor::parse(input)?;
    if matches!(color, CssColor::CurrentColor) {
      return Err(input.new_custom_error(ParserError::InvalidValue));
    }

    Ok(OverrideColors {
      index: index as u16,
      color,
    })
  }
}

impl ToCss for OverrideColors {
  fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
  where
    W: std::fmt::Write,
  {
    (self.index as CSSInteger).to_css(dest)?;
    dest.write_char(' ')?;
    self.color.to_css(dest)
  }
}

impl OverrideColors {
  fn get_fallback(&self, kind: ColorFallbackKind) -> OverrideColors {
    OverrideColors {
      index: self.index,
      color: self.color.get_fallback(kind),
    }
  }
}

impl<'i> FontPaletteValuesRule<'i> {
  pub(crate) fn minify(&mut self, context: &mut MinifyContext<'_, 'i>, _: bool) {
    let mut properties = Vec::with_capacity(self.properties.len());
    for property in &self.properties {
      match property {
        FontPaletteValuesProperty::OverrideColors(override_colors) => {
          // Generate color fallbacks.
          if let Some(targets) = context.targets {
            let mut fallbacks = ColorFallbackKind::empty();
            for o in override_colors {
              fallbacks |= o.color.get_necessary_fallbacks(*targets);
            }

            if fallbacks.contains(ColorFallbackKind::RGB) {
              properties.push(FontPaletteValuesProperty::OverrideColors(
                override_colors.iter().map(|o| o.get_fallback(ColorFallbackKind::RGB)).collect(),
              ));
            }

            if fallbacks.contains(ColorFallbackKind::P3) {
              properties.push(FontPaletteValuesProperty::OverrideColors(
                override_colors.iter().map(|o| o.get_fallback(ColorFallbackKind::P3)).collect(),
              ));
            }

            let override_colors = if fallbacks.contains(ColorFallbackKind::LAB) {
              override_colors.iter().map(|o| o.get_fallback(ColorFallbackKind::P3)).collect()
            } else {
              override_colors.clone()
            };

            properties.push(FontPaletteValuesProperty::OverrideColors(override_colors));
          } else {
            properties.push(property.clone())
          }
        }
        _ => properties.push(property.clone()),
      }
    }

    self.properties = properties;
  }

  pub(crate) fn get_fallbacks(&mut self, targets: Browsers) -> Vec<CssRule<'i>> {
    // Get fallbacks for unparsed properties. These will generate @supports rules
    // containing duplicate @font-palette-values rules.
    let mut fallbacks = ColorFallbackKind::empty();
    for property in &self.properties {
      match property {
        FontPaletteValuesProperty::Custom(CustomProperty { value, .. }) => {
          fallbacks |= value.get_necessary_fallbacks(targets);
        }
        _ => {}
      }
    }

    let mut res = Vec::new();
    let lowest_fallback = fallbacks.lowest();
    fallbacks.remove(lowest_fallback);

    if fallbacks.contains(ColorFallbackKind::P3) {
      res.push(self.get_fallback(ColorFallbackKind::P3));
    }

    if fallbacks.contains(ColorFallbackKind::LAB)
      || (!lowest_fallback.is_empty() && lowest_fallback != ColorFallbackKind::LAB)
    {
      res.push(self.get_fallback(ColorFallbackKind::LAB));
    }

    if !lowest_fallback.is_empty() {
      for property in &mut self.properties {
        match property {
          FontPaletteValuesProperty::Custom(CustomProperty { value, .. }) => {
            *value = value.get_fallback(lowest_fallback);
          }
          _ => {}
        }
      }
    }

    res
  }

  fn get_fallback(&self, kind: ColorFallbackKind) -> CssRule<'i> {
    let properties = self
      .properties
      .iter()
      .map(|property| match property {
        FontPaletteValuesProperty::Custom(custom) => FontPaletteValuesProperty::Custom(CustomProperty {
          name: custom.name.clone(),
          value: custom.value.get_fallback(kind),
        }),
        _ => property.clone(),
      })
      .collect();
    CssRule::Supports(SupportsRule {
      condition: kind.supports_condition(),
      rules: CssRuleList(vec![CssRule::FontPaletteValues(FontPaletteValuesRule {
        name: self.name.clone(),
        properties,
        loc: self.loc.clone(),
      })]),
      loc: self.loc.clone(),
    })
  }
}

impl<'i> ToCss for FontPaletteValuesRule<'i> {
  fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
  where
    W: std::fmt::Write,
  {
    dest.add_mapping(self.loc);
    dest.write_str("@font-palette-values ")?;
    self.name.to_css(dest)?;
    dest.whitespace()?;
    dest.write_char('{')?;
    dest.indent();
    let len = self.properties.len();
    for (i, prop) in self.properties.iter().enumerate() {
      dest.newline()?;
      prop.to_css(dest)?;
      if i != len - 1 || !dest.minify {
        dest.write_char(';')?;
      }
    }
    dest.dedent();
    dest.newline()?;
    dest.write_char('}')
  }
}

impl<'i> ToCss for FontPaletteValuesProperty<'i> {
  fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
  where
    W: std::fmt::Write,
  {
    macro_rules! property {
      ($prop: literal, $value: expr) => {{
        dest.write_str($prop)?;
        dest.delim(':', false)?;
        $value.to_css(dest)
      }};
    }

    match self {
      FontPaletteValuesProperty::FontFamily(f) => property!("font-family", f),
      FontPaletteValuesProperty::BasePalette(b) => property!("base-palette", b),
      FontPaletteValuesProperty::OverrideColors(o) => property!("override-colors", o),
      FontPaletteValuesProperty::Custom(custom) => {
        dest.write_str(custom.name.as_ref())?;
        dest.delim(':', false)?;
        custom.value.to_css(dest, true)
      }
    }
  }
}