figlet.lua
local Figlet = {}
local deutsch = {196, 214, 220, 228, 246, 252, 223}
local fcharlist = {}
local magic, hardblank, charheight, maxlen, smush, cmtlines, ffright2left, smush2
local function readfontchar(fontfile, theord)
  local t = {}
  fcharlist[theord] = t
  
  
  for i = 1, charheight do
    local line = assert(fontfile:read("*l"), "Not enough character lines for character " .. theord)
    local line = string.gsub(line, "%s+$", "")     assert(line ~= "", "Unexpected empty line")
        local endchar = line:sub(-1) 
        while line:sub(-1) == endchar do
      line = line:sub(1, #line - 1)
    end 
    table.insert(t, line)
  end 
end 
function Figlet.readfont(filename)
  local fontfile = assert(io.open(filename, "r"))
  local s
  fcharlist = {}
    s = assert(fontfile:read("*l"), "Empty FIGlet file")
    
    magic, hardblank, charheight, maxlen, smush, cmtlines, ffright2left, smush2 = string.match(s,
                                                                                             "^(flf2).(.) (%d+) %d+ (%d+) (%-?%d+) (%d+) ?(%d*) ?(%d*) ?(%-?%d*)")
  assert(magic, "Not a FIGlet 2 font file")
    charheight = tonumber(charheight)
  maxlen = tonumber(maxlen)
  smush = tonumber(smush)
  cmtlines = tonumber(cmtlines)
    if charheight < 1 then
    charheight = 1
  end 
    for i = 1, cmtlines do
    assert(fontfile:read("*l"), "Not enough comment lines")
  end 
    for theord = string.byte(' '), string.byte('~') do
    readfontchar(fontfile, theord)
  end 
    for theord = 1, 7 do
    readfontchar(fontfile, deutsch[theord])
  end 
      
  repeat
    local extra = fontfile:read("*l")
    if not extra then
      break
    end 
    local negative, theord = string.match(extra, "^(%-?)0[xX](%x+)")
    if theord then
      theord = tonumber(theord, 16)
      if negative == "-" then
        theord = -theord
      end     else
      theord = string.match(extra, "^%d+")
      assert(theord, "Unexpected line:" .. extra)
      theord = tonumber(theord)
    end 
    readfontchar(fontfile, theord)
  until false
  fontfile:close()
  
  for k, v in pairs(fcharlist) do
        local leading_space = true
    local trailing_space = true
    for _, line in ipairs(v) do
      if line:sub(1, 1) ~= " " then
        leading_space = false
      end       if line:sub(-1, -1) ~= " " then
        trailing_space = false
      end     end 
        for i, line in ipairs(v) do
      if leading_space then
        v[i] = line:sub(2)
      end       if trailing_space then
        v[i] = line:sub(1, -2)
      end     end   end end 
local function addchar(which, output, kern, smush)
  local c = fcharlist[string.byte(which)]
  if not c then
    return
  end 
  for i = 1, charheight do
    if smush and output[i] ~= "" and which ~= " " then
      local lhc = output[i]:sub(-1)
      local rhc = c[i]:sub(1, 1)
      output[i] = output[i]:sub(1, -2)       if rhc ~= " " then
        output[i] = output[i] .. rhc
      else
        output[i] = output[i] .. lhc
      end
      output[i] = output[i] .. c[i]:sub(2)
    else
      output[i] = output[i] .. c[i]
    end 
    if not (kern or smush) or which == " " then
      output[i] = output[i] .. " "
    end   end 
end 
function Figlet.ascii_art(s, kern, smush)
  assert(fcharlist)
  assert(charheight > 0)
    local output = {}
  for i = 1, charheight do
    output[i] = ""
  end 
  for i = 1, #s do
    local c = s:sub(i, i)
    if c >= " " and c < "\127" then
      addchar(c, output, kern, smush)
    end 
  end 
    local fixedblank = string.gsub(hardblank, "[%%%]%^%-$().[*+?]", "%%%1")
  for i, line in ipairs(output) do
    output[i] = string.gsub(line, fixedblank, " ")
  end 
  return output
end 
function Figlet.getString(str, kern, smush)
  local tbl = Figlet.ascii_art(str, kern, smush)
  return table.concat(tbl, "\n")
end
function Figlet.getKern(str)
  return Figlet.getString(str, true)
end
function Figlet.getSmush(str)
  return Figlet.getString(str, true, true)
end
return Figlet