~alcinnz/fontconfig-pure

ref: 94860b2edf2154be90a772add719a54168bbaa08 fontconfig-pure/lib/Graphics/Text/Font/Choose/Pattern.hs -rw-r--r-- 13.5 KiB
94860b2e — Adrian Cochrane Add docstrings everywhere! 6 months ago
                                                                                
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
{-# LANGUAGE DeriveGeneric, CApiFFI #-}
{-# LANGUAGE OverloadedStrings #-}
-- | Dynamically-typed datastructure describing a font, whether resolved or a query.
-- Can be parsed from CSS.
module Graphics.Text.Font.Choose.Pattern(Pattern, Pattern'(..), module M, Binding(..),
        setValue, setValues, getValue, getValues, equalSubset, defaultSubstitute,
        nameParse, nameUnparse, nameFormat, validPattern, validPattern',
        -- For Graphics.Text.Font.Choose.FontSet
        parseFontStretch, parseFontWeight, parseFontFeatures, parseFontVars) where

import Data.Map as M
import Data.MessagePack (MessagePack(..), Object(..))
import Test.QuickCheck (Arbitrary(..), elements)
import Data.Hashable (Hashable(..))
import GHC.Generics (Generic)

import Foreign.C.String (CString)
import Foreign.Ptr (Ptr)
import Control.Exception (throw)
import Graphics.Text.Font.Choose.Internal.FFI (withMessage, fromMessage0, withCString', peekCString')

import Graphics.Text.Font.Choose.Value
import Graphics.Text.Font.Choose.ObjectSet
import Graphics.Text.Font.Choose.Result
import Graphics.Text.Font.Choose.Weight

import Stylist (PropertyParser(..))
import Data.CSS.Syntax.Tokens (Token(..), NumericValue(..))
import Data.Text (Text, unpack)
import qualified Data.Text as Txt
import Data.Scientific (toRealFloat)
import Data.List (intercalate)
import Data.Maybe as Mb (listToMaybe, fromMaybe, mapMaybe)
import Data.Char (isAscii)
import Prelude as L

-- | Holds both patterns to match against the available fonts, as well as the information about each font.
type Pattern = M.Map Text [(Binding, Value)]
-- | Wrapper around `Pattern` supporting useful typeclasses.
data Pattern' = Pattern' { unPattern :: Pattern } deriving (Eq, Read, Show, Generic)
-- | The precedance for a field of a Pattern.
data Binding = Strong | Weak | Same deriving (Eq, Ord, Enum, Read, Show, Generic)

instance Hashable Binding where
    hash = fromEnum
instance MessagePack Binding where
    fromObject (ObjectBool True) = Just Strong
    fromObject (ObjectBool False) = Just Weak
    fromObject ObjectNil = Just Same
    fromObject _ = Nothing
    toObject Strong = ObjectBool True
    toObject Weak = ObjectBool False
    toObject Same = ObjectNil

instance Hashable Pattern' where hash = hash . unPattern
instance MessagePack Pattern' where
    fromObject = fmap Pattern' . fromObject
    toObject = toObject . unPattern

instance Arbitrary Pattern' where
    -- FIXME: Stop enforcing singletons, without incurring too many invalid patterns!
    arbitrary = Pattern' <$> M.mapKeys normKey <$> M.map (:[]) <$> arbitrary
        where
            normKey = Txt.pack . L.filter (/= '\0') . L.map toAscii . L.take 17
            toAscii :: Char -> Char
            toAscii ch = toEnum $ fromEnum ch `mod` 128
instance Arbitrary Binding where
    arbitrary = elements [Strong, Weak] -- Same doesn't roundtrip!

-- | Does the pattern hold a value we can process?
validPattern :: Pattern -> Bool
validPattern self = not (M.null self) &&
        all (validValue . snd) (concat $ M.elems self) &&
        all (not . L.null) (M.elems self) &&
        all (not . Txt.null) (M.keys self) &&
        all ((/= Same) . fst) (concat $ M.elems self) &&
        all (not . Txt.elem '\0') (M.keys self) &&
        all (Txt.all isAscii) (M.keys self) &&
        all (\k -> Txt.length k < 18) (M.keys self)
-- | Variant of `validPattern` which applies to the `Pattern'` wrapper.
validPattern' :: Pattern' -> Bool
validPattern' = validPattern . unPattern

-- | Replace a field with a singular type-casted value.
setValue :: ToValue v => Text -> Binding -> v -> Pattern -> Pattern
setValue key strength v self = setValues key strength [v] self
-- | Replace a field with multiple type-casted values.
setValues :: ToValue v => Text -> Binding -> [v] -> Pattern -> Pattern
setValues key strength vs self = M.insert key [(strength, toValue v) | v <- vs] self

-- | Retrieve a field's primary type-casted value.
getValue :: ToValue v => Text -> Pattern -> Maybe v
getValue key self = fromValue . snd =<< listToMaybe =<< M.lookup key self
-- | Retrieve a field's type-casted values.
getValues :: ToValue v => Text -> Pattern -> [v]
getValues key self = Mb.mapMaybe (fromValue . snd) $ fromMaybe [] $ M.lookup key self

-- | Returns whether the given patterns have exactly the same values for all of the given objects.
equalSubset :: Pattern -> Pattern -> ObjectSet -> Bool
equalSubset a b os | validPattern a && validPattern b =
    case withMessage fcPatternEqualSubset [toObject a, toObject b, toObject os] of
        0 -> False
        1 -> True
        _ -> throw ErrOOM
  | otherwise = False

foreign import capi "fontconfig-wrap.h" fcPatternEqualSubset :: CString -> Int -> Int

-- | Supplies default values for underspecified font patterns:
-- Patterns without a specified style or weight are set to Medium
-- Patterns without a specified style or slant are set to Roman
-- Patterns without a specified pixel size are given one computed from any specified point size (default 12), dpi (default 75) and scale (default 1).
defaultSubstitute :: Pattern -> Pattern
defaultSubstitute a | validPattern a = fromMessage0 $ withMessage fcDefaultSubstitute a
    | otherwise = a

foreign import capi "fontconfig-wrap.h" fcDefaultSubstitute :: CString -> Int -> Ptr Int -> CString

-- | Converts name from the standard text format described above into a pattern.
nameParse :: String -> Pattern
nameParse = fromMessage0 . withCString' fcNameParse

foreign import capi "fontconfig-wrap.h" fcNameParse :: CString -> Ptr Int -> CString

-- | Converts the given pattern into the standard text format described above.
nameUnparse :: Pattern -> String
nameUnparse a | validPattern a = peekCString' $ withMessage fcNameUnparse a
    | otherwise = ""

foreign import capi "fontconfig-wrap.h" fcNameUnparse :: CString -> Int -> CString

-- | Format a pattern into a string according to a format specifier
-- See https://fontconfig.pages.freedesktop.org/fontconfig/fontconfig-devel/fcpatternformat.html for full details.
nameFormat :: Pattern -> String -> String
nameFormat a b
    | validPattern a = peekCString' $ flip withCString' b $ withMessage fcNameFormat a
    | otherwise = ""

foreign import capi "fontconfig-wrap.h" fcNameFormat :: CString -> Int -> CString -> CString

------
--- CSS
------

parseFontFamily :: [Token] -> ([String], Bool, [Token])
parseFontFamily (String font:Comma:toks) = let (fonts, b, tail') = parseFontFamily toks
    in (unpack font:fonts, b, tail')
parseFontFamily (Ident font:Comma:toks) = let (fonts, b, tail') = parseFontFamily toks
    in (unpack font:fonts, b, tail')
parseFontFamily (String font:toks) = ([unpack font], True, toks)
parseFontFamily (Ident font:toks) = ([unpack font], True, toks)
parseFontFamily toks = ([], False, toks) -- Invalid syntax!

parseFontFeatures :: [Token] -> ([(String, Int)], Bool, [Token])
parseFontFeatures (String feat:toks) | feature@(_:_:_:_:[]) <- unpack feat = case toks of
    Comma:toks' -> let (feats, b, tail') = parseFontFeatures toks' in ((feature, 1):feats, b, tail')
    Ident "on":Comma:toks' -> let (f, b, t) = parseFontFeatures toks' in ((feature, 1):f, b, t)
    Ident "on":toks' -> ([(feature, 1)], True, toks')
    Ident "off":Comma:toks' -> let (f, b, t) = parseFontFeatures toks' in ((feature, 1):f, b, t)
    Ident "off":toks' -> ([(feature, 1)], True, toks')
    Number _ (NVInteger x):Comma:toks' ->
        let (feats, b, tail') = parseFontFeatures toks' in ((feature, fromEnum x):feats, b, tail')
    Number _ (NVInteger x):toks' -> ([(feature, fromEnum x)], True, toks')
    _ -> ([], False, String feat:toks)
parseFontFeatures toks = ([], False, toks)

-- | Parse OpenType variables from CSS syntax.
parseFontVars :: [Token] -> ([(String, Double)], Bool, [Token])
parseFontVars (String var':Number _ x:Comma:toks) | var@(_:_:_:_:[]) <- unpack var' =
    let (vars, b, tail') = parseFontVars toks in ((var, nv2double x):vars, b, tail')
parseFontVars (String var':Number _ x:toks) | var@(_:_:_:_:[]) <- unpack var' =
    ([(var, nv2double x)], True, toks)
parseFontVars toks = ([], False, toks)

parseLength :: Double -> NumericValue -> Text -> Double
parseLength super len unit = convert (nv2double len) unit
  where
    convert = c
    c x "pt" = x -- Unit FontConfig expects!
    c x "pc" = x/6 `c` "in"
    c x "in" = x/72 `c` "pt"
    c x "Q" = x/40 `c` "cm"
    c x "mm" = x/10 `c` "cm"
    c x "cm" = x/2.54 `c` "in"
    c x "px" = x/96 `c` "in" -- Conversion factor during early days of CSS, got entrenched.
    c x "em" = x * super
    c x "%" = x/100 `c` "em"
    c _ _ = 0/0 -- NaN

-- | Parse the CSS font-stretch property.
parseFontStretch :: Token -> Maybe Int -- Result in percentages
parseFontStretch (Percentage _ x) = Just $ fromEnum $ nv2double x
parseFontStretch (Ident "ultra-condensed") = Just 50
parseFontStretch (Ident "extra-condensed") = Just 63 -- 62.5%, but round towards 100%
parseFontStretch (Ident "condensed") = Just 75
parseFontStretch (Ident "semi-condensed") = Just 88 -- 87.5% actually...
parseFontStretch (Ident "normal") = Just 100
parseFontStretch (Ident "initial") = Just 100
parseFontStretch (Ident "semi-expanded") = Just 112 -- 112.5% actually...
parseFontStretch (Ident "expanded") = Just 125
parseFontStretch (Ident "extra-expanded") = Just 150
parseFontStretch (Ident "ultra-expanded") = Just 200
parseFontStretch _ = Nothing

-- Conversion between CSS scale & FontConfig scale is non-trivial, use lookuptable.
-- | Parse the CSS font-weight property.
parseFontWeight :: Token -> Maybe Int
parseFontWeight (Ident k) | k `elem` ["initial", "normal"] = Just 80
parseFontWeight (Ident "bold") = Just 200
parseFontWeight (Number _ (NVInteger x)) = Just $ weightFromOpenType $ fromEnum x
parseFontWeight _ = Nothing

nv2double :: NumericValue -> Double
nv2double (NVInteger x) = fromInteger x
nv2double (NVNumber x) = toRealFloat x

sets :: ToValue v => Text -> Binding -> [v] -> Pattern -> Maybe Pattern
sets a b c d = Just $ setValues a b c d
set :: ToValue v => Text -> Binding -> v -> Pattern -> Maybe Pattern
set a b c d = Just $ setValue a b c d
seti :: Text -> Binding -> Int -> Pattern -> Maybe Pattern
seti a b c d = Just $ setValue a b (c :: Int) d
unset' :: Text -> Pattern -> Maybe Pattern
unset' a b = Just $ M.delete a b

getSize :: Pattern -> Double
getSize pat | Just [(_, ValueDouble x)] <- M.lookup "size" pat = x
    | otherwise = 10

instance PropertyParser Pattern' where
    temp = Pattern' M.empty

    longhand _ (Pattern' self) "font-family" toks
        | (fonts, True, []) <- parseFontFamily toks = Pattern' <$> sets "family" Strong fonts self

    -- font-size: initial should be configurable!
    longhand (Pattern' super) (Pattern' self) "font-size" [Dimension _ x unit]
        | let y = parseLength (getSize super) x unit, not $ isNaN y =
            Pattern' <$> set "size" Strong y self
    longhand super self "font-size" [Percentage x y] =
        longhand super self "font-size" [Dimension x y "%"]

    longhand _ (Pattern' self) "font-style" [Ident "initial"] = Pattern' <$> seti "slant" Strong 0 self
    longhand _ (Pattern' self) "font-style" [Ident "normal"] = Pattern' <$> seti "slant" Strong 0 self
    longhand _ (Pattern' self) "font-style" [Ident "italic"] = Pattern' <$> seti "slant" Strong 100 self
    longhand _ (Pattern' self) "font-style" [Ident "oblique"] = Pattern' <$> seti "slant" Strong 110 self

    -- Conversion between CSS scale & FontConfig scale is non-trivial, use lookuptable.
    -- FIXME: Use Graphics.Text.Font.Choose.Weight!
    longhand _ (Pattern' self) "font-weight" [tok]
        | Just x <- parseFontWeight tok = Pattern' <$> seti "weight" Strong x self
    longhand super self "font-weight" [Number _ (NVInteger x)]
        | x > 920 = longhand super self "font-weight" [Number "" $ NVInteger 950]
        | otherwise = longhand super self "font-weight" [Number "" $ NVInteger $ (x `div` 100) * 100]
    longhand _ (Pattern' self) "font-weight" [Ident "lighter"]
        | Just ((_, ValueInt x):_) <- M.lookup "weight" self, x > 200 =
            Pattern' <$> seti "weight" Strong 200 self
        -- minus 100 adhears to the CSS standard awefully well in this new scale.
        | Just ((_, ValueInt x):_) <- M.lookup "weight" self =
            Pattern' <$> seti "weight" Strong (max (x - 100) 0) self
        | otherwise = Pattern' <$> seti "weight" Strong 0 self
    longhand _ self'@(Pattern' self) "font-weight" [Ident "bolder"]
        | Just ((_, ValueInt x):_) <- M.lookup "weight" self, x <= 65 =
            Pattern' <$> seti "weight" Strong 80 self
        | Just ((_, ValueInt x):_) <- M.lookup "weight" self, x <= 150 =
            Pattern' <$> seti "weight" Strong 200 self
        | Just ((_, ValueInt x):_) <- M.lookup "weight" self, x < 210 =
            Pattern' <$> seti "weight" Strong 210 self
        | Just ((_, ValueInt _):_) <- M.lookup "weight" self = Just self' -- As bold as it goes...
        | otherwise = Pattern' <$> seti "weight" Strong 200 self

    longhand _ (Pattern' self) "font-feature-settings" [Ident k]
        | k `elem` ["initial", "normal"] = Pattern' <$> unset' "fontfeatures" self
    longhand _ (Pattern' self) "font-feature-settings" toks
        | (features, True, []) <- parseFontFeatures toks = Pattern' <$>
            set "fontfeatures" Strong (intercalate "," $ L.map fst features) self

    longhand _ (Pattern' self) "font-variation-settings" [Ident k]
        | k `elem` ["initial", "normal"] = Pattern' <$> unset' "variable" self
    longhand _ (Pattern' self) "font-variation-settings" toks
        | (_, True, []) <- parseFontVars toks = Pattern' <$> set "variable" Strong True self

    longhand _ (Pattern' s) "font-stretch" [tok]
        | Just x <- parseFontStretch tok = Pattern' <$> seti "width" Strong x s

    longhand _ _ _ _ = Nothing