Guest User

Untitled

a guest
Nov 19th, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.95 KB | None | 0 0
  1. -- Figlet font API
  2. -- Created by MaoZedong
  3.  
  4. local function split(str)
  5.     local parts = {}
  6.  
  7.     for part in str:gmatch("[^%s]+") do
  8.         table.insert(parts, part)
  9.     end
  10.  
  11.     return parts
  12. end
  13.  
  14. local function skip(tbl, n)
  15.     return table.pack(select(n + 1, table.unpack(tbl)))
  16. end
  17.  
  18. local function esc(x)
  19.     return (x:gsub('%%', '%%%%')
  20.     :gsub('^%^', '%%^')
  21.     :gsub('%$$', '%%$')
  22.     :gsub('%(', '%%(')
  23.     :gsub('%)', '%%)')
  24.     :gsub('%.', '%%.')
  25.     :gsub('%[', '%%[')
  26.     :gsub('%]', '%%]')
  27.     :gsub('%*', '%%*')
  28.     :gsub('%+', '%%+')
  29.     :gsub('%-', '%%-')
  30.     :gsub('%?', '%%?'))
  31. end
  32.  
  33. function parseFont(name)
  34.     local f = fs.open(name, "r")
  35.     local lines = {}
  36.  
  37.     for line in f.readLine do
  38.         table.insert(lines, line)
  39.     end
  40.  
  41.     f.close()
  42.  
  43.     local header = split(lines[1])
  44.     local hardblank = header[1]:sub(#header[1], #header[1])
  45.     local height = math.abs(tonumber(header[2]))
  46.     local comments = math.abs(tonumber(header[6]))
  47.  
  48.     local fontLines = skip(lines, comments + 1)
  49.  
  50.     return {
  51.         fontLines = fontLines,
  52.         hardblank = hardblank,
  53.         height = height,
  54.         chars = {}
  55.     }
  56. end
  57.  
  58. local function parseChar(char, font)
  59.     if font.chars[char] ~= nil then
  60.         return font.chars[char]
  61.     end
  62.  
  63.     local height = font.height
  64.     local start = (string.byte(char) - 32) * height
  65.     local charLines = {}
  66.  
  67.     for i = 1, height do
  68.         charLines[i] = font.fontLines[start + i]
  69.         :gsub("@", "")
  70.         :gsub(esc(font.hardblank), " ")
  71.     end
  72.  
  73.     font.chars[char] = charLines
  74.  
  75.     return charLines
  76. end
  77.  
  78. function write(font, str)
  79.     local chars = {}
  80.     local result = ""
  81.  
  82.     for i = 1, #str do
  83.         chars[i] = parseChar(str:sub(i, i), font)
  84.     end
  85.  
  86.     for i = 1, #chars[1] do
  87.         for j = 1, #str do
  88.             result = result .. chars[j][i]
  89.         end
  90.  
  91.         result = result .. "\n"
  92.     end
  93.  
  94.     return result
  95. end
Advertisement
Add Comment
Please, Sign In to add comment