-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdingoo_font.lua
More file actions
62 lines (49 loc) · 1.74 KB
/
dingoo_font.lua
File metadata and controls
62 lines (49 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require "string"
---------------------------------------------------------
-- a font library for the dingoo, as native has faults --
---------------------------------------------------------
-- creates a table of images from a font
-- ImageData source, String char_list, Number char_width, Number seperator_width
local function setFontImage(source, char_list, char_width, seperator_width)
local font = {}
font.char_width = char_width
font.char_height = source:getHeight()
font.seperator_width = seperator_width
-- iterate through string and get correct image for each character
for i=1, char_list:len() do
-- get the single substr at i
local char = char_list:sub(i, i)
-- create new imagedata for char
local char_data = love.image.newImageData(font.char_width, font.char_height)
-- copy the section of the char image we want for this character
local j = i -1
local src_x = (j*char_width) + (i*seperator_width)
char_data:paste(source, 0, 0, src_x, 0, font.char_height)
font[char] = love.graphics.newImage(char_data)
end
return font
end
-- print the text at the selected x,y
-- returns true if displayed, false if not
local function dingPrint(font, text, x, y)
-- iterate through each char and print
for i=1, text:len() do
local j = i -1
-- set correct position
local xpos = x + (j*font.char_width) + (j*font.seperator_width)
-- get the correct character to display
local char = text:sub(i, i)
-- check if the char is in the table
if (font[char] ~= nil) then
love.graphics.draw(font[char], xpos, y, 0, 1, 1, 0, 0)
else
return false
end
end
return true
end
-- package
dingoo_font = {
setFontImage = setFontImage,
dingPrint = dingPrint,
}