<commit_before>
{-# LANGUAGE RecordWildCards, LambdaCase, BangPatterns #-}

module FontRendering ( withFontRenderer
                     , FontRenderer
                     , drawText
                     , drawTextSimple
                     , drawTextBitmap
                     , gatherCacheStats
                     , TextLayout(..)
                       -- Wrap TextureAtlas exports
                     , debugDumpAtlas
                       -- Pass through (or wrap) some FT2 exports
                     , debugPrintTest
                     , getFT2Version
                     , loadTypeface
                     , FT2.FT2Exception(..)
                     , getLoadedTypeface
                     , FT2.Typeface
                     ) where

import qualified Graphics.Rendering.OpenGL as GL
import Control.Monad
import Control.Exception
import Control.Applicative
import qualified Data.Vector.Storable as VS
import qualified Data.HashMap.Strict as HM
import Data.Hashable
import Data.IORef
import Data.Bits
import Data.Word
import Data.Maybe
import Data.List
import Data.Function
import Text.Printf

import qualified FT2Interface as FT2
import qualified TextureAtlas as TA
import GLHelpers
import QuadRendering

-- OpenGL font rendering based on the FreeType 2 wrapper in FT2Interface

-- TODO: Add support for a fallback typeface which we use when another font lacks a glyph
--
-- TODO: Replace four floats with a rectangle type, also do so in other modules
--
-- TODO: Text layout is fairly slow, cache and / or speed up
--
-- TODO: Investigate kerning and grid fitting issues again, make sure we really got it right

data FontRenderer = FontRenderer
    { frFT2              :: !FT2.FT2Library
    , frTexAtlas         :: !TA.TextureAtlas
    , frGlyphCache       :: !(IORef (HM.HashMap GlyphCacheKey GlyphCacheEntry))
    , frKernCache        :: !(IORef (HM.HashMap KernCacheKey  Float          ))
    , frDefForceAutohint :: !Bool
    , frDefDisableKern   :: !Bool
    , frUseTexAtlas      :: !Bool
    }

withFontRenderer :: Bool -> Bool -> Bool -> Int -> (FontRenderer -> IO a) -> IO a
withFontRenderer frDefForceAutohint frDefDisableKern frUseTexAtlas atlasTexSize f =
    FT2.withFT2 $ \frFT2 -> -- We initialize our own FT2 library
        TA.withTextureAtlas atlasTexSize
                            1
                            GL.Alpha
                            GL.Alpha8
                            GL.UnsignedByte
                            (0 :: Word8)
                            TFMinMag<commit_msg>Added TODO<commit_after>
{-# LANGUAGE RecordWildCards, LambdaCase, BangPatterns #-}

module FontRendering ( withFontRenderer
                     , FontRenderer
                     , drawText
                     , drawTextSimple
                     , drawTextBitmap
                     , gatherCacheStats
                     , TextLayout(..)
                       -- Wrap TextureAtlas exports
                     , debugDumpAtlas
                       -- Pass through (or wrap) some FT2 exports
                     , debugPrintTest
                     , getFT2Version
                     , loadTypeface
                     , FT2.FT2Exception(..)
                     , getLoadedTypeface
                     , FT2.Typeface
                     ) where

import qualified Graphics.Rendering.OpenGL as GL
import Control.Monad
import Control.Exception
import Control.Applicative
import qualified Data.Vector.Storable as VS
import qualified Data.HashMap.Strict as HM
import Data.Hashable
import Data.IORef
import Data.Bits
import Data.Word
import Data.Maybe
import Data.List
import Data.Function
import Text.Printf

import qualified FT2Interface as FT2
import qualified TextureAtlas as TA
import GLHelpers
import QuadRendering

-- OpenGL font rendering based on the FreeType 2 wrapper in FT2Interface

-- TODO: Add support for a fallback typeface which we use when another font lacks a glyph
--
-- TODO: Replace four floats with a rectangle type, also do so in other modules
--
-- TODO: Text layout is fairly slow, cache and / or speed up
--
-- TODO: Investigate kerning and grid fitting issues again, make sure we really got it right
--
-- TODO: Add scaling factor to draw text with a different size than what the font is set at

data FontRenderer = FontRenderer
    { frFT2              :: !FT2.FT2Library
    , frTexAtlas         :: !TA.TextureAtlas
    , frGlyphCache       :: !(IORef (HM.HashMap GlyphCacheKey GlyphCacheEntry))
    , frKernCache        :: !(IORef (HM.HashMap KernCacheKey  Float          ))
    , frDefForceAutohint :: !Bool
    , frDefDisableKern   :: !Bool
    , frUseTexAtlas      :: !Bool
    }

withFontRenderer :: Bool -> Bool -> Bool -> Int -> (FontRenderer -> IO a) -> IO a
withFontRenderer frDefForceAutohint frDefDisableKern frUseTexAtlas atlasTexSize f =
    FT2.withFT2 $ \frFT2 -> -- We initialize our own FT2 library
        TA.withTextureAtlas atlasTexSize
                            1
                            GL.Alpha
                            GL.Alpha8
                            GL.UnsignedByte
                            (0 :: Word8)
                            TFMinMag
{'fraction_non_alphanumeric': 0.06179775280898876, 'fraction_numerical': 0.0077489345215032935, 'mean_word_length': 1.7173684210526317, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '2808290', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1300, 'n_words': 430}