OSDN Git Service

ltj-base.lua: use precompiled cache (.luc, .lub) if possible, as luaotfload
authorHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Sun, 22 Dec 2013 08:33:16 +0000 (17:33 +0900)
committerHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Sun, 22 Dec 2013 08:33:16 +0000 (17:33 +0900)
I tested with the manual of LuaTeX-ja:

                   .luc     .lua
(Japanese manual) 10.446s  10.624s
(English manual)   8.565s   8.723s

Used LuaTeX:
  > This is LuaTeX, Version beta-0.76.0-2013061708  (TeX Live 2013) (rev 4627)

src/ltj-base.lua
src/ltj-compat.lua
src/ltj-jisx0208.lua
src/ltj-otf.lua
src/ltj-rmlgbm.lua
src/luatexja-compat.sty
test/test19-ivs.tex

index 63ba97e..cc241a4 100644 (file)
@@ -3,7 +3,7 @@
 --
 luatexbase.provides_module({
   name = 'luatexja.base',
-  date = '2011/11/18',
+  date = '2013/12/22',
   description = '',
 })
 module('luatexja.base', package.seeall)
@@ -440,14 +440,28 @@ end
 
 
 -------------------- cache management
+-- load_cache (filename, outdate)
+--   * filename: without suffix '.lua'
+--   * outdate(t): return true iff the cache is outdated
+--   * return value: non-nil iff the cache is up-to-date
+-- save_cache (filename, t): no return value
+-- save_cache_luc (filename, t): no return value
+--   save_cache always calls save_cache_luc. 
+--   But sometimes we want to create only the precompiled cache,
+--   when its 'text' version is already present in LuaTeX-ja distribution.
+
 require('lualibs-lpeg') -- string.split
+require('lualibs-os')   -- os.type
 do
-   local path = string.split(
-      kpse.expand_var("$TEXMFCACHE;$TEXMFVAR;$TEXMFSYSVAR"), ';'
-   )
+   local path = kpse.expand_var("$TEXMFVAR;$TEXMFSYSVAR;$TEXMFCACHE")
+   if os.type~='windows' then path = string.gsub(path, ':', ';') end
+   path = string.split(path, ';')
+
    local cache_dir = '/luatexja'
    local find_file = kpse.find_file
    local join, isreadable = file.join, file.isreadable
+   local tofile, serialize = table.tofile, table.serialize
+   local luc_suffix = jit and '.lub' or '.luc'
 
    -- determine save path
    local savepath = ''
@@ -457,18 +471,48 @@ do
       if lfs.isdir(testpath) then savepath = testpath; break end
    end
 
-   -- filename: WITHOUT suffix '.lua'
+   function save_cache_luc(filename, t)
+      local fullpath = savepath .. '/' ..  filename .. luc_suffix
+      local s = serialize(t, 'return', false)
+      if s then
+        local f = io.open(fullpath, 'wb')
+        if f then 
+           f:write(string.dump(load(s), true)) 
+           texio.write('(save cache: ' .. fullpath .. ')')
+        end
+        f:close()
+      end
+   end
+
+   function save_cache(filename, t)
+      local fullpath = savepath .. '/' ..  filename .. '.lua'
+      tofile(fullpath, t, 'return', false)
+      texio.write('(save cache: ' .. fullpath .. ')')
+      save_cache_luc(filename, t)
+   end
+
+   local function luc_load (n)
+      texio.write('(load cache: ' .. n .. ')')
+      local f = loadfile(n, 'b'); return f
+   end
    function load_cache (filename, outdate)
-      local kpsefound = find_file(filename .. '.lua')
-      local result
-      if kpsefound and isreadable(kpsefound) then
-        result = require(kpsefound)
+      local r = load_cache_a(filename ..  luc_suffix, outdate, luc_load)
+      if r then 
+        return r
       else
-        for _,v in pairs(path) do
-           local fn = join(v, cache_dir, filename .. '.lua')
-           if isreadable(fn) then 
-              result = require(fn); break
-           end
+         local r = load_cache_a(filename .. '.lua', outdate, loadfile)
+        if r then save_cache_luc(filename, r) end -- update the precompiled cache
+        return r
+      end
+   end
+   
+   function load_cache_a (filename, outdate, loader)
+      local result
+      for _,v in pairs(path) do
+        local fn = join(v, cache_dir, filename)
+        if isreadable(fn) then 
+           result = loader(fn)
+           if result then result = result(); break end
         end
       end
       if (not result) or outdate(result) then 
@@ -478,12 +522,6 @@ do
       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
index 6f66b99..6fe8e4f 100644 (file)
@@ -5,6 +5,17 @@
 luatexja.load_module('base');   local ltjb = luatexja.base
 luatexja.load_module('stack');  local ltjs = luatexja.stack
 
+-- load jisx0208 table
+local cache_ver = 2
+
+local cache_outdate_fn = function (t) return t.version~=cache_ver end
+local jisx0208 = ltjb.load_cache('ltj-jisx0208',cache_outdate_fn)
+if not jisx0208 then -- make cache
+   jisx0208 = require('ltj-jisx0208.lua')
+   ltjb.save_cache_luc('ltj-jisx0208', jisx0208)
+end
+
+
 -- \kuten, \jis, \euc, \sjis, \ucs, \kansuji
 local function to_kansuji(num)
    if not num then num=0; return
@@ -40,7 +51,7 @@ local function from_kuten(i)
                         "I'm going to use 0 instead of that illegal character code.")
       i=0 
    end
-   tex.write(tostring(luatexja.jisx0208.table_jisx0208_uptex[i] or 0))
+   tex.write(tostring(jisx0208.table_jisx0208_uptex[i] or 0))
 end
 
 -- \euc: EUC-JP による符号位置 => Unicode 符号位置
index 1150dcb..1d9a70b 100644 (file)
@@ -1,4 +1,5 @@
-luatexja.jisx0208 = {
+return {
+version = 2,
 table_jisx0208_uptex = { 
  [0x101] = 0x3000, --( )
  [0x102] = 0x3001, --(、) 
index 41a3926..4fa7021 100644 (file)
@@ -1,16 +1,9 @@
 --
--- luatexja/otf.lua
+-- luatexja/ltj-otf.lua
 --
-luatexbase.provides_module({
-  name = 'luatexja.otf',
-  date = '2013/05/11',
-  description = 'The OTF Lua module for LuaTeX-ja',
-})
-
 require('unicode')
 require('lualibs')
 
-
 luatexja.load_module('base');      local ltjb = luatexja.base
 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
 luatexja.load_module('rmlgbm');    local ltjr = luatexja.rmlgbm
@@ -191,6 +184,7 @@ do
            for _,at in pairs(ga) do
               local bu, vsel = at.unicode, (at.variant or -1)
               if vsel~=-1 then
+                 if vsel>=0xE0100 then vsel = vsel - 0xE0100 end
                  if not ivs[bu] then ivs[bu] = {} end
                  uniq_flag = true
                   for i,_ in pairs(ivs[bu]) do
@@ -221,12 +215,12 @@ do
 
 -- loading and saving
    local font_ivs_basename = {} -- key: basename
-   local cache_ver = '3'
+   local cache_ver = '4'
    local checksum = file.checksum
 
    local function prepare_ivs_data(n, id)
       -- test if already loaded
-      if type(id)=='number' then 
+      if type(id)=='number' then -- sometimes id is an integer
          font_ivs_table[n] = font_ivs_table[id]; return 
       end
       local fname = id.filename
@@ -237,13 +231,13 @@ do
          font_ivs_table[n] = font_ivs_basename[bname]; return
       end
       
-      -- if cache is present; read them
-      local newsum = checksum(fname)
+      -- if the cache is present, read it
+      local newsum = checksum(fname) -- MD5 checksum of the fontfile
       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 the cache is not found or outdated, save the cache
       if dat then 
         font_ivs_basename[bname] = dat[1] or {}
       else
@@ -282,6 +276,7 @@ do
                   local qc = q.char
                   if (qc>=0xFE00 and qc<=0xFE0F) or (qc>=0xE0100 and qc<0xE01F0) then 
                     -- q is a variation selector
+                    if qc>=0xE0100 then qc = qc - 0xE0100 end
                      pt = pt and pt[p.char];  pt = pt and  pt[qc]
                      head = node_remove(head,q)
                      if pt then
@@ -324,7 +319,6 @@ do
    end
 end
 
--------------------- all done
 luatexja.otf = {
   append_jglyph = append_jglyph,
   enable_ivs = enable_ivs,  -- 隠し機能: IVS
index 283bc1c..5c8aa9f 100644 (file)
@@ -3,7 +3,7 @@
 --
 luatexbase.provides_module({
   name = 'luatexja.rmlgbm',
-  date = '2013/05/14',
+  date = '2013/12/22',
   description = 'Definitions of non-embedded Japanese (or other CJK) fonts',
 })
 module('luatexja.rmlgbm', package.seeall)
index 7feb21a..8d1d2d6 100644 (file)
@@ -65,7 +65,7 @@
   \expandafter\let\csname ifltj@in@latex\expandafter\endcsname
     \csname iftrue\endcsname
   \NeedsTeXFormat{LaTeX2e}
-  \ProvidesPackage{luatexja-compat}[2013/05/14 Compatibility with pTeX]
+  \ProvidesPackage{luatexja-compat}[2013/12/22 Compatibility with pTeX]
 \fi                             %</LaTeX>
 
 % ltj-compat-ptex.sty is not maintained for a long time.
 % not yet implemented.
 
 %%------------------
-\RequireLuaTeXjaSubmodule{jisx0208}
 \RequireLuaTeXjaSubmodule{compat}
 
 \def\euc{\ltj@code@conv{euc}}
index c1bc5ad..90979ac 100644 (file)
          for i,_ in pairs(pt) do t[1+#t]=i end
          sort(t)
          for _,i in ipairs(t) do 
-            tex.sprint('\\oalign{' .. s .. uchar(i+0xE0100)
+            if i<0xF0 then -- only IVS
+               tex.sprint('\\oalign{' .. s .. uchar(i+0xE0100)
                           .. '\\crcr\\hss\\tiny' .. tostring(i) .. '\\hss\\crcr}') 
+            end
          end
       end
    end