@@ -90,40 +90,42 @@ import Control.Monad.IO.Class (MonadIO, liftIO)
90
90
import Data.Bits ((.&.) , (.|.) )
91
91
import Data.ByteString (ByteString )
92
92
import Data.ByteString.Unsafe (unsafePackCString , unsafeUseAsCStringLen )
93
- import Data.Text (Text , unpack )
93
+ import Data.Text (Text )
94
94
import Data.Text.Encoding (decodeUtf8 )
95
- import Data.Text.Foreign (lengthWord16 , unsafeCopyToPtr )
96
- import Data.Word (Word16 , Word8 )
95
+ import Data.Word (Word8 )
97
96
import Foreign.C.String (CString )
98
- import Foreign.C.Types (CInt , CUShort )
99
- import Foreign.Marshal.Alloc (alloca , allocaBytes )
97
+ import Foreign.C.Types (CInt )
98
+ import Foreign.Marshal.Alloc (alloca )
100
99
import Foreign.Marshal.Utils (fromBool , toBool , with )
101
100
import Foreign.Ptr (Ptr , castPtr , nullPtr )
102
- import Foreign.Storable (peek , pokeByteOff )
101
+ import Foreign.Storable (peek )
103
102
import GHC.Generics (Generic )
104
103
import SDL (SDLException (SDLCallFailed ), Surface (.. ))
105
104
import SDL.Internal.Exception
106
105
import SDL.Raw.Filesystem (rwFromConstMem )
107
106
import SDL.Vect (V4 (.. ))
108
- import System.IO (utf8 )
109
107
110
- import qualified Data.Text.Foreign
111
108
import qualified Foreign.C.String
112
- import qualified GHC.Foreign
113
109
import qualified SDL.Raw
114
110
import qualified SDL.Raw.Font
115
111
116
112
-- stolen from https://github.com/haskell-game/dear-imgui.hs/blob/main/src/DearImGui/Internal/Text.hs
117
113
#if MIN_VERSION_text(2,0,1)
118
114
115
+ import qualified Data.Text.Foreign
116
+
119
117
withCString :: Text -> (CString -> IO a ) -> IO a
120
118
withCString = Data.Text.Foreign. withCString
121
119
122
120
#else
123
121
122
+ import qualified Data.Text
123
+ import qualified GHC.Foreign
124
+ import qualified System.IO
125
+
124
126
withCString :: Text -> (CString -> IO a ) -> IO a
125
127
withCString t action = do
126
- GHC.Foreign. withCString utf8 (unpack t) $ \ textPtr ->
128
+ GHC.Foreign. withCString System.IO. utf8 (Data.Text. unpack t) $ \ textPtr ->
127
129
action textPtr
128
130
129
131
#endif
@@ -222,10 +224,10 @@ unmanaged p = Surface p Nothing
222
224
solid :: MonadIO m => Font -> Color -> Text -> m SDL. Surface
223
225
solid (Font font) (V4 r g b a) text =
224
226
fmap unmanaged .
225
- throwIfNull " SDL.Font.solid" " TTF_RenderUNICODE_Solid " .
226
- liftIO . withText text $ \ ptr ->
227
+ throwIfNull " SDL.Font.solid" " TTF_RenderUTF8_Solid " .
228
+ liftIO . withCString text $ \ ptr ->
227
229
with (SDL.Raw. Color r g b a) $ \ fg ->
228
- SDL.Raw.Font. renderUNICODE_Solid font (castPtr ptr) fg
230
+ SDL.Raw.Font. renderUTF8_Solid font (castPtr ptr) fg
229
231
230
232
-- | Uses the /slow and nice, but with a solid box/ method.
231
233
--
@@ -237,11 +239,11 @@ solid (Font font) (V4 r g b a) text =
237
239
shaded :: MonadIO m => Font -> Color -> Color -> Text -> m SDL. Surface
238
240
shaded (Font font) (V4 r g b a) (V4 r2 g2 b2 a2) text =
239
241
fmap unmanaged .
240
- throwIfNull " SDL.Font.shaded" " TTF_RenderUNICODE_Shaded " .
241
- liftIO . withText text $ \ ptr ->
242
+ throwIfNull " SDL.Font.shaded" " TTF_RenderUTF8_Shaded " .
243
+ liftIO . withCString text $ \ ptr ->
242
244
with (SDL.Raw. Color r g b a) $ \ fg ->
243
245
with (SDL.Raw. Color r2 g2 b2 a2) $ \ bg ->
244
- SDL.Raw.Font. renderUNICODE_Shaded font (castPtr ptr) fg bg
246
+ SDL.Raw.Font. renderUTF8_Shaded font (castPtr ptr) fg bg
245
247
246
248
-- | The /slow slow slow, but ultra nice over another image/ method, 'blended'
247
249
-- renders text at high quality.
@@ -254,21 +256,10 @@ shaded (Font font) (V4 r g b a) (V4 r2 g2 b2 a2) text =
254
256
blended :: MonadIO m => Font -> Color -> Text -> m SDL. Surface
255
257
blended (Font font) (V4 r g b a) text =
256
258
fmap unmanaged .
257
- throwIfNull " SDL.Font.blended" " TTF_RenderUNICODE_Blended " .
258
- liftIO . withText text $ \ ptr ->
259
+ throwIfNull " SDL.Font.blended" " TTF_RenderUTF8_Blended " .
260
+ liftIO . withCString text $ \ ptr ->
259
261
with (SDL.Raw. Color r g b a) $ \ fg ->
260
- SDL.Raw.Font. renderUNICODE_Blended font (castPtr ptr) fg
261
-
262
- -- Analogous to Data.Text.Foreign.useAsPtr, just appends a null-byte.
263
- -- FIXME: Is this even necessary?
264
- withText :: Text -> (Ptr Word16 -> IO a ) -> IO a
265
- withText text act =
266
- allocaBytes len $ \ ptr -> do
267
- unsafeCopyToPtr text ptr
268
- pokeByteOff ptr (len - 2 ) (0 :: CUShort )
269
- act ptr
270
- where
271
- len = 2 * (lengthWord16 text + 1 )
262
+ SDL.Raw.Font. renderUTF8_Blended font (castPtr ptr) fg
272
263
273
264
-- Helper function for converting a bitmask into a list of values.
274
265
fromMaskWith :: (Enum a , Bounded a ) => (a -> CInt ) -> CInt -> [a ]
@@ -481,18 +472,18 @@ glyphMetrics (Font font) ch =
481
472
size :: MonadIO m => Font -> Text -> m (Int , Int )
482
473
size (Font font) text =
483
474
liftIO .
484
- withText text $ \ ptr ->
475
+ withCString text $ \ ptr ->
485
476
alloca $ \ w ->
486
477
alloca $ \ h ->
487
- SDL.Raw.Font. sizeUNICODE font (castPtr ptr) w h
478
+ SDL.Raw.Font. sizeUTF8 font (castPtr ptr) w h
488
479
>>= \ case
489
480
0 -> do
490
481
w' <- fromIntegral <$> peek w
491
482
h' <- fromIntegral <$> peek h
492
483
return (w', h')
493
484
_ -> do
494
485
err <- getError
495
- throwIO $ SDLCallFailed " SDL.Font.size" " TTF_SizeUNICODE " err
486
+ throwIO $ SDLCallFailed " SDL.Font.size" " TTF_SizeUTF8 " err
496
487
497
488
-- | Same as 'solid', but renders a single glyph instead.
498
489
solidGlyph :: MonadIO m => Font -> Color -> Char -> m SDL. Surface
@@ -528,10 +519,10 @@ blendedGlyph (Font font) (V4 r g b a) ch =
528
519
blendedWrapped :: MonadIO m => Font -> Color -> Int -> Text -> m SDL. Surface
529
520
blendedWrapped (Font font) (V4 r g b a) wrapLength text =
530
521
fmap unmanaged .
531
- throwIfNull " SDL.Font.blended" " TTF_RenderUNICODE_Blended_Wrapped " .
532
- liftIO . withText text $ \ ptr ->
522
+ throwIfNull " SDL.Font.blended" " TTF_RenderUTF8_Blended_Wrapped " .
523
+ liftIO . withCString text $ \ ptr ->
533
524
with (SDL.Raw. Color r g b a) $ \ fg ->
534
- 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
535
526
536
527
-- | From a given 'Font' get the kerning size of two glyphs.
537
528
getKerningSize :: MonadIO m => Font -> Index -> Index -> m Int
0 commit comments