OSDN Git Service

5806472b8b0ef91509abb13811c077699f805c88
[luatex-ja/luatexja.git] / src / ltj-ruby.lua
1 --
2 -- ltj-ruby.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.ruby',
6   date = '2014/02/06',
7   description = 'Ruby',
8 })
9 module('luatexja.ruby', package.seeall)
10 local err, warn, info, log = luatexbase.errwarinf(_NAME)
11
12 luatexja.load_module('stack');     local ltjs = luatexja.stack
13
14 local Dnode = node.direct or node
15
16 local nullfunc = function(n) return n end
17 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
18 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
19
20 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
21 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
22 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
23 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
24 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
25 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
26 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
27
28 local node_new = Dnode.new
29 local node_remove = luatexja.Dnode_remove -- Dnode.remove
30 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
31 local node_copy, node_free, node_tail = Dnode.copy, Dnode.free, Dnode.tail
32 local has_attr, set_attr = Dnode.has_attribute, Dnode.set_attribute
33 local insert_before, insert_after = Dnode.insert_before, Dnode.insert_after
34
35 local id_hlist = node.id('hlist')
36 local id_vlist = node.id('vlist')
37 local id_rule = node.id('rule')
38 local id_whatsit = node.id('whatsit')
39 local id_glue = node.id('glue')
40 local id_kern = node.id('kern')
41 local id_glue_spec = node.id('glue_spec')
42 local sid_user = node.subtype('user_defined')
43 local ltjs_get_stack_table = luatexja.stack.get_stack_table
44 local id_pbox_w = 258 -- cluster which consists of a whatsit
45
46 local attr_icflag = luatexbase.attributes['ltj@icflag']
47 -- ルビ処理用の attribute は他のやつの流用なので注意!
48 -- 進入許容量 (sp)
49 local attr_ruby_maxprep = luatexbase.attributes['ltj@charclass']
50 local attr_ruby_maxpostp = luatexbase.attributes['ltj@kcat0']
51 local attr_ruby_maxmargin = luatexbase.attributes['ltj@kcat1']
52 local attr_ruby_stretch = luatexbase.attributes['ltj@kcat2']
53 local attr_ruby_mode = luatexbase.attributes['ltj@kcat3']
54 local attr_ruby = luatexbase.attributes['ltj@rubyattr']
55 -- ルビ内部処理用
56 -- jfmglue 中では「2つ前のクラスタもルビ」のフラグ(true = 1)
57 -- (whatsit).value node ではルビ全角の値(sp単位)
58 -- 行分割前後では,「何番目のルビ関連ノード」か
59
60 local round, floor = tex.round, math.floor
61 local min, max = math.min, math.max
62
63 local FROM_JFM       = luatexja.icflag_table.FROM_JFM
64 local PROCESSED      = luatexja.icflag_table.PROCESSED
65 local KANJI_SKIP     = luatexja.icflag_table.KANJI_SKIP
66 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
67 local XKANJI_SKIP    = luatexja.icflag_table.XKANJI_SKIP
68 local XKANJI_SKIP_JFM= luatexja.icflag_table.XKANJI_SKIP_JFM
69
70 luatexja.userid_table.RUBY_PRE = luatexbase.newuserwhatsitid('ruby_pre',  'luatexja')
71 luatexja.userid_table.RUBY_POST = luatexbase.newuserwhatsitid('ruby_post',  'luatexja')
72 local RUBY_PRE  = luatexja.userid_table.RUBY_PRE
73 local RUBY_POST = luatexja.userid_table.RUBY_POST
74
75 ----------------------------------------------------------------
76 -- TeX interface 0
77 ----------------------------------------------------------------
78 if Dnode ~= node then
79    function cpbox() return node_copy(Dnode.getbox(0)) end
80 else
81    function cpbox() return node.copy(tex.box[0]) end
82 end
83
84
85 ----------------------------------------------------------------
86 -- 補助関数群 1
87 ----------------------------------------------------------------
88
89 local function gauss(coef)
90    -- #coef 式,#coef 変数の連立1次方程式系を掃きだし法で解く.
91    local deg = #coef
92    for i = 1, deg do
93       if coef[i][i]==0 then
94          for j = i+1, deg do 
95             if coef[j][i]~=0 then
96                coef[i], coef[j] = coef[j], coef[i]; break
97             end
98          end
99       end
100       for j = 1,deg do 
101          local d = coef[i][i];
102          if j~=i then
103             local e = coef[j][i]
104             for k = 1, deg+1 do coef[j][k] = coef[j][k] - e*coef[i][k]/d end
105          else
106             for k = 1, deg+1 do coef[i][k] = coef[i][k]/d end
107          end
108       end
109    end
110 end
111
112 -- 実行回数 + ルビ中身 から uniq_id を作る関数
113 -- 未実装.これを使えば 2 回目以降の組版に 1 回目の情報が使える
114
115
116 -- concatenation of boxes: reusing nodes
117 -- ルビ組版が行われている段落/hboxでの設定が使われる.
118 -- ルビ文字を格納しているボックスでの設定ではない!
119 local function concat(f, b)
120    if f then
121       if b then
122          local h = getlist(f)
123          setfield(node_tail(h), 'next', getlist(b))
124          setfield(f, 'head', nil); node_free(f)
125          setfield(b, 'head', nil); node_free(b)
126          return Dnode.hpack(luatexja.jfmglue.main(h,false))
127       else 
128          return f
129       end
130    elseif b then
131       return b
132    else
133       local h = node_new(id_hlist)
134       setfield(h, 'subtype', 0)
135       setfield(h, 'width', 0)
136       setfield(h, 'height', 0)
137       setfield(h, 'depth', 0)
138       setfield(h, 'glue_set', 0)
139       setfield(h, 'glue_order', 0)
140       setfield(h, 'head', nil)
141       return h
142    end
143 end
144
145 local function expand_3bits(num)
146    local t = {}; local a = num
147    for i = 1, 10 do
148       t[i] = a%8; a = floor(a/8)
149    end
150    return t
151 end
152 ----------------------------------------------------------------
153 -- 補助関数群 2
154 ----------------------------------------------------------------
155
156 -- box の中身のノードは再利用される
157 local function enlarge(box, new_width, pre, middle, post, prenw, postnw)
158    -- pre, middle, post: 伸縮比率
159    -- prenw, postnw: 前後の自然長 (sp)
160    local h = getlist(box); local hh, hd = getfield(box, 'height'), getfield(box, 'depth')
161    local hx = h
162    while hx do
163       if has_attr(hx, attr_icflag) == KANJI_SKIP
164          or has_attr(hx, attr_icflag) == KANJI_SKIP_JFM
165          or has_attr(hx, attr_icflag) == XKANJI_SKIP
166          or has_attr(hx, attr_icflag) == XKANJI_SKIP_JFM
167          or has_attr(hx, attr_icflag) == FROM_JFM then
168          -- この 5 種類の空白をのばす
169             if getid(hx) == id_kern then
170                local k = node_new(id_glue)
171                local ks = node_new(id_glue_spec)
172                setfield(ks, 'width', getfield(hx, 'kern'))
173                setfield(ks, 'stretch_order', 2)
174                setfield(ks, 'stretch', round(middle*65536))
175                setfield(ks, 'shrink_order', 0)
176                setfield(ks, 'shrink', 0)
177                setfield(k, 'subtype', 0)
178                setfield(k, 'spec', ks)
179                h = insert_after(h, hx, k);
180                h = node_remove(h, hx); node_free(hx); hx = k
181          else -- glue
182             local ks = node_copy(getfield(hx, 'spec'))
183             setfield(ks, 'stretch_order', 2)
184             setfield(ks, 'stretch', round(middle*65536))
185             setfield(ks, 'shrink_order', 0)
186             setfield(ks, 'shrink', 0)
187             setfield(hx, 'spec', ks)
188          end
189       end
190       hx = node_next(hx)
191    end
192    -- 先頭の空白を挿入
193    local k = node_new(id_glue);
194    local ks = node_new(id_glue_spec)
195    setfield(ks, 'width', prenw)
196    setfield(ks, 'stretch_order', 2)
197    setfield(ks, 'stretch', round(pre*65536))
198    setfield(ks, 'shrink_order', 0)
199    setfield(ks, 'shrink', 0)
200    setfield(k, 'subtype', 0)
201    setfield(k, 'spec', ks)
202    h = insert_before(h, h, k);
203    -- 末尾の空白を挿入
204    local k = node_new(id_glue);
205    local ks = node_new(id_glue_spec);
206    setfield(ks, 'width', postnw)
207    setfield(ks, 'stretch_order', 2)
208    setfield(ks, 'stretch', round(post*65536))
209    setfield(ks, 'shrink_order', 0)
210    setfield(ks, 'shrink', 0)
211    setfield(k, 'subtype', 0)
212    setfield(k, 'spec', ks)
213    insert_after(h, node_tail(h), k);
214    -- hpack
215    setfield(box, 'head', nil); node_free(box)
216    box = Dnode.hpack(h, new_width, 'exactly')
217    setfield(box, 'height', hh)
218    setfield(box, 'depth', hd)
219    return box
220 end
221
222 ----------------------------------------------------------------
223 -- TeX interface
224 ----------------------------------------------------------------
225
226 -- rtlr: ルビ部分のボックスたち r1, r2, ...
227 -- rtlp: 親文字 のボックスたち p1, p2, ...
228 local function texiface_low(rst, rtlr, rtlp)
229    local w = node_new(id_whatsit, sid_user)
230    setfield(w, 'type', 110); setfield(w, 'user_id', RUBY_PRE)
231    local wv = node_new(id_whatsit, sid_user)
232    setfield(w, 'value', to_node(wv))
233    setfield(wv, 'type', 100)
234    setfield(wv, 'value', floor(#rtlr))
235    set_attr(wv, attr_ruby, rst.rubyzw)
236    set_attr(wv, attr_ruby_maxmargin, rst.maxmargin)
237    set_attr(wv, attr_ruby_maxprep, rst.intrusionpre)
238    set_attr(wv, attr_ruby_maxpostp, rst.intrusionpost)
239    set_attr(wv, attr_ruby_stretch, rst.stretch)
240    set_attr(wv, attr_ruby_mode, rst.mode)
241    local n = wv
242    for i = 1, #rtlr do
243       _, n = insert_after(wv, n, rtlr[i])
244       _, n = insert_after(wv, n, rtlp[i])
245    end
246    -- w.value: (whatsit) .. r1 .. p1 .. r2 .. p2
247    Dnode.write(w)
248 end
249
250 -- rst: table
251 function texiface(rst, rtlr, rtlp)
252    if #rtlr ~= #rtlp then
253       for i,v in pairs(rtlr) do node_free(v) end
254       for i,v in pairs(rtlp) do node_free(v) end
255       luatexja.base.package_error('luatexja-ruby',
256                                   'Group count mismatch between the ruby and\n' ..
257                                      'the body (' .. #rtlr .. ' != ' .. #rtlp .. ').',
258                                   '')
259    else
260       local f = true
261       for i = 1,#rtlr do
262          if getfield(rtlr[i], 'width') > getfield(rtlp[i], 'width') then f = false; break end
263       end
264       if f then -- モノルビ * n
265          local r,p = {true}, {true}
266          for i = 1,#rtlr do
267             r[1] = rtlr[i]; p[1] = rtlp[i]; texiface_low(rst, r, p)
268          end
269       else
270          texiface_low(rst, rtlr, rtlp)
271       end
272    end
273 end
274
275
276 ----------------------------------------------------------------
277 -- pre_line_break
278 ----------------------------------------------------------------
279
280 -- r, p の中身のノードは再利用される
281 local function enlarge_parent(r, p, ppre, pmid, ppost, mapre, mapost, intmode)
282    -- r: ルビ部分の格納された box,p: 同,親文字
283    local rwidth = getfield(r, 'width')
284    local sumprot = rwidth - getfield(p, 'width') -- >0
285    local pre_intrusion, post_intrusion
286    if intmode == 0 then --  とりあえず組んでから決める
287       p = enlarge(p, rwidth, ppre, pmid, ppost, 0, 0) 
288       pre_intrusion  = min(mapre, round(ppre*getfield(p, 'glue_set')*65536))
289       post_intrusion = min(mapost, round(ppost*getfield(p, 'glue_set')*65536))
290    elseif intmode == 1 then
291       pre_intrusion = min(mapre, sumprot); 
292       post_intrusion = min(mapost, max(sumprot-pre_intrusion, 0))
293       p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion)
294    elseif intmode == 2 then
295       post_intrusion = min(mapost, sumprot); 
296       pre_intrusion = min(mapre, max(sumprot-post_intrusion, 0))
297       p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion) 
298    else --  intmode == 3
299       local n = min(mapre, mapost)*2
300       if n < sumprot then
301          pre_intrusion = n/2; post_intrusion = n/2
302       else
303          pre_intrusion = floor(sumprot/2); post_intrusion = sumprot - pre_intrusion
304       end
305       p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion) 
306       pre_intrusion = min(mapre, pre_intrusion + round(ppre*getfield(p, 'glue_set')*65536))
307       post_intrusion = min(mapost, post_intrusion + round(ppost*getfield(p, 'glue_set')*65536))
308    end
309    setfield(r, 'shift', -pre_intrusion)
310    local rwidth = rwidth - pre_intrusion - post_intrusion
311    setfield(r, 'width', rwidth)
312    setfield(p, 'width', rwidth)
313    local ps = getfield(getlist(p), 'spec')
314    setfield(ps, 'width', getfield(ps, 'width') - pre_intrusion)
315    return r, p, post_intrusion
316 end
317
318 -- ルビボックスの生成(単一グループ)
319 -- returned value: <new box>, <ruby width>, <post_intrusion>
320 local max_margin
321 local function new_ruby_box(r, p, ppre, pmid, ppost, 
322                             rpre, rmid, rpost, mapre, mapost, intmode)
323    local post_intrusion = 0
324    if getfield(r, 'width') > getfield(p, 'width') then  -- change the width of p
325       r, p, post_intrusion  = enlarge_parent(r, p, ppre, pmid, ppost, mapre, mapost, intmode)
326    elseif getfield(r, 'width') < getfield(p, 'width') then -- change the width of r
327       r = enlarge(r, getfield(p, 'width'), rpre, rmid, rpost, 0, 0) 
328       post_intrusion = 0
329       local need_repack = false
330       -- margin が大きくなりすぎた時の処理
331       if round(rpre*getfield(r, 'glue_set')*65536) > max_margin then
332          local ps = getfield(getlist(r), 'spec'); need_repack = true
333          setfield(ps, 'width', max_margin)
334          setfield(ps, 'stretch', 1) -- 全く伸縮しないのも困る
335       end
336       if round(rpost*getfield(r, 'glue_set')*65536) > max_margin then
337          local ps = getfield(node_tail(getlist(r)), 'spec'); need_repack = true
338          setfield(ps, 'width', max_margin)
339          setfield(ps, 'stretch', 1) -- 全く伸縮しないのも困る
340       end
341       if need_repack then
342          local rt = r
343          r = Dnode.hpack(getlist(r), getfield(r, 'width'), 'exactly')
344          setfield(rt, 'head', nil); node_free(rt);
345       end
346    end
347    local a = node_new(id_rule)
348    setfield(a, 'width', 0)
349    setfield(a, 'height', 0)
350    setfield(a, 'depth', 0)
351    insert_after(r, r, a); insert_after(r, a, p)
352    setfield(p, 'next', nil)
353    a = Dnode.vpack(r)
354    setfield(a, 'height', getfield(p, 'height'))
355    setfield(a, 'depth', getfield(p, 'depth'))
356    setfield(a, 'shift', -(getfield(r, 'height')+getfield(r, 'depth')))
357    return a, getfield(r, 'width'), post_intrusion
358 end
359
360
361 -- High-level routine in pre_linebreak_filter
362 local max_post_intrusion_backup
363 local max_allow_pre, max_allow_post
364
365
366 -- 中付き熟語ルビ,cmp containers
367 -- 「文字の構成を考えた」やつはどうしよう
368 local function pre_low_cal_box(w, cmp)
369    local rb = {}
370    local pb = {}
371    local kf = {}
372    -- kf[i] : container 1--i からなる行末形
373    -- kf[cmp+i] : container i--cmp からなる行頭形
374    -- kf[2cmp+1] : 行中形
375    local wv = getfield(w, 'value')
376    local mdt -- nt*: node temp
377    local coef = {} -- 連立一次方程式の拡大係数行列
378    local rtb = expand_3bits(has_attr(wv, attr_ruby_stretch))
379    local rtc = expand_3bits(has_attr(wv, attr_ruby_mode))
380    local intmode = floor(has_attr(wv, attr_ruby_mode)/4)%4
381
382    -- node list 展開・行末形の計算
383    local nt, nta, ntb = wv, nil, nil -- nt*: node temp
384    for i = 1, cmp do
385       nt = node_next(nt); rb[i] = nt; nta = concat(nta, node_copy(nt))
386       nt = node_next(nt); pb[i] = nt; ntb = concat(ntb, node_copy(nt))
387       coef[i] = {}
388       for j = 1, 2*i do coef[i][j] = 1 end
389       for j = 2*i+1, 2*cmp+1 do coef[i][j] = 0 end
390       kf[i], coef[i][2*cmp+2]
391          = new_ruby_box(node_copy(nta), node_copy(ntb), 
392                         rtb[6], rtb[5], rtb[4], rtc[10], rtc[9], rtc[8], 
393                         max_allow_pre, 0, intmode)
394    end
395    node_free(nta); node_free(ntb)
396
397    -- 行頭形の計算
398    nta = nil; ntb = nil
399    for i = cmp,1,-1 do
400       coef[cmp+i] = {}
401       for j = 1, 2*i-1 do coef[cmp+i][j] = 0 end
402       for j = 2*i, 2*cmp+1 do coef[cmp+i][j] = 1 end
403       nta = concat(node_copy(rb[i]), nta); ntb = concat(node_copy(pb[i]), ntb)
404       kf[cmp+i], coef[cmp+i][2*cmp+2], mdt
405          = new_ruby_box(node_copy(nta), node_copy(ntb), 
406                         rtb[9], rtb[8], rtb[7], rtc[10], rtc[9], rtc[8], 
407                         0, max_allow_post, intmode)
408       if max_post_intrusion_backup < mdt then max_post_intrusion_backup = mdt end
409    end
410
411    -- ここで,nta, ntb には全 container を連結した box が入っているので
412    -- それを使って行中形を計算する.
413    coef[2*cmp+1] = {}
414    for j = 1, 2*cmp+1 do coef[2*cmp+1][j] = 1 end
415    kf[2*cmp+1], coef[2*cmp+1][2*cmp+2], mdt
416       = new_ruby_box(nta, ntb,
417                      rtb[3], rtb[2], rtb[1], rtc[10], rtc[9], rtc[8], 
418                      max_allow_pre, max_allow_post, intmode)
419    if max_post_intrusion_backup < mdt then max_post_intrusion_backup = mdt end
420
421    -- w.value の node list 更新.
422    local nt = wv
423    Dnode.flush_list(node_next(wv))
424    for i = 1, 2*cmp+1 do setfield(nt, 'next', kf[i]); nt = kf[i]  end
425
426    gauss(coef) -- 掃きだし法で連立方程式形 coef を解く
427    return coef
428 end
429
430 -- ノード追加
431 local function pre_low_app_node(head, w, cmp, coef, ht, dp)
432    -- メインの node list 更新
433    local nt, ntb = node_new(id_glue), node_new(id_glue_spec)
434    setfield(ntb, 'width', coef[1][2*cmp+2])
435    setfield(ntb, 'stretch_order', 0); setfield(ntb, 'stretch', 0)
436    setfield(ntb, 'shrink_order', 0); setfield(ntb, 'shrink', 0)
437    setfield(nt, 'subtype', 0); setfield(nt, 'spec', ntb)
438    set_attr(nt, attr_ruby, 1); set_attr(w, attr_ruby, 2)
439    head = insert_before(head, w, nt)
440    nt = w
441    for i = 1, cmp do
442       -- rule
443       local nta = node_new(id_rule); 
444       setfield(nta, 'width', coef[i*2][2*cmp+2])
445       setfield(nta, 'height', ht); setfield(nta, 'depth', dp)
446       setfield(nta, 'subtype', 0)
447       insert_after(head, nt, nta)
448       set_attr(nta, attr_ruby, 2*i+1)
449       -- glue
450       nt = node_new(id_glue)
451       local ntb = node_new(id_glue_spec);
452       setfield(ntb, 'width', coef[i*2+1][2*cmp+2])
453       setfield(ntb, 'stretch_order', 0); setfield(ntb, 'stretch', 0)
454       setfield(ntb, 'shrink_order', 0); setfield(ntb, 'shrink', 0)
455       setfield(nt, 'subtype', 0); setfield(nt, 'spec', ntb)
456       set_attr(nt, attr_ruby, 2*i+2)
457       insert_after(head, nta, nt)
458    end
459    tex.setattribute(attr_ruby, -0x7FFFFFFF)
460    setfield(w, 'user_id', RUBY_POST)
461    return head, node_next(nt)
462 end
463
464 local function pre_high(ahead)
465    if not ahead then return ahead end
466    local head = to_direct(ahead)
467    max_post_intrusion_backup = 0
468    local n = head
469    while n do
470       if getid(n) == id_whatsit then
471          if getsubtype(n) == sid_user and getfield(n, 'user_id') == RUBY_PRE then
472             local nv = getfield(n, 'value')
473             max_allow_pre = has_attr(nv, attr_ruby_maxprep) or 0
474             if has_attr(n, attr_ruby) == 1 then 
475                -- 直前のルビで intrusion がおこる可能性あり.安全策をとる.
476                max_allow_pre = max(0, max_allow_pre - max_post_intrusion_backup)
477             end
478             max_post_intrusion_backup = 0
479             max_allow_post = has_attr(nv, attr_ruby_maxpostp) or 0
480             max_margin = has_attr(nv, attr_ruby_maxmargin) or 0
481             local coef = pre_low_cal_box(n, getfield(nv, 'value'))
482             local s = node_tail(nv) --ルビ文字
483             head, n = pre_low_app_node(
484                head, n, getfield(nv, 'value'), coef, 
485                getfield(s, 'height'), getfield(s, 'depth')
486             )
487          else 
488             n = node_next(n)
489          end
490       else
491          n = node_next(n)
492       end
493    end
494    return to_node(head)
495 end 
496 luatexbase.add_to_callback('pre_linebreak_filter', pre_high, 'ltj.ruby.pre', 100)
497 luatexbase.add_to_callback('hpack_filter', pre_high, 'ltj.ruby.pre', 100)
498
499 ----------------------------------------------------------------
500 -- post_line_break
501 ----------------------------------------------------------------
502
503 local function post_lown(rs, rw, ch)
504 -- ch: the head of `current' hlist
505    if #rs ==0 or not rw then return ch end
506    local hn = has_attr(rs[1], attr_ruby)
507    local fn = has_attr(rs[#rs], attr_ruby)
508    local cmp = getfield(getfield(rw, 'value'), 'value')
509    if hn==1 then 
510       if fn==2*cmp+2 then
511          hn = node_tail(getfield(rw, 'value'))
512          node_remove(getfield(rw, 'value'), hn)
513          insert_after(ch, rs[#rs], hn)
514          set_attr(hn, attr_icflag,  PROCESSED)
515       else
516          local deg = (fn-1)/2
517          hn = getfield(rw, 'value')
518          for i = 1, deg do hn = node_next(hn) end; 
519          node_remove(getfield(rw, 'value'), hn)
520          setfield(hn, 'next', nil)
521          insert_after(ch, rs[#rs], hn)
522          set_attr(hn, attr_icflag,  PROCESSED)
523      end
524    else
525       local deg = (hn-1)/2; 
526       if deg == 1 then deg = 2 end
527       hn = getfield(rw, 'value'); for i = 1, cmp+deg-1 do hn = node_next(hn) end
528       -- -1 is needed except the case hn = 3, 
529       --   because a ending-line form is removed already from the list
530       node_remove(getfield(rw, 'value'), hn); setfield(hn, 'next', nil)
531       insert_after(ch, rs[#rs], hn)
532       set_attr(hn, attr_icflag,  PROCESSED)
533    end
534    for i = 1,#rs do 
535       ch = node_remove(ch, rs[i]); node_free(rs[i]) 
536    end
537    -- cleanup
538    if fn >= 2*cmp+1 then
539       --setfield(hn, 'next', nil); 
540       node_free(rw); 
541    end
542    return ch;
543 end
544
545
546 local function post_high_break(head)
547    local h = to_direct(head); 
548    local rs = {};  -- rs: sequence of ruby_nodes, 
549    local rw = nil; -- rw: main whatsit
550    while h do 
551       if getid(h) == id_hlist then
552          local ha = getlist(h); rs = {}
553         while ha do
554             local hai = getid(ha)
555             local i = (((hai == id_glue and getsubtype(ha)==0) 
556                            or (hai == id_rule and getsubtype(ha)==0)
557                            or (hai == id_whatsit and getsubtype(ha)==sid_user 
558                                   and getfield(ha, 'user_id')==RUBY_POST))
559                           and has_attr(ha, attr_ruby)) or 0
560             if i==1 then 
561                setfield(h, 'head', post_lown(rs, rw, getlist(h))); rs = {}; rw = nil
562                rs[1] = ha; ha = node_next(ha)
563             elseif i>=3 then 
564                rs[#rs+1] = ha; ha = node_next(ha)
565             elseif i==2 then 
566                rw = ha
567                local hb, hc =  node_remove(getlist(h), rw)
568                setfield(h, 'head', hb); ha = hc
569             else
570                ha = node_next(ha)
571             end
572          end
573          setfield(h, 'head', post_lown(rs, rw, getlist(h)))
574       end
575       h = node_next(h)
576    end
577
578    return to_node(head)
579 end 
580
581 local function post_high_hbox(ahead)
582    local ha = to_direct(ahead); local head = ha
583    local rs = {};  -- rs: sequence of ruby_nodes, 
584    local rw = nil; -- rw: main whatsit
585    while ha do
586       local hai = getid(ha)
587       local i = (((hai == id_glue and getsubtype(ha)==0) 
588                      or (hai == id_rule and getsubtype(ha)==0)
589                      or (hai == id_whatsit and getsubtype(ha)==sid_user 
590                             and getfield(ha, 'user_id', RUBY_POST)))
591                     and has_attr(ha, attr_ruby)) or 0
592       if i==1 then 
593          head = post_lown(rs, rw, head); rs = {}; rw = nil
594          table.insert(rs, ha); ha = node_next(ha)
595       elseif i>=3 then 
596          table.insert(rs, ha); ha = node_next(ha)
597       elseif i==2 then 
598          rw = ha; head, ha = node_remove(head, rw)
599       else
600          ha = node_next(ha)
601       end
602    end
603    return to_node(post_lown(rs, rw, head))
604 end
605
606 luatexbase.add_to_callback('post_linebreak_filter', post_high_break, 'ltj.ruby.post_break', 100)
607 luatexbase.add_to_callback('hpack_filter', post_high_hbox, 'ltj.ruby.post_hbox', 101)
608
609
610 ----------------------------------------------------------------
611 -- for jfmglue callbacks
612 ----------------------------------------------------------------
613 do
614    local RIPRE  = luatexja.stack_table_index.RIPRE
615    local function whatsit_callback(Np, lp, Nq, bsl) 
616       if Np.nuc then return Np 
617       elseif getfield(lp, 'user_id') == RUBY_PRE then
618          Np.first, Np.nuc, Np.last = lp, lp, lp
619          local lpv = getfield(lp, 'value')
620          local x = node_next(node_next(lpv))
621          Np.last_char = luatexja.jfmglue.check_box_high(Np, getlist(x), nil)
622          if Nq.id ~=id_pbox_w and  type(Nq.char)=='number' then
623             if has_attr(lpv, attr_ruby_maxprep) < 0 then -- auto
624                local p = round((ltjs.table_current_stack[RIPRE + Nq.char] or 0)
625                                   *has_attr(lpv, attr_ruby))
626                if has_attr(lpv, attr_ruby_mode)%2 == 0 then -- intrusion 無効
627                   p = 0
628                end
629                set_attr(lpv, attr_ruby_maxprep, p)
630             end
631             if Nq.prev_ruby then 
632                set_attr(lp, attr_ruby, 1)
633             end
634          else
635             set_attr(getfield(lp, 'value'), attr_ruby_maxprep, 0)
636          end
637          return Np
638       end
639    end
640    luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
641                               "luatexja.ruby.np_info", 1)
642 end
643
644 do
645    local RIPOST = luatexja.stack_table_index.RIPOST
646    local function whatsit_after_callback(s, Nq, Np, bsl)
647       if not s and  getfield(Nq.nuc, 'user_id') == RUBY_PRE then
648          local nqnv = getfield(Nq.nuc, 'value')
649          local x =  node_next(node_next(nqnv))
650          for i = 2, getfield(nqnv, 'value') do x = node_next(node_next(x)) end
651          Nq.last_char = luatexja.jfmglue.check_box_high(Nq, getlist(x), nil)
652          luatexja.jfmglue.after_hlist(Nq)
653          if Np and Np.id ~=id_pbox_w and type(Np.char)=='number' then
654             if has_attr(nqnv, attr_ruby_maxpostp) < 0 then -- auto
655                local p = round((ltjs.table_current_stack[RIPOST + Np.char] or 0)
656                                   *has_attr(nqnv, attr_ruby))
657                if has_attr(nqnv, attr_ruby_mode)%2 == 0 then -- intrusion 無効
658                   p = 0
659                end
660                if has_attr(nqnv, attr_ruby_mode)%4 >= 2 then
661                   local q = has_attr(nqnv, attr_ruby_maxprep)
662                   if q < p then p = q
663                   elseif q > p then
664                      set_attr(nqnv, attr_ruby_maxprep, p)
665                   end
666                end
667                set_attr(nqnv, attr_ruby_maxpostp, p)
668             end
669             Np.prev_ruby = true -- 前のクラスタがルビであったことのフラグ
670          else -- 直前が文字以外なら intrusion なし
671             local nqnv = getfield(Nq.nuc, 'value')
672             set_attr(nqnv, attr_ruby_maxpostp, 0)
673             if has_attr(nqnv, attr_ruby_mode)%4 >= 2 then
674                set_attr(nqnv, attr_ruby_maxprep, 0)
675             end
676             if Np and Np.id == id_pbox_w then
677                set_attr(getfield(Np.nuc, 'value'), attr_ruby_maxprep, 0)
678             end
679          end
680          return true
681       else
682          return s
683       end
684    end
685    luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
686                               "luatexja.ruby.np_info_after", 1)
687 end
688
689