OSDN Git Service

Import from old kitagawa_ruby branch (may not work now)
authorHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Thu, 6 Feb 2014 05:33:52 +0000 (14:33 +0900)
committerHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Thu, 6 Feb 2014 05:33:52 +0000 (14:33 +0900)
src/addons/luatexja-ruby.sty [new file with mode: 0644]
src/ltj-ruby.lua [new file with mode: 0644]
test/test14-ruby.tex [new file with mode: 0644]

diff --git a/src/addons/luatexja-ruby.sty b/src/addons/luatexja-ruby.sty
new file mode 100644 (file)
index 0000000..4c371bc
--- /dev/null
@@ -0,0 +1,188 @@
+%
+% luatexja-ruby.sty
+%
+
+% LaTeX only!
+\NeedsTeXFormat{LaTeX2e}
+\ProvidesPackage{luatexja-ruby}[2012/4/21 v0.1]
+\RequirePackage{luatexja}
+
+%%------------------
+\newluatexattribute\ltj@rubyattr
+\RequireLuaTeXjaSubmodule{ruby}
+
+% ltjset/getparameter への追加設定
+% これらは段落単位の設定.
+
+%% 引数:ルビ全角を単位とした実数
+%% この文字への pre-, post-intrusion の許容量
+\define@key[ltj]{japaram}{rubypreintrusion}{%
+  \expandafter\ltj@@set@stack@real#1:{ripre}{0}{0x7FFFFFFF}}
+\define@key[ltj]{japaram}{rubypostintrusion}{%
+  \expandafter\ltj@@set@stack@real#1:{ripost}{0}{0x7FFFFFFF}}
+\def\ltj@@set@stack@real#1,#2:#3#4#5{%
+  \directlua{luatexja.stack.set_stack_table(luatexja.isglobal, \asluastring{#3},
+    \ltj@safe@num@or\ltj@defnum{#1},\ltj@safe@real{#2},#4,#5)}}
+
+% ルビ用のキー設定
+\def\ltj@@rkeydef#1{
+  \define@key[ltj]{ruby}{#1}{\expandafter\def\csname ltj@@rubyip@#1\endcsname{##1}}
+}
+%% attr_ruby_mode
+%% bit 0: intrusion を有効にするか(1: 有効)
+%% bit 1: 前後の intrusion 許容量を小さい方に揃える (1: yes)
+%% bit 2, 3: intrusion をどう使って親文字を配置する 
+%%   00: intrusion なしでとりあえず計算し,左右の突出分を進入に割り当てる
+%%   01: pre-intrusion でまかなえるだけまかない,無理なら post- も使う
+%%   10: post を優先
+%%   11: 2 min (pre,post) までは pre, post に均等配分しようとする
+%%     >=01 で,intrusion で賄えきれなかった場合はいつものように伸長する.
+%% bit 4: 熟語ルビの際の処理方法(0: 常にグループ,1: 可能な限りブロックごとに)
+%%        ↑bit 4 は今は無効
+\ltj@@rkeydef{mode}
+%% intrusion 量強制固定(bit 0, bit 1 より優先,負数で「自動」)
+%% attr は sp 単位だが,ユーザーはルビ全角単位で指定する
+%% attr_ruby_maxprep, attr_ruby_maxpostp
+\ltj@@rkeydef{intrusionpre}
+\ltj@@rkeydef{intrusionpost}
+%% 親文字伸長の際の比,{0}{1}{1} などと0--7 の数 3 つで指定
+%% attr_ruby_stretch (head left,middle,right)(end)(middle) 27-bits 
+\ltj@@rkeydef{stretchhead} % 行頭形
+\ltj@@rkeydef{stretchend}  % 行末形
+\ltj@@rkeydef{stretch}     % 行中形
+%% ルビが伸長するときの比 {1}{2}{1} などと0--7 の数 3 つで指定
+%% attr_ruby_mode 上位部分
+\ltj@@rkeydef{stretchruby}
+%% ルビ<親のとき,ルビと親文字の端の最大値 
+%% attr_ruby_maxmargin
+%% attr は sp 単位だが,ユーザーは親文字全角単位で指定
+\ltj@@rkeydef{maxmargin}
+\savekeys[ltj]{ruby}{%
+  mode, intrusionpre, intrusionpost, 
+  stretchhead, stretchend, stretch, stretchruby, 
+  maxmargin, rubysize, 
+}
+
+%%%%%%%% setkeys の別名
+\protected\def\ltjsetruby{\setkeys[ltj]{ruby}}
+
+% ここからは ruby マクロ内でなんとかされる事項
+%% ルビと親文字の大きさの比
+\ltj@@rkeydef{rubysize}
+
+% 肩つき用簡易設定.
+\define@key[ltj]{ruby}{kata}[none]{\setkeys[ltj]{ruby}{mode=9,stretchruby=001}}
+% 拡張肩つき用簡易設定.
+\define@key[ltj]{ruby}{ekata}[none]{\setkeys[ltj]{ruby}{stretch=001,stretchruby=001}}
+
+%%%%%%%% 補助関数
+
+% Lua ソースに渡す table 生成
+\def\ltj@@ruby@create@table#1{% #1: ルビ全角
+    \string{
+      rubyzw = \ltj@safe@dimen{#1}, 
+      maxmargin = \ltj@safe@dimen{\ltj@@rubyip@maxmargin\zw},
+      intrusionpre = \ltj@safe@dimen{\ltj@@rubyip@intrusionpre#1},
+      intrusionpost = \ltj@safe@dimen{\ltj@@rubyip@intrusionpost#1},
+      stretch 
+        = 262144 * \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretchhead
+         + 512 * \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretchend
+         + \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretch,
+      mode 
+        = 2097152 * \expandafter\ltj@@ruby@cts\ltj@@rubyip@stretchruby
+          + \ltj@safe@num{\ltj@@rubyip@mode},
+    \string}
+}
+\def\ltj@@ruby@cts#1#2#3{%
+  ((\ltj@safe@num{#1}) * 64 + (\ltj@safe@num{#2}) * 8 + \ltj@safe@num{#3})%
+}
+
+
+%%%%%%%% ZR さんの PXrubrica パッケージ中のコードから引用・改変
+% \pxrr@decompbar: a|bc -> \ltj@@ruby@mark{a}\ltj@@ruby@mark{bc}
+\let\pxrr@res\empty
+\def\pxrr@ifx#1{%
+  \ifx#1\expandafter\@firstoftwo
+  \else\expandafter\@secondoftwo
+  \fi
+}
+\def\pxrr@nil{\noexpand\pxrr@nil}
+\def\pxrr@end{\noexpand\pxrr@end}
+\def\pxrr@appto#1#2{%
+  \expandafter\def\expandafter#1\expandafter{#1#2}%
+}
+\def\pxrr@decompbar#1{%
+  \let\pxrr@res\@empty
+  \pxrr@decompbar@loopa\pxrr@nil#1|\pxrr@end|%
+}
+\def\pxrr@decompbar@loopa#1|{%
+  \expandafter\pxrr@decompbar@loopb\expandafter{\@gobble#1}%
+}
+\def\pxrr@decompbar@loopb#1{%
+  \pxrr@decompbar@loopc#1\relax\pxrr@nil{#1}%
+}
+\def\pxrr@decompbar@loopc#1#2\pxrr@nil#3{%
+  \pxrr@ifx{#1\pxrr@end}{}{%
+    \ifx\pxrr@res\@empty
+      \def\pxrr@res{\ltj@@ruby@mark}%
+    \else
+      \pxrr@appto\pxrr@res{\ltj@@ruby@mark}%
+    \fi
+    \pxrr@appto\pxrr@res{{#3}}%
+    \pxrr@decompbar@loopa\pxrr@nil
+  }%
+}
+%%%%%%%% ここまで
+
+%%%%%%%% TeX command
+\protected\def\ltjruby{\@ifnextchar[\ltj@@ruby{\ltj@@ruby[]}}%]
+\let\ruby=\ltjruby
+\def\ltj@@ruby[#1]#2#3{{% #1: option #2: 親文字群,#3: ルビ文字列群,共に| 区切り
+  \setkeys[ltj]{ruby}{#1}%
+  \directlua{ruby_tmplist_r = \string{\string}; ruby_tmplist_p = \string{\string}}%
+  \leavevmode\dimen0=\f@size pt\dimen1=\ltj@@rubyip@rubysize\dimen0%
+  % 引数展開,テーブルにセット
+  \pxrr@decompbar{#2}{\let\ltj@@ruby@mark\ltj@@ruby@sp\pxrr@res}%
+  \pxrr@decompbar{#3}{\let\ltj@@ruby@mark\ltj@@ruby@sr\pxrr@res}%
+  {\fontsize{\ltj@@rubyip@rubysize\dimen0}\z@\selectfont\global\dimen1=\zw}%
+  \directlua{%
+    luatexja.ruby.texiface(\ltj@@ruby@create@table{\dimen1},
+    ruby_tmplist_r, ruby_tmplist_p)}%
+}}
+
+\def\ltj@@ruby@sr#1{%
+  \setbox0=\hbox{\fontsize{\dimen1}\z@\selectfont#1}%
+  \directlua{table.insert(ruby_tmplist_r, node.copy(tex.box[0]))}%
+}
+\def\ltj@@ruby@sp#1{%
+  \setbox0=\hbox{\selectfont#1}%
+  \directlua{table.insert(ruby_tmplist_p, node.copy(tex.box[0]))}%
+}
+
+% 初期値.要調整
+%% ひらがな
+\count@="3040\loop\relax\ifnum \count@<"30A0
+  \ltjsetparameter{rubypreintrusion={\the\count@,1}, 
+    rubypostintrusion={\the\count@,1}}
+  \advance\count@1\repeat
+%% 「」
+\ltjsetparameter{rubypreintrusion={`「,1}, 
+  rubypostintrusion={`」,1}}
+%% ,、
+\ltjsetparameter{rubypreintrusion={`,,1}, 
+  rubypostintrusion={`,,1}}
+\ltjsetparameter{rubypreintrusion={`、,1}, 
+  rubypostintrusion={`、,1}}
+%% ・
+\ltjsetparameter{rubypreintrusion={`・,1}, 
+  rubypostintrusion={`・,1}}
+
+\setkeys[ltj]{ruby}{
+  maxmargin=0.5, 
+  stretchruby={1}{2}{1}, stretch = {1}{2}{1},
+  stretchhead = {0}{1}{1}, stretchend = {1}{1}{0},
+  intrusionpre = -1, intrusionpost = -1, 
+  mode = 1, rubysize = 0.5
+}
+
+\endinput
\ No newline at end of file
diff --git a/src/ltj-ruby.lua b/src/ltj-ruby.lua
new file mode 100644 (file)
index 0000000..df9b4f7
--- /dev/null
@@ -0,0 +1,594 @@
+--
+-- ltj-ruby.lua
+--
+luatexbase.provides_module({
+  name = 'luatexja.ruby',
+  date = '2012/04/21',
+  version = '0.1',
+  description = 'Ruby',
+})
+module('luatexja.ruby', package.seeall)
+local err, warn, info, log = luatexbase.errwarinf(_NAME)
+
+local id_hlist = node.id('hlist')
+local id_vlist = node.id('vlist')
+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_glue_spec = node.id('glue_spec')
+local sid_user = node.subtype('user_defined')
+local ltjs_get_penalty_table = luatexja.stack.get_penalty_table
+local id_pbox_w = node.id('hlist') + 513      -- cluster which consists of a whatsit
+
+local attr_icflag = luatexbase.attributes['ltj@icflag']
+-- ルビ処理用の attribute は他のやつの流用なので注意!
+-- 進入許容量 (sp)
+local attr_ruby_maxprep = luatexbase.attributes['ltj@charclass']
+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 = luatexbase.attributes['ltj@rubyattr']
+-- ルビ内部処理用
+-- jfmglue 中では「2つ前のクラスタもルビ」のフラグ(true = 1)
+-- (whatsit).value node ではルビ全角の値(sp単位)
+-- 行分割前後では,「何番目のルビ関連ノード」か
+
+local round = tex.round
+local floor = math.floor
+local min = math.min
+local max = math.max
+--local node_type = node.type
+local node_new = node.new
+local node_remove = node.remove
+-- local node_prev = node.prev
+local node_next = node.next
+local node_copy = node.copy
+local node_tail = node.tail
+local node_free = node.free
+local has_attr = node.has_attribute
+local set_attr = node.set_attribute
+local node_insert_before = node.insert_before
+local node_insert_after = node.insert_after
+
+local FROM_JFM = 4
+local KANJI_SKIP = 6
+local XKANJI_SKIP = 7
+local PROCESSED = 8
+
+local uid_ruby_pre = 30120
+local uid_ruby_post = 30121
+
+local box_stack_level
+
+----------------------------------------------------------------
+-- 補助関数群 1
+----------------------------------------------------------------
+
+local function gauss(coef)
+   -- #coef 式,#coef 変数の連立1次方程式系を掃きだし法で解く.
+   local deg = #coef
+   for i = 1, deg do
+      if coef[i][i]==0 then
+        for j = i+1, deg do 
+           if coef[j][i]~=0 then 
+              local tmp = coef[i]; coef[i] = coef[j]; coef[j] = tmp
+              break
+           end
+        end
+      end
+      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
+           for k = 1, deg+1 do coef[i][k] = coef[i][k]/d end
+        end
+      end
+   end
+end
+
+-- 実行回数 + ルビ中身 から uniq_id を作る関数
+-- 未実装.これを使えば 2 回目以降の組版に 1 回目の情報が使える
+
+
+-- concatenation of boxes: reusing nodes
+-- ルビ組版が行われている段落/hboxでの設定が使われる.
+-- ルビ文字を格納しているボックスでの設定ではない!
+local function concat(f, b)
+   local r
+   if f then
+      if b then
+         local h = f.head
+         node_tail(h).next = b.head
+        f.head = nil; b.head = nil
+        node_free(f); node_free(b)
+        r = node.hpack(luatexja.jfmglue.main(h,false))
+      else 
+        r = f
+      end
+   elseif b then
+      r = b
+   else
+      local h = node_new(id_hlist)
+      h.subtype = 0; h.width = 0; h.height = 0; h.depth = 0;
+      h.head = nil; h.glue_set = 0; h.glue_order = 0
+      r = h
+   end
+   return r
+end
+
+local function expand_3bits(num)
+   local t = {}; local a = num
+   for i = 1, 10 do
+      table.insert(t,a%8); a = floor(a/8)
+   end
+   return t
+end
+----------------------------------------------------------------
+-- 補助関数群 2
+----------------------------------------------------------------
+
+-- box の中身のノードは再利用される
+local function enlarge(box, new_width, pre, middle, post, prenw, postnw)
+   -- pre, middle, post: 伸縮比率
+   -- prenw, postnw: 前後の自然長 (sp)
+   local h = box.head; local hh = box.height; local hd = box.depth
+   local hx = h
+   while hx do
+      if has_attr(hx, attr_icflag) == KANJI_SKIP
+         or has_attr(hx, attr_icflag) == XKANJI_SKIP
+         or has_attr(hx, attr_icflag) == FROM_JFM then
+        -- この 3 種類の空白をのばす
+         if hx.id == id_kern then
+            local k = node_new(id_glue);
+            local ks = node_new(id_glue_spec);
+            ks.width = hx.kern; 
+            ks.stretch_order = 2; ks.stretch = round(middle*65536);
+            ks.shrink_order = 0; ks.shrink = 0; 
+            k.subtype = 0; k.spec = ks
+            h = node_insert_after(h, hx, k);
+            h = node_remove(h, hx); node_free(hx); hx = k;
+         else -- glue
+            hx.spec = node_copy(hx.spec);
+            hx.spec.stretch_order=2; hx.spec.stretch = round(middle*65536);
+            hx.spec.shrink_order=0; hx.spec.shrink = 0;
+         end
+      end
+      hx = node_next(hx)
+   end
+   -- 先頭と末尾の空白を挿入
+   local k = node_new(id_glue);
+   local ks = node_new(id_glue_spec);
+   ks.width = prenw; ks.stretch_order = 2; ks.stretch = round(pre*65536);
+   ks.shrink_order = 0; ks.shrink = 0;
+   k.subtype = 0; k.spec = ks; h = node_insert_before(h, h, k);
+   local k = node_new(id_glue);
+   local ks = node_new(id_glue_spec);
+   ks.width = postnw; ks.stretch_order = 2; ks.stretch = round(post*65536);
+   ks.shrink_order = 0; ks.shrink = 0;
+   k.subtype = 0; k.spec = ks; node_insert_after(h, node_tail(h), k);
+   -- hpack
+   box.head = nil; node_free(box);
+   box = node.hpack(h, new_width, 'exactly')
+   box.height = hh; box.depth = hd; return box
+end
+
+----------------------------------------------------------------
+-- TeX interface
+----------------------------------------------------------------
+
+local function texiface_low(rst, rtlr, rtlp)
+   local w = node_new(id_whatsit, sid_user)
+   w.type = 110; w.user_id = uid_ruby_pre;
+   w.value = node_new(id_whatsit, sid_user)
+   w.value.type = 100; w.value.value = floor(#rtlr)
+   set_attr(w.value, attr_ruby, rst.rubyzw)
+   set_attr(w.value, attr_ruby_maxmargin, rst.maxmargin)
+   set_attr(w.value, attr_ruby_maxprep, rst.intrusionpre)
+   set_attr(w.value, attr_ruby_maxpostp, rst.intrusionpost)
+   set_attr(w.value, attr_ruby_stretch, rst.stretch)
+   set_attr(w.value, attr_ruby_mode, rst.mode)
+   local m = w.value
+   for i = 1, #rtlr do
+      local n = rtlr[i]; m.next = n; m = rtlp[i]; n.next = m
+   end
+   -- w.value: (whatsit) .. r1 .. p1 .. r2 .. p2
+   node.write(w)
+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
+      luatexja.base.package_error('luatexja-ruby',
+                                 'Group count mismatch between the ruby and\n' ..
+                                    'the body (' .. #rtlr .. ' != ' .. #rtlp .. ').',
+                                 '')
+   else
+      local f = true
+      for i = 1,#rtlr do
+        if rtlr[i].width > rtlp[i].width then f = false; break end
+      end
+      if f then -- モノルビ * n
+        local r,p = {true}, {true}
+        for i = 1,#rtlr do
+           r[1] = rtlr[i]; p[1] = rtlp[i]; texiface_low(rst, r, p)
+        end
+      else
+        texiface_low(rst, rtlr, rtlp)
+      end
+   end
+end
+
+
+----------------------------------------------------------------
+-- pre_line_break
+----------------------------------------------------------------
+
+-- r, p の中身のノードは再利用される
+local function enlarge_parent(r, p, ppre, pmid, ppost, mapre, mapost, intmode)
+   local sumprot = r.width  - p.width -- >0
+   local pre_intrusion, post_intrusion
+   if intmode == 0 then --  とりあえず組んでから決める
+      p = enlarge(p, r.width, ppre, pmid, ppost, 0, 0) 
+      pre_intrusion = min(mapre, round(ppre*p.glue_set*65536))
+      post_intrusion = min(mapost, round(ppost*p.glue_set*65536))
+   elseif intmode == 1 then
+      pre_intrusion = min(mapre, sumprot); 
+      post_intrusion = min(mapost, max(sumprot-pre_intrusion, 0))
+      p = enlarge(p, r.width, ppre, pmid, ppost, pre_intrusion, post_intrusion)
+   elseif intmode == 2 then
+      post_intrusion = min(mapost, sumprot); 
+      pre_intrusion = min(mapre, max(sumprot-post_intrusion, 0))
+      p = enlarge(p, r.width, ppre, pmid, ppost, pre_intrusion, post_intrusion) 
+   else --  intmode == 3
+      local n = min(mapre, mapost)*2
+      if n < sumprot then
+        pre_intrusion = n/2; post_intrusion = n/2
+      else
+        pre_intrusion = floor(sumprot/2); post_intrusion = sumprot - pre_intrusion
+      end
+      p = enlarge(p, r.width, ppre, pmid, ppost, pre_intrusion, post_intrusion) 
+      pre_intrusion = min(mapre, pre_intrusion + round(ppre*p.glue_set*65536))
+      post_intrusion = min(mapost, post_intrusion + round(ppost*p.glue_set*65536))
+   end
+      r.shift = -pre_intrusion
+      r.width = r.width - pre_intrusion - post_intrusion
+      p.width = r.width
+      p.head.spec.width = p.head.spec.width - pre_intrusion
+   return r, p, post_intrusion
+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)
+   local post_intrusion = 0
+   if r.width > p.width then  -- change the width of p
+      r, p, post_intrusion  = enlarge_parent(r, p, ppre, pmid, ppost, mapre, mapost, intmode) 
+   elseif r.width < p.width then -- change the width of r
+      r = enlarge(r, p.width, rpre, rmid, rpost, 0, 0) 
+      post_intrusion = 0
+      local need_repack = false
+      -- margin が大きくなりすぎた時の処理
+      if round(rpre*r.glue_set*65536) > max_margin then
+        local sp = r.head.spec; need_repack = true
+        sp.width = max_margin; sp.stretch = 1 -- 全く伸縮しないのも困る
+      end
+      if round(rpost*r.glue_set*65536) > max_margin then
+        local sp = node_tail(r.head).spec; need_repack = true
+        sp.width = max_margin; sp.stretch = 1 -- 全く伸縮しないのも困る
+      end
+      if need_repack then
+        local rt = r
+        r = node.hpack(r.head, r.width, 'exactly')
+        rt.head = nil; node_free(rt);
+      end
+   end
+   local a = node_new(id_rule); a.width = 0; a.height = 0; a.depth = 0
+   node_insert_after(r, r, a); node_insert_after(r, a, p); p.next = nil
+   a = node.vpack(r); a.height = p.height; a.depth = p.depth;
+   a.shift = -(r.height+r.depth)
+   return a, r.width, post_intrusion
+end
+
+
+-- High-level routine in pre_linebreak_filter
+local max_post_intrusion_backup
+local max_allow_pre, max_allow_post
+
+
+-- 中付き熟語ルビ,cmp containers
+-- 「文字の構成を考えた」やつはどうしよう
+local function pre_low_cal_box(w, cmp)
+   local rb = {}
+   local pb = {}
+   local kf = {}
+   -- kf[i] : container 1--i からなる行末形
+   -- kf[cmp+i] : container i--cmp からなる行頭形
+   -- kf[2cmp+1] : 行中形
+   local nt, nta, ntb, mdt -- nt*: node temp
+   local coef = {} -- 連立一次方程式の拡大係数行列
+   local rtb = expand_3bits(has_attr(w.value, attr_ruby_stretch))
+   local rtc = expand_3bits(has_attr(w.value, attr_ruby_mode))
+   local intmode = floor(has_attr(w.value, attr_ruby_mode)/4)%4
+
+   -- node list 展開・行末形の計算
+   nta = nil; ntb = nil; nt = w.value
+   for i = 1, cmp do
+      nt = nt.next; rb[i] = nt; nta = concat(nta, node_copy(nt))
+      nt = nt.next; pb[i] = nt; ntb = concat(ntb, node_copy(nt))
+      coef[i] = {}
+      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), 
+                       rtb[6], rtb[5], rtb[4], rtc[10], rtc[9], rtc[8], 
+                       max_allow_pre, 0, intmode)
+   end
+   node.free(nta); node.free(ntb)
+
+   -- 行頭形の計算
+   nta = nil; ntb = 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
+        = 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
+      = 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 更新.
+   nt = w.value
+   node.flush_list(nt.next)
+   for i = 1, 2*cmp+1 do nt.next = kf[i]; nt = kf[i]  end
+
+   gauss(coef) -- 掃きだし法で連立方程式形 coef を解く
+   return coef
+end
+
+-- ノード追加
+local function pre_low_app_node(head, w, cmp, coef, ht, dp)
+   local nt, nta, ntb
+   -- メインの node list 更新
+   nt = node_new(id_glue); ntb = node_new(id_glue_spec);
+   ntb.width = coef[1][2*cmp+2]; ntb.stretch_order = 0; ntb.stretch = 0;
+   ntb.shrink_order = 0; ntb.shrink = 0; nt.subtype = 0; nt.spec = ntb; 
+   head = node_insert_before(head, w, nt)
+   set_attr(nt, attr_ruby, 1)
+   set_attr(w, attr_ruby, 2); nt = w
+   for i = 1, cmp do
+      -- rule
+      nta = node_new(id_rule); nta.width = coef[i*2][2*cmp+2]; 
+      nta.height = ht; nta.depth =dp; nta.subtype = 0
+      node_insert_after(head, nt, nta); set_attr(nta, attr_ruby, 2*i+1)
+      -- glue
+      nt = node_new(id_glue); ntb = node_new(id_glue_spec);
+      ntb.width = coef[i*2+1][2*cmp+2]; ntb.stretch_order = 0; ntb.stretch = 0;
+      ntb.shrink_order = 0; ntb.shrink = 0; nt.subtype = 0; nt.spec = ntb; 
+      head = node_insert_after(head, nta, nt); set_attr(nt, attr_ruby, 2*i+2)
+   end
+   tex.setattribute(attr_ruby, -0x7FFFFFFF);
+   w.user_id = uid_ruby_post;
+   return head, node_next(nt)
+end
+
+local function pre_high(head)
+   local n = head; max_post_intrusion_backup = 0
+   if not n then return head end
+   while n do
+      if n.id == id_whatsit then
+         if n.subtype == sid_user and n.user_id == uid_ruby_pre then
+            max_allow_pre = has_attr(n.value, 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)
+           end
+           max_post_intrusion_backup = 0
+            max_allow_post = has_attr(n.value, attr_ruby_maxpostp) or 0
+            max_margin = has_attr(n.value, attr_ruby_maxmargin) or 0
+           local coef = pre_low_cal_box(n, n.value.value)
+           local s = node_tail(n.value) --ルビ文字
+           head, n = pre_low_app_node(
+              head, n, n.value.value, coef, s.height, s.depth
+           )
+         else 
+            n = n.next
+         end
+      else
+         n = n.next
+      end
+   end
+   return head
+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)
+
+----------------------------------------------------------------
+-- post_line_break
+----------------------------------------------------------------
+
+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 = rw.value.value
+   if hn==1 then 
+      if fn==2*cmp+2 then
+        hn = node_tail(rw.value); node_remove(rw.value, hn)
+        node_insert_after(ch, rs[#rs], hn)
+         set_attr(hn, attr_icflag,  PROCESSED)
+      else
+        local deg = (fn-1)/2
+         hn = rw.value; for i = 1, deg do hn = hn.next end; 
+        node_remove(rw.value, hn); hn.next = nil;
+         node_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 = rw.value; for i = 1, cmp+deg-1 do hn = hn.next end
+      -- -1 is needed except the case hn = 3, 
+      --   because a ending-line form is removed already from the list
+      node_remove(rw.value, hn); hn.next = nil
+      node_insert_after(ch, rs[#rs], hn)
+      set_attr(hn, attr_icflag,  PROCESSED)
+   end
+   for i, v in ipairs(rs) do ch = node_remove(ch, v); node_free(v) end
+   if fn >= 2*cmp+1 then
+      rw.next = nil; node_free(rw); 
+   end
+   return ch;
+end
+
+
+local function post_high_break(head)
+   local h = head; 
+   local rs = {};  -- rs: sequence of ruby_nodes, 
+   local rw = nil; -- rw: main whatsit
+   while h do 
+      if h.id == id_hlist then
+         local ha = h.head; rs = {}
+         while ha do
+            local i = (((ha.id == id_glue and ha.subtype==0) 
+                 or (ha.id == id_rule and ha.subtype==0)
+                 or (ha.id == id_whatsit and ha.subtype==sid_user 
+                    and ha.user_id==uid_ruby_post))
+                   and has_attr(ha, attr_ruby)) or 0
+            if i==1 then 
+               h.head = post_lown(rs, rw, h.head); rs = {}; rw = nil
+               table.insert(rs, ha); ha = node_next(ha)
+            elseif i>=3 then 
+               table.insert(rs, ha); ha = node_next(ha)
+            elseif i==2 then 
+               rw = ha; h.head, ha = node_remove(h.head, rw); 
+            else
+               ha = node_next(ha)
+            end
+         end
+         h.head = post_lown(rs, rw, h.head);
+      end
+      h = node_next(h)
+   end
+   return head
+end 
+
+local function post_high_hbox(head)
+   local ha = head; 
+   local rs = {};  -- rs: sequence of ruby_nodes, 
+   local rw = nil; -- rw: main whatsit
+   while ha do
+      local i = (((ha.id == id_glue and ha.subtype==0) 
+               or (ha.id == id_rule and ha.subtype==0)
+               or (ha.id == id_whatsit and ha.subtype==sid_user 
+                    and ha.user_id==uid_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)
+      elseif i>=3 then 
+         table.insert(rs, ha); ha = node_next(ha)
+      elseif i==2 then 
+         rw = ha; head, ha = node_remove(head, rw); 
+      else
+         ha = node_next(ha)
+      end
+   end
+   return post_lown(rs, rw, head);
+end
+
+luatexbase.add_to_callback('post_linebreak_filter', post_high_break, 'ltj.ruby.post_break', 100)
+luatexbase.add_to_callback('hpack_filter', post_high_hbox, 'ltj.ruby.post_hbox', 101)
+
+
+----------------------------------------------------------------
+-- for jfmglue callbacks
+----------------------------------------------------------------
+function whatsit_callback(Np, lp, Nq, bsl) 
+   if Np.nuc then return Np 
+   elseif lp.user_id == uid_ruby_pre then
+      Np.first = lp; Np.nuc = lp; Np.last = lp
+      local x = lp.value.next.next
+      Np.last_char = luatexja.jfmglue.check_box_high(Np, x.head, nil)
+      if Nq.id ~=id_pbox_w and  Nq.char then
+        if has_attr(lp.value, attr_ruby_maxprep) < 0 then -- auto
+           local p = round(ltjs_get_penalty_table('ripre', Nq.char, 0, bsl)
+                        *has_attr(lp.value, attr_ruby))
+           if has_attr(lp.value, attr_ruby_mode)%2 == 0 then -- intrusion 無効
+              p = 0
+           end
+           set_attr(lp.value, attr_ruby_maxprep, p)
+        end
+        if Nq.prev_ruby then 
+           set_attr(lp, attr_ruby, 1)
+        end
+      else
+        set_attr(lp.value, attr_ruby_maxprep, 0)
+      end
+      return Np
+   end
+end
+function whatsit_after_callback(s, Nq, Np, bsl)
+   if not s and Nq.nuc.user_id == uid_ruby_pre then
+      local x = Nq.nuc.value.next.next
+      for i = 2, Nq.nuc.value.value do x = x.next.next end
+      Nq.last_char = luatexja.jfmglue.check_box_high(Nq, x.head, nil)
+      luatexja.jfmglue.after_hlist(Nq)
+      s = true
+      if Np and Np.id ~=id_pbox_w and  Np.char then
+         if has_attr(Nq.nuc.value, attr_ruby_maxpostp) < 0 then -- auto
+            local p = floor(ltjs_get_penalty_table('ripost', Np.char, 0, bsl)
+                         *has_attr(Nq.nuc.value, attr_ruby))
+            if has_attr(Nq.nuc.value, attr_ruby_mode)%2 == 0 then -- intrusion 無効
+               p = 0
+            end
+            if has_attr(Nq.nuc.value, attr_ruby_mode)%4 >= 2 then
+               local q = has_attr(Nq.nuc.value, attr_ruby_maxprep)
+               if q < p then p = q
+               elseif q > p then
+                  set_attr(Nq.nuc.value, attr_ruby_maxprep, p)
+               end
+            end
+            set_attr(Nq.nuc.value, attr_ruby_maxpostp, p)
+         end
+         Np.prev_ruby = true -- 前のクラスタがルビであったことのフラグ
+      else -- 直前が文字以外なら intrusion なし
+         set_attr(Nq.nuc.value, attr_ruby_maxpostp, 0)
+         if has_attr(Nq.nuc.value, attr_ruby_mode)%4 >= 2 then
+            set_attr(Nq.nuc.value, attr_ruby_maxprep, 0)
+         end
+         if Np and Np.id == id_pbox_w then
+            set_attr(Np.nuc.value, attr_ruby_maxprep, 0)
+         end
+      end
+   end
+   return s
+end
+
+luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
+                           "luatexja.ruby.np_info", 1)
+luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
+                           "luatexja.ruby.np_info_after", 1)
+
diff --git a/test/test14-ruby.tex b/test/test14-ruby.tex
new file mode 100644 (file)
index 0000000..1d36ebc
--- /dev/null
@@ -0,0 +1,511 @@
+%#! lualatex
+%%% a test of ruby.
+\documentclass[b5paper,10pt]{ltjsarticle}
+\usepackage{fontspec,luacode,luatexja-otf,amsmath}
+\usepackage{unicode-math}
+\setmathfont{XITS Math}
+\setmainfont{TeX Gyre Termes}
+\setsansfont{TeX Gyre Heros}
+\setmonofont{LMMono10-Regular}
+\usepackage{luatexja-ruby,showexpl,booktabs}
+\lstset{preset=\huge,basicstyle=\ttfamily}
+\fboxsep=0mm
+%\usepackage{lua-visual-debug}
+
+\def\kata{\ltjsetruby{kata}} 
+% 肩つきルビ用簡易設定.グループルビでは使用しないこと
+\def\notalign{\setkeys[ltj]{ruby}{stretchhead = {1}{2}{1}, stretchend = {1}{2}{1}}}
+% 行頭・行末で揃えない
+
+\def\Node#1#2{\,\vcenter{\hbox{\fboxsep=1pt\fbox{\vbox{\small\halign{\hfil##\hfil\cr
+  #1\mathstrut\cr\noalign{\hrule height.4pt}\strut#2\cr}}}}\,}}
+\def\LuaTeX{Lua\TeX}
+
+% 三分ルビ用
+\DeclareFontShape{JY3}{mc}{mc}{n}{<-> [0.92487] 
+  psft:Ryumin-Light:extend=0.67;jfm=ujisc33}{}
+
+\title{ルビ実装のサンプル}
+\begin{document}
+\maketitle
+
+\section{使い方}
+\begin{itemize}
+\item パッケージ読み込み:\verb+\usepackage{luatexja-ruby}+のみ.\LaTeXe のみサポート.
+\item 命令:
+\begin{itemize}
+\item \verb+\ltjruby[option]{親|文|字}{おや|も|じ}+
+\item \verb|\ltjsetruby{option}|
+\end{itemize}
+\item \verb|\ltjsetparameter|に追加のキー
+\begin{itemize}
+\item \verb+rubypreintrusion={char_code, pre_int}+:全角単位
+\item \verb+rubypostintrusion={char_code, post_int}+:全角単位
+\end{itemize}
+\item \verb|\ltjsetruby{option}|他でサポートするオプション:後ろに指定したものが優先
+\begin{itemize}
+\item \verb+mode+:進入処理のモードを表すbit vector.意味はluatexja-ruby.sty参照.
+\item \verb+intrusionpre=<real>+:前進入量指定.ルビ全角単位で指定.
+負の長さは文字種に応じた自動指定を意味.
+\item \verb+intrusionpost=<real>+:後進入量指定.
+\item \verb+stretchhead={left}{middle}{right}+:行頭形でルビ文字の合計長が親文字の合計長より
+長い時に,親文字間に入れる空白の割合.それぞれ0--7の自然数で指定.
+\item \verb+stretchend={left}{middle}{right}+:行末形〜.
+\item \verb+stretch={left}{middle}{right}+:行中形〜.
+\item \verb+stretchruby={left}{middle}{right}+:ルビ文字の方が長い時の空白の割合.
+\item \verb+maxmargin=<real>+:親文字の方がルビより長い時に,ルビの先頭と親文字の先頭に許される
+最大の空白量.親文字全角単位.
+\item \verb+rubysize=<real>+:ルビ文字の親文字に対する大きさ.
+\end{itemize}
+\end{itemize}
+
+\section{実装の大まかな方法}
+次の例で説明する.
+\begin{LTXexample}
+……を\ruby{流|暢}{りゆう|ちよう}に……
+\end{LTXexample}
+
+\begin{enumerate}
+\item \verb|\ruby|コマンド自体は,一旦次のnode listを値とするwhatsit~$W$を作って,
+現在の水平リストへと挿入する(必要ならば\verb|\leavevmode|も実行):
+\[
+ \Node{whatsit $w$}{value: 2}\longrightarrow 
+ \Node{hlist $r_1$}{「りゆう」}\longrightarrow
+ \Node{hlist $p_1$}{「流」}\longrightarrow
+ \Node{hlist $r_2$}{「ちよう」}\longrightarrow
+ \Node{hlist $p_2$}{「暢」}
+\]
+ここで,最初の$w$の値2は,ルビが2つのパーツ「\ruby{流}{りゆう}」「\ruby{暢}{ちよう}」からなっていることを
+表している.$r_i$達の中の文字は既にルビの大きさである.この値を$\mathit{cmp}$とおこう.
+\item \LuaTeX-jaの和文処理グルー挿入処理においては,「先頭が『流』,最後が『暢』であるようなhboxを\verb|\unhbox|で展開したもの」と扱われる.
+言い換えれば,ルビ部分を無視した単なる「流暢」という和文文字の並びとして扱われる\footnote{「流」「暢」の間のグルーは既に入っている,と扱われる.}.
+次のサンプルを参照
+\begin{LTXexample}
+\leavevmode\hbox{.}A\\
+%↑xkanjiskip 
+\ruby{.}{}A
+%↑2分
+\end{LTXexample}
+\item 和文処理グルーの挿入が終わった後で,可動グループルビのためのノードの挿入に入る.
+\begin{enumerate}
+\item $W$の周辺は次のようなノード列になる.
+\begin{align*}
+ (\text{other nodes})&\longrightarrow
+ \Node{glue $g_0$}{}\longrightarrow \Node{whatsit~$W$}{元からある}\longrightarrow \Node{rule $r_1$}{}
+\\&\longrightarrow
+ \Node{glue $g_1$}{}\longrightarrow \Node{rule $r_2$}{}\longrightarrow 
+ \Node{glue $g_2$}{}\longrightarrow (\text{other nodes})
+\end{align*}
+$W$の前後に$2\mathit{cmp}+1=5$個のノードが挿入される.
+
+\item このようにノードを挿入する目的は,\TeX の行分割処理自体に影響を加えずに可動グループルビ
+を実現させることにある.
+\begin{gather*}
+ (\text{other nodes})\longrightarrow
+ \Node{glue $g_0$}{}\longrightarrow \Node{whatsit~$W$}{元からある}\longrightarrow \Node{rule $r_1$}{}\\
+\noalign{\hrulefill 行の境目\hrulefill}
+\Node{rule $r_2$}{}\longrightarrow 
+ \Node{glue $g_2$}{}\longrightarrow (\text{other nodes})
+\end{gather*}
+のようになったとしたら,「\ruby{流}{りゆう}」「\ruby{暢}{ちよう}」の間で行分割が起きた,ということがわかり,
+$g_i$,~$r_i$達のノードを適切に置き換えればよい(後で詳しく説明する).
+
+\item なお,$r_i$達の高さ・深さは組み上がった後のそれであり,現状では親文字部分の高さ・深さと一致する.
+$g_i$,~$r_i$達の幅は,以下の対応に沿って計算される.
+
+\begin{center}\small
+\begin{tabular}{cllll}
+\toprule
+\multicolumn{1}{c}{\sf node名}&\multicolumn{1}{c}{\sf 組み方}&\multicolumn{1}{c}{\sf サンプル}%
+&\multicolumn{1}{c}{\sf 対応するノード並び}\\
+\midrule
+$n_1$&行末1グループ&
+\huge
+\fbox{を}{\color{blue}\gt\ruby[intrusionpre=1,mode=5,stretch=110]{流}{りゆう}}\vrule
+&
+$g_0\rightarrow W\rightarrow r_1$\\
+$n_2$&行末2グループ&
+\huge
+\fbox{を}{\color{blue}\gt\ruby[intrusionpre=1,mode=1,stretch=110]{流|暢}{りゆう|ちよう}}\vrule
+&
+$g_0\rightarrow W\rightarrow r_1\rightarrow g_2 \rightarrow r_2$\\
+$n_3$&行頭1グループ&
+\huge
+\vrule{\color{blue}\gt\ruby[intrusionpost=1,mode=1,stretch=011]{暢}{ちよう}}\fbox{に}
+&
+$r_2\rightarrow g_2$\\
+$n_4$&行頭2グループ&
+\huge
+\vrule{\color{blue}\gt\ruby[intrusionpost=1,mode=1,stretch=011]{流|暢}{りゆう|ちよう}}\fbox{に}
+&
+$W\rightarrow r_1\rightarrow g_2 \rightarrow r_2\rightarrow g_2$\\
+$n_5$&行中&
+\huge
+\fbox{を}{\color{blue}\gt\ruby[intrusionpost=0.5,intrusionpre=0.5,mode=1]{流|暢}{りゆう|ちよう}}\fbox{に}
+&
+$g_0\rightarrow W\rightarrow r_1\rightarrow g_2 \rightarrow r_2\rightarrow g_2$\\
+
+\bottomrule
+\end{tabular}
+\end{center}
+
+
+この5元連立方程式を解いて$g_i$,~$r_i$達の幅を求める.
+なお,進入長は予め計算しておく.例えばこの場合,
+\[
+ g_0+r_1+g_2+r_2+g_2 = 3\,\mathrm{zw}-(0.25\,\mathrm{zw}\times 2)=2.5\,\mathrm{zw}
+\]
+である(zwは親文字全角の幅).
+
+\item また,ルビ処理を統括しているwhatsit~$W$の値も
+\[
+ \Node{whatsit $w$}{value: 2}\longrightarrow 
+ \Node{vlist $n_1$}{末1}\longrightarrow
+ \Node{vlist $n_2$}{末2}\longrightarrow
+ \Node{vlist $n_3$}{頭1}\longrightarrow
+ \Node{vlist $n_4$}{頭2}\longrightarrow
+ \Node{vlist $n_5$}{中}
+\]
+に置き換えておく.
+\end{enumerate}
+\item \LuaTeX の行分割処理を普通に行う.
+\item 行分割の結果に従って,$g_i$,~$r_i$達を適切に置換する.
+
+例えば行分割の結果
+\begin{gather*}
+ (\text{other nodes})\longrightarrow
+ \Node{glue $g_0$}{}\longrightarrow \Node{whatsit~$W$}{元からある}\longrightarrow \Node{rule $r_1$}{}
+  \tag{行A}\\
+\noalign{\hrulefill 行の境目\hrulefill}
+\Node{rule $r_2$}{}\longrightarrow 
+ \Node{glue $g_2$}{}\longrightarrow (\text{other nodes})\tag{行B}
+\end{gather*}
+のようになったとしよう.
+\begin{enumerate}
+\item 処理は段落の上の行から順番に行われる.行Aの処理がまわってきたとしよう.
+
+\item 行Aの先頭から順番に眺めていく.すると「whatsit~$W$由来」のノード,$g_0$,~$W$,~$r_1$が見つかり,
+行Aはここで終わっている.
+
+まず,行Aのhboxの中身からwhatsit~$W$を消去(リストから取り除くだけで,$W$のメモリを解放するわけではない)する.
+$g_0$,~($W$,)~$r_1$というノードの並びは,「行末1グループ」$n_1$に対応しているので,
+$g_0$,~$r_1$を行Aから除去・メモリ解放し,代わりに$n_1$を行Aの中身に追加する.
+
+\item 次に行Bの処理にうつる.行Aでルビの処理は完了していない(2パーツのルビなのにまだ1パーツ目しか使っていないからである)ので,
+「whatsit~$W$由来」のノードがいくつか残っているはずである.
+
+案の定,$r_2$,~$g_2$というノード列が見つかった.これは「行頭1グループ」$n_3$に対応しているので,
+$r_2$,~$g_2$を行Bから除去・メモリ解放し,代わりに$n_3$を行Bの中身に挿入する.
+
+\item これで2パーツとも使い切ったことになるので,
+隔離しておいた$W$を,(使われなかった$n_2$,~$n_4$,~$n_5$などと共に)メモリ解放する.結果として
+次のようになった:
+\begin{gather*}
+ (\text{other nodes})\longrightarrow
+ \Node{vlist $n_1$}{末1}\tag{行A}\\
+\noalign{\hrulefill 行の境目\hrulefill}
+\Node{vlist $n_3$}{頭1}\longrightarrow (\text{other nodes})\tag{行B}
+\end{gather*}
+\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)}\ 
+
+進入ありの例:\quad と\ruby{暁}{あかつき}の
+
+進入なしの例:\quad {\setkeys[ltj]{ruby}{mode=0}と\ruby{暁}{あかつき}の}
+
+突出ありの例:\quad {\setkeys[ltj]{ruby}{stretch={1}{2}{1}}\ruby{聴衆}{ちようしゆう}}
+
+突出なしの例:\quad {\setkeys[ltj]{ruby}{stretch={0}{2}{0}}\ruby{聴衆}{ちようしゆう}}
+
+
+\paragraph{mode}\ 
+
+mode 00xx: {\setkeys[ltj]{ruby}{mode=1}は\ruby{美}{うつく}しい
+  \quad は\ruby{聴衆}{ちようしゆう}と}
+
+mode 01xx: {\setkeys[ltj]{ruby}{mode=5}は\ruby{美}{うつく}しい
+  \quad は\ruby{聴衆}{ちようしゆう}と}
+
+mode 10xx: {\setkeys[ltj]{ruby}{mode=9}は\ruby{美}{うつく}しい
+  \quad は\ruby{聴衆}{ちようしゆう}と}
+
+mode 11xx: {\setkeys[ltj]{ruby}{mode=13}%
+\ltjsetparameter{rubypreintrusion={`立,0.5}, 
+  rubypostintrusion={`立,0.5}}は\ruby{暁}{あかつき}立\quad
+は\ruby{聴衆}{ちようしゆう}立\par}
+↑ここでは「立」には0.5字がけを許容
+
+中付きでは00xx, 11xxを選択して下さい
+
+
+\section{いくつかの例}
+\def\rubytest{\ruby{黄金橋}{ゴールデンゲートブリッジ}\relax}
+
+\setbox0=\vbox{\hsize=22\zw%
+ああああ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ}
+%\directlua{ltj.ext_show_node_list(tex.box[0], '? ', print)}
+\fbox{\box0}
+
+\def\rubytest{\ruby{国府津}{こうづ}\relax}
+% グループルビ
+
+\setbox0=\vbox{\hsize=18\zw%
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ}
+%\directlua{ltj.ext_show_node_list(tex.box[0], '? ', print)}
+\fbox{\box0}
+
+\def\rubytest{\ruby{●●|◆}{◆◆◆◆◆◆|●●●}\relax}
+\setbox0=\vbox{\hsize=19\zw%
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこイ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこウ
+あ\rubytest いうえおかきくけこエ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ}
+%\directlua{ltj.ext_show_node_list(tex.box[0], '? ', print)}
+\fbox{\box0}
+
+\def\rubytest{\ruby{●●|□}{◆◆◆|●●●}\relax}
+\setbox0=\vbox{\hsize=19\zw%
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこイ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこウ
+あ\rubytest いうえおかきくけこエ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ
+あ\rubytest いうえおかきくけこ}
+%\directlua{ltj.ext_show_node_list(tex.box[0], '? ', print)}
+\fbox{\box0}
+
+\def\rubytest{\ruby{異|様}{い|よう}\relax}
+\setbox0=\vbox{\hsize=19\zw%
+あ\rubytest いうえくけこ
+あ\rubytest いうえくけこ
+あ\rubytest いうえくけこ
+あ\rubytest いうえくけこイ
+あ\rubytest いうえおかきくけこ}
+%\directlua{ltj.ext_show_node_list(tex.box[0], '? ', print)}
+\fbox{\box0}
+
+\def\rubytest{\ruby{□|■|□}{■■|□□□|■■}\relax}
+
+\setbox0=\vbox{\hsize=23\zw%
+あ\rubytest いうえおかきくけこうえおかきくけこ
+あ\rubytest いうえおかきくけこうえおかきくけこ
+あ\rubytest いう□おかきくけこうえおかきくけこ
+あ\rubytest いう□おかきくけこうえおかきくけこ
+あ\rubytest いう□おかきくけこうえおかきくけこ
+あ\rubytest いうえおかきくけこ}
+\fbox{\box0}
+
+\begin{description}
+\def\sample{又\ruby{承}{うけたまわ}る\quad \ruby{疎}{そ}\quad は\ruby{俄}{にわか}勉強
+  \quad 後\ruby{俄}{にわか}勉強}
+\item[標準] \sample
+\item[肩つき] {\kata\sample}
+\item[拡張肩つき]{\ltjsetruby{ekata}\sample}
+\end{description}
+
+
+\newpage
+\section{jlreq 20120403の例}
+
+\obeylines\newcommand*{\噂}{\CID{7642}}
+%================================== 横組
+\paragraph{3.3.1節}\ \par
+3.49 \ruby{君|子}{くん|し}は\ruby{和}{わ}して\ruby{同}{どう}ぜず
+3.50 \ruby{人}{ひと}に\ruby{誨}{おし}えて\ruby{倦}{う}まず\par% 中付き
+3.51 \ruby{鬼}{き}\ruby{門}{もん}の\ruby{方}{ほう}\ruby{角}{がく}を% モノ中付き
+\ruby{凝}{ぎょう}\ruby{視}{し}する
+3.52 \ruby{鬼|門}{き|もん}の\ruby{方|角}{ほう|がく}を% 熟語(グループ扱い)
+\ruby{凝|視}{ぎよう|し}する
+3.53 \ruby{茅場町}{かやばちよう}\quad\ruby{茅場}{かやば}\ruby{町}{ちよう}\par% 複合語
+3.54 \ruby{紫陽花}{あじさい}\quad\ruby{坩堝}{るつぼ}\quad\ruby{田舎}{いなか}\par% 熟字訓 
+3.55 \ruby{模型}{モデル}\quad\ruby{顧客}{クライアント}\quad% カタカナルビ
+\ruby{境界面}{インターフエース}\quad\ruby{避難所}{アジール}
+3.56 \ruby{編集者}{editor}\quad \ruby{editor}{エディター}% 欧文
+
+\paragraph{3.3.3節}\ \par
+3.58 に\ruby{幟}{のぼり}を\quad{\kata に\ruby{幟}{のぼり}を}%
+  \quad \ruby{韋}{い}\ruby{編}{へん}\ruby{三}{さん}\ruby{絶}{ぜつ}
+3.59 に\ruby{幟}{\kanjiseries{mc}\selectfont のぼり}を
+3.60 \ruby{韋}{い}\ruby{編}{へん}\ruby{三}{さん}\ruby{絶}{ぜつ}\quad%
+{\setkeys[ltj]{ruby}{rubysize=0.4}% 文字サイズ変更
+  \ruby{韋}{い}\ruby{編}{へん}\ruby{三}{さん}\ruby{絶}{ぜつ}}
+
+\paragraph{3.3.4節}\ \par
+3.61図(両側ルビ)はまだ未サポートにより省略
+
+\paragraph{3.3.5節 モノルビ}\ \par
+3.62 の\ruby{葯}{やく}に
+3.63 版面の\ruby{地}{ち}に\quad{\kata 版面の\ruby{地}{ち}に}
+3.64× {\kata 版面の\ruby{地}{ち}に}(まだ縦組み未サポート)\par% 横組み肩つき
+3.65 の\ruby{砦}{とりで}に\quad{\kata の\ruby{砦}{とりで}に}
+{\kata 3.66上 の\ruby{旬}{しゆん}に\quad 後\ruby{旬}{しゆん}に
+3.66下 の\ruby{旬}{しゆん}又\quad 後\ruby{旬}{しゆん}又\par}
+
+
+\paragraph{3.3.6節 グループルビ}\ \par
+3.67 は\ruby{冊子体}{コーデツクス}と
+3.68 \ruby{模型}{モデル}\quad \ruby{利用許諾}{ライセンス}
+3.69 {\setkeys[ltj]{ruby}{stretchruby=010}% 両端を揃える流儀
+  \ruby{模型}{モデル}\quad \ruby{利用許諾}{ライセンス}}
+3.70 \ruby{なげきの聖母像}{ビエタ}←自動調整
+3.71 \ruby{顧客}{クライアント}\quad \ruby{境界面}{インターフエース}
+3.72 {\setkeys[ltj]{ruby}{stretch=010, stretchhead = 010, stretchend = 010}% はみ出さない流儀
+  \ruby{顧客}{クライアント}\quad \ruby{境界面}{インターフエース}}
+
+
+\paragraph{3.3.7節 熟語ルビ}\ \par
+3.73左 \ruby{杞|憂}{き|ゆう}\quad \ruby{畏|怖}{い|ふ}
+3.73右 {\kata\ruby{杞|憂}{き|ゆう}\quad \ruby{畏|怖}{い|ふ}}
+3.74 の\ruby{流|儀}{りゆう|ぎ}を\quad の\ruby{無|常}{む|じよう}を\quad%
+の\ruby{成|就}{じよう|じゆ}を\quad
+3.74 の\ruby{紋|章}{もん|しよう}を\quad の\ruby{象|徴}{しよう|ちよう}を
+
+要調整3.75 {\kata の\ruby{流|儀}{りゆう|ぎ}を\quad の\ruby{無|常}{む|じよう}を\quad%
+の\ruby{成|就}{じよう|じゆ}を\quad 
+要調整3.75 の\ruby{紋|章}{もん|しよう}を\quad の\ruby{象|徴}{しよう|ちよう}を}
+
+3.76× の\ruby{流}{りゆう}\ruby{儀}{ぎ}を\quad の\ruby{無}{む}\ruby{常}{じよう}を\quad%
+
+3.77\ {\notalign%この図では揃えない
+\hbox{\vrule\vbox{\hsize=5\zw ……の\ruby{流|儀}{りゆう|ぎ}を}\vrule}\quad%
+% ↑どうすれば改行されるのだろう
+\hbox{\vrule\vbox{\hsize=5\zw ……の\ruby{無|常}{む|じよう}を}\vrule}}
+
+\paragraph{3.3.8節 ルビはみ出し}\ \par
+3.78 \ruby{人}{ひと}は\ruby{死}{し}して\ruby{名}{な}を\ruby{残}{のこ}す\par% ベタ
+要調整3.79 漢字の部首には\ruby{偏}{へん}・\ruby{冠}{かんむり}・\ruby{脚}{きやく}・%
+\ruby{旁}{つくり}がある
+要調整3.79 漢字の部首には\ruby{偏}{へん},\ruby{冠}{かんむり},\ruby{脚}{きやく},%
+\ruby{旁}{つくり}がある
+3.79 この\ruby{\噂}{うわさ}の好きな人は%
+\ruby{懐}{ふところ}ぐあいもよく、\ruby{檜}{ひのき}を
+3.80 漢字の部首には「\ruby{偏}{へん}」「\ruby{冠}{かんむり}」「\ruby{脚}{きやく}」%
+「\ruby{旁}{つくり}」がある
+3.80 この\ruby{\噂}{うわさ}好きな人は\ruby{懐}{ふところ}具合もよく、\ruby{檜}{ひのき}材を
+要調整3.81× に\ruby{暁}{あかつき}の\kern-1\zw の\ruby{趣}{おもむき}を
+3.82 に\ruby{暁}{あかつき}の\ruby{趣}{おもむき}を
+{%
+  \ltjsetparameter{rubypostintrusion={`好,0.5}}
+  \ltjsetparameter{rubypostintrusion={`具,0.5}}
+  \ltjsetparameter{rubypostintrusion={`材,0.5}}
+3,83 この\ruby{\噂}{うわさ}の好きな人は\ruby{懐}{ふところ}ぐあいもよく、\ruby{檜}{ひのき}を
+3.83 この\ruby{\噂}{うわさ}好きな人は\ruby{懐}{ふところ}具合もよく、\ruby{檜}{ひのき}材を
+}
+{%
+  \makeatletter\count@="3040\loop\relax\ifnum \count@<"30A0%
+  \ltjsetparameter{rubypreintrusion={\the\count@,0}, %
+    rubypostintrusion={\the\count@,0}}%
+  \advance\count@1 \repeat
+3.84 この\ruby{\噂}{うわさ}の好きな人は\ruby{懐}{ふところ}ぐあいもよく、\ruby{檜}{ひのき}を
+3.84 この\ruby{\噂}{うわさ}好きな人は\ruby{懐}{ふところ}具合もよく、\ruby{檜}{ひのき}材を
+}
+要調整3.85\ {\notalign%この図では揃えない
+\hbox{\vrule\vbox{\hsize=15\zw% なぜ行末形にならない!
+あああああああああああの\penalty10000\ruby{徑}{こみち}をあああああああ%
+ああああああああああああああああいの\ruby{徑}{こみち}}\vrule}}
+
+3.86\ %
+\hbox{\vrule\vbox{\hsize=15\zw
+ああああああああああああの\ruby{徑}{こみち}をあああああああ%
+あああああああ\ruby{徑}{こみち}を}\vrule}
+
+
+\newpage
+{\Large 要調整}
+
+\paragraph{F.1--2節}\ \par
+
+F.01 {\kata\ruby{治|癒}{ち|ゆ}\quad\ruby{模|索}{も|さく}\quad%
+\ruby{遷|移}{せん|い}\quad\ruby{混|沌}{こん|とん}}
+F.01中 \ruby{治|癒}{ち|ゆ}\quad\ruby{模|索}{も|さく}\quad%
+\ruby{遷|移}{せん|い}\quad\ruby{混|沌}{こん|とん}
+
+F.02 \ruby{橋|頭|堡}{きよう|とう|ほ}
+
+F.03 {\kata\ruby{凝|視}{ぎよう|し}\quad\ruby{調|理|師}{ちよう|り|し}\quad%
+\ruby{思|春|期}{し|しゆん|き}\quad\ruby{管|状|花}{かん|じよう|か}\quad%
+\ruby{蒸|気|船}{じよう|き|せん}}
+F.03 \ruby{凝|視}{ぎよう|し}\quad\ruby{調|理|師}{ちよう|り|し}\quad%
+\ruby{思|春|期}{し|しゆん|き}\quad\ruby{管|状|花}{かん|じよう|か}\quad%
+\ruby{蒸|気|船}{じよう|き|せん}
+
+F.04 {\kata\ruby{未|熟}{み|じゆく}\quad\ruby{法|華|経}{ほ|け|きよう}\quad%
+\ruby{顕|微|鏡}{けん|び|きよう}\quad\ruby{課|徴|金}{か|ちよう|きん}\quad%
+\ruby{古|戦|場}{こ|せん|じよう}}
+F.04 \ruby{未|熟}{み|じゆく}\quad\ruby{法|華|経}{ほ|け|きよう}\quad%
+\ruby{顕|微|鏡}{けん|び|きよう}\quad\ruby{課|徴|金}{か|ちよう|きん}\quad%
+\ruby{古|戦|場}{こ|せん|じよう}
+
+F.05 の\ruby{峻|別}{しゆん|べつ}は
+
+F.06以降は未チェック
+\end{document}