~alcinnz/fontconfig-pure

ref: 2fb2b76a7ec0f9f977c2a7c4db1c7117920d9cb4 fontconfig-pure/Graphics/Text/Font/Choose/Range.hs -rw-r--r-- 1.1 KiB
2fb2b76a — Adrian Cochrane Expose convenience APIs for coercing values & pattern properties to a type. 2 years ago
                                                                                
24a77a5f Adrian Cochrane
e21707cb Adrian Cochrane
24a77a5f Adrian Cochrane
5aedd01f Adrian Cochrane
24a77a5f Adrian Cochrane
e21707cb Adrian Cochrane
24a77a5f Adrian Cochrane
e21707cb Adrian Cochrane
5aedd01f Adrian Cochrane
e21707cb Adrian Cochrane
5aedd01f Adrian Cochrane
e21707cb 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
35
36
37
{-# LANGUAGE DeriveGeneric #-}
module Graphics.Text.Font.Choose.Range where

import Foreign.Ptr (Ptr)
import Control.Exception (bracket)
import Foreign.Marshal.Alloc (alloca)
import Foreign.Storable (peek)

import GHC.Generics (Generic)
import Data.Hashable (Hashable)
import Graphics.Text.Font.Choose.Result (throwNull, throwFalse)

data Range = Range Double Double deriving (Eq, Show, Ord, Generic)
iRange i j = toEnum i `Range` toEnum j

instance Hashable Range

------
--- Low-level
------
data Range'
type Range_ = Ptr Range'

withRange :: Range -> (Range_ -> IO a) -> IO a
withRange (Range i j) = bracket (throwNull <$> fcRangeCreateDouble i j) fcRangeDestroy
foreign import ccall "FcRangeCreateDouble" fcRangeCreateDouble ::
    Double -> Double -> IO Range_
foreign import ccall "FcRangeDestroy" fcRangeDestroy :: Range_ -> IO ()

thawRange :: Range_ -> IO Range
thawRange range' = alloca $ \i' -> alloca $ \j' -> do
    throwFalse <$> fcRangeGetDouble range' i' j'
    i <- peek i'
    j <- peek j'
    return $ Range i j
foreign import ccall "FcRangeGetDouble" fcRangeGetDouble ::
    Range_ -> Ptr Double -> Ptr Double -> IO Bool