lewp_css/domain/at_rules/font_face/
font_weight.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    crate::{
6        parsers::{Parse, ParserContext},
7        CustomParseError,
8    },
9    cssparser::{serialize_identifier, ParseError, Parser, ToCss},
10    std::fmt,
11};
12
13/// A font-weight value for a @font-face rule.
14/// The font-weight CSS property specifies the weight or boldness of the font.
15#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
16pub enum FontWeight {
17    _100,
18    _200,
19    _300,
20    _400,
21    _500,
22    _600,
23    _700,
24    _800,
25    _900,
26}
27
28impl ToCss for FontWeight {
29    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
30        use self::FontWeight::*;
31
32        match *self {
33            _100 => serialize_identifier("100", dest),
34            _200 => serialize_identifier("200", dest),
35            _300 => serialize_identifier("300", dest),
36            _400 => serialize_identifier("400", dest),
37            _500 => serialize_identifier("500", dest),
38            _600 => serialize_identifier("600", dest),
39            _700 => serialize_identifier("700", dest),
40            _800 => serialize_identifier("800", dest),
41            _900 => serialize_identifier("900", dest),
42        }
43    }
44}
45
46impl Parse for FontWeight {
47    fn parse<'i, 't>(
48        _: &ParserContext,
49        input: &mut Parser<'i, 't>,
50    ) -> Result<FontWeight, ParseError<'i, CustomParseError<'i>>> {
51        let result = input.r#try(|input| {
52            let ident = input.expect_ident().map_err(|_| ())?;
53            match_ignore_ascii_case! {
54                ident,
55                "normal" => Ok(Self::normal),
56                "bold" => Ok(Self::bold),
57                _ => Err(())
58            }
59        });
60
61        result.or_else(|_| {
62            Self::from_int(input.expect_integer()?).map_err(|()| {
63                ParseError::from(
64                    CustomParseError::FontFaceAtRuleFontWeightWasNotAValidIdentifierOrInteger,
65                )
66            })
67        })
68    }
69}
70
71impl FontWeight {
72    /// Convert from an integer to Weight
73    /// As of CSS Fonts Module Level 3, only the following values are valid: 100, 200, 300, 400, 500, 600, 700, 800 and 900
74    pub fn from_int(weight: i32) -> Result<Self, ()> {
75        use self::FontWeight::*;
76
77        match weight {
78            100 => Ok(_100),
79            200 => Ok(_200),
80            300 => Ok(_300),
81            400 => Ok(_400),
82            500 => Ok(_500),
83            600 => Ok(_600),
84            700 => Ok(_700),
85            800 => Ok(_800),
86            900 => Ok(_900),
87            _ => Err(()),
88        }
89    }
90
91    pub fn isExactlyNormal(&self) -> bool {
92        *self == FontWeight::_400
93    }
94
95    pub fn isExactlyBold(&self) -> bool {
96        *self == FontWeight::_700
97    }
98
99    pub fn isBold(&self) -> bool {
100        *self > FontWeight::_500
101    }
102
103    /// Return the bolder weight
104    pub fn bolder(self) -> Self {
105        use self::FontWeight::*;
106
107        if self < _400 {
108            _400
109        } else if self < _600 {
110            _700
111        } else {
112            _900
113        }
114    }
115
116    /// Returns the lighter weight
117    pub fn lighter(self) -> Self {
118        use self::FontWeight::*;
119
120        if self < _600 {
121            _100
122        } else if self < _800 {
123            _400
124        } else {
125            _700
126        }
127    }
128
129    pub const normal: FontWeight = FontWeight::_400;
130
131    pub const bold: FontWeight = FontWeight::_700;
132}