Add random checks to see if font changed in the meantime.

This commit is contained in:
kaeza 2013-10-18 06:11:21 -02:00
parent 6f95d10fd6
commit cbbe2ccaef

View File

@ -55,6 +55,37 @@ local charwidth = { }
-- File to cache the font size to. -- File to cache the font size to.
local CHARDB_FILE = minetest.get_worldpath().."/homedecor_chardb" local CHARDB_FILE = minetest.get_worldpath().."/homedecor_chardb"
local function check_random_chars()
for i = 1, 5 do
local c = math.random(32, 126)
local filename = FONT_FMT:format(TP, c)
local f = io.open(filename)
-- 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?
-- Just assume it's different.
if not (w and h) then return true end
local ch = string.char(c)
if (not charwidth[ch]) -- Char is not cached.
or (charwidth[ch] ~= w) -- Width differs.
or (LINE_HEIGHT and (LINE_HEIGHT ~= h)) -- Height differs
then
-- In any case, file is different; rebuild cache.
return true
end
end
-- OK, our superficial check passed. If the textures are messed up,
-- it's not our problem.
return false
end
local function build_char_db() local function build_char_db()
LINE_HEIGHT = nil LINE_HEIGHT = nil
@ -72,13 +103,14 @@ local function build_char_db()
if cdbf then if cdbf then
minetest.log("info", "[homedecor] Reading cached character database.") minetest.log("info", "[homedecor] Reading cached character database.")
for line in cdbf:lines() do for line in cdbf:lines() do
local ch, w = line:match("(0x[0-9A-Fa-f]+)%s+([0-9][0-9]+)") local ch, w = line:match("(0x[0-9A-Fa-f]+)%s+([0-9][0-9]*)")
if ch and w then if ch and w then
local c = tonumber(ch, 16) local c = tonumber(ch)
w = tonumber(w) w = tonumber(w)
print("*** DEBUG: c="..tostring(c)..", w="..tostring(w))
if c and w then if c and w then
if c == 0 then if c == 0 then
LINE_HEIGHT = h LINE_HEIGHT = w
elseif (c >= 32) and (c < 127) then elseif (c >= 32) and (c < 127) then
charwidth[string.char(c)] = w charwidth[string.char(c)] = w
total_width = total_width + w total_width = total_width + w
@ -93,8 +125,21 @@ local function build_char_db()
-- XXX: Remember to change similar lines below if this changes. -- XXX: Remember to change similar lines below if this changes.
SIGN_WIDTH = math.floor((total_width / char_count) * 16) SIGN_WIDTH = math.floor((total_width / char_count) * 16)
SIGN_PADDING = SIGN_WIDTH / 14 -- Totally arbitrary. SIGN_PADDING = SIGN_WIDTH / 14 -- Totally arbitrary.
return
-- Check some random characters to see if the file on disk differs
-- from the cached one. If so, then ditch cached data and rebuild
-- (font probably was changed).
print("*** DEBUG: Randomly checking cache.")
if not check_random_chars() then
print("*** DEBUG: yey all ok.")
return
end
print("*** DEBUG: something's fucked up; rebuild cache.")
else else
print("[homedecor] Warning:"
.." Could not find font line height in cached DB."
.." Trying brute force."
)
minetest.log("warning", "[homedecor]" minetest.log("warning", "[homedecor]"
.." Could not find font line height in cached DB." .." Could not find font line height in cached DB."
.." Trying brute force." .." Trying brute force."
@ -104,6 +149,8 @@ local function build_char_db()
-- OK, something went wrong... try brute force loading from texture files. -- OK, something went wrong... try brute force loading from texture files.
charwidth = { }
total_width = 0 total_width = 0
char_count = 0 char_count = 0
LINE_HEIGHT = nil LINE_HEIGHT = nil