2 -- luatexja/jfmglue.lua
4 luatexbase.provides_module({
5 name = 'luatexja.jfmglue',
8 description = 'Insertion process of JFM glues and kanjiskip',
10 module('luatexja.jfmglue', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
14 luatexja.load_module('stack'); local ltjs = luatexja.stack
15 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
16 luatexja.load_module('pretreat'); local ltjp = luatexja.pretreat
18 local has_attr = node.has_attribute
19 local set_attr = node.set_attribute
20 local insert_before = node.insert_before
21 local node_next = node.next
22 local round = tex.round
23 local uniq_id = 0 -- unique id
24 local ltjs_get_penalty_table = ltjs.get_penalty_table
25 local ltjf_font_metric_table = ltjf.font_metric_table
26 local ltjf_find_char_class = ltjf.find_char_class
28 local ligature_head = 1
29 local ligature_tail = 2
31 local id_glyph = node.id('glyph')
32 local id_hlist = node.id('hlist')
33 local id_vlist = node.id('vlist')
34 local id_rule = node.id('rule')
35 local id_ins = node.id('ins')
36 local id_mark = node.id('mark')
37 local id_adjust = node.id('adjust')
38 local id_disc = node.id('disc')
39 local id_whatsit = node.id('whatsit')
40 local id_math = node.id('math')
41 local id_glue = node.id('glue')
42 local id_kern = node.id('kern')
43 local id_penalty = node.id('penalty')
45 local id_glue_spec = node.id('glue_spec')
46 local id_jglyph = node.id('glyph') + 256 -- Japanese character
47 local id_box_like = node.id('hlist') + 256 -- vbox, shifted hbox
48 local id_pbox = node.id('hlist') + 512 -- already processed nodes (by \unhbox)
49 local id_pbox_w = node.id('hlist') + 513 -- cluster which consists of a whatsit
50 local sid_user = node.subtype('user_defined')
52 local sid_start_link = node.subtype('pdf_start_link')
53 local sid_start_thread = node.subtype('pdf_start_thread')
54 local sid_end_link = node.subtype('pdf_end_link')
55 local sid_end_thread = node.subtype('pdf_end_thread')
65 local IC_PROCESSED = 9
71 local attr_orig_char = luatexbase.attributes['ltj@origchar']
72 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
73 local attr_icflag = luatexbase.attributes['ltj@icflag']
74 local attr_autospc = luatexbase.attributes['ltj@autospc']
75 local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
76 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
77 local max_dimen = 1073741823
81 -------------------- Helper functions
83 local function copy_attr(new, old)
87 -- This function is called only for acquiring `special' characters.
88 local function fast_find_char_class(c,m)
89 return m.size_cache.chars[c] or 0
92 local spec_zero_glue = node.new(id_glue_spec)
93 spec_zero_glue.width = 0; spec_zero_glue.stretch_order = 0; spec_zero_glue.stretch = 0
94 spec_zero_glue.shrink_order = 0; spec_zero_glue.shrink = 0
96 local function get_zero_spec()
97 return node.copy(spec_zero_glue)
100 local function skip_table_to_spec(n)
101 local g = node.new(id_glue_spec)
102 local st = ltjs.get_skip_table(n, box_stack_level)
103 g.width = st.width; g.stretch = st.stretch; g.shrink = st.shrink
104 g.stretch_order = st.stretch_order; g.shrink_order = st.shrink_order
109 local function add_penalty(p,e)
110 if p.penalty>=10000 then
111 if e<=-10000 then p.penalty = 0 end
112 elseif p.penalty<=-10000 then
113 if e>=10000 then p.penalty = 0 end
115 p.penalty = p.penalty + e
116 if p.penalty>=10000 then p.penalty = 10000
117 elseif p.penalty<=-10000 then p.penalty = -10000 end
123 diffmet_rule = math.two_average
124 function math.two_add(a,b) return a+b end
125 function math.two_average(a,b) return (a+b)*0.5 end
127 -------------------- idea
128 -- 2 node の間に glue/kern/penalty を挿入する.
129 -- 基本方針: char node q と char node p の間
132 -- first: 最初の node,nuc: p,last: 最後の node
135 -- 実際の glue は Np.last, Nq.first の間に挿入される
136 -- Bp: Np.last, Nq.first の間の penalty node 達の配列
138 -- Np, Nq, Bp, widow_Bp について
140 -- 1回のループごとに Nq = Np, Np = (new table) となるのは効率が悪いので,
141 -- Np <-> Nq 入れ替え,その後 Np をクリアすることでテーブルを再利用.
142 -- 同様の関係は Bp, widow_Bp にも.
146 -- node x が non-char node のときは,x のみ
147 -- x が char_node のときは,
148 -- - x が \accent の第二引数だったとき
149 -- [kern2 kern y kern2] x の 3 node が核に加わる
150 -- - x の直後に \/ 由来 kern があったとき
151 -- その \/ 由来の kern が核に加わる
153 -- ins, mark, adjust, whatsit, penalty
155 -- Nq.last .. + .. Bp.first .... Bp[last] .... * .. Np.first
156 -- +: kern from LINEEND はここに入る
157 -- *: jfm glue はここに入る
159 local head -- the head of current list
162 local widow_Bp, widow_Np -- \jcharwidowpenalty 挿入位置管理用
164 local ihb_flag -- JFM グルー挿入抑止用 flag
165 -- on: \inhibitglue 指定時,hlist の周囲
167 -------------------- hlist 内の文字の検索
169 local first_char, last_char, find_first_char
171 local function check_box(box_ptr, box_end)
172 local p = box_ptr; local found_visible_node = false
174 find_first_char = false; first_char = nil; last_char = nil
177 while p and p~=box_end do
181 p = node_next(node_next(node_next(p))); pid = p.id
182 elseif IC_PROCESSED == has_attr(p, attr_icflag) then
183 p = node_next(p); pid = p.id
186 if pid==id_glyph then
188 if find_first_char then
189 first_char = p; find_first_char = false
191 last_char = p; found_visible_node = true; p=node_next(p)
192 if (not p) or p==box_end then return found_visible_node end
196 if pid==id_hlist then
197 if PACKED == has_attr(p, attr_icflag) then
198 if find_first_char then
199 first_char = p.head; find_first_char = false
201 last_char = p.head; found_visible_node = true
204 if check_box(p.head, nil) then found_visible_node = true end
205 else if find_first_char then
206 find_first_char = false
212 elseif not (pid==id_ins or pid==id_mark
213 or pid==id_adjust or pid==id_whatsit
214 or pid==id_penalty) then
215 found_visible_node = true
216 if find_first_char then
217 find_first_char = false
224 return found_visible_node
227 function check_box_high(Nx, box_ptr, box_end)
228 first_char = nil; last_char = nil; find_first_char = true
229 if check_box(box_ptr, box_end) then
231 if first_char.font == has_attr(first_char, attr_curjfnt) then
232 set_np_xspc_jachar(Nx, first_char)
234 set_np_xspc_alchar(Nx, first_char.char,first_char, ligature_head)
241 -------------------- Np の計算と情報取得
243 luatexbase.create_callback("luatexja.jfmglue.whatsit_getinfo", "data",
244 function (Np, lp, Nq, box_stack_level)
245 if Np.nuc then return Np
247 return Np -- your code
250 luatexbase.create_callback("luatexja.jfmglue.whatsit_after", "data",
251 function (stat, Nq, Np, box_stack_level) return false end)
256 local function set_attr_icflag_processed(p)
257 if (has_attr(p, attr_icflag) or 0)<= ITALIC then
258 set_attr(p, attr_uniqid, uniq_id)
259 set_attr(p, attr_icflag, PROCESSED)
263 local function check_next_ickern(lp)
264 if lp.id == id_kern and ITALIC == has_attr(lp, attr_icflag) then
265 set_attr(lp, attr_icflag, IC_PROCESSED)
266 set_attr(lp, attr_uniqid, uniq_id)
267 Np.last = lp; return node_next(lp)
269 Np.last = Np.nuc; return lp
273 local function calc_np_pbox(lp, last)
274 local uid = has_attr(lp, attr_uniqid)
275 Np.first = Np.first or lp; Np.id = id_pbox
276 local lpa = KINSOKU -- dummy=
277 while lp~=last and lpa>=PACKED and lpa~=BOXBDD
278 and uid == has_attr(lp, attr_uniqid) do
279 Np.nuc = lp; set_attr(lp, attr_uniqid, uniq_id)
280 lp = node_next(lp); lpa = has_attr(lp, attr_icflag) or 0
282 return check_next_ickern(lp)
286 local calc_np_auxtable = {
287 [id_glyph] = function(lp)
288 Np.first, Np.nuc = (Np.first or lp), lp;
289 Np.id = (lp.font == has_attr(lp, attr_curjfnt)) and id_jglyph or id_glyph
290 set_attr(lp, attr_uniqid, uniq_id)
291 --set_attr_icflag_processed(lp) treated in ltj-setwidth.lua
292 return true, check_next_ickern(node_next(lp));
294 [id_hlist] = function(lp)
295 Np.first = Np.first or lp; Np.last = lp; Np.nuc = lp;
296 set_attr_icflag_processed(lp)
297 Np.id = (lp.shift~=0) and id_box_like or id_hlist
298 return true, node_next(lp)
300 box_like = function(lp)
301 Np.first = Np.first or lp; Np.nuc = lp; Np.last = lp;
302 Np.id = id_box_like; set_attr_icflag_processed(lp);
303 return true, node_next(lp);
306 set_attr_icflag_processed(lp); return false, node_next(lp)
308 [id_whatsit] = function(lp)
309 if lp.subtype==sid_user then
310 if lp.user_id==30111 then
311 local lq = node_next(lp);
312 head = node.remove(head, lp); node.free(lp); ihb_flag = true
315 set_attr_icflag_processed(lp)
316 luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo",
317 Np, lp, Nq, box_stack_level)
319 Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc;
320 return true, node_next(lp)
322 return false, node_next(lp)
326 -- we do special treatment for these whatsit nodes.
327 if lp.subtype == sid_start_link or lp.subtype == sid_start_thread then
329 elseif lp.subtype == sid_end_link or lp.subtype == sid_end_thread then
330 Np.first, Nq.last = nil, lp;
332 set_attr_icflag_processed(lp); return false, node_next(lp)
335 [id_math] = function(lp)
336 Np.first, Np.nuc = (Np.first or lp), lp;
337 set_attr_icflag_processed(lp); lp = node_next(lp)
338 while lp.id~=id_math do
339 set_attr_icflag_processed(lp); lp = node_next(lp)
341 set_attr_icflag_processed(lp);
342 Np.last, Np.id = lp, id_math;
343 return true, node_next(lp);
345 discglue = function(lp)
346 Np.first, Np.nuc, Np.last = (Np.first or lp), lp, lp;
347 Np.id = lp.id; set_attr_icflag_processed(lp); return true, node_next(lp)
349 [id_kern] = function(lp)
350 Np.first = Np.first or lp
351 if lp.subtype==2 then
352 set_attr_icflag_processed(lp); lp = node_next(lp)
353 set_attr_icflag_processed(lp); lp = node_next(lp)
354 set_attr_icflag_processed(lp); lp = node_next(lp)
355 set_attr_icflag_processed(lp); Np.nuc = lp
356 Np.id = (lp.font == has_attr(lp, attr_curjfnt)) and id_jglyph or id_glyph
357 return true, check_next_ickern(node_next(lp));
359 Np.id = id_kern; set_attr_icflag_processed(lp);
360 Np.last = lp; return true, node_next(lp)
363 [id_penalty] = function(lp)
364 Bp[#Bp+1] = lp; set_attr_icflag_processed(lp);
365 return false, node_next(lp)
368 calc_np_auxtable[id_vlist] = calc_np_auxtable.box_like
369 calc_np_auxtable[id_rule] = calc_np_auxtable.box_like
370 calc_np_auxtable[13] = calc_np_auxtable.box_like
371 calc_np_auxtable[id_ins] = calc_np_auxtable.skip
372 calc_np_auxtable[id_mark] = calc_np_auxtable.skip
373 calc_np_auxtable[id_adjust] = calc_np_auxtable.skip
374 calc_np_auxtable[id_disc] = calc_np_auxtable.discglue
375 calc_np_auxtable[id_glue] = calc_np_auxtable.discglue
377 function calc_np(lp, last)
379 -- We assume lp = node_next(Np.last)
380 Np, Nq, ihb_flag = Nq, Np, false
381 for k in pairs(Np) do Np[k] = nil end
382 for k = 1,#Bp do Bp[k] = nil end
384 local lpa = has_attr(lp, attr_icflag) or 0
386 if lpa == BOXBDD then
387 local lq = node_next(lp)
388 head = node.remove(head, lp); node.free(lp); lp = lq
389 else return calc_np_pbox(lp, last)
392 k, lp = calc_np_auxtable[lp.id](lp)
393 if k then return lp end
401 local calc_np = calc_np
403 -- extract informations from Np
404 -- We think that "Np is a Japanese character" if Np.met~=nil,
405 -- "Np is an alphabetic character" if Np.pre~=nil,
406 -- "Np is not a character" otherwise.
410 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
411 function set_np_xspc_jachar(Nx, x)
412 local m = ltjf_font_metric_table[x.font]
413 local c = has_attr(x, attr_orig_char) or 0
414 local cls = ltjf_find_char_class(x.char, m)
415 if cls==0 and c ~= x.char then cls = ltjf_find_char_class(-c, m) end
416 Nx.class = cls; set_attr(x, attr_jchar_class, cls)
417 Nx.lend = m.size_cache.char_type[cls].kern[fast_find_char_class('lineend', m)] or 0
418 Nx.met, Nx.var, Nx.char = m, m.var, c
419 Nx.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
420 Nx.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
421 local y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
422 Nx.xspc_before, Nx.xspc_after = (y%2==1), (y>=2)
423 Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
425 local set_np_xspc_jachar = set_np_xspc_jachar
428 local floor = math.floor
429 function set_np_xspc_alchar(Nx, c,x, lig)
431 if lig == ligature_head then
432 while x.components and x.subtype and math.floor(x.subtype*0.5)%2==1 do
433 x = x.components; c = x.char
436 while x.components and x.subtype and math.floor(x.subtype*0.5)%2==1 do
437 x = node.tail(x.components); c = x.char
440 Nx.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
441 Nx.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
444 Nx.pre, Nx.post, Nx.char = 0, 0, -1
447 local y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
448 Nx.xspc_before, Nx.xspc_after = (y%2==1), (y>=2)
449 Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
451 local set_np_xspc_alchar = set_np_xspc_alchar
454 function extract_np()
455 local x, i = Np.nuc, Np.id;
456 if i == id_jglyph then return set_np_xspc_jachar(Np, x)
457 elseif i == id_glyph then return set_np_xspc_alchar(Np, x.char, x, ligature_head)
458 elseif i == id_hlist then Np.last_char = check_box_high(Np, x.head, nil)
459 elseif i == id_pbox then Np.last_char = check_box_high(Np, Np.first, node_next(Np.last))
460 elseif i == id_disc then Np.last_char = check_box_high(Np, x.replace, nil)
461 elseif i == id_math then return set_np_xspc_alchar(Np, -1, x)
465 -- change the information for the next loop
466 -- (will be done if Nx is an alphabetic character or a hlist)
467 function after_hlist(Nx)
468 local s = Nx.last_char
470 if s.font == has_attr(s, attr_curjfnt) then
471 set_np_xspc_jachar(Nx, s)
473 set_np_xspc_alchar(Nx, s.char, s, ligature_tail)
476 Nx.pre, Nx.met = nil, nil
480 function after_alchar(Nx)
482 return set_np_xspc_alchar(Nx, x.char,x, ligature_tail)
486 local after_hlist, after_alchar, extract_np = after_hlist, after_alchar, extract_np
488 -------------------- 最下層の処理
490 local function lineend_fix(g)
491 if g and g.id==id_kern then
493 elseif Nq.lend~=0 then
495 g = node.new(id_kern); copy_attr(g, Nq.nuc);
496 g.subtype = 1; g.kern = -Nq.lend;
497 set_attr(g, attr_icflag, LINEEND)
498 set_attr(g, attr_uniqid, uniq_id)
499 elseif g.id==id_kern then
500 g.kern = g.kern - Nq.lend
502 g.spec.width = g.spec.width - Nq.lend
508 -- change penalties (or create a new penalty, if needed)
509 local function handle_penalty_normal(post, pre, g)
510 local a = (pre or 0) + (post or 0)
512 if (a~=0 and not(g and g.id==id_kern)) or Nq.lend~=0 then
513 local p = node.new(id_penalty); copy_attr(p, Nq.nuc)
514 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
516 head = insert_before(head, Np.first, p)
518 set_attr(p, attr_icflag, KINSOKU)
519 set_attr(p, attr_uniqid, uniq_id)
521 --else for _, v in pairs(Bp) do v.penalty = v.penalty + a end
522 else for _, v in pairs(Bp) do add_penalty(v,a) end
526 local function handle_penalty_always(post, pre, g)
527 local a = (pre or 0) + (post or 0)
529 if not (g and g.id==id_glue) or Nq.lend~=0 then
530 local p = node.new(id_penalty); copy_attr(p, Nq.nuc)
531 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
533 head = insert_before(head, Np.first, p)
535 set_attr(p, attr_icflag, KINSOKU)
536 set_attr(p, attr_uniqid, uniq_id)
538 else for _, v in pairs(Bp) do add_penalty(v,a) end
542 local function handle_penalty_suppress(post, pre, g)
543 local a = (pre or 0) + (post or 0)
545 if g and g.id==id_glue then
546 local p = node.new(id_penalty); copy_attr(p, Nq.nuc)
547 p.penalty = 10000; head = insert_before(head, Np.first, p)
549 set_attr(p, attr_icflag, KINSOKU)
550 set_attr(p, attr_uniqid, uniq_id)
552 else for _, v in pairs(Bp) do add_penalty(v,a) end
556 -- 和文文字間の JFM glue を node 化
557 local function new_jfm_glue(Nn, bc, ac)
558 -- bc, ac: char classes
560 local z = Nn.met.size_cache.char_type[bc]
561 local gt = z.glue[ac]
563 local h = node.new(id_glue_spec)
564 h.width, h.stretch, h.shrink = gt[1], gt[2], gt[3]
565 h.stretch_order, h.shrink_order = 0, 0
566 g = node.new(id_glue); copy_attr(g, Nn.nuc)
567 g.subtype = 0; g.spec = h
568 set_attr(g, attr_icflag, FROM_JFM); set_attr(g, attr_uniqid, uniq_id)
569 elseif z.kern[ac] then
570 g = node.new(id_kern); copy_attr(g, Nn.nuc)
571 g.subtype = 1; g.kern = z.kern[ac]
572 set_attr(g, attr_icflag, FROM_JFM); set_attr(g, attr_uniqid, uniq_id)
577 -- Nq.last (kern w) .... (glue/kern g) Np.first
578 local function real_insert(w, g)
580 local h = node.new(id_kern); copy_attr(h, Nq.nuc)
581 set_attr(h, attr_icflag, LINE_END)
582 set_attr(h, attr_uniqid, uniq_id)
583 h.kern = w; h.subtype = 1
584 head = node.insert_after(head, Nq.last, h)
587 head = insert_before(head, Np.first, g)
592 -------------------- 和文文字間空白量の決定
595 local function get_kanji_skip_from_jfm(Nn)
596 local i = Nn.met.size_cache.kanjiskip
598 return { i[1], i[2], i[3] }
602 local function get_kanjiskip()
603 local g = node.new(id_glue); copy_attr(g, Nq.nuc)
604 if Np.auto_kspc or Nq.auto_kspc then
605 if kanji_skip.width == max_dimen then
606 local gx = node.new(id_glue_spec);
607 gx.stretch_order = 0; gx.shrink_order = 0
608 local bk = get_kanji_skip_from_jfm(Nq)
610 if (Np.met.size_cache==Nq.met.size_cache) and (Nq.var==Np.var) then
613 ak = get_kanji_skip_from_jfm(Np)
617 gx.width = round(diffmet_rule(bk[1], ak[1]))
618 gx.stretch = round(diffmet_rule(bk[2], ak[2]))
619 gx.shrink = -round(diffmet_rule(-bk[3], -ak[3]))
621 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
624 gx.width = ak[1]; gx.stretch = ak[2]; gx.shrink = ak[3]
625 else node.free(gx); gx = get_zero_spec() -- fallback
628 else g.spec=node.copy(kanji_skip) end
630 g.spec = get_zero_spec()
632 set_attr(g, attr_icflag, KANJI_SKIP)
633 set_attr(g, attr_uniqid, uniq_id)
637 local function calc_ja_ja_aux(gb,ga)
641 if not ga then return gb end
642 local k = node.type(gb.id) .. node.type(ga.id)
643 if k == 'glueglue' then
645 gb.spec.width = round(diffmet_rule(gb.spec.width, ga.spec.width))
646 gb.spec.stretch = round(diffmet_rule(gb.spec.stretch,ga.spec.shrink))
647 gb.spec.shrink = -round(diffmet_rule(-gb.spec.shrink, -ga.spec.shrink))
650 elseif k == 'kernkern' then
652 gb.kern = round(diffmet_rule(gb.kern, ga.kern))
655 elseif k == 'kernglue' then
656 -- gb: kern, ga: glue
657 ga.spec.width = round(diffmet_rule(gb.kern,ga.spec.width))
658 ga.spec.stretch = round(diffmet_rule(ga.spec.stretch, 0))
659 ga.spec.shrink = -round(diffmet_rule(-ga.spec.shrink, 0))
663 -- gb: glue, ga: kern
664 gb.spec.width = round(diffmet_rule(ga.kern, gb.spec.width))
665 gb.spec.stretch = round(diffmet_rule(gb.spec.stretch, 0))
666 gb.spec.shrink = -round(diffmet_rule(-gb.spec.shrink, 0))
673 local function calc_ja_ja_glue()
674 if ihb_flag then return nil
675 elseif (Nq.met.size_cache==Np.met.size_cache) and (Nq.var==Np.var) then
676 return new_jfm_glue(Nq, Nq.class, Np.class)
678 return calc_ja_ja_aux(new_jfm_glue(Nq, Nq.class, fast_find_char_class('diffmet',Nq.met)),
679 new_jfm_glue(Np, fast_find_char_class('diffmet',Np.met), Np.class))
683 -------------------- 和欧文間空白量の決定
686 local function get_xkanji_skip_from_jfm(Nn)
687 local i = Nn.met.size_cache.xkanjiskip
689 return i and { i[1], i[2], i[3] }
693 local function get_xkanjiskip(Nn)
694 local g = node.new(id_glue); copy_attr(g, Nn.nuc)
695 if Nq.xspc_after and Np.xspc_before and (Nq.auto_xspc or Np.auto_xspc) then
696 if xkanji_skip.width == max_dimen then
697 local gx = node.new(id_glue_spec);
698 gx.stretch_order = 0; gx.shrink_order = 0
699 local bk = get_xkanji_skip_from_jfm(Nn)
701 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
702 else node.free(gx); gx = get_zero_spec() -- fallback
705 else g.spec=node.copy(xkanji_skip) end
707 g.spec = get_zero_spec()
709 set_attr(g, attr_icflag, XKANJI_SKIP)
710 set_attr(g, attr_uniqid, uniq_id)
715 -------------------- 隣接した「塊」間の処理
717 local function get_OA_skip()
719 return new_jfm_glue(Np, fast_find_char_class(((Nq.id == id_math and -1) or 'jcharbdd'), Np.met), Np.class)
723 local function get_OB_skip()
725 return new_jfm_glue(Nq, Nq.class, fast_find_char_class(((Np.id == id_math and -1) or'jcharbdd'), Nq.met))
730 -- (anything) .. jachar
731 local function handle_np_jachar(mode)
732 if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id==id_pbox_w) and Nq.met) then
733 local g = lineend_fix(calc_ja_ja_glue() or get_kanjiskip()) -- M->K
734 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
735 elseif Nq.met then -- Nq.id==id_hlist
736 local g = get_OA_skip() or get_kanjiskip() -- O_A->K
737 handle_penalty_normal(0, Np.pre, g); real_insert(0, g)
739 local g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
740 if Nq.id==id_hlist then Nq.post = 0 end
741 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
743 local g = get_OA_skip() -- O_A
744 if Nq.id==id_glue then handle_penalty_normal(0, Np.pre, g)
745 elseif Nq.id==id_kern then handle_penalty_suppress(0, Np.pre, g)
746 else handle_penalty_always(0, Np.pre, g)
750 -- \jcharwidowpenalty 挿入予定箇所更新
751 if mode and ltjs_get_penalty_table('kcat', Np.char, 0, box_stack_level)%2~=1 then
752 widow_Np.first, widow_Bp, Bp = Np.first, Bp, widow_Bp
756 -- jachar .. (anything)
757 local function handle_nq_jachar()
759 if Np.id==id_hlist then Np.pre = 0 end
760 local g = lineend_fix(get_OB_skip() or get_xkanjiskip(Nq)) -- O_B->X
761 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
763 local g = lineend_fix(get_OB_skip()) -- O_B
764 if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
765 elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
766 else handle_penalty_always(Nq.post, 0, g)
768 real_insert(Nq.lend, g)
772 -- (anything) .. (和文文字で始まる hlist)
773 local function handle_np_ja_hlist()
774 if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id == id_pbox_w) and Nq.met) then
775 local g = lineend_fix(get_OB_skip() or get_kanjiskip()) -- O_B->K
776 handle_penalty_normal(Nq.post, 0, g); real_insert(Nq.lend, g)
777 elseif Nq.met then -- Nq.id==id_hlist
778 local g = get_kanjiskip() -- K
779 handle_penalty_suppress(0, 0, g); real_insert(0, g)
781 local g = get_xkanjiskip(Np) -- X
782 handle_penalty_suppress(0, 0, g); real_insert(0, g)
786 -- (和文文字で終わる hlist) .. (anything)
787 local function handle_nq_ja_hlist()
789 local g = get_xkanjiskip(Nq) -- X
790 handle_penalty_suppress(0, 0, g); real_insert(0, g)
794 -- Nq が前側のクラスタとなることによる修正
795 local function adjust_nq()
796 if Nq.id==id_glyph then after_alchar(Nq)
797 elseif Nq.id==id_hlist or Nq.id==id_pbox or Nq.id==id_disc then after_hlist(Nq)
798 elseif Nq.id == id_pbox_w then
799 luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
800 false, Nq, Np, box_stack_level)
804 -------------------- 開始・終了時の処理
807 local function handle_list_tail(mode)
810 -- the current list is to be line-breaked:
811 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
813 g = node.new(id_kern); g.subtype = 0; g.kern = Np.lend
814 copy_attr(g, Np.nuc); set_attr(g, attr_icflag, BOXBDD)
815 node.insert_after(head, Np.last, g)
818 -- Insert \jcharwidowpenalty
819 Bp = widow_Bp; Np = widow_Np; Nq.lend = 0
821 handle_penalty_normal(0,
822 ltjs_get_penalty_table('jwp', 0, 0, box_stack_level))
825 -- the current list is the contents of a hbox
826 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
827 local g = new_jfm_glue(Np, Np.class, fast_find_char_class('boxbdd',Np.met))
829 set_attr(g, attr_icflag, BOXBDD)
830 head = node.insert_after(head, Np.last, g)
837 local function handle_list_head(par_indented)
838 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
842 g = new_jfm_glue(Np, fast_find_char_class('parbdd',Np.met), Np.class)
844 g = new_jfm_glue(Np, fast_find_char_class('boxbdd',Np.met), Np.class)
847 set_attr(g, attr_icflag, BOXBDD)
848 if g.id==id_glue and #Bp==0 then
849 local h = node.new(id_penalty); copy_attr(h, Np.nuc)
850 h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
852 head = insert_before(head, Np.first, g)
859 -- return value: (the initial cursor lp), (last node)
860 local function init_var(mode)
862 if uniq_id == 0x7FFFFFF then uniq_id = 0 end
863 Bp = {}; widow_Bp = {}; widow_Np = {first = nil}
864 box_stack_level = ltjp.box_stack_level
865 kanji_skip=skip_table_to_spec('kanjiskip')
866 xkanji_skip=skip_table_to_spec('xkanjiskip')
868 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
869 first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil,
870 post=nil, pre=nil, xspc_after=nil, xspc_before=nil,
873 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
874 first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil,
875 post=nil, pre=nil, xspc_after=nil, xspc_before=nil,
878 -- the current list is to be line-breaked:
879 -- hbox from \parindent is skipped.
880 local lp, par_indented = head, false
881 while lp and ((lp.id==id_whatsit and lp.subtype~=sid_user)
882 or ((lp.id==id_hlist) and (lp.subtype==3))) do
883 if (lp.id==id_hlist) and (lp.subtype==3) then par_indented = true end
885 return lp, node.tail(head), par_indented
887 -- the current list is the contents of a hbox:
888 -- insert a sentinelEG
889 local g = node.new(id_kern)
890 node.insert_after(head, node.tail(head), g); last = g
891 return head, g, false
895 local function cleanup(mode, last)
896 -- adjust attr_icflag for avoiding error
897 tex.setattribute('global', attr_icflag, 0)
898 node.free(kanji_skip); node.free(xkanji_skip)
900 local h = node_next(head)
901 if h.id == id_penalty and h.penalty == 10000 then
903 if h.id == id_glue and h.subtype == 15 and not h.next then
909 head = node.remove(head, last); node.free(last);-- remove the sentinel
913 -------------------- 外部から呼ばれる関数
916 function main(ahead, mode)
917 if not ahead then return ahead end
919 local lp, last, par_indented = init_var(mode);
920 lp = calc_np(lp, last)
922 extract_np(); handle_list_head(par_indented)
924 return cleanup(mode, last)
926 lp = calc_np(lp, last)
928 extract_np(); adjust_nq()
930 if Np.id == id_jglyph then
931 handle_np_jachar(mode)
933 if Np.id==id_hlist then handle_np_ja_hlist()
934 else handle_np_jachar() end
936 if Nq.id==id_hlist then handle_nq_ja_hlist()
937 else handle_nq_jachar() end
939 lp = calc_np(lp, last)
941 handle_list_tail(mode)
942 return cleanup(mode, last)
947 function create_inhibitglue_node()
948 local tn = node.new(id_whatsit, sid_user)
949 tn.user_id=30111; tn.type=100; tn.value=1
953 -- Node for indicating beginning of a paragraph
955 function create_beginpar_node()
956 local tn = node.new(id_whatsit, sid_user)
957 tn.user_id=30114; tn.type=100; tn.value=1
963 local function whatsit_callback(Np, lp, Nq, bsl)
964 if Np and Np.nuc then return Np
965 elseif Np and lp.user_id == 30114 then
966 Np.first = lp; Np.nuc = lp; Np.last = lp
969 Np.pre = 0; Np.post = 0
970 Np.xspc_before = false
971 Np.xspc_after = false
976 local function whatsit_after_callback(s, Nq, Np, bsl)
977 if not s and Nq.nuc.user_id == 30114 then
978 local x, y = node.prev(Nq.nuc), Nq.nuc
979 Nq.first, Nq.nuc, Nq.last = x, x, x
980 head = node.remove(head, y)
985 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
986 "luatexja.beginpar.np_info", 1)
987 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
988 "luatexja.beginpar.np_info_after", 1)