OSDN Git Service

Import from old kitagawa_ruby branch (may not work now)
[luatex-ja/luatexja.git] / src / ltj-ruby.lua
1 --
2 -- ltj-ruby.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.ruby',
6   date = '2012/04/21',
7   version = '0.1',
8   description = 'Ruby',
9 })
10 module('luatexja.ruby', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12
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
23
24 local attr_icflag = luatexbase.attributes['ltj@icflag']
25 -- ルビ処理用の attribute は他のやつの流用なので注意!
26 -- 進入許容量 (sp)
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']
30
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']
34 -- ルビ内部処理用
35 -- jfmglue 中では「2つ前のクラスタもルビ」のフラグ(true = 1)
36 -- (whatsit).value node ではルビ全角の値(sp単位)
37 -- 行分割前後では,「何番目のルビ関連ノード」か
38
39 local round = tex.round
40 local floor = math.floor
41 local min = math.min
42 local max = math.max
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
55
56 local FROM_JFM = 4
57 local KANJI_SKIP = 6
58 local XKANJI_SKIP = 7
59 local PROCESSED = 8
60
61 local uid_ruby_pre = 30120
62 local uid_ruby_post = 30121
63
64 local box_stack_level
65
66 ----------------------------------------------------------------
67 -- 補助関数群 1
68 ----------------------------------------------------------------
69
70 local function gauss(coef)
71    -- #coef 式,#coef 変数の連立1次方程式系を掃きだし法で解く.
72    local deg = #coef
73    for i = 1, deg do
74       if coef[i][i]==0 then
75          for j = i+1, deg do 
76             if coef[j][i]~=0 then 
77                local tmp = coef[i]; coef[i] = coef[j]; coef[j] = tmp
78                break
79             end
80          end
81       end
82       for j = 1,deg do 
83          local d = coef[i][i];
84          if j~=i then
85             local e = coef[j][i]
86             for k = 1, deg+1 do coef[j][k] = coef[j][k] - e*coef[i][k]/d end
87          else
88             for k = 1, deg+1 do coef[i][k] = coef[i][k]/d end
89          end
90       end
91    end
92 end
93
94 -- 実行回数 + ルビ中身 から uniq_id を作る関数
95 -- 未実装.これを使えば 2 回目以降の組版に 1 回目の情報が使える
96
97
98 -- concatenation of boxes: reusing nodes
99 -- ルビ組版が行われている段落/hboxでの設定が使われる.
100 -- ルビ文字を格納しているボックスでの設定ではない!
101 local function concat(f, b)
102    local r
103    if f then
104       if b then
105          local h = f.head
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))
110       else 
111          r = f
112       end
113    elseif b then
114       r = b
115    else
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
119       r = h
120    end
121    return r
122 end
123
124 local function expand_3bits(num)
125    local t = {}; local a = num
126    for i = 1, 10 do
127       table.insert(t,a%8); a = floor(a/8)
128    end
129    return t
130 end
131 ----------------------------------------------------------------
132 -- 補助関数群 2
133 ----------------------------------------------------------------
134
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
140    local hx = h
141    while hx do
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
145          -- この 3 種類の空白をのばす
146          if hx.id == id_kern then
147             local k = node_new(id_glue);
148             local ks = node_new(id_glue_spec);
149             ks.width = hx.kern; 
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;
155          else -- glue
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;
159          end
160       end
161       hx = node_next(hx)
162    end
163    -- 先頭と末尾の空白を挿入
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);
174    -- hpack
175    box.head = nil; node_free(box);
176    box = node.hpack(h, new_width, 'exactly')
177    box.height = hh; box.depth = hd; return box
178 end
179
180 ----------------------------------------------------------------
181 -- TeX interface
182 ----------------------------------------------------------------
183
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)
195    local m = w.value
196    for i = 1, #rtlr do
197       local n = rtlr[i]; m.next = n; m = rtlp[i]; n.next = m
198    end
199    -- w.value: (whatsit) .. r1 .. p1 .. r2 .. p2
200    node.write(w)
201 end
202
203 -- rst: table
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 .. ').',
211                                   '')
212    else
213       local f = true
214       for i = 1,#rtlr do
215          if rtlr[i].width > rtlp[i].width then f = false; break end
216       end
217       if f then -- モノルビ * n
218          local r,p = {true}, {true}
219          for i = 1,#rtlr do
220             r[1] = rtlr[i]; p[1] = rtlp[i]; texiface_low(rst, r, p)
221          end
222       else
223          texiface_low(rst, rtlr, rtlp)
224       end
225    end
226 end
227
228
229 ----------------------------------------------------------------
230 -- pre_line_break
231 ----------------------------------------------------------------
232
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) 
249    else --  intmode == 3
250       local n = min(mapre, mapost)*2
251       if n < sumprot then
252          pre_intrusion = n/2; post_intrusion = n/2
253       else
254          pre_intrusion = floor(sumprot/2); post_intrusion = sumprot - pre_intrusion
255       end
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))
259    end
260       r.shift = -pre_intrusion
261       r.width = r.width - pre_intrusion - post_intrusion
262       p.width = r.width
263       p.head.spec.width = p.head.spec.width - pre_intrusion
264    return r, p, post_intrusion
265 end
266
267 -- ルビボックスの生成(単一グループ)
268 -- returned value: <new box>, <ruby width>, <post_intrusion>
269 local max_margin
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) 
277       post_intrusion = 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 -- 全く伸縮しないのも困る
283       end
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 -- 全く伸縮しないのも困る
287       end
288       if need_repack then
289          local rt = r
290          r = node.hpack(r.head, r.width, 'exactly')
291          rt.head = nil; node_free(rt);
292       end
293    end
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
299 end
300
301
302 -- High-level routine in pre_linebreak_filter
303 local max_post_intrusion_backup
304 local max_allow_pre, max_allow_post
305
306
307 -- 中付き熟語ルビ,cmp containers
308 -- 「文字の構成を考えた」やつはどうしよう
309 local function pre_low_cal_box(w, cmp)
310    local rb = {}
311    local pb = {}
312    local kf = {}
313    -- kf[i] : container 1--i からなる行末形
314    -- kf[cmp+i] : container i--cmp からなる行頭形
315    -- kf[2cmp+1] : 行中形
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
321
322    -- node list 展開・行末形の計算
323    nta = nil; ntb = nil; nt = w.value
324    for i = 1, cmp do
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))
327       coef[i] = {}
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)
334    end
335    node.free(nta); node.free(ntb)
336
337    -- 行頭形の計算
338    nta = nil; ntb = nil
339    for i = cmp,1,-1 do
340       coef[cmp+i] = {}
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
349    end
350
351    -- ここで,nta, ntb には全 container を連結した box が入っているので
352    -- それを使って行中形を計算する.
353    coef[2*cmp+1] = {}
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
360
361    -- w.value の node list 更新.
362    nt = w.value
363    node.flush_list(nt.next)
364    for i = 1, 2*cmp+1 do nt.next = kf[i]; nt = kf[i]  end
365
366    gauss(coef) -- 掃きだし法で連立方程式形 coef を解く
367    return coef
368 end
369
370 -- ノード追加
371 local function pre_low_app_node(head, w, cmp, coef, ht, dp)
372    local nt, nta, ntb
373    -- メインの node list 更新
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
380    for i = 1, cmp do
381       -- rule
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)
385       -- glue
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)
390    end
391    tex.setattribute(attr_ruby, -0x7FFFFFFF);
392    w.user_id = uid_ruby_post;
393    return head, node_next(nt)
394 end
395
396 local function pre_high(head)
397    local n = head; max_post_intrusion_backup = 0
398    if not n then return head end
399    while n do
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)
406             end
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
414             )
415          else 
416             n = n.next
417          end
418       else
419          n = n.next
420       end
421    end
422    return head
423 end 
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)
426
427 ----------------------------------------------------------------
428 -- post_line_break
429 ----------------------------------------------------------------
430
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
437    if hn==1 then 
438       if fn==2*cmp+2 then
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)
442       else
443          local deg = (fn-1)/2
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)
448      end
449    else
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)
458    end
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); 
462    end
463    return ch;
464 end
465
466
467 local function post_high_break(head)
468    local h = head; 
469    local rs = {};  -- rs: sequence of ruby_nodes, 
470    local rw = nil; -- rw: main whatsit
471    while h do 
472       if h.id == id_hlist then
473          local ha = h.head; rs = {}
474          while ha do
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
480             if i==1 then 
481                h.head = post_lown(rs, rw, h.head); rs = {}; rw = nil
482                table.insert(rs, ha); ha = node_next(ha)
483             elseif i>=3 then 
484                table.insert(rs, ha); ha = node_next(ha)
485             elseif i==2 then 
486                rw = ha; h.head, ha = node_remove(h.head, rw); 
487             else
488                ha = node_next(ha)
489             end
490          end
491          h.head = post_lown(rs, rw, h.head);
492       end
493       h = node_next(h)
494    end
495    return head
496 end 
497
498 local function post_high_hbox(head)
499    local ha = head; 
500    local rs = {};  -- rs: sequence of ruby_nodes, 
501    local rw = nil; -- rw: main whatsit
502    while ha do
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
508       if i==1 then 
509          head = post_lown(rs, rw, head); rs = {}; rw = nil
510          table.insert(rs, ha); ha = node_next(ha)
511       elseif i>=3 then 
512          table.insert(rs, ha); ha = node_next(ha)
513       elseif i==2 then 
514          rw = ha; head, ha = node_remove(head, rw); 
515       else
516          ha = node_next(ha)
517       end
518    end
519    return post_lown(rs, rw, head);
520 end
521
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)
524
525
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 無効
540                p = 0
541             end
542             set_attr(lp.value, attr_ruby_maxprep, p)
543          end
544          if Nq.prev_ruby then 
545             set_attr(lp, attr_ruby, 1)
546          end
547       else
548          set_attr(lp.value, attr_ruby_maxprep, 0)
549       end
550       return Np
551    end
552 end
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)
559       s = true
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 無効
565                p = 0
566             end
567             if has_attr(Nq.nuc.value, attr_ruby_mode)%4 >= 2 then
568                local q = has_attr(Nq.nuc.value, attr_ruby_maxprep)
569                if q < p then p = q
570                elseif q > p then
571                   set_attr(Nq.nuc.value, attr_ruby_maxprep, p)
572                end
573             end
574             set_attr(Nq.nuc.value, attr_ruby_maxpostp, p)
575          end
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)
581          end
582          if Np and Np.id == id_pbox_w then
583             set_attr(Np.nuc.value, attr_ruby_maxprep, 0)
584          end
585       end
586    end
587    return s
588 end
589
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)
594