Simplify a bit the code.

This commit is contained in:
kaeza 2013-10-24 19:52:33 -02:00
parent b4a16205f3
commit 8c2ee97b8f

View File

@ -23,14 +23,20 @@ local PNG_HDR = string.char(0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A)
-- Read the image size from a PNG file. -- Read the image size from a PNG file.
-- Returns image_w, image_h. -- Returns image_w, image_h.
-- Only the LSB is read from each field! -- Only the LSB is read from each field!
local function read_png_size(f) local function read_char_size(c)
local filename = FONT_FMT:format(TP, c)
local f = io.open(filename, "rb")
f:seek("set", 0x0) f:seek("set", 0x0)
local hdr = f:read(8) local hdr = f:read(8)
if hdr ~= PNG_HDR then return end if hdr ~= PNG_HDR then
f:close()
return
end
f:seek("set", 0x13) f:seek("set", 0x13)
local ws = f:read(1) local ws = f:read(1)
f:seek("set", 0x17) f:seek("set", 0x17)
local hs = f:read(1) local hs = f:read(1)
f:close()
return ws:byte(), hs:byte() return ws:byte(), hs:byte()
end end
@ -59,18 +65,9 @@ local CHARDB_FILE = minetest.get_worldpath().."/homedecor_chardb"
local function check_random_chars() local function check_random_chars()
for i = 1, 5 do for i = 1, 5 do
local c = math.random(32, 126) local c = math.random(32, 126)
local filename = FONT_FMT:format(TP, c) local w, h = read_char_size(c)
local f = io.open(filename, "rb")
-- File does not exist (or cannot be read, or ...).
-- Just assume it's different.
if not f then return true end
local w, h = read_png_size(f)
f:close()
-- File is not a PNG... wut? -- File is not a PNG... wut?
-- Just assume it's different.
if not (w and h) then return true end if not (w and h) then return true end
local ch = string.char(c) local ch = string.char(c)
@ -145,18 +142,13 @@ local function build_char_db()
char_count = 0 char_count = 0
for c = 32, 126 do for c = 32, 126 do
local filename = FONT_FMT:format(TP, c) local w, h = read_char_size(c)
local f = io.open(filename, "rb") if w and h then
if f then
local ch = string.char(c) local ch = string.char(c)
local w, h = read_png_size(f) charwidth[ch] = w
f:close() total_width = total_width + w
if w and h then char_count = char_count + 1
charwidth[ch] = w if not LINE_HEIGHT then LINE_HEIGHT = h end
total_width = total_width + w
char_count = char_count + 1
if not LINE_HEIGHT then LINE_HEIGHT = h end
end
end end
end end