OSDN Git Service

Merge branch 'kmaeda_fontspec' into kitagawa_test
[luatex-ja/luatexja.git] / src / ltj-ruby.lua
index d0f8115..a84256c 100644 (file)
@@ -3,7 +3,7 @@
 --
 luatexbase.provides_module({
   name = 'luatexja.ruby',
-  date = '2014/02/06',
+  date = '2014/04/02',
   description = 'Ruby',
 })
 module('luatexja.ruby', package.seeall)
@@ -53,6 +53,7 @@ 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_intergap = luatexbase.attributes['ltj@kcat5']
 local attr_ruby = luatexbase.attributes['ltj@rubyattr']
 -- ルビ内部処理用,以下のようにノードによって使われ方が異なる
 -- * (whatsit) では JAglue 処理時に,
@@ -108,8 +109,21 @@ local function gauss(coef)
    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 make_uniq_id
 do
@@ -176,11 +190,10 @@ do
       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
+         local hic = has_attr(hx, attr_icflag)
+        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
            -- この 5 種類の空白をのばす
               if getid(hx) == id_kern then
                  local k = node_new(id_glue)
@@ -247,8 +260,9 @@ local function texiface_low(rst, rtlr, rtlp)
    setfield(wv, 'value', floor(#rtlr))
    set_attr(wv, attr_ruby, rst.rubyzw)
    set_attr(wv, attr_ruby_maxmargin, rst.maxmargin)
-   set_attr(wv, attr_ruby_maxprep, rst.intrusionpre)
-   set_attr(wv, attr_ruby_maxpostp, rst.intrusionpost)
+   set_attr(wv, attr_ruby_maxprep, rst.pre)
+   set_attr(wv, attr_ruby_maxpostp, rst.post)
+   set_attr(wv, attr_ruby_intergap, rst.intergap)
    set_attr(wv, attr_ruby_stretch, rst.stretch)
    set_attr(wv, attr_ruby_mode, rst.mode)
    local n = wv
@@ -335,8 +349,14 @@ end
 -- returned value: <new box>, <ruby width>, <post_intrusion>
 local max_margin
 local function new_ruby_box(r, p, ppre, pmid, ppost, 
-                           rpre, rmid, rpost, mapre, mapost, intmode)
+                           mapre, mapost, imode, rgap)
    local post_intrusion = 0
+   local intmode = imode%4
+   local rpre, rmid, rpost, rsmash
+   imode = floor(imode/262144); rsmash = (imode%2 ==1)
+   imode = floor(imode/2); rpost = imode%8;
+   imode = (imode-rpost)/8;  rmid  = imode%8;
+   imode = (imode-rmid)/8;   rpre  = imode%8
    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
@@ -360,15 +380,20 @@ local function new_ruby_box(r, p, ppre, pmid, ppost,
         setfield(rt, 'head', nil); node_free(rt);
       end
    end
-   local a = node_new(id_rule)
-   setfield(a, 'width', 0)
-   setfield(a, 'height', 0)
-   setfield(a, 'depth', 0)
-   insert_after(r, r, a); insert_after(r, a, p)
-   setfield(p, 'next', nil)
-   a = Dnode.vpack(r)
-   setfield(a, 'shift', 0)
+   local a, k = node_new(id_rule), node_new(id_kern)
+   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)
    set_attr(a, attr_ruby, post_intrusion)
+   if rsmash or getfield(a, 'height')<getfield(p, 'height') then
+      local k = node_new(id_kern)
+      setfield(k, 'kern', -getfield(a, 'height')+getfield(p, 'height'))
+      setfield(a, 'head', k); insert_before(r, r, k)
+      setfield(a, 'height', getfield(p, 'height'))
+   end
+
    return a, getfield(r, 'width'), post_intrusion
 end
 
@@ -391,8 +416,8 @@ local function pre_low_cal_box(w, cmp)
    local mdt -- nt*: node temp
    local coef = {} -- 連立一次方程式の拡大係数行列
    local rtb = expand_3bits(has_attr(wv, attr_ruby_stretch))
-   local rtc = expand_3bits(has_attr(wv, attr_ruby_mode))
-   local intmode = floor(has_attr(wv, attr_ruby_mode)/4)%4
+   local rgap = has_attr(wv, attr_ruby_intergap)
+   local intmode = floor(has_attr(wv, attr_ruby_mode)/4)
 
    -- node list 展開・行末形の計算
    local nt, nta, ntb = wv, nil, nil -- nt*: node temp
@@ -404,8 +429,7 @@ local function pre_low_cal_box(w, cmp)
       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), 
-                       rtb[6], rtb[5], rtb[4], rtc[10], rtc[9], rtc[8], 
-                       max_allow_pre, 0, intmode)
+                       rtb[6], rtb[5], rtb[4], max_allow_pre, 0, intmode, rgap)
    end
    node_free(nta); node_free(ntb)
 
@@ -418,8 +442,7 @@ local function pre_low_cal_box(w, cmp)
       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), 
-                       rtb[9], rtb[8], rtb[7], rtc[10], rtc[9], rtc[8], 
-                       0, max_allow_post, intmode)
+                       rtb[9], rtb[8], rtb[7], 0, max_allow_post, intmode, rgap)
    end
 
    -- ここで,nta, ntb には全 container を連結した box が入っているので
@@ -427,19 +450,33 @@ 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], rtc[10], rtc[9], rtc[8], 
-                    max_allow_pre, max_allow_post, intmode)
+      = 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))
    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 first_whatsit
+do
+   local traverse_id = Dnode.traverse_id
+   function first_whatsit(n) -- n 以後で最初の whatsit
+      for h in traverse_id(id_whatsit, n) do
+         return h
+      end
+      return nil
+   end
+end
+
 -- ノード追加
 local function pre_low_app_node(head, w, cmp, coef, ht, dp)
    -- メインの node list 更新
@@ -469,43 +506,43 @@ local function pre_low_app_node(head, w, cmp, coef, ht, dp)
       set_attr(nt, attr_ruby, 2*i+2)
       insert_after(head, nta, nt)
    end
-   tex.setattribute(attr_ruby, -0x7FFFFFFF)
+   tex.setattribute('global', attr_ruby, -0x7FFFFFFF)
    setfield(w, 'user_id', RUBY_POST)
-   return head, node_next(nt)
+   return head, first_whatsit(node_next(nt))
 end
 
 local function pre_high(ahead)
    if not ahead then return ahead end
    local head = to_direct(ahead)
    post_intrusion_backup = 0
-   local n = head
+   local n = first_whatsit(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
-           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
-           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'))
-           local s = node_tail(nv) --ルビ文字
-           head, n = pre_low_app_node(
-              head, n, getfield(nv, 'value'), coef, 
-               getfield(s, 'height'), getfield(s, 'depth')
-           )
-         else 
-            n = node_next(n)
+      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
+         local atr = has_attr(n, attr_ruby) or 0
+         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
+         max_margin = has_attr(nv, attr_ruby_maxmargin) or 0
+         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, 
+            getfield(s, 'height'), getfield(s, 'depth')
+         )
       else
-         n = node_next(n)
+         n = first_whatsit(node_next(n))
       end
    end
    return to_node(head)
@@ -572,28 +609,6 @@ 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 rw = nil  -- rw: main whatsit
@@ -603,38 +618,24 @@ local function post_high_break(head)
       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 = 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)
+        else -- i>=3 
+           rs[#rs+1] = ha; ha = node_next(ha)
         end
       end
       setfield(h, 'head', post_lown(rs, rw, cmp, getlist(h)))
@@ -649,23 +650,23 @@ local function post_high_hbox(ahead)
    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 
          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))
@@ -687,18 +688,32 @@ do
          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)
@@ -746,9 +761,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