~alcinnz/fontconfig-pure

ref: bea597e765a8a1b1098fa7aba31d109aa8d6e507 fontconfig-pure/lib/Graphics/Text/Font/Choose/Range.hs -rw-r--r-- 714 bytes
bea597e7 — Adrian Cochrane Write init/finalize & pattern language bindings, haskell-side! 8 months ago
                                                                                
4da6f787 Adrian Cochrane
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module Graphics.Text.Font.Choose.Range where

import Data.MessagePack (MessagePack(..), Object(..))
import qualified Data.Vector as V
import qualified Data.IntMap as IM

-- | Matches a numeric range.
data Range = Range Double Double deriving (Eq, Show, Ord)
-- | Matches an integral range.
iRange :: Int -> Int -> Range
iRange i j = toEnum i `Range` toEnum j

instance MessagePack Range where
    toObject (Range start end) = ObjectMap $ V.fromList [
        (ObjectInt 0, ObjectDouble start),
        (ObjectInt 1, ObjectDouble end)
      ]
    fromObject msg
        | Just msg' <- fromObject msg =
            Just (IM.findWithDefault 0 0 msg' `Range` IM.findWithDefault 0 1 msg')
        | otherwise = Nothing