forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfont_feature_values.rs
More file actions
331 lines (300 loc) · 9.45 KB
/
font_feature_values.rs
File metadata and controls
331 lines (300 loc) · 9.45 KB
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
//! The `@font-feature-values` rule.
use super::Location;
use crate::error::{ParserError, PrinterError};
use crate::parser::ParserOptions;
use crate::printer::Printer;
use crate::properties::font::FamilyName;
use crate::traits::{Parse, ToCss};
use crate::values::ident::Ident;
use crate::values::number::CSSInteger;
#[cfg(feature = "visitor")]
use crate::visitor::Visit;
use cssparser::*;
use indexmap::IndexMap;
use smallvec::SmallVec;
use std::fmt::Write;
/// A [@font-feature-values](https://drafts.csswg.org/css-fonts/#font-feature-values) rule.
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
pub struct FontFeatureValuesRule<'i> {
/// The name of the font feature values.
#[cfg_attr(feature = "serde", serde(borrow))]
pub name: Vec<FamilyName<'i>>,
/// The rules within the `@font-feature-values` rule.
pub rules: IndexMap<FontFeatureSubruleType, FontFeatureSubrule<'i>>,
/// The location of the rule in the source file.
#[cfg_attr(feature = "visitor", skip_visit)]
pub loc: Location,
}
impl<'i> FontFeatureValuesRule<'i> {
pub(crate) fn parse<'t, 'o>(
family_names: Vec<FamilyName<'i>>,
input: &mut Parser<'i, 't>,
loc: Location,
options: &ParserOptions<'o, 'i>,
) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let mut rules = IndexMap::new();
let mut rule_parser = FontFeatureValuesRuleParser {
rules: &mut rules,
options,
};
let mut parser = RuleBodyParser::new(input, &mut rule_parser);
while let Some(decl_or_rule) = parser.next() {
if let Err((err, _)) = decl_or_rule {
if parser.parser.options.error_recovery {
parser.parser.options.warn(err);
continue;
}
return Err(err);
}
}
Ok(FontFeatureValuesRule {
name: family_names,
rules,
loc,
})
}
}
struct FontFeatureValuesRuleParser<'a, 'o, 'i> {
rules: &'a mut IndexMap<FontFeatureSubruleType, FontFeatureSubrule<'i>>,
options: &'a ParserOptions<'o, 'i>,
}
impl<'a, 'o, 'i> cssparser::DeclarationParser<'i> for FontFeatureValuesRuleParser<'a, 'o, 'i> {
type Declaration = ();
type Error = ParserError<'i>;
}
impl<'a, 'o, 'i> cssparser::AtRuleParser<'i> for FontFeatureValuesRuleParser<'a, 'o, 'i> {
type Prelude = FontFeatureSubruleType;
type AtRule = ();
type Error = ParserError<'i>;
fn parse_prelude<'t>(
&mut self,
name: CowRcStr<'i>,
input: &mut Parser<'i, 't>,
) -> Result<Self::Prelude, ParseError<'i, Self::Error>> {
let loc = input.current_source_location();
FontFeatureSubruleType::parse_string(&name)
.map_err(|_| loc.new_custom_error(ParserError::AtRuleInvalid(name.clone().into())))
}
fn parse_block<'t>(
&mut self,
prelude: Self::Prelude,
start: &ParserState,
input: &mut Parser<'i, 't>,
) -> Result<Self::AtRule, ParseError<'i, Self::Error>> {
let loc = start.source_location();
let mut decls = IndexMap::new();
let mut has_existing = false;
let declarations = if let Some(rule) = self.rules.get_mut(&prelude) {
has_existing = true;
&mut rule.declarations
} else {
&mut decls
};
let mut decl_parser = FontFeatureDeclarationParser { declarations };
let mut parser = RuleBodyParser::new(input, &mut decl_parser);
while let Some(decl) = parser.next() {
if let Err((err, _)) = decl {
if self.options.error_recovery {
self.options.warn(err);
continue;
}
return Err(err);
}
}
if !has_existing {
self.rules.insert(
prelude,
FontFeatureSubrule {
name: prelude,
declarations: decls,
loc: Location {
source_index: self.options.source_index,
line: loc.line,
column: loc.column,
},
},
);
}
Ok(())
}
}
impl<'a, 'o, 'i> QualifiedRuleParser<'i> for FontFeatureValuesRuleParser<'a, 'o, 'i> {
type Prelude = ();
type QualifiedRule = ();
type Error = ParserError<'i>;
}
impl<'a, 'o, 'i> RuleBodyItemParser<'i, (), ParserError<'i>> for FontFeatureValuesRuleParser<'a, 'o, 'i> {
fn parse_declarations(&self) -> bool {
false
}
fn parse_qualified(&self) -> bool {
false
}
}
impl<'i> ToCss for FontFeatureValuesRule<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@font-feature-values ")?;
self.name.to_css(dest)?;
dest.whitespace()?;
dest.write_char('{')?;
if !self.rules.is_empty() {
dest.newline()?;
for rule in self.rules.values() {
rule.to_css(dest)?;
dest.newline()?;
}
}
dest.write_char('}')
}
}
impl<'i> FontFeatureValuesRule<'i> {
pub(crate) fn merge(&mut self, other: &FontFeatureValuesRule<'i>) {
debug_assert_eq!(self.name, other.name);
for (prelude, rule) in &other.rules {
if let Some(existing) = self.rules.get_mut(prelude) {
existing
.declarations
.extend(rule.declarations.iter().map(|(k, v)| (k.clone(), v.clone())));
} else {
self.rules.insert(*prelude, rule.clone());
}
}
}
}
/// The name of the `@font-feature-values` sub-rule.
/// font-feature-value-type = <@stylistic> | <@historical-forms> | <@styleset> | <@character-variant>
/// | <@swash> | <@ornaments> | <@annotation>
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Parse, ToCss)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "kebab-case")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
pub enum FontFeatureSubruleType {
/// @stylistic = @stylistic { <declaration-list> }
Stylistic,
/// @historical-forms = @historical-forms { <declaration-list> }
HistoricalForms,
/// @styleset = @styleset { <declaration-list> }
Styleset,
/// @character-variant = @character-variant { <declaration-list> }
CharacterVariant,
/// @swash = @swash { <declaration-list> }
Swash,
/// @ornaments = @ornaments { <declaration-list> }
Ornaments,
/// @annotation = @annotation { <declaration-list> }
Annotation,
}
/// A sub-rule of `@font-feature-values`
/// https://drafts.csswg.org/css-fonts/#font-feature-values-syntax
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "camelCase")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub struct FontFeatureSubrule<'i> {
/// The name of the `@font-feature-values` sub-rule.
pub name: FontFeatureSubruleType,
/// The declarations within the `@font-feature-values` sub-rules.
#[cfg_attr(feature = "serde", serde(borrow))]
pub declarations: IndexMap<Ident<'i>, SmallVec<[CSSInteger; 1]>>,
/// The location of the rule in the source file.
#[cfg_attr(feature = "visitor", skip_visit)]
pub loc: Location,
}
impl<'i> ToCss for FontFeatureSubrule<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_char('@')?;
self.name.to_css(dest)?;
dest.write_char('{')?;
dest.indent();
let len = self.declarations.len();
for (i, (name, value)) in self.declarations.iter().enumerate() {
dest.newline()?;
name.to_css(dest)?;
dest.delim(':', false)?;
let mut first = true;
for index in value {
if first {
first = false;
} else {
dest.write_char(' ')?;
}
index.to_css(dest)?;
}
if i != len - 1 || !dest.minify {
dest.write_char(';')?;
}
}
dest.dedent();
dest.newline()?;
dest.write_char('}')
}
}
struct FontFeatureDeclarationParser<'a, 'i> {
declarations: &'a mut IndexMap<Ident<'i>, SmallVec<[CSSInteger; 1]>>,
}
impl<'a, 'i> cssparser::DeclarationParser<'i> for FontFeatureDeclarationParser<'a, 'i> {
type Declaration = ();
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 mut indices = SmallVec::new();
loop {
if let Ok(value) = CSSInteger::parse(input) {
indices.push(value);
} else {
break;
}
}
if indices.is_empty() {
return Err(input.new_custom_error(ParserError::InvalidValue));
}
self.declarations.insert(Ident(name.into()), indices);
Ok(())
}
}
/// Default methods reject all at rules.
impl<'a, 'i> AtRuleParser<'i> for FontFeatureDeclarationParser<'a, 'i> {
type Prelude = ();
type AtRule = ();
type Error = ParserError<'i>;
}
impl<'a, 'i> QualifiedRuleParser<'i> for FontFeatureDeclarationParser<'a, 'i> {
type Prelude = ();
type QualifiedRule = ();
type Error = ParserError<'i>;
}
impl<'a, 'i> RuleBodyItemParser<'i, (), ParserError<'i>> for FontFeatureDeclarationParser<'a, 'i> {
fn parse_qualified(&self) -> bool {
false
}
fn parse_declarations(&self) -> bool {
true
}
}