| 1 |
-------------------------------------------------------------------------------- |
| 2 |
-- | |
| 3 |
-- Module : Graphics.UI.GLUT.Fonts |
| 4 |
-- Copyright : (c) Sven Panne 2002-2018 |
| 5 |
-- License : BSD3 |
| 6 |
-- |
| 7 |
-- Maintainer : Sven Panne <[email protected]> |
| 8 |
-- Stability : stable |
| 9 |
-- Portability : portable |
| 10 |
-- |
| 11 |
-- GLUT supports two types of font rendering: stroke fonts, meaning each |
| 12 |
-- character is rendered as a set of line segments; and bitmap fonts, where each |
| 13 |
-- character is a bitmap generated with |
| 14 |
-- 'Graphics.Rendering.OpenGL.GL.Bitmaps.bitmap'. Stroke fonts have the |
| 15 |
-- advantage that because they are geometry, they can be arbitrarily scale and |
| 16 |
-- rendered. Bitmap fonts are less flexible since they are rendered as bitmaps |
| 17 |
-- but are usually faster than stroke fonts. |
| 18 |
-- |
| 19 |
-------------------------------------------------------------------------------- |
| 20 |
|
| 21 |
module Graphics.UI.GLUT.Fonts ( |
| 22 |
Font(..), BitmapFont(..), StrokeFont(..), |
| 23 |
) where |
| 24 |
|
| 25 |
import Control.Monad.IO.Class ( MonadIO(..) ) |
| 26 |
import Data.Char ( ord ) |
| 27 |
import Foreign.C.String ( withCString ) |
| 28 |
import Foreign.C.Types ( CInt ) |
| 29 |
import Foreign.Ptr ( castPtr ) |
| 30 |
import Graphics.Rendering.OpenGL ( GLint, GLfloat ) |
| 31 |
|
| 32 |
import Graphics.UI.GLUT.Raw |
| 33 |
|
| 34 |
-------------------------------------------------------------------------------- |
| 35 |
|
| 36 |
class Font a where |
| 37 |
-- | Render the string in the named font, without using any display lists. |
| 38 |
-- Rendering a nonexistent character has no effect. |
| 39 |
-- |
| 40 |
-- If the font is a bitmap font, 'renderString' automatically sets the OpenGL |
| 41 |
-- unpack pixel storage modes it needs appropriately and saves and restores |
| 42 |
-- the previous modes before returning. The generated call to |
| 43 |
-- 'Graphics.Rendering.OpenGL.GL.bitmap' will adjust the current raster |
| 44 |
-- position based on the width of the string. |
| 45 |
-- If the font is a stroke font, |
| 46 |
-- 'Graphics.Rendering.OpenGL.GL.CoordTrans.translate' is used to translate |
| 47 |
-- the current model view matrix to advance the width of the string. |
| 48 |
|
| 49 |
renderString :: MonadIO m => a -> String -> m () |
| 50 |
|
| 51 |
-- | For a bitmap font, return the width in pixels of a string. For a stroke |
| 52 |
-- font, return the width in units. While the width of characters in a font |
| 53 |
-- may vary (though fixed width fonts do not vary), the maximum height |
| 54 |
-- characteristics of a particular font are fixed. |
| 55 |
|
| 56 |
stringWidth :: MonadIO m => a -> String -> m GLint |
| 57 |
|
| 58 |
-- | (/freeglut only/) For a bitmap font, return the maximum height of the |
| 59 |
-- characters in the given font measured in pixels. For a stroke font, |
| 60 |
-- return the height in units. |
| 61 |
|
| 62 |
fontHeight :: MonadIO m => a -> m GLfloat |
| 63 |
|
| 64 |
instance Font BitmapFont where |
| 65 |
renderString = bitmapString |
| 66 |
stringWidth = bitmapLength |
| 67 |
fontHeight = bitmapHeight |
| 68 |
|
| 69 |
|
| 70 |
instance Font StrokeFont where |
| 71 |
renderString = strokeString |
| 72 |
stringWidth = strokeLength |
| 73 |
fontHeight = strokeHeight |
| 74 |
|
| 75 |
-------------------------------------------------------------------------------- |
| 76 |
|
| 77 |
bitmapString :: MonadIO m => BitmapFont -> String -> m () |
| 78 |
bitmapString f s = do |
| 79 |
i <- marshalBitmapFont f |
| 80 |
mapM_ (\c -> withChar c (glutBitmapCharacter i)) s |
| 81 |
|
| 82 |
withChar :: Char -> (CInt -> m a) -> m a |
| 83 |
withChar c f = f . fromIntegral . ord $ c |
| 84 |
|
| 85 |
-------------------------------------------------------------------------------- |
| 86 |
|
| 87 |
strokeString :: MonadIO m => StrokeFont -> String -> m () |
| 88 |
strokeString f s = do |
| 89 |
i <- marshalStrokeFont f |
| 90 |
mapM_ (\c -> withChar c (glutStrokeCharacter i)) s |
| 91 |
|
| 92 |
-------------------------------------------------------------------------------- |
| 93 |
|
| 94 |
bitmapLength :: MonadIO m |
| 95 |
=> BitmapFont -- ^ Bitmap font to use. |
| 96 |
-> String -- ^ String to return width of (not confined to 8 |
| 97 |
-- bits). |
| 98 |
-> m GLint -- ^ Width in pixels. |
| 99 |
bitmapLength f s = liftIO $ do |
| 100 |
i <- marshalBitmapFont f |
| 101 |
fmap fromIntegral $ withCString s (glutBitmapLength i . castPtr) |
| 102 |
|
| 103 |
-------------------------------------------------------------------------------- |
| 104 |
|
| 105 |
strokeLength :: MonadIO m |
| 106 |
=> StrokeFont -- ^ Stroke font to use. |
| 107 |
-> String -- ^ String to return width of (not confined to 8 |
| 108 |
-- bits). |
| 109 |
-> m GLint -- ^ Width in units. |
| 110 |
strokeLength f s = liftIO $ do |
| 111 |
i <- marshalStrokeFont f |
| 112 |
fmap fromIntegral $ withCString s (glutStrokeLength i . castPtr) |
| 113 |
|
| 114 |
-------------------------------------------------------------------------------- |
| 115 |
|
| 116 |
bitmapHeight :: MonadIO m |
| 117 |
=> BitmapFont -- ^ Bitmap font to use. |
| 118 |
-> m GLfloat -- ^ Height in pixels. |
| 119 |
bitmapHeight f = liftIO $ do |
| 120 |
i <- marshalBitmapFont f |
| 121 |
fromIntegral `fmap` glutBitmapHeight i |
| 122 |
|
| 123 |
-------------------------------------------------------------------------------- |
| 124 |
|
| 125 |
strokeHeight :: MonadIO m |
| 126 |
=> StrokeFont -- ^ Stroke font to use. |
| 127 |
-> m GLfloat -- ^ Height in units. |
| 128 |
strokeHeight f = glutStrokeHeight =<< marshalStrokeFont f |