OSDN Git Service

'Kanjiskip=<bool>' in luatexja-fontspec.
[luatex-ja/luatexja.git] / src / ltj-ruby.lua
index e21c95a..da638f5 100644 (file)
@@ -3,7 +3,7 @@
 --
 luatexbase.provides_module({
   name = 'luatexja.ruby',
-  date = '2014/05/08',
+  date = '2015/09/18',
   description = 'Ruby annotation',
 })
 module('luatexja.ruby', package.seeall)
@@ -12,7 +12,6 @@ local err, warn, info, log = luatexbase.errwarinf(_NAME)
 luatexja.load_module('stack');     local ltjs = luatexja.stack
 
 local Dnode = node.direct or node
-
 local nullfunc = function(n) return n end
 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
@@ -26,7 +25,7 @@ local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char e
 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
 
 local node_new = Dnode.new
-local node_remove = luatexja.Dnode_remove -- Dnode.remove
+local node_remove = Dnode.remove
 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
 local node_copy, node_free, node_tail = Dnode.copy, Dnode.free, Dnode.tail
 local has_attr, set_attr = Dnode.has_attribute, Dnode.set_attribute
@@ -125,6 +124,15 @@ end
 
 -- 実行回数 + ルビ中身 から uniq_id を作る関数
 old_break_info = {} -- public, 前 run 時の分割情報
+local cache_handle
+function read_old_break_info()
+   if  tex.jobname then
+      local fname = tex.jobname .. '.ltjruby'
+      local real_file = kpse.find_file(fname)
+      if real_file then dofile(real_file) end
+      cache_handle = io.open(fname, 'w')
+   end
+end
 local make_uniq_id
 do
    local exec_count = 0
@@ -137,29 +145,37 @@ end
 -- concatenation of boxes: reusing nodes
 -- ルビ組版が行われている段落/hboxでの設定が使われる.
 -- ルビ文字を格納しているボックスでの設定ではない!
-local function concat(f, b)
-   if f then
-      if b then
-         local h = getlist(f)
-         setfield(node_tail(h), 'next', getlist(b))
-        setfield(f, 'head', nil); node_free(f)
-         setfield(b, 'head', nil); node_free(b)
-        return Dnode.hpack(luatexja.jfmglue.main(h,false))
+local concat
+do
+   local node_prev = (Dnode ~= node) and Dnode.getprev or node.prev
+   function concat(f, b)
+      if f then
+        if b then
+           local h, nh = getlist(f), getlist(b)
+           if getid(nh)==id_whatsit and getsubtype(nh)==sid_user then
+              nh=node_next(nh); node_free(node_prev(nh))
+           end
+           setfield(node_tail(h), 'next', nh)
+           setfield(f, 'head', nil); node_free(f)
+           setfield(b, 'head', nil); node_free(b)
+           local g = luatexja.jfmglue.main(h,false)
+           return Dnode.hpack(g)
+        else
+           return f
+        end
+      elseif b then
+        return b
       else
-        return f
+        local h = node_new(id_hlist)
+        setfield(h, 'subtype', 0)
+        setfield(h, 'width', 0)
+        setfield(h, 'height', 0)
+        setfield(h, 'depth', 0)
+        setfield(h, 'glue_set', 0)
+        setfield(h, 'glue_order', 0)
+        setfield(h, 'head', nil)
+        return h
       end
-   elseif b then
-      return b
-   else
-      local h = node_new(id_hlist)
-      setfield(h, 'subtype', 0)
-      setfield(h, 'width', 0)
-      setfield(h, 'height', 0)
-      setfield(h, 'depth', 0)
-      setfield(h, 'glue_set', 0)
-      setfield(h, 'glue_order', 0)
-      setfield(h, 'head', nil)
-      return h
    end
 end
 
@@ -477,6 +493,7 @@ do
    end
 end
 
+local next_cluster_array = {}
 -- ノード追加
 local function pre_low_app_node(head, w, cmp, coef, ht, dp)
    -- メインの node list 更新
@@ -497,17 +514,21 @@ local function pre_low_app_node(head, w, cmp, coef, ht, dp)
       insert_after(head, nt, nta)
       set_attr(nta, attr_ruby, 2*i+1)
       -- glue
-      nt = node_new(id_glue)
-      local ntb = node_new(id_glue_spec);
+       local ntb = node_new(id_glue_spec);
       setfield(ntb, 'width', coef[i*2+1][2*cmp+2])
       setfield(ntb, 'stretch_order', 0); setfield(ntb, 'stretch', 0)
       setfield(ntb, 'shrink_order', 0); setfield(ntb, 'shrink', 0)
+      if i~=cmp or not next_cluster_array[w] then
+        nt = node_new(id_glue); insert_after(head, nta, nt)
+      else
+        nt = next_cluster_array[w]
+      end
       setfield(nt, 'subtype', 0); setfield(nt, 'spec', ntb)
       set_attr(nt, attr_ruby, 2*i+2)
-      insert_after(head, nta, nt)
    end
    tex.setattribute('global', attr_ruby, -0x7FFFFFFF)
    setfield(w, 'user_id', RUBY_POST)
+   next_cluster_array[w]=nil
    return head, first_whatsit(node_next(nt))
 end
 
@@ -557,11 +578,11 @@ local post_lown
 do
    local function write_aux(wv, num)
       local id = has_attr(wv, attr_ruby_id)
-      if false and id>0 then
-        tex.sprint(cat_lp,
-                   '\\write\\@mainaux{\\string\\directlua{luatexja.ruby.old_break_info['
+      if id>0 and cache_handle then
+        cache_handle:write(
+                   'luatexja.ruby.old_break_info['
                       .. tostring(id) .. ']=' .. num
-                      .. '}}')
+                      .. '\n')
       end
    end
 
@@ -575,7 +596,7 @@ do
         if fn==2*cmp+2 then
            local hn = node_tail(wv)
            node_remove(wv, hn)
-           insert_after(ch, rs[#rs], hn)
+           insert_after(ch, rs[1], hn)
            set_attr(hn, attr_icflag,  PROCESSED)
            write_aux(wv, has_attr(hn, attr_ruby))-- 行中形
         else
@@ -583,7 +604,7 @@ do
            for i = 1, deg do hn = node_next(hn) end;
            node_remove(wv, hn)
            setfield(hn, 'next', nil)
-           insert_after(ch, rs[#rs], hn)
+           insert_after(ch, rs[1], hn)
            set_attr(hn, attr_icflag,  PROCESSED)
            write_aux(wv, has_attr(hn, attr_ruby))
         end
@@ -593,7 +614,7 @@ do
         -- -1 is needed except the case hn = 3,
         --   because a ending-line form is removed already from the list
         node_remove(wv, hn); setfield(hn, 'next', nil)
-        insert_after(ch, rs[#rs], hn)
+        insert_after(ch, rs[1], hn)
         set_attr(hn, attr_icflag,  PROCESSED)
         if fn == 2*cmp-1 then
            write_aux(wv, has_attr(hn, attr_ruby))
@@ -727,8 +748,15 @@ end
 
 do
    local RIPOST = luatexja.stack_table_index.RIPOST
-   local function whatsit_after_callback(s, Nq, Np, bsl)
+   local function whatsit_after_callback(s, Nq, Np)
       if not s and  getfield(Nq.nuc, 'user_id') == RUBY_PRE then
+        if Np then
+           local last_glue = node_new(id_glue)
+           set_attr(last_glue, attr_icflag, 0)
+           insert_before(Nq.nuc, Np.first, last_glue)
+           Np.first = last_glue
+           next_cluster_array[Nq.nuc] = last_glue -- ルビ処理用のグルー
+        end
          local nqnv = getfield(Nq.nuc, 'value')
          local x =  node_next(node_next(nqnv))
          for i = 2, getfield(nqnv, 'value') do x = node_next(node_next(x)) end