lewp_css/domain/at_rules/font_face/
font_feature_settings.rs

1// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.
3
4use {
5    super::FontFeatureSetting,
6    crate::{
7        parsers::{Parse, ParserContext},
8        CustomParseError,
9    },
10    cssparser::{
11        serialize_identifier,
12        serialize_string,
13        ParseError,
14        Parser,
15        ToCss,
16    },
17    std::{collections::BTreeMap, fmt},
18};
19
20#[derive(Debug, Clone)]
21pub struct FontFeatureSettings(pub BTreeMap<String, u32>);
22
23impl ToCss for FontFeatureSettings {
24    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
25        if self.0.is_empty() {
26            serialize_identifier("normal", dest)
27        } else {
28            for (openTypeFeatureTag, integer) in self.0.iter() {
29                serialize_string(openTypeFeatureTag, dest)?;
30                let integer = *integer;
31                if integer != 1 {
32                    dest.write_str(" ")?;
33                    integer.to_css(dest)?;
34                }
35            }
36            Ok(())
37        }
38    }
39}
40
41impl Parse for FontFeatureSettings {
42    fn parse<'i, 't>(
43        _: &ParserContext,
44        input: &mut Parser<'i, 't>,
45    ) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
46        if input
47            .r#try(|input| input.expect_ident_matching("normal"))
48            .is_ok()
49        {
50            Ok(FontFeatureSettings(BTreeMap::new()))
51        } else {
52            let mut settings = BTreeMap::new();
53            for setting in input.parse_comma_separated(|input| {
54                FontFeatureSetting::parse(input)
55            })? {
56                settings.insert(setting.0, setting.1);
57            }
58            Ok(FontFeatureSettings(settings))
59        }
60    }
61}
62
63impl FontFeatureSettings {
64    #[inline(always)]
65    pub fn setting(&self, openTypeFeatureTag: &str) -> Option<u32> {
66        self.0.get(openTypeFeatureTag).copied()
67    }
68
69    #[inline(always)]
70    pub fn isOn(&self, openTypeFeatureTag: &str) -> Option<bool> {
71        self.0.get(openTypeFeatureTag).map(|integer| *integer == 1)
72    }
73
74    #[inline(always)]
75    pub fn isOff(&self, openTypeFeatureTag: &str) -> Option<bool> {
76        self.0.get(openTypeFeatureTag).map(|integer| *integer == 0)
77    }
78
79    #[inline(always)]
80    pub fn isNormal(&self) -> bool {
81        self.0.is_empty()
82    }
83
84    #[inline(always)]
85    pub fn normal() -> Self {
86        FontFeatureSettings(Default::default())
87    }
88}