FeynmanTech

TPTLua Texter: GD Font Generator

Oct 1st, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.56 KB | None | 0 0
  1. require'gd'
  2.  
  3. local charchart = {}
  4.  
  5. font = {}
  6.  
  7. local f, charstr
  8.  
  9. function font.new(w, h, name)
  10.     ftf_w = w
  11.     ftf_h = h
  12.     ftf_name = name
  13.     charStr = ({string.gsub([[
  14. texter_font[char] = {
  15.     ['descender'] = 0,
  16.     ['kerning'] = 0,
  17.     ['pixels']  = {
  18. ]], 'texter_font', name)})[1]
  19.     f = io.open(name .. '.txt', 'w')
  20.     f:write(tostring(({string.gsub(string.gsub(string.gsub([[
  21. _NAME = {}
  22. _NAME['width'] = _W
  23. _NAME['height'] = _H
  24.     ]], '_NAME', name), '_W', w), '_H', h)})[1]))
  25. end
  26.  
  27. function font.letter(file, char)
  28.     currentfont_pixels = {}
  29.     for x = 1, ftf_w do
  30.         for y = 1, ftf_h + 2 do
  31.             currentfont_pixels[y] = {}
  32.             currentfont_pixels[y][x] = 0
  33.         end
  34.     end
  35.     itype = string.sub(file, -4, -1)
  36.     itype = string.gsub(itype, '%.', '')
  37.     if itype == 'jpeg' or itype == 'jpg' then
  38.         im = gd.createFromJpeg(file)
  39.     elseif itype == 'png' then
  40.         im = gd.createFromPng(file)
  41.     elseif itype == 'gif' then
  42.         im = gd.createFromGif(file)
  43.     end
  44.     local w, h = im:sizeXY()
  45.     for x = 0, w do
  46.         for y = 0, h do
  47.             color = tostring(bit.tohex(im:getPixel(x, y)))
  48.             r = string.sub(color, 3, 4)
  49.             g = string.sub(color, 5, 6)
  50.             b = string.sub(color, 7, 8)
  51.             if r ~= '00' and g ~= '00' and b ~= '00' then
  52.                 currentfont_pixels[y + 1][x + 1] = 1
  53.             else
  54.                 --currentfont_pixels[y + 1][x + 1] = 0
  55.             end
  56.         end
  57.     end
  58.     f:write(({string.gsub(charStr, 'char', string.format('%q', char))})[1])
  59.     for y = 1, ftf_h do
  60.         for x = 1, ftf_w do
  61.             if x == 1 then
  62.                 f:write('\t\t{', currentfont_pixels[y][x] and tostring(currentfont_pixels[y][x]) or '0', ', ')
  63.             elseif x == ftf_w then
  64.                 f:write(currentfont_pixels[y][x] and tostring(currentfont_pixels[y][x]) or '0', '}')
  65.                 if y == ftf_h then
  66.                     f:write('\n')
  67.                 else
  68.                     f:write(',\n')
  69.                 end
  70.             else
  71.                 f:write(currentfont_pixels[y][x] and tostring(currentfont_pixels[y][x]) or '0', ', ')
  72.             end
  73.         end
  74.     end
  75.     f:write('\t}\n}')
  76. end
  77.  
  78. function font.auto()
  79.     local charstr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-"\',.?/'
  80.     for k = 1, #charstr do
  81.         local image = gd.createTrueColor(ftf_w, ftf_h)
  82.         image:png(string.sub(charstr, k, k) .. '.png')
  83.         os.execute(string.sub(charstr, k, k) .. '.png')
  84.         letter(string.sub(charstr, k, k) .. '.png', string.sub(charstr, k, k))
  85.     end
  86. end
  87.  
  88. function font.done()
  89.     f:write(ftf_name .. [[['NULL'] = {
  90.     ['descender'] = 0,
  91.     ['kerning'] = 0,
  92.     ['pixels']  = {
  93.         {1, 1, 1, 1, 1},
  94.         {1, 0, 0, 1, 1},
  95.         {1, 1, 1, 0, 1},
  96.         {1, 1, 0, 1, 1},
  97.         {1, 1, 1, 1, 1},
  98.         {1, 1, 0, 1, 1},
  99.         {1, 1, 1, 1, 1},
  100.         {0, 0, 0, 0, 0},
  101.         {0, 0, 0, 0, 0}
  102.     }
  103. }]])
  104.     f:close()
  105. end
Advertisement
Add Comment
Please, Sign In to add comment