Skip to content

Commit 09c6889

Browse files
committed
Switch to UTF8 calls using the just defined withCString
1 parent c8ce8b3 commit 09c6889

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

src/SDL/Font.hs

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -90,40 +90,42 @@ 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, unpack)
93+
import Data.Text (Text)
9494
import Data.Text.Encoding (decodeUtf8)
95-
import Data.Text.Foreign (lengthWord16, unsafeCopyToPtr)
96-
import Data.Word (Word16, Word8)
95+
import Data.Word (Word8)
9796
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)
10099
import Foreign.Marshal.Utils (fromBool, toBool, with)
101100
import Foreign.Ptr (Ptr, castPtr, nullPtr)
102-
import Foreign.Storable (peek, pokeByteOff)
101+
import Foreign.Storable (peek)
103102
import GHC.Generics (Generic)
104103
import SDL (SDLException (SDLCallFailed), Surface (..))
105104
import SDL.Internal.Exception
106105
import SDL.Raw.Filesystem (rwFromConstMem)
107106
import SDL.Vect (V4 (..))
108-
import System.IO (utf8)
109107

110-
import qualified Data.Text.Foreign
111108
import qualified Foreign.C.String
112-
import qualified GHC.Foreign
113109
import qualified SDL.Raw
114110
import qualified SDL.Raw.Font
115111

116112
-- stolen from https://github.com/haskell-game/dear-imgui.hs/blob/main/src/DearImGui/Internal/Text.hs
117113
#if MIN_VERSION_text(2,0,1)
118114

115+
import qualified Data.Text.Foreign
116+
119117
withCString :: Text -> (CString -> IO a) -> IO a
120118
withCString = Data.Text.Foreign.withCString
121119

122120
#else
123121

122+
import qualified Data.Text
123+
import qualified GHC.Foreign
124+
import qualified System.IO
125+
124126
withCString :: Text -> (CString -> IO a) -> IO a
125127
withCString t action = do
126-
GHC.Foreign.withCString utf8 (unpack t) $ \textPtr ->
128+
GHC.Foreign.withCString System.IO.utf8 (Data.Text.unpack t) $ \textPtr ->
127129
action textPtr
128130

129131
#endif
@@ -222,10 +224,10 @@ unmanaged p = Surface p Nothing
222224
solid :: MonadIO m => Font -> Color -> Text -> m SDL.Surface
223225
solid (Font font) (V4 r g b a) text =
224226
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 ->
227229
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
229231

230232
-- | Uses the /slow and nice, but with a solid box/ method.
231233
--
@@ -237,11 +239,11 @@ solid (Font font) (V4 r g b a) text =
237239
shaded :: MonadIO m => Font -> Color -> Color -> Text -> m SDL.Surface
238240
shaded (Font font) (V4 r g b a) (V4 r2 g2 b2 a2) text =
239241
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 ->
242244
with (SDL.Raw.Color r g b a) $ \fg ->
243245
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
245247

246248
-- | The /slow slow slow, but ultra nice over another image/ method, 'blended'
247249
-- renders text at high quality.
@@ -254,21 +256,10 @@ shaded (Font font) (V4 r g b a) (V4 r2 g2 b2 a2) text =
254256
blended :: MonadIO m => Font -> Color -> Text -> m SDL.Surface
255257
blended (Font font) (V4 r g b a) text =
256258
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 ->
259261
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
272263

273264
-- Helper function for converting a bitmask into a list of values.
274265
fromMaskWith :: (Enum a, Bounded a) => (a -> CInt) -> CInt -> [a]
@@ -481,18 +472,18 @@ glyphMetrics (Font font) ch =
481472
size :: MonadIO m => Font -> Text -> m (Int, Int)
482473
size (Font font) text =
483474
liftIO .
484-
withText text $ \ptr ->
475+
withCString text $ \ptr ->
485476
alloca $ \w ->
486477
alloca $ \h ->
487-
SDL.Raw.Font.sizeUNICODE font (castPtr ptr) w h
478+
SDL.Raw.Font.sizeUTF8 font (castPtr ptr) w h
488479
>>= \case
489480
0 -> do
490481
w' <- fromIntegral <$> peek w
491482
h' <- fromIntegral <$> peek h
492483
return (w', h')
493484
_ -> do
494485
err <- getError
495-
throwIO $ SDLCallFailed "SDL.Font.size" "TTF_SizeUNICODE" err
486+
throwIO $ SDLCallFailed "SDL.Font.size" "TTF_SizeUTF8" err
496487

497488
-- | Same as 'solid', but renders a single glyph instead.
498489
solidGlyph :: MonadIO m => Font -> Color -> Char -> m SDL.Surface
@@ -528,10 +519,10 @@ blendedGlyph (Font font) (V4 r g b a) ch =
528519
blendedWrapped :: MonadIO m => Font -> Color -> Int -> Text -> m SDL.Surface
529520
blendedWrapped (Font font) (V4 r g b a) wrapLength text =
530521
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 ->
533524
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
535526

536527
-- | From a given 'Font' get the kerning size of two glyphs.
537528
getKerningSize :: MonadIO m => Font -> Index -> Index -> m Int

0 commit comments

Comments
 (0)