OSDN Git Service

Ruby: use the aux file to record the width of overhang
authorHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Sun, 9 Feb 2014 14:07:55 +0000 (23:07 +0900)
committerHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Sun, 9 Feb 2014 14:07:55 +0000 (23:07 +0900)
src/addons/luatexja-ruby.sty
src/jfm-ujis.lua
src/ltj-ruby.lua
test/test14-ruby.pdf
test/test14-ruby.tex

index 511aa86..6df9a0b 100644 (file)
@@ -9,6 +9,7 @@
 
 %%------------------
 \newluatexattribute\ltj@rubyattr
+\ltj@rubyattr=0
 \RequireLuaTeXjaSubmodule{ruby}
 
 % ltjset/getparameter への追加設定
index f64080e..bf17237 100644 (file)
@@ -3,19 +3,20 @@
 -- based on upnmlminr-h.tfm (a metric in UTF/OTF package used by upTeX).
 
 -- JIS X 4051:2004 では,行末の句読点や中点はベタなのでそれに従う
--- kanjiskip: 0pt plus .25zw minus 0pt
+-- kanjiskip:    0pt plus .25zw minus 0pt
 -- xkanjiskip: .25zw plus .25zw (or .0833zw) minus .125zw
 
 
 luatexja.jfont.define_jfm {
    dir = 'yoko',
    zw = 1.0, zh = 1.0,
-   kanjiskip =  {0.0, 0.05, 0},
+   kanjiskip =  {0.0, 0.25, 0},
    xkanjiskip = {0.25, 0.25, .125},
    [0] = {
       align = 'left', left = 0.0, down = 0.0,
       width = 1.0, height = 0.88, depth = 0.12, italic=0.0,
       glue = {
+        -- [index] = { width, stretch, shrink, [priority, [ratio]] }
         [1] = { 0.5 , 0.0, 0.5,  0, 1 },
         [2] = {0, 0, 0},
         [3] = { 0.25, 0.0, 0.25, 1, 1 },
@@ -243,4 +244,4 @@ luatexja.jfont.define_jfm {
       }
    },
 
-}
\ No newline at end of file
+}
index 5806472..d0f8115 100644 (file)
@@ -38,6 +38,7 @@ local id_rule = node.id('rule')
 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
@@ -51,22 +52,20 @@ local attr_ruby_maxpostp = luatexbase.attributes['ltj@kcat0']
 local attr_ruby_maxmargin = luatexbase.attributes['ltj@kcat1']
 local attr_ruby_stretch = luatexbase.attributes['ltj@kcat2']
 local attr_ruby_mode = luatexbase.attributes['ltj@kcat3']
+local attr_ruby_id = luatexbase.attributes['ltj@kcat4'] -- uniq id
 local attr_ruby = luatexbase.attributes['ltj@rubyattr']
--- ルビ内部処理用
--- jfmglue 中では「2つ前のクラスタもルビ」のフラグ(true = 1)
--- (whatsit).value node ではルビ全角の値(sp単位)
--- 行分割前後では,「何番目のルビ関連ノード」か
+-- ルビ内部処理用,以下のようにノードによって使われ方が異なる
+-- * (whatsit) では JAglue 処理時に,
+--     「2つ前のクラスタもルビ」 ==> そのルビクラスタの id
+--   otherwise ==> unset
+-- * (whatsit).value node ではルビ全角の値(sp単位)
+-- * 行分割で whatsit の前後に並ぶノードでは,「何番目のルビ関連ノード」か
+-- * (whatsit).value に続く整形済み vbox たちでは post_intrusion の値
+local cat_lp = luatexbase.catcodetables['latex-package']
 
 local round, floor = tex.round, math.floor
 local min, max = math.min, math.max
 
-local FROM_JFM       = luatexja.icflag_table.FROM_JFM
-local PROCESSED      = luatexja.icflag_table.PROCESSED
-local KANJI_SKIP     = luatexja.icflag_table.KANJI_SKIP
-local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
-local XKANJI_SKIP    = luatexja.icflag_table.XKANJI_SKIP
-local XKANJI_SKIP_JFM= luatexja.icflag_table.XKANJI_SKIP_JFM
-
 luatexja.userid_table.RUBY_PRE = luatexbase.newuserwhatsitid('ruby_pre',  'luatexja')
 luatexja.userid_table.RUBY_POST = luatexbase.newuserwhatsitid('ruby_post',  'luatexja')
 local RUBY_PRE  = luatexja.userid_table.RUBY_PRE
@@ -102,7 +101,7 @@ local function gauss(coef)
         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
@@ -111,7 +110,15 @@ end
 
 -- 実行回数 + ルビ中身 から uniq_id を作る関数
 -- 未実装.これを使えば 2 回目以降の組版に 1 回目の情報が使える
-
+old_break_info = {} -- public, 前 run 時の分割情報
+local make_uniq_id
+do
+   local exec_count = 0
+   make_uniq_id = function (w)
+      exec_count = exec_count + 1
+      return exec_count
+   end
+end
 
 -- concatenation of boxes: reusing nodes
 -- ルビ組版が行われている段落/hboxでの設定が使われる.
@@ -154,71 +161,77 @@ end
 ----------------------------------------------------------------
 
 -- box の中身のノードは再利用される
-local function enlarge(box, new_width, pre, middle, post, prenw, postnw)
-   -- pre, middle, post: 伸縮比率
-   -- prenw, postnw: 前後の自然長 (sp)
-   local h = getlist(box); local hh, hd = getfield(box, 'height'), getfield(box, 'depth')
-   local hx = h
-   while hx do
-      if has_attr(hx, attr_icflag) == KANJI_SKIP
-         or has_attr(hx, attr_icflag) == KANJI_SKIP_JFM
-         or has_attr(hx, attr_icflag) == XKANJI_SKIP
-         or has_attr(hx, attr_icflag) == XKANJI_SKIP_JFM
-         or has_attr(hx, attr_icflag) == 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)
-               h = insert_after(h, hx, k);
-               h = node_remove(h, hx); node_free(hx); hx = k
-         else -- glue
-            local ks = node_copy(getfield(hx, '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)
-         end
+local enlarge
+do
+   local FROM_JFM       = luatexja.icflag_table.FROM_JFM
+   local PROCESSED      = luatexja.icflag_table.PROCESSED
+   local KANJI_SKIP     = luatexja.icflag_table.KANJI_SKIP
+   local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
+   local XKANJI_SKIP    = luatexja.icflag_table.XKANJI_SKIP
+   local XKANJI_SKIP_JFM= luatexja.icflag_table.XKANJI_SKIP_JFM
+   enlarge = function (box, new_width, pre, middle, post, prenw, postnw)
+      -- pre, middle, post: 伸縮比率
+      -- prenw, postnw: 前後の自然長 (sp)
+      local h = getlist(box); 
+      local hh, hd = getfield(box, 'height'), getfield(box, 'depth')
+      local hx = h
+      while hx do
+        if has_attr(hx, attr_icflag) == KANJI_SKIP
+           or has_attr(hx, attr_icflag) == KANJI_SKIP_JFM
+           or has_attr(hx, attr_icflag) == XKANJI_SKIP
+           or has_attr(hx, attr_icflag) == XKANJI_SKIP_JFM
+           or has_attr(hx, attr_icflag) == 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)
+                 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)
+              end
+        end
+        hx = node_next(hx)
       end
-      hx = node_next(hx)
+      -- 先頭の空白を挿入
+      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)
+      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)
+      insert_after(h, node_tail(h), k);
+      -- hpack
+      setfield(box, 'head', nil); node_free(box)
+      box = Dnode.hpack(h, new_width, 'exactly')
+      setfield(box, 'height', hh)
+      setfield(box, 'depth', hd)
+      return box
    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)
-   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)
-   insert_after(h, node_tail(h), k);
-   -- hpack
-   setfield(box, 'head', nil); node_free(box)
-   box = Dnode.hpack(h, new_width, 'exactly')
-   setfield(box, 'height', hh)
-   setfield(box, 'depth', hd)
-   return box
 end
 
+
 ----------------------------------------------------------------
 -- TeX interface
 ----------------------------------------------------------------
@@ -244,14 +257,14 @@ 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)
+   Dnode.write(w); return w,wv
 end
 
 -- rst: table
 function texiface(rst, rtlr, rtlp)
    if #rtlr ~= #rtlp then
-      for i,v in pairs(rtlr) do node_free(v) end
-      for i,v in pairs(rtlp) do node_free(v) end
+      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',
                                  'Group count mismatch between the ruby and\n' ..
                                     'the body (' .. #rtlr .. ' != ' .. #rtlp .. ').',
@@ -259,7 +272,9 @@ function texiface(rst, rtlr, rtlp)
    else
       local f = true
       for i = 1,#rtlr do
-        if getfield(rtlr[i], 'width') > getfield(rtlp[i], 'width') then f = false; break end
+        if getfield(rtlr[i], 'width') > getfield(rtlp[i], 'width') then
+           f = false; break
+        end
       end
       if f then -- モノルビ * n
         local r,p = {true}, {true}
@@ -267,12 +282,13 @@ function texiface(rst, rtlr, rtlp)
            r[1] = rtlr[i]; p[1] = rtlp[i]; texiface_low(rst, r, p)
         end
       else
-        texiface_low(rst, rtlr, rtlp)
+        local w, wv = texiface_low(rst, rtlr, rtlp)
+        local id = make_uniq_id(w)
+        set_attr(wv, attr_ruby_id, id)
       end
    end
 end
 
-
 ----------------------------------------------------------------
 -- pre_line_break
 ----------------------------------------------------------------
@@ -351,15 +367,14 @@ local function new_ruby_box(r, p, ppre, pmid, ppost,
    insert_after(r, r, a); insert_after(r, a, p)
    setfield(p, 'next', nil)
    a = Dnode.vpack(r)
-   setfield(a, 'height', getfield(p, 'height'))
-   setfield(a, 'depth', getfield(p, 'depth'))
-   setfield(a, 'shift', -(getfield(r, 'height')+getfield(r, 'depth')))
+   setfield(a, 'shift', 0)
+   set_attr(a, attr_ruby, post_intrusion)
    return a, getfield(r, 'width'), post_intrusion
 end
 
 
 -- High-level routine in pre_linebreak_filter
-local max_post_intrusion_backup
+local post_intrusion_backup
 local max_allow_pre, max_allow_post
 
 
@@ -395,28 +410,26 @@ local function pre_low_cal_box(w, cmp)
    node_free(nta); node_free(ntb)
 
    -- 行頭形の計算
-   nta = nil; ntb = nil
+   local nta, ntb = nil, nil
    for i = cmp,1,-1 do
       coef[cmp+i] = {}
       for j = 1, 2*i-1 do coef[cmp+i][j] = 0 end
       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], mdt
+      kf[cmp+i], coef[cmp+i][2*cmp+2]
         = new_ruby_box(node_copy(nta), node_copy(ntb), 
                        rtb[9], rtb[8], rtb[7], rtc[10], rtc[9], rtc[8], 
                        0, max_allow_post, intmode)
-      if max_post_intrusion_backup < mdt then max_post_intrusion_backup = mdt end
    end
 
    -- ここで,nta, ntb には全 container を連結した box が入っているので
    -- それを使って行中形を計算する.
    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], mdt
+   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], rtc[10], rtc[9], rtc[8], 
                     max_allow_pre, max_allow_post, intmode)
-   if max_post_intrusion_backup < mdt then max_post_intrusion_backup = mdt end
 
    -- w.value の node list 更新.
    local nt = wv
@@ -464,18 +477,22 @@ end
 local function pre_high(ahead)
    if not ahead then return ahead end
    local head = to_direct(ahead)
-   max_post_intrusion_backup = 0
+   post_intrusion_backup = 0
    local n = head
    while n do
       if getid(n) == id_whatsit then
          if getsubtype(n) == sid_user and getfield(n, 'user_id') == RUBY_PRE then
             local nv = getfield(n, 'value')
             max_allow_pre = has_attr(nv, attr_ruby_maxprep) or 0
-           if has_attr(n, attr_ruby) == 1 then 
-              -- 直前のルビで intrusion がおこる可能性あり.安全策をとる.
-              max_allow_pre = max(0, max_allow_pre - max_post_intrusion_backup)
+           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)
            end
-           max_post_intrusion_backup = 0
+           post_intrusion_backup = 0
             max_allow_post = has_attr(nv, attr_ruby_maxpostp) or 0
             max_margin = has_attr(nv, attr_ruby_maxmargin) or 0
            local coef = pre_low_cal_box(n, getfield(nv, 'value'))
@@ -499,89 +516,137 @@ luatexbase.add_to_callback('hpack_filter', pre_high, 'ltj.ruby.pre', 100)
 ----------------------------------------------------------------
 -- post_line_break
 ----------------------------------------------------------------
+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 
+                      .. '}}')
+      end
+   end
 
-local function post_lown(rs, rw, ch)
--- ch: the head of `current' hlist
-   if #rs ==0 or not rw then return ch end
-   local hn = has_attr(rs[1], attr_ruby)
-   local fn = has_attr(rs[#rs], attr_ruby)
-   local cmp = getfield(getfield(rw, 'value'), 'value')
-   if hn==1 then 
-      if fn==2*cmp+2 then
-        hn = node_tail(getfield(rw, 'value'))
-         node_remove(getfield(rw, 'value'), hn)
-        insert_after(ch, rs[#rs], hn)
-         set_attr(hn, attr_icflag,  PROCESSED)
+   post_lown = function (rs, rw, cmp, ch)
+      -- ch: the head of `current' hlist
+      if #rs ==0 or not rw then return ch end
+      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 fn==2*cmp+2 then
+           local hn = node_tail(wv)
+           node_remove(wv, hn)
+           insert_after(ch, rs[#rs], 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; 
+           node_remove(wv, hn)
+           setfield(hn, 'next', nil)
+           insert_after(ch, rs[#rs], hn)
+           set_attr(hn, attr_icflag,  PROCESSED)
+           write_aux(wv, has_attr(hn, attr_ruby))
+        end
       else
-        local deg = (fn-1)/2
-         hn = getfield(rw, 'value')
-         for i = 1, deg do hn = node_next(hn) end; 
-        node_remove(getfield(rw, 'value'), hn)
-         setfield(hn, 'next', nil)
-         insert_after(ch, rs[#rs], hn)
-         set_attr(hn, attr_icflag,  PROCESSED)
-     end
-   else
-      local deg = (hn-1)/2; 
-      if deg == 1 then deg = 2 end
-      hn = getfield(rw, 'value'); for i = 1, cmp+deg-1 do hn = node_next(hn) end
-      -- -1 is needed except the case hn = 3, 
-      --   because a ending-line form is removed already from the list
-      node_remove(getfield(rw, 'value'), hn); setfield(hn, 'next', nil)
-      insert_after(ch, rs[#rs], hn)
-      set_attr(hn, attr_icflag,  PROCESSED)
-   end
-   for i = 1,#rs do 
-      ch = node_remove(ch, rs[i]); node_free(rs[i]) 
-   end
-   -- cleanup
-   if fn >= 2*cmp+1 then
-      --setfield(hn, 'next', nil); 
-      node_free(rw); 
+        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, 
+        --   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)
+        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 
+        local ri = rs[i]
+        ch = node_remove(ch, ri); node_free(ri);
+      end
+      -- cleanup
+      if fn >= 2*cmp+1 then node_free(rw) end
+      return ch;
    end
-   return ch;
 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 h = to_direct(head); 
-   local rs = {};  -- rs: sequence of ruby_nodes, 
-   local rw = nil; -- rw: main whatsit
-   while h do 
-      if getid(h) == id_hlist then
-         local ha = getlist(h); rs = {}
-        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 
-               setfield(h, 'head', post_lown(rs, rw, getlist(h))); rs = {}; rw = nil
-               rs[1] = ha; ha = node_next(ha)
-            elseif i>=3 then 
-               rs[#rs+1] = ha; ha = node_next(ha)
-            elseif i==2 then 
-               rw = ha
-               local hb, hc =  node_remove(getlist(h), rw)
-               setfield(h, 'head', hb); ha = hc
-            else
-               ha = node_next(ha)
-            end
-         end
-         setfield(h, 'head', post_lown(rs, rw, getlist(h)))
+   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 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 
+           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 = hn
+           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 
+           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)
+        end
       end
-      h = node_next(h)
+      setfield(h, 'head', post_lown(rs, rw, cmp, getlist(h)))
    end
-
-   return to_node(head)
+   return head
 end 
 
 local function post_high_hbox(ahead)
    local ha = to_direct(ahead); local head = ha
    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) 
@@ -590,17 +655,20 @@ local function post_high_hbox(ahead)
                             and getfield(ha, 'user_id', RUBY_POST)))
                     and has_attr(ha, attr_ruby)) or 0
       if i==1 then 
-         head = post_lown(rs, rw, head); rs = {}; rw = nil
-         table.insert(rs, ha); ha = node_next(ha)
+         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 
-         table.insert(rs, ha); ha = node_next(ha)
+         rs[#rs+1] = ha; ha = node_next(ha)
       elseif i==2 then 
-         rw = ha; head, ha = node_remove(head, rw)
+         rw = ha
+        cmp = getfield(getfield(rw, 'value'), 'value')
+        head, ha = node_remove(head, rw)
       else
          ha = node_next(ha)
       end
    end
-   return to_node(post_lown(rs, rw, head))
+   return to_node(post_lown(rs, rw, cmp, head))
 end
 
 luatexbase.add_to_callback('post_linebreak_filter', post_high_break, 'ltj.ruby.post_break', 100)
@@ -620,6 +688,7 @@ do
          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))
@@ -628,11 +697,11 @@ do
                end
                set_attr(lpv, attr_ruby_maxprep, p)
             end
-            if Nq.prev_ruby then 
-               set_attr(lp, attr_ruby, 1)
+            if Nq.prev_ruby then
+               set_attr(lp, attr_ruby, Nq.prev_ruby)
             end
-         else
-            set_attr(getfield(lp, 'value'), attr_ruby_maxprep, 0)
+         elseif has_attr(lpv, attr_ruby_maxprep) < 0 then -- auto
+            set_attr(lpv, attr_ruby_maxprep, 0)
          end
          return Np
       end
@@ -651,13 +720,15 @@ do
          Nq.last_char = luatexja.jfmglue.check_box_high(Nq, getlist(x), nil)
          luatexja.jfmglue.after_hlist(Nq)
          if Np and Np.id ~=id_pbox_w and type(Np.char)=='number' then
+           -- Np is a JAchar
+           local rm = has_attr(nqnv, attr_ruby_mode)
             if has_attr(nqnv, attr_ruby_maxpostp) < 0 then -- auto
                local p = round((ltjs.table_current_stack[RIPOST + Np.char] or 0)
                                   *has_attr(nqnv, attr_ruby))
-               if has_attr(nqnv, attr_ruby_mode)%2 == 0 then -- intrusion 無効
+               if rm%2 == 0 then -- intrusion 無効
                   p = 0
                end
-               if has_attr(nqnv, attr_ruby_mode)%4 >= 2 then
+               if rm%4 >= 2 then
                   local q = has_attr(nqnv, attr_ruby_maxprep)
                   if q < p then p = q
                   elseif q > p then
@@ -666,16 +737,19 @@ do
                end
                set_attr(nqnv, attr_ruby_maxpostp, p)
             end
-            Np.prev_ruby = true -- 前のクラスタがルビであったことのフラグ
-         else -- 直前が文字以外なら intrusion なし
+            Np.prev_ruby = has_attr(getfield(Nq.nuc, 'value'), attr_ruby_id)
+           -- 前のクラスタがルビであったことのフラグ
+         else -- 直前が文字以外
            local nqnv = getfield(Nq.nuc, 'value')
-            set_attr(nqnv, attr_ruby_maxpostp, 0)
-           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(getfield(Np.nuc, 'value'), attr_ruby_maxprep, 0)
-            end
+            if has_attr(nqnv, attr_ruby_maxpostp) < 0 then -- auto
+              set_attr(nqnv, attr_ruby_maxpostp, 0)
+              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
       else
@@ -686,4 +760,3 @@ do
                               "luatexja.ruby.np_info_after", 1)
 end
 
-
index ad1b21b..f877e99 100644 (file)
Binary files a/test/test14-ruby.pdf and b/test/test14-ruby.pdf differ
index d7d7b1f..cf76953 100644 (file)
@@ -4,9 +4,9 @@
 \usepackage{fontspec,luacode,luatexja-otf,amsmath}
 \usepackage{unicode-math}
 \setmathfont{XITS Math}
-\setmainfont{TeX Gyre Termes}
-\setsansfont{TeX Gyre Heros}
-\setmonofont{LMMono10-Regular}
+\setmainfont[Ligatures=TeX]{TeX Gyre Termes}
+\setsansfont[Ligatures=TeX]{TeX Gyre Heros}
+\setmonofont[Ligatures=TeX]{LMMono10-Regular}
 \usepackage{luatexja-ruby,showexpl,booktabs}
 \lstset{preset=\huge,basicstyle=\ttfamily}
 \fboxsep=0mm
 
 \title{ルビ実装のサンプル}
 \begin{document}
-%\fontsize{13.19873}{10}\selectfont%
-%
-%を\hbox{\ruby{凝}{ぎょう}}あ\ruby{視}{し}する
-%\end{document}
+%\fontsize{13.19873}{15}\selectfont%
+
 \maketitle
 
 \section{使い方}
 \begin{itemize}
-\item パッケージ読み込み:\verb+\usepackage{luatexja-ruby}+のみ.\LaTeXe のみサポート.
+\item パッケージ読み込み:\verb+\usepackage{luatexja-ruby}+ のみ.\LaTeXe のみサポート.
 \item 命令:
 \begin{itemize}
 \item \verb+\ltjruby[option]{親|文|字}{おや|も|じ}+
 \end{itemize}
 \item \verb|\ltjsetparameter|に追加のキー
 \begin{itemize}
-\item \verb+rubypreintrusion={char_code, pre_int}+:全角単位
-\item \verb+rubypostintrusion={char_code, post_int}+:全角単位
+\item \verb+rubypreintrusion={<char_code>, <pre_int>}+:ルビ全角単位.
+\verb+<char_code>+ の文字に掛けられる前のルビ文字列の最大長.
+\item \verb+rubypostintrusion={<char_code>, <post_int>}+:ルビ全角単位.
 \end{itemize}
-\item \verb|\ltjsetruby{option}|他でサポートするオプション:後ろに指定したものが優先
+\item \verb|\ltjsetruby{option}| 他でサポートするオプション:後ろに指定したものが優先
 \begin{itemize}
 \item \verb+mode+:進入処理のモードを表すbit vector.意味はluatexja-ruby.sty参照.
 \item \verb+intrusionpre=<real>+:前進入量指定.ルビ全角単位で指定.
 \item \verb+rubysize=<real>+:ルビ文字の親文字に対する大きさ.
 \end{itemize}
 \end{itemize}
+\section{TODO}
+\begin{itemize}
+\item 熟語ルビの配置が「モノルビの連続」「グループルビ」のどちらかしかない(行分割は可能).
+「一字だけかけても良い」なんてどうやるんだろう.
+\item ルビ文字のはみ出しが繋がらないようにする処理(図3.82)には注意.
+例えば,
+\begin{center}\Large
+\ltjsetparameter{rubypreintrusion={`◆,1}, 
+  rubypostintrusion={`◆,1}}
+\ruby{陵}{りよう}◆\ruby{陵}{みささぎ}
+\end{center}
+において,後者の「\ruby{陵}{みささぎ}」のルビが前の「◆」にかかる量は次のように決まる:
+\begin{enumerate}
+\item 1回目の実行では,行分割前に「\ruby{陵}{りよう}」の後側進入量は前もって知ることはでき
+      ない.なので,「\ruby{陵}{りよう}」は行中形で組まれるものとして
+「\ruby{陵}{みささぎ}」前側進入許容量は
+\[
+ \underbrace{0.5\,\mathrm{zw}}_{\text{元々の許容量}}
+-\underbrace{0.25\,\mathrm{zw}}_{\text{前のルビの後側進入量}}=0.25\,\mathrm{zw}
+\]
+となる.なお,行分割後,「\ruby{陵}{りよう}」が実際に組まれた時に使われた後側進入量は
+auxファイルに記述される.
+\item 2回目以降の実行では,auxファイルに保存された「\ruby{陵}{りよう}」の後側進入量
+を用いて,「\ruby{陵}{みささぎ}」前側進入許容量を計算する.
+\end{enumerate}
+なお,auxファイルに保存する際,各 \verb+\ruby+ 命令の呼び出しを識別するキーが必要になるが,
+そのキーとしては単純に「何個目の \verb+\ruby+ 命令か」である.
+
+\item 実装方法の都合上,ルビの直前・直後・途中で2箇所以上の改行が起きる場合
+(以下のパーツの組み方が出てくる)に対応できない.
+\begin{center}\small
+\begin{tabular}{ll}
+\toprule
+\multicolumn{1}{c}{\sf 組み方}&\multicolumn{1}{c}{\sf サンプル}\\
+\midrule
+単独1&
+\huge
+\vrule{\color{blue}\gt\ruby[]{流}{りゆう}}\vrule\\
+単独2&
+\huge
+\vrule{\color{blue}\gt\ruby[]{暢}{ちよう}}\vrule\\
+単独$(1+2)$&
+\huge
+\vrule{\color{blue}\gt\ruby[stretch=010]{流|暢}{りゆう|ちよう}}\vrule\\
+\bottomrule
+\end{tabular}
+\end{center}
+
+\item 段落最終行の \verb+\parfillskip+ が(伸縮の結果)0\,pt以下であり,かつ
+その直前がルビだった場合,該当のルビは行末形になる.
+\end{itemize}
 
 \section{実装の大まかな方法}
 次の例で説明する.
 現在の水平リストへと挿入する(必要ならば\verb|\leavevmode|も実行):
 \[
  \Node{whatsit $w$}{value: 2}\longrightarrow 
- \Node{hlist $r_1$}{「りゆう」}\longrightarrow
+ \Node{hlist $s_1$}{「りゆう」}\longrightarrow
  \Node{hlist $p_1$}{「流」}\longrightarrow
- \Node{hlist $r_2$}{「ちよう」}\longrightarrow
+ \Node{hlist $s_2$}{「ちよう」}\longrightarrow
  \Node{hlist $p_2$}{「暢」}
 \]
 ここで,最初の$w$の値2は,ルビが2つのパーツ「\ruby{流}{りゆう}」「\ruby{暢}{ちよう}」からなっていることを
-表している.$r_i$達の中の文字は既にルビの大きさである.この値を$\mathit{cmp}$とおこう.
-\item \LuaTeX-jaの和文処理グルー挿入処理においては,「先頭が『流』,最後が『暢』であるようなhboxを\verb|\unhbox|で展開したもの」と扱われる.
+表している.この値を$\mathit{cmp}$とおこう.
+$s_i$達の中の文字は既にルビの大きさである.
+\item \LuaTeX-jaの和文処理グルー挿入処理において,
+whatsit~$W$は,
+「先頭が『流』,最後が『暢』であるようなhboxを\verb|\unhbox|で展開したもの」と扱われる.
 言い換えれば,ルビ部分を無視した単なる「流暢」という和文文字の並びとして扱われる\footnote{「流」「暢」の間のグルーは既に入っている,と扱われる.}.
 次のサンプルを参照
 \begin{LTXexample}
@@ -114,8 +167,8 @@ $W$の前後に$2\mathit{cmp}+1=5$個のノードが挿入される.
 のようになったとしたら,「\ruby{流}{りゆう}」「\ruby{暢}{ちよう}」の間で行分割が起きた,ということがわかり,
 $g_i$,~$r_i$達のノードを適切に置き換えればよい(後で詳しく説明する).
 
-\item ã\81ªã\81\8aï¼\8c$r_i$é\81\94ã\81®é«\98ã\81\95ã\83»æ·±ã\81\95ã\81¯çµ\84ã\81¿ä¸\8aã\81\8cã\81£ã\81\9få¾\8cã\81®ã\81\9dã\82\8cã\81§ã\81\82ã\82\8aï¼\8cç\8f¾ç\8a¶ã\81§ã\81¯è¦ªæ\96\87å­\97é\83¨å\88\86ã\81®é«\98ã\81\95ã\83»æ·±ã\81\95ã\81¨ä¸\80è\87´ã\81\99ã\82\8bï¼\8e
-$g_i$,~$r_i$達の幅は,以下の対応に沿って計算される.
+\item なお,$r_i$達の高さ・深さは組み上がった後のそれである.
+$g_i$,~$r_i$達の幅は,以下の対応に沿って算出する.
 
 \begin{center}\small
 \begin{tabular}{cllll}
@@ -153,14 +206,15 @@ $g_0\rightarrow W\rightarrow r_1\rightarrow g_2 \rightarrow r_2\rightarrow g_2$\
 \end{tabular}
 \end{center}
 
+\medskip
 
-この5元連立方程式を解いて$g_i$,~$r_i$達の幅を求める.
-なお,進入長は予め計算しておく.例えばこの場合,
+例えばこの場合,$n_5$に対して
 \[
  g_0+r_1+g_2+r_2+g_2 = 3\,\mathrm{zw}-(0.25\,\mathrm{zw}\times 2)=2.5\,\mathrm{zw}
 \]
-である(zwは親文字全角の幅).
-
+という方程式が立つ(zwは親文字全角の幅,進入量込).
+$n_1$から$n_5$まで計5本の方程式が立つが,これらはGau\ss の消去法で解くことができて
+$g_i$,~$r_i$達の幅が求まる.
 \item また,ルビ処理を統括しているwhatsit~$W$の値も
 \[
  \Node{whatsit $w$}{value: 2}\longrightarrow 
@@ -213,52 +267,6 @@ $r_2$,~$g_2$を行Bから除去・メモリ解放し,代わりに$n_3$を行B
 \end{enumerate}
 \end{enumerate}
 
-\section{TODO}
-\begin{itemize}
-\item 熟語ルビの配置が「モノルビの連続」「グループルビ」のどちらかしかない(行分割は可能).
-「一字だけかけても良い」なんてどうやるんだろう.
-\item 図3.82のように,ルビ文字のはみ出しが繋がらないようにする処理が不安定である.
-例えば,
-\begin{center}\Large
-\ruby{陵}{りよう}と\ruby{陵}{みささぎ}
-\end{center}
-において,後者の「\ruby{陵}{みささぎ}」のルビは前の「と」にかかっていない.
-これは次の理由によるものである:
-\begin{enumerate}
-\item ルビのところで行分割を正しく実装するためには,行分割前に行頭・行中・行末形の幅を
-決定しておかねばならない.
-\item 「\ruby{陵}{りよう}」の後側進入量は,行頭形のとき(ルビ全角)が最大となる.
-\item 「\ruby{陵}{みささぎ}」のルビ文字のはみ出しが重ならないように,前側進入許容量は
-\[
- \underbrace{0.5\,\mathrm{zw}}_{\text{元々の許容量}}
--\underbrace{0.5\,\mathrm{zw}}_{\text{前のルビの後側進入量の最大値}}=0
-\]
-となる.そのため,「\ruby{陵}{みささぎ}」のルビ文字は「と」にはかからない.
-\end{enumerate}
-1パスでは厳しいだろうが,2パスでなら
-全てのルビにおいて「どの形で出力されたか」をauxに保存することでは改善されると思う.
-しかし,どのように保存するか
-(具体的には,各\verb|\ruby|命令の呼びだしをどういうキーに変換するか)が問題だ.
-\item 実装方法の都合上,ルビの直前・直後・途中で2箇所以上の改行が起きる場合(以下のパーツの組み方が出てくる)に対応できない.
-\begin{center}\small
-\begin{tabular}{ll}
-\toprule
-\multicolumn{1}{c}{\sf 組み方}&\multicolumn{1}{c}{\sf サンプル}\\
-\midrule
-単独1&
-\huge
-\vrule{\color{blue}\gt\ruby[]{流}{りゆう}}\vrule\\
-単独2&
-\huge
-\vrule{\color{blue}\gt\ruby[]{暢}{ちよう}}\vrule\\
-単独$(1+2)$&
-\huge
-\vrule{\color{blue}\gt\ruby[stretch=010]{流|暢}{りゆう|ちよう}}\vrule\\
-\bottomrule
-\end{tabular}
-\end{center}
-
-\end{itemize}
 
 \paragraph{進入(intrusion)と突出(protrusion)}\ 
 
@@ -441,7 +449,8 @@ mode 11xx: {\setkeys[ltj]{ruby}{mode=13}%
 3.76× の\ruby{流}{りゆう}\ruby{儀}{ぎ}を\quad の\ruby{無}{む}\ruby{常}{じよう}を\quad%
 
 要調整 3.77\ {\notalign%この図では揃えない
-\hbox{\vrule\vbox{\hsize=5\zw ……の\ruby{流|儀}{りゆう|ぎ}を}\vrule}\quad%
+\hbox{\vrule\vbox{\hsize=10\zw あああああああの%
+  \ruby{流|儀}{りゆう|ぎ}がある.}\vrule}\quad%
 % ↑どうすれば改行されるのだろう
 \hbox{\vrule\vbox{\hsize=5\zw ……の\ruby{無|常}{む|じよう}を}\vrule}}
 
@@ -476,15 +485,15 @@ mode 11xx: {\setkeys[ltj]{ruby}{mode=13}%
 要調整3.85\ {\notalign%この図では揃えない
 \hbox{\vrule\vbox{\hsize=15\zw% なぜ行末形にならない!
 あああああああああああああの\ruby{徑}{こみち}\penalty-1000をあああああああ%
\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\84ã\81®\ruby{å¾\91}{ã\81\93ã\81¿ã\81¡}}\vrule}}
\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\82ã\81\84ã\81®\ruby{å¾\91}{ã\81\93ã\81¿ã\81¡}ã\81\82ã\81\82}\vrule}}
 
 3.86\ %
 \hbox{\vrule\vbox{\hsize=15\zw
 ああああああああああああの\ruby{徑}{こみち}をあああああああ%
 あああああああ\ruby{徑}{こみち}を}\vrule}
 
-要調整3.87\ %
-\hbox{\vrule\vbox{\hsize=15\zw% なぜ行末形にならない!
+3.87\ %
+\hbox{\vrule\vbox{\hsize=15\zw%
 ああああああああああああの\ruby{飾り}{アクセサリー}等あああああああ%
 あああああああああああああああ共\ruby{飾り}{アクセサリー}}\vrule}
 
@@ -518,4 +527,5 @@ F.04 \ruby{未|熟}{み|じゆく}\quad\ruby{法|華|経}{ほ|け|きよう}\qua
 F.05 の\ruby{峻|別}{しゆん|べつ}は
 
 F.06以降は未チェック
+
 \end{document}