4 luatexbase.provides_module({
5 name = 'luatexja.ruby',
9 module('luatexja.ruby', package.seeall)
10 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12 luatexja.load_module('stack'); local ltjs = luatexja.stack
14 local Dnode = node.direct or node
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
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
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
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
47 local attr_icflag = luatexbase.attributes['ltj@icflag']
48 -- ルビ処理用の attribute は他のやつの流用なので注意!
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']
66 local round, floor = tex.round, math.floor
67 local min, max = math.min, math.max
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
74 ----------------------------------------------------------------
76 ----------------------------------------------------------------
78 function cpbox() return node_copy(Dnode.getbox(0)) end
80 function cpbox() return node.copy(tex.box[0]) end
84 ----------------------------------------------------------------
86 ----------------------------------------------------------------
88 local function gauss(coef)
89 -- #coef 式,#coef 変数の連立1次方程式系を掃きだし法で解く.
95 coef[i], coef[j] = coef[j], coef[i]; break
100 local d = coef[i][i];
103 for k = 1, deg+1 do coef[j][k] = coef[j][k] - e*coef[i][k]/d end
105 for k = 1, deg+1 do coef[i][k] = coef[i][k]/d end
111 -- 実行回数 + ルビ中身 から uniq_id を作る関数
112 -- 未実装.これを使えば 2 回目以降の組版に 1 回目の情報が使える
113 old_break_info = {} -- public, 前 run 時の分割情報
117 make_uniq_id = function (w)
118 exec_count = exec_count + 1
123 -- concatenation of boxes: reusing nodes
124 -- ルビ組版が行われている段落/hboxでの設定が使われる.
125 -- ルビ文字を格納しているボックスでの設定ではない!
126 local function concat(f, b)
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))
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)
152 local function expand_3bits(num)
153 local t = {}; local a = num
155 t[i] = a%8; a = floor(a/8)
159 ----------------------------------------------------------------
161 ----------------------------------------------------------------
163 -- box の中身のノードは再利用される
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')
179 local hic = has_attr(hx, attr_icflag)
180 if (hic == KANJI_SKIP) or (hic == KANJI_SKIP_JFM)
181 or (hic == XKANJI_SKIP) or (hic == XKANJI_SKIP_JFM)
182 or ((hic<=FROM_JFM+2) and (hic>=FROM_JFM-2)) then
184 if getid(hx) == id_kern then
185 local k = node_new(id_glue)
186 local ks = node_new(id_glue_spec)
187 setfield(ks, 'width', getfield(hx, 'kern'))
188 setfield(ks, 'stretch_order', 2)
189 setfield(ks, 'stretch', round(middle*65536))
190 setfield(ks, 'shrink_order', 0); setfield(ks, 'shrink', 0)
191 setfield(k, 'subtype', 0); setfield(k, 'spec', ks)
192 h = insert_after(h, hx, k);
193 h = node_remove(h, hx); node_free(hx); hx = k
195 local old_spec = getfield(hx, 'spec')
196 local ks = node_copy(old_spec)
197 setfield(ks, 'stretch_order', 2)
198 setfield(ks, 'stretch', round(middle*65536))
199 setfield(ks, 'shrink_order', 0); setfield(ks, 'shrink', 0)
200 setfield(hx, 'spec', ks)
201 -- decrease old_spec's reference count
202 local b = node_new(id_glue)
203 setfield(b, 'spec', old_spec); node_free(b)
209 local k = node_new(id_glue);
210 local ks = node_new(id_glue_spec)
211 setfield(ks, 'width', prenw)
212 setfield(ks, 'stretch_order', 2); setfield(ks, 'stretch', round(pre*65536))
213 setfield(ks, 'shrink_order', 0); setfield(ks, 'shrink', 0)
214 setfield(k, 'subtype', 0); setfield(k, 'spec', ks)
215 h = insert_before(h, h, k);
217 local k = node_new(id_glue);
218 local ks = node_new(id_glue_spec);
219 setfield(ks, 'width', postnw)
220 setfield(ks, 'stretch_order', 2); setfield(ks, 'stretch', round(post*65536))
221 setfield(ks, 'shrink_order', 0); setfield(ks, 'shrink', 0)
222 setfield(k, 'subtype', 0);setfield(k, 'spec', ks)
223 insert_after(h, node_tail(h), k);
225 setfield(box, 'head', nil); node_free(box)
226 box = Dnode.hpack(h, new_width, 'exactly')
227 setfield(box, 'height', hh)
228 setfield(box, 'depth', hd)
234 ----------------------------------------------------------------
236 ----------------------------------------------------------------
238 -- rtlr: ルビ部分のボックスたち r1, r2, ...
239 -- rtlp: 親文字 のボックスたち p1, p2, ...
240 local function texiface_low(rst, rtlr, rtlp)
241 local w = node_new(id_whatsit, sid_user)
242 setfield(w, 'type', 110); setfield(w, 'user_id', RUBY_PRE)
243 local wv = node_new(id_whatsit, sid_user)
244 setfield(w, 'value', to_node(wv))
245 setfield(wv, 'type', 100)
246 setfield(wv, 'value', floor(#rtlr))
247 set_attr(wv, attr_ruby, rst.rubyzw)
248 set_attr(wv, attr_ruby_maxmargin, rst.maxmargin)
249 set_attr(wv, attr_ruby_maxprep, rst.intrusionpre)
250 set_attr(wv, attr_ruby_maxpostp, rst.intrusionpost)
251 set_attr(wv, attr_ruby_stretch, rst.stretch)
252 set_attr(wv, attr_ruby_mode, rst.mode)
255 _, n = insert_after(wv, n, rtlr[i])
256 _, n = insert_after(wv, n, rtlp[i])
258 -- w.value: (whatsit) .. r1 .. p1 .. r2 .. p2
259 Dnode.write(w); return w,wv
263 function texiface(rst, rtlr, rtlp)
264 if #rtlr ~= #rtlp then
265 for i=1, #rtlr do node_free(rtlr[i]) end
266 for i=1, #rtlp do node_free(rtlp[i]) end
267 luatexja.base.package_error('luatexja-ruby',
268 'Group count mismatch between the ruby and\n' ..
269 'the body (' .. #rtlr .. ' != ' .. #rtlp .. ').',
274 if getfield(rtlr[i], 'width') > getfield(rtlp[i], 'width') then
278 if f then -- モノルビ * n
279 local r,p = {true}, {true}
281 r[1] = rtlr[i]; p[1] = rtlp[i]; texiface_low(rst, r, p)
284 local w, wv = texiface_low(rst, rtlr, rtlp)
285 local id = make_uniq_id(w)
286 set_attr(wv, attr_ruby_id, id)
291 ----------------------------------------------------------------
293 ----------------------------------------------------------------
295 -- r, p の中身のノードは再利用される
296 local function enlarge_parent(r, p, ppre, pmid, ppost, mapre, mapost, intmode)
297 -- r: ルビ部分の格納された box,p: 同,親文字
298 local rwidth = getfield(r, 'width')
299 local sumprot = rwidth - getfield(p, 'width') -- >0
300 local pre_intrusion, post_intrusion
301 if intmode == 0 then -- とりあえず組んでから決める
302 p = enlarge(p, rwidth, ppre, pmid, ppost, 0, 0)
303 pre_intrusion = min(mapre, round(ppre*getfield(p, 'glue_set')*65536))
304 post_intrusion = min(mapost, round(ppost*getfield(p, 'glue_set')*65536))
305 elseif intmode == 1 then
306 pre_intrusion = min(mapre, sumprot);
307 post_intrusion = min(mapost, max(sumprot-pre_intrusion, 0))
308 p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion)
309 elseif intmode == 2 then
310 post_intrusion = min(mapost, sumprot);
311 pre_intrusion = min(mapre, max(sumprot-post_intrusion, 0))
312 p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion)
314 local n = min(mapre, mapost)*2
316 pre_intrusion = n/2; post_intrusion = n/2
318 pre_intrusion = floor(sumprot/2); post_intrusion = sumprot - pre_intrusion
320 p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion)
321 pre_intrusion = min(mapre, pre_intrusion + round(ppre*getfield(p, 'glue_set')*65536))
322 post_intrusion = min(mapost, post_intrusion + round(ppost*getfield(p, 'glue_set')*65536))
324 setfield(r, 'shift', -pre_intrusion)
325 local rwidth = rwidth - pre_intrusion - post_intrusion
326 setfield(r, 'width', rwidth)
327 setfield(p, 'width', rwidth)
328 local ps = getfield(getlist(p), 'spec')
329 setfield(ps, 'width', getfield(ps, 'width') - pre_intrusion)
330 return r, p, post_intrusion
334 -- returned value: <new box>, <ruby width>, <post_intrusion>
336 local function new_ruby_box(r, p, ppre, pmid, ppost,
337 rpre, rmid, rpost, mapre, mapost, intmode)
338 local post_intrusion = 0
339 if getfield(r, 'width') > getfield(p, 'width') then -- change the width of p
340 r, p, post_intrusion = enlarge_parent(r, p, ppre, pmid, ppost, mapre, mapost, intmode)
341 elseif getfield(r, 'width') < getfield(p, 'width') then -- change the width of r
342 r = enlarge(r, getfield(p, 'width'), rpre, rmid, rpost, 0, 0)
344 local need_repack = false
345 -- margin が大きくなりすぎた時の処理
346 if round(rpre*getfield(r, 'glue_set')*65536) > max_margin then
347 local ps = getfield(getlist(r), 'spec'); need_repack = true
348 setfield(ps, 'width', max_margin)
349 setfield(ps, 'stretch', 1) -- 全く伸縮しないのも困る
351 if round(rpost*getfield(r, 'glue_set')*65536) > max_margin then
352 local ps = getfield(node_tail(getlist(r)), 'spec'); need_repack = true
353 setfield(ps, 'width', max_margin)
354 setfield(ps, 'stretch', 1) -- 全く伸縮しないのも困る
358 r = Dnode.hpack(getlist(r), getfield(r, 'width'), 'exactly')
359 setfield(rt, 'head', nil); node_free(rt);
362 local a = node_new(id_rule)
363 setfield(a, 'width', 0)
364 setfield(a, 'height', 0)
365 setfield(a, 'depth', 0)
366 insert_after(r, r, a); insert_after(r, a, p)
367 setfield(p, 'next', nil)
369 setfield(a, 'shift', 0)
370 set_attr(a, attr_ruby, post_intrusion)
371 return a, getfield(r, 'width'), post_intrusion
375 -- High-level routine in pre_linebreak_filter
376 local post_intrusion_backup
377 local max_allow_pre, max_allow_post
380 -- 中付き熟語ルビ,cmp containers
381 -- 「文字の構成を考えた」やつはどうしよう
382 local function pre_low_cal_box(w, cmp)
386 -- kf[i] : container 1--i からなる行末形
387 -- kf[cmp+i] : container i--cmp からなる行頭形
389 local wv = getfield(w, 'value')
390 local mdt -- nt*: node temp
391 local coef = {} -- 連立一次方程式の拡大係数行列
392 local rtb = expand_3bits(has_attr(wv, attr_ruby_stretch))
393 local rtc = expand_3bits(has_attr(wv, attr_ruby_mode))
394 local intmode = floor(has_attr(wv, attr_ruby_mode)/4)%4
396 -- node list 展開・行末形の計算
397 local nt, nta, ntb = wv, nil, nil -- nt*: node temp
399 nt = node_next(nt); rb[i] = nt; nta = concat(nta, node_copy(nt))
400 nt = node_next(nt); pb[i] = nt; ntb = concat(ntb, node_copy(nt))
402 for j = 1, 2*i do coef[i][j] = 1 end
403 for j = 2*i+1, 2*cmp+1 do coef[i][j] = 0 end
404 kf[i], coef[i][2*cmp+2]
405 = new_ruby_box(node_copy(nta), node_copy(ntb),
406 rtb[6], rtb[5], rtb[4], rtc[10], rtc[9], rtc[8],
407 max_allow_pre, 0, intmode)
409 node_free(nta); node_free(ntb)
412 local nta, ntb = nil, nil
415 for j = 1, 2*i-1 do coef[cmp+i][j] = 0 end
416 for j = 2*i, 2*cmp+1 do coef[cmp+i][j] = 1 end
417 nta = concat(node_copy(rb[i]), nta); ntb = concat(node_copy(pb[i]), ntb)
418 kf[cmp+i], coef[cmp+i][2*cmp+2]
419 = new_ruby_box(node_copy(nta), node_copy(ntb),
420 rtb[9], rtb[8], rtb[7], rtc[10], rtc[9], rtc[8],
421 0, max_allow_post, intmode)
424 -- ここで,nta, ntb には全 container を連結した box が入っているので
427 for j = 1, 2*cmp+1 do coef[2*cmp+1][j] = 1 end
428 kf[2*cmp+1], coef[2*cmp+1][2*cmp+2], post_intrusion_backup
429 = new_ruby_box(nta, ntb,
430 rtb[3], rtb[2], rtb[1], rtc[10], rtc[9], rtc[8],
431 max_allow_pre, max_allow_post, intmode)
433 -- w.value の node list 更新.
435 Dnode.flush_list(node_next(wv))
436 for i = 1, 2*cmp+1 do setfield(nt, 'next', kf[i]); nt = kf[i] end
438 gauss(coef) -- 掃きだし法で連立方程式形 coef を解く
443 local function first_whatsit(n) -- n 以後で最初の whatsit
444 for h in Dnode.traverse_id(id_whatsit, n) do
451 local function pre_low_app_node(head, w, cmp, coef, ht, dp)
453 local nt, ntb = node_new(id_glue), node_new(id_glue_spec)
454 setfield(ntb, 'width', coef[1][2*cmp+2])
455 setfield(ntb, 'stretch_order', 0); setfield(ntb, 'stretch', 0)
456 setfield(ntb, 'shrink_order', 0); setfield(ntb, 'shrink', 0)
457 setfield(nt, 'subtype', 0); setfield(nt, 'spec', ntb)
458 set_attr(nt, attr_ruby, 1); set_attr(w, attr_ruby, 2)
459 head = insert_before(head, w, nt)
463 local nta = node_new(id_rule);
464 setfield(nta, 'width', coef[i*2][2*cmp+2])
465 setfield(nta, 'height', ht); setfield(nta, 'depth', dp)
466 setfield(nta, 'subtype', 0)
467 insert_after(head, nt, nta)
468 set_attr(nta, attr_ruby, 2*i+1)
470 nt = node_new(id_glue)
471 local ntb = node_new(id_glue_spec);
472 setfield(ntb, 'width', coef[i*2+1][2*cmp+2])
473 setfield(ntb, 'stretch_order', 0); setfield(ntb, 'stretch', 0)
474 setfield(ntb, 'shrink_order', 0); setfield(ntb, 'shrink', 0)
475 setfield(nt, 'subtype', 0); setfield(nt, 'spec', ntb)
476 set_attr(nt, attr_ruby, 2*i+2)
477 insert_after(head, nta, nt)
479 tex.setattribute(attr_ruby, -0x7FFFFFFF)
480 setfield(w, 'user_id', RUBY_POST)
481 return head, first_whatsit(node_next(nt))
484 local function pre_high(ahead)
485 if not ahead then return ahead end
486 local head = to_direct(ahead)
487 post_intrusion_backup = 0
488 local n = first_whatsit(head)
490 if getsubtype(n) == sid_user and getfield(n, 'user_id') == RUBY_PRE then
491 local nv = getfield(n, 'value')
492 max_allow_pre = has_attr(nv, attr_ruby_maxprep) or 0
493 local atr = has_attr(n, attr_ruby) or 0
495 -- 直前のルビで intrusion がおこる可能性あり.
496 -- 前 run のデータが残っていればそれを使用,
497 -- そうでなければ行中形のデータを利用する
498 local op = old_break_info[atr] or post_intrusion_backup
499 max_allow_pre = max(0, max_allow_pre - op)
501 post_intrusion_backup = 0
502 max_allow_post = has_attr(nv, attr_ruby_maxpostp) or 0
503 max_margin = has_attr(nv, attr_ruby_maxmargin) or 0
504 local coef = pre_low_cal_box(n, getfield(nv, 'value'))
505 local s = node_tail(nv) --ルビ文字
506 head, n = pre_low_app_node(
507 head, n, getfield(nv, 'value'), coef,
508 getfield(s, 'height'), getfield(s, 'depth')
511 n = first_whatsit(node_next(n))
516 luatexbase.add_to_callback('pre_linebreak_filter', pre_high, 'ltj.ruby.pre', 100)
517 luatexbase.add_to_callback('hpack_filter', pre_high, 'ltj.ruby.pre', 100)
519 ----------------------------------------------------------------
521 ----------------------------------------------------------------
524 local function write_aux(wv, num)
525 local id = has_attr(wv, attr_ruby_id)
528 '\\write\\@mainaux{\\string\\directlua{luatexja.ruby.old_break_info['
529 .. tostring(id) .. ']=' .. num
534 post_lown = function (rs, rw, cmp, ch)
535 -- ch: the head of `current' hlist
536 if #rs ==0 or not rw then return ch end
537 local hn = has_attr(rs[1], attr_ruby)
538 local fn = has_attr(rs[#rs], attr_ruby)
539 local wv = getfield(rw, 'value')
542 local hn = node_tail(wv)
544 insert_after(ch, rs[#rs], hn)
545 set_attr(hn, attr_icflag, PROCESSED)
546 write_aux(wv, has_attr(hn, attr_ruby))-- 行中形
548 local deg, hn = (fn-1)/2, wv
549 for i = 1, deg do hn = node_next(hn) end;
551 setfield(hn, 'next', nil)
552 insert_after(ch, rs[#rs], hn)
553 set_attr(hn, attr_icflag, PROCESSED)
554 write_aux(wv, has_attr(hn, attr_ruby))
557 local deg, hn = max((hn-1)/2,2), wv
558 for i = 1, cmp+deg-1 do hn = node_next(hn) end
559 -- -1 is needed except the case hn = 3,
560 -- because a ending-line form is removed already from the list
561 node_remove(wv, hn); setfield(hn, 'next', nil)
562 insert_after(ch, rs[#rs], hn)
563 set_attr(hn, attr_icflag, PROCESSED)
564 if fn == 2*cmp-1 then
565 write_aux(wv, has_attr(hn, attr_ruby))
570 ch = node_remove(ch, ri); node_free(ri);
573 if fn >= 2*cmp+1 then node_free(rw) end
578 -- local function is_zero_parfillskip(h,n)
579 -- if getid(n)==id_glue then
580 -- if getsubtype(n)==15 then
581 -- local ns = getfield(n, 'spec')
582 -- local n_width = getfield(ns, 'width')
583 -- if getfield(h, 'glue_sign')==1
584 -- and getfield(h, 'glue_order') == getfield(ns, 'stretch_order') then
586 -- + round(getfield(h, 'glue_set')*getfield(ns, 'stretch'))
587 -- elseif getfield(h, 'glue_sign')==2
588 -- and getfield(h, 'glue_order') == getfield(ns, 'shrink_order') then
590 -- - round(getfield(h, 'glue_set')*getfield(n,s 'shrink'))
592 -- n = node_next(n) -- rightskip 未完
593 -- return (n_width <= 0)
600 local function post_high_break(head)
601 local rs = {} -- rs: sequence of ruby_nodes,
602 local rw = nil -- rw: main whatsit
603 local cmp = -2 -- dummy
604 for h in Dnode.traverse_id(id_hlist, to_direct(head)) do
605 for i = 1, #rs do rs[i] = nil end
606 local ha = getlist(h)
608 local hai = getid(ha)
609 local i = (((hai == id_glue and getsubtype(ha)==0)
610 or (hai == id_rule and getsubtype(ha)==0)
611 or (hai == id_whatsit and getsubtype(ha)==sid_user
612 and getfield(ha, 'user_id')==RUBY_POST))
613 and has_attr(ha, attr_ruby)) or 0
615 setfield(h, 'head', post_lown(rs, rw, cmp, getlist(h)))
616 for i = 2, #rs do rs[i] = nil end -- rs[1] is set by the next statement
617 rs[1], rw = ha, nil; ha = node_next(ha)
618 elseif i==2*cmp+2 then
619 -- local par_not_end = true
620 -- local hn = node_next(ha)
621 -- if hn and getid(hn)==id_penalty and getfield(hn, 'penalty')==10000 then
622 -- local hm = node_next(hn)
623 -- if is_zero_parfillskip(h,hm) then
624 -- par_not_end = false
627 -- if par_not_end then
628 rs[#rs+1] = ha; ha = node_next(ha)
630 -- setfield(h, 'head', node_remove(getlist(h), ha)); break
633 rs[#rs+1] = ha; ha = node_next(ha)
636 cmp = getfield(getfield(rw, 'value'), 'value')
637 local hb, hc = node_remove(getlist(h), rw)
638 setfield(h, 'head', hb); ha = hc
643 setfield(h, 'head', post_lown(rs, rw, cmp, getlist(h)))
648 local function post_high_hbox(ahead)
649 local ha = to_direct(ahead); local head = ha
650 local rs = {}; -- rs: sequence of ruby_nodes,
651 local rw = nil; -- rw: main whatsit
654 local hai = getid(ha)
655 local i = (((hai == id_glue and getsubtype(ha)==0)
656 or (hai == id_rule and getsubtype(ha)==0)
657 or (hai == id_whatsit and getsubtype(ha)==sid_user
658 and getfield(ha, 'user_id', RUBY_POST)))
659 and has_attr(ha, attr_ruby)) or 0
661 head = post_lown(rs, rw, cmp, head)
662 for i = 2, #rs do rs[i] = nil end -- rs[1] is set by the next statement
663 rs[1], rw = ha, nil; ha = node_next(ha)
665 rs[#rs+1] = ha; ha = node_next(ha)
668 cmp = getfield(getfield(rw, 'value'), 'value')
669 head, ha = node_remove(head, rw)
674 return to_node(post_lown(rs, rw, cmp, head))
677 luatexbase.add_to_callback('post_linebreak_filter', post_high_break, 'ltj.ruby.post_break', 100)
678 luatexbase.add_to_callback('hpack_filter', post_high_hbox, 'ltj.ruby.post_hbox', 101)
681 ----------------------------------------------------------------
682 -- for jfmglue callbacks
683 ----------------------------------------------------------------
685 local RIPRE = luatexja.stack_table_index.RIPRE
686 local function whatsit_callback(Np, lp, Nq, bsl)
687 if Np.nuc then return Np
688 elseif getfield(lp, 'user_id') == RUBY_PRE then
689 Np.first, Np.nuc, Np.last = lp, lp, lp
690 local lpv = getfield(lp, 'value')
691 local x = node_next(node_next(lpv))
692 Np.last_char = luatexja.jfmglue.check_box_high(Np, getlist(x), nil)
693 if Nq.id ~=id_pbox_w and type(Nq.char)=='number' then
695 if has_attr(lpv, attr_ruby_maxprep) < 0 then -- auto
696 local p = round((ltjs.table_current_stack[RIPRE + Nq.char] or 0)
697 *has_attr(lpv, attr_ruby))
698 if has_attr(lpv, attr_ruby_mode)%2 == 0 then -- intrusion 無効
701 set_attr(lpv, attr_ruby_maxprep, p)
704 set_attr(lp, attr_ruby, Nq.prev_ruby)
706 elseif has_attr(lpv, attr_ruby_maxprep) < 0 then -- auto
707 set_attr(lpv, attr_ruby_maxprep, 0)
712 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
713 "luatexja.ruby.np_info", 1)
717 local RIPOST = luatexja.stack_table_index.RIPOST
718 local function whatsit_after_callback(s, Nq, Np, bsl)
719 if not s and getfield(Nq.nuc, 'user_id') == RUBY_PRE then
720 local nqnv = getfield(Nq.nuc, 'value')
721 local x = node_next(node_next(nqnv))
722 for i = 2, getfield(nqnv, 'value') do x = node_next(node_next(x)) end
723 Nq.last_char = luatexja.jfmglue.check_box_high(Nq, getlist(x), nil)
724 luatexja.jfmglue.after_hlist(Nq)
725 if Np and Np.id ~=id_pbox_w and type(Np.char)=='number' then
727 local rm = has_attr(nqnv, attr_ruby_mode)
728 if has_attr(nqnv, attr_ruby_maxpostp) < 0 then -- auto
729 local p = round((ltjs.table_current_stack[RIPOST + Np.char] or 0)
730 *has_attr(nqnv, attr_ruby))
731 if rm%2 == 0 then -- intrusion 無効
735 local q = has_attr(nqnv, attr_ruby_maxprep)
738 set_attr(nqnv, attr_ruby_maxprep, p)
741 set_attr(nqnv, attr_ruby_maxpostp, p)
743 Np.prev_ruby = has_attr(getfield(Nq.nuc, 'value'), attr_ruby_id)
744 -- 前のクラスタがルビであったことのフラグ
746 local nqnv = getfield(Nq.nuc, 'value')
747 if has_attr(nqnv, attr_ruby_maxpostp) < 0 then -- auto
748 set_attr(nqnv, attr_ruby_maxpostp, 0)
749 if has_attr(nqnv, attr_ruby_mode)%4 >= 2 then
750 set_attr(nqnv, attr_ruby_maxprep, 0)
752 --if Np and Np.id == id_pbox_w then
753 -- set_attr(nqnv, attr_ruby_maxprep, 0)
762 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
763 "luatexja.ruby.np_info_after", 1)