diff --git a/sdl2-ttf.cabal b/sdl2-ttf.cabal index 2f20ab9..42736c1 100644 --- a/sdl2-ttf.cabal +++ b/sdl2-ttf.cabal @@ -45,7 +45,7 @@ library bytestring >= 0.10.4.0, sdl2 >= 2.2, template-haskell, - text >= 1.1.0.0, + text >= 1.1.0.0 && < 2 || >= 2.0.1, th-abstraction >= 0.4.0.0, transformers >= 0.4 diff --git a/src/SDL/Font.hs b/src/SDL/Font.hs index ecb1b72..6cd4c5a 100644 --- a/src/SDL/Font.hs +++ b/src/SDL/Font.hs @@ -16,7 +16,7 @@ throwing an 'SDLException' in case it encounters an error. -} -{-# LANGUAGE DeriveGeneric, LambdaCase, OverloadedStrings #-} +{-# LANGUAGE CPP, DeriveGeneric, LambdaCase, OverloadedStrings #-} module SDL.Font ( @@ -90,11 +90,11 @@ import Control.Monad.IO.Class (MonadIO, liftIO) import Data.Bits ((.&.), (.|.)) import Data.ByteString (ByteString) import Data.ByteString.Unsafe (unsafePackCString, unsafeUseAsCStringLen) -import Data.Text (Text) +import Data.Text (Text, unpack) import Data.Text.Encoding (decodeUtf8) import Data.Text.Foreign (lengthWord16, unsafeCopyToPtr) import Data.Word (Word16, Word8) -import Foreign.C.String (CString, withCString) +import Foreign.C.String (CString) import Foreign.C.Types (CInt, CUShort) import Foreign.Marshal.Alloc (alloca, allocaBytes) import Foreign.Marshal.Utils (fromBool, toBool, with) @@ -105,10 +105,29 @@ import SDL (SDLException (SDLCallFailed), Surface (..)) import SDL.Internal.Exception import SDL.Raw.Filesystem (rwFromConstMem) import SDL.Vect (V4 (..)) +import System.IO (utf8) +import qualified Data.Text.Foreign +import qualified Foreign.C.String +import qualified GHC.Foreign import qualified SDL.Raw import qualified SDL.Raw.Font +-- stolen from https://github.com/haskell-game/dear-imgui.hs/blob/main/src/DearImGui/Internal/Text.hs +#if MIN_VERSION_text(2,0,1) + +withCString :: Text -> (CString -> IO a) -> IO a +withCString = Data.Text.Foreign.withCString + +#else + +withCString :: Text -> (CString -> IO a) -> IO a +withCString t action = do + GHC.Foreign.withCString utf8 (unpack t) $ \textPtr -> + action textPtr + +#endif + -- | Gets the major, minor, patch versions of the linked @SDL2_ttf@ library. -- -- You may call this without initializing the library with 'initialize'. @@ -149,7 +168,7 @@ load :: MonadIO m => FilePath -> PointSize -> m Font load path pts = fmap Font . throwIfNull "SDL.Font.load" "TTF_OpenFont" . - liftIO . withCString path $ + liftIO . Foreign.C.String.withCString path $ flip SDL.Raw.Font.openFont $ fromIntegral pts -- | Same as 'load', but accepts a 'ByteString' containing a font instead. @@ -173,7 +192,7 @@ loadIndex :: MonadIO m => FilePath -> PointSize -> Index -> m Font loadIndex path pts i = fmap Font . throwIfNull "SDL.Font.loadIndex" "TTF_OpenFontIndex" . - liftIO . withCString path $ \cpath -> + liftIO . Foreign.C.String.withCString path $ \cpath -> SDL.Raw.Font.openFontIndex cpath (fromIntegral pts) (fromIntegral i) -- | Same as 'loadIndex', but accepts a 'ByteString' containing a font instead.