OSDN Git Service

ltj-jfmglue.lua: fix around italic correction
[luatex-ja/luatexja.git] / src / ltj-jfont.lua
index f53f28d..0669b83 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
@@ -572,32 +603,94 @@ end
 
 
 ------------------------------------------------------------------------
--- MISC
+-- 縦書き用字形への変換テーブル
 ------------------------------------------------------------------------
+local font_vert_table = {} -- key: fontnumber
+do
+   local font_vert_basename = {} -- key: basename
+   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 and not dest[i] then
+              dest[i] = s
+           end
+        end
+      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)
-      else
-        local h = font_getfont(f)
-        if h then
-           setfield(g, 'kern', h.characters[getchar(p)].italic)
+   local function prepare_vert_data(n, id)
+      -- test if already loaded
+      if type(id)=='number' then -- sometimes id is an integer
+         return
+      elseif (not id) or font_vert_table[n]  then return
+      end
+      local fname = id.filename
+      local bname = file.basename(fname)
+      if not fname then
+         font_vert_table[n] = {}; return
+      elseif font_vert_basename[bname] then
+         font_vert_table[n] = font_vert_basename[bname]; return
+      end
+      local vtable = {}
+      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 then
+              add_feature_table(v.subtables[1], s, vtable)
+           end
+        end
+      end
+      font_vert_basename[bname] = vtable
+      font_vert_table[n] = vtable
+   end
+   -- 縦書き用字形への変換
+   function get_vert_glyph(n, chr)
+      local fn = font_vert_table[n]
+      return fn and fn[chr] or chr
+   end
+   luatexbase.add_to_callback('luatexja.define_font',
+                             function (res, name, size, id)
+                                prepare_vert_data(id, res)
+                             end,
+                             'prepare_vert_data', 1)
+
+   local function a (n, dat) font_vert_table[n] = dat end
+   ltjr.vert_addfunc = a
+
+end
+
+------------------------------------------------------------------------
+-- MISC
+------------------------------------------------------------------------
+do
+   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, 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