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 id_glyph = node.id('glyph')
29 local id_hlist = node.id('hlist')
30 local id_vlist = node.id('vlist')
31 local id_rule = node.id('rule')
32 local id_ins = node.id('ins')
33 local id_mark = node.id('mark')
34 local id_adjust = node.id('adjust')
35 local id_disc = node.id('disc')
36 local id_whatsit = node.id('whatsit')
37 local id_math = node.id('math')
38 local id_glue = node.id('glue')
39 local id_kern = node.id('kern')
40 local id_penalty = node.id('penalty')
42 local id_glue_spec = node.id('glue_spec')
43 local id_jglyph = node.id('glyph') + 256 -- Japanese character
44 local id_box_like = node.id('hlist') + 256 -- vbox, shifted hbox
45 local id_pbox = node.id('hlist') + 512 -- already processed nodes (by \unhbox)
46 local id_pbox_w = node.id('hlist') + 513 -- cluster which consists of a whatsit
47 local sid_user = node.subtype('user_defined')
49 local sid_start_link = node.subtype('pdf_start_link')
50 local sid_start_thread = node.subtype('pdf_start_thread')
51 local sid_end_link = node.subtype('pdf_end_link')
52 local sid_end_thread = node.subtype('pdf_end_thread')
62 local IC_PROCESSED = 9
68 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
69 local attr_orig_char = luatexbase.attributes['ltj@origchar']
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
78 local par_indented -- is the paragraph indented?
80 -------------------- Helper functions
82 local function copy_attr(new, old)
86 -- This function is called only for acquiring `special' characters.
87 local function fast_find_char_class(c,m)
88 return m.size_cache.chars[c] or 0
91 local spec_zero_glue = node.new(id_glue_spec)
92 spec_zero_glue.width = 0; spec_zero_glue.stretch_order = 0; spec_zero_glue.stretch = 0
93 spec_zero_glue.shrink_order = 0; spec_zero_glue.shrink = 0
95 local function get_zero_spec()
96 return node.copy(spec_zero_glue)
99 local function skip_table_to_spec(n)
100 local g = node.new(id_glue_spec)
101 local st = ltjs.get_skip_table(n, box_stack_level)
102 g.width = st.width; g.stretch = st.stretch; g.shrink = st.shrink
103 g.stretch_order = st.stretch_order; g.shrink_order = st.shrink_order
108 local function add_penalty(p,e)
109 if p.penalty>=10000 then
110 if e<=-10000 then p.penalty = 0 end
111 elseif p.penalty<=-10000 then
112 if e>=10000 then p.penalty = 0 end
114 p.penalty = p.penalty + e
115 if p.penalty>=10000 then p.penalty = 10000
116 elseif p.penalty<=-10000 then p.penalty = -10000 end
122 diffmet_rule = math.two_average
123 function math.two_add(a,b) return a+b end
124 function math.two_average(a,b) return (a+b)*0.5 end
126 -------------------- idea
127 -- 2 node の間に glue/kern/penalty を挿入する.
128 -- 基本方針: char node q と char node p の間
131 -- first: 最初の node,nuc: p,last: 最後の node
134 -- 実際の glue は Np.last, Nq.first の間に挿入される
135 -- Bp: Np.last, Nq.first の間の penalty node 達の配列
137 -- Np, Nq, Bp, widow_Bp について
139 -- 1回のループごとに Nq = Np, Np = (new table) となるのは効率が悪いので,
140 -- Np <-> Nq 入れ替え,その後 Np をクリアすることでテーブルを再利用.
141 -- 同様の関係は Bp, widow_Bp にも.
145 -- node x が non-char node のときは,x のみ
146 -- x が char_node のときは,
147 -- - x が \accent の第二引数だったとき
148 -- [kern2 kern y kern2] x の 3 node が核に加わる
149 -- - x の直後に \/ 由来 kern があったとき
150 -- その \/ 由来の kern が核に加わる
152 -- ins, mark, adjust, whatsit, penalty
154 -- Nq.last .. + .. Bp.first .... Bp[last] .... * .. Np.first
155 -- +: kern from LINEEND はここに入る
156 -- *: jfm glue はここに入る
158 local head -- the head of current list
159 local last -- the last node of current list
160 local lp -- 外側での list 走査時のカーソル
163 local widow_Bp, widow_Np -- \jcharwidowpenalty 挿入位置管理用
165 local ihb_flag -- JFM グルー挿入抑止用 flag
166 -- on: \inhibitglue 指定時,hlist の周囲
168 -------------------- hlist 内の文字の検索
170 local first_char, last_char, find_first_char
172 local function check_box(box_ptr, box_end)
173 local p = box_ptr; local found_visible_node = false
175 find_first_char = false; first_char = nil; last_char = nil
178 while p and p~=box_end do
182 p = node_next(node_next(node_next(p))); pid = p.id
183 elseif has_attr(p, attr_icflag)==IC_PROCESSED then
184 p = node_next(p); pid = p.id
187 if pid==id_glyph then
189 if find_first_char then
190 first_char = p; find_first_char = false
192 last_char = p; found_visible_node = true; p=node_next(p)
193 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 if find_first_char then
200 first_char = p.head; find_first_char = false
202 last_char = p.head; found_visible_node = true
205 if check_box(p.head, nil) then found_visible_node = true end
206 else if find_first_char then
207 find_first_char = false
213 elseif not (pid==id_ins or pid==id_mark
214 or pid==id_adjust or pid==id_whatsit
215 or pid==id_penalty) then
216 found_visible_node = true
217 if find_first_char then
218 find_first_char = false
225 return found_visible_node
228 function check_box_high(Nx, box_ptr, box_end)
229 first_char = nil; last_char = nil; find_first_char = true
230 if check_box(box_ptr, box_end) then
232 if first_char.font == has_attr(first_char, attr_curjfnt) then
233 set_np_xspc_jachar(Nx, first_char)
235 set_np_xspc_alchar(Nx, first_char.char,first_char, ligature_head)
242 -------------------- Np の計算と情報取得
244 luatexbase.create_callback("luatexja.jfmglue.whatsit_getinfo", "data",
245 function (Np, lp, Nq, box_stack_level)
246 if Np.nuc then return Np
248 return Np -- your code
251 luatexbase.create_callback("luatexja.jfmglue.whatsit_after", "data",
252 function (stat, Nq, Np, box_stack_level) return false end)
255 local function set_attr_icflag_processed(p)
256 if (has_attr(p, attr_icflag) or 0)<= ITALIC then
257 set_attr(p, attr_uniqid, uniq_id)
258 set_attr(p, attr_icflag, PROCESSED)
262 local function check_next_ickern()
263 if lp.id == id_kern and has_attr(lp, attr_icflag)==ITALIC then
264 set_attr(lp, attr_icflag, IC_PROCESSED)
265 set_attr(lp, attr_uniqid, uniq_id)
266 Np.last = lp; lp = node_next(lp)
267 else Np.last = Np.nuc end
270 local function calc_np_pbox()
271 local uid = has_attr(lp, attr_uniqid)
272 Np.first = Np.first or lp; Np.id = id_pbox
273 lpa = KINSOKU -- dummy=
274 while lp~=last and lpa>=PACKED and lpa~=BOXBDD
275 and has_attr(lp, attr_uniqid) == uid do
276 Np.nuc = lp; set_attr(lp, attr_uniqid, uniq_id)
277 lp = node_next(lp); lpa = has_attr(lp, attr_icflag) or 0
279 return check_next_ickern()
283 local calc_np_auxtable = {
284 [id_glyph] = function()
285 Np.first, Np.nuc = (Np.first or lp), lp;
286 Np.id = (lp.font==has_attr(lp, attr_curjfnt)) and id_jglyph or id_glyph
287 set_attr(lp, attr_uniqid, uniq_id)
288 --set_attr_icflag_processed(lp) treated in ltj-setwidth.lua
289 lp = node_next(lp); check_next_ickern(); return true
291 [id_hlist] = function()
292 Np.first = Np.first or lp; Np.last = lp; Np.nuc = lp;
293 set_attr_icflag_processed(lp)
299 lp = node_next(lp); return true
301 [id_vlist] = function()
302 Np.first = Np.first or lp; Np.nuc = lp; Np.last = lp;
303 Np.id = id_box_like; set_attr_icflag_processed(lp);
304 lp = node_next(lp); return true
306 [id_rule] = function()
307 Np.first = Np.first or 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_ins] = function()
312 set_attr_icflag_processed(lp); lp = node_next(lp)
315 [id_mark] = function()
316 set_attr_icflag_processed(lp); lp = node_next(lp)
319 [id_adjust] = function()
320 set_attr_icflag_processed(lp); lp = node_next(lp)
323 [id_disc] = function()
324 Np.first = Np.first or lp;
325 Np.nuc = lp; set_attr_icflag_processed(lp);
326 Np.last = lp; Np.id = id_disc; lp = node_next(lp); return true
328 [id_whatsit] = function()
329 if lp.subtype==sid_user then
330 if lp.user_id==30111 then
331 local lq = node_next(lp)
332 head = node.remove(head, lp); node.free(lp); lp = lq; ihb_flag = true
334 set_attr_icflag_processed(lp)
335 luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo"
336 , Np, lp, Nq, box_stack_level)
339 Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc; return true
343 -- we do special treatment for these whatsit nodes.
344 if lp.subtype == sid_start_link or lp.subtype == sid_start_thread then
346 elseif lp.subtype == sid_end_link or lp.subtype == sid_end_thread then
347 Nq.last = lp; Np.first = nil
349 set_attr_icflag_processed(lp); lp = node_next(lp)
353 [id_math] = function()
354 Np.first = Np.first or lp; Np.nuc = lp;
355 set_attr_icflag_processed(lp); lp = node_next(lp)
356 while lp.id~=id_math do
357 set_attr_icflag_processed(lp); lp = node_next(lp)
359 set_attr_icflag_processed(lp);
360 Np.last = lp; Np.id = id_math; lp = node_next(lp);
363 [id_glue] = function()
364 Np.first = Np.first or lp; Np.nuc = lp; set_attr_icflag_processed(lp);
365 Np.last = lp; Np.id = id_glue; lp = node_next(lp); return true
367 [id_kern] = function()
368 Np.first = Np.first or lp
369 if lp.subtype==2 then
370 set_attr_icflag_processed(lp); lp = node_next(lp)
371 set_attr_icflag_processed(lp); lp = node_next(lp)
372 set_attr_icflag_processed(lp); lp = node_next(lp)
373 set_attr_icflag_processed(lp); Np.nuc = lp
374 if lp.font == has_attr(lp, attr_curjfnt) then
379 lp = node_next(lp); check_next_ickern();
381 Np.id = id_kern; set_attr_icflag_processed(lp);
382 Np.last = lp; lp = node_next(lp)
386 [id_penalty] = function()
387 Bp[#Bp+1] = lp; set_attr_icflag_processed(lp);
388 lp = node_next(lp); return false
391 Np.first = Np.first or lp; Np.nuc = lp; Np.last = lp;
392 Np.id = id_box_like; set_attr_icflag_processed(lp);
393 lp = node_next(lp); return true
397 local function calc_np()
398 local last, k = last, nil
399 -- We assume lp = node_next(Np.last)
400 Np, Nq, ihb_flag = Nq, Np, false
401 for k in pairs(Np) do Np[k] = nil end;
402 for k in pairs(Bp) do Bp[k] = nil end
404 local lpa = has_attr(lp, attr_icflag) or 0
406 if lpa == BOXBDD then
407 local lq = node_next(lp)
408 head = node.remove(head, lp); node.free(lp); lp = lq
409 else calc_np_pbox(); return
411 elseif calc_np_auxtable[lp.id]() then return end
419 -- extract informations from Np
420 -- We think that "Np is a Japanese character" if Np.met~=nil,
421 -- "Np is an alphabetic character" if Np.pre~=nil,
422 -- "Np is not a character" otherwise.
425 function set_np_xspc_jachar(Nx, x)
426 local m = ltjf_font_metric_table[x.font]
427 local c = has_attr(x, attr_orig_char) or 0
428 local cls = ltjf_find_char_class(x.char, m) or 0
429 if cls==0 and c ~= x.char then cls = ltjf_find_char_class(-c, m) end
430 Nx.class = cls; set_attr(x, attr_jchar_class, cls)
431 Nx.lend = m.size_cache.char_type[cls].kern[fast_find_char_class('lineend', m)] or 0
432 Nx.met, Nx.var, Nx.char = m, m.var, c
433 Nx.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
434 Nx.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
435 local y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
436 Nx.xspc_before, Nx.xspc_after = (y%2==1), (y>=2)
437 Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
441 local ligature_head = 1
442 local ligature_tail = 2
443 function set_np_xspc_alchar(Nx, c,x, lig)
445 if lig == ligature_head then
446 while x.components and x.subtype and math.floor(x.subtype*0.5)%2==1 do
447 x = x.components; c = x.char
450 while x.components and x.subtype and math.floor(x.subtype*0.5)%2==1 do
451 x = node.tail(x.components); c = x.char
454 Nx.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
455 Nx.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
458 Nx.pre = 0; Nx.post = 0; Nx.char = -1
461 local y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
462 Nx.xspc_before = (y%2==1)
463 Nx.xspc_after = (y>=2)
464 Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
468 local function extract_np()
469 local x, i = Np.nuc, Np.id;
470 if i == id_jglyph then return set_np_xspc_jachar(Np, x)
471 elseif i == id_glyph then return set_np_xspc_alchar(Np, x.char, x, ligature_head)
472 elseif i == id_hlist then Np.last_char = check_box_high(Np, x.head, nil)
473 elseif i == id_pbox then Np.last_char = check_box_high(Np, Np.first, node_next(Np.last))
474 elseif i == id_disc then Np.last_char = check_box_high(Np, x.replace, nil)
475 elseif i == id_math then return set_np_xspc_alchar(Np, -1, x)
479 -- change the information for the next loop
480 -- (will be done if Nx is an alphabetic character or a hlist)
481 function after_hlist(Nx)
483 if Nx.last_char.font == has_attr(Nx.last_char, attr_curjfnt) then
484 set_np_xspc_jachar(Nx, Nx.last_char);
486 set_np_xspc_alchar(Nx, Nx.last_char.char,Nx.last_char, ligature_tail)
489 Nx.pre = nil; Nx.met = nil
493 local function after_alchar(Nx)
495 return set_np_xspc_alchar(Nx, x.char,x, ligature_tail)
499 -------------------- 最下層の処理
501 local function lineend_fix(g)
502 if g and g.id==id_kern then
504 elseif Nq.lend~=0 then
506 g = node.new(id_kern); copy_attr(g, Nq.nuc);
507 g.subtype = 1; g.kern = -Nq.lend;
508 set_attr(g, attr_icflag, LINEEND)
509 set_attr(g, attr_uniqid, uniq_id)
510 elseif g.id==id_kern then
511 g.kern = g.kern - Nq.lend
513 g.spec.width = g.spec.width - Nq.lend
519 -- change penalties (or create a new penalty, if needed)
520 local function handle_penalty_normal(post, pre, g)
521 local a = (pre or 0) + (post or 0)
523 if (a~=0 and not(g and g.id==id_kern)) or Nq.lend~=0 then
524 local p = node.new(id_penalty); copy_attr(p, Nq.nuc)
525 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
527 head = insert_before(head, Np.first, p)
529 set_attr(p, attr_icflag, KINSOKU)
530 set_attr(p, attr_uniqid, uniq_id)
532 else for i, v in pairs(Bp) do add_penalty(v,a) end
536 local function handle_penalty_always(post, pre, g)
537 local a = (pre or 0) + (post or 0)
539 if not (g and g.id==id_glue) or Nq.lend~=0 then
540 local p = node.new(id_penalty); copy_attr(p, Nq.nuc)
541 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
543 head = insert_before(head, Np.first, p)
545 set_attr(p, attr_icflag, KINSOKU)
546 set_attr(p, attr_uniqid, uniq_id)
548 else for i, v in pairs(Bp) do add_penalty(v,a) end
552 local function handle_penalty_suppress(post, pre, g)
553 local a = (pre or 0) + (post or 0)
555 if g and g.id==id_glue then
556 local p = node.new(id_penalty); copy_attr(p, Nq.nuc)
557 p.penalty = 10000; head = insert_before(head, Np.first, p)
559 set_attr(p, attr_icflag, KINSOKU)
560 set_attr(p, attr_uniqid, uniq_id)
562 else for i, v in pairs(Bp) do add_penalty(v,a) end
566 -- 和文文字間の JFM glue を node 化
567 local function new_jfm_glue(Nn, bc, ac)
568 -- bc, ac: char classes
570 local z = Nn.met.size_cache.char_type[bc]
572 local h = node.new(id_glue_spec)
573 h.width = z.glue[ac][1]
574 h.stretch = z.glue[ac][2]
575 h.shrink = z.glue[ac][3]
576 h.stretch_order=0; h.shrink_order=0
577 g = node.new(id_glue); copy_attr(g, Nn.nuc)
578 g.subtype = 0; g.spec = h
579 set_attr(g, attr_icflag, FROM_JFM); set_attr(g, attr_uniqid, uniq_id)
580 elseif z.kern[ac] then
581 g = node.new(id_kern); copy_attr(g, Nn.nuc)
582 g.subtype = 1; g.kern = z.kern[ac]
583 set_attr(g, attr_icflag, FROM_JFM); set_attr(g, attr_uniqid, uniq_id)
588 -- Nq.last (kern w) .... (glue/kern g) Np.first
589 local function real_insert(w, g)
591 local h = node.new(id_kern); copy_attr(h, Nq.nuc)
592 set_attr(h, attr_icflag, LINE_END)
593 set_attr(h, attr_uniqid, uniq_id)
594 h.kern = w; h.subtype = 1
595 head = node.insert_after(head, Nq.last, h)
598 head = insert_before(head, Np.first, g)
603 -------------------- 和文文字間空白量の決定
606 local function get_kanji_skip_from_jfm(Nn)
607 local i = Nn.met.size_cache.kanjiskip
609 return { i[1], i[2], i[3] }
613 local function get_kanjiskip()
614 local g = node.new(id_glue); copy_attr(g, Nq.nuc)
615 if Np.auto_kspc or Nq.auto_kspc then
616 if kanji_skip.width == max_dimen then
617 local gx = node.new(id_glue_spec);
618 gx.stretch_order = 0; gx.shrink_order = 0
619 local bk = get_kanji_skip_from_jfm(Nq)
621 if (Np.met.size_cache==Nq.met.size_cache) and (Nq.var==Np.var) then
624 ak = get_kanji_skip_from_jfm(Np)
628 gx.width = round(diffmet_rule(bk[1], ak[1]))
629 gx.stretch = round(diffmet_rule(bk[2], ak[2]))
630 gx.shrink = -round(diffmet_rule(-bk[3], -ak[3]))
632 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
635 gx.width = ak[1]; gx.stretch = ak[2]; gx.shrink = ak[3]
636 else node.free(gx); gx = get_zero_spec() -- fallback
639 else g.spec=node.copy(kanji_skip); node.free(gx) end
641 g.spec = get_zero_spec(); node.free(gx)
643 set_attr(g, attr_icflag, KANJI_SKIP)
644 set_attr(g, attr_uniqid, uniq_id)
648 local function calc_ja_ja_aux(gb,ga)
652 if not ga then return gb end
653 local k = node.type(gb.id) .. node.type(ga.id)
654 if k == 'glueglue' then
656 gb.spec.width = round(diffmet_rule(gb.spec.width, ga.spec.width))
657 gb.spec.stretch = round(diffmet_rule(gb.spec.stretch,ga.spec.shrink))
658 gb.spec.shrink = -round(diffmet_rule(-gb.spec.shrink, -ga.spec.shrink))
661 elseif k == 'kernkern' then
663 gb.kern = round(diffmet_rule(gb.kern, ga.kern))
666 elseif k == 'kernglue' then
667 -- gb: kern, ga: glue
668 ga.spec.width = round(diffmet_rule(gb.kern,ga.spec.width))
669 ga.spec.stretch = round(diffmet_rule(ga.spec.stretch, 0))
670 ga.spec.shrink = -round(diffmet_rule(-ga.spec.shrink, 0))
674 -- gb: glue, ga: kern
675 gb.spec.width = round(diffmet_rule(ga.kern, gb.spec.width))
676 gb.spec.stretch = round(diffmet_rule(gb.spec.stretch, 0))
677 gb.spec.shrink = -round(diffmet_rule(-gb.spec.shrink, 0))
684 local function calc_ja_ja_glue()
685 if ihb_flag then return nil
686 elseif (Nq.met.size_cache==Np.met.size_cache) and (Nq.var==Np.var) then
687 return new_jfm_glue(Nq, Nq.class, Np.class)
689 return calc_ja_ja_aux(new_jfm_glue(Nq, Nq.class, fast_find_char_class('diffmet',Nq.met)),
690 new_jfm_glue(Np, fast_find_char_class('diffmet',Np.met), Np.class))
694 -------------------- 和欧文間空白量の決定
697 local function get_xkanji_skip_from_jfm(Nn)
698 local i = Nn.met.size_cache.xkanjiskip
700 return i and { i[1], i[2], i[3] }
704 local function get_xkanjiskip(Nn)
705 local g = node.new(id_glue); copy_attr(g, Nn.nuc)
706 if Nq.xspc_after and Np.xspc_before and (Nq.auto_xspc or Np.auto_xspc) then
707 if xkanji_skip.width == max_dimen then
708 local gx = node.new(id_glue_spec);
709 gx.stretch_order = 0; gx.shrink_order = 0
710 local bk = get_xkanji_skip_from_jfm(Nn)
712 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
713 else node.free(gx); gx = get_zero_spec() -- fallback
716 else g.spec=node.copy(xkanji_skip) end
718 g.spec = get_zero_spec()
720 set_attr(g, attr_icflag, XKANJI_SKIP)
721 set_attr(g, attr_uniqid, uniq_id)
726 -------------------- 隣接した「塊」間の処理
728 local function get_OA_skip()
730 return new_jfm_glue(Np, fast_find_char_class(((Nq.id == id_math and -1) or 'jcharbdd'), Np.met), Np.class)
734 local function get_OB_skip()
736 return new_jfm_glue(Nq, Nq.class, fast_find_char_class(((Np.id == id_math and -1) or'jcharbdd'), Nq.met))
741 -- (anything) .. jachar
742 local function handle_np_jachar()
743 if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id==id_pbox_w) and Nq.met) then
744 local g = lineend_fix(calc_ja_ja_glue() or get_kanjiskip()) -- M->K
745 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
746 elseif Nq.met then -- Nq.id==id_hlist
747 local g = get_OA_skip() or get_kanjiskip() -- O_A->K
748 handle_penalty_normal(0, Np.pre, g); real_insert(0, g)
750 local g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
751 if Nq.id==id_hlist then Nq.post = 0 end
752 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
754 local g = get_OA_skip() -- O_A
755 if Nq.id==id_glue then handle_penalty_normal(0, Np.pre, g)
756 elseif Nq.id==id_kern then handle_penalty_suppress(0, Np.pre, g)
757 else handle_penalty_always(0, Np.pre, g)
761 -- \jcharwidowpenalty 挿入予定箇所更新
762 if mode and ltjs_get_penalty_table('kcat', Np.char, 0, box_stack_level)%2~=1 then
763 widow_Np.first = Np.first;
764 local Bpr = widow_Bp; widow_Bp = Bp; Bp = Bpr
768 -- jachar .. (anything)
769 local function handle_nq_jachar()
771 if Np.id==id_hlist then Np.pre = 0 end
772 local g = lineend_fix(get_OB_skip() or get_xkanjiskip(Nq)) -- O_B->X
773 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
775 local g = lineend_fix(get_OB_skip()) -- O_B
776 if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
777 elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
778 else handle_penalty_always(Nq.post, 0, g)
780 real_insert(Nq.lend, g)
784 -- (anything) .. (和文文字で始まる hlist)
785 local function handle_np_ja_hlist()
786 if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id == id_pbox_w) and Nq.met) then
787 local g = lineend_fix(get_OB_skip() or get_kanjiskip()) -- O_B->K
788 handle_penalty_normal(Nq.post, 0, g); real_insert(Nq.lend, g)
789 elseif Nq.met then -- Nq.id==id_hlist
790 local g = get_kanjiskip() -- K
791 handle_penalty_suppress(0, 0, g); real_insert(0, g)
793 local g = get_xkanjiskip(Np) -- X
794 handle_penalty_suppress(0, 0, g); real_insert(0, g)
798 -- (和文文字で終わる hlist) .. (anything)
799 local function handle_nq_ja_hlist()
801 local g = get_xkanjiskip(Nq) -- X
802 handle_penalty_suppress(0, 0, g); real_insert(0, g)
806 -- Nq が前側のクラスタとなることによる修正
807 local function adjust_nq()
808 if Nq.id==id_glyph then after_alchar(Nq)
809 elseif Nq.id==id_hlist or Nq.id==id_pbox or Nq.id==id_disc then after_hlist(Nq)
810 elseif Nq.id == id_pbox_w then
811 luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
812 false, Nq, Np, box_stack_level)
816 -------------------- 開始・終了時の処理
819 local function handle_list_tail()
822 -- the current list is to be line-breaked:
823 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
825 g = node.new(id_kern); g.subtype = 0; g.kern = Np.lend
826 copy_attr(g, Np.nuc); set_attr(g, attr_icflag, BOXBDD)
827 node.insert_after(head, Np.last, g)
830 -- Insert \jcharwidowpenalty
831 Bp = widow_Bp; Np = widow_Np; Nq.lend = 0
833 handle_penalty_normal(0,
834 ltjs_get_penalty_table('jwp', 0, 0, box_stack_level))
837 -- the current list is the contents of a hbox
838 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
839 local g = new_jfm_glue(Np, Np.class, fast_find_char_class('boxbdd',Np.met))
841 set_attr(g, attr_icflag, BOXBDD)
842 head = node.insert_after(head, Np.last, g)
849 local function handle_list_head()
850 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
854 g = new_jfm_glue(Np, fast_find_char_class('parbdd',Np.met), Np.class)
856 g = new_jfm_glue(Np, fast_find_char_class('boxbdd',Np.met), Np.class)
859 set_attr(g, attr_icflag, BOXBDD)
860 if g.id==id_glue and #Bp==0 then
861 local h = node.new(id_penalty); copy_attr(h, Np.nuc)
862 h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
864 head = insert_before(head, Np.first, g)
871 local function init_var()
873 if uniq_id == 0x7FFFFFF then uniq_id = 0 end
874 lp = head; Bp = {}; widow_Bp = {}; widow_Np = {first = nil}
876 box_stack_level = ltjp.box_stack_level
877 kanji_skip=skip_table_to_spec('kanjiskip')
878 xkanji_skip=skip_table_to_spec('xkanjiskip')
880 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
881 first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil,
882 post=nil, pre=nil, xspc_after=nil, xspc_before=nil,
885 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
886 first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil,
887 post=nil, pre=nil, xspc_after=nil, xspc_before=nil,
890 -- the current list is to be line-breaked:
891 -- hbox from \parindent is skipped.
892 while lp and ((lp.id==id_whatsit and lp.subtype~=sid_user)
893 or ((lp.id==id_hlist) and (lp.subtype==3))) do
894 if (lp.id==id_hlist) and (lp.subtype==3) then par_indented = true end
898 -- the current list is the contents of a hbox:
899 -- insert a sentinelEG
900 last=node.tail(head); local g = node.new(id_kern)
901 node.insert_after(head, last, g); last = g
905 local function cleanup()
906 -- adjust attr_icflag for avoiding error
907 tex.setattribute('global', attr_icflag, 0)
908 node.free(kanji_skip); node.free(xkanji_skip)
910 local h = node_next(head)
911 if h.id == id_penalty and h.penalty == 10000 then
913 if h.id == id_glue and h.subtype == 15 and not h.next then
919 head = node.remove(head, last); node.free(last);-- remove the sentinel
923 -------------------- 外部から呼ばれる関数
926 function main(ahead, amode)
927 if not ahead then return ahead end
928 head = ahead; mode = amode; init_var(); calc_np()
930 extract_np(); handle_list_head()
936 extract_np(); adjust_nq()
938 if Np.id == id_jglyph then
941 if Np.id==id_hlist then handle_np_ja_hlist()
942 else handle_np_jachar() end
944 if Nq.id==id_hlist then handle_nq_ja_hlist()
945 else handle_nq_jachar() end
955 function create_inhibitglue_node()
956 local tn = node.new(id_whatsit, sid_user)
957 tn.user_id=30111; tn.type=100; tn.value=1
961 -- Node for indicating beginning of a paragraph
963 function create_beginpar_node()
964 local tn = node.new(id_whatsit, sid_user)
965 tn.user_id=30114; tn.type=100; tn.value=1
969 local function whatsit_callback(Np, lp, Nq, bsl)
970 if Np and Np.nuc then return Np
971 elseif Np and lp.user_id == 30114 then
972 Np.first = lp; Np.nuc = lp; Np.last = lp
975 Np.pre = 0; Np.post = 0
976 Np.xspc_before = false
977 Np.xspc_after = false
982 local function whatsit_after_callback(s, Nq, Np, bsl)
983 if not s and Nq.nuc.user_id == 30114 then
984 local x, y = node.prev(Nq.nuc), Nq.nuc
985 Nq.first, Nq.nuc, Nq.last = x, x, x
986 head = node.remove(head, y)
991 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
992 "luatexja.beginpar.np_info", 1)
993 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
994 "luatexja.beginpar.np_info_after", 1)