cl fond
1.1.0Bindings to libfond, a simple text rendering engine for OpenGL
Table of Contents
About cl-fond
This is a bindings library to libfond, a simple OpenGL text rendering engine.
How To
Precompiled versions of the underlying library are included in this. If you want to build it manually however, refer to the libfond repository.
Load the system through ASDF or Quicklisp:
(ql:quickload :cl-fond)
Before you can create any of the objects, you need to have an OpenGL 3.3+ context current. Creating a context is outside of the scope of this library. Please refer to your framework. Once you do have a context handy, you can create a font object:
(defvar *font* (cl-fond:make-font #p"~/test.ttf" "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .!?"))
The string there specifies the character set for which the font should be loaded. You will only be able to render characters from that set. Note that the more characters you have and the bigger you make the font size, the larger the texture it will have to allocate to map out the characters. If there is an error of some kind, like being unable to read the file or fit it into a texture atlas, a condition is signalled. Otherwise the font should be readily loaded and you can start working with it.
(compute-extent *font* "Hello there!")
If you actually want to render text, you are given two options:
- Using the readily provided buffer class
- Rendering the text manually
The former has the advantage of being much simpler, at the cost of rendering to an offscreen texture instead of directly to the screen. The latter gives you more control over how you place and render the text at the cost of having to write your own shaders to do it. Let's look at the easy way first:
(defvar *buffer* (cl-fond:make-buffer *font* 512 30))
With a default font size of 20px, 30px for the height should be enough room. This function may again error if it fails to properly allocate the resources it needs in the back. Once it is ready you can start rendering text to it.
(cl-fond:render *buffer* "Sup man." :y (height *font*))
We set the y coordinate to the height of the font here, as libfond sets 0,0 in the top left corner and x,y of the text on the left of its baseline. Thus in order to make the text visible on the buffer, we need to offset the y coordinate by at least the font's height. Once rendered, it returns the OpenGL texture that it rendered to. You can then render this texture onto a quad in your primary render code. Keep in mind that you need to respect the aspect ratio of the buffer, and that scaling its texture beyond the specified size will make things look blurry.
If you want to manually render the characters instead, you'll want to use compute-text as follows:
(cl-fond:compute-text *font* "Heyo.")
Returned by it will be an OpenGL vertex array to draw with and the number of elements it contains. After binding your shader program and setting things up properly, you can then draw the characters as follows:
(gl:bind-texture :texture-2d (cl-fond:texture *font*))
(gl:bind-vertex-array vao)
(%gl:draw-elements :triangles count :unsigned-int 0)
The VAO contains two vec2 inputs: the position and the texture coordinate. Note that the position is scaled to pixel size. You will likely want to adapt this in your vertex shader to fit appropriately. In your fragment shader you will only want to read out the r component of the texture. Every other component is 0.
See the default vertex and fragment shaders used by the buffer for an example.
Also See
- cepl.fond for integration into CEPL.
System Information
Definition Index
-
CL-FOND-CFFI
- ORG.SHIRAKUMO.FRAF.FOND.CFFI
No documentation provided.-
EXTERNAL SPECIAL-VARIABLE *STATIC*
Variable containing the path to the static directory. That directory contains the precompiled library binaries.
-
EXTERNAL CLASS BUFFER
This struct allows for convenience in rendering, as it will render text for you into a texture. You must allocate this struct yourself and make sure that it is zeroed out before you do anything with it. Not zeroing out will land you in a world of pain. See BUFFER-FONT See BUFFER-TEXTURE See BUFFER-WIDTH See BUFFER-HEIGHT See BUFFER-PROGRAM See BUFFER-FRAMEBUFFER See FREE-BUFFER See LOAD-BUFFER See RENDER-BUFFER See RENDER-BUFFER-U
-
EXTERNAL CLASS EXTENT
This struct contains information about the extents of a text. You must allocate this struct yourself and make sure that it is zeroed out before you do anything with it. Not zeroing out will land you in a world of pain. See EXTENT-L See EXTENT-R See EXTENT-T See EXTENT-B See EXTENT-GAP See COMPUTE-EXTENT See COMPUTE-EXTENT-U
-
EXTERNAL CLASS FONT
This is the primary struct that contains all relevant information about a font. You must allocate this struct yourself and make sure that it is zeroed out before you do anything with it. Not zeroing out will land you in a world of pain. See FONT-FILE See FONT-INDEX See FONT-SIZE See FONT-CHARACTERS See FONT-CODEPOINTS See FONT-WIDTH See FONT-HEIGHT See FONT-OVERSAMPLE See FONT-ATLAS See FONT-FONTDATA See FONT-CHARDATA See FONT-FONTINFO See FONT-CONVERTED-CODEPOINTS See LOAD-FONT See LOAD-FONT-FIT See FREE-FONT See COMPUTE-TEXT See COMPUTE-TEXT-U See COMPUTE-EXTENT See COMPUTE-EXTENT-U
-
EXTERNAL CONDITION ERROR
No documentation provided. -
EXTERNAL FUNCTION BUFFER-FONT
- POINTER-TO-BUFFER
Pointer to the font that it renders. This can be exchanged for another font, if you should decide to want to render with a different font. See BUFFER
-
EXTERNAL FUNCTION (SETF BUFFER-FONT)
- VALUE
- POINTER-TO-BUFFER
No documentation provided. -
EXTERNAL FUNCTION BUFFER-FRAMEBUFFER
- POINTER-TO-BUFFER
This is an internal field. See BUFFER
-
EXTERNAL FUNCTION (SETF BUFFER-FRAMEBUFFER)
- VALUE
- POINTER-TO-BUFFER
No documentation provided. -
EXTERNAL FUNCTION BUFFER-HEIGHT
- POINTER-TO-BUFFER
The height of the texture. See BUFFER
-
EXTERNAL FUNCTION (SETF BUFFER-HEIGHT)
- VALUE
- POINTER-TO-BUFFER
No documentation provided. -
EXTERNAL FUNCTION BUFFER-PROGRAM
- POINTER-TO-BUFFER
This is an internal field. See BUFFER
-
EXTERNAL FUNCTION (SETF BUFFER-PROGRAM)
- VALUE
- POINTER-TO-BUFFER
No documentation provided. -
EXTERNAL FUNCTION BUFFER-TEXTURE
- POINTER-TO-BUFFER
The OpenGL texture ID to which this buffer renders to. See BUFFER
-
EXTERNAL FUNCTION (SETF BUFFER-TEXTURE)
- VALUE
- POINTER-TO-BUFFER
No documentation provided. -
EXTERNAL FUNCTION BUFFER-WIDTH
- POINTER-TO-BUFFER
The width of the texture. See BUFFER
-
EXTERNAL FUNCTION (SETF BUFFER-WIDTH)
- VALUE
- POINTER-TO-BUFFER
No documentation provided. -
EXTERNAL FUNCTION COMPUTE-EXTENT
- FONT
- TEXT
- EXTENT
-
EXTERNAL FUNCTION COMPUTE-EXTENT-U
- FONT
- TEXT
- SIZE
- EXTENT
Same as COMPUTE-EXTENT but taking an UTF32 encoded string and its size. See COMPUTE-EXTENT See FONT See EXTENT
-
EXTERNAL FUNCTION COMPUTE-TEXT
- FONT
- TEXT
- N
- VAO
Compute the Vertex Array Object to render the given text. Here, n and vao are output arguments, containing the number of elements and the OpenGL VAO ID respectively. The text must be UTF8 encoded and null-terminated. The returned VAO contains two arrays, at location 0 and 1, with both being vec2s. The first being the vertex coordinates and the second being the texture coordinates. The vertex coordinates start at 0 and increase in x and y as per the font's size. You are responsible for scaling it as appropriate for your display. The texture coordinates are for the font's atlas texture. If the text contains a Linefeed character (U+000A) a new line is started automatically by resetting X to 0 and decreasing Y by the necessary height for a new line. See FONT
-
EXTERNAL FUNCTION COMPUTE-TEXT-U
- FONT
- TEXT
- SIZE
- N
- VAO
Same as COMPUTE-EXTENT but taking an UTF32 encoded string and its size. See COMPUTE-TEXT See FONT
-
EXTERNAL FUNCTION DECODE-UTF8
- STRING
- DECODED
- SIZE
Decode the given UTF8 string into an UTF32 string. The resulting string is put into decoded, and its size is put into size. This is used by the non _u functions to decode the string. You may want to use this internally, if you need to re-use the same string often and don't wnat to pay the conversion cost.
-
EXTERNAL FUNCTION ERROR
- DATUM
- &REST
- ARGUMENTS
Invoke the signal facility on a condition formed from DATUM and ARGUMENTS. If the condition is not handled, the debugger is invoked.
-
EXTERNAL FUNCTION EXTENT-B
- POINTER-TO-EXTENT
How far down the text extends from its baseline. See EXTENT
-
EXTERNAL FUNCTION (SETF EXTENT-B)
- VALUE
- POINTER-TO-EXTENT
No documentation provided. -
EXTERNAL FUNCTION EXTENT-GAP
- POINTER-TO-EXTENT
The gap between lines of the text. See EXTENT
-
EXTERNAL FUNCTION (SETF EXTENT-GAP)
- VALUE
- POINTER-TO-EXTENT
No documentation provided. -
EXTERNAL FUNCTION EXTENT-L
- POINTER-TO-EXTENT
How far to the left the text extends from zero. See EXTENT
-
EXTERNAL FUNCTION (SETF EXTENT-L)
- VALUE
- POINTER-TO-EXTENT
No documentation provided. -
EXTERNAL FUNCTION EXTENT-R
- POINTER-TO-EXTENT
How far to the right the text extends from zero. See EXTENT
-
EXTERNAL FUNCTION (SETF EXTENT-R)
- VALUE
- POINTER-TO-EXTENT
No documentation provided. -
EXTERNAL FUNCTION EXTENT-T
- POINTER-TO-EXTENT
How far up the text extends from its baseline. See EXTENT
-
EXTERNAL FUNCTION (SETF EXTENT-T)
- VALUE
- POINTER-TO-EXTENT
No documentation provided. -
EXTERNAL FUNCTION FOND-ERROR
Return the current error code.
-
EXTERNAL FUNCTION FOND-ERROR-STRING
- ERROR
Return a string for a human-readable error message of the given error code.
-
EXTERNAL FUNCTION FONT-ATLAS
- POINTER-TO-FONT
The OpenGL texture ID for the atlas. See FONT
-
EXTERNAL FUNCTION (SETF FONT-ATLAS)
- VALUE
- POINTER-TO-FONT
No documentation provided. -
EXTERNAL FUNCTION FONT-CHARACTERS
- POINTER-TO-FONT
A UTF8 encoded string of characters that this font instance will be able to render. Must be null-terminated. See FONT
-
EXTERNAL FUNCTION (SETF FONT-CHARACTERS)
- VALUE
- POINTER-TO-FONT
No documentation provided. -
EXTERNAL FUNCTION FONT-CHARDATA
- POINTER-TO-FONT
This is an internal field. See FONT
-
EXTERNAL FUNCTION (SETF FONT-CHARDATA)
- VALUE
- POINTER-TO-FONT
No documentation provided. -
EXTERNAL FUNCTION FONT-CODEPOINTS
- POINTER-TO-FONT
An array of Unicode codepoints that this font instance will be able to render. Must be null-terminated. This is automatically filled in for you if it is NULL and the characters field is provided instead. See FONT
-
EXTERNAL FUNCTION (SETF FONT-CODEPOINTS)
- VALUE
- POINTER-TO-FONT
No documentation provided. -
EXTERNAL FUNCTION FONT-CONVERTED-CODEPOINTS
- POINTER-TO-FONT
This is an internal field. See FONT
-
EXTERNAL FUNCTION (SETF FONT-CONVERTED-CODEPOINTS)
- VALUE
- POINTER-TO-FONT
No documentation provided. -
EXTERNAL FUNCTION FONT-FILE
- POINTER-TO-FONT
Accessor for the path to the TTF file. See FONT
-
EXTERNAL FUNCTION (SETF FONT-FILE)
- VALUE
- POINTER-TO-FONT
No documentation provided. -
EXTERNAL FUNCTION FONT-FONTDATA
- POINTER-TO-FONT
This is an internal field. See FONT
-
EXTERNAL FUNCTION (SETF FONT-FONTDATA)
- VALUE
- POINTER-TO-FONT
No documentation provided. -
EXTERNAL FUNCTION FONT-FONTINFO
- POINTER-TO-FONT
This is an internal field. See FONT
-
EXTERNAL FUNCTION (SETF FONT-FONTINFO)
- VALUE
- POINTER-TO-FONT
No documentation provided. -
EXTERNAL FUNCTION FONT-HEIGHT
- POINTER-TO-FONT
The height of the glyph texture atlas. See FONT
-
EXTERNAL FUNCTION (SETF FONT-HEIGHT)
- VALUE
- POINTER-TO-FONT
No documentation provided. -
EXTERNAL FUNCTION FONT-INDEX
- POINTER-TO-FONT
Accessor for the index of the font within the TTF file. You probably don't need to set this. See FONT
-
EXTERNAL FUNCTION (SETF FONT-INDEX)
- VALUE
- POINTER-TO-FONT
No documentation provided. -
EXTERNAL FUNCTION FONT-OVERSAMPLE
- POINTER-TO-FONT
How much oversampling should be done. Higher oversampling might improve the quality of the rendering, but will need a much bigger atlas size: width*oversampling,height*oversampling See FONT
-
EXTERNAL FUNCTION (SETF FONT-OVERSAMPLE)
- VALUE
- POINTER-TO-FONT
No documentation provided. -
EXTERNAL FUNCTION FONT-SIZE
- POINTER-TO-FONT
The vertical font size in pixels. If you render it above this resolution, you'll get blurring. See FONT
-
EXTERNAL FUNCTION (SETF FONT-SIZE)
- VALUE
- POINTER-TO-FONT
No documentation provided. -
EXTERNAL FUNCTION FONT-WIDTH
- POINTER-TO-FONT
The width of the glyph texture atlas. See FONT
-
EXTERNAL FUNCTION (SETF FONT-WIDTH)
- VALUE
- POINTER-TO-FONT
No documentation provided. -
EXTERNAL FUNCTION FREE-BUFFER
- BUFFER
Free all the data that was allocated into the struct by fond_load_buffer. This will /not/ free the font struct. See BUFFER
-
EXTERNAL FUNCTION FREE-FONT
- FONT
Free all the data that was allocated into the struct by LOAD-FONT* This will /not/ free the characters array, the file array, or the codepoints array if the codepoints array was not computed by LOAD-FONT* See FONT
-
EXTERNAL FUNCTION LOAD-BUFFER
- BUFFER
Load the buffer struct and allocate the necessary OpenGL data. The following fields must be set in the struct: font width height See BUFFER
-
EXTERNAL FUNCTION LOAD-FONT
- FONT
Load the font struct and allocate the necessary OpenGL data. The following fields must be set in the struct: file size width height characters or codepoints See FONT
-
EXTERNAL FUNCTION LOAD-FONT-FIT
- FONT
- MAX-SIZE
Load the font struct, attempting to fit an atlas automatically. This may not result in the most compact atlas possible. max_size is the maximum size of the width or height that can be reached before it gives up. The following fields must be set in the struct: file size characters or codepoints See FONT
-
EXTERNAL FUNCTION RENDER-BUFFER
- BUFFER
- TEXT
- X
- Y
- COLOR
Render the given text to the buffer's texture. The text will be rendered at the given offset, with x and y being in pixels. color can be either 0 for white text, or an array of four floats, representing RGBA of the text's colour in that order. See BUFFER
-
EXTERNAL FUNCTION RENDER-BUFFER-U
- BUFFER
- TEXT
- SIZE
- X
- Y
- COLOR
Same as RENDER-BUFFER but taking an UTF32 encoded string and its size. See RENDER-BUFFER See BUFFER
-
EXTERNAL FUNCTION UPDATE-TEXT
- FONT
- TEXT
- N
- VBO
- EBO
No documentation provided. -
EXTERNAL FUNCTION UPDATE-TEXT-U
- FONT
- TEXT
- SIZE
- N
- VBO
- EBO
No documentation provided. -
EXTERNAL OPTIMIZER ERROR
No documentation provided. -
EXTERNAL TRANSFORM ERROR
No documentation provided.
-
CL-FOND
- ORG.SHIRAKUMO.FRAF.FOND
No documentation provided.-
EXTERNAL CLASS BUFFER
This is a representation of a text buffer. The buffer allows you to conveniently draw text to a texture. You will then merely need to draw the texture to the screen, rather than needing to manually create a shader and handle the drawing yourself. See C-OBJECT See FONT See MAKE-BUFFER See RENDER See WIDTH See HEIGHT See TEXTURE
-
EXTERNAL CLASS C-OBJECT
Base class for all objects that are tied to a foreign object somehow. See HANDLE See ALLOCATE-HANDLE See FREE-HANDLE See FREE
-
EXTERNAL CLASS FONT
This is a representation of a font file. Using this class you can draw and compute text for a specific font at a specific size. The library will take care of rendering the font glyphs to an atlas to allow fast rendering of text. See C-OBJECT See MAKE-FONT See COMPUTE-TEXT See COMPUTE-EXTENT See FILE See SIZE See TEXT-HEIGHT See WIDTH See HEIGHT See TEXTURE See CHARSET
-
EXTERNAL CONDITION FOND-ERROR
Condition signalled when an error occurs in the underlying libfond. See ERROR-CODE
-
EXTERNAL FUNCTION CHARSET
- FONT
Returns a string containing all the characters this font instance is capable of drawing. See FONT
-
EXTERNAL FUNCTION COMPUTE-EXTENT
- FONT
- TEXT
- &KEY
- START
- END
Compute the extent of the given text in pixels. Returns a property list with the following entries: :L --- The extent to the left from zero. :R --- The extent to the right from zero. :T --- The extent up from the baseline. :B --- The extent down from the baseline. :GAP --- The gap between two lines of text. See FONT
-
EXTERNAL FUNCTION COMPUTE-TEXT
- FONT
- TEXT
Compute the vertex array object to draw the given text. Returns two values: the OpenGL VAO ID, and the number of elements stored in the VAO. In order to actually use this, you will need to bind the font's texture and use a vertex and fragment shader that draw the VAO as appropriate. The VAO contains two arrays of vec2s for this purpose: the first being the vertex coordinates, and the second being the texture coordinates. The texture only contains a red component, with the other components being set to zero. See FONT
-
EXTERNAL FUNCTION FILE
- FONT
Returns the pathname for the TTF file this font contains. See FONT
-
EXTERNAL FUNCTION MAKE-BUFFER
- FONT
- WIDTH
- HEIGHT
-
EXTERNAL FUNCTION MAKE-FONT
- FILE
- CHARSET
- &REST
- ARGS
- &KEY
- INDEX
- SIZE
- OVERSAMPLE
- WIDTH
- HEIGHT
Create a new font instance for the given TTF file and character set. The charset should be a string containing all characters that the font should be able to draw. The index is the index of the font within the file. Some TTF files can contain multiple fonts in one. Usually, you won't need to set this parameter. Oversample is the oversampling factor that should be used. Using oversampling might result in better quality rendering, at the cost of massively increasing the necessary size of the atlas. The necessary size is multiplied by the oversampling factor in both dimensions. The width and height are optional factors that when given specify the starting texture atlas size to use to attempt to fit the glyphs. If this size is too small, an error is signalled. See FONT
-
EXTERNAL FUNCTION SIZE
- FONT
Returns the font's size in pixels. See FONT
-
EXTERNAL FUNCTION TEXT-HEIGHT
- FONT
Returns the height of a text line from its baseline. See FONT
-
EXTERNAL FUNCTION UPDATE-TEXT
- FONT
- TEXT
- VBO
- EBO
No documentation provided. -
EXTERNAL GENERIC-FUNCTION ERROR-CODE
- CONDITION
The error code that the condition carries. See FOND-ERROR See CL-FOND-CFFI:FOND-ERROR-STRING
-
EXTERNAL GENERIC-FUNCTION (SETF ERROR-CODE)
- NEW-VALUE
- CONDITION
No documentation provided. -
EXTERNAL GENERIC-FUNCTION FONT
- OBJECT
Accessor to the font that this buffer uses to render with. You can change this at any time to switch what to render with.
-
EXTERNAL GENERIC-FUNCTION (SETF FONT)
- NEW-VALUE
- OBJECT
No documentation provided. -
EXTERNAL GENERIC-FUNCTION FREE
- OBJECT
Immediately frees the foreign object associated with the instance.
-
EXTERNAL GENERIC-FUNCTION HANDLE
- OBJECT
Accessor to the pointer to the foreign object. See C-OBJECT
-
EXTERNAL GENERIC-FUNCTION (SETF HANDLE)
- NEW-VALUE
- OBJECT
No documentation provided. -
EXTERNAL GENERIC-FUNCTION HEIGHT
- FONT
-
EXTERNAL GENERIC-FUNCTION RENDER
- BUFFER
- TEXT
- &KEY
- X
- Y
- COLOR
Render the given text to the buffer. The X and Y coordinates are offsets inside the texture, counting from the top left, as is usual for graphical applications. Thus Y grows downwards, rather than upwards like in OpenGL. The color should be a list of floats in the range of [0,1] representing RGBA in that order, defaulting to 1 for each component. Returns the OpenGL texture ID that we rendered to.
-
EXTERNAL GENERIC-FUNCTION TEXTURE
- FONT
-
EXTERNAL GENERIC-FUNCTION WIDTH
- FONT