From 63dff173d9a4d201890b0afa9f3168b45cc5f681 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Fri, 5 Jul 2013 23:56:29 +0200 Subject: prefer getwritablepath() to manual path building --- luaotfload-database.lua | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'luaotfload-database.lua') diff --git a/luaotfload-database.lua b/luaotfload-database.lua index c0aadaf..30ebb47 100644 --- a/luaotfload-database.lua +++ b/luaotfload-database.lua @@ -54,6 +54,7 @@ local utf8gsub = unicode.utf8.gsub local utf8lower = unicode.utf8.lower --- these come from Lualibs/Context +local getwritablepath = caches.getwritablepath local filebasename = file.basename local filecollapsepath = file.collapsepath or file.collapse_path local filedirname = file.dirname @@ -99,9 +100,9 @@ names.path = { dir = "", --- db and cache directory basename = config.luaotfload.names_file or "luaotfload-names.lua", - path = "", --- full path to db file + index = "", --- full path to db file lookup_basename = "luaotfload-lookup-cache.lua", --- cache file name - lookup_path = "", --- cache full path + lookups = "", --- cache full path } -- We use the cache.* of ConTeXt (see luat-basics-gen), we can @@ -109,13 +110,15 @@ names.path = { -- uses TEXMFCACHE or TEXMFVAR as starting points. local writable_path if caches then - writable_path = caches.getwritablepath "names" + writable_path = getwritablepath ("names", "") if not writable_path then - luaotfload.error("Impossible to find a suitable writeable cache...") + luaotfload.error + ("Impossible to find a suitable writeable cache...") end - names.path.dir = writable_path - names.path.path = filejoin(writable_path, names.path.basename) - names.path.lookup_path = filejoin(writable_path, names.path.lookup_basename) + names.path.dir = writable_path + names.path.index = getwritablepath ("names", names.path.basename) + names.path.lookups = getwritablepath ("names", + names.path.lookup_basename) else --- running as script, inject some dummies caches = { } logs = { report = function () end } @@ -330,7 +333,7 @@ local fuzzy_limit = 1 --- display closest only --- bool? -> dbobj load_names = function (dry_run) local starttime = os.gettimeofday() - local foundname, data = load_lua_file(names.path.path) + local foundname, data = load_lua_file(names.path.index) if data then report("both", 2, "db", @@ -352,7 +355,7 @@ load_names = function (dry_run) report("both", 0, "db", [[Font names database not found, generating new one.]]) report("both", 0, "db", - [[This can take several minutes; please be patient.]]) + [[This can take several minutes; please be patient.]]) data = update_names(fontnames_init(false), nil, dry_run) local success = save_names(data) if not success then @@ -365,7 +368,7 @@ end --- unit -> dbobj load_lookups = function ( ) - local foundname, data = load_lua_file(names.path.lookup_path) + local foundname, data = load_lua_file(names.path.lookups) if data then report("both", 3, "cache", "Lookup cache loaded (%s)", foundname) @@ -1930,12 +1933,12 @@ save_lookups = function ( ) local lookups = names.lookups local path = ensure_names_path() if fileiswritable(path) then - local luaname, lucname = make_savenames(names.path.lookup_path) + local luaname, lucname = make_savenames(names.path.lookups) if luaname then tabletofile(luaname, lookups, true) if lucname and type(caches.compile) == "function" then os.remove(lucname) - caches.compile(lookups, luaname, lucname) + print("\ncompile>", caches.compile(lookups, luaname, lucname)) report("both", 3, "cache", "Lookup cache saved") return true end @@ -1950,8 +1953,8 @@ end save_names = function (fontnames) if not fontnames then fontnames = names.data end local path = ensure_names_path() - if fileiswritable(path) then - local luaname, lucname = make_savenames(names.path.path) + if fileiswritable (path) then + local luaname, lucname = make_savenames(names.path.index) if luaname then --tabletofile(luaname, fontnames, true, { reduce=true }) tabletofile(luaname, fontnames, true) @@ -1964,6 +1967,8 @@ save_names = function (fontnames) return true end end + --else + --report("both", 0, "db", "Failed to save names database") end report("both", 0, "db", "Failed to save names database") return false @@ -2048,8 +2053,7 @@ end local getwritablecachepath = function ( ) --- fonts.handlers.otf doesn’t exist outside a Luatex run, --- so we have to improvise - local writable = caches.getwritablepath - (config.luaotfload.cache_dir) + local writable = getwritablepath (config.luaotfload.cache_dir) if writable then return writable end -- cgit v1.2.3 From c917abdd4c6032b1e715555bc626a987b014e4ab Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 6 Jul 2013 01:37:49 +0200 Subject: systemize path handling ; improve cache write errors --- luaotfload-database.lua | 161 +++++++++++++++++++++++++++--------------------- 1 file changed, 90 insertions(+), 71 deletions(-) (limited to 'luaotfload-database.lua') diff --git a/luaotfload-database.lua b/luaotfload-database.lua index 30ebb47..f09c348 100644 --- a/luaotfload-database.lua +++ b/luaotfload-database.lua @@ -27,6 +27,7 @@ local require = require local tonumber = tonumber local unpack = table.unpack + local fontloaderinfo = fontloader.info local fontloaderopen = fontloader.open local iolines = io.lines @@ -40,6 +41,7 @@ local lfsattributes = lfs.attributes local lfsdir = lfs.dir local mathabs = math.abs local mathmin = math.min +local osremove = os.remove local stringfind = string.find local stringformat = string.format local stringgmatch = string.gmatch @@ -52,7 +54,6 @@ local tablesort = table.sort local texiowrite_nl = texio.write_nl local utf8gsub = unicode.utf8.gsub local utf8lower = unicode.utf8.lower - --- these come from Lualibs/Context local getwritablepath = caches.getwritablepath local filebasename = file.basename @@ -96,29 +97,57 @@ end names.version = 2.207 names.data = nil --- contains the loaded database names.lookups = nil --- contains the lookup cache -names.path = { - dir = "", --- db and cache directory - basename = config.luaotfload.names_file + +names.path = { index = { }, lookups = { } } +names.path.globals = { + prefix = "", --- writable_path/names_dir + names_dir = config.luaotfload.names_dir or "names", + index_file = config.luaotfload.index_file or "luaotfload-names.lua", - index = "", --- full path to db file - lookup_basename = "luaotfload-lookup-cache.lua", --- cache file name - lookups = "", --- cache full path + lookups_file = "luaotfload-lookup-cache.lua", } --- We use the cache.* of ConTeXt (see luat-basics-gen), we can --- use it safely (all checks and directory creations are already done). It --- uses TEXMFCACHE or TEXMFVAR as starting points. -local writable_path +--- string -> (string * string) +local make_luanames = function (path) + return filereplacesuffix(path, "lua"), + filereplacesuffix(path, "luc") +end + +local report = logs.names_report + +--[[doc-- + We use the functions in the cache.* namespace that come with the + fontloader (see luat-basics-gen). it’s safe to use for the most part + since most checks and directory creations are already done. It + uses TEXMFCACHE or TEXMFVAR as starting points. + + There is one quirk, though: ``getwritablepath()`` will always + assume that files in subdirectories of the cache tree are writable. + It gives no feedback at all if it fails to open a file in write + mode. This may cause trouble when the index or lookup cache were + created by different user. +--doc]]-- + if caches then - writable_path = getwritablepath ("names", "") - if not writable_path then + local globals = names.path.globals + local names_dir = globals.names_dir + + prefix = getwritablepath (names_dir, "") + if not prefix then luaotfload.error ("Impossible to find a suitable writeable cache...") + else + report ("log", 0, "db", + "root cache directory is " .. prefix) end - names.path.dir = writable_path - names.path.index = getwritablepath ("names", names.path.basename) - names.path.lookups = getwritablepath ("names", - names.path.lookup_basename) + + globals.prefix = prefix + local lookups = names.path.lookups + local index = names.path.index + local lookups_file = filejoin (prefix, globals.lookups_file) + local index_file = filejoin (prefix, globals.index_file) + lookups.lua, lookups.luc = make_luanames (lookups_file) + index.lua, index.luc = make_luanames (index_file) else --- running as script, inject some dummies caches = { } logs = { report = function () end } @@ -129,9 +158,6 @@ end Auxiliary functions --doc]]-- - -local report = logs.names_report - --- string -> string local sanitize_string = function (str) if str ~= nil then @@ -270,12 +296,6 @@ local fontnames_init = function (keep_cache) --- returns dbobj } end ---- string -> (string * string) -local make_savenames = function (path) - return filereplacesuffix(path, "lua"), - filereplacesuffix(path, "luc") -end - --- When loading a lua file we try its binary complement first, which --- is assumed to be located at an identical path, carrying the suffix --- .luc. @@ -333,7 +353,7 @@ local fuzzy_limit = 1 --- display closest only --- bool? -> dbobj load_names = function (dry_run) local starttime = os.gettimeofday() - local foundname, data = load_lua_file(names.path.index) + local foundname, data = load_lua_file(names.path.index.lua) if data then report("both", 2, "db", @@ -368,7 +388,7 @@ end --- unit -> dbobj load_lookups = function ( ) - local foundname, data = load_lua_file(names.path.lookups) + local foundname, data = load_lua_file(names.path.lookups.lua) if data then report("both", 3, "cache", "Lookup cache loaded (%s)", foundname) @@ -1912,15 +1932,6 @@ update_names = function (fontnames, force, dry_run) return newfontnames end ---- unit -> string -local ensure_names_path = function ( ) - local path = names.path.dir - if not lfsisdir(path) then - lfsmkdirs(path) - end - return path -end - --- The lookup cache is an experimental feature of version 2.2; --- instead of incorporating it into the database it gets its own --- file. As we update it after every single addition this saves us @@ -1928,23 +1939,28 @@ end --- unit -> bool save_lookups = function ( ) - ---- this is boilerplate and should be refactored into something - ---- usable by both the db and the cache writers - local lookups = names.lookups - local path = ensure_names_path() - if fileiswritable(path) then - local luaname, lucname = make_savenames(names.path.lookups) - if luaname then - tabletofile(luaname, lookups, true) - if lucname and type(caches.compile) == "function" then - os.remove(lucname) - print("\ncompile>", caches.compile(lookups, luaname, lucname)) - report("both", 3, "cache", "Lookup cache saved") - return true - end + local lookups = names.lookups + local path = names.path.lookups + local luaname, lucname = path.lua, path.luc + if fileiswritable (luaname) and fileiswritable (lucname) then + tabletofile (luaname, lookups, true) + osremove (lucname) + caches.compile (lookups, luaname, lucname) + --- double check ... + if lfsisfile (luaname) and lfsisfile (luaname) then + report ("both", 3, "cache", "Lookup cache saved") + return true end + report ("info", 0, "cache", "Could not compile lookup cache") + return false + end + report ("info", 0, "cache", "Lookup cache file not writable") + if not fileiswritable (luaname) then + report ("info", 0, "cache", "failed to write %s", luaname) + end + if not fileiswritable (lucname) then + report ("info", 0, "cache", "failed to write %s", lucname) end - report("info", 0, "cache", "Could not write lookup cache") return false end @@ -1952,25 +1968,28 @@ end --- dbobj? -> bool save_names = function (fontnames) if not fontnames then fontnames = names.data end - local path = ensure_names_path() - if fileiswritable (path) then - local luaname, lucname = make_savenames(names.path.index) - if luaname then - --tabletofile(luaname, fontnames, true, { reduce=true }) - tabletofile(luaname, fontnames, true) - if lucname and type(caches.compile) == "function" then - os.remove(lucname) - caches.compile(fontnames, luaname, lucname) - report("info", 1, "db", "Font names database saved") - report("info", 3, "db", "Text: " .. luaname) - report("info", 3, "db", "Byte: " .. lucname) - return true - end + local path = names.path.index + local luaname, lucname = path.lua, path.luc + if fileiswritable (luaname) and fileiswritable (lucname) then + tabletofile (luaname, fontnames, true) + osremove (lucname) + caches.compile (fontnames, luaname, lucname) + if lfsisfile (luaname) and lfsisfile (luaname) then + report ("info", 1, "db", "Font index saved") + report ("info", 3, "db", "Text: " .. luaname) + report ("info", 3, "db", "Byte: " .. lucname) + return true end - --else - --report("both", 0, "db", "Failed to save names database") + report ("info", 0, "db", "Could not compile font index") + return false + end + report ("info", 0, "db", "Index file not writable") + if not fileiswritable (luaname) then + report ("info", 0, "db", "failed to write %s", luaname) + end + if not fileiswritable (lucname) then + report ("info", 0, "db", "failed to write %s", lucname) end - report("both", 0, "db", "Failed to save names database") return false end @@ -2004,7 +2023,7 @@ local purge_from_cache = function (category, path, list, all) if string.find(filename,"luatex%-cache") then -- safeguard if all then report("info", 5, "cache", "removing %s", filename) - os.remove(filename) + osremove(filename) n = n + 1 else local suffix = file.suffix(filename) @@ -2013,7 +2032,7 @@ local purge_from_cache = function (category, path, list, all) filename, "lua", "luc") if lfs.isfile(checkname) then report("info", 5, "cache", "Removing %s", filename) - os.remove(filename) + osremove(filename) n = n + 1 end end -- cgit v1.2.3 From 0df29d1d2f8c6c3a9e11959207912197a43d456f Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 6 Jul 2013 02:18:35 +0200 Subject: fix names of bytecode compiled files --- luaotfload-database.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'luaotfload-database.lua') diff --git a/luaotfload-database.lua b/luaotfload-database.lua index f09c348..f939e83 100644 --- a/luaotfload-database.lua +++ b/luaotfload-database.lua @@ -1947,7 +1947,7 @@ save_lookups = function ( ) osremove (lucname) caches.compile (lookups, luaname, lucname) --- double check ... - if lfsisfile (luaname) and lfsisfile (luaname) then + if lfsisfile (luaname) and lfsisfile (lucname) then report ("both", 3, "cache", "Lookup cache saved") return true end @@ -1956,10 +1956,10 @@ save_lookups = function ( ) end report ("info", 0, "cache", "Lookup cache file not writable") if not fileiswritable (luaname) then - report ("info", 0, "cache", "failed to write %s", luaname) + report ("info", 0, "cache", "Failed to write %s", luaname) end if not fileiswritable (lucname) then - report ("info", 0, "cache", "failed to write %s", lucname) + report ("info", 0, "cache", "Failed to write %s", lucname) end return false end @@ -1974,7 +1974,7 @@ save_names = function (fontnames) tabletofile (luaname, fontnames, true) osremove (lucname) caches.compile (fontnames, luaname, lucname) - if lfsisfile (luaname) and lfsisfile (luaname) then + if lfsisfile (luaname) and lfsisfile (lucname) then report ("info", 1, "db", "Font index saved") report ("info", 3, "db", "Text: " .. luaname) report ("info", 3, "db", "Byte: " .. lucname) @@ -1985,10 +1985,10 @@ save_names = function (fontnames) end report ("info", 0, "db", "Index file not writable") if not fileiswritable (luaname) then - report ("info", 0, "db", "failed to write %s", luaname) + report ("info", 0, "db", "Failed to write %s", luaname) end if not fileiswritable (lucname) then - report ("info", 0, "db", "failed to write %s", lucname) + report ("info", 0, "db", "Failed to write %s", lucname) end return false end -- cgit v1.2.3 From 3abd68b25bf2c12371df046c0a5ff035bcd20d11 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Wed, 10 Jul 2013 14:00:32 +0200 Subject: prioritize ``prefmodifiers`` oer ``subfamily`` follow-up to these previous modifications: * https://github.com/phi-gamma/luaotfload/commit/fcdbafd0c86fdba08 * https://github.com/phi-gamma/luaotfload/commit/2938e7d63404f1d2e * https://github.com/phi-gamma/luaotfload/commit/0e51cff12b24d264b addresses https://github.com/lualatex/luaotfload/issues/108 i.e. "name:DejaVu Serif" does not resolve to the Condensed face anymore. --- luaotfload-database.lua | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'luaotfload-database.lua') diff --git a/luaotfload-database.lua b/luaotfload-database.lua index f939e83..7215e11 100644 --- a/luaotfload-database.lua +++ b/luaotfload-database.lua @@ -799,7 +799,7 @@ resolve = function (_, _, specification) -- the 1st two parameters are used by C if facenames then family = facenames.family subfamily = facenames.subfamily - prefmodifiers = facenames.prefmodifiers + prefmodifiers = facenames.prefmodifiers or facenames.subfamily fullname = facenames.fullname psname = facenames.psname fontname = facenames.fontname @@ -818,16 +818,19 @@ resolve = function (_, _, specification) -- the 1st two parameters are used by C if continue == false then break end elseif style == subfamily then exact = add_to_match(exact, askedsize, face) + elseif synonym_set[style] and synonym_set[style][prefmodifiers] + or synonym_set.regular[prefmodifiers] + then + --- treat synonyms for prefmodifiers as first-class + --- (needed to prioritize DejaVu Book over Condensed) + exact = add_to_match(exact, askedsize, face) elseif name == fullname or name == pfullname or name == fontname or name == psname then - synonymous, continue = add_to_match(synonymous, askedsize, face) - elseif synonym_set[style] and - (synonym_set[style][prefmodifiers] or - synonym_set[style][subfamily]) - or synonym_set.regular[prefmodifiers] + synonymous = add_to_match(synonymous, askedsize, face) + elseif synonym_set[style] and synonym_set[style][subfamily] or synonym_set.regular[subfamily] then synonymous = add_to_match(synonymous, askedsize, face) -- cgit v1.2.3 From 12bc6faf98fa9e8fc4ecc1db2cde14ff620b0429 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Wed, 10 Jul 2013 18:01:15 +0200 Subject: bump version, update NEWS --- luaotfload-database.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'luaotfload-database.lua') diff --git a/luaotfload-database.lua b/luaotfload-database.lua index 7215e11..e0b977b 100644 --- a/luaotfload-database.lua +++ b/luaotfload-database.lua @@ -1,5 +1,5 @@ if not modules then modules = { } end modules ['luaotfload-database'] = { - version = 2.3, + version = "2.3a", comment = "companion to luaotfload.lua", author = "Khaled Hosny, Elie Roux, Philipp Gesang", copyright = "Luaotfload Development Team", -- cgit v1.2.3