4 luatexbase.provides_module({
5 name = 'luatexja.ruby',
7 description = 'Ruby annotation',
10 luatexja.load_module 'stack'; local ltjs = luatexja.stack
11 luatexja.load_module 'base'; local ltjb = luatexja.base
13 local to_node = node.direct.tonode
14 local to_direct = node.direct.todirect
16 local getfield = node.direct.getfield
17 local getid = node.direct.getid
18 local getfont = node.direct.getfont
19 local getlist = node.direct.getlist
20 local getchar = node.direct.getchar
21 local getsubtype = node.direct.getsubtype
22 local getkern = node.direct.getkern
23 local getwidth = node.direct.getwidth
24 local getheight = node.direct.getheight
25 local getdepth = node.direct.getdepth
26 local getwhd = node.direct.getwhd
27 local setfield = node.direct.setfield
28 local setglue = luatexja.setglue
29 local setkern = node.direct.setkern
30 local setnext = node.direct.setnext
31 local setshift = node.direct.setshift
32 local setwidth = node.direct.setwidth
33 local setheight = node.direct.setheight
34 local setdepth = node.direct.setdepth
35 local setwhd = node.direct.setwhd
36 local setlist = node.direct.setlist
38 local node_new = node.direct.new
39 local node_remove = node.direct.remove
40 local node_next = node.direct.getnext
41 local node_copy, node_tail = node.direct.copy, node.direct.tail
42 local node_free = node.direct.flush_node or node.direct.free
43 local has_attr, set_attr = node.direct.has_attribute, node.direct.set_attribute
44 local insert_before, insert_after = node.direct.insert_before, node.direct.insert_after
45 local hpack = node.direct.hpack
47 local id_hlist = node.id 'hlist'
48 local id_vlist = node.id 'vlist'
49 local id_rule = node.id 'rule'
50 local id_whatsit= node.id 'whatsit'
51 local id_glue = node.id 'glue'
52 local id_kern = node.id 'kern'
53 local id_penalty= node.id 'penalty'
54 local sid_user = node.subtype 'user_defined'
55 local ltjs_get_stack_table = luatexja.stack.get_stack_table
56 local id_pbox_w = 258 -- cluster which consists of a whatsit
58 local attr_icflag = luatexbase.attributes['ltj@icflag']
59 -- ルビ処理用の attribute は他のやつの流用なので注意!
61 local attr_ruby_id = luatexbase.attributes['ltj@kcat4'] -- uniq id
62 local attr_ruby = luatexbase.attributes['ltj@rubyattr']
63 -- ルビ内部処理用,以下のようにノードによって使われ方が異なる
64 -- * (whatsit) では JAglue 処理時に,
65 -- 「2つ前のクラスタもルビ」 ==> そのルビクラスタの id
66 -- otherwise ==> unset
67 -- * (whatsit).value node ではルビ全角の値(sp単位)
68 -- * 行分割で whatsit の前後に並ぶノードでは,「何番目のルビ関連ノード」か
69 -- * (whatsit).value に続く整形済み vbox たちでは post_intrusion の値
70 local attr_ruby_post_jfmgk = luatexbase.attributes['ltj@kcat3']
71 -- JAglue 処理時に,2つ前のクラスタもルビであれば,そのルビが直後の和文処理グルーへ
72 -- 正の進入をしたか否か(した:1,しなかった:0)
73 local cat_lp = luatexbase.catcodetables['latex-package']
75 local round, floor = tex.round, math.floor
76 local min, max = math.min, math.max
78 luatexja.userid_table.RUBY_PRE = luatexbase.newuserwhatsitid('ruby_pre', 'luatexja')
79 luatexja.userid_table.RUBY_POST = luatexbase.newuserwhatsitid('ruby_post', 'luatexja')
80 local RUBY_PRE = luatexja.userid_table.RUBY_PRE
81 local RUBY_POST = luatexja.userid_table.RUBY_POST
82 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
84 ----------------------------------------------------------------
86 ----------------------------------------------------------------
88 local getbox = node.direct.getbox
89 function luatexja.ruby.cpbox() return node_copy(getbox(0)) end
92 ----------------------------------------------------------------
94 ----------------------------------------------------------------
96 local function gauss(coef)
97 -- #coef 式,#coef 変数の連立1次方程式系を掃きだし法で解く.
100 if coef[i][i]==0 then
102 if coef[j][i]~=0 then
103 coef[i], coef[j] = coef[j], coef[i]; break
108 local d = coef[i][i];
111 for k = 1, deg+1 do coef[j][k] = coef[j][k] - e*coef[i][k]/d end
113 for k = 1, deg+1 do coef[i][k] = coef[i][k]/d end
119 local function solve_1(coef)
120 local a, b, c = coef[1][4], coef[2][4], coef[3][4]
121 coef[1][4], coef[2][4], coef[3][4] = c-b, a+b-c, c-a
125 local function solve_2(coef)
126 local a, b, c, d, e = coef[1][6], coef[2][6], coef[3][6], coef[4][6], coef[5][6]
127 coef[1][6], coef[2][6], coef[3][6], coef[4][6], coef[5][6]
128 = e-c, a+c-e, e-a-d, b+d-e, e-b
133 -- 実行回数 + ルビ中身 から uniq_id を作る関数
134 luatexja.ruby.old_break_info = {} -- public, 前 run 時の分割情報
135 local old_break_info = luatexja.ruby.old_break_info
137 function luatexja.ruby.read_old_break_info()
139 local fname = tex.jobname .. '.ltjruby'
140 local real_file = kpse.find_file(fname)
141 if real_file then dofile(real_file) end
142 cache_handle = io.open(fname, 'w')
144 cache_handle:write('local lrob=luatexja.ruby.old_break_info\n')
151 make_uniq_id = function (w)
152 exec_count = exec_count + 1
157 -- concatenation of boxes: reusing nodes
158 -- ルビ組版が行われている段落/hboxでの設定が使われる.
159 -- ルビ文字を格納しているボックスでの設定ではない!
160 local function get_attr_icflag(p)
161 return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
165 local node_prev = node.direct.getprev
166 function concat(f, b)
169 local h, nh = getlist(f), getlist(b)
170 if getid(nh)==id_whatsit and getsubtype(nh)==sid_user then
171 nh=node_next(nh); node_free(node_prev(nh))
173 set_attr(nh, attr_icflag,
174 get_attr_icflag(nh) + PROCESSED_BEGIN_FLAG)
175 setnext(node_tail(h), nh)
176 setlist(f, nil); node_free(f)
177 setlist(b, nil); node_free(b)
178 local g = luatexja.jfmglue.main(h,false)
186 local h = node_new(id_hlist, 0)
188 setfield(h, 'glue_set', 0)
189 setfield(h, 'glue_order', 0)
196 local function expand_3bits(num)
197 local t = {}; local a = num
199 t[i] = a%8; a = floor(a/8)
203 ----------------------------------------------------------------
205 ----------------------------------------------------------------
207 -- box の中身のノードは再利用される
210 local FROM_JFM = luatexja.icflag_table.FROM_JFM
211 local PROCESSED = luatexja.icflag_table.PROCESSED
212 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
213 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
214 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
215 local XKANJI_SKIP_JFM= luatexja.icflag_table.XKANJI_SKIP_JFM
216 enlarge = function (box, new_width, pre, middle, post, prenw, postnw)
217 -- pre, middle, post: 伸縮比率
218 -- prenw, postnw: 前後の自然長 (sp)
219 local h = getlist(box);
220 local _, hh, hd = getwhd(box)
223 local hic = has_attr(hx, attr_icflag) or 0
224 if (hic == KANJI_SKIP) or (hic == KANJI_SKIP_JFM)
225 or (hic == XKANJI_SKIP) or (hic == XKANJI_SKIP_JFM)
226 or ((hic<=FROM_JFM+63) and (hic>=FROM_JFM)) then
228 if getid(hx) == id_kern then
229 local k = node_new(id_glue, 0)
230 setglue(k, getkern(hx), round(middle*65536), 0,
232 h = insert_after(h, hx, k);
233 h = node_remove(h, hx); node_free(hx); hx = k
235 setglue(hx, getwidth(hx), round(middle*65536), 0,
242 local k = node_new(id_glue);
243 setglue(k, prenw, round(pre*65536), 0, 2, 0)
244 h = insert_before(h, h, k);
246 local k = node_new(id_glue);
247 setglue(k, postnw, round(post*65536), 0, 2, 0)
248 insert_after(h, node_tail(h), k);
250 setlist(box, nil); node_free(box)
251 box = hpack(h, new_width, 'exactly')
252 setheight(box, hh); setdepth(box, hd)
258 ----------------------------------------------------------------
260 ----------------------------------------------------------------
262 -- rtlr: ルビ部分のボックスたち r1, r2, ...
263 -- rtlp: 親文字 のボックスたち p1, p2, ...
264 local function texiface_low(rst, rtlr, rtlp)
265 local w = node_new(id_whatsit, sid_user)
266 setfield(w, 'type', 110); setfield(w, 'user_id', RUBY_PRE)
267 local wv = node_new(id_whatsit, sid_user)
268 setfield(w, 'value', to_node(wv))
269 setfield(wv, 'type', 108)
270 setfield(wv, 'value', rst); rst.count = floor(#rtlr)
271 setfield(wv, 'user_id', RUBY_PRE) -- dummy
274 _, n = insert_after(wv, n, rtlr[i])
275 _, n = insert_after(wv, n, rtlp[i])
277 -- w.value: (whatsit) .. r1 .. p1 .. r2 .. p2
278 node.direct.write(w); return w,wv
282 function luatexja.ruby.texiface(rst, rtlr, rtlp)
283 if #rtlr ~= #rtlp then
284 for i=1, #rtlr do node_free(rtlr[i]) end
285 for i=1, #rtlp do node_free(rtlp[i]) end
286 ltjb.package_error('luatexja-ruby',
287 'Group count mismatch between the ruby and\n' ..
288 'the body (' .. #rtlr .. ' != ' .. #rtlp .. ').',
291 local f, eps = true, rst.eps
293 if getwidth(rtlr[i]) > getwidth(rtlp[i]) + eps then
297 if f then -- モノルビ * n
298 local r,p = {true}, {true}
300 r[1] = rtlr[i]; p[1] = rtlp[i]; texiface_low(rst, r, p)
303 local w, wv = texiface_low(rst, rtlr, rtlp)
304 local id = make_uniq_id(w)
305 set_attr(wv, attr_ruby_id, id)
310 ----------------------------------------------------------------
312 ----------------------------------------------------------------
314 -- r, p の中身のノードは再利用される
315 local function enlarge_parent(r, p, tmp_tbl, no_begin, no_end)
316 -- r: ルビ部分の格納された box,p: 同,親文字
317 local rwidth = getwidth(r)
318 local sumprot = rwidth - getwidth(p) -- >0
319 local pre_intrusion, post_intrusion
320 local ppre, pmid, ppost = tmp_tbl.ppre, tmp_tbl.pmid, tmp_tbl.ppost
321 local mapre, mapost = tmp_tbl.mapre, tmp_tbl.mapost
322 local intmode = floor(tmp_tbl.mode/4)%4
323 if no_begin then mapre = mapre + tmp_tbl.before_jfmgk end
324 if no_end then mapost = mapost + tmp_tbl.after_jfmgk end
325 if (tmp_tbl.mode%4 >=2) and (tmp_tbl.pre<0) and (tmp_tbl.post<0) then
326 mapre = min(mapre,mapost); mapost = mapre
328 if intmode == 0 then -- とりあえず組んでから決める
329 p = enlarge(p, rwidth, ppre, pmid, ppost, 0, 0)
330 pre_intrusion = min(mapre, round(ppre*getfield(p, 'glue_set')*65536))
331 post_intrusion = min(mapost, round(ppost*getfield(p, 'glue_set')*65536))
332 elseif intmode == 1 then
333 pre_intrusion = min(mapre, sumprot);
334 post_intrusion = min(mapost, max(sumprot-pre_intrusion, 0))
335 p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion)
336 elseif intmode == 2 then
337 post_intrusion = min(mapost, sumprot);
338 pre_intrusion = min(mapre, max(sumprot-post_intrusion, 0))
339 p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion)
341 local n = min(mapre, mapost)*2
343 pre_intrusion = n/2; post_intrusion = n/2
345 pre_intrusion = floor(sumprot/2); post_intrusion = sumprot - pre_intrusion
347 p = enlarge(p, rwidth, ppre, pmid, ppost, pre_intrusion, post_intrusion)
348 pre_intrusion = min(mapre, pre_intrusion + round(ppre*getfield(p, 'glue_set')*65536))
349 post_intrusion = min(mapost, post_intrusion + round(ppost*getfield(p, 'glue_set')*65536))
351 setshift(r, -pre_intrusion)
352 local rwidth = rwidth - pre_intrusion - post_intrusion
353 setwidth(r, rwidth); setwidth(p, rwidth)
354 local ps = getlist(p)
355 setwidth(ps, getwidth(ps) - pre_intrusion)
356 local orig_post_intrusion, post_jfmgk = post_intrusion, false
358 if orig_post_intrusion > tmp_tbl.after_jfmgk then
359 orig_post_intrusion = orig_post_intrusion - tmp_tbl.after_jfmgk
360 post_jfmgk = (tmp_tbl.after_jfmgk > 0)
362 orig_post_intrusion = 0
363 post_jfmgk = (post_intrusion > 0)
366 return r, p, orig_post_intrusion, post_jfmgk
370 -- returned value: <new box>, <ruby width>, <post_intrusion>
372 local function new_ruby_box(r, p, tmp_tbl, no_begin, no_end)
373 local post_intrusion, post_jfmgk = 0, false
375 local ppre, pmid, ppost = tmp_tbl.ppre, tmp_tbl.pmid, tmp_tbl.ppost
376 local mapre, mapost = tmp_tbl.mapre, tmp_tbl.mapost
377 local rpre, rmid, rpost, rsmash
378 imode = floor(tmp_tbl.mode/0x100000); rsmash = (imode%2 ==1)
379 imode = floor(imode/2); rpost = imode%8;
380 imode = (imode-rpost)/8; rmid = imode%8;
381 imode = (imode-rmid)/8; rpre = imode%8
382 if getwidth(r) > getwidth(p) then -- change the width of p
383 r, p, post_intrusion, post_jfmgk = enlarge_parent(r, p, tmp_tbl, no_begin, no_end)
384 elseif getwidth(r) < getwidth(p) then -- change the width of r
385 r = enlarge(r, getwidth(p), rpre, rmid, rpost, 0, 0)
387 local need_repack = false
388 -- margin が大きくなりすぎた時の処理
389 if round(rpre*getfield(r, 'glue_set')*65536) > max_margin then
390 local ps = getlist(r); need_repack = true
391 setwidth(ps, max_margin)
392 setfield(ps, 'stretch', 1) -- 全く伸縮しないのも困る
394 if round(rpost*getfield(r, 'glue_set')*65536) > max_margin then
395 local ps = node_tail(getlist(r)); need_repack = true
396 setwidth(ps, max_margin)
397 setfield(ps, 'stretch', 1) -- 全く伸縮しないのも困る
401 r = hpack(getlist(r), getwidth(r), 'exactly')
402 setlist(rt, nil); node_free(rt);
405 local a, k = node_new(id_rule), node_new(id_kern, 1)
406 setwhd(a, 0, 0, 0); setkern(k, tmp_tbl.intergap)
407 insert_after(r, r, a); insert_after(r, a, k);
408 insert_after(r, k, p); setnext(p, nil)
409 if tmp_tbl.rubydepth >= 0 then setdepth(r, tmp_tbl.rubydepth) end
410 if tmp_tbl.baseheight >= 0 then setheight(p, tmp_tbl.baseheight) end
411 a = node.direct.vpack(r); setshift(a, 0)
412 set_attr(a, attr_ruby, post_intrusion)
413 set_attr(a, attr_ruby_post_jfmgk, post_jfmgk and 1 or 0)
414 if rsmash or getheight(a)<getheight(p) then
415 local k = node_new(id_kern, 1)
416 setkern(k, -getheight(a)+getheight(p))
417 setlist(a, k); insert_before(r, r, k)
418 setheight(a, getheight(p))
421 return a, getwidth(r), post_intrusion, post_jfmgk
425 -- High-level routine in pre_linebreak_filter
426 local post_intrusion_backup, post_jfmgk_backup
427 local max_allow_pre, max_allow_post
430 -- 中付き熟語ルビ,cmp containers
431 -- 「文字の構成を考えた」やつはどうしよう
432 local function pre_low_cal_box(w, cmp)
436 -- kf[i] : container 1--i からなる行末形
437 -- kf[cmp+i] : container i--cmp からなる行頭形
439 local wv = getfield(w, 'value')
440 local rst = getfield(wv, 'value')
441 local mdt -- nt*: node temp
442 local coef = {} -- 連立一次方程式の拡大係数行列
443 local rtb = expand_3bits(rst.stretch)
445 -- node list 展開・行末形の計算
446 local nt, nta, ntb = wv, nil, nil -- nt*: node temp
447 rst.ppre, rst.pmid, rst.ppost = rtb[6], rtb[5], rtb[4]
448 rst.mapre, rst.mapost = max_allow_pre, 0
450 nt = node_next(nt); rb[i] = nt; nta = concat(nta, node_copy(nt))
451 nt = node_next(nt); pb[i] = nt; ntb = concat(ntb, node_copy(nt))
453 for j = 1, 2*i do coef[i][j] = 1 end
454 for j = 2*i+1, 2*cmp+1 do coef[i][j] = 0 end
455 kf[i], coef[i][2*cmp+2]
456 = new_ruby_box(node_copy(nta), node_copy(ntb), rst, true, false)
458 node_free(nta); node_free(ntb)
461 local nta, ntb = nil, nil
462 rst.ppre, rst.pmid, rst.ppost = rtb[9], rtb[8], rtb[7]
463 rst.mapre, rst.mapost = 0, max_allow_post
466 for j = 1, 2*i-1 do coef[cmp+i][j] = 0 end
467 for j = 2*i, 2*cmp+1 do coef[cmp+i][j] = 1 end
468 nta = concat(node_copy(rb[i]), nta); ntb = concat(node_copy(pb[i]), ntb)
469 kf[cmp+i], coef[cmp+i][2*cmp+2]
470 = new_ruby_box(node_copy(nta), node_copy(ntb), rst, false, true)
473 -- ここで,nta, ntb には全 container を連結した box が入っているので
476 for j = 1, 2*cmp+1 do coef[2*cmp+1][j] = 1 end
477 rst.ppre, rst.pmid, rst.ppost = rtb[3], rtb[2], rtb[1]
478 rst.mapre, rst.mapost = max_allow_pre, max_allow_post
479 kf[2*cmp+1], coef[2*cmp+1][2*cmp+2], post_intrusion_backup, post_jfmgk_backup
480 = new_ruby_box(nta, ntb, rst, true, true)
482 -- w.value の node list 更新.
484 node.direct.flush_list(node_next(wv))
485 for i = 1, 2*cmp+1 do setnext(nt, kf[i]); nt = kf[i] end
487 if cmp==1 then solve_1(coef)
488 elseif cmp==2 then solve_2(coef)
490 gauss(coef) -- 掃きだし法で連立方程式形 coef を解く
497 local traverse_id = node.direct.traverse_id
498 function first_whatsit(n) -- n 以後で最初の whatsit
499 for h in traverse_id(id_whatsit, n) do
506 local next_cluster_array = {}
508 local function pre_low_app_node(head, w, cmp, coef, ht, dp)
510 local nt = node_new(id_glue)
511 setglue(nt, coef[1][2*cmp+2], 0, 0, 0, 0)
512 set_attr(nt, attr_ruby, 1); set_attr(w, attr_ruby, 2)
513 head = insert_before(head, w, nt)
517 local nta = node_new(id_rule, 0);
518 setwhd(nta, coef[i*2][2*cmp+2], ht, dp)
519 insert_after(head, nt, nta)
520 set_attr(nta, attr_ruby, 2*i+1)
522 if i~=cmp or not next_cluster_array[w] then
523 nt = node_new(id_glue); insert_after(head, nta, nt)
525 nt = next_cluster_array[w]
527 setglue(nt, coef[i*2+1][2*cmp+2], 0, 0, 0, 0)
528 set_attr(nt, attr_ruby, 2*i+2)
530 tex.setattribute('global', attr_ruby, -0x7FFFFFFF)
531 setfield(w, 'user_id', RUBY_POST)
532 next_cluster_array[w]=nil
533 return head, first_whatsit(node_next(nt))
536 local function pre_high(ahead)
537 if not ahead then return ahead end
538 local head = to_direct(ahead)
539 post_intrusion_backup, post_jfmgk_backup = 0, false
540 local n = first_whatsit(head)
542 if getsubtype(n) == sid_user and getfield(n, 'user_id') == RUBY_PRE then
543 local nv = getfield(n, 'value')
544 local rst = getfield(nv, 'value')
545 max_allow_pre = rst.pre or 0
546 local atr = has_attr(n, attr_ruby) or 0
547 if max_allow_pre < 0 then
548 -- 直前のルビで intrusion がおこる可能性あり.
549 -- 前 run のデータが残っていればそれを使用,
550 -- そうでなければ行中形のデータを利用する
551 local op = (atr>0) and (old_break_info[atr] or post_intrusion_backup) or 0
552 max_allow_pre = max(0, -max_allow_pre - op)
554 if rst.exclude_pre_from_prev_ruby and atr>0 and old_break_info[-atr]
555 and (old_break_info[-atr]>0 or post_jfmgk_backup) then
556 -- 「直前のルビが JFM グルーに進入→現在のルビの前文字進入はなし」という状況
557 max_allow_pre = 0; rst.exclude_pre_from_prev_ruby=false
559 if rst.exclude_pre_jfmgk_from_prev_ruby
560 and atr>0 and ((old_break_info[atr] or post_intrusion_backup) > 0) then
561 -- 「直前のルビが文字に進入→現在のルビの和文処理グルーへの進入はなし」という状況
564 post_intrusion_backup, post_jfmgk_backup = 0, false
565 max_allow_post = rst.post or 0
566 max_margin = rst.maxmargin or 0
567 local coef = pre_low_cal_box(n, rst.count)
568 local s = node_tail(nv) --ルビ文字
569 head, n = pre_low_app_node(
570 head, n, rst.count, coef, getheight(s), getdepth(s)
573 n = first_whatsit(node_next(n))
578 luatexbase.add_to_callback('pre_linebreak_filter', pre_high, 'ltj.ruby.pre', 100)
579 luatexbase.add_to_callback('hpack_filter', pre_high, 'ltj.ruby.pre', 100)
581 ----------------------------------------------------------------
583 ----------------------------------------------------------------
586 local function write_aux(wv, num, bool)
587 local id = has_attr(wv, attr_ruby_id)
588 if id>0 and cache_handle then
590 'lrob[' .. tostring(id) .. ']=' .. num .. '\nlrob[' .. tostring(-id) .. ']=' .. tostring(bool) .. '\n')
594 post_lown = function (rs, rw, cmp, ch)
595 -- ch: the head of `current' hlist
596 if #rs ==0 or not rw then return ch end
597 local hn = has_attr(rs[1], attr_ruby)
598 local fn = has_attr(rs[#rs], attr_ruby)
599 local wv = getfield(rw, 'value')
602 local hn = node_tail(wv)
604 insert_after(ch, rs[1], hn)
605 set_attr(hn, attr_icflag, PROCESSED)
606 write_aux(wv, has_attr(hn, attr_ruby), has_attr(hn, attr_ruby_post_jfmgk))-- 行中形
608 local deg, hn = (fn-1)/2, wv
609 for i = 1, deg do hn = node_next(hn) end;
612 insert_after(ch, rs[1], hn)
613 set_attr(hn, attr_icflag, PROCESSED)
614 write_aux(wv, has_attr(hn, attr_ruby), has_attr(hn, attr_ruby_post_jfmgk))
617 local deg, hn = max((hn-1)/2,2), wv
618 for i = 1, cmp+deg-1 do hn = node_next(hn) end
619 -- -1 is needed except the case hn = 3,
620 -- because a ending-line form is removed already from the list
621 node_remove(wv, hn); setnext(hn, nil)
622 insert_after(ch, rs[1], hn)
623 set_attr(hn, attr_icflag, PROCESSED)
624 if fn == 2*cmp-1 then
625 write_aux(wv, has_attr(hn, attr_ruby), has_attr(hn, attr_ruby_post_jfmgk))
630 ch = node_remove(ch, ri); node_free(ri);
633 if fn >= 2*cmp+1 then node_free(rw) end
638 local function post_high_break(head)
639 local rs = {} -- rs: sequence of ruby_nodes,
640 local rw = nil -- rw: main whatsit
641 local cmp = -2 -- dummy
642 for h in node.direct.traverse_id(id_hlist, to_direct(head)) do
643 for i = 1, #rs do rs[i] = nil end
644 local ha = getlist(h)
646 local hai = getid(ha)
647 local i = ((hai == id_glue and getsubtype(ha)==0)
648 or (hai == id_rule and getsubtype(ha)==0)
649 or (hai == id_whatsit and getsubtype(ha)==sid_user
650 and getfield(ha, 'user_id', RUBY_POST)))
651 and has_attr(ha, attr_ruby) or 0
655 setlist(h, post_lown(rs, rw, cmp, getlist(h)))
656 for i = 2, #rs do rs[i] = nil end -- rs[1] is set by the next statement
657 rs[1], rw = ha, nil; ha = node_next(ha)
660 cmp = getfield(getfield(rw, 'value'), 'value').count
661 local hb, hc = node_remove(getlist(h), rw)
662 setlist(h, hb); ha = hc
664 rs[#rs+1] = ha; ha = node_next(ha)
667 setlist(h, post_lown(rs, rw, cmp, getlist(h)))
672 local function post_high_hbox(ahead)
673 local ha = to_direct(ahead); local head = ha
674 local rs = {}; -- rs: sequence of ruby_nodes,
675 local rw = nil; -- rw: main whatsit
678 local hai = getid(ha)
679 local i = ((hai == id_glue and getsubtype(ha)==0)
680 or (hai == id_rule and getsubtype(ha)==0)
681 or (hai == id_whatsit and getsubtype(ha)==sid_user
682 and getfield(ha, 'user_id', RUBY_POST)))
683 and has_attr(ha, attr_ruby) or 0
687 head = post_lown(rs, rw, cmp, head)
688 for i = 2, #rs do rs[i] = nil end -- rs[1] is set by the next statement
689 rs[1], rw = ha, nil; ha = node_next(ha)
692 cmp = getfield(getfield(rw, 'value'), 'value').count
693 head, ha = node_remove(head, rw)
695 rs[#rs+1] = ha; ha = node_next(ha)
698 return to_node(post_lown(rs, rw, cmp, head))
701 luatexbase.add_to_callback('post_linebreak_filter', post_high_break, 'ltj.ruby.post_break', 100)
702 luatexbase.add_to_callback('hpack_filter', post_high_hbox, 'ltj.ruby.post_hbox', 101)
705 ----------------------------------------------------------------
706 -- for jfmglue callbacks
707 ----------------------------------------------------------------
709 local RIPRE = luatexja.stack_table_index.RIPRE
710 local RIPOST = luatexja.stack_table_index.RIPOST
712 local function whatsit_callback(Np, lp, Nq)
713 if Np.nuc then return Np
714 elseif getfield(lp, 'user_id') == RUBY_PRE then
715 Np.first, Np.nuc, Np.last = lp, lp, lp
716 local lpv = getfield(lp, 'value')
717 local rst = getfield(lpv, 'value')
718 local x = node_next(node_next(lpv))
719 Np.last_char = luatexja.jfmglue.check_box_high(Np, getlist(x), nil)
720 if Nq.id ~=id_pbox_w then
721 if type(Nq.char)=='number' then
723 if rst.pre < 0 then -- auto
724 local s = ltjs.table_current_stack[RIPRE + Nq.char] or 0
725 local p = round(abs(s)*rst.rubyzw)
726 if rst.mode%2 == 0 then -- intrusion 無効
729 rst.pre = -p; rst.exclude_pre_from_prev_ruby = (s<0);
730 rst.exclude_pre_jfmgk_from_prev_ruby = (ltjs.table_current_stack[RIPOST +Nq.char] or 0)<0;
733 set_attr(lp, attr_ruby, Nq.prev_ruby)
735 elseif rst.pre < 0 then -- auto
736 if Nq.char == 'parbdd' then
737 local s = ltjs.table_current_stack[RIPRE - 1] or 0
738 local p = round(abs(s)*rst.rubyzw)
740 if rst.mode%2 == 0 then -- intrusion 無効
743 rst.pre = p; rst.exclude_pre_from_prev_ruby = (s<0);
744 rst.exclude_pre_jfmgk_from_prev_ruby = (ltjs.table_current_stack[RIPOST -1] or 0)<0;
749 elseif rst.pre < 0 then -- auto
757 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
758 "luatexja.ruby.np_info", 1)
762 local FROM_JFM = luatexja.icflag_table.FROM_JFM
763 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
764 local function add_gk(t, index, p)
765 local ic = get_attr_icflag(p)
766 if ic and (ic>FROM_JFM) and (ic<KANJI_SKIP) then ic = FROM_JFM end
767 if t.intrude_jfmgk[ic] then
768 if getid(p)==id_kern then t[index] = t[index] + getkern(p)
769 else t[index] = t[index] + getwidth(p) end
772 local RIPOST = luatexja.stack_table_index.RIPOST
774 local function whatsit_after_callback(s, Nq, Np, head)
775 if not s and getfield(Nq.nuc, 'user_id') == RUBY_PRE then
777 local last_glue = node_new(id_glue)
778 set_attr(last_glue, attr_icflag, 0)
779 insert_before(Nq.nuc, Np.first, last_glue)
781 next_cluster_array[Nq.nuc] = last_glue -- ルビ処理用のグルー
783 local nqnv = getfield(Nq.nuc, 'value')
784 local rst = getfield(nqnv, 'value')
786 if type(Nq.gk)=="table" then
787 for _,v in ipairs(Nq.gk) do add_gk(rst, 'before_jfmgk', v) end
788 else add_gk(rst, 'before_jfmgk', Nq.gk) end
790 local x = node_next(node_next(nqnv))
791 for i = 2, rst.count do x = node_next(node_next(x)) end
792 Nq.last_char = luatexja.jfmglue.check_box_high(Nq, getlist(x), nil)
793 luatexja.jfmglue.after_hlist(Nq)
794 if Np and Np.id ~=id_pbox_w and type(Np.char)=='number' then
796 if rst.post < 0 then -- auto
797 local p = round(abs(ltjs.table_current_stack[RIPOST + Np.char] or 0)*rst.rubyzw)
798 if rst.mode%2 == 0 then -- intrusion 無効
803 Np.prev_ruby = has_attr(getfield(Nq.nuc, 'value'), attr_ruby_id)
804 -- 前のクラスタがルビであったことのフラグ
806 local nqnv = getfield(Nq.nuc, 'value')
807 local rst = getfield(nqnv, 'value')
808 if rst.post < 0 then -- auto
817 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
818 "luatexja.ruby.np_info_after", 1)
819 local function w (s, Nq, Np)
820 if not s and getfield(Nq.nuc, 'user_id') == RUBY_PRE then
821 local rst = getfield(getfield(Nq.nuc, 'value'), 'value')
823 if type(Np.gk)=="table" then
824 for _,v in ipairs(Np.gk) do add_gk(rst, 'after_jfmgk', v) end
825 else add_gk(rst, 'after_jfmgk', Np.gk) end
829 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_last_minute", w,
830 "luatexja.ruby.np_info_last_minute", 1)