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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use Atom;
use cssparser::{AtRuleParser, AtRuleType, BasicParseErrorKind, DeclarationListParser, DeclarationParser, Parser};
use cssparser::{CowRcStr, RuleListParser, SourceLocation, QualifiedRuleParser, Token, serialize_identifier};
use error_reporting::{ContextualParseError, ParseErrorReporter};
#[cfg(feature = "gecko")]
use gecko_bindings::bindings::Gecko_AppendFeatureValueHashEntry;
#[cfg(feature = "gecko")]
use gecko_bindings::structs::{self, gfxFontFeatureValueSet, nsTArray};
use parser::{ParserContext, ParserErrorContext, Parse};
use shared_lock::{SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt;
use style_traits::{ParseError, StyleParseErrorKind, ToCss};
use stylesheets::CssRuleType;
use values::computed::font::FamilyName;
#[derive(Clone, Debug, PartialEq)]
pub struct FFVDeclaration<T> {
pub name: Atom,
pub value: T,
}
impl<T: ToCss> ToCss for FFVDeclaration<T> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
serialize_identifier(&self.name.to_string(), dest)?;
dest.write_str(": ")?;
self.value.to_css(dest)?;
dest.write_str(";")
}
}
#[cfg(feature = "gecko")]
pub trait ToGeckoFontFeatureValues {
fn to_gecko_font_feature_values(&self, array: &mut nsTArray<u32>);
}
#[derive(Clone, Debug, PartialEq)]
pub struct SingleValue(pub u32);
impl Parse for SingleValue {
fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<SingleValue, ParseError<'i>> {
let location = input.current_source_location();
match *input.next()? {
Token::Number { int_value: Some(v), .. } if v >= 0 => Ok(SingleValue(v as u32)),
ref t => Err(location.new_unexpected_token_error(t.clone())),
}
}
}
impl ToCss for SingleValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.to_css(dest)
}
}
#[cfg(feature = "gecko")]
impl ToGeckoFontFeatureValues for SingleValue {
fn to_gecko_font_feature_values(&self, array: &mut nsTArray<u32>) {
unsafe { array.set_len_pod(1); }
array[0] = self.0 as u32;
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct PairValues(pub u32, pub Option<u32>);
impl Parse for PairValues {
fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<PairValues, ParseError<'i>> {
let location = input.current_source_location();
let first = match *input.next()? {
Token::Number { int_value: Some(a), .. } if a >= 0 => a as u32,
ref t => return Err(location.new_unexpected_token_error(t.clone())),
};
let location = input.current_source_location();
match input.next() {
Ok(&Token::Number { int_value: Some(b), .. }) if b >= 0 => {
Ok(PairValues(first, Some(b as u32)))
}
Ok(t) => Err(location.new_unexpected_token_error(t.clone())),
Err(_) => Ok(PairValues(first, None))
}
}
}
impl ToCss for PairValues {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.to_css(dest)?;
if let Some(second) = self.1 {
dest.write_char(' ')?;
second.to_css(dest)?;
}
Ok(())
}
}
#[cfg(feature = "gecko")]
impl ToGeckoFontFeatureValues for PairValues {
fn to_gecko_font_feature_values(&self, array: &mut nsTArray<u32>) {
let len = if self.1.is_some() { 2 } else { 1 };
unsafe { array.set_len_pod(len); }
array[0] = self.0 as u32;
if let Some(second) = self.1 {
array[1] = second as u32;
};
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct VectorValues(pub Vec<u32>);
impl Parse for VectorValues {
fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<VectorValues, ParseError<'i>> {
let mut vec = vec![];
loop {
let location = input.current_source_location();
match input.next() {
Ok(&Token::Number { int_value: Some(a), .. }) if a >= 0 => {
vec.push(a as u32);
},
Ok(t) => return Err(location.new_unexpected_token_error(t.clone())),
Err(_) => break,
}
}
if vec.len() == 0 {
return Err(input.new_error(BasicParseErrorKind::EndOfInput));
}
Ok(VectorValues(vec))
}
}
impl ToCss for VectorValues {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
let mut iter = self.0.iter();
let first = iter.next();
if let Some(first) = first {
first.to_css(dest)?;
for value in iter {
dest.write_char(' ')?;
value.to_css(dest)?;
}
}
Ok(())
}
}
#[cfg(feature = "gecko")]
impl ToGeckoFontFeatureValues for VectorValues {
fn to_gecko_font_feature_values(&self, array: &mut nsTArray<u32>) {
unsafe { array.set_len_pod(self.0.len() as u32); }
for (dest, value) in array.iter_mut().zip(self.0.iter()) {
*dest = *value;
}
}
}
pub fn parse_family_name_list<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Vec<FamilyName>, ParseError<'i>> {
input.parse_comma_separated(|i| FamilyName::parse(context, i)).map_err(|e| e.into())
}
struct FFVDeclarationsParser<'a, 'b: 'a, T: 'a> {
context: &'a ParserContext<'b>,
declarations: &'a mut Vec<FFVDeclaration<T>>,
}
impl<'a, 'b, 'i, T> AtRuleParser<'i> for FFVDeclarationsParser<'a, 'b, T> {
type PreludeNoBlock = ();
type PreludeBlock = ();
type AtRule = ();
type Error = StyleParseErrorKind<'i>;
}
impl<'a, 'b, 'i, T> DeclarationParser<'i> for FFVDeclarationsParser<'a, 'b, T>
where T: Parse
{
type Declaration = ();
type Error = StyleParseErrorKind<'i>;
fn parse_value<'t>(&mut self, name: CowRcStr<'i>, input: &mut Parser<'i, 't>)
-> Result<(), ParseError<'i>> {
let value = input.parse_entirely(|i| T::parse(self.context, i))?;
let new = FFVDeclaration {
name: Atom::from(&*name),
value: value,
};
update_or_push(&mut self.declarations, new);
Ok(())
}
}
macro_rules! font_feature_values_blocks {
(
blocks = [
$( #[$doc: meta] $name: tt $ident: ident / $ident_camel: ident / $gecko_enum: ident: $ty: ty, )*
]
) => {
#[derive(Clone, Debug, PartialEq)]
pub struct FontFeatureValuesRule {
pub family_names: Vec<FamilyName>,
$(
#[$doc]
pub $ident: Vec<FFVDeclaration<$ty>>,
)*
pub source_location: SourceLocation,
}
impl FontFeatureValuesRule {
fn new(family_names: Vec<FamilyName>, location: SourceLocation) -> Self {
FontFeatureValuesRule {
family_names: family_names,
$(
$ident: vec![],
)*
source_location: location,
}
}
pub fn parse<R>(context: &ParserContext,
error_context: &ParserErrorContext<R>,
input: &mut Parser,
family_names: Vec<FamilyName>,
location: SourceLocation)
-> FontFeatureValuesRule
where R: ParseErrorReporter
{
let mut rule = FontFeatureValuesRule::new(family_names, location);
{
let mut iter = RuleListParser::new_for_nested_rule(input, FontFeatureValuesRuleParser {
context: context,
error_context: error_context,
rule: &mut rule,
});
while let Some(result) = iter.next() {
if let Err((error, slice)) = result {
let location = error.location;
let error = ContextualParseError::UnsupportedRule(slice, error);
context.log_css_error(error_context, location, error);
}
}
}
rule
}
pub fn font_family_to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
let mut iter = self.family_names.iter();
iter.next().unwrap().to_css(dest)?;
for val in iter {
dest.write_str(", ")?;
val.to_css(dest)?;
}
Ok(())
}
pub fn value_to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
$(
if self.$ident.len() > 0 {
dest.write_str(concat!("@", $name, " {\n"))?;
let iter = self.$ident.iter();
for val in iter {
val.to_css(dest)?;
dest.write_str("\n")?
}
dest.write_str("}\n")?
}
)*
Ok(())
}
pub fn len(&self) -> usize {
let mut len = 0;
$(
len += self.$ident.len();
)*
len
}
#[cfg(feature = "gecko")]
pub fn set_at_rules(&self, dest: *mut gfxFontFeatureValueSet) {
for ref family in self.family_names.iter() {
let family = family.name.to_ascii_lowercase();
$(
if self.$ident.len() > 0 {
for val in self.$ident.iter() {
let array = unsafe {
Gecko_AppendFeatureValueHashEntry(
dest,
family.as_ptr(),
structs::$gecko_enum,
val.name.as_ptr()
)
};
unsafe {
val.value.to_gecko_font_feature_values(&mut *array);
}
}
}
)*
}
}
}
impl ToCssWithGuard for FontFeatureValuesRule {
fn to_css<W>(&self, _guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result
where W: fmt::Write
{
dest.write_str("@font-feature-values ")?;
self.font_family_to_css(dest)?;
dest.write_str(" {\n")?;
self.value_to_css(dest)?;
dest.write_str("}")
}
}
fn update_or_push<T>(vec: &mut Vec<FFVDeclaration<T>>, element: FFVDeclaration<T>) {
let position = vec.iter().position(|ref val| val.name == element.name);
if let Some(index) = position {
vec[index].value = element.value;
} else {
vec.push(element);
}
}
enum BlockType {
$(
$ident_camel,
)*
}
struct FontFeatureValuesRuleParser<'a, R: 'a> {
context: &'a ParserContext<'a>,
error_context: &'a ParserErrorContext<'a, R>,
rule: &'a mut FontFeatureValuesRule,
}
impl<'a, 'i, R: ParseErrorReporter> QualifiedRuleParser<'i> for FontFeatureValuesRuleParser<'a, R> {
type Prelude = ();
type QualifiedRule = ();
type Error = StyleParseErrorKind<'i>;
}
impl<'a, 'i, R: ParseErrorReporter> AtRuleParser<'i> for FontFeatureValuesRuleParser<'a, R> {
type PreludeNoBlock = ();
type PreludeBlock = BlockType;
type AtRule = ();
type Error = StyleParseErrorKind<'i>;
fn parse_prelude<'t>(&mut self,
name: CowRcStr<'i>,
input: &mut Parser<'i, 't>)
-> Result<AtRuleType<(), BlockType>, ParseError<'i>> {
match_ignore_ascii_case! { &*name,
$(
$name => Ok(AtRuleType::WithBlock(BlockType::$ident_camel)),
)*
_ => Err(input.new_error(BasicParseErrorKind::AtRuleBodyInvalid)),
}
}
fn parse_block<'t>(
&mut self,
prelude: BlockType,
input: &mut Parser<'i, 't>
) -> Result<Self::AtRule, ParseError<'i>> {
debug_assert_eq!(self.context.rule_type(), CssRuleType::FontFeatureValues);
match prelude {
$(
BlockType::$ident_camel => {
let parser = FFVDeclarationsParser {
context: &self.context,
declarations: &mut self.rule.$ident,
};
let mut iter = DeclarationListParser::new(input, parser);
while let Some(declaration) = iter.next() {
if let Err((error, slice)) = declaration {
let location = error.location;
let error = ContextualParseError::UnsupportedKeyframePropertyDeclaration(
slice, error
);
self.context.log_css_error(self.error_context, location, error);
}
}
},
)*
}
Ok(())
}
}
}
}
font_feature_values_blocks! {
blocks = [
#[doc = "A @swash blocksck. \
Specifies a feature name that will work with the swash() \
functional notation of font-variant-alternates."]
"swash" swash / Swash / NS_FONT_VARIANT_ALTERNATES_SWASH: SingleValue,
#[doc = "A @stylistic block. \
Specifies a feature name that will work with the annotation() \
functional notation of font-variant-alternates."]
"stylistic" stylistic / Stylistic / NS_FONT_VARIANT_ALTERNATES_STYLISTIC: SingleValue,
#[doc = "A @ornaments block. \
Specifies a feature name that will work with the ornaments() ] \
functional notation of font-variant-alternates."]
"ornaments" ornaments / Ornaments / NS_FONT_VARIANT_ALTERNATES_ORNAMENTS: SingleValue,
#[doc = "A @annotation block. \
Specifies a feature name that will work with the stylistic() \
functional notation of font-variant-alternates."]
"annotation" annotation / Annotation / NS_FONT_VARIANT_ALTERNATES_ANNOTATION: SingleValue,
#[doc = "A @character-variant block. \
Specifies a feature name that will work with the styleset() \
functional notation of font-variant-alternates. The value can be a pair."]
"character-variant" character_variant / CharacterVariant / NS_FONT_VARIANT_ALTERNATES_CHARACTER_VARIANT:
PairValues,
#[doc = "A @styleset block. \
Specifies a feature name that will work with the character-variant() \
functional notation of font-variant-alternates. The value can be a list."]
"styleset" styleset / Styleset / NS_FONT_VARIANT_ALTERNATES_STYLESET: VectorValues,
]
}