firefly/mods/firefly_font/init.lua
2026-02-14 12:37:11 -05:00

46 lines
1.2 KiB
Lua

local ns = firefly
local modname = minetest.get_current_modname()
ns.fonts = {}
function ns.rasterize(str, font, max_width)
if not font then font = "firefly_normal" end
local atlas = ns.fonts[font] or minetest.parse_json(ns.read_file(minetest.get_modpath(modname).."/fonts/"..font..".json"))
if not ns.fonts[font] then
ns.fonts[font] = atlas
end
local newline = string.byte("\n", 1)
local out = {}
local width = 0
local height = 0
local x = 0
local y = 0
for i = 0, #str do
local char = str:byte(i)
if not char then goto continue end
if char == newline then
goto continue
end
local glyph = atlas[string.char(char)]
local num = #out
out[num +1] = ":"
out[num +2] = x
out[num +3] = ","
out[num +4] = y
out[num +5] = "="
out[num +6] = font
out[num +7] = "/firefly_char_"
out[num +8] = char
out[num +9] = ".png"
x = x +glyph.width +glyph.advance
if x > width then
width = x
end
::continue::
end
return "[combine:"..width.."x"..height..table.concat(out)
end