~alcinnz/fontconfig-pure

ref: dbefdc068d0644a778f12cc661e87a4086429505 fontconfig-pure/lib/Graphics/Text/Font/Choose/Range.hs -rw-r--r-- 1.1 KiB
dbefdc06 — Adrian Cochrane Document modules. 6 months ago
                                                                                
40a431c7 Adrian Cochrane
dbefdc06 Adrian Cochrane
58463bff Adrian Cochrane
4da6f787 Adrian Cochrane
40a431c7 Adrian Cochrane
4da6f787 Adrian Cochrane
40a431c7 Adrian Cochrane
4da6f787 Adrian Cochrane
40a431c7 Adrian Cochrane
58463bff Adrian Cochrane
40a431c7 Adrian Cochrane
58463bff Adrian Cochrane
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
{-# LANGUAGE DeriveGeneric #-}
-- | A range between 2 values.
module Graphics.Text.Font.Choose.Range(Range(..), iRange, validRange) where

import Data.MessagePack (MessagePack(..), Object(..))
import Test.QuickCheck (Arbitrary(..))
import GHC.Generics (Generic(..))
import Data.Hashable (Hashable(..))
import qualified Data.Vector as V
import qualified Data.IntMap as IM

-- | Matches a numeric range.
data Range = Range Double Double deriving (Eq, Read, Show, Ord, Generic)
-- | 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
instance Arbitrary Range where
    arbitrary = do
        (a, b) <- arbitrary
        return $ Range a $ a + abs b + 1
instance Hashable Range

validRange :: Range -> Bool
validRange (Range start end) = start < end