OSDN Git Service

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