OSDN Git Service

renamed cache file: ivs_*.lua -> extra_*.lua
[luatex-ja/luatexja.git] / src / ltj-jfont.lua
index f53f28d..9104f3b 100644 (file)
@@ -3,13 +3,15 @@
 --
 luatexbase.provides_module({
   name = 'luatexja.jfont',
-  date = '2014/02/01',
+  date = '2014/10/03',
   description = 'Loader for Japanese fonts',
 })
 module('luatexja.jfont', package.seeall)
 
 luatexja.load_module('base');      local ltjb = luatexja.base
 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
+luatexja.load_module('rmlgbm');    local ltjr = luatexja.rmlgbm
+luatexja.load_module('direction'); local ltjd = luatexja.direction
 
 
 local Dnode = node.direct or node
@@ -92,7 +94,6 @@ function define_jfm(t)
                v.align = 'left' -- left
             end
            if real_char then
-              print(i, v.width)
               if type(v.width)~='number' and v.width~='prop' then
                  defjfm_res= nil; return
               else
@@ -196,12 +197,25 @@ luatexbase.create_callback("luatexja.find_char_class", "data",
                           function (arg, fmtable, char)
                              return 0
                           end)
-
-function find_char_class(c,m)
--- c: character code, m:
-   if not m then return 0 end
-   return m.chars[c] or
-      luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
+do
+   local start_time_measure = ltjb.start_time_measure
+   local stop_time_measure = ltjb.stop_time_measure
+   local fcc_temp = { chars_cbcache = {} }
+   setmetatable(
+      fcc_temp.chars_cbcache,
+      {
+         __index = function () return 0 end,
+      })
+   function find_char_class(c,m)
+      -- c: character code, m:
+      local r = (m or fcc_temp).chars_cbcache[c]
+      if not r then
+         r = m.chars[c] or
+            luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
+         m.chars_cbcache[c or 0] = r
+      end
+      return r
+   end
 end
 
 
@@ -268,6 +282,7 @@ do
                        descent = ad.descender,
                        chars = sz.chars, char_type = sz.char_type,
                        kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip,
+                        chars_cbcache = {},
       }
 
       fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
@@ -279,13 +294,18 @@ do
 end
 
 do
+   local get_dir_count = ltjd.get_dir_count
+   local dir_tate = luatexja.dir_table.dir_tate
+   local tex_get_attr = tex.getattribute
    -- PUBLIC function
    function get_zw()
-      local a = font_metric_table[tex.attribute[attr_curjfnt]]
+      local a = font_metric_table[
+        tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
       return a and a.zw or 0
    end
    function get_zh()
-      local a = font_metric_table[tex.attribute[attr_curjfnt]]
+      local a = font_metric_table[
+        tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
       return a and a.zw or 0
    end
 end
@@ -325,7 +345,7 @@ do
       end
       if jfm_file_name~='' then
         local l = name:sub(-1)
-        name = name 
+        name = name
            .. ((l==':' or l==';') and '' or ';')
            .. 'jfm=' .. jfm_file_name
         if jfm_var~='' then
@@ -334,7 +354,18 @@ do
       end
       return name
    end
-   luatexja.jfont.extract_metric = extract_metric
+
+   -- define_font callback
+   local otfl_fdr = fonts.definers.read
+   local ltjr_font_callback = ltjr.font_callback
+   function luatexja.font_callback(name, size, id)
+      local new_name = extract_metric(name)
+      local res =  ltjr_font_callback(new_name, size, id, otfl_fdr)
+      luatexbase.call_callback('luatexja.define_font', res, new_name, size, id)
+      return res
+   end
+   luatexbase.create_callback('luatexja.define_font', 'simple', function (n) return n end)
+   luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
 end
 
 ------------------------------------------------------------------------
@@ -516,7 +547,7 @@ do
         alt_font_base_num = tex.getattribute(attr_curtfnt)
       else
         alt_font_base_num = tex.getattribute(attr_curjfnt)
-      end          
+      end
       local t = alt_font_table[alt_font_base_num]
       if t then
          for i,_ in pairs(t) do t[i]=nil end
@@ -570,34 +601,181 @@ do
 
 end
 
-
 ------------------------------------------------------------------------
--- MISC
+-- 追加のフォント情報
 ------------------------------------------------------------------------
+font_extra_info = {}
+local font_extra_info = font_extra_info -- key: fontnumber
+
+-- IVS and vertical metrics
+local prepare_fl_data
+do
+   local ivs -- temp table
+   local sort = table.sort
+   local function add_fl_table(dest, tg, unitable, glyphmax)
+      for i = 0, glyphmax-1 do
+        if tg[i] then
+           local gv = tg[i]
+           if gv.altuni then
+              for _,at in pairs(gv.altuni) do
+                 local bu, vsel = at.unicode, at.variant
+                 if vsel then
+                    if vsel>=0xE0100 then vsel = vsel - 0xE0100 end
+                    dest = dest or {}; dest[bu] = dest[bu] or {}
+                    local uniq_flag = true
+                    for i,_ in pairs(dest[bu]) do
+                       if i==vs then uniq_flag = false; break end
+                    end
+                    if uniq_flag then
+                       dest[bu][vsel] = unitable[gv.name]
+                    end
+                 end
+              end
+           end
+        end
+      end
+      return dest
+   end
+   prepare_fl_data = function (dest, id, fname)
+      local fl = fontloader.open(fname)
+      local unicodes = id.resources.unicodes
+      if fl.glyphs then
+        dest = add_fl_table(dest, fl.glyphs, id.resources.unicodes, fl.glyphmax)
+      end
+      if fl.subfonts then
+         for _,v in pairs(fl.subfonts) do
+            dest = add_fl_table(dest, v.glyphs, id.resources.unicodes, v.glyphmax)
+         end
+      end
+      fontloader.close(fl)
+      return dest
+   end
+end
+
+-- 縦書き用字形への変換テーブル
+local prepare_vert_data
+do
+   local function add_feature_table(tname, src, dest)
+      for i,v in pairs(src) do
+        if type(v.slookups)=='table' then
+           local s = v.slookups[tname]
+           if s  then
+              if not dest then dest = {} end
+              if not dest[i] then dest[i] = {} end
+              dest[i].vert = dest[i].vert or s
+           end
+        end
+      end
+      return dest
+   end
+   prepare_vert_data = function (dest, id) 
+      local a = id.resources.sequences
+      if a then
+        local s = id.shared.rawdata.descriptions
+        for i,v in pairs(a) do
+           if v.features.vert or v.features.vrt2 then
+              dest= add_feature_table(v.subtables[1], s, dest)
+           end
+        end
+      end
+      return dest
+   end
+   -- 縦書き用字形への変換
+   function get_vert_glyph(n, chr)
+      local fn = font_extra_info[n]
+      return (fn and fn[chr] and fn[chr].vert) or chr
+   end
+end
 
-local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
--- EXT: italic correction
-function append_italic()
-   local p = to_direct(tex.nest[tex.nest.ptr].tail)
-   if p and getid(p)==id_glyph then
-      local f = getfont(p)
-      local g = node_new(id_kern)
-      setfield(g, 'subtype', 1)
-      set_attr(g, attr_icflag, ITALIC)
-      if is_ucs_in_japanese_char(p) then
-        f = has_attr(p, attr_curjfnt)
-        local j = font_metric_table[f]
-        setfield(g, 'kern', j.char_type[find_char_class(getchar(p), j)].italic)
+-- 
+do
+   local font_extra_basename = {} -- key: basename
+   local cache_ver = 2
+   local checksum = file.checksum
+
+   local function prepare_extra_data(n, id)
+      -- test if already loaded
+      if type(id)=='number' then -- sometimes id is an integer
+         return
+      elseif (not id) or font_extra_info[n]  then return
+      end
+      local fname = id.filename
+      local bname = file.basename(fname)
+      if not fname then
+         font_extra_info[n] = {}; return
+      elseif font_extra_basename[bname] then
+         font_extra_info[n] = font_extra_basename[bname]; return
+      end
+      -- if the cache is present, read it
+      local newsum = checksum(fname) -- MD5 checksum of the fontfile
+      local v = "extra_" .. string.lower(file.nameonly(fname))
+      local dat = ltjb.load_cache(v,
+         function (t) return (t.version~=cache_ver) or (t.chksum~=newsum) end
+      )
+      -- if the cache is not found or outdated, save the cache
+      if dat then
+        font_extra_basename[bname] = dat[1] or {}
       else
-        local h = font_getfont(f)
-        if h then
-           setfield(g, 'kern', h.characters[getchar(p)].italic)
+        local dat = nil
+        dat = prepare_vert_data(dat, id)
+        dat = prepare_fl_data(dat, id, fname)
+        font_extra_basename[bname] = dat or {}
+        ltjb.save_cache( v,
+                         {
+                            chksum = checksum(fname),
+                            version = cache_ver,
+                            dat,
+                         })
+      end
+      font_extra_info[n] = font_extra_basename[bname]
+   end
+   luatexbase.add_to_callback('luatexja.define_font',
+                             function (res, name, size, id)
+                                prepare_extra_data(id, res)
+                             end,
+                             'ltj.prepare_extra_data', 1)
+
+   local function a (n, dat) font_extra_info[n] = dat end
+   ltjr.vert_addfunc = a
+
+   local identifiers = fonts.hashes.identifiers
+   for i=1,font.nextid()-1 do
+      if identifiers[i] then prepare_extra_data(i, identifiers[i]) end
+   end
+end
+
+------------------------------------------------------------------------
+-- MISC
+------------------------------------------------------------------------
+do
+   local get_dir_count = ltjd.get_dir_count
+   local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
+   local tex_set_attr = tex.setattribute
+   local font = font
+   -- EXT: italic correction
+   function append_italic()
+      local p = to_direct(tex.nest[tex.nest.ptr].tail)
+      if p and getid(p)==id_glyph then
+        local f = getfont(p)
+        local g = node_new(id_kern)
+        setfield(g, 'subtype', 1)
+        set_attr(g, attr_icflag, ITALIC)
+        if is_ucs_in_japanese_char(p) then
+           f = has_attr(p, (get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
+           local j = font_metric_table[f]
+           setfield(g, 'kern', j.char_type[find_char_class(getchar(p), j)].italic)
         else
-           tex.attribute[attr_icflag] = 0
-           return node_free(g)
+           local h = font_getfont(f) or font.fonts[f]
+           if h then
+              setfield(g, 'kern', h.characters[getchar(p)].italic)
+           else
+              tex_set_attr(attr_icflag, 0)
+              return node_free(g)
+           end
         end
+        node_write(g)
+        tex_set_attr(attr_icflag, 0)
       end
-      node_write(g)
-      tex.attribute[attr_icflag] = 0
    end
 end
+