From 1148e466517c16e8f357349bc9cd3fd827aaf8a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Fdez?= Date: Wed, 13 Jan 2021 11:45:30 +0100 Subject: [PATCH] migrate fontlist to persist --- frontend/fontlist.lua | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/frontend/fontlist.lua b/frontend/fontlist.lua index 2ce4f58df..17d4942e7 100644 --- a/frontend/fontlist.lua +++ b/frontend/fontlist.lua @@ -1,8 +1,8 @@ local CanvasContext = require("document/canvascontext") local DataStorage = require("datastorage") -local dump = require("dump") local FT = require("ffi/freetype") local HB = require("ffi/harfbuzz") +local Persist = require("persist") local util = require("util") local logger = require("logger") local dbg = require("dbg") @@ -168,12 +168,15 @@ end function FontList:getFontList() if #self.fontlist > 0 then return self.fontlist end - local cache_file = DataStorage:getDataDir() .. "/cache/fontinfo.dat" - local ok - ok, self.fontinfo = pcall(dofile, cache_file) - if not ok or not self.fontinfo then - self.fontinfo = {} + local cache = Persist:new{ + path = DataStorage:getDataDir() .. "/cache/fontinfo.dat" + } + + local t, err = cache:load() + if not t then + logger.info(cache.path, err, "initializing it") end + self.fontinfo = t or {} -- used for marking fonts we're seeing local mark = { cache_dirty = false } @@ -194,16 +197,10 @@ function FontList:getFontList() if dbg.is_verbose then -- when verbose debug is on, always dump the cache in plain text (to inspect the db output) - local cache = io.open(cache_file, "w") - cache:write("return " .. dump(self.fontinfo)) - cache:close() + cache:save(self.fontinfo) elseif mark.cache_dirty then -- otherwise dump the db in binary (more compact), and only if something has changed - local bc = load("return " .. dump(self.fontinfo)) - bc = string.dump(bc, true) - local cache = io.open(cache_file, "wb") - cache:write(bc) - cache:close() + cache:save(self.fontinfo, true) end local names = self.fontnames