Abstract
LISPBUILDER-SDL-TTF provides a Lisp wrapper for the SDL_TTF library, and provides support for loading and rendering True-Type fonts in an SDL application. The SDL_TTF wrapper is in itself a wrapper around the FreeType 1.2 library, available at: Freetype Homepage. LISPBUILDER-SDL-TTF has a dependency on the LISPBUILDER-SDL package. LISPBUILDER-SDL-TTF is distributed under the MIT-style license.
Current Version: The current version of LISPBUILDER-SDL-TTF is version 0.2.
The TrueType library is automatically initialized and uninitialized by the LISPBUILDER-SDL package. The functions INIT-TTF and QUIT-TTF are added to the lists SDL:*EXTERNAL-INIT-ON-STARTUP* and SDL:*EXTERNAL-QUIT-ON-EXIT*. LISPBUILDER-SDL iterates over these lists in calls to SDL:INIT-SDL, SDL:QUIT-SDL and SDL:WITH-INIT in order to initialize or uninitialize any external libraries such as LISPBUILDER-SDL-TTF.
To open a TrueType font use INITIALISE-FONT or the macro WITH-OPEN-FONT. LISPBUILDER-SDL-TTF allows more than one FONT to be used simultaneously within a single SDL application.
LISPBUILDER-SDL-TTF has the concept of a *DEFAULT-FONT*. A FONT may be bound to *DEFAULT-FONT* by using INITIALISE-DEFAULT-FONT, WITH-OPEN-FONT, or WITH-DEFAULT-FONT. All functions that take a &KEYword or &OPTIONAL FONT argument accept *DEFAULT-FONT* unless a FONT is specified. This makes calling functions a bit easier as the :FONT parameter need not be explicitely passed. For example: (GET-FONT-SIZE "TEXT" :SIZE :W), compared to (GET-FONT-SIZE "TEXT" :SIZE :W :FONT ANY-FONT)
Functions and symbols exported from the LISPBUILDER-SDL-TTF package are
accessible from the LISPBUILDER-SDL-TTF: prefix or the
shorter form SDL-TTF: nickname.
This example was created with the following code:
(sdl:with-init ()
(sdl:window 320 240)
(sdl-ttf:initialise-default-font)
(sdl-ttf:draw-string-solid-* "Hello TTF World!" 25 50
:color (sdl:color :r 255 :g 0 :b 255))
(sdl:with-events ()
(:quit-event () t)
(:video-expose-event () (sdl:update-display))))
*default-font*
*default-font-path*
close-font
draw-font
draw-font
draw-font-at
draw-font-at-*
draw-string-blended
draw-string-blended-*
draw-string-shaded
draw-string-shaded-*
draw-string-solid
draw-string-solid-*
font
free-font
get-font-ascent
get-font-descent
get-font-face-family-name
get-font-face-style-name
get-font-faces
get-font-height
get-font-line-skip
get-font-size
get-font-style
get-glyph-metric
init-ttf
initialise-default-font
initialise-font
is-font-face-fixed-width
is-init
new-font
open-font
quit-ttf
render-string-blended
render-string-shaded
render-string-solid
set-font-style
with-default-font
with-open-font
The following table describes the status of the Lisp implementations that have been tested with LISPBUILDER-SDL-TTF:
| Lisp Implementation | LISPBUILDER-SDL-TTF Status | Comments | ||
|---|---|---|---|---|
| Win32 | Linux | MacOS | ||
| CLISP v2.38 | Working | Working | No | |
| Lispworks v4.4.6 Personal | Working | Working | No | |
| Allegro Express 8.0 | Unknown | Unknown | No | |
| OpenMCL | NA | NA | Unknown | |
| SBCL | Working | Working | No | |
svn co https://lispbuilder.svn.sourceforge.net/svnroot/lispbuilder lispbuilder
See the LISPBUILDER documentation for installation instructions.
LISPBUILDER-SDL-TTF requires that a glue library lispbuilder-sdl-ttf-glue is also installed
in the library search path. lispbuilder-sdl-ttf-glue wraps the native font rendering functions
provided by SDL_TTF as these functions require that a SDL_Color struct is passed
by value. However CFFI only supports passing structs by reference.
lispbuilder-sdl-ttf-glue is installed as part of the LISPBUILDER-SDL-TTF-BINARIES
package into the directory LISPBUILDER-SDL-TTF/BIN.
To rebuild the glue library:
LISPBUILDER-SDL-TTF/Makefile.mingw. Modify the existing line
SDL_TTF_CFLAGS=$(CFLAGS) -ISDL-1.2.11/include -ISDL_ttf-2.0.8.LISPBUILDER-SDL-TTF/ directory, execute make
to create the glue library.lispbuilder-sdl-ttf-glue library will be found in the
LISPBUILDER-SDL-TTF/BUILD/ directory.LISPBUILDER-SDL-TTF/BUILD/lispbuilder-sdl-ttf-glue.dll to the
BIN directory LISPBUILDER-SDL-TTF/BIN/lispbuilder-sdl-ttf-glue.dllTo rebuild the glue library:
LISPBUILDER-SDL-TTF/ directory, execute make
to create the glue library.lispbuilder-sdl-ttf-glue library will be found in the
LISPBUILDER-SDL-TTF/BUILD/ directory.LISPBUILDER-SDL-TTF/ directory, execute make install_ttf
to install the lispbuilder-sdl-ttf-glue library.See the LISPBUILDER documentation for Usage instructions.
The following examples are contained in the package LISPBUILDER-SDL-TTF-EXAMPLES:
Enter the following at the REPL to load the examples in the LISPBUILDER-SDL-TTF-EXAMPLES package:
(asdf:operate 'asdf:load-op :lispbuilder-sdl-TTF-examples)
LISPBUILDER-SDL-TTF has a dependency on LISPBUILDER-SDL.
LISPBUILDER-SDL-TTF is distributed under the MIT-style license.
[Special variable]
*default-font*
When a
LISPBUILDER-SDL-TTFfunction or macro has anOPTIONALorKEYword argumentFONT, the default action is to bind to*DEFAULT-FONT*.*DEFAULT-FONT*is set toNILinitially. A font must be initialized by calling WITH-OPEN-FONT, INITIALISE-FONT, or INITIALISE-DEFAULT-FONT. These will bind*DEFAULT-FONT*to a Truetype fontFONT.
[Special variable]
*default-font-path*
The path to the default Truetype font,
"Bitstream Vera"
[Function]
close-font &key font *default-font* => result
Closes the font
FONTwhen theSDL_TTFfont library is intitialized. NOTE:CLOSE-FONTdoes not uninitialise the font library, and does not bind*DEFAULT-FONT*toNIL. ReturnsTif successful, orNILif the font cannot be closed or theSDL_TTFfont library is not initialized.
[Generic function]
draw-font &key font surface => result
Blit the cached SURFACE in FONT to the destination surface SURFACE. The cached surface is created during a previous call to any of the DRAW-STRING* functions. Uses POSITION in the cached SURFACE to render to the X/Y coordinates on the destination SURFACE.
This function can speed up blitting when the text remains unchanged between screen updates.
[Function]
draw-font &key font surface => result
[Generic function]
draw-font-at position &key font surface => result
See DRAW-FONT. POINT is used to position the cached SURFACE, where POINT is of type SDL:POINT.
[Method]
draw-font-at position &key font surface => result
[Generic function]
draw-font-at-* x y &key font surface => result
See DRAW-FONT. X and Y are used to position the cached SURFACE, where X and Y are INTEGERS.
[Method]
draw-font-at-* x y &key font surface => result
[Function]
draw-string-blended text position &key font surface color => result
POSITIONis theXandYcoordinates to renderTEXT, of typeSDL:POINT.
[Function]
draw-string-blended-* text x y &key font surface color => result
Draw text
TEXTusing fontFONTwith colorCOLORonto surfaceSURFACE, using the Blended mode.Parameters
TEXTis the text to render, of typeSTRING.XandYare the x and y coordinates of theTEXT, asINTEGERs.FONTis aFONTobject. Binds to*DEFAULT-FONT*by default.SURFACEis the target surface, of typeSDL:SDL-SURFACE.COLORis the color used to renderTEXT, of typeSDL:SDL-COLORReturn
- Returns the font
FONT.Example
(DRAW-STRING-SOLID-* "Hello World!" 0 0 :FONT *DEFAULT-FONT* :SURFACE A-SURFACE :COLOR A-COLOR)
[Function]
draw-string-shaded text position fg-color bg-color &key font surface => result
Parmeters
POSITIONis theXandYcoordinates to renderTEXT, of typeSDL:POINT.
[Function]
draw-string-shaded-* text x y fg-color bg-color &key font surface => result
Draw text
TEXTusing fontFONTwith foreground colorFG-COLORand background colorBG-COLORonto surfaceSURFACE, using the Shaded mode.Parameters
TEXTis the text to render, of typeSTRING.XandYare the x and y coordinates of theTEXT, asINTEGERs.FG-COLORis the foreground color of theTEXT, of typeSDL:SDL-COLORBG-COLORis the background color of theTEXT, of typeSDL:SDL-COLORFONTis aFONTobject. Binds to*DEFAULT-FONT*by default.SURFACEis the target surface, of typeSDL:SDL-SURFACE.Return
- Returns the font
FONT.Example
(DRAW-STRING-SHADED-* "Hello World!" 0 0 fg-col bg-col :FONT *DEFAULT-FONT* :SURFACE A-SURFACE)
[Function]
draw-string-solid text position &key font surface color => result
See DRAW-STRING-SOLID-*.
Parmeters
POSITIONis theXandYcoordinates to renderTEXT, of typeSDL:POINT.
[Function]
draw-string-solid-* text x y &key font surface color => result
Draw text
TEXTusing fontFONTwith colorCOLORonto surfaceSURFACE, using the Solid mode.Parameters
TEXTis the text to render, of typeSTRING.XandYare the x and y coordinates of theTEXT, asINTEGERs.FONTis aFONTobject. Binds to*DEFAULT-FONT*by default.SURFACEis the target surface, of typeSDL:SDL-SURFACE.COLORis the color used to renderTEXT, of typeSDL:SDL-COLORReturn
- Returns the font
FONT.Example
(DRAW-STRING-SOLID-* "Hello World!" 0 0 :FONT *DEFAULT-FONT* :SURFACE A-SURFACE :COLOR A-COLOR)
[Standard class]
font
A
FONTobject is wrapper around a foreignTTF_Fontobject.The
FONTobject maintains the most recent surfaceSDL:SURFACEcreated by a call to any of theRENDER-STRING*functions. Use DRAW-FONT, DRAW-FONT-AT or DRAW-FONT-AT-* to draw the cached surface.Prior to the first call to a
RENDER-STRING*function, the cached surface isNIL.
[Generic function]
free-font font => result
[Method]
free-font (font font) => result
Free the resources used by the font
FONT. Free's any cached surface. Closes theTTF_Fontobject only when theFONTwas created within the current INIT-TTF/QUIT-TTF sequence. In other words, when(EQ (GENERATION FONT) *GENERATION). Otherwise it is assumed that theTTF_Fontwas closed in a previous call toQUIT-TTF.
[Function]
get-font-ascent &key font => result
Returns the maximum pixel ascent of all glyphs of font
FONT. This can also be interpreted as the distance from the top of the font to the baseline. It could be used when drawing an individual glyph relative to a top point, by combining it with the glyph's maxy metric to resolve the top of the rectangle used when blitting the glyph on the screen.Parameters
FONTis aFONTobject. Binds to*DEFAULT-FONT*by default.Returns
- Returns the ascent of the
FONTas anINTEGER.
[Function]
get-font-descent &key font => result
Returns the maximum pixel descent of all glyphs of font
FONT. This can also be interpreted as the distance from the baseline to the bottom of the font. It could be used when drawing an individual glyph relative to a bottom point, by combining it with the glyph’s maxy metric to resolve the top of the rectangle used when blitting the glyph on the screen.Parameters
FONTis aFONTobject. Binds to*DEFAULT-FONT*by default.Returns
- Returns the descent of the
FONTas anINTEGER.
[Function]
get-font-face-family-name &key font => result
Returns the current font face family name of font
FONTorNILif the information is unavailable.Parameters
FONTis aFONTobject. Binds to*DEFAULT-FONT*by default.Returns
- Returns the name of the
FONTface family name as aSTRING, orNILif unavailable.
[Function]
get-font-face-style-name &key font => result
Returns the current font face style name of font
FONT, orNILif the information is unavailable.Parameters
FONTis aFONTobject. Binds to*DEFAULT-FONT*by default.Returns
- Returns the name of the
FONTface style as aSTRING, orNILif unavailable.
[Function]
get-font-faces &key font => result
Returns the number of faces 'sub-fonts' available in the font
FONT. This is a count of the number of specific fonts (based on size and style and other typographical features perhaps) contained in the font itself. It seems to be a useless fact to know, since it can’t be applied in any otherSDL_TTFfunctions.Parameters
FONTis aFONTobject. Binds to*DEFAULT-FONT*by default.Returns
- Returns the number of faces in the
FONTas anINTEGER.
[Function]
get-font-height &key font => result
Returns the maximum pixel height of all glyphs of font
FONT. Use this height for rendering text as close together vertically as possible, though adding at least one pixel height to it will space it so they can't touch. Remember thatSDL_TTFdoesn't handle multiline printing so you are responsible for line spacing, see GET-FONT-LINE-SKIP as well.Parameters
FONTis aFONTobject. Binds to*DEFAULT-FONT*by default.Returns
- Retuns the height of the
FONTas anINTEGER.
[Function]
get-font-line-skip &key font => result
Returns the recommended pixel height of a rendered line of text of the font
FONT. This is usually larger than the GET-FONT-HEIGHT of the font.Parameters
FONTis aFONTobject. Binds to*DEFAULT-FONT*by default.Returns
- Returns the pixel height of the
FONTas anINTEGER.
[Function]
get-font-size text &key size font => result
Calculates and returns the resulting
SIZEof theSDL:SURFACEthat is required to render the textTEXT, orNILon error. No actual rendering is performed, however correct kerning is calculated for the actual width. The height returned is the same as returned using GET-FONT-HEIGHT.Parameters
TEXTis the string to size, of typeSTRING.SIZEmust be one of;:Wfor the text width or:Hfor the text height.FONTis the font used to calculate the size of theTEXT. Binds to*DEFAULT-FONT*by default.Return
- Returns the width or height of the specified
SDL:SURFACE, orNILupon error.
[Function]
get-font-style &key font => result
Returns the rendering style of the font
FONT. If no style is set then:STYLE-NORMALis returned, orNILupon error.Parameters
FONTis aFONTobject. Binfs to*DEFAULT-FONT*by default.Return
- Retuns the font style as one or more of:
:STYLE-NORMAL,:STYLE-BOLD,:STYLE-ITALIC,:STYLE-UNDERLINE
[Function]
get-glyph-metric ch &key metric font => result
Returns the glyph metrics
METRICfor the characterCH, orNILupon error.Parameters
CHis a UNICODE chararacter specified as anINTEGER.FONTis aFONTobject from which to retrieve the glyph metrics of the characterCH. Binds to*DEFAULT-FONT*by default.METRICis aKEYword argument and may be one of;:MINX, for the minimumXoffset.:MAXX, for the maximumXoffset.:MINY, for the minimumYoffset.:MAXY, for the maximumYoffset.:ADVANCE, for the advance offset.Return
- Returns the glyph metric as an
INTEGER.Example
(GET-GLYPH-METRIC UNICODE-CHAR :METRIC :MINX :FONT *DEFAULT-FONT*)
[Function]
init-ttf => result
Initializes the
SDL_TTFfont library if uninitialized. ReturnsTif the library was uninitialized and is successfully initialized, or else returnsNILif uninitialized.
[Function]
initialise-default-font &optional free filename size => result
Creates a new
FONTobject that is loaded from the file at locationFILENAME, and binds this to the symbol*DEFAULT-FONT*. Although several truetype fonts may used within a single SDL application, only a singleFONTmay be bound to the symbol*DEFAULT-FONT*at any one time. Generates anERRORifFREEisNILand*DEFAULT-FONT*is already bound to aFONTwhenINITIALISE-DEFAULT-FONTis called.Parameters
FREEwhen set toTwill automatically free any font that is already bound to*DEFAULT-FONT*.FILENAMEis the optional file name of the font, as a STRING. Default value isVera.ttf.SIZEis theINTEGERpoint size of the newFONT.Return
- Returns a new
FONT, orNILif unsuccessful.
[Function]
initialise-font filename size => result
Creates a new
FONTobject of sizeSIZEloaded from the file at locationFILENAME. Automatically initialises theSDL_TTFtruetype font library if the library is uninitialised whenFONTis loaded.Parameters
FILENAMEis the file name of theFONT, of typeSTRING.SIZEis theINTEGERpoint size of the newFONT.Return
- Returns a new
FONT, orNILif unsuccessful.
[Function]
is-font-face-fixed-width &key font => result
Returns
Tif the font face is of a fixed width, orNILotherwise. Fixed width fonts are monospace, meaning every character that exists in the font is the same width.Parameters
FONTis aFONTobject. Binds to*DEFAULT-FONT*by default.Returns
- Retuns
TifFONTis of fixed width, andNILotherwise.
[Function]
is-init => result
Queries the initialization status of the
SDL_TTFtruetype library. ReturnsTif this library is already initialized andNILif uninitialized.
[Function]
new-font fp => result
Creates and returns a new
FONTobject whenFPis a pointer to a foreignTTF_Fontobject. ReturnsNILotherwise.
[Function]
open-font filename size => result
Creates a new
FONTfrom the file loaded at the location specified byFILENAME. NOTE: Does not bind the newFONTto*DEFAULT-FONT*. Does not attempt to initialize theSDL_TTFtruetype library if the library is uninitialised.Parameters
FILENAMEis the name of the truetype font to be loaded, of typeSTRINGSIZEis the size of theFONT, as anINTEGER.Returns
- Returns new
FONTobject if successful, returnsNILif unsuccessful.
[Function]
quit-ttf => result
Uninitializes the
SDL_TTFfont library if already initialized. Increments the*generation*count. ReturnsNIL.
[Function]
render-string-blended text &key font color free cache => result
Render text
TEXTusing fontFONTwith colorCOLORinto a newSURFACE, using the Blended mode. Unless:FREE T, the caller is responsible for freeing the newSURFACE. Use:CACHE Tto cache the newly created surface in theFONTobject.Parameters
TEXTis the text to render, of typeSTRING.FONTis aFONTobject. Binds to*DEFAULT-FONT*by default.COLORis the color used to render theTEXT, of typeSDL:SDL-COLORFREEwhenTwill free the existing cachedSURFACEinFONT.CACHEwhenTwill cache the newly createdSURFACEinFONT.Return
- Returns the new surface
SDL:SDL-SURFACE.Example
(DRAW-STRING-BLENDED "Hello World!" :FONT *DEFAULT-FONT* :COLOR A-COLOR)
[Function]
render-string-shaded text fg-color bg-color &key font free cache => result
Render text
TEXTusing fontFONTwith foreground and background colorsFG-COLORandBG-COLORinto a newSURFACE, using the Solid mode. Unless:FREE T, the caller is responsible for freeing the newSURFACE. Use:CACHE Tto cache the newly created surface in theFONTobject.Parameters
TEXTis the text to render, of typeSTRING.FG-COLORis the foreground color of theTEXT, of typeSDL:SDL-COLORBG-COLORis the background color of theTEXT, of typeSDL:SDL-COLORFONTis aFONTobject. Binds to *DEFAULT-FONT*by default.FREEwhenTwill free the existing cachedSURFACEinFONT.CACHEwhenTwill cache the newly createdSURFACEinFONT.Return
- Returns the new surface
SDL:SDL-SURFACE.Example
(DRAW-STRING-SHADED "Hello World!" fg-col bg-col :FONT DEFAULT-FONT :COLOR A-COLOR)
[Function]
render-string-solid text &key font color free cache => result
Render text
TEXTusing fontFONTwith colorCOLORinto a newSURFACE, using the Solid mode. Unless:FREE T, the caller is responsible for freeing the newSURFACE. Use:CACHE Tto cache the newly created surface in theFONTobject.Parameters
TEXTis the text to render, of typeSTRING.FONTis aFONTobject. Binds to*DEFAULT-FONT*by default.COLORis the color used to render theTEXT, of typeSDL:SDL-COLORFREEwhenTwill free the existing cachedSURFACEinFONT.CACHEwhenTwill cache the newly createdSURFACEinFONT.Return
- Returns the new surface
SDL:SDL-SURFACE.Example
(DRAW-STRING-SOLID "Hello World!" :FONT *DEFAULT-FONT* :COLOR A-COLOR)
[Function]
set-font-style style &key font => result
Sets the rendering style
STYLEof fontFONT. This will flush the internal cache of previously rendered glyphs, even if there is no change in style, so it may be best to check the current style using GET-FONT-STYLE first.Parameters
FONTis aFONTobject.STYLEis a list of one or more::STYLE-NORMAL,:STYLE-BOLD,:STYLE-ITALIC,:STYLE-UNDERLINE. NOTE: Combining:STYLE-UNDERLINEwith anything can cause a segfault, other combinations may also do this.
[Macro]
with-default-font (font) declaration* statement* => result
Binds
FONTto*DEFAULT-FONT*within the scope ofWITH-DEFAULT-FONT.
[Macro]
with-open-font (font-name size) declaration* statement* => result
This is a convenience macro that will first attempt to initialize the truetype font library and if successful, will then open the font in the file
FONT-NAMEprior to evaluating the forms inBODY. Will exit if the library cannot be initialized or theFONTcannot be opened. ClosesFONTafter the forms inBODYhave evaluated.Binds
FONTto a shadowed instance of*DEFAULT-FONT*valid within the scope ofWITH-OPEN-FONT.WITH-OPEN-FONTcalls may be nested.Parameters
FONT-NAMEis the file name of the truetype font to be opened, of typeSTRING.SIZEis theINTEGERpoint size of the font.