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)
13 luatexja.load_module('base'); local ltjb = luatexja.base
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 node_type = node.type
19 local node_new = node.new
20 local node_remove = node.remove
21 local node_prev = node.prev
22 local node_next = node.next
23 local node_copy = node.copy
24 local node_tail = node.tail
25 local node_free = node.free
26 local has_attr = node.has_attribute
27 local set_attr = node.set_attribute
28 local node_insert_before = node.insert_before
29 local node_insert_after = node.insert_after
30 local round = tex.round
31 local table_insert = table.insert
32 local uniq_id = 0 -- unique id
34 local id_glyph = node.id('glyph')
35 local id_hlist = node.id('hlist')
36 local id_vlist = node.id('vlist')
37 local id_rule = node.id('rule')
38 local id_ins = node.id('ins')
39 local id_mark = node.id('mark')
40 local id_adjust = node.id('adjust')
41 local id_disc = node.id('disc')
42 local id_whatsit = node.id('whatsit')
43 local id_math = node.id('math')
44 local id_glue = node.id('glue')
45 local id_kern = node.id('kern')
46 local id_penalty = node.id('penalty')
48 local id_glue_spec = node.id('glue_spec')
49 local id_jglyph = node.id('glyph') + 256 -- Japanese character
50 local id_box_like = node.id('hlist') + 256 -- vbox, shifted hbox
51 local id_pbox = node.id('hlist') + 512 -- already processed nodes (by \unhbox)
52 local id_pbox_w = node.id('hlist') + 513 -- cluster which consists of a whatsit
53 local sid_user = node.subtype('user_defined')
63 local IC_PROCESSED = 9
69 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
70 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
71 local attr_icflag = luatexbase.attributes['ltj@icflag']
72 local attr_autospc = luatexbase.attributes['ltj@autospc']
73 local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
74 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
75 local max_dimen = 1073741823
77 local ltjs_get_penalty_table = ltjs.get_penalty_table
78 local ltjs_get_skip_table = ltjs.get_skip_table
79 local ltjf_find_char_class = ltjf.find_char_class
80 local ltjf_font_metric_table = ltjf.font_metric_table
81 local ltjf_metrics = ltjf.metrics
83 local par_indented -- is the paragraph indented?
85 -------------------- Helper functions
87 -- This function is called only for acquiring `special' characters.
88 local function fast_find_char_class(c,m)
89 return m.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)/2 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
160 local last -- the last node of current list
161 local lp -- 外側での list 走査時のカーソル
164 local widow_Bp, widow_Np -- \jcharwidowpenalty 挿入位置管理用
166 local ihb_flag -- JFM グルー挿入抑止用 flag
167 -- on: \inhibitglue 指定時,hlist の周囲
169 -------------------- hlist 内の文字の検索
171 local first_char, last_char, find_first_char
173 local function check_box(box_ptr, box_end)
174 local p = box_ptr; local found_visible_node = false
176 find_first_char = false; first_char = nil; last_char = nil
179 while p and p~=box_end do
183 p = node_next(node_next(node_next(p))); pid = p.id
184 elseif has_attr(p, attr_icflag)==IC_PROCESSED then
185 p = node_next(p); pid = p.id
188 if pid==id_glyph then
190 if find_first_char then
191 first_char = p; find_first_char = false
193 last_char = p; found_visible_node = true; p=node_next(p)
194 if (not p) or p==box_end then return found_visible_node end
197 if pid==id_hlist then
198 if has_attr(p, attr_icflag)==PACKED then
199 for q in node.traverse_id(id_glyph, p.head) do
200 if find_first_char then
201 first_char = q; find_first_char = false
203 last_char = q; found_visible_node = true; break
207 if check_box(p.head, nil) then found_visible_node = true end
208 else if find_first_char then
209 find_first_char = false
215 elseif not (pid==id_ins or pid==id_mark
216 or pid==id_adjust or pid==id_whatsit
217 or pid==id_penalty) then
218 found_visible_node = true
219 if find_first_char then
220 find_first_char = false
227 return found_visible_node
230 function check_box_high(Nx, box_ptr, box_end)
231 first_char = nil; last_char = nil; find_first_char = true
232 if check_box(box_ptr, box_end) then
234 if first_char.font == has_attr(first_char, attr_curjfnt) then
235 set_np_xspc_jachar(Nx, first_char)
237 set_np_xspc_alchar(Nx, first_char.char,first_char, ligature_head)
244 -------------------- Np の計算と情報取得
246 luatexbase.create_callback("luatexja.jfmglue.whatsit_getinfo", "data",
247 function (Np, lp, Nq, box_stack_level)
248 if Np.nuc then return Np
250 return Np -- your code
253 luatexbase.create_callback("luatexja.jfmglue.whatsit_after", "data",
254 function (stat, Nq, Np, box_stack_level) return false end)
257 local function set_attr_icflag_processed(p)
258 local a = has_attr(p, attr_icflag) or 0
260 set_attr(p, attr_uniqid, uniq_id)
261 set_attr(p, attr_icflag, PROCESSED)
265 local function check_next_ickern()
266 if lp.id == id_kern and has_attr(lp, attr_icflag)==ITALIC then
267 set_attr(lp, attr_icflag, IC_PROCESSED)
268 set_attr(lp, attr_uniqid, uniq_id)
269 Np.last = lp; lp = node_next(lp)
270 else Np.last = Np.nuc end
273 local function calc_np_pbox()
274 local uid = has_attr(lp, attr_uniqid)
275 Np.first = lp; Np.id = id_pbox
276 lpa = KINSOKU -- dummy=
277 while lp~=last and lpa>=PACKED and lpa~=BOXBDD
278 and has_attr(lp, attr_uniqid) == uid 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
285 local calc_np_auxtable = {
286 [id_glyph] = function()
288 if lp.font == has_attr(lp, attr_curjfnt) then
293 Np.first = lp; Np.nuc = lp; set_attr_icflag_processed(lp)
294 lp = node_next(lp); check_next_ickern(); return true
296 [id_hlist] = function()
297 Np.first = lp; Np.last = lp; Np.nuc = lp;
298 set_attr_icflag_processed(lp)
304 lp = node_next(lp); return true
306 [id_vlist] = function()
307 Np.first = lp; Np.nuc = lp; Np.last = lp;
308 Np.id = id_box_like; set_attr_icflag_processed(lp);
309 lp = node_next(lp); return true
311 [id_rule] = function()
312 Np.first = lp; Np.nuc = lp; Np.last = lp;
313 Np.id = id_box_like; set_attr_icflag_processed(lp);
314 lp = node_next(lp); return true
316 [id_ins] = function()
317 set_attr_icflag_processed(lp); lp = node_next(lp)
320 [id_mark] = function()
321 set_attr_icflag_processed(lp); lp = node_next(lp)
324 [id_adjust] = function()
325 set_attr_icflag_processed(lp); lp = node_next(lp)
328 [id_disc] = function()
329 Np.first = lp; Np.nuc = lp; set_attr_icflag_processed(lp);
330 Np.last = lp; Np.id = id_disc; lp = node_next(lp); return true
332 [id_whatsit] = function()
333 if lp.subtype==sid_user then
334 if lp.user_id==30111 then
335 local lq = node_next(lp)
336 head = node_remove(head, lp); node_free(lp); lp = lq; ihb_flag = true
338 set_attr_icflag_processed(lp)
339 luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo"
340 , Np, lp, Nq, box_stack_level)
343 Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc; return true
347 set_attr_icflag_processed(lp)
351 [id_math] = function()
352 Np.first = lp; Np.nuc = lp;
353 set_attr_icflag_processed(lp); lp = node_next(lp)
354 while lp.id~=id_math do
355 set_attr_icflag_processed(lp); lp = node_next(lp)
357 set_attr_icflag_processed(lp);
358 Np.last = lp; Np.id = id_math; lp = node_next(lp);
361 [id_glue] = function()
362 Np.first = lp; Np.nuc = lp; set_attr_icflag_processed(lp);
363 Np.last = lp; Np.id = id_glue; lp = node_next(lp); return true
365 [id_kern] = function()
367 if lp.subtype==2 then
368 set_attr_icflag_processed(lp); lp = node_next(lp)
369 set_attr_icflag_processed(lp); lp = node_next(lp)
370 set_attr_icflag_processed(lp); lp = node_next(lp)
371 set_attr_icflag_processed(lp); Np.nuc = lp
372 if lp.font == has_attr(lp, attr_curjfnt) then
377 lp = node_next(lp); check_next_ickern();
379 Np.id = id_kern; set_attr_icflag_processed(lp);
380 Np.last = lp; lp = node_next(lp)
384 [id_penalty] = function()
385 Bp[#Bp+1] = lp; set_attr_icflag_processed(lp);
386 lp = node_next(lp); return false
389 Np.first = lp; Np.nuc = lp; Np.last = lp;
390 Np.id = id_box_like; set_attr_icflag_processed(lp);
391 lp = node_next(lp); return true
395 local function calc_np()
396 -- We assume lp = node_next(Np.last)
398 Nr = Nq; for k in pairs(Nr) do Nr[k] = nil end
400 for k in pairs(Bp) do Bp[k] = nil end
403 lpa = has_attr(lp, attr_icflag) or 0
405 if lpa == BOXBDD then
406 local lq = node_next(lp)
407 head = node_remove(head, lp); node_free(lp); lp = lq
408 else calc_np_pbox(); return
410 elseif calc_np_auxtable[lp.id]() then return end
415 -- extract informations from Np
416 -- We think that "Np is a Japanese character" if Np.met~=nil,
417 -- "Np is an alphabetic character" if Np.pre~=nil,
418 -- "Np is not a character" otherwise.
421 function set_np_xspc_jachar(Nx, x)
422 local z = ltjf_font_metric_table[x.font]
424 local cls = ltjf_find_char_class(c, z)
425 local m = ltjf_metrics[z.jfm]
426 set_attr(x, attr_jchar_class, cls)
432 Nx.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
433 Nx.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
434 z = fast_find_char_class('lineend', m)
435 local y = m.size_cache[Nx.size].char_type[Nx.class]
436 if y.kern and y.kern[z] then
441 y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
442 Nx.xspc_before = (y%2==1)
443 Nx.xspc_after = (y>=2)
444 Nx.auto_kspc = (has_attr(x, attr_autospc)==1)
445 Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
449 local ligature_head = 1
450 local ligature_tail = 2
451 function set_np_xspc_alchar(Nx, c,x, lig)
453 if lig == ligature_head then
454 while x.components and x.subtype and math.floor(x.subtype/2)%2==1 do
455 x = x.components; c = x.char
458 while x.components and x.subtype and math.floor(x.subtype/2)%2==1 do
459 x = node_tail(x.components); c = x.char
462 Nx.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
463 Nx.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
466 Nx.pre = 0; Nx.post = 0; Nx.char = -1
469 local y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
470 Nx.xspc_before = (y%2==1)
471 Nx.xspc_after = (y>=2)
472 Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
476 local function extract_np()
478 if Np.id == id_jglyph then set_np_xspc_jachar(Np, x)
479 elseif Np.id == id_glyph then set_np_xspc_alchar(Np, x.char, x, ligature_head)
480 elseif Np.id == id_hlist then Np.last_char = check_box_high(Np, x.head, nil)
481 elseif Np.id == id_pbox then Np.last_char = check_box_high(Np, Np.first, node.next(Np.last))
482 elseif Np.id == id_disc then Np.last_char = check_box_high(Np, x.replace, nil)
483 elseif Np.id == id_math then set_np_xspc_alchar(Np, -1, x)
487 -- change the information for the next loop
488 -- (will be done if Nx is an alphabetic character or a hlist)
489 function after_hlist(Nx)
491 if Nx.last_char.font == has_attr(Nx.last_char, attr_curjfnt) then
492 set_np_xspc_jachar(Nx, Nx.last_char);
494 set_np_xspc_alchar(Nx, Nx.last_char.char,Nx.last_char, ligature_tail)
497 Nx.pre = nil; Nx.met = nil
501 local function after_alchar(Nx)
503 set_np_xspc_alchar(Nx, x.char,x, ligature_tail)
507 -------------------- 最下層の処理
509 local function lineend_fix(g)
510 if g and g.id==id_kern then
512 elseif Nq.lend~=0 then
514 g = node_new(id_kern); g.subtype = 1; g.kern = -Nq.lend;
515 set_attr(g, attr_icflag, LINEEND)
516 set_attr(g, attr_uniqid, uniq_id)
517 elseif g.id==id_kern then
518 g.kern = g.kern - Nq.lend
520 g.spec.width = g.spec.width - Nq.lend
526 -- change penalties (or create a new penalty, if needed)
527 local function handle_penalty_normal(post, pre, g)
528 local a = (pre or 0) + (post or 0)
530 if (a~=0 and not(g and g.id==id_kern)) or Nq.lend~=0 then
531 local p = node_new(id_penalty)
532 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
534 head = node_insert_before(head, Np.first, p)
536 set_attr(p, attr_icflag, KINSOKU)
537 set_attr(p, attr_uniqid, uniq_id)
539 else for i, v in pairs(Bp) do add_penalty(v,a) end
543 local function handle_penalty_always(post, pre, g)
544 local a = (pre or 0) + (post or 0)
546 if not (g and g.id==id_glue) or Nq.lend~=0 then
547 local p = node_new(id_penalty)
548 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
550 head = node_insert_before(head, Np.first, p)
552 set_attr(p, attr_icflag, KINSOKU)
553 set_attr(p, attr_uniqid, uniq_id)
555 else for i, v in pairs(Bp) do add_penalty(v,a) end
559 local function handle_penalty_suppress(post, pre, g)
560 local a = (pre or 0) + (post or 0)
562 if g and g.id==id_glue then
563 local p = node_new(id_penalty)
564 p.penalty = 10000; head = node_insert_before(head, Np.first, p)
566 set_attr(p, attr_icflag, KINSOKU)
567 set_attr(p, attr_uniqid, uniq_id)
569 else for i, v in pairs(Bp) do add_penalty(v,a) end
573 -- 和文文字間の JFM glue を node 化
574 local function new_jfm_glue(Nn, bc, ac)
575 -- bc, ac: char classes
577 local z = Nn.met.size_cache[Nn.size].char_type[bc]
578 if z.glue and z.glue[ac] then
579 local h = node_new(id_glue_spec)
580 h.width = z.glue[ac][1]
581 h.stretch = z.glue[ac][2]
582 h.shrink = z.glue[ac][3]
583 h.stretch_order=0; h.shrink_order=0
584 g = node_new(id_glue)
585 g.subtype = 0; g.spec = h
586 elseif z.kern and z.kern[ac] then
587 g = node_new(id_kern)
588 g.subtype = 1; g.kern = z.kern[ac]
591 set_attr(g, attr_icflag, FROM_JFM); set_attr(g, attr_uniqid, uniq_id)
596 -- Nq.last (kern w) .... (glue/kern g) Np.first
597 local function real_insert(w, g)
599 local h = node_new(id_kern)
600 set_attr(h, attr_icflag, LINE_END)
601 set_attr(h, attr_uniqid, uniq_id)
602 h.kern = w; h.subtype = 1
603 head = node_insert_after(head, Nq.last, h)
606 head = node_insert_before(head, Np.first, g)
611 -------------------- 和文文字間空白量の決定
614 local function get_kanji_skip_from_jfm(Nn)
615 local i = Nn.met.size_cache[Nn.size].kanjiskip
617 return { i[1], i[2], i[3] }
621 local function get_kanjiskip()
622 local g = node_new(id_glue)
623 if Np.auto_kspc or Nq.auto_kspc then
624 if kanji_skip.width == max_dimen then
625 local gx = node_new(id_glue_spec);
626 gx.stretch_order = 0; gx.shrink_order = 0
627 local bk = get_kanji_skip_from_jfm(Nq)
629 if (Np.met==Nq.met) and (Nq.size==Np.size) and (Nq.var==Np.var) then
632 ak = get_kanji_skip_from_jfm(Np)
636 gx.width = round(diffmet_rule(bk[1], ak[1]))
637 gx.stretch = round(diffmet_rule(bk[2], ak[2]))
638 gx.shrink = -round(diffmet_rule(-bk[3], -ak[3]))
640 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
643 gx.width = ak[1]; gx.stretch = ak[2]; gx.shrink = ak[3]
644 else node_free(gx); gx = get_zero_spec() -- fallback
647 else g.spec=node_copy(kanji_skip); node_free(gx) end
649 g.spec = get_zero_spec(); node_free(gx)
651 set_attr(g, attr_icflag, KANJI_SKIP)
652 set_attr(g, attr_uniqid, uniq_id)
656 local function calc_ja_ja_aux(gb,ga)
660 if not ga then return gb end
661 local k = node.type(gb.id) .. node.type(ga.id)
662 if k == 'glueglue' then
664 gb.spec.width = round(diffmet_rule(gb.spec.width, ga.spec.width))
665 gb.spec.stretch = round(diffmet_rule(gb.spec.stretch,ga.spec.shrink))
666 gb.spec.shrink = -round(diffmet_rule(-gb.spec.shrink, -ga.spec.shrink))
669 elseif k == 'kernkern' then
671 gb.kern = round(diffmet_rule(gb.kern, ga.kern))
674 elseif k == 'kernglue' then
675 -- gb: kern, ga: glue
676 ga.spec.width = round(diffmet_rule(gb.kern,ga.spec.width))
677 ga.spec.stretch = round(diffmet_rule(ga.spec.stretch, 0))
678 ga.spec.shrink = -round(diffmet_rule(-ga.spec.shrink, 0))
682 -- gb: glue, ga: kern
683 gb.spec.width = round(diffmet_rule(ga.kern, gb.spec.width))
684 gb.spec.stretch = round(diffmet_rule(gb.spec.stretch, 0))
685 gb.spec.shrink = -round(diffmet_rule(-gb.spec.shrink, 0))
692 local function calc_ja_ja_glue()
693 if ihb_flag then return nil
694 elseif (Nq.size==Np.size) and (Nq.met==Np.met) and (Nq.var==Np.var) then
695 return new_jfm_glue(Nq, Nq.class, Np.class)
697 local g = new_jfm_glue(Nq, Nq.class,
698 fast_find_char_class('diffmet',Nq.met))
699 local h = new_jfm_glue(Np, fast_find_char_class('diffmet',Np.met),
701 return calc_ja_ja_aux(g,h)
705 -------------------- 和欧文間空白量の決定
708 local function get_xkanji_skip_from_jfm(Nn)
709 local i = Nn.met.size_cache[Nn.size].xkanjiskip
711 return { i[1], i[2], i[3] }
715 local function get_xkanjiskip(Nn)
716 local g = node_new(id_glue)
717 if Nq.xspc_after and Np.xspc_before and (Nq.auto_xspc or Np.auto_xspc) then
718 if xkanji_skip.width == max_dimen then
719 local gx = node_new(id_glue_spec);
720 gx.stretch_order = 0; gx.shrink_order = 0
721 local bk = get_xkanji_skip_from_jfm(Nn)
723 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
724 else node_free(gx); gx = get_zero_spec() -- fallback
727 else g.spec=node_copy(xkanji_skip) end
729 g.spec = get_zero_spec()
731 set_attr(g, attr_icflag, XKANJI_SKIP)
732 set_attr(g, attr_uniqid, uniq_id)
737 -------------------- 隣接した「塊」間の処理
739 local function get_OA_skip()
741 local c = Nq.char or 'jcharbdd'
742 return new_jfm_glue(Np, fast_find_char_class(c,Np.met), Np.class)
746 local function get_OB_skip()
748 local c = Np.char or 'jcharbdd'
749 return new_jfm_glue(Nq, Nq.class, fast_find_char_class(c,Nq.met))
754 -- (anything) .. jachar
755 local function handle_np_jachar()
757 if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id==id_pbox_w) and Nq.met) then
758 g = calc_ja_ja_glue() or get_kanjiskip() -- M->K
760 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
761 elseif Nq.met then -- Nq.id==id_hlist
762 g = get_OA_skip() or get_kanjiskip() -- O_A->K
763 handle_penalty_normal(0, Np.pre, g); real_insert(0, g)
765 g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
766 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
768 g = get_OA_skip() -- O_A
769 if Nq.id==id_glue then handle_penalty_normal(0, Np.pre, g)
770 elseif Nq.id==id_kern then handle_penalty_suppress(0, Np.pre, g)
771 else handle_penalty_always(0, Np.pre, g)
775 -- \jcharwidowpenalty 挿入予定箇所更新
776 if mode and ltjs_get_penalty_table('kcat', Np.char, 0, box_stack_level)%2~=1 then
777 widow_Np.first = Np.first;
778 local Bpr = widow_Bp; widow_Bp = Bp; Bp = Bpr
782 -- jachar .. (anything)
783 local function handle_nq_jachar()
786 g = get_OB_skip() or get_xkanjiskip(Nq) -- O_B->X
788 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
790 g = get_OB_skip(); g = lineend_fix(g) -- O_B
791 if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
792 elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
793 else handle_penalty_always(Nq.post, 0, g)
795 real_insert(Nq.lend, g)
799 -- (anything) .. (和文文字で終わる hlist)
800 local function handle_np_ja_hlist()
802 if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id == id_pbox_w) and Nq.met) then
803 g = get_OB_skip() or get_kanjiskip() -- O_B->K
805 handle_penalty_normal(Nq.post, 0, g); real_insert(Nq.lend, g)
806 elseif Nq.met then -- Nq.id==id_hlist
807 g = get_kanjiskip() -- K
808 handle_penalty_suppress(0, 0, g); real_insert(0, g)
810 g = get_xkanjiskip(Np) -- X
811 handle_penalty_suppress(0, 0, g); real_insert(0, g)
815 -- (和文文字で終わる hlist) .. (anything)
816 local function handle_nq_ja_hlist()
819 g = get_xkanjiskip(Nq) -- X
820 handle_penalty_suppress(0, 0, g); real_insert(0, g)
824 -------------------- 開始・終了時の処理
827 local function handle_list_tail()
830 -- the current list is to be line-breaked:
831 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
833 g = node_new(id_kern); g.subtype = 0; g.kern = Np.lend
834 set_attr(g, attr_icflag, BOXBDD)
835 node_insert_after(head, Np.last, g)
838 -- Insert \jcharwidowpenalty
839 Bp = widow_Bp; Np = widow_Np; Nq.lend = 0
841 handle_penalty_normal(0,
842 ltjs_get_penalty_table('jwp', 0, 0, box_stack_level))
845 -- the current list is the contents of a hbox
846 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
847 local g = new_jfm_glue(Np, Np.class, fast_find_char_class('boxbdd',Np.met))
849 set_attr(g, attr_icflag, BOXBDD)
850 head = node_insert_after(head, Np.last, g)
853 head = node_remove(head, last); node_free(last);-- remove the sentinel
855 node_free(kanji_skip); node_free(xkanji_skip)
859 local function handle_list_head()
860 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
864 g = new_jfm_glue(Np, fast_find_char_class('parbdd',Np.met), Np.class)
866 g = new_jfm_glue(Np, fast_find_char_class('boxbdd',Np.met), Np.class)
869 set_attr(g, attr_icflag, BOXBDD)
870 if g.id==id_glue and #Bp==0 then
871 local h = node_new(id_penalty)
872 h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
874 head = node_insert_before(head, Np.first, g)
881 local function init_var()
883 if uniq_id == 0x7FFFFFF then uniq_id = 0 end
884 lp = head; Bp = {}; widow_Bp = {}; widow_Np = {first = nil}
886 box_stack_level = ltjp.box_stack_level
887 kanji_skip=skip_table_to_spec('kanjiskip')
888 xkanji_skip=skip_table_to_spec('xkanjiskip')
890 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
891 first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil,
892 post=nil, pre=nil, var=nil, xspc_after=nil, xspc_before=nil,
895 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
896 first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil,
897 post=nil, pre=nil, var=nil, xspc_after=nil, xspc_before=nil,
900 -- the current list is to be line-breaked:
901 -- hbox from \parindent is skipped.
902 while lp and ((lp.id==id_whatsit and lp.subtype~=sid_user)
903 or ((lp.id==id_hlist) and (lp.subtype==3))) do
904 if (lp.id==id_hlist) and (lp.subtype==3) then par_indented = true end
908 -- the current list is the contents of a hbox:
909 -- insert a sentinelEG
910 last=node.tail(head); local g = node_new(id_kern)
911 node_insert_after(head, last, g); last = g
915 -- Nq が前側のクラスタとなることによる修正
916 local function adjust_nq()
917 if Nq.id==id_glyph then after_alchar(Nq)
918 elseif Nq.id==id_hlist or Nq.id==id_pbox or Nq.id==id_disc then after_hlist(Nq)
919 elseif Nq.id == id_pbox_w then
920 luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
921 false, Nq, Np, box_stack_level)
924 -------------------- 外部から呼ばれる関数
927 function main(ahead, amode)
928 if not ahead then return ahead end
929 head = ahead; mode = amode; init_var(); calc_np()
931 extract_np(); handle_list_head()
933 if not mode then head = node_remove(head, last); node_free(last) end
938 extract_np(); adjust_nq()
940 if Np.id == id_jglyph then
943 if Np.id==id_hlist then handle_np_ja_hlist()
944 else handle_np_jachar() end
946 if Nq.id==id_hlist then handle_nq_ja_hlist()
947 else handle_nq_jachar() end
952 -- adjust attr_icflag
953 tex.attribute[attr_icflag] = -(0x7FFFFFFF)
959 function create_inhibitglue_node()
960 local tn = node_new(id_whatsit, sid_user)
961 tn.user_id=30111; tn.type=100; tn.value=1
965 -- Node for indicating beginning of a paragraph
967 function create_beginpar_node()
968 local tn = node_new(id_whatsit, sid_user)
969 tn.user_id=30114; tn.type=100; tn.value=1
973 local function whatsit_callback(Np, lp, Nq, bsl)
974 if Np.nuc then return Np
975 elseif lp.user_id == 30114 then
976 Np.first = lp; Np.nuc = lp; Np.last = lp
979 Np.pre = 0; Np.post = 0
980 Np.xspc_before = false
981 Np.xspc_after = false
986 local function whatsit_after_callback(s, Nq, Np, bsl)
987 if not s and Nq.nuc.user_id == 30114 then
988 local x, y = node.prev(Nq.nuc), Nq.nuc
989 Nq.first, Nq.nuc, Nq.last = x, x, x
990 head = node_remove(head, y)
995 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
996 "luatexja.beginpar.np_info", 1)
997 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
998 "luatexja.beginpar.np_info_after", 1)