- local sort = table.sort
- local uniq_flag
- local function add_ivs_table(tg, unitable)
- for gu, gv in pairs(tg) do
- local ga = gv.altuni
- if ga then
- for _,at in pairs(ga) do
- local bu, vs = at.unicode, (at.variant or 0)-0xE0100
- if vs>=0 and vs<0xF0 then
- if not ivs[bu] then ivs[bu] = {} end
- uniq_flag = true
- for i,_ in pairs(ivs[bu]) do
- if i==vs then uniq_flag = false; break end
- end
- if uniq_flag then
- ivs[bu][vs] = unitable[gv.name]
- end
- end
- end
- end
- end
- end
- local function make_ivs_table(id, fname)
- ivs = {}
- local fl = fontloader.open(fname)
- local ft = fontloader.to_table(fl)
- local unicodes = id.resources.unicodes
- add_ivs_table(ft.glyphs, id.resources.unicodes)
- if ft.subfonts then
- for _,v in pairs(ft.subfonts) do
- add_ivs_table(v.glyphs, id.resources.unicodes)
- end
- end
- fontloader.close(fl)
- return ivs
- end
-
--- loading and saving
- local font_ivs_basename = {} -- key: basename
- local path = {
- localdir = kpse.expand_var("$TEXMFVAR"),
- systemdir = kpse.expand_var("$TEXMFSYSVAR"),
- }
- local cache_dir = '/luatexja'
- local checksum = file.checksum
-
- local function ivs_cache_save(id, fname)
- local savepath = path.localdir .. cache_dir
- if not lfs.isdir(savepath) then dir.mkdirs(savepath) end
- savepath = file.join(savepath, "ivs_" .. string.lower(file.nameonly(fname)) .. ".lua")
- local result = make_ivs_table(id, fname)
- if file.iswritable(savepath) then
- table.tofile(savepath, { checksum(fname), result },
- 'return', false, true, false )
- --ltjb.package_info_no_line('luatexja', "saved :'" .. savepath .. "'", '')
- print("saved :'" .. savepath .. "'", '')
- else
- --ltjb.package_warning_no_line('luatexja', "failed to save to '" .. savepath .. "'", '')
- print("failed to save to '" .. savepath .. "'", '')
- end
- return result
- end
-
- local function ivs_cache_load(cname, id, fname)
- local result = require(cname)
- local newsum = checksum(fname)
- if newsum~=result[1] then
- return ivs_cache_save(id, fname)
- else
- return result[2]
- end
- end
-
- local function prepare_ivs_data(n, id)
- -- test if already loaded
- if type(id)=='number' then
- font_ivs_table[n] = font_ivs_table[id]; return
- end
- local fname = id.filename
- local bname = file.basename(fname)
- if not fname then
- font_ivs_table[n] = {}; return
- elseif font_ivs_basename[bname] then
- font_ivs_table[n] = font_ivs_basename[bname]; return
- end
-
- -- if cache is present; read them
- local v = "ivs_" .. string.lower(file.nameonly(fname)) .. ".lua"
- local localpath = file.join(path.localdir, cache_dir, v)
- local systempath = file.join(path.systemdir, cache_dir , v)
- local kpsefound = kpse.find_file(v)
- if kpsefound and file.isreadable(kpsefound) then
- font_ivs_basename[bname] = ivs_cache_load(kpsefound, id, fname)
- elseif file.isreadable(localpath) then
- font_ivs_basename[bname] = ivs_cache_load(localpath, id, fname)
- elseif file.isreadable(systempath) then
- font_ivs_basename[bname] = ivs_cache_load(systempath, id, fname)
- else
- font_ivs_basename[bname] = ivs_cache_save(id, fname)
- end
- if not font_ivs_basename[bname] then font_ivs_basename[bname] = {} end
- font_ivs_table[n] = font_ivs_basename[bname]
- end
- local ivs = {}
- ivs.font_ivs_table = font_ivs_table
- luatexja.ivs = ivs
-