Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generates a bitmap version of love's default font with an outline.
local infont
local outfont
local glyphstring = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-=_+[]{}\\|;:'\",./<>?"
local teststring = [[
Who packed five dozen old quart jugs in my box?
Prating jokers quizzically vexed me with fibs.
Brawny gods just flocked up to quiz and vex him.
Watch "Jeopardy!", Alex Trebek's fun TV quiz game.
]]
local border = 2
local TEST_CANVAS
local TEXT_Y
function love.load()
love.graphics.setBackgroundColor(100, 100, 100)
love.window.setTitle("Prerenderedish Font Test")
infont = love.graphics.newFont(20)
TEXT_Y = infont:getHeight()
local fheight = infont:getHeight() + border
local canvas = love.graphics.newCanvas(infont:getWidth(glyphstring) + #glyphstring * (border), fheight, "normal")
love.graphics.setCanvas(canvas)
love.graphics.setColor(255, 255, 0)
love.graphics.rectangle("fill", 0, 0, 1, fheight)
love.graphics.setFont(infont)
local offset = 1
local gprint = love.graphics.print
--love.graphics.setBlendMode("premultiplied")
for index = 1, #glyphstring do
local char = glyphstring:sub(index, index)
love.graphics.setColor(0, 0, 0)
for index = 0, border + 1 do
gprint(char, offset, index)
gprint(char, offset + border, index)
end
gprint(char, offset + border / 2, 0)
gprint(char, offset + border / 2, border)
love.graphics.setColor(255, 255, 255)
gprint(char, offset + border / 2, 0)
offset = offset + infont:getWidth(char) + 1
end
offset = 1
for index = 1, #glyphstring do
offset = offset + infont:getWidth(glyphstring:sub(index, index)) + 1
love.graphics.setColor(255, 255, 0)
love.graphics.rectangle("fill", offset, 0, 1, fheight)
end
love.graphics.setCanvas()
TEST_CANVAS = canvas
outfont = love.graphics.newImageFont(canvas:getImageData(), glyphstring)
canvas:getImageData():encode("outfont.png")
end
function text(text, r, g, b, no_inc)
if (r) then
love.graphics.setColor(r, g or r, b or g or r)
else
love.graphics.setColor(255, 255, 255)
end
love.graphics.print(text, 0, TEXT_Y)
local _, lines = love.graphics.getFont():getWrap(text, 800)
TEXT_Y = TEXT_Y + (love.graphics.getFont():getHeight() * lines + 2)
end
function br()
TEXT_Y = TEXT_Y + love.graphics.getFont():getHeight()
end
function love.draw()
love.graphics.setBlendMode("alpha")
love.graphics.setFont(infont)
love.graphics.setColor(255, 255, 255)
love.graphics.draw(TEST_CANVAS, 0, 0)
text("BITMAP FONT SOURCE", 0)
br()
text(teststring)
text("REGULAR FONT", 0)
br()
if (outfont) then
love.graphics.setFont(outfont)
text(teststring)
love.graphics.setFont(infont)
text("BITMAP FONT", 0)
br()
end
TEXT_Y = infont:getHeight() + 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment