Loading
- game.addFont(filename, size)
- Loads a font file with the given filename and the
given size in pixels. This can load TTF and FON formats.
Returns a font object.
Methods
- font:getTextSize(text)
- Returns the width and height of the resulting surface of the
LATIN1 encoded text rendered using font. No
actual rendering is done. The returned height is the same as
returned by font:getHeight().
- font:getHeight()
- Returns the maximum pixel height of all glyphs of the given
font.
- font:getLineSkip()
- Returns the recommended pixel height of a rendered line of
text of the loaded font. This usually is larger than the
result of font:getHeight().
- font:generateImage(text)
- Returns an image containing the text rendered with the
given font. The text color is white and the background is
transparent. To use another color call font:generateImage()
with a table as sole argument.
- font:generateImage(table)
- This is nearly the same as the last one. This time a
table contains the arguments. The first element of the array
part of the table has to be the text. The table
may have an optional color entry. This has to be a table
containing three numbers between 0 and 255, one for each color
component (red, green, blue). An optional fourth entry is the
transparency (0-255).
- font:print(x, y, text)
- Prints the given text at the coordinates x and
y. The text color is white and the background is transparent.
To use another color call font:print()
with a table as sole argument.
Note: This function is REALLY slow! That is
due to the creation of an image everytime it is called. In fact this
function behaves like calling font:generateImage(text) and then
rendering the returned image using image:render(x,y). I don't
recommended to use this function!
- font:print(table)
- This is nearly the same as the last one. This time a
table contains the arguments. The first three elements of the
array part of the table have to be the x- and y-coordinate and
the text. The table may have an optional color entry. This has
to be a table containing three numbers between 0 and 255, one for each
color component (red, green, blue). An optional fourth entry is the
transparency (0-255).
Note: This function is REALLY slow! That is
due to the creation of an image everytime it is called. In fact this
function behaves like calling font:generateImage(table) and then
rendering the returned image using image:render(x,y). I don't
recommended to use this function!
Examples
game.init("Font Test", 600, 400, 32, false)
-- load a font
font = game.addFont("fonts/Vera.ttf", 20)
-- get the recommended line height in pixel
lineSkip = font:getLineSkip()
-- get the text size of a sample text
w, h = font:getTextSize("Hello World")
-- define a color (opaque blue)
cBlue = {0, 0, 255, 255}
-- the transparency defaults to 255 (opaque), so this is the same:
cBlue = {0, 0, 255}
-- generate an image containing some text using the default color
image_1 = font:generateImage("Hello World")
-- generate an image using the defined color cBlue
-- -- because font:generateImage() gets only one parameter and this is a table
-- -- we can omit the parenthesises and just use the curly brackets of the table
-- -- constructor
image_2 = font:generateImage{"Hello World", color = cBlue}
-- this has the same result:
image_2 = font:generateImage{"Hello World", color = {0, 0, 255}}
main = {
render = function(dt)
-- render the already loaded images:
image_1:render(100,50)
image_2:render(100 + w, 50 + lineSkip)
-- render some text using font:print():
font:print(100, 50 + 2*lineSkip, "Hello World")
font:print{100 + w, 50 + 3*lineSkip, "Hello World", color = cBlue}
end
}