4 luatexbase.provides_module({
5 name = 'luatexja.ruby',
10 module('luatexja.ruby', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
13 local id_hlist = node.id('hlist')
14 local id_vlist = node.id('vlist')
15 local id_rule = node.id('rule')
16 local id_whatsit = node.id('whatsit')
17 local id_glue = node.id('glue')
18 local id_kern = node.id('kern')
19 local id_glue_spec = node.id('glue_spec')
20 local sid_user = node.subtype('user_defined')
21 local ltjs_get_penalty_table = luatexja.stack.get_penalty_table
22 local id_pbox_w = node.id('hlist') + 513 -- cluster which consists of a whatsit
24 local attr_icflag = luatexbase.attributes['ltj@icflag']
25 -- ルビ処理用の attribute は他のやつの流用なので注意!
27 local attr_ruby_maxprep = luatexbase.attributes['ltj@charclass']
28 local attr_ruby_maxpostp = luatexbase.attributes['ltj@kcat0']
29 local attr_ruby_maxmargin = luatexbase.attributes['ltj@kcat1']
31 local attr_ruby_stretch = luatexbase.attributes['ltj@kcat2']
32 local attr_ruby_mode = luatexbase.attributes['ltj@kcat3']
33 local attr_ruby = luatexbase.attributes['ltj@rubyattr']
35 -- jfmglue 中では「2つ前のクラスタもルビ」のフラグ(true = 1)
36 -- (whatsit).value node ではルビ全角の値(sp単位)
37 -- 行分割前後では,「何番目のルビ関連ノード」か
39 local round = tex.round
40 local floor = math.floor
43 --local node_type = node.type
44 local node_new = node.new
45 local node_remove = node.remove
46 -- local node_prev = node.prev
47 local node_next = node.next
48 local node_copy = node.copy
49 local node_tail = node.tail
50 local node_free = node.free
51 local has_attr = node.has_attribute
52 local set_attr = node.set_attribute
53 local node_insert_before = node.insert_before
54 local node_insert_after = node.insert_after
61 local uid_ruby_pre = 30120
62 local uid_ruby_post = 30121
66 ----------------------------------------------------------------
68 ----------------------------------------------------------------
70 local function gauss(coef)
71 -- #coef 式,#coef 変数の連立1次方程式系を掃きだし法で解く.
77 local tmp = coef[i]; coef[i] = coef[j]; coef[j] = tmp
86 for k = 1, deg+1 do coef[j][k] = coef[j][k] - e*coef[i][k]/d end
88 for k = 1, deg+1 do coef[i][k] = coef[i][k]/d end
94 -- 実行回数 + ルビ中身 から uniq_id を作る関数
95 -- 未実装.これを使えば 2 回目以降の組版に 1 回目の情報が使える
98 -- concatenation of boxes: reusing nodes
99 -- ルビ組版が行われている段落/hboxでの設定が使われる.
100 -- ルビ文字を格納しているボックスでの設定ではない!
101 local function concat(f, b)
106 node_tail(h).next = b.head
107 f.head = nil; b.head = nil
108 node_free(f); node_free(b)
109 r = node.hpack(luatexja.jfmglue.main(h,false))
116 local h = node_new(id_hlist)
117 h.subtype = 0; h.width = 0; h.height = 0; h.depth = 0;
118 h.head = nil; h.glue_set = 0; h.glue_order = 0
124 local function expand_3bits(num)
125 local t = {}; local a = num
127 table.insert(t,a%8); a = floor(a/8)
131 ----------------------------------------------------------------
133 ----------------------------------------------------------------
135 -- box の中身のノードは再利用される
136 local function enlarge(box, new_width, pre, middle, post, prenw, postnw)
137 -- pre, middle, post: 伸縮比率
138 -- prenw, postnw: 前後の自然長 (sp)
139 local h = box.head; local hh = box.height; local hd = box.depth
142 if has_attr(hx, attr_icflag) == KANJI_SKIP
143 or has_attr(hx, attr_icflag) == XKANJI_SKIP
144 or has_attr(hx, attr_icflag) == FROM_JFM then
146 if hx.id == id_kern then
147 local k = node_new(id_glue);
148 local ks = node_new(id_glue_spec);
150 ks.stretch_order = 2; ks.stretch = round(middle*65536);
151 ks.shrink_order = 0; ks.shrink = 0;
152 k.subtype = 0; k.spec = ks
153 h = node_insert_after(h, hx, k);
154 h = node_remove(h, hx); node_free(hx); hx = k;
156 hx.spec = node_copy(hx.spec);
157 hx.spec.stretch_order=2; hx.spec.stretch = round(middle*65536);
158 hx.spec.shrink_order=0; hx.spec.shrink = 0;
164 local k = node_new(id_glue);
165 local ks = node_new(id_glue_spec);
166 ks.width = prenw; ks.stretch_order = 2; ks.stretch = round(pre*65536);
167 ks.shrink_order = 0; ks.shrink = 0;
168 k.subtype = 0; k.spec = ks; h = node_insert_before(h, h, k);
169 local k = node_new(id_glue);
170 local ks = node_new(id_glue_spec);
171 ks.width = postnw; ks.stretch_order = 2; ks.stretch = round(post*65536);
172 ks.shrink_order = 0; ks.shrink = 0;
173 k.subtype = 0; k.spec = ks; node_insert_after(h, node_tail(h), k);
175 box.head = nil; node_free(box);
176 box = node.hpack(h, new_width, 'exactly')
177 box.height = hh; box.depth = hd; return box
180 ----------------------------------------------------------------
182 ----------------------------------------------------------------
184 local function texiface_low(rst, rtlr, rtlp)
185 local w = node_new(id_whatsit, sid_user)
186 w.type = 110; w.user_id = uid_ruby_pre;
187 w.value = node_new(id_whatsit, sid_user)
188 w.value.type = 100; w.value.value = floor(#rtlr)
189 set_attr(w.value, attr_ruby, rst.rubyzw)
190 set_attr(w.value, attr_ruby_maxmargin, rst.maxmargin)
191 set_attr(w.value, attr_ruby_maxprep, rst.intrusionpre)
192 set_attr(w.value, attr_ruby_maxpostp, rst.intrusionpost)
193 set_attr(w.value, attr_ruby_stretch, rst.stretch)
194 set_attr(w.value, attr_ruby_mode, rst.mode)
197 local n = rtlr[i]; m.next = n; m = rtlp[i]; n.next = m
199 -- w.value: (whatsit) .. r1 .. p1 .. r2 .. p2
204 function texiface(rst, rtlr, rtlp)
205 if #rtlr ~= #rtlp then
206 for i,v in pairs(rtlr) do node_free(v) end
207 for i,v in pairs(rtlp) do node_free(v) end
208 luatexja.base.package_error('luatexja-ruby',
209 'Group count mismatch between the ruby and\n' ..
210 'the body (' .. #rtlr .. ' != ' .. #rtlp .. ').',
215 if rtlr[i].width > rtlp[i].width then f = false; break end
217 if f then -- モノルビ * n
218 local r,p = {true}, {true}
220 r[1] = rtlr[i]; p[1] = rtlp[i]; texiface_low(rst, r, p)
223 texiface_low(rst, rtlr, rtlp)
229 ----------------------------------------------------------------
231 ----------------------------------------------------------------
233 -- r, p の中身のノードは再利用される
234 local function enlarge_parent(r, p, ppre, pmid, ppost, mapre, mapost, intmode)
235 local sumprot = r.width - p.width -- >0
236 local pre_intrusion, post_intrusion
237 if intmode == 0 then -- とりあえず組んでから決める
238 p = enlarge(p, r.width, ppre, pmid, ppost, 0, 0)
239 pre_intrusion = min(mapre, round(ppre*p.glue_set*65536))
240 post_intrusion = min(mapost, round(ppost*p.glue_set*65536))
241 elseif intmode == 1 then
242 pre_intrusion = min(mapre, sumprot);
243 post_intrusion = min(mapost, max(sumprot-pre_intrusion, 0))
244 p = enlarge(p, r.width, ppre, pmid, ppost, pre_intrusion, post_intrusion)
245 elseif intmode == 2 then
246 post_intrusion = min(mapost, sumprot);
247 pre_intrusion = min(mapre, max(sumprot-post_intrusion, 0))
248 p = enlarge(p, r.width, ppre, pmid, ppost, pre_intrusion, post_intrusion)
250 local n = min(mapre, mapost)*2
252 pre_intrusion = n/2; post_intrusion = n/2
254 pre_intrusion = floor(sumprot/2); post_intrusion = sumprot - pre_intrusion
256 p = enlarge(p, r.width, ppre, pmid, ppost, pre_intrusion, post_intrusion)
257 pre_intrusion = min(mapre, pre_intrusion + round(ppre*p.glue_set*65536))
258 post_intrusion = min(mapost, post_intrusion + round(ppost*p.glue_set*65536))
260 r.shift = -pre_intrusion
261 r.width = r.width - pre_intrusion - post_intrusion
263 p.head.spec.width = p.head.spec.width - pre_intrusion
264 return r, p, post_intrusion
268 -- returned value: <new box>, <ruby width>, <post_intrusion>
270 local function new_ruby_box(r, p, ppre, pmid, ppost,
271 rpre, rmid, rpost, mapre, mapost, intmode)
272 local post_intrusion = 0
273 if r.width > p.width then -- change the width of p
274 r, p, post_intrusion = enlarge_parent(r, p, ppre, pmid, ppost, mapre, mapost, intmode)
275 elseif r.width < p.width then -- change the width of r
276 r = enlarge(r, p.width, rpre, rmid, rpost, 0, 0)
278 local need_repack = false
279 -- margin が大きくなりすぎた時の処理
280 if round(rpre*r.glue_set*65536) > max_margin then
281 local sp = r.head.spec; need_repack = true
282 sp.width = max_margin; sp.stretch = 1 -- 全く伸縮しないのも困る
284 if round(rpost*r.glue_set*65536) > max_margin then
285 local sp = node_tail(r.head).spec; need_repack = true
286 sp.width = max_margin; sp.stretch = 1 -- 全く伸縮しないのも困る
290 r = node.hpack(r.head, r.width, 'exactly')
291 rt.head = nil; node_free(rt);
294 local a = node_new(id_rule); a.width = 0; a.height = 0; a.depth = 0
295 node_insert_after(r, r, a); node_insert_after(r, a, p); p.next = nil
296 a = node.vpack(r); a.height = p.height; a.depth = p.depth;
297 a.shift = -(r.height+r.depth)
298 return a, r.width, post_intrusion
302 -- High-level routine in pre_linebreak_filter
303 local max_post_intrusion_backup
304 local max_allow_pre, max_allow_post
307 -- 中付き熟語ルビ,cmp containers
308 -- 「文字の構成を考えた」やつはどうしよう
309 local function pre_low_cal_box(w, cmp)
313 -- kf[i] : container 1--i からなる行末形
314 -- kf[cmp+i] : container i--cmp からなる行頭形
316 local nt, nta, ntb, mdt -- nt*: node temp
317 local coef = {} -- 連立一次方程式の拡大係数行列
318 local rtb = expand_3bits(has_attr(w.value, attr_ruby_stretch))
319 local rtc = expand_3bits(has_attr(w.value, attr_ruby_mode))
320 local intmode = floor(has_attr(w.value, attr_ruby_mode)/4)%4
322 -- node list 展開・行末形の計算
323 nta = nil; ntb = nil; nt = w.value
325 nt = nt.next; rb[i] = nt; nta = concat(nta, node_copy(nt))
326 nt = nt.next; pb[i] = nt; ntb = concat(ntb, node_copy(nt))
328 for j = 1, 2*i do coef[i][j] = 1 end
329 for j = 2*i+1, 2*cmp+1 do coef[i][j] = 0 end
330 kf[i], coef[i][2*cmp+2]
331 = new_ruby_box(node_copy(nta), node_copy(ntb),
332 rtb[6], rtb[5], rtb[4], rtc[10], rtc[9], rtc[8],
333 max_allow_pre, 0, intmode)
335 node.free(nta); node.free(ntb)
341 for j = 1, 2*i-1 do coef[cmp+i][j] = 0 end
342 for j = 2*i, 2*cmp+1 do coef[cmp+i][j] = 1 end
343 nta = concat(node_copy(rb[i]), nta); ntb = concat(node_copy(pb[i]), ntb)
344 kf[cmp+i], coef[cmp+i][2*cmp+2], mdt
345 = new_ruby_box(node_copy(nta), node_copy(ntb),
346 rtb[9], rtb[8], rtb[7], rtc[10], rtc[9], rtc[8],
347 0, max_allow_post, intmode)
348 if max_post_intrusion_backup < mdt then max_post_intrusion_backup = mdt end
351 -- ここで,nta, ntb には全 container を連結した box が入っているので
354 for j = 1, 2*cmp+1 do coef[2*cmp+1][j] = 1 end
355 kf[2*cmp+1], coef[2*cmp+1][2*cmp+2], mdt
356 = new_ruby_box(nta, ntb,
357 rtb[3], rtb[2], rtb[1], rtc[10], rtc[9], rtc[8],
358 max_allow_pre, max_allow_post, intmode)
359 if max_post_intrusion_backup < mdt then max_post_intrusion_backup = mdt end
361 -- w.value の node list 更新.
363 node.flush_list(nt.next)
364 for i = 1, 2*cmp+1 do nt.next = kf[i]; nt = kf[i] end
366 gauss(coef) -- 掃きだし法で連立方程式形 coef を解く
371 local function pre_low_app_node(head, w, cmp, coef, ht, dp)
374 nt = node_new(id_glue); ntb = node_new(id_glue_spec);
375 ntb.width = coef[1][2*cmp+2]; ntb.stretch_order = 0; ntb.stretch = 0;
376 ntb.shrink_order = 0; ntb.shrink = 0; nt.subtype = 0; nt.spec = ntb;
377 head = node_insert_before(head, w, nt)
378 set_attr(nt, attr_ruby, 1)
379 set_attr(w, attr_ruby, 2); nt = w
382 nta = node_new(id_rule); nta.width = coef[i*2][2*cmp+2];
383 nta.height = ht; nta.depth =dp; nta.subtype = 0
384 node_insert_after(head, nt, nta); set_attr(nta, attr_ruby, 2*i+1)
386 nt = node_new(id_glue); ntb = node_new(id_glue_spec);
387 ntb.width = coef[i*2+1][2*cmp+2]; ntb.stretch_order = 0; ntb.stretch = 0;
388 ntb.shrink_order = 0; ntb.shrink = 0; nt.subtype = 0; nt.spec = ntb;
389 head = node_insert_after(head, nta, nt); set_attr(nt, attr_ruby, 2*i+2)
391 tex.setattribute(attr_ruby, -0x7FFFFFFF);
392 w.user_id = uid_ruby_post;
393 return head, node_next(nt)
396 local function pre_high(head)
397 local n = head; max_post_intrusion_backup = 0
398 if not n then return head end
400 if n.id == id_whatsit then
401 if n.subtype == sid_user and n.user_id == uid_ruby_pre then
402 max_allow_pre = has_attr(n.value, attr_ruby_maxprep) or 0
403 if has_attr(n, attr_ruby) == 1 then
404 -- 直前のルビで intrusion がおこる可能性あり.安全策をとる.
405 max_allow_pre = max(0, max_allow_pre - max_post_intrusion_backup)
407 max_post_intrusion_backup = 0
408 max_allow_post = has_attr(n.value, attr_ruby_maxpostp) or 0
409 max_margin = has_attr(n.value, attr_ruby_maxmargin) or 0
410 local coef = pre_low_cal_box(n, n.value.value)
411 local s = node_tail(n.value) --ルビ文字
412 head, n = pre_low_app_node(
413 head, n, n.value.value, coef, s.height, s.depth
424 luatexbase.add_to_callback('pre_linebreak_filter', pre_high, 'ltj.ruby.pre', 100)
425 luatexbase.add_to_callback('hpack_filter', pre_high, 'ltj.ruby.pre', 100)
427 ----------------------------------------------------------------
429 ----------------------------------------------------------------
431 local function post_lown(rs, rw, ch)
432 -- ch: the head of `current' hlist
433 if #rs ==0 or not rw then return ch end
434 local hn = has_attr(rs[1],attr_ruby)
435 local fn = has_attr(rs[#rs],attr_ruby)
436 local cmp = rw.value.value
439 hn = node_tail(rw.value); node_remove(rw.value, hn)
440 node_insert_after(ch, rs[#rs], hn)
441 set_attr(hn, attr_icflag, PROCESSED)
444 hn = rw.value; for i = 1, deg do hn = hn.next end;
445 node_remove(rw.value, hn); hn.next = nil;
446 node_insert_after(ch, rs[#rs], hn)
447 set_attr(hn, attr_icflag, PROCESSED)
450 local deg = (hn-1)/2;
451 if deg == 1 then deg = 2 end
452 hn = rw.value; for i = 1, cmp+deg-1 do hn = hn.next end
453 -- -1 is needed except the case hn = 3,
454 -- because a ending-line form is removed already from the list
455 node_remove(rw.value, hn); hn.next = nil
456 node_insert_after(ch, rs[#rs], hn)
457 set_attr(hn, attr_icflag, PROCESSED)
459 for i, v in ipairs(rs) do ch = node_remove(ch, v); node_free(v) end
460 if fn >= 2*cmp+1 then
461 rw.next = nil; node_free(rw);
467 local function post_high_break(head)
469 local rs = {}; -- rs: sequence of ruby_nodes,
470 local rw = nil; -- rw: main whatsit
472 if h.id == id_hlist then
473 local ha = h.head; rs = {}
475 local i = (((ha.id == id_glue and ha.subtype==0)
476 or (ha.id == id_rule and ha.subtype==0)
477 or (ha.id == id_whatsit and ha.subtype==sid_user
478 and ha.user_id==uid_ruby_post))
479 and has_attr(ha, attr_ruby)) or 0
481 h.head = post_lown(rs, rw, h.head); rs = {}; rw = nil
482 table.insert(rs, ha); ha = node_next(ha)
484 table.insert(rs, ha); ha = node_next(ha)
486 rw = ha; h.head, ha = node_remove(h.head, rw);
491 h.head = post_lown(rs, rw, h.head);
498 local function post_high_hbox(head)
500 local rs = {}; -- rs: sequence of ruby_nodes,
501 local rw = nil; -- rw: main whatsit
503 local i = (((ha.id == id_glue and ha.subtype==0)
504 or (ha.id == id_rule and ha.subtype==0)
505 or (ha.id == id_whatsit and ha.subtype==sid_user
506 and ha.user_id==uid_ruby_post))
507 and has_attr(ha, attr_ruby)) or 0
509 head = post_lown(rs, rw, head); rs = {}; rw = nil
510 table.insert(rs, ha); ha = node_next(ha)
512 table.insert(rs, ha); ha = node_next(ha)
514 rw = ha; head, ha = node_remove(head, rw);
519 return post_lown(rs, rw, head);
522 luatexbase.add_to_callback('post_linebreak_filter', post_high_break, 'ltj.ruby.post_break', 100)
523 luatexbase.add_to_callback('hpack_filter', post_high_hbox, 'ltj.ruby.post_hbox', 101)
526 ----------------------------------------------------------------
527 -- for jfmglue callbacks
528 ----------------------------------------------------------------
529 function whatsit_callback(Np, lp, Nq, bsl)
530 if Np.nuc then return Np
531 elseif lp.user_id == uid_ruby_pre then
532 Np.first = lp; Np.nuc = lp; Np.last = lp
533 local x = lp.value.next.next
534 Np.last_char = luatexja.jfmglue.check_box_high(Np, x.head, nil)
535 if Nq.id ~=id_pbox_w and Nq.char then
536 if has_attr(lp.value, attr_ruby_maxprep) < 0 then -- auto
537 local p = round(ltjs_get_penalty_table('ripre', Nq.char, 0, bsl)
538 *has_attr(lp.value, attr_ruby))
539 if has_attr(lp.value, attr_ruby_mode)%2 == 0 then -- intrusion 無効
542 set_attr(lp.value, attr_ruby_maxprep, p)
545 set_attr(lp, attr_ruby, 1)
548 set_attr(lp.value, attr_ruby_maxprep, 0)
553 function whatsit_after_callback(s, Nq, Np, bsl)
554 if not s and Nq.nuc.user_id == uid_ruby_pre then
555 local x = Nq.nuc.value.next.next
556 for i = 2, Nq.nuc.value.value do x = x.next.next end
557 Nq.last_char = luatexja.jfmglue.check_box_high(Nq, x.head, nil)
558 luatexja.jfmglue.after_hlist(Nq)
560 if Np and Np.id ~=id_pbox_w and Np.char then
561 if has_attr(Nq.nuc.value, attr_ruby_maxpostp) < 0 then -- auto
562 local p = floor(ltjs_get_penalty_table('ripost', Np.char, 0, bsl)
563 *has_attr(Nq.nuc.value, attr_ruby))
564 if has_attr(Nq.nuc.value, attr_ruby_mode)%2 == 0 then -- intrusion 無効
567 if has_attr(Nq.nuc.value, attr_ruby_mode)%4 >= 2 then
568 local q = has_attr(Nq.nuc.value, attr_ruby_maxprep)
571 set_attr(Nq.nuc.value, attr_ruby_maxprep, p)
574 set_attr(Nq.nuc.value, attr_ruby_maxpostp, p)
576 Np.prev_ruby = true -- 前のクラスタがルビであったことのフラグ
577 else -- 直前が文字以外なら intrusion なし
578 set_attr(Nq.nuc.value, attr_ruby_maxpostp, 0)
579 if has_attr(Nq.nuc.value, attr_ruby_mode)%4 >= 2 then
580 set_attr(Nq.nuc.value, attr_ruby_maxprep, 0)
582 if Np and Np.id == id_pbox_w then
583 set_attr(Np.nuc.value, attr_ruby_maxprep, 0)
590 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
591 "luatexja.ruby.np_info", 1)
592 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
593 "luatexja.ruby.np_info_after", 1)