--- /dev/null
+--
+-- ltj-ruby.lua
+--
+luatexbase.provides_module({
+ name = 'luatexja.ruby',
+ date = '2014/02/06',
+ description = 'Ruby',
+})
+module('luatexja.ruby', package.seeall)
+local err, warn, info, log = luatexbase.errwarinf(_NAME)
+
+luatexja.load_module('stack'); local ltjs = luatexja.stack
+
+local Dnode = node.direct or node
+
+local nullfunc = function(n) return n end
+local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
+local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
+
+local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
+local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
+local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
+local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
+local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
+local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
+local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
+
+local node_new = Dnode.new
+local node_remove = luatexja.Dnode_remove -- Dnode.remove
+local node_next = (Dnode ~= node) and Dnode.getnext or node.next
+local node_copy, node_free, node_tail = Dnode.copy, Dnode.free, Dnode.tail
+local has_attr, set_attr = Dnode.has_attribute, Dnode.set_attribute
+local insert_before, insert_after = Dnode.insert_before, Dnode.insert_after
+
+local 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_penalty = node.id('penalty')
+local id_glue_spec = node.id('glue_spec')
+local sid_user = node.subtype('user_defined')
+local ltjs_get_stack_table = luatexja.stack.get_stack_table
+local id_pbox_w = 258 -- cluster which consists of a whatsit
+
+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_id = luatexbase.attributes['ltj@kcat4'] -- uniq id
+local attr_ruby = luatexbase.attributes['ltj@rubyattr']
+-- ルビ内部処理用,以下のようにノードによって使われ方が異なる
+-- * (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
+
+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
+local RUBY_POST = luatexja.userid_table.RUBY_POST
+
+----------------------------------------------------------------
+-- TeX interface 0
+----------------------------------------------------------------
+if Dnode ~= node then
+ function cpbox() return node_copy(Dnode.getbox(0)) end
+else
+ function cpbox() return node.copy(tex.box[0]) end
+end
+
+
+----------------------------------------------------------------
+-- 補助関数群 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
+ coef[i], coef[j] = coef[j], coef[i]; 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 回目の情報が使える
+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での設定が使われる.
+-- ルビ文字を格納しているボックスでの設定ではない!
+local function concat(f, b)
+ if f then
+ if b then
+ local h = getlist(f)
+ setfield(node_tail(h), 'next', getlist(b))
+ setfield(f, 'head', nil); node_free(f)
+ setfield(b, 'head', nil); node_free(b)
+ return Dnode.hpack(luatexja.jfmglue.main(h,false))
+ else
+ return f
+ end
+ elseif b then
+ return b
+ else
+ local h = node_new(id_hlist)
+ setfield(h, 'subtype', 0)
+ setfield(h, 'width', 0)
+ setfield(h, 'height', 0)
+ setfield(h, 'depth', 0)
+ setfield(h, 'glue_set', 0)
+ setfield(h, 'glue_order', 0)
+ setfield(h, 'head', nil)
+ return h
+ end
+end
+
+local function expand_3bits(num)
+ local t = {}; local a = num
+ for i = 1, 10 do
+ t[i] = a%8; a = floor(a/8)
+ end
+ return t
+end
+----------------------------------------------------------------
+-- 補助関数群 2
+----------------------------------------------------------------
+
+-- box の中身のノードは再利用される
+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
+ -- 先頭の空白を挿入
+ 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
+end
+
+
+----------------------------------------------------------------
+-- TeX interface
+----------------------------------------------------------------
+
+-- rtlr: ルビ部分のボックスたち r1, r2, ...
+-- rtlp: 親文字 のボックスたち p1, p2, ...
+local function texiface_low(rst, rtlr, rtlp)
+ local w = node_new(id_whatsit, sid_user)
+ setfield(w, 'type', 110); setfield(w, 'user_id', RUBY_PRE)
+ local wv = node_new(id_whatsit, sid_user)
+ setfield(w, 'value', to_node(wv))
+ setfield(wv, 'type', 100)
+ 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_stretch, rst.stretch)
+ set_attr(wv, attr_ruby_mode, rst.mode)
+ local n = wv
+ for i = 1, #rtlr do
+ _, n = insert_after(wv, n, rtlr[i])
+ _, n = insert_after(wv, n, rtlp[i])
+ end
+ -- w.value: (whatsit) .. r1 .. p1 .. r2 .. p2
+ Dnode.write(w); return w,wv
+end
+
+-- rst: table
+function texiface(rst, rtlr, rtlp)
+ if #rtlr ~= #rtlp then
+ for i=1, #rtlr do node_free(rtlr[i]) end
+ for i=1, #rtlp do node_free(rtlp[i]) end
+ luatexja.base.package_error('luatexja-ruby',
+ 'Group count mismatch between the ruby and\n' ..
+ 'the body (' .. #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
+ 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
+ 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
+----------------------------------------------------------------
+
+-- r, p の中身のノードは再利用される
+local function enlarge_parent(r, p, ppre, pmid, ppost, mapre, mapost, intmode)
+ -- r: ルビ部分の格納された box,p: 同,親文字
+ local rwidth = getfield(r, 'width')
+ local sumprot = rwidth - getfield(p, 'width') -- >0
+ local pre_intrusion, post_intrusion
+ if intmode == 0 then -- とりあえず組んでから決める
+ p = enlarge(p, rwidth, ppre, pmid, ppost, 0, 0)
+ pre_intrusion = min(mapre, round(ppre*getfield(p, 'glue_set')*65536))
+ post_intrusion = min(mapost, round(ppost*getfield(p, 'glue_set')*65536))
+ elseif intmode == 1 then
+ pre_intrusion = min(mapre, sumprot);
+ post_intrusion = min(mapost, max(sumprot-pre_intrusion, 0))
+ p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion)
+ elseif intmode == 2 then
+ post_intrusion = min(mapost, sumprot);
+ pre_intrusion = min(mapre, max(sumprot-post_intrusion, 0))
+ p = enlarge(p, rwidth, 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, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion)
+ pre_intrusion = min(mapre, pre_intrusion + round(ppre*getfield(p, 'glue_set')*65536))
+ post_intrusion = min(mapost, post_intrusion + round(ppost*getfield(p, 'glue_set')*65536))
+ end
+ setfield(r, 'shift', -pre_intrusion)
+ local rwidth = rwidth - pre_intrusion - post_intrusion
+ setfield(r, 'width', rwidth)
+ setfield(p, 'width', rwidth)
+ local ps = getfield(getlist(p), 'spec')
+ setfield(ps, 'width', getfield(ps, '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 getfield(r, 'width') > getfield(p, 'width') then -- change the width of p
+ r, p, post_intrusion = enlarge_parent(r, p, ppre, pmid, ppost, mapre, mapost, intmode)
+ elseif getfield(r, 'width') < getfield(p, 'width') then -- change the width of r
+ r = enlarge(r, getfield(p, 'width'), rpre, rmid, rpost, 0, 0)
+ post_intrusion = 0
+ local need_repack = false
+ -- margin が大きくなりすぎた時の処理
+ if round(rpre*getfield(r, 'glue_set')*65536) > max_margin then
+ local ps = getfield(getlist(r), 'spec'); need_repack = true
+ setfield(ps, 'width', max_margin)
+ setfield(ps, 'stretch', 1) -- 全く伸縮しないのも困る
+ end
+ if round(rpost*getfield(r, 'glue_set')*65536) > max_margin then
+ local ps = getfield(node_tail(getlist(r)), 'spec'); need_repack = true
+ setfield(ps, 'width', max_margin)
+ setfield(ps, 'stretch', 1) -- 全く伸縮しないのも困る
+ end
+ if need_repack then
+ local rt = r
+ r = Dnode.hpack(getlist(r), getfield(r, 'width'), 'exactly')
+ 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)
+ set_attr(a, attr_ruby, post_intrusion)
+ return a, getfield(r, 'width'), post_intrusion
+end
+
+
+-- High-level routine in pre_linebreak_filter
+local 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 wv = getfield(w, 'value')
+ 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
+
+ -- node list 展開・行末形の計算
+ local nt, nta, ntb = wv, nil, nil -- nt*: node temp
+ for i = 1, cmp do
+ nt = node_next(nt); rb[i] = nt; nta = concat(nta, node_copy(nt))
+ nt = node_next(nt); 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)
+
+ -- 行頭形の計算
+ 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]
+ = 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)
+ 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], 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)
+
+ -- 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 を解く
+ return coef
+end
+
+-- ノード追加
+local function pre_low_app_node(head, w, cmp, coef, ht, dp)
+ -- メインの node list 更新
+ local nt, ntb = node_new(id_glue), node_new(id_glue_spec)
+ setfield(ntb, 'width', coef[1][2*cmp+2])
+ setfield(ntb, 'stretch_order', 0); setfield(ntb, 'stretch', 0)
+ setfield(ntb, 'shrink_order', 0); setfield(ntb, 'shrink', 0)
+ setfield(nt, 'subtype', 0); setfield(nt, 'spec', ntb)
+ set_attr(nt, attr_ruby, 1); set_attr(w, attr_ruby, 2)
+ head = insert_before(head, w, nt)
+ nt = w
+ for i = 1, cmp do
+ -- rule
+ local nta = node_new(id_rule);
+ setfield(nta, 'width', coef[i*2][2*cmp+2])
+ setfield(nta, 'height', ht); setfield(nta, 'depth', dp)
+ setfield(nta, 'subtype', 0)
+ insert_after(head, nt, nta)
+ set_attr(nta, attr_ruby, 2*i+1)
+ -- glue
+ nt = node_new(id_glue)
+ local ntb = node_new(id_glue_spec);
+ setfield(ntb, 'width', coef[i*2+1][2*cmp+2])
+ setfield(ntb, 'stretch_order', 0); setfield(ntb, 'stretch', 0)
+ setfield(ntb, 'shrink_order', 0); setfield(ntb, 'shrink', 0)
+ setfield(nt, 'subtype', 0); setfield(nt, 'spec', ntb)
+ set_attr(nt, attr_ruby, 2*i+2)
+ insert_after(head, nta, nt)
+ end
+ tex.setattribute(attr_ruby, -0x7FFFFFFF)
+ setfield(w, 'user_id', RUBY_POST)
+ return head, 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
+ 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)
+ end
+ else
+ n = node_next(n)
+ end
+ end
+ return to_node(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 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
+
+ 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, 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
+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
+ 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
+ setfield(h, 'head', post_lown(rs, rw, cmp, getlist(h)))
+ end
+ 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)
+ 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
+ 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)
+ end
+ end
+ 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)
+luatexbase.add_to_callback('hpack_filter', post_high_hbox, 'ltj.ruby.post_hbox', 101)
+
+
+----------------------------------------------------------------
+-- for jfmglue callbacks
+----------------------------------------------------------------
+do
+ local RIPRE = luatexja.stack_table_index.RIPRE
+ local function whatsit_callback(Np, lp, Nq, bsl)
+ if Np.nuc then return Np
+ elseif getfield(lp, 'user_id') == RUBY_PRE then
+ Np.first, Np.nuc, Np.last = lp, lp, lp
+ local lpv = getfield(lp, 'value')
+ local x = node_next(node_next(lpv))
+ Np.last_char = luatexja.jfmglue.check_box_high(Np, getlist(x), nil)
+ if Nq.id ~=id_pbox_w and type(Nq.char)=='number' then
+ -- Nq is a JAchar
+ if has_attr(lpv, attr_ruby_maxprep) < 0 then -- auto
+ local p = round((ltjs.table_current_stack[RIPRE + Nq.char] or 0)
+ *has_attr(lpv, attr_ruby))
+ if has_attr(lpv, attr_ruby_mode)%2 == 0 then -- intrusion 無効
+ p = 0
+ 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)
+ end
+ return Np
+ end
+ end
+ luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
+ "luatexja.ruby.np_info", 1)
+end
+
+do
+ local RIPOST = luatexja.stack_table_index.RIPOST
+ local function whatsit_after_callback(s, Nq, Np, bsl)
+ if not s and getfield(Nq.nuc, 'user_id') == RUBY_PRE then
+ local nqnv = getfield(Nq.nuc, 'value')
+ local x = node_next(node_next(nqnv))
+ for i = 2, getfield(nqnv, 'value') do x = node_next(node_next(x)) end
+ 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 rm%2 == 0 then -- intrusion 無効
+ p = 0
+ end
+ if rm%4 >= 2 then
+ local q = has_attr(nqnv, attr_ruby_maxprep)
+ if q < p then p = q
+ elseif q > p then
+ set_attr(nqnv, attr_ruby_maxprep, p)
+ end
+ end
+ set_attr(nqnv, attr_ruby_maxpostp, p)
+ end
+ Np.prev_ruby = has_attr(getfield(Nq.nuc, 'value'), attr_ruby_id)
+ -- 前のクラスタがルビであったことのフラグ
+ else -- 直前が文字以外
+ local nqnv = getfield(Nq.nuc, 'value')
+ 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
+ return s
+ end
+ end
+ luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
+ "luatexja.ruby.np_info_after", 1)
+end
+
--- /dev/null
+%#! luajitlatex
+%%% a test of ruby.
+\documentclass[b5paper,10pt]{ltjsarticle}
+\usepackage{fontspec,luacode,luatexja-otf,amsmath}
+\usepackage{unicode-math}
+\setmathfont{XITS Math}
+\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
+%\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}
+%\fontsize{13.19873}{15}\selectfont%
+
+\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>}+:ルビ全角単位.
+\verb+<char_code>+ の文字に掛けられる前のルビ文字列の最大長.
+\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{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{実装の大まかな方法}
+次の例で説明する.
+\begin{LTXexample}
+……を\ruby{流|暢}{りゆう|ちよう}に……
+\end{LTXexample}
+
+\begin{enumerate}
+\item \verb|\ruby|コマンド自体は,一旦次のnode listを値とするwhatsit~$W$を作って,
+現在の水平リストへと挿入する(必要ならば\verb|\leavevmode|も実行):
+\[
+ \Node{whatsit $w$}{value: 2}\longrightarrow
+ \Node{hlist $s_1$}{「りゆう」}\longrightarrow
+ \Node{hlist $p_1$}{「流」}\longrightarrow
+ \Node{hlist $s_2$}{「ちよう」}\longrightarrow
+ \Node{hlist $p_2$}{「暢」}
+\]
+ここで,最初の$w$の値2は,ルビが2つのパーツ「\ruby{流}{りゆう}」「\ruby{暢}{ちよう}」からなっていることを
+表している.この値を$\mathit{cmp}$とおこう.
+$s_i$達の中の文字は既にルビの大きさである.
+\item \LuaTeX-jaの和文処理グルー挿入処理において,
+whatsit~$W$は,
+「先頭が『流』,最後が『暢』であるような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}
+
+\medskip
+
+例えばこの場合,$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は親文字全角の幅,進入量込).
+$n_1$から$n_5$まで計5本の方程式が立つが,これらはGau\ss の消去法で解くことができて
+$g_i$,~$r_i$達の幅が求まる.
+\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}
+
+
+\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.53右の説明はこれでいいのか?
+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=10\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% なぜ行末形にならない!
+あああああああああああああの\ruby{徑}{こみち}\penalty-1000をあああああああ%
+あああああああああああああああああいの\ruby{徑}{こみち}ああ}\vrule}}
+
+3.86\ %
+\hbox{\vrule\vbox{\hsize=15\zw
+ああああああああああああの\ruby{徑}{こみち}をあああああああ%
+あああああああ\ruby{徑}{こみち}を}\vrule}
+
+3.87\ %
+\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}