OSDN Git Service

Dnode -> node.direct, stopped to use 'spec'
[luatex-ja/luatexja.git] / src / ltj-ruby.lua
index b9ebe67..bf57c77 100644 (file)
@@ -3,34 +3,32 @@
 --
 luatexbase.provides_module({
   name = 'luatexja.ruby',
-  date = '2014/03/19',
-  description = 'Ruby',
+  date = '2015/09/18',
+  description = 'Ruby annotation',
 })
 module('luatexja.ruby', package.seeall)
 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
-
-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
+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 +37,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
@@ -75,13 +72,11 @@ local RUBY_POST = luatexja.userid_table.RUBY_POST
 ----------------------------------------------------------------
 -- 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 cpbox() return node_copy(getbox(0)) end
 end
 
-
 ----------------------------------------------------------------
 -- 補助関数群 1
 ----------------------------------------------------------------
@@ -91,27 +86,49 @@ local function gauss(coef)
    local deg = #coef
    for i = 1, deg do
       if coef[i][i]==0 then
-        for j = i+1, deg do 
+        for j = i+1, deg do
            if coef[j][i]~=0 then
                coef[i], coef[j] = coef[j], coef[i]; break
            end
         end
       end
-      for j = 1,deg do 
+      for j = 1,deg do
         local d = coef[i][i];
         if j~=i then
            local e = coef[j][i]
            for k = 1, deg+1 do coef[j][k] = coef[j][k] - e*coef[i][k]/d end
-        else 
+        else
            for k = 1, deg+1 do coef[i][k] = coef[i][k]/d end
         end
       end
    end
 end
 
+local function solve_1(coef)
+   local a, b, c = coef[1][4], coef[2][4], coef[3][4]
+   coef[1][4], coef[2][4], coef[3][4] = c-b, a+b-c, c-a
+   return coef
+end
+
+local function solve_2(coef)
+   local a, b, c, d, e = coef[1][6], coef[2][6], coef[3][6], coef[4][6], coef[5][6]
+   coef[1][6], coef[2][6], coef[3][6], coef[4][6], coef[5][6]
+      = e-c, a+c-e, e-a-d, b+d-e, e-b
+   return coef
+end
+
+
 -- 実行回数 + ルビ中身 から uniq_id を作る関数
--- 未実装.これを使えば 2 回目以降の組版に 1 回目の情報が使える
 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
@@ -124,29 +141,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))
-      else 
-        return f
+local concat
+do
+   local node_prev = node.direct.getprev
+   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 node.direct.hpack(g)
+        else
+           return f
+        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
-   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
 
@@ -173,7 +198,7 @@ do
    enlarge = function (box, new_width, pre, middle, post, prenw, postnw)
       -- pre, middle, post: 伸縮比率
       -- prenw, postnw: 前後の自然長 (sp)
-      local h = getlist(box); 
+      local h = getlist(box);
       local hh, hd = getfield(box, 'height'), getfield(box, 'depth')
       local hx = h
       while hx do
@@ -184,47 +209,29 @@ do
            -- この 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,7 +265,7 @@ 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
@@ -301,17 +308,17 @@ local function enlarge_parent(r, p, ppre, pmid, ppost, mapre, mapost, intmode)
    local sumprot = rwidth - getfield(p, 'width') -- >0
    local pre_intrusion, post_intrusion
    if intmode == 0 then --  とりあえず組んでから決める
-      p = enlarge(p, rwidth, ppre, pmid, ppost, 0, 0) 
+      p = enlarge(p, rwidth, ppre, pmid, ppost, 0, 0)
       pre_intrusion  = min(mapre, round(ppre*getfield(p, 'glue_set')*65536))
       post_intrusion = min(mapost, round(ppost*getfield(p, 'glue_set')*65536))
    elseif intmode == 1 then
-      pre_intrusion = min(mapre, sumprot); 
+      pre_intrusion = min(mapre, sumprot);
       post_intrusion = min(mapost, max(sumprot-pre_intrusion, 0))
       p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion)
    elseif intmode == 2 then
-      post_intrusion = min(mapost, sumprot); 
+      post_intrusion = min(mapost, sumprot);
       pre_intrusion = min(mapre, max(sumprot-post_intrusion, 0))
-      p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion) 
+      p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion)
    else --  intmode == 3
       local n = min(mapre, mapost)*2
       if n < sumprot then
@@ -319,7 +326,7 @@ local function enlarge_parent(r, p, ppre, pmid, ppost, mapre, mapost, intmode)
       else
         pre_intrusion = floor(sumprot/2); post_intrusion = sumprot - pre_intrusion
       end
-      p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion) 
+      p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion)
       pre_intrusion = min(mapre, pre_intrusion + round(ppre*getfield(p, 'glue_set')*65536))
       post_intrusion = min(mapost, post_intrusion + round(ppost*getfield(p, 'glue_set')*65536))
    end
@@ -327,7 +334,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
@@ -335,7 +342,7 @@ end
 -- ルビボックスの生成(単一グループ)
 -- returned value: <new box>, <ruby width>, <post_intrusion>
 local max_margin
-local function new_ruby_box(r, p, ppre, pmid, ppost, 
+local function new_ruby_box(r, p, ppre, pmid, ppost,
                            mapre, mapost, imode, rgap)
    local post_intrusion = 0
    local intmode = imode%4
@@ -347,23 +354,23 @@ local function new_ruby_box(r, p, ppre, pmid, ppost,
    if getfield(r, 'width') > getfield(p, 'width') then  -- change the width of p
       r, p, post_intrusion  = enlarge_parent(r, p, ppre, pmid, ppost, mapre, mapost, intmode)
    elseif getfield(r, 'width') < getfield(p, 'width') then -- change the width of r
-      r = enlarge(r, getfield(p, 'width'), rpre, rmid, rpost, 0, 0) 
+      r = enlarge(r, getfield(p, 'width'), rpre, rmid, rpost, 0, 0)
       post_intrusion = 0
       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
@@ -372,7 +379,7 @@ local function new_ruby_box(r, p, ppre, pmid, ppost,
    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)
@@ -415,7 +422,7 @@ local function pre_low_cal_box(w, cmp)
       for j = 1, 2*i do coef[i][j] = 1 end
       for j = 2*i+1, 2*cmp+1 do coef[i][j] = 0 end
       kf[i], coef[i][2*cmp+2]
-        = new_ruby_box(node_copy(nta), node_copy(ntb), 
+        = new_ruby_box(node_copy(nta), node_copy(ntb),
                        rtb[6], rtb[5], rtb[4], max_allow_pre, 0, intmode, rgap)
    end
    node_free(nta); node_free(ntb)
@@ -428,7 +435,7 @@ local function pre_low_cal_box(w, cmp)
       for j = 2*i, 2*cmp+1 do coef[cmp+i][j] = 1 end
       nta = concat(node_copy(rb[i]), nta); ntb = concat(node_copy(pb[i]), ntb)
       kf[cmp+i], coef[cmp+i][2*cmp+2]
-        = new_ruby_box(node_copy(nta), node_copy(ntb), 
+        = new_ruby_box(node_copy(nta), node_copy(ntb),
                        rtb[9], rtb[8], rtb[7], 0, max_allow_post, intmode, rgap)
    end
 
@@ -437,57 +444,62 @@ local function pre_low_cal_box(w, cmp)
    coef[2*cmp+1] = {}
    for j = 1, 2*cmp+1 do coef[2*cmp+1][j] = 1 end
    kf[2*cmp+1], coef[2*cmp+1][2*cmp+2], post_intrusion_backup
-      = new_ruby_box(nta, ntb, rtb[3], rtb[2], rtb[1], 
+      = new_ruby_box(nta, ntb, rtb[3], rtb[2], rtb[1],
                      max_allow_pre, max_allow_post, intmode, rgap)
 
    -- 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
 
-   gauss(coef) -- 掃きだし法で連立方程式形 coef を解く
+   if cmp==1 then     solve_1(coef)
+   elseif cmp==2 then solve_2(coef)
+   else
+      gauss(coef) -- 掃きだし法で連立方程式形 coef を解く
+   end
    return coef
 end
 
-
-local function first_whatsit(n) -- n 以後で最初の whatsit
-   for h in Dnode.traverse_id(id_whatsit, n) do
-      return h
+local first_whatsit
+do
+   local traverse_id = node.direct.traverse_id
+   function first_whatsit(n) -- n 以後で最初の whatsit
+      for h in traverse_id(id_whatsit, n) do
+         return h
+      end
+      return nil
    end
-   return nil
 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
    for i = 1, cmp do
       -- rule
-      local nta = node_new(id_rule); 
+      local nta = node_new(id_rule);
       setfield(nta, 'width', coef[i*2][2*cmp+2])
       setfield(nta, 'height', ht); setfield(nta, 'depth', dp)
       setfield(nta, 'subtype', 0)
       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
 
@@ -498,15 +510,19 @@ local function pre_high(ahead)
    local n = first_whatsit(head)
    while n do
       if getsubtype(n) == sid_user and getfield(n, 'user_id') == RUBY_PRE then
-         local nv = getfield(n, 'value')
+        local nv = getfield(n, 'value')
          max_allow_pre = has_attr(nv, attr_ruby_maxprep) or 0
          local atr = has_attr(n, attr_ruby) or 0
-         if atr >0 then 
-            -- 直前のルビで intrusion がおこる可能性あり.
-            -- 前 run のデータが残っていればそれを使用,
-            -- そうでなければ行中形のデータを利用する
-            local op = old_break_info[atr] or post_intrusion_backup
-            max_allow_pre = max(0, max_allow_pre - op)
+         if max_allow_pre < 0 then
+            if atr>0 then
+               -- 直前のルビで intrusion がおこる可能性あり.
+               -- 前 run のデータが残っていればそれを使用,
+               -- そうでなければ行中形のデータを利用する
+               local op = old_break_info[atr] or post_intrusion_backup
+               max_allow_pre = max(0, -max_allow_pre - op)
+            else
+               max_allow_pre = -max_allow_pre
+            end
          end
          post_intrusion_backup = 0
          max_allow_post = has_attr(nv, attr_ruby_maxpostp) or 0
@@ -514,7 +530,7 @@ local function pre_high(ahead)
          local coef = pre_low_cal_box(n, getfield(nv, 'value'))
          local s = node_tail(nv) --ルビ文字
          head, n = pre_low_app_node(
-            head, n, getfield(nv, 'value'), coef, 
+            head, n, getfield(nv, 'value'), coef,
             getfield(s, 'height'), getfield(s, 'depth')
          )
       else
@@ -522,7 +538,7 @@ local function pre_high(ahead)
       end
    end
    return to_node(head)
-end 
+end
 luatexbase.add_to_callback('pre_linebreak_filter', pre_high, 'ltj.ruby.pre', 100)
 luatexbase.add_to_callback('hpack_filter', pre_high, 'ltj.ruby.pre', 100)
 
@@ -533,11 +549,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[' 
-                      .. tostring(id) .. ']=' .. num 
-                      .. '}}')
+      if id>0 and cache_handle then
+        cache_handle:write(
+                   'luatexja.ruby.old_break_info['
+                      .. tostring(id) .. ']=' .. num
+                      .. '\n')
       end
    end
 
@@ -547,35 +563,35 @@ do
       local hn = has_attr(rs[1], attr_ruby)
       local fn = has_attr(rs[#rs], attr_ruby)
       local wv = getfield(rw, 'value')
-      if hn==1 then 
+      if hn==1 then
         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
            local deg, hn = (fn-1)/2, wv
-           for i = 1, deg do hn = node_next(hn) end; 
+           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
       else
-        local deg, hn = max((hn-1)/2,2), wv 
+        local deg, hn = max((hn-1)/2,2), wv
         for i = 1, cmp+deg-1 do hn = node_next(hn) end
-        -- -1 is needed except the case hn = 3, 
+        -- -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))
         end
       end
-      for i = 1,#rs do 
+      for i = 1,#rs do
         local ri = rs[i]
         ch = node_remove(ch, ri); node_free(ri);
       end
@@ -585,100 +601,64 @@ do
    end
 end
 
--- local function is_zero_parfillskip(h,n)
---    if getid(n)==id_glue then
---       if getsubtype(n)==15 then
---      local ns = getfield(n, 'spec')
---      local n_width = getfield(ns, 'width')
---      if getfield(h, 'glue_sign')==1 
---         and getfield(h, 'glue_order') == getfield(ns, 'stretch_order') then
---            n_width = n_width 
---               + round(getfield(h, 'glue_set')*getfield(ns, 'stretch'))
---      elseif getfield(h, 'glue_sign')==2
---         and getfield(h, 'glue_order') == getfield(ns, 'shrink_order') then
---            n_width = n_width 
---               - round(getfield(h, 'glue_set')*getfield(n,s 'shrink'))
---      end
---      n = node_next(n) -- rightskip 未完
---      return (n_width <= 0)
---       else return false
---       end
---    else return false
---    end
--- end
-
 local function post_high_break(head)
-   local rs = {}   -- rs: sequence of ruby_nodes, 
+   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
         local hai = getid(ha)
-        local i = (((hai == id_glue and getsubtype(ha)==0) 
-                       or (hai == id_rule and getsubtype(ha)==0)
-                       or (hai == id_whatsit and getsubtype(ha)==sid_user 
-                              and getfield(ha, 'user_id')==RUBY_POST))
-                      and has_attr(ha, attr_ruby)) or 0
-        if i==1 then 
+        local i = ((hai == id_glue and getsubtype(ha)==0)
+                       or (hai == id_rule and getsubtype(ha)==0)
+                       or (hai == id_whatsit and getsubtype(ha)==sid_user
+                              and getfield(ha, 'user_id', RUBY_POST)))
+            and has_attr(ha, attr_ruby) or 0
+        if i==0 then
+            ha = node_next(ha)
+         elseif i==1 then
            setfield(h, 'head', post_lown(rs, rw, cmp, getlist(h)))
            for i = 2, #rs do rs[i] = nil end -- rs[1] is set by the next statement
            rs[1], rw = ha, nil; ha = node_next(ha)
-        elseif i==2*cmp+2 then
-           -- local par_not_end = true
-           -- local hn = node_next(ha)
-           -- if hn and getid(hn)==id_penalty and getfield(hn, 'penalty')==10000 then
-           --    local hm = node_next(hn)
-           --    if is_zero_parfillskip(h,hm) then
-           --       par_not_end = false
-           --    end
-           -- end
-           -- if par_not_end then
-              rs[#rs+1] = ha; ha = node_next(ha)
-           -- else
-           --   setfield(h, 'head', node_remove(getlist(h), ha)); break
-           -- end
-        elseif i>=3 then 
-           rs[#rs+1] = ha; ha = node_next(ha)
-        elseif i==2 then 
+        elseif i==2 then
            rw = ha
            cmp = getfield(getfield(rw, 'value'), 'value')
            local hb, hc =  node_remove(getlist(h), rw)
            setfield(h, 'head', hb); ha = hc
-        else
-           ha = node_next(ha)
+        else -- i>=3
+           rs[#rs+1] = ha; ha = node_next(ha)
         end
       end
       setfield(h, 'head', post_lown(rs, rw, cmp, getlist(h)))
    end
    return head
-end 
+end
 
 local function post_high_hbox(ahead)
    local ha = to_direct(ahead); local head = ha
-   local rs = {};  -- rs: sequence of ruby_nodes, 
+   local rs = {};  -- rs: sequence of ruby_nodes,
    local rw = nil; -- rw: main whatsit
    local cmp
    while ha do
       local hai = getid(ha)
-      local i = (((hai == id_glue and getsubtype(ha)==0) 
-                     or (hai == id_rule and getsubtype(ha)==0)
-                     or (hai == id_whatsit and getsubtype(ha)==sid_user 
-                            and getfield(ha, 'user_id', RUBY_POST)))
-                    and has_attr(ha, attr_ruby)) or 0
-      if i==1 then 
+      local i = ((hai == id_glue and getsubtype(ha)==0)
+                    or (hai == id_rule and getsubtype(ha)==0)
+                    or (hai == id_whatsit and getsubtype(ha)==sid_user
+                           and getfield(ha, 'user_id', RUBY_POST)))
+         and has_attr(ha, attr_ruby) or 0
+      if i==0 then
+         ha = node_next(ha)
+      elseif i==1 then
          head = post_lown(rs, rw, cmp, head)
          for i = 2, #rs do rs[i] = nil end -- rs[1] is set by the next statement
         rs[1], rw = ha, nil; ha = node_next(ha)
-      elseif i>=3 then 
-         rs[#rs+1] = ha; ha = node_next(ha)
-      elseif i==2 then 
+      elseif i==2 then
          rw = ha
         cmp = getfield(getfield(rw, 'value'), 'value')
         head, ha = node_remove(head, rw)
-      else
-         ha = node_next(ha)
+      else -- i >= 3
+         rs[#rs+1] = ha; ha = node_next(ha)
       end
    end
    return to_node(post_lown(rs, rw, cmp, head))
@@ -693,25 +673,39 @@ 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) 
-      if Np.nuc then return Np 
+   local function whatsit_callback(Np, lp, Nq, bsl)
+      if Np.nuc then return Np
       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))
          Np.last_char = luatexja.jfmglue.check_box_high(Np, getlist(x), nil)
-         if Nq.id ~=id_pbox_w and  type(Nq.char)=='number' then
-           -- Nq is a JAchar
-            if has_attr(lpv, attr_ruby_maxprep) < 0 then -- auto
-               local p = round((ltjs.table_current_stack[RIPRE + Nq.char] or 0)
-                                  *has_attr(lpv, attr_ruby))
-               if has_attr(lpv, attr_ruby_mode)%2 == 0 then -- intrusion 無効
-                  p = 0
+         if Nq.id ~=id_pbox_w then
+            if type(Nq.char)=='number' then
+               -- Nq is a JAchar
+               if has_attr(lpv, attr_ruby_maxprep) < 0 then -- auto
+                  local p = round((ltjs.table_current_stack[RIPRE + Nq.char] or 0)
+                                     *has_attr(lpv, attr_ruby))
+                  if has_attr(lpv, attr_ruby_mode)%2 == 0 then -- intrusion 無効
+                     p = 0
+                  end
+                  set_attr(lpv, attr_ruby_maxprep, -p)
+               end
+               if Nq.prev_ruby then
+                  set_attr(lp, attr_ruby, Nq.prev_ruby)
+               end
+            elseif has_attr(lpv, attr_ruby_maxprep) < 0 then -- auto
+               if Nq.char == 'parbdd' then
+                  local p = round((ltjs.table_current_stack[RIPRE-1] or 0)
+                                     *has_attr(lpv, attr_ruby))
+                  p = min(p, Nq.width)
+                 if has_attr(lpv, attr_ruby_mode)%2 == 0 then -- intrusion 無効
+                     p = 0
+                  end
+                  set_attr(lpv, attr_ruby_maxprep, p)
+               else
+                  set_attr(lpv, attr_ruby_maxprep, 0)
                end
-               set_attr(lpv, attr_ruby_maxprep, p)
-            end
-            if Nq.prev_ruby then
-               set_attr(lp, attr_ruby, Nq.prev_ruby)
             end
          elseif has_attr(lpv, attr_ruby_maxprep) < 0 then -- auto
             set_attr(lpv, attr_ruby_maxprep, 0)
@@ -725,8 +719,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
@@ -759,9 +760,6 @@ do
               if has_attr(nqnv, attr_ruby_mode)%4 >= 2 then
                  set_attr(nqnv, attr_ruby_maxprep, 0)
               end
-              --if Np and Np.id == id_pbox_w then
-               --  set_attr(nqnv, attr_ruby_maxprep, 0)
-              --end
            end
          end
         return true