OSDN Git Service

moved cache managementcode into ltj-base.lua.
authorHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Sat, 21 Dec 2013 13:32:05 +0000 (22:32 +0900)
committerHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Sat, 21 Dec 2013 13:32:05 +0000 (22:32 +0900)
src/ltj-base.lua
src/ltj-otf.lua
src/ltj-rmlgbm.lua

index d45a376..63ba97e 100644 (file)
@@ -438,5 +438,53 @@ function get_cs(s)
    tex.sprint(cat_lp,'\\' .. s)
 end
 
+
+-------------------- cache management
+require('lualibs-lpeg') -- string.split
+do
+   local path = string.split(
+      kpse.expand_var("$TEXMFCACHE;$TEXMFVAR;$TEXMFSYSVAR"), ';'
+   )
+   local cache_dir = '/luatexja'
+   local find_file = kpse.find_file
+   local join, isreadable = file.join, file.isreadable
+
+   -- determine save path
+   local savepath = ''
+   for _,v in pairs(path) do
+      local testpath =  join(v, cache_dir)
+      if not lfs.isdir(testpath) then dir.mkdirs(testpath) end
+      if lfs.isdir(testpath) then savepath = testpath; break end
+   end
+
+   -- filename: WITHOUT suffix '.lua'
+   function load_cache (filename, outdate)
+      local kpsefound = find_file(filename .. '.lua')
+      local result
+      if kpsefound and isreadable(kpsefound) then
+        result = require(kpsefound)
+      else
+        for _,v in pairs(path) do
+           local fn = join(v, cache_dir, filename .. '.lua')
+           if isreadable(fn) then 
+              result = require(fn); break
+           end
+        end
+      end
+      if (not result) or outdate(result) then 
+        return nil 
+      else 
+        return result 
+      end
+   end
+
+   function save_cache(filename, t)
+      local fullpath = savepath .. '/' ..  filename .. '.lua'
+      table.tofile(fullpath, t, 'return', false, true, false )
+      package_info_no_line('luatexja', "save '" .. fullpath .. "'", '')
+   end
+
+end
+
 -------------------- all done
 -- EOF
index e36a197..41a3926 100644 (file)
@@ -181,6 +181,7 @@ luatexbase.add_to_callback("luatexja.find_char_class",
 local font_ivs_table = {} -- key: fontnumber
 local enable_ivs
 do
+   local ivs
    local sort = table.sort
    local uniq_flag
    local function add_ivs_table(tg, unitable)
@@ -188,15 +189,15 @@ 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
+              local bu, vsel = at.unicode, (at.variant or -1)
+              if vsel~=-1 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]
+                     ivs[bu][vsel] = unitable[gv.name]
                   end
               end
            end
@@ -220,40 +221,9 @@ do
 
 -- 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 cache_ver = '3'
    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 
@@ -268,25 +238,26 @@ do
       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)
+      local newsum = checksum(fname)
+      local v = "ivs_" .. string.lower(file.nameonly(fname))
+      local dat = ltjb.load_cache(v, 
+         function (t) return (t.version~=cache_ver) or (t.chksum~=newsum) end
+      )
+      -- if cache is not found or outdated, save the cache
+      if dat then 
+        font_ivs_basename[bname] = dat[1] or {}
       else
-         font_ivs_basename[bname] = ivs_cache_save(id, fname)
+        dat = make_ivs_table(id, fname)
+        font_ivs_basename[bname] = dat or {}
+        ltjb.save_cache( v,
+                         {
+                            chksum = checksum(fname), 
+                            version = cache_ver,
+                            dat,
+                         })
       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
 
 -- 組版時
    local function ivs_jglyph(char, bp, pf)
@@ -309,8 +280,9 @@ do
                local q = node_next(p) -- the next node of p
                if q and q.id==id_glyph then
                   local qc = q.char
-                  if qc>=0xE0100 and qc<0xE01F0 then -- q is an IVS selector
-                     pt = pt and pt[p.char];  pt = pt and  pt[qc-0xE0100]
+                  if (qc>=0xFE00 and qc<=0xFE0F) or (qc>=0xE0100 and qc<0xE01F0) then 
+                    -- q is a variation selector
+                     pt = pt and pt[p.char];  pt = pt and  pt[qc]
                      head = node_remove(head,q)
                      if pt then
                         local np = ivs_jglyph(pt, p, pf)
index 7ee8103..283bc1c 100644 (file)
@@ -14,11 +14,7 @@ luatexja.load_module('base');      local ltjb = luatexja.base
 cidfont_data = {}
 local cidfont_data = cidfont_data
 local cache_chars = {}
-local path           = {
-    localdir  = kpse.expand_var("$TEXMFVAR"),
-    systemdir = kpse.expand_var("$TEXMFSYSVAR"),
-}
-local cache_dir = '/luatexja'
+local cache_ver = '2'
 
 local cid_reg, cid_order, cid_supp, cid_name
 local cid_replace = {
@@ -184,42 +180,24 @@ do
       end
 
       -- Save
-      local savepath  = path.localdir .. cache_dir
-      if not lfs.isdir(savepath) then
-         dir.mkdirs(savepath)
-      end
-      savepath = file.join(savepath, "ltj-cid-auto-" 
-                           .. string.lower(cid_name)  .. ".lua")
-      if file.iswritable(savepath) then
-         k.characters[46].width = math.floor(655360/14);
-        -- Standard fonts are ``seriffed''. 
-         table.tofile(savepath, k,'return', false, true, false )
-         ltjb.package_info_no_line('luatexja', "saved :'" .. savepath .. "'", '')
-      else 
-         ltjb.package_warning_no_line('luatexja', "failed to save to '" .. savepath .. "'", '')
-      end
+      k.characters[46].width = math.floor(655360/14);
+      ltjb.save_cache( "ltj-cid-auto-" .. string.lower(cid_name),
+                      {
+                         version = cache_ver,
+                         k,
+                      })
    end
 end
 local make_cid_font = make_cid_font
 
 -- 
-local function cid_cache_load(fullpath)
-   cidfont_data[cid_name] = require(fullpath)
-   cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
-end
-
+local function cid_cache_outdated(t) return t.version~=cache_ver end
 local function read_cid_font()
-   -- local v = "ltj-cid-" .. string.lower(cid_name) .. ".lua"
-   local v = "ltj-cid-auto-" .. string.lower(cid_name) .. ".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
-      cid_cache_load(kpsefound)
-   elseif file.isreadable(localpath)  then
-      cid_cache_load(localpath)
-   elseif file.isreadable(systempath) then
-      cid_cache_load(systempath)
+   local dat = ltjb.load_cache("ltj-cid-auto-" .. string.lower(cid_name),
+                              cid_cache_outdated )
+   if dat then 
+      cidfont_data[cid_name] = dat[1]
+      cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
    else
       -- Now we must create the virtual metrics from CMap.
       make_cid_font()