Skip to content

Commit 4a92a6a

Browse files
authored
Merge pull request #16 from haskell-game/update-to-text-2.0
Update to work with text >= 2.0.1
2 parents d42d654 + ee704c5 commit 4a92a6a

File tree

3 files changed

+63
-55
lines changed

3 files changed

+63
-55
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
Haskell bindings for the True Type Font library for SDL.
99

1010
- libsdl <https://www.libsdl.org>
11-
- sdl2-ttf <https://www.libsdl.org/projects/SDL_ttf/>
11+
- sdl2-ttf <https://github.com/libsdl-org/SDL_ttf>
1212

1313
Both the raw and the higher level bindings should allow you to use any aspect
1414
of the original SDL2_ttf library. Please report an issue if you encounter a bug

sdl2-ttf.cabal

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: sdl2-ttf
22
version: 2.1.3
33
synopsis: Bindings to SDL2_ttf.
4-
description: Haskell bindings to SDL2_ttf C++ library <http://www.libsdl.org/projects/SDL_ttf/>.
4+
description: Haskell bindings to SDL2_ttf C++ library <https://github.com/libsdl-org/SDL_ttf>.
55
bug-reports: https://github.com/haskell-game/sdl2-ttf/issues
66
license: BSD3
77
license-file: LICENSE
@@ -14,7 +14,7 @@ copyright: Copyright © 2013-2022 Ömer Sinan Ağacan, Siniša Biđin, Rongc
1414
category: Font, Foreign binding, Graphics
1515
build-type: Simple
1616
cabal-version: >=1.10
17-
tested-with: GHC ==8.0.2 || ==8.2.2 || ==8.4.4 || ==8.6.5 || ==8.8.4 || ==8.10.7 || ==9.0.2 || ==9.2.3
17+
tested-with: GHC ==8.0.2 || ==8.2.2 || ==8.4.4 || ==8.6.5 || ==8.8.4 || ==8.10.7 || ==9.0.2 || ==9.2.3 || ==9.4
1818

1919
source-repository head
2020
type: git
@@ -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: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2321
module 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)
9189
import 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 (..))
107104
import 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
111109
import qualified SDL.Raw
112110
import 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
151170
load 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
175194
loadIndex 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
205224
solid :: MonadIO m => Font -> Color -> Text -> m SDL.Surface
206225
solid (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 =
220239
shaded :: MonadIO m => Font -> Color -> Color -> Text -> m SDL.Surface
221240
shaded (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 =
237256
blended :: MonadIO m => Font -> Color -> Text -> m SDL.Surface
238257
blended (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.
257265
fromMaskWith :: (Enum a, Bounded a) => (a -> CInt) -> CInt -> [a]
@@ -464,18 +472,18 @@ glyphMetrics (Font font) ch =
464472
size :: MonadIO m => Font -> Text -> m (Int, Int)
465473
size (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.
481489
solidGlyph :: 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.
511519
blendedWrapped :: MonadIO m => Font -> Color -> Int -> Text -> m SDL.Surface
512520
blendedWrapped (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.
520528
getKerningSize :: MonadIO m => Font -> Index -> Index -> m Int

0 commit comments

Comments
 (0)