summaryrefslogtreecommitdiff
path: root/lib/Graphics/Fountainhead/Type.hs
blob: beaf6e438e517af6dba5b7ce531597237b46d235 (plain)
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
{- This Source Code Form is subject to the terms of the Mozilla Public License,
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
   obtain one at https://mozilla.org/MPL/2.0/. -}

-- | Generic font types.
module Graphics.Fountainhead.Type
    ( F2Dot14(..)
    , Fixed32(..)
    , FWord
    , UFWord
    , fixed2Double
    , newlineBuilder
    , succIntegral
    , ttfEpoch
    ) where

import qualified Data.Text.Lazy.Builder as Text.Builder
import Data.Bits ((.>>.), (.&.))
import Data.Int (Int16)
import Data.Word (Word16, Word32)
import Data.Time (Day(..))
import Data.Time.Calendar.OrdinalDate (fromOrdinalDate)

newtype Fixed32 = Fixed32 Word32
    deriving (Eq, Show)

newtype F2Dot14 = F2Dot14 Int16
    deriving (Eq, Show)

type FWord = Int16
type UFWord = Word16

ttfEpoch :: Day
ttfEpoch = fromOrdinalDate 1904 1

succIntegral :: Integral a => a -> Int
succIntegral = succ . fromIntegral

fixed2Double :: F2Dot14 -> Double
fixed2Double (F2Dot14 fixed) =
    let mantissa = realToFrac (fixed .>>. 14)
        fraction = realToFrac (fixed .&. 0x3fff) / 16384.0
     in mantissa + fraction

newlineBuilder :: Text.Builder.Builder
newlineBuilder = Text.Builder.singleton '\n'