OSDN Git Service

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