Created
November 1, 2011 17:26
-
-
Save bortels/1331260 to your computer and use it in GitHub Desktop.
Hershey Roman Simplex font using packed data for Codea
This file contains hidden or 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
-- Just a "Main" tab for Codea to demo the class | |
function setup() | |
f=Font() | |
f:init() | |
frame=1 | |
offset = 0 | |
stroke_width=2 | |
iparameter("stroke_width", 1, 5, stroke_width) | |
speed=2 | |
parameter("speed", 0, 8, speed) | |
x=50 | |
text = "Why do you necessarily have to be wrong just because " | |
.. "a few million people think you are? " | |
bannerscale= 2 | |
parameter("bannerscale", 0, 10, bannerscale) | |
bannerlength = f:stringwidth(text, bannerscale) | |
spintxt = "Bananas!" | |
spinscale = 1.5 | |
spinwidth = f:stringwidth(spintxt, spinscale) | |
rf = 0.03 -- rainbow frequency | |
end | |
function draw() | |
background(0, 0, 0, 255) | |
frame = frame + 1 -- used by lots of things | |
stroke(226, 235, 234, 255) | |
noSmooth() | |
strokeWidth(stroke_width) | |
lineCapMode(ROUND) | |
f:drawstring("Hershey Roman Simplex " .. frame, 150, 200) | |
-- smooth scroll | |
stroke(140, 147, 215, 255) | |
local bl = f:drawstring(text .. text, | |
x - offset, HEIGHT/2, | |
bannerscale) | |
offset = offset + speed | |
if (offset > bl) then offset = 0 end | |
-- changing size | |
fps = string.format("%.2f", frame / ElapsedTime) | |
f:drawstring(fps .. " fps", 500, 600, math.sin(frame / 94)) | |
-- rainbow! | |
stroke(math.sin(rf*frame)*127 + 128, | |
math.sin(rf*frame + 2)*127 + 128, | |
math.sin(rf*frame + 4)*127 + 128, 255) | |
f:drawstring("Daddy loves Gracie and Maddy", 200, 20) | |
-- spinner (using external positioning) | |
translate(200, 550) | |
rotate(frame*2.5) | |
translate(0-spinwidth/2, -10) | |
stroke(255, 252, 0, 255) | |
f:drawstring(spintxt, 0, 0, spinscale) | |
end |
This file contains hidden or 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
Font = class() | |
Font = class() | |
function Font:init() | |
-- font data - 2 decimal character # of points, | |
-- followed by 2*points of point data | |
-- 9->-9, 8-<-8, ... 1->-1, 0->0, A->1, B->2, ... Z->26 | |
self.code = "9876543210ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
-- this is the Hershey Roman Simplex font for ascii 32-127 | |
self.fontdata = | |
"00160810EUEG11EBDAE0FAEB0516DUDN11LULN1121KYD711QYJ711DLRL11" | |
.. "CFQF2620HYH411LYL411QROTLUHUETCRCPDNEMGLMJOIPHQFQCOAL0H0EACC" | |
.. "3124UUC011HUJSJQIOGNENCPCRDTFUHUJTMSPSSTUU11QGOFNDNBP0R0TAUC" | |
.. "UESGQG3426WLWMVNUNTMSKQFOCMAK0G0EADBCDCFDHEILMMNNPNRMTKUITHR" | |
.. "HPIMKJPCRAT0V0WAWB0710ESDTEUFTFREPDO1014KYIWGTEPDKDGEBG2I5K7" | |
.. "1014CYEWGTIPJKJGIBG2E5C70816HUHI11CRML11MRCL0526MRM011DIVI08" | |
.. "10FAE0DAEBFAF1E3D40226DIVI0510EBDAE0FAEB0222TYB71720IUFTDQCL" | |
.. "CIDDFAI0K0NAPDQIQLPQNTKUIU0420FQHRKUK01420DPDQESFTHULUNTOSPQ" | |
.. "POOMMJC0Q01520EUPUJMMMOLPKQHQFPCNAK0H0EADBCD0620MUCGRG11MUM0" | |
.. "1720OUEUDLEMHNKNNMPKQHQFPCNAK0H0EADBCD2320PROTLUJUGTEQDLDGEC" | |
.. "GAJ0K0NAPCQFQGPJNLKMJMGLEJDG0520QUG011CUQU2920HUETDRDPENGMKL" | |
.. "NKPIQGQDPBOAL0H0EADBCDCGDIFKILMMONPPPROTLUHU2320PNOKMIJHIHFI" | |
.. "DKCNCODRFTIUJUMTORPNPIODMAJ0H0EADC1110ENDMELFMEN11EBDAE0FAEB" | |
.. "1410ENDMELFMEN11FAE0DAEBFAF1E3D40324TRDIT00526DLVL11DFVF0324" | |
.. "DRTID02018CPCQDSETGUKUMTNSOQOONMMLIJIG11IBHAI0JAIB5527RMQOOP" | |
.. "LPJOINHKHHIFKENEPFQH11LPJNIKIHJFKE11RPQHQFSEUEWGXJXLWOVQTSRT" | |
.. "OULUITGSEQDOCLCIDFEDGBIAL0O0RATBUC11SPRHRFSE0818IUA011IUQ011" | |
.. "DGNG2321DUD011DUMUPTQSRQROQMPLMK11DKMKPJQIRGRDQBPAM0D01821RP" | |
.. "QROTMUIUGTERDPCMCHDEECGAI0M0OAQCRE1521DUD011DUKUNTPRQPRMRHQE" | |
.. "PCNAK0D01119DUD011DUQU11DKLK11D0Q00818DUD011DUQU11DKLK2221RP" | |
.. "QROTMUIUGTERDPCMCHDEECGAI0M0OAQCRERH11MHRH0822DUD011RUR011DK" | |
.. "RK0208DUD01016LULEKBJAH0F0DACBBEBG0821DUD011RUDG11ILR00517DU" | |
.. "D011D0P01124DUD011DUL011TUL011TUT00822DUD011DUR011RUR02122IU" | |
.. "GTERDPCMCHDEECGAI0M0OAQCRESHSMRPQROTMUIU1321DUD011DUMUPTQSRQ" | |
.. "RNQLPKMJDJ2422IUGTERDPCMCHDEECGAI0M0OAQCRESHSMRPQROTMUIU11LD" | |
.. "R21621DUD011DUMUPTQSRQROQMPLMKDK11KKR02020QROTLUHUETCRCPDNEM" | |
.. "GLMJOIPHQFQCOAL0H0EACC0516HUH011AUOU1022DUDFECGAJ0L0OAQCRFRU" | |
.. "0518AUI011QUI01124BUG011LUG011LUQ011VUQ00520CUQ011QUC00618AU" | |
.. "IKI011QUIK0820QUC011CUQU11C0Q01114DYD711EYE711DYKY11D7K70214" | |
.. "0UN31114IYI711JYJ711CYJY11C7J71016FOHRJO11CLHQML11HQH0021602" | |
.. "P20710FUETDRDPEOFPEQ1719ONO011OKMMKNHNFMDKCHCFDCFAH0K0MAOC17" | |
.. "19DUD011DKFMHNKNMMOKPHPFOCMAK0H0FADC1418OKMMKNHNFMDKCHCFDCFA" | |
.. "H0K0MAOC1719OUO011OKMMKNHNFMDKCHCFDCFAH0K0MAOC1718CHOHOJNLMM" | |
.. "KNHNFMDKCHCFDCFAH0K0MAOC0812JUHUFTEQE011BNIN2219ONO2N5M6K7H7" | |
.. "F611OKMMKNHNFMDKCHCFDCFAH0K0MAOC1019DUD011DJGMINLNNMOJO00808" | |
.. "CUDTEUDVCU11DND01110EUFTGUFVEU11FNF3E6C7A70817DUD011NNDD11HH" | |
.. "O00208DUD01830DND011DJGMINLNNMOJO011OJRMTNWNYMZJZ01019DND011" | |
.. "DJGMINLNNMOJO01719HNFMDKCHCFDCFAH0K0MAOCPFPHOKMMKNHN1719DND7" | |
.. "11DKFMHNKNMMOKPHPFOCMAK0H0FADC1719ONO711OKMMKNHNFMDKCHCFDCFA" | |
.. "H0K0MAOC0813DND011DHEKGMINLN1717NKMMJNGNDMCKDIFHKGMFNDNCMAJ0" | |
.. "G0DACC0812EUEDFAH0J011BNIN1019DNDDEAG0J0LAOD11ONO00516BNH011" | |
.. "NNH01122CNG011KNG011KNO011SNO00517CNN011NNC00916BNH011NNH0F4" | |
.. "D6B7A70817NNC011CNNN11C0N03914IYGXFWEUESFQGPHNHLFJ11GXFVFTGR" | |
.. "HQIOIMHKDIHGIEICHAG0F2F4G611FHHFHDGBFAE1E3F5G6I70208DYD73914" | |
.. "EYGXHWIUISHQGPFNFLHJ11GXHVHTGRFQEOEMFKJIFGEEECFAG0H2H4G611HH" | |
.. "FFFDGBHAI1I3H5G6E72324CFCHDKFLHLJKNHPGRGTHUJ11CHDJFKHKJJNGPF" | |
.. "RFTGUJUL" | |
local i=1 | |
local c=32 | |
self.font = {} | |
while (i < string.len(self.fontdata)) do | |
local cs = string.char(c) | |
self.font[cs] = {} | |
local points = string.sub(self.fontdata, i, i+1) | |
self.font[cs].points = points | |
self.font[cs].char = cs | |
self.font[cs].ascii = c | |
self.font[cs].width = string.sub(self.fontdata, i+2, i+3) | |
i=i+4 | |
self.font[cs].data = string.sub(self.fontdata, i, i+points*2) | |
i = i + points*2 | |
c = c + 1 | |
end | |
i=-9 | |
self.decode = {} | |
for c in self.code:gmatch"." do | |
self.decode[c]=i | |
i=i+1 | |
end | |
end | |
function Font:stringwidth(s, sc) | |
sc = sc or 1 | |
local x, l, i = 0, string.len(s) | |
for i=1, l do | |
x = x + (self.font[s:sub(i, i)].width * sc) | |
end | |
return x | |
end | |
function Font:drawstring(s, x, y, sc) | |
sc = sc or 1 | |
local ox=x | |
local l, i | |
l = string.len(s) | |
for i = 1, l do | |
local c = s:sub(i, i) | |
local w = self.font[c].width * sc | |
if ((x + w) >= 0) then | |
x = x + (self:drawchar(c, x, y, sc)) | |
else | |
x = x + w | |
end | |
if (x > WIDTH) then break end -- skip offscreen | |
end | |
return x - ox | |
end | |
function Font:drawchar(c, x, y, sc) | |
sc = sc or 1 | |
local ax, ay, bx, by, minx, maxx = -1, -1, -1, -1, -1, -1 | |
local p, plot | |
local ch = self.font[c] | |
for p=1, ch.points do | |
ax=bx | |
ay=by | |
bx=self.decode[ch.data:sub(p*2-1, p*2-1)] | |
by=self.decode[ch.data:sub(p*2, p*2)] | |
plot=true | |
if ((ax==-1) and (ay==-1)) then plot=false end | |
if ((bx==-1) and (by==-1)) then plot=false end | |
if (plot) then | |
line(x+ax*sc, y+ay*sc, x+bx*sc, y+by*sc) | |
end | |
end | |
return ch.width * sc -- for drawstring | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment