OSDN Git Service

Merge branch 'master' into kitagawa_test
[luatex-ja/luatexja.git] / src / ltj-ruby.lua
index 84c31d7..060b322 100644 (file)
@@ -3,34 +3,31 @@
 --
 luatexbase.provides_module({
   name = 'luatexja.ruby',
-  date = '2014/05/08',
+  date = '2018/09/29',
   description = 'Ruby annotation',
 })
-module('luatexja.ruby', package.seeall)
-local err, warn, info, log = luatexbase.errwarinf(_NAME)
-
+luatexja.ruby = {}
 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
-
-local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
-local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
-local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
-local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
-local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
-local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
-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_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
-local insert_before, insert_after = Dnode.insert_before, Dnode.insert_after
+luatexja.load_module('base');      local ltjb = luatexja.base
+
+local to_node =  node.direct.tonode
+local to_direct =  node.direct.todirect
+
+local setfield =  node.direct.setfield
+local setglue = luatexja.setglue
+local getfield =  node.direct.getfield
+local getid =  node.direct.getid
+local getfont =  node.direct.getfont
+local getlist =  node.direct.getlist
+local getchar =  node.direct.getchar
+local getsubtype =  node.direct.getsubtype
+
+local node_new = node.direct.new
+local node_remove = node.direct.remove
+local node_next =  node.direct.getnext
+local node_copy, node_free, node_tail = node.direct.copy, node.direct.free, node.direct.tail
+local has_attr, set_attr = node.direct.has_attribute, node.direct.set_attribute
+local insert_before, insert_after = node.direct.insert_before, node.direct.insert_after
 
 local id_hlist = node.id('hlist')
 local id_vlist = node.id('vlist')
@@ -39,7 +36,6 @@ local id_whatsit = node.id('whatsit')
 local id_glue = node.id('glue')
 local id_kern = node.id('kern')
 local id_penalty = node.id('penalty')
-local id_glue_spec = node.id('glue_spec')
 local sid_user = node.subtype('user_defined')
 local ltjs_get_stack_table = luatexja.stack.get_stack_table
 local id_pbox_w = 258 -- cluster which consists of a whatsit
@@ -71,17 +67,16 @@ luatexja.userid_table.RUBY_PRE = luatexbase.newuserwhatsitid('ruby_pre',  'luate
 luatexja.userid_table.RUBY_POST = luatexbase.newuserwhatsitid('ruby_post',  'luatexja')
 local RUBY_PRE  = luatexja.userid_table.RUBY_PRE
 local RUBY_POST = luatexja.userid_table.RUBY_POST
+local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
 
 ----------------------------------------------------------------
 -- TeX interface 0
 ----------------------------------------------------------------
-if Dnode ~= node then
-   function cpbox() return node_copy(Dnode.getbox(0)) end
-else
-   function cpbox() return node.copy(tex.box[0]) end
+do
+   local getbox = node.direct.getbox
+   function luatexja.ruby.cpbox() return node_copy(getbox(0)) end
 end
 
-
 ----------------------------------------------------------------
 -- 補助関数群 1
 ----------------------------------------------------------------
@@ -124,7 +119,17 @@ end
 
 
 -- 実行回数 + ルビ中身 から uniq_id を作る関数
-old_break_info = {} -- public, 前 run 時の分割情報
+luatexja.ruby.old_break_info = {} -- public, 前 run 時の分割情報
+local old_break_info = luatexja.ruby.old_break_info
+local cache_handle
+function luatexja.ruby.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 +142,42 @@ 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 = node.direct.getprev
+   local function get_attr_icflag(p)
+      return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
+   end
+   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
+            set_attr(nh, attr_icflag,
+             get_attr_icflag(nh) + PROCESSED_BEGIN_FLAG)
+           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 node.direct.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
 
@@ -190,54 +208,36 @@ do
       local hh, hd = getfield(box, 'height'), getfield(box, 'depth')
       local hx = h
       while hx do
-         local hic = has_attr(hx, attr_icflag)
+        local hic = has_attr(hx, attr_icflag) or 0
         if (hic == KANJI_SKIP) or (hic == KANJI_SKIP_JFM)
             or (hic == XKANJI_SKIP) or (hic == XKANJI_SKIP_JFM)
-            or ((hic<=FROM_JFM+2) and (hic>=FROM_JFM-2)) then
+            or ((hic<=FROM_JFM+63) and (hic>=FROM_JFM)) then
            -- この 5 種類の空白をのばす
               if getid(hx) == id_kern then
                  local k = node_new(id_glue)
-                 local ks = node_new(id_glue_spec)
-                 setfield(ks, 'width', getfield(hx, 'kern'))
-                 setfield(ks, 'stretch_order', 2)
-                 setfield(ks, 'stretch', round(middle*65536))
-                 setfield(ks, 'shrink_order', 0); setfield(ks, 'shrink', 0)
-                 setfield(k, 'subtype', 0); setfield(k, 'spec', ks)
+                 setglue(k, getfield(hx, 'kern'), round(middle*65536), 0,
+                            2, 0)
+                 setfield(k, 'subtype', 0);
                  h = insert_after(h, hx, k);
                  h = node_remove(h, hx); node_free(hx); hx = k
               else -- glue
-                 local old_spec = getfield(hx, 'spec')
-                 local ks = node_copy(old_spec)
-                 setfield(ks, 'stretch_order', 2)
-                 setfield(ks, 'stretch', round(middle*65536))
-                 setfield(ks, 'shrink_order', 0); setfield(ks, 'shrink', 0)
-                 setfield(hx, 'spec', ks)
-                 -- decrease old_spec's reference count
-                 local b = node_new(id_glue)
-                 setfield(b, 'spec', old_spec); node_free(b)
+                  setglue(hx, getfield(hx, 'width'), round(middle*65536), 0,
+                            2, 0)
               end
         end
         hx = node_next(hx)
       end
       -- 先頭の空白を挿入
       local k = node_new(id_glue);
-      local ks = node_new(id_glue_spec)
-      setfield(ks, 'width', prenw)
-      setfield(ks, 'stretch_order', 2); setfield(ks, 'stretch', round(pre*65536))
-      setfield(ks, 'shrink_order', 0); setfield(ks, 'shrink', 0)
-      setfield(k, 'subtype', 0); setfield(k, 'spec', ks)
+      setglue(k, prenw, round(pre*65536), 0, 2, 0)
       h = insert_before(h, h, k);
       -- 末尾の空白を挿入
       local k = node_new(id_glue);
-      local ks = node_new(id_glue_spec);
-      setfield(ks, 'width', postnw)
-      setfield(ks, 'stretch_order', 2); setfield(ks, 'stretch', round(post*65536))
-      setfield(ks, 'shrink_order', 0); setfield(ks, 'shrink', 0)
-      setfield(k, 'subtype', 0);setfield(k, 'spec', ks)
+      setglue(k, postnw, round(post*65536), 0, 2, 0)
       insert_after(h, node_tail(h), k);
       -- hpack
       setfield(box, 'head', nil); node_free(box)
-      box = Dnode.hpack(h, new_width, 'exactly')
+      box = node.direct.hpack(h, new_width, 'exactly')
       setfield(box, 'height', hh)
       setfield(box, 'depth', hd)
       return box
@@ -258,6 +258,7 @@ local function texiface_low(rst, rtlr, rtlp)
    setfield(w, 'value', to_node(wv))
    setfield(wv, 'type', 100)
    setfield(wv, 'value', floor(#rtlr))
+   setfield(wv, 'user_id', RUBY_PRE) -- dummy
    set_attr(wv, attr_ruby, rst.rubyzw)
    set_attr(wv, attr_ruby_maxmargin, rst.maxmargin)
    set_attr(wv, attr_ruby_maxprep, rst.pre)
@@ -271,15 +272,15 @@ local function texiface_low(rst, rtlr, rtlp)
       _, n = insert_after(wv, n, rtlp[i])
    end
    -- w.value: (whatsit) .. r1 .. p1 .. r2 .. p2
-   Dnode.write(w); return w,wv
+   node.direct.write(w); return w,wv
 end
 
 -- rst: table
-function texiface(rst, rtlr, rtlp)
+function luatexja.ruby.texiface(rst, rtlr, rtlp)
    if #rtlr ~= #rtlp then
       for i=1, #rtlr do node_free(rtlr[i]) end
       for i=1, #rtlp do node_free(rtlp[i]) end
-      luatexja.base.package_error('luatexja-ruby',
+      ltjb.package_error('luatexja-ruby',
                                  'Group count mismatch between the ruby and\n' ..
                                     'the body (' .. #rtlr .. ' != ' .. #rtlp .. ').',
                                  '')
@@ -340,7 +341,7 @@ local function enlarge_parent(r, p, ppre, pmid, ppost, mapre, mapost, intmode)
    local rwidth = rwidth - pre_intrusion - post_intrusion
    setfield(r, 'width', rwidth)
    setfield(p, 'width', rwidth)
-   local ps = getfield(getlist(p), 'spec')
+   local ps = getlist(p)
    setfield(ps, 'width', getfield(ps, 'width') - pre_intrusion)
    return r, p, post_intrusion
 end
@@ -365,30 +366,30 @@ local function new_ruby_box(r, p, ppre, pmid, ppost,
       local need_repack = false
       -- margin が大きくなりすぎた時の処理
       if round(rpre*getfield(r, 'glue_set')*65536) > max_margin then
-        local ps = getfield(getlist(r), 'spec'); need_repack = true
+        local ps = getlist(r); need_repack = true
         setfield(ps, 'width', max_margin)
          setfield(ps, 'stretch', 1) -- 全く伸縮しないのも困る
       end
       if round(rpost*getfield(r, 'glue_set')*65536) > max_margin then
-        local ps = getfield(node_tail(getlist(r)), 'spec'); need_repack = true
+        local ps = node_tail(getlist(r)); need_repack = true
         setfield(ps, 'width', max_margin)
          setfield(ps, 'stretch', 1) -- 全く伸縮しないのも困る
       end
       if need_repack then
         local rt = r
-        r = Dnode.hpack(getlist(r), getfield(r, 'width'), 'exactly')
+        r = node.direct.hpack(getlist(r), getfield(r, 'width'), 'exactly')
         setfield(rt, 'head', nil); node_free(rt);
       end
    end
-   local a, k = node_new(id_rule), node_new(id_kern)
+   local a, k = node_new(id_rule), node_new(id_kern, 1)
    setfield(a, 'width', 0); setfield(a, 'height', 0)
    setfield(a, 'depth', 0); setfield(k, 'kern', rgap)
    insert_after(r, r, a); insert_after(r, a, k);
    insert_after(r, k, p); setfield(p, 'next', nil)
-   a = Dnode.vpack(r); setfield(a, 'shift', 0)
+   a = node.direct.vpack(r); setfield(a, 'shift', 0)
    set_attr(a, attr_ruby, post_intrusion)
    if rsmash or getfield(a, 'height')<getfield(p, 'height') then
-      local k = node_new(id_kern)
+      local k = node_new(id_kern, 1)
       setfield(k, 'kern', -getfield(a, 'height')+getfield(p, 'height'))
       setfield(a, 'head', k); insert_before(r, r, k)
       setfield(a, 'height', getfield(p, 'height'))
@@ -455,7 +456,7 @@ local function pre_low_cal_box(w, cmp)
 
    -- w.value の node list 更新.
    local nt = wv
-   Dnode.flush_list(node_next(wv))
+   node.direct.flush_list(node_next(wv))
    for i = 1, 2*cmp+1 do setfield(nt, 'next', kf[i]); nt = kf[i]  end
 
    if cmp==1 then     solve_1(coef)
@@ -468,7 +469,7 @@ end
 
 local first_whatsit
 do
-   local traverse_id = Dnode.traverse_id
+   local traverse_id = node.direct.traverse_id
    function first_whatsit(n) -- n 以後で最初の whatsit
       for h in traverse_id(id_whatsit, n) do
          return h
@@ -477,14 +478,12 @@ do
    end
 end
 
+local next_cluster_array = {}
 -- ノード追加
 local function pre_low_app_node(head, w, cmp, coef, ht, dp)
    -- メインの node list 更新
-   local nt, ntb = node_new(id_glue), node_new(id_glue_spec)
-   setfield(ntb, 'width', coef[1][2*cmp+2])
-   setfield(ntb, 'stretch_order', 0); setfield(ntb, 'stretch', 0)
-   setfield(ntb, 'shrink_order', 0); setfield(ntb, 'shrink', 0)
-   setfield(nt, 'subtype', 0); setfield(nt, 'spec', ntb)
+   local nt = node_new(id_glue)
+   setglue(nt, coef[1][2*cmp+2], 0, 0, 0, 0)
    set_attr(nt, attr_ruby, 1); set_attr(w, attr_ruby, 2)
    head = insert_before(head, w, nt)
    nt = w
@@ -497,17 +496,17 @@ 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);
-      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)
-      setfield(nt, 'subtype', 0); setfield(nt, 'spec', ntb)
+      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
+      setglue(nt, coef[i*2+1][2*cmp+2], 0, 0, 0, 0)
       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 +556,11 @@ local post_lown
 do
    local function write_aux(wv, num)
       local id = has_attr(wv, attr_ruby_id)
-      if 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 +574,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 +582,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 +592,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))
@@ -613,7 +612,7 @@ local function post_high_break(head)
    local rs = {}   -- rs: sequence of ruby_nodes,
    local rw = nil  -- rw: main whatsit
    local cmp = -2  -- dummy
-   for h in Dnode.traverse_id(id_hlist, to_direct(head)) do
+   for h in node.direct.traverse_id(id_hlist, to_direct(head)) do
       for i = 1, #rs do rs[i] = nil end
       local ha = getlist(h)
       while ha do
@@ -681,9 +680,9 @@ luatexbase.add_to_callback('hpack_filter', post_high_hbox, 'ltj.ruby.post_hbox',
 ----------------------------------------------------------------
 do
    local RIPRE  = luatexja.stack_table_index.RIPRE
-   local function whatsit_callback(Np, lp, Nq, bsl)
+   local function whatsit_callback(Np, lp, Nq)
       if Np.nuc then return Np
-      elseif getfield(lp, 'user_id') == RUBY_PRE then
+      elseif  getfield(lp, 'user_id') == RUBY_PRE then
          Np.first, Np.nuc, Np.last = lp, lp, lp
          local lpv = getfield(lp, 'value')
          local x = node_next(node_next(lpv))
@@ -719,6 +718,8 @@ do
             set_attr(lpv, attr_ruby_maxprep, 0)
          end
          return Np
+      else
+        return Np
       end
    end
    luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
@@ -727,8 +728,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