Skip to content

Commit c8ce8b3

Browse files
committed
Take withCString from new Text
1 parent 99f2484 commit c8ce8b3

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

sdl2-ttf.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ library
4545
bytestring >= 0.10.4.0,
4646
sdl2 >= 2.2,
4747
template-haskell,
48-
text >= 1.1.0.0,
48+
text >= 1.1.0.0 && < 2 || >= 2.0.1,
4949
th-abstraction >= 0.4.0.0,
5050
transformers >= 0.4
5151

src/SDL/Font.hs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2121
module SDL.Font
2222
(
@@ -90,11 +90,11 @@ import Control.Monad.IO.Class (MonadIO, liftIO)
9090
import Data.Bits ((.&.), (.|.))
9191
import Data.ByteString (ByteString)
9292
import Data.ByteString.Unsafe (unsafePackCString, unsafeUseAsCStringLen)
93-
import Data.Text (Text)
93+
import Data.Text (Text, unpack)
9494
import Data.Text.Encoding (decodeUtf8)
9595
import Data.Text.Foreign (lengthWord16, unsafeCopyToPtr)
9696
import Data.Word (Word16, Word8)
97-
import Foreign.C.String (CString, withCString)
97+
import Foreign.C.String (CString)
9898
import Foreign.C.Types (CInt, CUShort)
9999
import Foreign.Marshal.Alloc (alloca, allocaBytes)
100100
import Foreign.Marshal.Utils (fromBool, toBool, with)
@@ -105,10 +105,29 @@ import SDL (SDLException (SDLCallFailed), Surface (..))
105105
import SDL.Internal.Exception
106106
import SDL.Raw.Filesystem (rwFromConstMem)
107107
import 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
109113
import qualified SDL.Raw
110114
import 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
149168
load 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
173192
loadIndex 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

Comments
 (0)