Font Render snippet for TIC


This is the code snippet for game TIC - tiny computer.
Game created by Vadim Grigoruk.
Snippet written by Crutiatix.

ABOUT:

Created for everyone who want's to use various fonts in Its game created in TIC.

SPECIFICATION:

function parseFont(font, new_font, word_space, letter_space)

function print2("string", x, y, color, font)

HOW TO USE:

1. Create font at BitFontMaker2 or browse a gallery.

2. Copy&Edit (from the gallery)

3. Click on the Data Import/Export

4. Copy a font data

5. Create in the code a variable and assign to it single-quoted a font data pasted from BitFontMaker2

6. Copy the snipped from CODE section to your code.

7. Call function parseFont() outside TIC() function. (argumetns described in specification)

8. Print a text by function print2()

CODE :

Minimal:

function parseFont (f,n,s,ss) function drawLetter (d,x,y,c) function string:replace(i,s)return self:sub(1,i-1)..s..self:sub(i+1) end; function bin(n,b) local r="";if not b then b=16 end while n>0 do r=r..tostring(n%2);n=math.floor(n/2) end if #r<b then for i=1,b-#r do r=r.."0" end end return r end;local lw = 0;x = x-1;y=y-1;local mb="";for iy,r in ipairs(d) do local b=bin(r) for ix=1,#b do local letter=b:sub(ix,ix) if ix>#mb then mb=mb.."0" end if letter=="1" then pix(x+ix,y+iy,c);mb=mb:replace(ix,"1") end end end for i=1,#mb do local s=mb:sub(i,i) if s=="1" then lw=lw+1 end end return lw end; local r=f:gsub("%[","{");r=r:gsub("%]","}");r=r:gsub(":","=");r=r:gsub('"(%d+)"=','[%1]=');r=r:gsub('"([%a%d_]+)"=','%1=');r=n.."="..r;load(r)();load(n..".spacing="..tostring(ss))();load(n..".space="..tostring(s))()end
function print2 (t,x,y,c,f) local a,b=65535,32769; local ns={a,b,b,b,b,b,b,b,b,b,b,b,b,b,b,a};local s="";local sc=0;local ap=0 for i=1, #t do s=t:sub(i,i);sc=string.byte(s) if f[sc] then ap=ap+drawLetter(f[sc],x+ap,y,c) elseif sc==32 then ap=ap+f.space else ap=ap+drawLetter(ns,x+ap,y,c) end ap=ap+f.spacing end end 

Commented with demo:

--font copied from http://www.pentacom.jp/pentacom/bitfontmaker2/edit...
local font = '{"33":[6,15,31,39,39,39,38,39,31,38,36,24,0,0,0,0],"34":[54,127,255,294,294,252,216,0,0,0,0,0,0,0,0,0],"35":[72,252,510,735,1758,2054,1759,1758,2052,1752,720,288,0,0,0,0],"36":[8,62,127,223,263,343,214,262,351,351,342,260,216,32,0,0],"37":[206,511,1023,1223,726,580,1912,3900,4540,5528,4432,3616,0,0,0,0],"38":[28,62,126,142,172,142,479,911,1334,692,1352,560,0,0,0,0],"39":[6,15,31,38,38,28,24,0,0,0,0,0,0,0,0,0],"40":[12,30,63,79,39,39,39,39,39,39,39,38,36,72,48,0],"41":[6,15,30,38,78,78,78,78,78,78,78,79,78,36,24,0],"42":[0,508,476,340,396,476,428,372,508,0,0,0,0,0,0,0],"43":[0,24,60,126,159,415,518,516,408,144,96,0,0,0,0,0],"44":[0,0,0,0,0,0,6,15,31,38,39,46,20,8,0,0],"45":[0,0,0,0,14,31,63,70,68,56,0,0,0,0,0,0],"46":[0,0,0,0,0,0,6,15,31,38,36,24,0,0,0,0],"47":[96,240,504,632,316,316,158,158,79,78,36,24,0,0,0,0],"48":[28,62,127,143,263,295,295,295,294,260,136,112,0,0,0,0],"49":[24,60,126,159,143,134,156,156,156,152,144,96,0,0,0,0],"50":[28,62,127,143,262,294,319,143,199,262,260,248,0,0,0,0],"51":[30,63,127,134,260,316,158,159,319,262,132,120,0,0,0,0],"52":[48,120,252,318,287,271,295,294,516,536,288,192,0,0,0,0],"53":[62,127,255,263,263,230,135,319,295,262,136,112,0,0,0,0],"54":[28,62,127,143,263,359,135,295,294,260,136,112,0,0,0,0],"55":[62,127,255,262,260,286,158,143,79,70,36,24,0,0,0,0],"56":[28,62,127,143,294,294,143,143,294,292,136,112,0,0,0,0],"57":[28,62,127,143,263,294,262,271,318,260,136,112,0,0,0,0],"58":[0,6,15,31,38,36,30,15,31,38,36,24,0,0,0,0],"59":[0,6,15,31,38,36,30,15,31,38,39,46,20,8,0,0],"60":[24,60,126,159,143,71,38,68,136,144,96,0,0,0,0,0],"61":[0,0,126,255,511,518,519,511,518,516,504,0,0,0,0,0],"62":[6,15,31,38,68,142,159,143,70,36,24,0,0,0,0,0],"63":[60,126,255,271,614,612,312,156,124,152,144,96,0,0,0,0],"64":[0,1920,8160,16368,32760,29816,64444,64476,64476,56284,27096,29240,16368,8160,1920,0],"65":[60,126,255,271,519,615,615,615,519,518,612,408,0,0,0,0],"66":[62,127,255,263,615,615,263,263,615,614,260,248,0,0,0,0],"67":[120,252,510,543,1039,1223,999,998,1220,1032,528,480,0,0,0,0],"68":[62,127,255,263,519,1127,1255,1255,1127,518,260,248,0,0,0,0],"69":[126,255,511,519,519,487,519,519,487,518,516,504,0,0,0,0],"70":[126,255,511,519,519,487,263,263,231,38,36,24,0,0,0,0],"71":[120,252,510,543,527,1991,2087,2086,2500,2056,1040,992,0,0,0,0],"72":[198,495,1023,1255,1255,1255,1031,1031,1255,1254,1188,792,0,0,0,0],"73":[6,15,31,39,39,39,39,39,39,38,36,24,0,0,0,0],"74":[24,60,124,156,156,158,159,159,134,132,72,48,0,0,0,0],"75":[198,495,1023,1255,615,295,135,135,263,614,1188,792,0,0,0,0],"76":[6,15,31,39,39,39,103,231,487,518,516,504,0,0,0,0],"77":[774,1935,4063,5095,4551,4551,4231,4231,4103,4646,4964,3544,0,0,0,0],"78":[198,495,1023,1255,1223,1159,1031,1031,1063,1126,1188,792,0,0,0,0],"79":[120,252,510,543,1039,2247,2535,2534,2244,1032,528,480,0,0,0,0],"80":[62,127,255,263,519,615,519,263,231,38,36,24,0,0,0,0],"81":[120,252,510,543,1039,2247,2535,2534,2116,1032,2064,2528,1536,0,0,0],"82":[126,255,511,519,1031,1127,519,263,551,1126,1188,792,0,0,0,0],"83":[124,254,511,527,518,452,142,287,575,518,260,248,0,0,0,0],"84":[126,255,511,518,516,412,156,156,156,152,144,96,0,0,0,0],"85":[198,495,1023,1255,1255,1255,1255,1255,1030,1028,520,496,0,0,0,0],"86":[102,255,511,615,615,615,614,614,268,264,144,96,0,0,0,0],"87":[1638,4095,8191,9831,9831,9255,8199,8582,8582,4492,4680,3120,0,0,0,0],"88":[198,495,1023,1255,1094,524,286,287,527,1094,1188,792,0,0,0,0],"89":[102,255,511,615,614,516,268,156,156,152,144,96,0,0,0,0],"90":[126,255,511,518,516,316,158,207,487,518,516,504,0,0,0,0],"91":[30,63,63,71,55,23,23,23,23,23,23,23,54,68,56,0],"92":[6,15,30,38,76,76,152,152,304,288,576,384,0,0,0,0],"93":[14,31,62,68,92,92,92,92,92,92,94,95,94,68,56,0],"94":[8,28,60,94,94,143,175,294,340,136,0,0,0,0,0,0],"95":[0,0,0,0,0,0,0,0,0,0,126,255,511,518,516,504],"96":[2,7,14,20,40,80,32,0,0,0,0,0,0,0,0,0],"97":[60,126,255,271,519,615,615,615,519,518,612,408,0,0,0,0],"98":[62,127,255,263,615,615,263,263,615,614,260,248,0,0,0,0],"99":[120,252,510,543,1039,1223,999,998,1220,1032,528,480,0,0,0,0],"100":[62,127,255,263,519,1127,1255,1255,1127,518,260,248,0,0,0,0],"101":[126,255,511,519,519,487,519,519,487,518,516,504,0,0,0,0],"102":[126,255,511,519,519,487,263,263,231,38,36,24,0,0,0,0],"103":[120,252,510,543,527,1991,2087,2086,2500,2056,1040,992,0,0,0,0],"104":[198,495,1023,1255,1255,1255,1031,1031,1255,1254,1188,792,0,0,0,0],"105":[6,15,31,39,39,39,39,39,39,38,36,24,0,0,0,0],"106":[24,60,124,156,156,158,159,159,134,132,72,48,0,0,0,0],"107":[198,495,1023,1255,615,295,135,135,263,614,1188,792,0,0,0,0],"108":[6,15,31,39,39,39,103,231,487,518,516,504,0,0,0,0],"109":[774,1935,4063,5095,4551,4551,4231,4231,4103,4646,4964,3544,0,0,0,0],"110":[198,495,1023,1255,1223,1159,1031,1031,1063,1126,1188,792,0,0,0,0],"111":[120,252,510,543,1039,2247,2535,2534,2244,1032,528,480,0,0,0,0],"112":[62,127,255,263,519,615,519,263,231,38,36,24,0,0,0,0],"113":[120,252,510,543,1039,2247,2535,2534,2116,1032,2064,2528,1536,0,0,0],"114":[126,255,511,519,1031,1127,519,263,551,1126,1188,792,0,0,0,0],"115":[124,254,511,527,518,452,142,287,575,518,260,248,0,0,0,0],"116":[126,255,511,518,516,412,156,156,156,152,144,96,0,0,0,0],"117":[198,495,1023,1255,1255,1255,1255,1255,1030,1028,520,496,0,0,0,0],"118":[102,255,511,615,615,615,614,614,268,264,144,96,0,0,0,0],"119":[1638,4095,8191,9831,9831,9255,8199,8582,8582,4492,4680,3120,0,0,0,0],"120":[198,495,1023,1255,1094,524,286,287,527,1094,1188,792,0,0,0,0],"121":[102,255,511,615,614,516,268,156,156,152,144,96,0,0,0,0],"122":[126,255,511,518,516,316,158,207,487,518,516,504,0,0,0,0],"123":[12,30,62,78,46,46,47,46,38,46,46,46,44,72,48,0],"124":[6,15,31,39,39,39,39,39,39,39,39,39,38,36,24,0],"125":[6,15,30,38,46,46,46,46,78,46,46,47,46,36,24,0],"126":[0,0,76,254,511,719,518,308,200,0,0,0,0,0,0,0],"169":[0,0,480,1008,1848,3804,4076,4076,3804,1848,1008,480,0,0,0,0],"174":[0,0,480,1008,1800,3820,3820,3852,3820,1512,1008,480,0,0,0,0],"name":"Extrudecopy","copy":"OriginalbyNic","letterspace":"64","basefont_size":"512","basefont_left":"62","basefont_top":"0","basefont":"Arial","basefont2":""}'

function string:replace(i, s) --function for replacing chars in string
 return self:sub(1,i-1)..s..self:sub(i+1)
end
function bin(number, bits_count) -- decimal number, length of binary number
 local result=""
 if not bits_count then bits_count = 16 end --set length of bin number
 --Converting dec to bin
 while number > 0 do
result = result..tostring(number%2)
number = math.floor(number/2)
 end
 --Append nulls
 if #result < bits_count then
for i=1, bits_count - #result do
result = result.."0"
end
 end
 return result
end 
function drawLetter (data, x, y, clr) -- array of numbers representing letter, position X, position Y, color
 local letter_width = 0
 -- correcting position increased by loop index
 x = x - 1 
 y = y - 1
 local merged_bits = "" -- help var for counting letter width
 -- looping over letter data
 for iy, row in ipairs(data) do
local bits = bin(row)
for ix=1, #bits do
local letter = bits:sub(ix,ix)
if ix > #merged_bits then merged_bits = merged_bits.."0" end -- extend var length
if letter == "1" then -- if pixel is true
pix(x+ix,y+iy, clr) -- draw
merged_bits = merged_bits:replace(ix,"1")
end
end
 end
 -- counting letter width from merged bits
 for i=1, #merged_bits do
local s = merged_bits:sub(i,i)
if s == "1" then letter_width = letter_width +1 end
 end
 return letter_width
end
function parseFont (fnt, name, space, spacing) -- variable containing font data, new name of font variable, size of space between words, size of space between letters
 -- Change font data to Lua Table
 local result = fnt:gsub("%[", "{")
 result = result:gsub("%]", "}")
 result = result:gsub(":", "=")
 result = result:gsub('"(%d+)"=', '[%1]=')
 result = result:gsub('"([%a%d_]+)"=', '%1=')
 result = name.."="..result
 load(result)() -- load table
 load(name..".spacing="..tostring(spacing))() -- assign spacing to font
 load(name..".space="..tostring(space))() -- assign space to font
end
function print2 (txt, x, y, clr, fnt) -- text, x, y, color, font
 local a, b = 65535, 32769 --bits for create symbol of unknown symbol
 local unknown_symbol = {a, b, b, b, b, b, b, b, b, b, b, b, b, b, b, a}
 local s = ""
 local string_code = 0
 local actual_pos = 0
 --Print loop
 for i=1, #txt do
s = txt:sub(i,i)
string_code = string.byte(s)
if fnt[string_code] then -- If letter is in font
actual_pos = actual_pos + drawLetter(fnt[string_code], x+actual_pos, y, clr) -- add to actual pos letter size and draw letter
elseif string_code == 32 then -- If letter is space
actual_pos = actual_pos + fnt.space -- add to actual position space size
else 
actual_pos = actual_pos + drawLetter(unknown_symbol, x+actual_pos, y, clr) -- draw letter fow unknown letter
end
actual_pos = actual_pos + fnt.spacing -- add to actual position space between letters
 end
end
parseFont(font,"SuperFont",5,1)
function TIC()
 cls(0)
 print2("TIC - tiny computer",2,10,7,SuperFont)
end


More information

Published 236 days ago
StatusPrototype
CategoryOther
PlatformsHTML5
Rating
(2)
AuthorCrutiatix
Tagsfonts, snippet, tic

Leave a comment

Log in with your itch.io account to leave a comment.