@@ -16,9 +16,7 @@ throwing an 'SDLException' in case it encounters an error.
1616
1717-}
1818
19- {-# LANGUAGE DeriveGeneric #-}
20- {-# LANGUAGE LambdaCase #-}
21- {-# LANGUAGE OverloadedStrings #-}
19+ {-# LANGUAGE CPP, DeriveGeneric, LambdaCase, OverloadedStrings #-}
2220
2321module SDL.Font
2422 (
@@ -86,31 +84,52 @@ module SDL.Font
8684 , blendedWrapped
8785 ) where
8886
89- import Control.Exception (throwIO )
90- import Control.Monad (unless )
87+ import Control.Exception (throwIO )
88+ import Control.Monad (unless )
9189import Control.Monad.IO.Class (MonadIO , liftIO )
92- import Data.Bits ((.&.) , (.|.) )
93- import Data.ByteString (ByteString )
94- import Data.ByteString.Unsafe (unsafeUseAsCStringLen , unsafePackCString )
95- import Data.Text (Text )
96- import Data.Text.Encoding (decodeUtf8 )
97- import Data.Text.Foreign (lengthWord16 , unsafeCopyToPtr )
98- import Data.Word (Word8 , Word16 )
99- import Foreign.C.String (CString , withCString )
100- import Foreign.C.Types (CUShort , CInt )
101- import Foreign.Marshal.Alloc (allocaBytes , alloca )
102- import Foreign.Marshal.Utils (with , fromBool , toBool )
103- import Foreign.Ptr (Ptr , castPtr , nullPtr )
104- import Foreign.Storable (peek , pokeByteOff )
105- import GHC.Generics (Generic )
106- import SDL (Surface (.. ), SDLException (SDLCallFailed ))
90+ import Data.Bits ((.&.) , (.|.) )
91+ import Data.ByteString (ByteString )
92+ import Data.ByteString.Unsafe (unsafePackCString , unsafeUseAsCStringLen )
93+ import Data.Text (Text )
94+ import Data.Text.Encoding (decodeUtf8 )
95+ import Data.Word (Word8 )
96+ import Foreign.C.String (CString )
97+ import Foreign.C.Types (CInt )
98+ import Foreign.Marshal.Alloc (alloca )
99+ import Foreign.Marshal.Utils (fromBool , toBool , with )
100+ import Foreign.Ptr (Ptr , castPtr , nullPtr )
101+ import Foreign.Storable (peek )
102+ import GHC.Generics (Generic )
103+ import SDL (SDLException (SDLCallFailed ), Surface (.. ))
107104import SDL.Internal.Exception
108- import SDL.Raw.Filesystem (rwFromConstMem )
109- import SDL.Vect (V4 (.. ))
105+ import SDL.Raw.Filesystem (rwFromConstMem )
106+ import SDL.Vect (V4 (.. ))
110107
108+ import qualified Foreign.C.String
111109import qualified SDL.Raw
112110import qualified SDL.Raw.Font
113111
112+ -- stolen from https://github.com/haskell-game/dear-imgui.hs/blob/main/src/DearImGui/Internal/Text.hs
113+ #if MIN_VERSION_text(2,0,1)
114+
115+ import qualified Data.Text.Foreign
116+
117+ withCString :: Text -> (CString -> IO a ) -> IO a
118+ withCString = Data.Text.Foreign. withCString
119+
120+ #else
121+
122+ import qualified Data.Text
123+ import qualified GHC.Foreign
124+ import qualified System.IO
125+
126+ withCString :: Text -> (CString -> IO a ) -> IO a
127+ withCString t action = do
128+ GHC.Foreign. withCString System.IO. utf8 (Data.Text. unpack t) $ \ textPtr ->
129+ action textPtr
130+
131+ #endif
132+
114133-- | Gets the major, minor, patch versions of the linked @SDL2_ttf@ library.
115134--
116135-- You may call this without initializing the library with 'initialize'.
@@ -151,7 +170,7 @@ load :: MonadIO m => FilePath -> PointSize -> m Font
151170load path pts =
152171 fmap Font .
153172 throwIfNull " SDL.Font.load" " TTF_OpenFont" .
154- liftIO . withCString path $
173+ liftIO . Foreign.C.String. withCString path $
155174 flip SDL.Raw.Font. openFont $ fromIntegral pts
156175
157176-- | Same as 'load', but accepts a 'ByteString' containing a font instead.
@@ -175,7 +194,7 @@ loadIndex :: MonadIO m => FilePath -> PointSize -> Index -> m Font
175194loadIndex path pts i =
176195 fmap Font .
177196 throwIfNull " SDL.Font.loadIndex" " TTF_OpenFontIndex" .
178- liftIO . withCString path $ \ cpath ->
197+ liftIO . Foreign.C.String. withCString path $ \ cpath ->
179198 SDL.Raw.Font. openFontIndex cpath (fromIntegral pts) (fromIntegral i)
180199
181200-- | Same as 'loadIndex', but accepts a 'ByteString' containing a font instead.
@@ -205,10 +224,10 @@ unmanaged p = Surface p Nothing
205224solid :: MonadIO m => Font -> Color -> Text -> m SDL. Surface
206225solid (Font font) (V4 r g b a) text =
207226 fmap unmanaged .
208- throwIfNull " SDL.Font.solid" " TTF_RenderUNICODE_Solid " .
209- liftIO . withText text $ \ ptr ->
227+ throwIfNull " SDL.Font.solid" " TTF_RenderUTF8_Solid " .
228+ liftIO . withCString text $ \ ptr ->
210229 with (SDL.Raw. Color r g b a) $ \ fg ->
211- SDL.Raw.Font. renderUNICODE_Solid font (castPtr ptr) fg
230+ SDL.Raw.Font. renderUTF8_Solid font (castPtr ptr) fg
212231
213232-- | Uses the /slow and nice, but with a solid box/ method.
214233--
@@ -220,11 +239,11 @@ solid (Font font) (V4 r g b a) text =
220239shaded :: MonadIO m => Font -> Color -> Color -> Text -> m SDL. Surface
221240shaded (Font font) (V4 r g b a) (V4 r2 g2 b2 a2) text =
222241 fmap unmanaged .
223- throwIfNull " SDL.Font.shaded" " TTF_RenderUNICODE_Shaded " .
224- liftIO . withText text $ \ ptr ->
242+ throwIfNull " SDL.Font.shaded" " TTF_RenderUTF8_Shaded " .
243+ liftIO . withCString text $ \ ptr ->
225244 with (SDL.Raw. Color r g b a) $ \ fg ->
226245 with (SDL.Raw. Color r2 g2 b2 a2) $ \ bg ->
227- SDL.Raw.Font. renderUNICODE_Shaded font (castPtr ptr) fg bg
246+ SDL.Raw.Font. renderUTF8_Shaded font (castPtr ptr) fg bg
228247
229248-- | The /slow slow slow, but ultra nice over another image/ method, 'blended'
230249-- renders text at high quality.
@@ -237,21 +256,10 @@ shaded (Font font) (V4 r g b a) (V4 r2 g2 b2 a2) text =
237256blended :: MonadIO m => Font -> Color -> Text -> m SDL. Surface
238257blended (Font font) (V4 r g b a) text =
239258 fmap unmanaged .
240- throwIfNull " SDL.Font.blended" " TTF_RenderUNICODE_Blended " .
241- liftIO . withText text $ \ ptr ->
259+ throwIfNull " SDL.Font.blended" " TTF_RenderUTF8_Blended " .
260+ liftIO . withCString text $ \ ptr ->
242261 with (SDL.Raw. Color r g b a) $ \ fg ->
243- SDL.Raw.Font. renderUNICODE_Blended font (castPtr ptr) fg
244-
245- -- Analogous to Data.Text.Foreign.useAsPtr, just appends a null-byte.
246- -- FIXME: Is this even necessary?
247- withText :: Text -> (Ptr Word16 -> IO a ) -> IO a
248- withText text act =
249- allocaBytes len $ \ ptr -> do
250- unsafeCopyToPtr text ptr
251- pokeByteOff ptr (len - 2 ) (0 :: CUShort )
252- act ptr
253- where
254- len = 2 * (lengthWord16 text + 1 )
262+ SDL.Raw.Font. renderUTF8_Blended font (castPtr ptr) fg
255263
256264-- Helper function for converting a bitmask into a list of values.
257265fromMaskWith :: (Enum a , Bounded a ) => (a -> CInt ) -> CInt -> [a ]
@@ -464,18 +472,18 @@ glyphMetrics (Font font) ch =
464472size :: MonadIO m => Font -> Text -> m (Int , Int )
465473size (Font font) text =
466474 liftIO .
467- withText text $ \ ptr ->
475+ withCString text $ \ ptr ->
468476 alloca $ \ w ->
469477 alloca $ \ h ->
470- SDL.Raw.Font. sizeUNICODE font (castPtr ptr) w h
478+ SDL.Raw.Font. sizeUTF8 font (castPtr ptr) w h
471479 >>= \ case
472480 0 -> do
473481 w' <- fromIntegral <$> peek w
474482 h' <- fromIntegral <$> peek h
475483 return (w', h')
476484 _ -> do
477485 err <- getError
478- throwIO $ SDLCallFailed " SDL.Font.size" " TTF_SizeUNICODE " err
486+ throwIO $ SDLCallFailed " SDL.Font.size" " TTF_SizeUTF8 " err
479487
480488-- | Same as 'solid', but renders a single glyph instead.
481489solidGlyph :: MonadIO m => Font -> Color -> Char -> m SDL. Surface
@@ -505,16 +513,16 @@ blendedGlyph (Font font) (V4 r g b a) ch =
505513 with (SDL.Raw. Color r g b a) $ \ fg ->
506514 SDL.Raw.Font. renderGlyph_Blended font (fromChar ch) fg
507515
508- -- | Same as 'blended', but renders across multiple lines.
516+ -- | Same as 'blended', but renders across multiple lines.
509517-- Text is wrapped to multiple lines on line endings and on word boundaries
510518-- if it extends beyond wrapLength in pixels.
511519blendedWrapped :: MonadIO m => Font -> Color -> Int -> Text -> m SDL. Surface
512520blendedWrapped (Font font) (V4 r g b a) wrapLength text =
513521 fmap unmanaged .
514- throwIfNull " SDL.Font.blended" " TTF_RenderUNICODE_Blended_Wrapped " .
515- liftIO . withText text $ \ ptr ->
522+ throwIfNull " SDL.Font.blended" " TTF_RenderUTF8_Blended_Wrapped " .
523+ liftIO . withCString text $ \ ptr ->
516524 with (SDL.Raw. Color r g b a) $ \ fg ->
517- SDL.Raw.Font. renderUNICODE_Blended_Wrapped font (castPtr ptr) fg $ fromIntegral wrapLength
525+ SDL.Raw.Font. renderUTF8_Blended_Wrapped font (castPtr ptr) fg $ fromIntegral wrapLength
518526
519527-- | From a given 'Font' get the kerning size of two glyphs.
520528getKerningSize :: MonadIO m => Font -> Index -> Index -> m Int
0 commit comments