FeynmanTech

HersheySimplex_CODEA

Apr 5th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.96 KB | None | 0 0
  1. HersheyRomanSimplex = class()
  2.  
  3. -- The Hershey Fonts were originally created by Dr.
  4. -- A. V. Hershey while working at the
  5. -- U. S. National Bureau of Standards.
  6.  
  7. -- Useful Links:
  8. -- http://emergent.unpythonic.net/software/hershey
  9. -- http://paulbourke.net/dataformats/hershey/
  10.  
  11. -- Re-encoding of font information and other shenanigans
  12. -- by Tom Bortels [email protected] November 2011
  13. -- all rights reversed (Hail Eris!)
  14.  
  15. -- "If I have seen a little further it is by standing
  16. -- on the shoulders of Giants."
  17. -- Isaac Newton
  18.  
  19. function HersheyRomanSimplex:init()
  20.    -- font data - 2 decimal character # of points,
  21.    -- followed by 2*points of point data
  22.    -- 9->-9, 8-<-8, ... 1->-1, 0->0, A->1, B->2, ... Z->26
  23.    self.code = "9876543210ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  24.    -- this is the Hershey Roman Simplex font for ascii 32-127
  25.    self.fontdata =
  26.       "00160810EUEG11EBDAE0FAEB0516DUDN11LULN1121KYD711QYJ711DLRL11"
  27.    .. "CFQF2620HYH411LYL411QROTLUHUETCRCPDNEMGLMJOIPHQFQCOAL0H0EACC"
  28.    .. "3124UUC011HUJSJQIOGNENCPCRDTFUHUJTMSPSSTUU11QGOFNDNBP0R0TAUC"
  29.    .. "UESGQG3426WLWMVNUNTMSKQFOCMAK0G0EADBCDCFDHEILMMNNPNRMTKUITHR"
  30.    .. "HPIMKJPCRAT0V0WAWB0710ESDTEUFTFREPDO1014KYIWGTEPDKDGEBG2I5K7"
  31.    .. "1014CYEWGTIPJKJGIBG2E5C70816HUHI11CRML11MRCL0526MRM011DIVI08"
  32.    .. "10FAE0DAEBFAF1E3D40226DIVI0510EBDAE0FAEB0222TYB71720IUFTDQCL"
  33.    .. "CIDDFAI0K0NAPDQIQLPQNTKUIU0420FQHRKUK01420DPDQESFTHULUNTOSPQ"
  34.    .. "POOMMJC0Q01520EUPUJMMMOLPKQHQFPCNAK0H0EADBCD0620MUCGRG11MUM0"
  35.    .. "1720OUEUDLEMHNKNNMPKQHQFPCNAK0H0EADBCD2320PROTLUJUGTEQDLDGEC"
  36.    .. "GAJ0K0NAPCQFQGPJNLKMJMGLEJDG0520QUG011CUQU2920HUETDRDPENGMKL"
  37.    .. "NKPIQGQDPBOAL0H0EADBCDCGDIFKILMMONPPPROTLUHU2320PNOKMIJHIHFI"
  38.    .. "DKCNCODRFTIUJUMTORPNPIODMAJ0H0EADC1110ENDMELFMEN11EBDAE0FAEB"
  39.    .. "1410ENDMELFMEN11FAE0DAEBFAF1E3D40324TRDIT00526DLVL11DFVF0324"
  40.    .. "DRTID02018CPCQDSETGUKUMTNSOQOONMMLIJIG11IBHAI0JAIB5527RMQOOP"
  41.    .. "LPJOINHKHHIFKENEPFQH11LPJNIKIHJFKE11RPQHQFSEUEWGXJXLWOVQTSRT"
  42.    .. "OULUITGSEQDOCLCIDFEDGBIAL0O0RATBUC11SPRHRFSE0818IUA011IUQ011"
  43.    .. "DGNG2321DUD011DUMUPTQSRQROQMPLMK11DKMKPJQIRGRDQBPAM0D01821RP"
  44.    .. "QROTMUIUGTERDPCMCHDEECGAI0M0OAQCRE1521DUD011DUKUNTPRQPRMRHQE"
  45.    .. "PCNAK0D01119DUD011DUQU11DKLK11D0Q00818DUD011DUQU11DKLK2221RP"
  46.    .. "QROTMUIUGTERDPCMCHDEECGAI0M0OAQCRERH11MHRH0822DUD011RUR011DK"
  47.    .. "RK0208DUD01016LULEKBJAH0F0DACBBEBG0821DUD011RUDG11ILR00517DU"
  48.    .. "D011D0P01124DUD011DUL011TUL011TUT00822DUD011DUR011RUR02122IU"
  49.    .. "GTERDPCMCHDEECGAI0M0OAQCRESHSMRPQROTMUIU1321DUD011DUMUPTQSRQ"
  50.    .. "RNQLPKMJDJ2422IUGTERDPCMCHDEECGAI0M0OAQCRESHSMRPQROTMUIU11LD"
  51.    .. "R21621DUD011DUMUPTQSRQROQMPLMKDK11KKR02020QROTLUHUETCRCPDNEM"
  52.    .. "GLMJOIPHQFQCOAL0H0EACC0516HUH011AUOU1022DUDFECGAJ0L0OAQCRFRU"
  53.    .. "0518AUI011QUI01124BUG011LUG011LUQ011VUQ00520CUQ011QUC00618AU"
  54.    .. "IKI011QUIK0820QUC011CUQU11C0Q01114DYD711EYE711DYKY11D7K70214"
  55.    .. "0UN31114IYI711JYJ711CYJY11C7J71016FOHRJO11CLHQML11HQH0021602"
  56.    .. "P20710FUETDRDPEOFPEQ1719ONO011OKMMKNHNFMDKCHCFDCFAH0K0MAOC17"
  57.    .. "19DUD011DKFMHNKNMMOKPHPFOCMAK0H0FADC1418OKMMKNHNFMDKCHCFDCFA"
  58.    .. "H0K0MAOC1719OUO011OKMMKNHNFMDKCHCFDCFAH0K0MAOC1718CHOHOJNLMM"
  59.    .. "KNHNFMDKCHCFDCFAH0K0MAOC0812JUHUFTEQE011BNIN2219ONO2N5M6K7H7"
  60.    .. "F611OKMMKNHNFMDKCHCFDCFAH0K0MAOC1019DUD011DJGMINLNNMOJO00808"
  61.    .. "CUDTEUDVCU11DND01110EUFTGUFVEU11FNF3E6C7A70817DUD011NNDD11HH"
  62.    .. "O00208DUD01830DND011DJGMINLNNMOJO011OJRMTNWNYMZJZ01019DND011"
  63.    .. "DJGMINLNNMOJO01719HNFMDKCHCFDCFAH0K0MAOCPFPHOKMMKNHN1719DND7"
  64.    .. "11DKFMHNKNMMOKPHPFOCMAK0H0FADC1719ONO711OKMMKNHNFMDKCHCFDCFA"
  65.    .. "H0K0MAOC0813DND011DHEKGMINLN1717NKMMJNGNDMCKDIFHKGMFNDNCMAJ0"
  66.    .. "G0DACC0812EUEDFAH0J011BNIN1019DNDDEAG0J0LAOD11ONO00516BNH011"
  67.    .. "NNH01122CNG011KNG011KNO011SNO00517CNN011NNC00916BNH011NNH0F4"
  68.    .. "D6B7A70817NNC011CNNN11C0N03914IYGXFWEUESFQGPHNHLFJ11GXFVFTGR"
  69.    .. "HQIOIMHKDIHGIEICHAG0F2F4G611FHHFHDGBFAE1E3F5G6I70208DYD73914"
  70.    .. "EYGXHWIUISHQGPFNFLHJ11GXHVHTGRFQEOEMFKJIFGEEECFAG0H2H4G611HH"
  71.    .. "FFFDGBHAI1I3H5G6E72324CFCHDKFLHLJKNHPGRGTHUJ11CHDJFKHKJJNGPF"
  72.    .. "RFTGUJUL"
  73.  
  74.    local i=1
  75.    local c=32
  76.    self.font = {}
  77.    while (i < string.len(self.fontdata)) do
  78.       local cs = string.char(c)
  79.       self.font[cs] = {}
  80.       local points = string.sub(self.fontdata, i, i+1)
  81.       self.font[cs].points = points
  82.       self.font[cs].char = cs
  83.       self.font[cs].ascii = c
  84.       self.font[cs].width = string.sub(self.fontdata, i+2, i+3)
  85.        i=i+4
  86.       self.font[cs].data = string.sub(self.fontdata, i, i+points*2)
  87.       i = i + points*2
  88.       c = c + 1
  89.    end
  90.    i=-9
  91.    self.decode = {}
  92.    for c in self.code:gmatch"." do
  93.       self.decode[c]=i
  94.       i=i+1
  95.    end
  96.  
  97.    -- label support
  98.    self.labels = {}
  99.    
  100. end
  101.  
  102. function HersheyRomanSimplex:addlabel(txt, x, y, size, rot, color)
  103.    if type(txt) == "table" then
  104.       local proto = { txt="UNDEFINED", x=0, y=0, size=1, rot=0, color={ 255, 255, 255, 255 } }
  105.       local k, v
  106.       for k,v in pairs(txt) do proto[k] = v end
  107.       table.insert(self.labels, proto)
  108.    else
  109.       txt = txt or "demo"
  110.       x = x or WIDTH/2
  111.       y = y or HEIGHT/2
  112.       size = size or 1
  113.       rot = rot or 0
  114.       color = color or { 255, 255, 255, 255 }
  115.       table.insert(self.labels, { txt=txt, x=x, y=y, size=size, rot=rot, color=color } )
  116.    end
  117.    return self.labels[# self.labels]
  118. end
  119.  
  120. function HersheyRomanSimplex:drawlabel(l)
  121.    if (l.skip) then return end
  122.    pushMatrix()
  123.    translate(l.x, l.y)
  124.    rotate(l.rot)
  125.    stroke(unpack(l.color))
  126.    self:drawstring(l.txt, 0, 0, l.size)
  127.    popMatrix()
  128. end
  129.  
  130. function HersheyRomanSimplex:draw()
  131.    local k, v
  132.    for k, v in pairs(self.labels) do
  133.       self:drawlabel(v)
  134.    end
  135. end
  136.    
  137. function HersheyRomanSimplex:stringwidth(s, sc)
  138.     sc = sc or 1
  139.     local x, l, i = 0, string.len(s)
  140.     for i=1, l do
  141.         x = x + (self.font[s:sub(i, i)].width * sc)
  142.     end
  143.     return x
  144. end
  145.  
  146. function HersheyRomanSimplex:drawstring(s, x, y, sc)
  147.    sc = sc or 1
  148.     local ox=x
  149.    local l, i
  150.    l = string.len(s)
  151.    for i = 1, l do
  152.       local c = s:sub(i, i)
  153.       local w = self.font[c].width
  154. --      if ((x + w) >= 0) then
  155. --         x = x + (self:drawchar(c, x, y, sc))
  156. --      else
  157. --         x = x + w * sc
  158. --      end
  159. --   if (x > WIDTH) then break end -- skip offscreen
  160.       if ((x+w) <0) or (x > WIDTH) then
  161.          x = x + w * sc
  162.       else
  163.          x = x + (self:drawchar(c, x, y, sc))
  164.       end
  165.    end
  166.    return x - ox
  167. end
  168.  
  169. function HersheyRomanSimplex:drawchar(c, x, y, sc)
  170.    sc = sc or 1
  171.    local ax, ay, bx, by, minx, maxx = -1, -1, -1, -1, -1, -1
  172.    local p, plot
  173.    local ch = self.font[c]
  174.    for p=1, ch.points do
  175.       ax=bx
  176.       ay=by
  177.       bx=self.decode[ch.data:sub(p*2-1, p*2-1)]
  178.       by=self.decode[ch.data:sub(p*2, p*2)]
  179.       plot=true
  180.       if ((ax==-1) and (ay==-1)) then plot=false end
  181.       if ((bx==-1) and (by==-1)) then plot=false end
  182.       if (plot) then  
  183.          line(x+ax*sc, y+ay*sc, x+bx*sc, y+by*sc)
  184.       end
  185.    end
  186.    return ch.width * sc -- for drawstring
  187. end
Advertisement
Add Comment
Please, Sign In to add comment