From fe4bf6c8999ce29d7cba0c426ae18a176b483b30 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Mon, 28 Nov 2022 21:05:57 +1300 Subject: [PATCH] Language bind CSS 'font-feature-settings' to FontConfig's 'fontfeatures'. --- Graphics/Text/Font/Choose/Pattern.hs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Graphics/Text/Font/Choose/Pattern.hs b/Graphics/Text/Font/Choose/Pattern.hs index daa49a1..c26de01 100644 --- a/Graphics/Text/Font/Choose/Pattern.hs +++ b/Graphics/Text/Font/Choose/Pattern.hs @@ -28,6 +28,7 @@ import Data.CSS.Syntax.Tokens (Token(..), NumericValue(..)) import Data.Text (unpack, Text) import Stylist (PropertyParser(..)) import Data.Scientific (toRealFloat) +import Data.List (intercalate) type Pattern = [(String, [(Binding, Value)])] data Binding = Strong | Weak | Same deriving (Eq, Ord, Enum, Show, Generic) @@ -177,6 +178,18 @@ parseFontFamily (String font:tail) = ([unpack font], True, tail) parseFontFamily (Ident font:tail) = ([unpack font], True, tail) parseFontFamily toks = ([], False, toks) -- Invalid syntax! +parseFontFeatures :: [Token] -> ([(String, Int)], Bool, [Token]) +parseFontFeatures (String feat:toks) | feature@(_:_:_:_:[]) <- unpack feat = case toks of + Comma:tail -> let (feats, b, tail') = parseFontFeatures tail in ((feature, 1):feats, b, tail') + Ident "on":Comma:tail -> let (f, b, t) = parseFontFeatures tail in ((feature, 1):f, b, t) + Ident "on":tail -> ([(feature, 1)], True, tail) + Ident "off":Comma:tail -> let (f, b, t) = parseFontFeatures tail in ((feature, 1):f, b, t) + Ident "off":tail -> ([(feature, 1)], True, tail) + Number _ (NVInteger x):Comma:tail -> + let (feats, b, tail') = parseFontFeatures tail in ((feature, fromEnum x):feats, b, tail') + Number _ (NVInteger x):tail -> ([(feature, fromEnum x)], True, tail) +parseFontFeatures toks = ([], False, toks) + parseLength :: Double -> NumericValue -> Text -> Double parseLength super length unit = convert (nv2double length) unit where @@ -249,4 +262,9 @@ instance PropertyParser Pattern where | ValueInt _ <- getValue "weight" self = Just self -- As bold as it goes... | otherwise = seti "weight" Strong 200 self + longhand _ self "font-feature-settings" [Ident k] | k `elem` ["initial", "normal"] = Just self + longhand _ self "font-feature-settings" toks + | (features, True, []) <- parseFontFeatures toks = + set "fontfeatures" Strong (intercalate "," $ map fst features) self + longhand _ _ _ _ = Nothing -- 2.30.2