<commit_before>-- | Bindings for markup
{-# LANGUAGE RecordWildCards #-}
module Graphics.FreetypeGL.Markup
    ( Markup(..), def
    , RGBA(..)
    , withMarkupPtr
    ) where

import qualified Bindings.FreetypeGL.Markup as MU
import           Bindings.FreetypeGL.Vec234 (C'vec4(..))
import           Foreign.Marshal.Alloc (alloca)
import           Foreign.Ptr (Ptr, nullPtr)
import           Foreign.Storable (Storable(..))
import           Graphics.FreetypeGL.TextureFont (TextureFont(..))
import qualified Graphics.FreetypeGL.TextureFont as TF

data RGBA = RGBA Float Float Float Float

-- We intentionally omit the family,size,bold,italic fields which
-- really don't belong in Markup, and are used to select a font. We
-- prefer to load fonts via file paths directly.

-- Ignoring the 'rise' field which is ignored by
-- the library
data Markup = Markup
    { spacing :: !Float
    , gamma :: !Float
    , foregroundColor :: !RGBA
    , backgroundColor :: !RGBA
    , outline :: !Bool
    , outlineColor :: !RGBA
    , underline :: !Bool
    , underlineColor :: !RGBA
    , overline :: !Bool
    , overlineColor :: !RGBA
    , strikethrough :: !Bool
    , strikethroughColor :: !RGBA
    }

def :: Markup
def =
    Markup
    { spacing = 0.0
    , gamma = 1.0
    , foregroundColor = white
    , backgroundColor = noColor
    , outline = False
    , outlineColor = noColor
    , underline = False
    , underlineColor = noColor
    , overline = False
    , overlineColor = noColor
    , strikethrough = False
    , strikethroughColor = noColor
    }
    where
        white = RGBA 1.0 1.0 1.0 1.0
        noColor = RGBA 0.0 0.0 0.0 0.0

rgbaToVec4 :: RGBA -> C'vec4
rgbaToVec4 (RGBA r g b a) =
    C'vec4
    (realToFrac r)
    (realToFrac g)
    (realToFrac b)
    (realToFrac a)

withMarkupPtr :: Markup -> TextureFont -> (Ptr MU.C'markup_t -> IO a) -> IO a
withMarkupPtr Markup{..} font@(TextureFont fontPtr) act =
    alloca $ \ptr ->
    do
        size <- TF.size font
        let cMarkup =
                MU.C'markup_t
                -- family,bold,italic unneeded, we always give a font:
                nullPtr (realToFrac size) 0 0
                0 -- rise is ignored
                (realToFrac spacing)
                (realToFrac gamma)
                (rgbaToVec4 foregroundColor)
                (rgbaToVec4 backgroundColor)
                (asCBool outline)
                (rgbaToVec4 outlineColor)
                (asCBool underline)
                (rgbaToVec4 underlineColor)
                (asCBool overline)
                (rgbaToVec4 overlineColor)
                (asCBool strikethrough)
                (rgbaToVec4 strikethroughColor)
                fontPtr
        poke ptr cMarkup
        act ptr
    where
        asCBool = fromIntegral . fromEnum<commit_msg>Use Maybe Colors instead of Bool, Color<commit_after>-- | Bindings for markup
{-# LANGUAGE RecordWildCards #-}
module Graphics.FreetypeGL.Markup
    ( Markup(..), def
    , RGBA(..)
    , withMarkupPtr
    ) where

import qualified Bindings.FreetypeGL.Markup as MU
import           Bindings.FreetypeGL.Vec234 (C'vec4(..))
import           Data.Function ((&))
import           Data.Maybe (fromMaybe)
import           Data.Maybe (isJust)
import           Foreign.C.Types (CInt)
import           Foreign.Marshal.Alloc (alloca)
import           Foreign.Ptr (Ptr, nullPtr)
import           Foreign.Storable (Storable(..))
import           Graphics.FreetypeGL.TextureFont (TextureFont(..))
import qualified Graphics.FreetypeGL.TextureFont as TF

data RGBA = RGBA Float Float Float Float

-- We intentionally omit the family,size,bold,italic fields which
-- really don't belong in Markup, and are used to select a font. We
-- prefer to load fonts via file paths directly.

-- Ignoring the 'rise' field which is ignored by
-- the library
data Markup = Markup
    { spacing :: !Float
    , gamma :: !Float
    , foregroundColor :: !RGBA
    , backgroundColor :: !RGBA
    , outlineColor :: !(Maybe RGBA)
    , underlineColor :: !(Maybe RGBA)
    , overlineColor :: !(Maybe RGBA)
    , strikethroughColor :: !(Maybe RGBA)
    }

def :: Markup
def =
    Markup
    { spacing = 0.0
    , gamma = 1.0
    , foregroundColor = white
    , backgroundColor = noColor
    , outlineColor = Nothing
    , underlineColor = Nothing
    , overlineColor = Nothing
    , strikethroughColor = Nothing
    }
    where
        white = RGBA 1.0 1.0 1.0 1.0

noColor :: RGBA
noColor = RGBA 0.0 0.0 0.0 0.0

rgbaToVec4 :: RGBA -> C'vec4
rgbaToVec4 (RGBA r g b a) =
    C'vec4
    (realToFrac r)
    (realToFrac g)
    (realToFrac b)
    (realToFrac a)

withMarkupPtr :: Markup -> TextureFont -> (Ptr MU.C'markup_t -> IO a) -> IO a
withMarkupPtr Markup{..} font@(TextureFont fontPtr) act =
    alloca $ \ptr ->
    do
        size <- TF.size font
        let cMarkup =
                MU.C'markup_t
                -- family,bold,italic unneeded, we always give a font:
                nullPtr (realToFrac size) 0 0
                0 -- rise is ignored
                (realToFrac spacing)
                (realToFrac gamma)
                (rgbaToVec4 foregroundColor)
                (rgbaToVec4 backgroundColor)
                & passColorAsCParams outlineColor
                & passColorAsCParams underlineColor
                & passColorAsCParams overlineColor
                & passColorAsCParams strikethroughColor
                & ($ fontPtr)
        poke ptr cMarkup
        act ptr

passColorAsCParams :: Maybe RGBA -> (CInt -> C'vec4 -> a) -> a
passColorAsCParams mColor f =
    f
    ((fromIntegral . fromEnum . isJust) mColor)
    (rgbaToVec4 (fromMaybe noColor mColor))
{'fraction_non_alphanumeric': 0.0943029016277424, 'fraction_numerical': 0.012738853503184714, 'mean_word_length': 2.070613796849538, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4492326', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1768, 'n_words': 499}