Font Support
Note
Have a look at the font plugin. This makes the usage of truetype fonts easier.
UTF-8
Scrupp supports UTF-8. If you want to use this feature, remember to edit your scripts using the UTF-8 mode of your editor. Save them with UTF-8 encoding (without BOM).
Loading
- scrupp.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 UTF-8 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. The color of the created image can be changed by setting the appropriate option during rendering (see image:render()).
- font:generateImage(table)
- This is nearly the same as the last one. This time a
table contains the arguments.
table = { text, color = {red, green, blue, [alpha]} -- optional }
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).
Remember: The color of an image can be changed during rendering.
Example
scrupp.init("Font Test", 600, 400, 32, false)
-- load a font
local font = scrupp.addFont("fonts/Vera.ttf", 20)
-- get the recommended line height in pixel
local lineSkip = font:getLineSkip()
-- get the text size of a sample text
local w, h = font:getTextSize("Hello World")
-- define a color (opaque blue)
local cBlue = {0, 0, 255, 255}
-- the transparency defaults to 255 (opaque), so this is the same:
local cBlue = {0, 0, 255}
-- generate an image containing some text using the default color
local 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
local image_2 = font:generateImage{"Hello World", color = cBlue}
-- this has the same result:
local 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)
end
}