Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require'gd'
- local charchart = {}
- font = {}
- local f, charstr
- function font.new(w, h, name)
- ftf_w = w
- ftf_h = h
- ftf_name = name
- charStr = ({string.gsub([[
- texter_font[char] = {
- ['descender'] = 0,
- ['kerning'] = 0,
- ['pixels'] = {
- ]], 'texter_font', name)})[1]
- f = io.open(name .. '.txt', 'w')
- f:write(tostring(({string.gsub(string.gsub(string.gsub([[
- _NAME = {}
- _NAME['width'] = _W
- _NAME['height'] = _H
- ]], '_NAME', name), '_W', w), '_H', h)})[1]))
- end
- function font.letter(file, char)
- currentfont_pixels = {}
- for x = 1, ftf_w do
- for y = 1, ftf_h + 2 do
- currentfont_pixels[y] = {}
- currentfont_pixels[y][x] = 0
- end
- end
- itype = string.sub(file, -4, -1)
- itype = string.gsub(itype, '%.', '')
- if itype == 'jpeg' or itype == 'jpg' then
- im = gd.createFromJpeg(file)
- elseif itype == 'png' then
- im = gd.createFromPng(file)
- elseif itype == 'gif' then
- im = gd.createFromGif(file)
- end
- local w, h = im:sizeXY()
- for x = 0, w do
- for y = 0, h do
- color = tostring(bit.tohex(im:getPixel(x, y)))
- r = string.sub(color, 3, 4)
- g = string.sub(color, 5, 6)
- b = string.sub(color, 7, 8)
- if r ~= '00' and g ~= '00' and b ~= '00' then
- currentfont_pixels[y + 1][x + 1] = 1
- else
- --currentfont_pixels[y + 1][x + 1] = 0
- end
- end
- end
- f:write(({string.gsub(charStr, 'char', string.format('%q', char))})[1])
- for y = 1, ftf_h do
- for x = 1, ftf_w do
- if x == 1 then
- f:write('\t\t{', currentfont_pixels[y][x] and tostring(currentfont_pixels[y][x]) or '0', ', ')
- elseif x == ftf_w then
- f:write(currentfont_pixels[y][x] and tostring(currentfont_pixels[y][x]) or '0', '}')
- if y == ftf_h then
- f:write('\n')
- else
- f:write(',\n')
- end
- else
- f:write(currentfont_pixels[y][x] and tostring(currentfont_pixels[y][x]) or '0', ', ')
- end
- end
- end
- f:write('\t}\n}')
- end
- function font.auto()
- local charstr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-"\',.?/'
- for k = 1, #charstr do
- local image = gd.createTrueColor(ftf_w, ftf_h)
- image:png(string.sub(charstr, k, k) .. '.png')
- os.execute(string.sub(charstr, k, k) .. '.png')
- letter(string.sub(charstr, k, k) .. '.png', string.sub(charstr, k, k))
- end
- end
- function font.done()
- f:write(ftf_name .. [[['NULL'] = {
- ['descender'] = 0,
- ['kerning'] = 0,
- ['pixels'] = {
- {1, 1, 1, 1, 1},
- {1, 0, 0, 1, 1},
- {1, 1, 1, 0, 1},
- {1, 1, 0, 1, 1},
- {1, 1, 1, 1, 1},
- {1, 1, 0, 1, 1},
- {1, 1, 1, 1, 1},
- {0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0}
- }
- }]])
- f:close()
- end
Advertisement
Add Comment
Please, Sign In to add comment