stb-truetype-0.1.1: A wrapper around Sean Barrett's TrueType rasterizer library.ContentsIndex
Graphics.Rendering.TrueType.STB
Contents
Initialization
Font metrics
Bitmaps
Cached glyph storage
Description

This is a wrapper around Sean Barrett's TrueType font rasterizer code. The original can be found at http://nothings.org/stb/stb_truetype.h. The version of stb-truetype used here is 0.2.

Note: it seems that compound glyphs are not implemented yet!

Synopsis
data TrueType
data Offset
data Font
data Glyph
loadTTF :: FilePath -> IO TrueType
withTTF :: FilePath -> (TrueType -> IO a) -> IO a
enumerateFonts :: TrueType -> IO [Offset]
initFont :: TrueType -> Offset -> IO Font
findGlyph :: Font -> Char -> IO (Maybe Glyph)
type Unscaled = Int
data HorizontalMetrics a = HMetrics {
advanceWidth :: a
leftSideBearing :: a
}
data VerticalMetrics a = VMetrics {
ascent :: a
descent :: a
lineGap :: a
}
data BoundingBox a = BBox (a, a) (a, a)
lineAdvance :: Num a => VerticalMetrics a -> a
verticalSize :: Num a => VerticalMetrics a -> a
scaleForPixelHeight :: VerticalMetrics Unscaled -> Float -> Float
getFontVerticalMetrics :: Font -> IO (VerticalMetrics Unscaled)
getGlyphHorizontalMetrics :: Font -> Glyph -> IO (HorizontalMetrics Unscaled)
getGlyphKernAdvance :: Font -> Glyph -> Glyph -> IO Unscaled
getGlyphBoundingBox :: Font -> Glyph -> IO (BoundingBox Unscaled)
type Scaling = (Float, Float)
data Bitmap = Bitmap {
bitmapSize :: (Int, Int)
bitmapPtr :: ForeignPtr Word8
}
newBitmap :: (Int, Int) -> IO Bitmap
withBitmap :: Bitmap -> (Int -> Int -> Ptr Word8 -> IO a) -> IO a
flipBitmap :: Bitmap -> IO Bitmap
type BitmapOfs = (Int, Int)
getGlyphBitmapBox :: Font -> Glyph -> Scaling -> IO (BoundingBox Int)
newGlyphBitmap :: Font -> Glyph -> Scaling -> IO (Bitmap, BitmapOfs)
renderGlyphIntoBitmap' :: Font -> Glyph -> Scaling -> Bitmap -> BitmapOfs -> IO ()
renderGlyphIntoBitmap :: Font -> Glyph -> Scaling -> Bitmap -> BitmapOfs -> IO ()
bitmapArray :: Bitmap -> IO (UArray (Int, Int) Word8)
bitmapFloatArray :: Bitmap -> IO (UArray (Int, Int) Float)
data CachedBitmap = CBM Bitmap BitmapOfs (HorizontalMetrics Float)
data BitmapCache
bmcVerticalMetrics :: BitmapCache -> VerticalMetrics Float
bmcScaling :: BitmapCache -> Scaling
newBitmapCache :: Font -> Bool -> (Float, Float) -> IO BitmapCache
getCachedBitmap :: BitmapCache -> Char -> IO (Maybe CachedBitmap)
Documentation
data TrueType
A TrueType font file (containing maybe multiple font sets) loaded into memory.
data Offset
A font offset inside a TrueType font file.
show/hide Instances
data Font
data Glyph
A glyph inside a font.
show/hide Instances
Initialization
loadTTF :: FilePath -> IO TrueType
withTTF :: FilePath -> (TrueType -> IO a) -> IO a
enumerateFonts :: TrueType -> IO [Offset]
Enumerates the fonts found in a TrueType file. Often there is only one, but there may be more.
initFont :: TrueType -> Offset -> IO Font
findGlyph :: Font -> Char -> IO (Maybe Glyph)
Note: this is cached.
Font metrics
type Unscaled = Int
data HorizontalMetrics a
Constructors
HMetrics
advanceWidth :: a
leftSideBearing :: a
show/hide Instances
data VerticalMetrics a
ascent is the coordinate above the baseline the font extends; descent is the coordinate below the baseline the font extends (i.e. it is typically negative) lineGap is the spacing between one row's descent and the next row's ascent... so you should advance the vertical position by ascent - descent + lineGap
Constructors
VMetrics
ascent :: a
descent :: a
lineGap :: a
show/hide Instances
data BoundingBox a
The convention is BBox (x0,y0) (x1,y1).
Constructors
BBox (a, a) (a, a)
show/hide Instances
lineAdvance :: Num a => VerticalMetrics a -> a
As calculated by (ascent - descent + lineGap).
verticalSize :: Num a => VerticalMetrics a -> a
As calculated by (ascent - descent).
scaleForPixelHeight :: VerticalMetrics Unscaled -> Float -> Float
getFontVerticalMetrics :: Font -> IO (VerticalMetrics Unscaled)
getGlyphHorizontalMetrics :: Font -> Glyph -> IO (HorizontalMetrics Unscaled)
getGlyphKernAdvance :: Font -> Glyph -> Glyph -> IO Unscaled
This is not yet implemented in stb_truetype; it always returns 0.
getGlyphBoundingBox :: Font -> Glyph -> IO (BoundingBox Unscaled)
Bitmaps
type Scaling = (Float, Float)
data Bitmap
A 8-bit grayscale bitmap.
Constructors
Bitmap
bitmapSize :: (Int, Int)
bitmapPtr :: ForeignPtr Word8
newBitmap :: (Int, Int) -> IO Bitmap
withBitmap :: Bitmap -> (Int -> Int -> Ptr Word8 -> IO a) -> IO a
flipBitmap :: Bitmap -> IO Bitmap
Flips the bitmap vertically (leaving the original unchanged)
type BitmapOfs = (Int, Int)
An offset (for example the pivot of the glyph)
getGlyphBitmapBox :: Font -> Glyph -> Scaling -> IO (BoundingBox Int)

Returns the size of the bitmap (in pixels) needed to render the glyph with the given scaling.

The box is centered around the glyph origin; so the bitmap width is x1-x0, height is y1-y0, and location to place the bitmap top left is (leftSideBearing*scale,y0). Note that the bitmap uses y-increases-down, but the shape uses y-increases-up, so the results of getGlyphBitmapBox and getGlyphBoundingBox are inverted.

newGlyphBitmap :: Font -> Glyph -> Scaling -> IO (Bitmap, BitmapOfs)
Creates a new bitmap just enough to fit the glyph with the given scaling, and renders the glyph into it. The offset returned is the offset in pixel space from the glyph origin to the top-left of the bitmap (so it's almost always negative).
renderGlyphIntoBitmap' :: Font -> Glyph -> Scaling -> Bitmap -> BitmapOfs -> IO ()
The offset is the top-left corner of the bounding box of the glyph, and must be nonnegative (otherwise nothing will happen).
renderGlyphIntoBitmap :: Font -> Glyph -> Scaling -> Bitmap -> BitmapOfs -> IO ()
The offset is the origin of the glyph. If the glyph extends from the bitmap in the positive direction, it is clipped; however, if it extends in the negative direction, no drawing will happen!
bitmapArray :: Bitmap -> IO (UArray (Int, Int) Word8)
NOTE: because of the way Haskell indexes rectangular arrays, the resulting array is indexed with (y,x), as opposed to what you would expect.
bitmapFloatArray :: Bitmap -> IO (UArray (Int, Int) Float)
Cached glyph storage
data CachedBitmap
Note: the metrics are scaled!
Constructors
CBM Bitmap BitmapOfs (HorizontalMetrics Float)
data BitmapCache
A "bitmap cache".
bmcVerticalMetrics :: BitmapCache -> VerticalMetrics Float
Note: these metrics are scaled!
bmcScaling :: BitmapCache -> Scaling
newBitmapCache :: Font -> Bool -> (Float, Float) -> IO BitmapCache
Creates a new cache where glyph bitmaps with the given scaling will be stored. The second argument is whether the resulting bitmaps should be flipped vertically or not (this is useful with OpenGL).
getCachedBitmap :: BitmapCache -> Char -> IO (Maybe CachedBitmap)
Fetches a rendered glyph bitmap from the cache (rendering it first if it was not present in the cache before).
Produced by Haddock version 2.4.1