Safe Haskell | None |
---|---|
Language | Haskell2010 |
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 v1.19
.
This is a very low-level library; if you just want to render text
using OpenGL, look at the the higher-level library minitypeset-opengl
Note: the glyph coordinate system uses te is the mathematical convention, that is, the Y coordinate increases upwards; unlike the screen and/or bitmap coordinate system, where Y increases downwards.
Synopsis
- newtype TrueType = TrueType ByteString
- data Offset
- data Font = Font {}
- data CFontInfo
- 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)
- notDefinedGlyphChar :: Char
- type Unscaled = Int
- type Scaling = (Float, Float)
- data HorizontalMetrics a = HMetrics {
- advanceWidth :: !a
- leftSideBearing :: !a
- data VerticalMetrics a = VMetrics {}
- 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 (Maybe (BoundingBox Unscaled))
- 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)
- bitmapArray :: Bitmap -> IO (UArray (Int, Int) Word8)
- bitmapFloatArray :: Bitmap -> IO (UArray (Int, Int) Float)
- 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 ()
- type SubpixelShift = (Float, Float)
- getGlyphBitmapBoxSubpixel :: Font -> Glyph -> Scaling -> SubpixelShift -> IO (BoundingBox Int)
- newGlyphBitmapSubpixel :: Font -> Glyph -> Scaling -> SubpixelShift -> IO (Bitmap, BitmapOfs)
- renderGlyphIntoBitmapSubpixel' :: Font -> Glyph -> Scaling -> SubpixelShift -> Bitmap -> BitmapOfs -> IO ()
- renderGlyphIntoBitmapSubpixel :: Font -> Glyph -> Scaling -> SubpixelShift -> Bitmap -> BitmapOfs -> IO ()
- data CachedBitmap = CBM !Glyph !Bitmap !BitmapOfs !(HorizontalMetrics Float)
- data BitmapCache = BMCache {
- bmc_fontinfo :: !Font
- bmc_scaling :: !(Float, Float)
- bmc_cache :: !(UnicodeCache (Maybe CachedBitmap))
- bmc_vmetrics :: !(VerticalMetrics Float)
- bmc_flipped :: !Bool
- bmcVerticalMetrics :: BitmapCache -> VerticalMetrics Float
- bmcScaling :: BitmapCache -> Scaling
- newBitmapCache :: Font -> Bool -> (Float, Float) -> IO BitmapCache
- getCachedBitmap :: BitmapCache -> Char -> IO (Maybe CachedBitmap)
- data UnicodeCache a
- newUnicodeCache :: IO (UnicodeCache a)
- lookupUnicodeCache :: Char -> (Char -> IO a) -> UnicodeCache a -> IO a
Documentation
A TrueType font file (containing maybe multiple font sets) loaded into memory.
A font offset inside a TrueType font file.
Font | |
|
Instances
Storable CFontInfo Source # | |
Defined in Graphics.Rendering.TrueType.STB |
Initialization
enumerateFonts :: TrueType -> IO [Offset] Source #
Enumerates the fonts found in a TrueType file. Often there is only one, but there may be more.
findGlyph :: Font -> Char -> IO (Maybe Glyph) Source #
Maps unicode characters to glyphs. As a hack, we map 0xffff
(which is not a
valid unicode code point) to the "not defined glyph" (glyph #0), which has no
character counterpart.
Note: this is cached, so you can call it many times if necessary.
notDefinedGlyphChar :: Char Source #
The character 0xffff
(which is not a valid unicode code point) which maps to the "not defined glyph"
Font metrics
data HorizontalMetrics a Source #
HMetrics | |
|
Instances
Functor HorizontalMetrics Source # | |
Defined in Graphics.Rendering.TrueType.STB fmap :: (a -> b) -> HorizontalMetrics a -> HorizontalMetrics b # (<$) :: a -> HorizontalMetrics b -> HorizontalMetrics a # | |
Show a => Show (HorizontalMetrics a) Source # | |
Defined in Graphics.Rendering.TrueType.STB showsPrec :: Int -> HorizontalMetrics a -> ShowS # show :: HorizontalMetrics a -> String # showList :: [HorizontalMetrics a] -> ShowS # |
data VerticalMetrics a Source #
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
Instances
Functor VerticalMetrics Source # | |
Defined in Graphics.Rendering.TrueType.STB fmap :: (a -> b) -> VerticalMetrics a -> VerticalMetrics b # (<$) :: a -> VerticalMetrics b -> VerticalMetrics a # | |
Show a => Show (VerticalMetrics a) Source # | |
Defined in Graphics.Rendering.TrueType.STB showsPrec :: Int -> VerticalMetrics a -> ShowS # show :: VerticalMetrics a -> String # showList :: [VerticalMetrics a] -> ShowS # |
data BoundingBox a Source #
The convention is BBox (x0,y0) (x1,y1)
.
BBox (a, a) (a, a) |
Instances
Show a => Show (BoundingBox a) Source # | |
Defined in Graphics.Rendering.TrueType.STB showsPrec :: Int -> BoundingBox a -> ShowS # show :: BoundingBox a -> String # showList :: [BoundingBox a] -> ShowS # |
lineAdvance :: Num a => VerticalMetrics a -> a Source #
As calculated by (ascent - descent + lineGap)
.
verticalSize :: Num a => VerticalMetrics a -> a Source #
As calculated by (ascent - descent)
.
scaleForPixelHeight :: VerticalMetrics Unscaled -> Float -> Float Source #
getGlyphHorizontalMetrics :: Font -> Glyph -> IO (HorizontalMetrics Unscaled) Source #
getGlyphKernAdvance :: Font -> Glyph -> Glyph -> IO Unscaled Source #
An additional amount to add to the 'advance' value between two glyphs
getGlyphBoundingBox :: Font -> Glyph -> IO (Maybe (BoundingBox Unscaled)) Source #
Bitmaps
A 8-bit grayscale bitmap.
Bitmap | |
|
flipBitmap :: Bitmap -> IO Bitmap Source #
Flips the bitmap vertically (leaving the original unchanged)
bitmapArray :: Bitmap -> IO (UArray (Int, Int) Word8) Source #
NOTE: because of the way Haskell indexes rectangular arrays,
the resulting array is indexed with (y,x)
, as opposed to what
you would expect.
Vanilla (non-subpixel) rendering
getGlyphBitmapBox :: Font -> Glyph -> Scaling -> IO (BoundingBox Int) Source #
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) Source #
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 () Source #
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 () Source #
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!
Subpixel rendering
type SubpixelShift = (Float, Float) Source #
The subpixel version of the rendering functions accept an additional fractional shift
getGlyphBitmapBoxSubpixel :: Font -> Glyph -> Scaling -> SubpixelShift -> IO (BoundingBox Int) Source #
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.
newGlyphBitmapSubpixel :: Font -> Glyph -> Scaling -> SubpixelShift -> IO (Bitmap, BitmapOfs) Source #
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).
renderGlyphIntoBitmapSubpixel' :: Font -> Glyph -> Scaling -> SubpixelShift -> Bitmap -> BitmapOfs -> IO () Source #
The offset is the top-left corner of the bounding box of the glyph, and must be nonnegative (otherwise nothing will happen).
renderGlyphIntoBitmapSubpixel :: Font -> Glyph -> Scaling -> SubpixelShift -> Bitmap -> BitmapOfs -> IO () Source #
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!
Cached glyph storage
data CachedBitmap Source #
Note: the metrics are scaled!
data BitmapCache Source #
A "bitmap cache".
BMCache | |
|
bmcVerticalMetrics :: BitmapCache -> VerticalMetrics Float Source #
Note: these metrics are scaled!
bmcScaling :: BitmapCache -> Scaling Source #
newBitmapCache :: Font -> Bool -> (Float, Float) -> IO BitmapCache Source #
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) Source #
Fetches a rendered glyph bitmap from the cache (rendering it first if it was not present in the cache before).
Unicode tables
data UnicodeCache a Source #
A table indexed by unicode code points.
Organized into small continous blocks (say 128 characters) so lookup should be pretty fast
newUnicodeCache :: IO (UnicodeCache a) Source #
lookupUnicodeCache :: Char -> (Char -> IO a) -> UnicodeCache a -> IO a Source #