Last active
February 28, 2018 11:18
-
-
Save moechofe/f74ebf546b29c04fe554458376d501e3 to your computer and use it in GitHub Desktop.
Small bitmap font tool, extract from a bigger project. May not works without proper integration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local glyphsMesh | |
local fontSelected=1 | |
local fontData={ | |
{row=0,col=0,space=5,spaces={ | |
[32]=3, | |
[33]=2,[34]=7,[35]=6,[36]=6,[37]=6,[39]=2,[40]=3,[41]=3,[42]=6,[43]=6,[44]=3,[45]=4,[46]=2,[47]=4,[48]=4, | |
[49]=3,[50]=4,[51]=4,[52]=4,[53]=4,[54]=4,[55]=4,[56]=4,[57]=4,[58]=2,[59]=3,[60]=4,[61]=4,[62]=4,[64]=7, | |
[73]=4,[74]=4,[75]=6,[76]=4,[77]=6, | |
[84]=6,[87]=6,[89]=6,[91]=3,[92]=4,[93]=3,[94]=4,[96]=7, | |
[102]=4,[105]=2,[106]=3,[108]=3,[109]=6, | |
[114]=4,[116]=4,[119]=6,[122]=4,[123]=4,[124]=2,[125]=4,[126]=6, | |
}}, | |
{row=0,col=16,space=4,spaces={ | |
[32]=3, | |
[33]=2,[34]=5,[35]=6,[36]=5,[38]=5,[39]=2,[40]=3,[41]=3,[44]=2,[45]=3,[46]=2, | |
[58]=2,[59]=2,[60]=3,[61]=3,[62]=3,[64]=7,[77]=6, | |
[91]=3,[93]=3,[94]=4,[96]=5, | |
[105]=3,[106]=3,[108]=3,[109]=6, | |
[114]=3,[116]=3,[124]=2, | |
}} | |
} | |
local function glyphsBuild(m,x,y,str,row,col) | |
local fnt=fontData[fontSelected] | |
local w,h=m.texture.width,m.texture.height | |
for i=1,#str do | |
local c=sb(str,i) | |
local i=c-33 | |
local q=m:addRect(x,y,2*4,3*4) | |
m:setRectTex(q,(i%16+fnt.col)*2*4/w,mf((i/16)+fnt.row+row)*3*4/h,2*4/w,3*4/h) | |
m:setRectColor(q,col or color(255,255,255)) | |
x=x+(fnt.spaces[c] or fnt.space) | |
end | |
end | |
function fnt(fnt) | |
fontSelected=(fnt-1)%#fontData+1 | |
end | |
function txt(x,y,str,col,shadow,outline) | |
if outline then | |
if shadow then | |
glyphsBuild(glyphsMesh,x+1,y-1,str,6,shadow) | |
end | |
glyphsBuild(glyphsMesh,x,y,str,6,outline) | |
end | |
if shadow then | |
glyphsBuild(glyphsMesh,x+1,y-1,str,0,shadow) | |
end | |
glyphsBuild(glyphsMesh,x,y,str,0,col) | |
end | |
function setup() | |
glyphsMesh=mesh() | |
glyphsMesh.texture=readImage("Project:fonts") | |
fnt(1) | |
txt(20,100,"Test",color(255,255,255)) | |
txt(20,200,"Test",color(255,255,255),false,color(0,0,0)) | |
end | |
function draw() | |
background(63,63,63) | |
glyphsMesh:draw() | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment