Skip to content

Instantly share code, notes, and snippets.

@thykka
Last active October 3, 2021 18:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thykka/6fba27b1eafa36c8ecda9cd529a0bf37 to your computer and use it in GitHub Desktop.
Save thykka/6fba27b1eafa36c8ecda9cd529a0bf37 to your computer and use it in GitHub Desktop.
Line Fonts for PICO-8
--[[
line fonts v0.1 - thykka 2021
==usage==
printl(
text,
x, y,
color,
font_size,
char_spacing,
line_spacing,
wrap_width
)
==customizing the font==
glyphs are defined as a list of lines,
where each line consists of a list of
alternating x and y coordinates.
e.g. the letter A could be drawn like:
____2
_____
__4_5
_____
1___3
where 1-2 and 2-3 represent coordinate
pairs for the first two lines, and 4-5
are coords for the third line.
]]
line_font_glyphs={
[' ']={{1/4,0/1}},
['!']={{1/4,0/1, 1/4,2/3},
{1/4,1/1, 1/4,1/1}},
['"']={{1/3,0/1, 0/1,1/4},
{2/3,0/1, 2/5,1/4}},
['#']={{1/4,0/1, 1/4,1/1},
{3/4,0/1, 3/4,1/1},
{0/1,1/4, 1/1,1/4},
{0/1,3/4, 1/1,3/4}},
['$']={{1/2,0/1, 1/4,1/1},
{3/4,0/1, 1/4,0/1, 0/1,1/4, 1/4,1/2, 1/2,1/2, 3/4,3/4, 1/2,1/1, 0/0,1/1}},
['%']={{0/1,1/1, 2/3,0/1},{0/1,0/1, 0/1,0/1}, {5/8,1/1, 5/8,1/1}},
['&']={{1/2,0/1, 1/4,0/1, 0/1,1/4, 1/4,1/2, 0/1,3/4, 1/4,1/1, 1/2,1/1, 3/4,3/4, 3/4,1/2},{1/4,1/2, 1/1,1/2}},
['\'']={{1/4,0/1, 0/1,1/4}},
['(']={{1/4,0/1, 0/1,1/4, 0/1,3/4, 1/4,1/1}},
[')']={{0/1,0/1, 1/4,1/4, 1/4,3/4, 0/1,1/1}},
['*']={{0/1,1/2, 1/1,1/2}, {1/3,0/1, 3/4,1/1}, {3/4,0/1, 1/4,1/1}},
['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/1, 0/1,1/1, 3/4,1/1}},
['{']={{1/2,0/1, 1/4,0/1, 1/4,1/1, 1/2,1/1},
{0/1,1/2, 1/4,1/2}},
['|']={{0/1,0/1, 0/1,1/1}},
['}']={{0/1,0/1, 1/4,0/1, 1/4,1/1, 0/1,1/1},
{1/4,1/2, 1/2,1/2}},
['~']={{0/1,1/2, 1/4,1/4, 1/2,1/2, 3/4,1/4}},
['○']={{1/4,0/1, 0/1,1/4, 1/4,1/2, 1/2,1/4, 1/4,0/1}}
}
linefont_last_y=0
function printl(message,x,y,color,scale,cspc,lspc,width)
scale=scale or 4
cspc=cspc or 1
lspc=lspc or 4
x,y=x or 0,y or linefont_last_y
ox=x
width=width or 127
for i,letter in ipairs(split(message,'')) do
if x > width-scale then
x=ox
y+=scale+1+lspc
end
x+=draw_letter(letter,x,y,scale,cspc,color)
end
linefont_last_y=y+scale+1+lspc
end
function draw_letter(letter,x,y,scale,spacing,color)
local glyph=line_font_glyphs[letter]
local max_w=0
if not glyph and scale==4 then
print(letter,x,y,2)
return 4
end
for i,points in ipairs(glyph) do
local lx,ly
for p=1,#points,2 do
local xp=x+scale*points[p]
local yp=y+scale*points[p+1]
if lx and ly then
line(lx,ly,xp,yp,color or 8-i)
end
lx,ly=xp,yp
max_w=max(max_w,lx-x+1)
end
end
if max_w > 0 then max_w += spacing end
return max_w --letter width
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment