@@ -16,7 +16,7 @@ throwing an 'SDLException' in case it encounters an error.
1616
1717-}
1818
19- {-# LANGUAGE DeriveGeneric, LambdaCase, OverloadedStrings #-}
19+ {-# LANGUAGE CPP, DeriveGeneric, LambdaCase, OverloadedStrings #-}
2020
2121module SDL.Font
2222 (
@@ -90,11 +90,11 @@ import Control.Monad.IO.Class (MonadIO, liftIO)
9090import Data.Bits ((.&.) , (.|.) )
9191import Data.ByteString (ByteString )
9292import Data.ByteString.Unsafe (unsafePackCString , unsafeUseAsCStringLen )
93- import Data.Text (Text )
93+ import Data.Text (Text , unpack )
9494import Data.Text.Encoding (decodeUtf8 )
9595import Data.Text.Foreign (lengthWord16 , unsafeCopyToPtr )
9696import Data.Word (Word16 , Word8 )
97- import Foreign.C.String (CString , withCString )
97+ import Foreign.C.String (CString )
9898import Foreign.C.Types (CInt , CUShort )
9999import Foreign.Marshal.Alloc (alloca , allocaBytes )
100100import Foreign.Marshal.Utils (fromBool , toBool , with )
@@ -105,10 +105,29 @@ import SDL (SDLException (SDLCallFailed), Surface (..))
105105import SDL.Internal.Exception
106106import SDL.Raw.Filesystem (rwFromConstMem )
107107import SDL.Vect (V4 (.. ))
108+ import System.IO (utf8 )
108109
110+ import qualified Data.Text.Foreign
111+ import qualified Foreign.C.String
112+ import qualified GHC.Foreign
109113import qualified SDL.Raw
110114import qualified SDL.Raw.Font
111115
116+ -- stolen from https://github.com/haskell-game/dear-imgui.hs/blob/main/src/DearImGui/Internal/Text.hs
117+ #if MIN_VERSION_text(2,0,1)
118+
119+ withCString :: Text -> (CString -> IO a ) -> IO a
120+ withCString = Data.Text.Foreign. withCString
121+
122+ #else
123+
124+ withCString :: Text -> (CString -> IO a ) -> IO a
125+ withCString t action = do
126+ GHC.Foreign. withCString utf8 (unpack t) $ \ textPtr ->
127+ action textPtr
128+
129+ #endif
130+
112131-- | Gets the major, minor, patch versions of the linked @SDL2_ttf@ library.
113132--
114133-- You may call this without initializing the library with 'initialize'.
@@ -149,7 +168,7 @@ load :: MonadIO m => FilePath -> PointSize -> m Font
149168load path pts =
150169 fmap Font .
151170 throwIfNull " SDL.Font.load" " TTF_OpenFont" .
152- liftIO . withCString path $
171+ liftIO . Foreign.C.String. withCString path $
153172 flip SDL.Raw.Font. openFont $ fromIntegral pts
154173
155174-- | Same as 'load', but accepts a 'ByteString' containing a font instead.
@@ -173,7 +192,7 @@ loadIndex :: MonadIO m => FilePath -> PointSize -> Index -> m Font
173192loadIndex path pts i =
174193 fmap Font .
175194 throwIfNull " SDL.Font.loadIndex" " TTF_OpenFontIndex" .
176- liftIO . withCString path $ \ cpath ->
195+ liftIO . Foreign.C.String. withCString path $ \ cpath ->
177196 SDL.Raw.Font. openFontIndex cpath (fromIntegral pts) (fromIntegral i)
178197
179198-- | Same as 'loadIndex', but accepts a 'ByteString' containing a font instead.
0 commit comments