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('stack'); local ltjs = luatexja.stack
14 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
15 luatexja.load_module('pretreat'); local ltjp = luatexja.pretreat
17 local has_attr = node.has_attribute
18 local set_attr = node.set_attribute
19 local insert_before = node.insert_before
20 local node_next = node.next
21 local round = tex.round
22 local ltjs_fast_get_penalty_table = ltjs.fast_get_penalty_table
23 local ltjf_font_metric_table = ltjf.font_metric_table
24 local ltjf_find_char_class = ltjf.find_char_class
25 local node_new = node.new
26 local node_copy = node.copy
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')
59 -- 実装予定: non-packed jchar 3
62 -- FROM_JFM: 4, 5, 6, 7, 8 →優先度高
65 local XKANJI_SKIP = 10
67 local IC_PROCESSED = 12
69 local PROCESSED_BEGIN_FLAG = 32
74 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
75 local attr_icflag = luatexbase.attributes['ltj@icflag']
76 local max_dimen = 1073741823
78 local function get_attr_icflag(p)
79 return (has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
82 -------------------- Helper functions
84 local function copy_attr(new, old)
88 -- This function is called only for acquiring `special' characters.
89 local function fast_find_char_class(c,m)
90 return m.size_cache.chars[c] or 0
94 local function slow_find_char_class(c, m, oc)
96 local cls = ltjf_find_char_class(oc, m)
97 if xc ~= oc and cls==0 then cls, xc = ltjf_find_char_class(-xc, m) end
101 local spec_zero_glue = node_new(id_glue_spec)
102 spec_zero_glue.width = 0; spec_zero_glue.stretch_order = 0; spec_zero_glue.stretch = 0
103 spec_zero_glue.shrink_order = 0; spec_zero_glue.shrink = 0
105 local function skip_table_to_spec(n)
106 local g, st = node_new(id_glue_spec), ltjs.fast_get_skip_table(n)
107 g.width = st.width; g.stretch = st.stretch; g.shrink = st.shrink
108 g.stretch_order = st.stretch_order; g.shrink_order = st.shrink_order
114 local function add_penalty(p,e)
117 if e<=-10000 then pp = 0 end
118 elseif pp<=-10000 then
119 if e>=10000 then pp = 0 end
122 if pp>=10000 then p.penalty = 10000
123 elseif pp<=-10000 then p.penalty = -10000
124 else p.penalty = pp end
130 diffmet_rule = math.two_paverage
131 function math.two_add(a,b) return a+b end
132 function math.two_average(a,b) return (a+b)*0.5 end
133 function math.two_paverage(a,b) return (a+b)*0.5 end
134 function math.two_pleft(a,b) return a end
135 function math.two_pright(a,b) return b end
137 local head -- the head of current list
140 local widow_Bp, widow_Np -- \jcharwidowpenalty 挿入位置管理用
142 local ihb_flag -- JFM グルー挿入抑止用 flag
143 -- on: \inhibitglue 指定時,hlist の周囲
145 -------------------- hlist 内の文字の検索
147 local first_char, last_char, find_first_char
149 local function check_box(box_ptr, box_end)
150 local p = box_ptr; local found_visible_node = false
152 find_first_char = false; first_char = nil; last_char = nil
155 while p and p~=box_end do
157 if pid==id_kern and p.subtype==2 then
158 p = node_next(node_next(node_next(p))); pid = p.id -- p must be glyph_node
160 if pid==id_glyph then
162 if find_first_char then
163 first_char = p; find_first_char = false
165 last_char = p; found_visible_node = true; p=node_next(p)
166 if (not p) or p==box_end then return found_visible_node end
168 pid = p.id -- p must be non-nil
170 if pid==id_kern and get_attr_icflag(p)==IC_PROCESSED then
172 elseif pid==id_hlist then
173 if PACKED == get_attr_icflag(p) then
174 if find_first_char then
175 first_char = p.head; find_first_char = false
177 last_char = p.head; found_visible_node = true
180 if check_box(p.head, nil) then found_visible_node = true end
181 else if find_first_char then
182 find_first_char = false
188 elseif not (pid==id_ins or pid==id_mark
189 or pid==id_adjust or pid==id_whatsit
190 or pid==id_penalty) then
191 found_visible_node = true
192 if find_first_char then
193 find_first_char = false
200 return found_visible_node
203 function check_box_high(Nx, box_ptr, box_end)
204 first_char = nil; last_char = nil; find_first_char = true
205 if check_box(box_ptr, box_end) then
207 if first_char.font == (has_attr(first_char, attr_curjfnt) or -1) then
208 set_np_xspc_jachar(Nx, first_char)
210 set_np_xspc_alchar(Nx, first_char.char,first_char, ligature_head)
217 -------------------- Np の計算と情報取得
219 luatexbase.create_callback("luatexja.jfmglue.whatsit_getinfo", "data",
220 function (Np, lp, Nq)
221 if Np.nuc then return Np
223 return Np -- your code
226 luatexbase.create_callback("luatexja.jfmglue.whatsit_after", "data",
227 function (stat, Nq, Np) return false end)
232 local function set_attr_icflag_processed(p)
233 if get_attr_icflag(p)<= ITALIC then
234 set_attr(p, attr_icflag, PROCESSED)
238 local function check_next_ickern(lp)
239 if lp.id == id_kern and ITALIC == get_attr_icflag(lp) then
240 set_attr(lp, attr_icflag, IC_PROCESSED)
241 Np.last = lp; return node_next(lp)
243 Np.last = Np.nuc; return lp
247 local function calc_np_pbox(lp, last)
248 Np.first = Np.first or lp; Np.id = id_pbox
249 local lpa = KINSOKU -- dummy=
250 set_attr(lp, attr_icflag, get_attr_icflag(lp));
251 while lp~=last and lpa>=PACKED and lpa<BOXBDD do
253 lp = node_next(lp); lpa = has_attr(lp, attr_icflag) or 0
254 -- get_attr_icflag() ではいけない!
256 return check_next_ickern(lp)
260 local calc_np_auxtable = {
261 [id_glyph] = function (lp)
262 Np.first, Np.nuc = (Np.first or lp), lp;
263 Np.id = (lp.font == (has_attr(lp, attr_curjfnt) or -1)) and id_jglyph or id_glyph
264 --set_attr_icflag_processed(lp) treated in ltj-setwidth.lua
265 return true, check_next_ickern(node_next(lp));
267 [id_hlist] = function(lp)
268 Np.first = Np.first or lp; Np.last = lp; Np.nuc = lp;
269 set_attr_icflag_processed(lp)
270 Np.id = (lp.shift~=0) and id_box_like or id_hlist
271 return true, node_next(lp)
273 box_like = function(lp)
274 Np.first = Np.first or lp; Np.nuc = lp; Np.last = lp;
275 Np.id = id_box_like; set_attr_icflag_processed(lp);
276 return true, node_next(lp);
279 set_attr_icflag_processed(lp); return false, node_next(lp)
281 [id_whatsit] = function(lp)
282 if lp.subtype==sid_user then
283 if lp.user_id==30111 then
284 local lq = node_next(lp);
285 head = node.remove(head, lp); node.free(lp); ihb_flag = true
288 set_attr_icflag_processed(lp)
289 luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo",
292 Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc;
293 return true, node_next(lp)
295 return false, node_next(lp)
299 -- we do special treatment for these whatsit nodes.
300 if lp.subtype == sid_start_link or lp.subtype == sid_start_thread then
302 elseif lp.subtype == sid_end_link or lp.subtype == sid_end_thread then
303 Np.first, Nq.last = nil, lp;
305 set_attr_icflag_processed(lp); return false, node_next(lp)
308 [id_math] = function(lp)
309 Np.first, Np.nuc = (Np.first or lp), lp;
310 set_attr_icflag_processed(lp); lp = node_next(lp)
311 while lp.id~=id_math do
312 set_attr_icflag_processed(lp); lp = node_next(lp)
314 set_attr_icflag_processed(lp);
315 Np.last, Np.id = lp, id_math;
316 return true, node_next(lp);
318 discglue = function(lp)
319 Np.first, Np.nuc, Np.last = (Np.first or lp), lp, lp;
320 Np.id = lp.id; set_attr_icflag_processed(lp); return true, node_next(lp)
322 [id_kern] = function(lp)
323 Np.first = Np.first or lp
324 if lp.subtype==2 then
325 set_attr_icflag_processed(lp); lp = node_next(lp)
326 set_attr_icflag_processed(lp); lp = node_next(lp)
327 set_attr_icflag_processed(lp); lp = node_next(lp)
328 set_attr_icflag_processed(lp); Np.nuc = lp
329 Np.id = (lp.font == (has_attr(lp, attr_curjfnt) or -1)) and id_jglyph or id_glyph
330 return true, check_next_ickern(node_next(lp));
332 Np.id = id_kern; set_attr_icflag_processed(lp);
333 Np.last = lp; return true, node_next(lp)
336 [id_penalty] = function(lp)
337 Bp[#Bp+1] = lp; set_attr_icflag_processed(lp);
338 return false, node_next(lp)
341 calc_np_auxtable[id_vlist] = calc_np_auxtable.box_like
342 calc_np_auxtable[id_rule] = calc_np_auxtable.box_like
343 calc_np_auxtable[13] = calc_np_auxtable.box_like
344 calc_np_auxtable[id_ins] = calc_np_auxtable.skip
345 calc_np_auxtable[id_mark] = calc_np_auxtable.skip
346 calc_np_auxtable[id_adjust] = calc_np_auxtable.skip
347 calc_np_auxtable[id_disc] = calc_np_auxtable.discglue
348 calc_np_auxtable[id_glue] = calc_np_auxtable.discglue
351 function calc_np(lp, last)
353 -- We assume lp = node_next(Np.last)
354 Np, Nq, ihb_flag = Nq, Np, false
355 -- We clear `predefined' entries of Np before pairs() loop,
356 -- because using only pairs() loop is slower.
357 Np.post, Np.pre, Np.xspc = nil, nil, nil
358 Np.first, Np.id, Np.last, Np.met = nil, nil, nil
359 Np.auto_kspc, Np.auto_xspc, Np.char, Np.class, Np.nuc = nil, nil, nil, nil, nil
360 for k in pairs(Np) do Np[k] = nil end
362 for k = 1,#Bp do Bp[k] = nil end
364 local lpa = has_attr(lp, attr_icflag) or 0
367 if lpa == BOXBDD then
368 local lq = node_next(lp)
369 head = node.remove(head, lp); node.free(lp); lp = lq
370 else return calc_np_pbox(lp, last)
373 k, lp = calc_np_auxtable[lp.id](lp)
374 if k then return lp end
381 local calc_np = calc_np
383 -- extract informations from Np
384 -- We think that "Np is a Japanese character" if Np.met~=nil,
385 -- "Np is an alphabetic character" if Np.pre~=nil,
386 -- "Np is not a character" otherwise.
390 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
391 local attr_orig_char = luatexbase.attributes['ltj@origchar']
392 local attr_autospc = luatexbase.attributes['ltj@autospc']
393 local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
394 function set_np_xspc_jachar(Nx, x)
395 local m = ltjf_font_metric_table[x.font]
396 local cls, c = slow_find_char_class(has_attr(x, attr_orig_char), m, x.char)
397 Nx.class = cls; set_attr(x, attr_jchar_class, cls)
398 Nx.met, Nx.char = m, c
399 Nx.pre = ltjs_fast_get_penalty_table('pre', c) or 0
400 Nx.post = ltjs_fast_get_penalty_table('post', c) or 0
401 Nx.xspc = ltjs_fast_get_penalty_table('xsp', c) or 3
402 Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
404 local set_np_xspc_jachar = set_np_xspc_jachar
407 local floor = math.floor
408 function set_np_xspc_alchar(Nx, c,x, lig)
410 local xc, xs = x.components, x.subtype
411 if lig == ligature_head then
412 while xc and xs and xs%4>=2 do
413 x = xc; xc, xs = x.components, x.subtype
417 while xc and xs and xs%4>=2 do
418 x = node.tail(xc); xc, xs = x.components, x.subtype
422 Nx.pre = ltjs_fast_get_penalty_table('pre', c) or 0
423 Nx.post = ltjs_fast_get_penalty_table('post', c) or 0
426 Nx.pre, Nx.post, Nx.char = 0, 0, -1
429 Nx.xspc = ltjs_fast_get_penalty_table('xsp', c) or 3
430 Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
432 local set_np_xspc_alchar = set_np_xspc_alchar
435 function extract_np()
436 local x, i = Np.nuc, Np.id;
437 if i == id_jglyph then return set_np_xspc_jachar(Np, x)
438 elseif i == id_glyph then return set_np_xspc_alchar(Np, x.char, x, ligature_head)
439 elseif i == id_hlist then Np.last_char = check_box_high(Np, x.head, nil)
440 elseif i == id_pbox then Np.last_char = check_box_high(Np, Np.first, node_next(Np.last))
441 elseif i == id_disc then Np.last_char = check_box_high(Np, x.replace, nil)
442 elseif i == id_math then return set_np_xspc_alchar(Np, -1, x)
446 -- change the information for the next loop
447 -- (will be done if Nx is an alphabetic character or a hlist)
448 function after_hlist(Nx)
449 local s = Nx.last_char
451 if s.font == (has_attr(s, attr_curjfnt) or -1) then
452 set_np_xspc_jachar(Nx, s)
454 set_np_xspc_alchar(Nx, s.char, s, ligature_tail)
457 Nx.pre, Nx.met = nil, nil
461 function after_alchar(Nx)
463 return set_np_xspc_alchar(Nx, x.char,x, ligature_tail)
467 local after_hlist, after_alchar, extract_np = after_hlist, after_alchar, extract_np
469 -------------------- 最下層の処理
471 -- change penalties (or create a new penalty, if needed)
472 local function handle_penalty_normal(post, pre, g)
473 local a = (pre or 0) + (post or 0)
475 if (a~=0 and not(g and g.id==id_kern)) then
476 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
477 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
479 head = insert_before(head, Np.first, p)
481 set_attr(p, attr_icflag, KINSOKU)
483 else for _, v in pairs(Bp) do add_penalty(v,a) end
487 local function handle_penalty_always(post, pre, g)
488 local a = (pre or 0) + (post or 0)
490 if not (g and g.id==id_glue) then
491 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
492 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
494 head = insert_before(head, Np.first, p)
496 set_attr(p, attr_icflag, KINSOKU)
498 else for _, v in pairs(Bp) do add_penalty(v,a) end
502 local function handle_penalty_suppress(post, pre, g)
503 local a = (pre or 0) + (post or 0)
505 if g and g.id==id_glue then
506 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
507 p.penalty = 10000; head = insert_before(head, Np.first, p)
509 set_attr(p, attr_icflag, KINSOKU)
511 else for _, v in pairs(Bp) do add_penalty(v,a) end
515 -- 和文文字間の JFM glue を node 化
516 local function new_jfm_glue(m, bc, ac)
517 -- bc, ac: char classes
518 local z = m.size_cache.char_type[bc]
519 local g, d = z.glue[ac], 0
521 g,d = node_copy(g[1]), g[2];
522 g.spec = node.copy(g.spec); -- node_copy は spec を copy しない(参照を増やすのみ)
526 g = node_new(id_kern); --copy_attr(g, Nn.nuc)
527 g.subtype = 1; g.kern, d = k[1], k[2]
528 set_attr(g, attr_icflag, FROM_JFM);
534 -- Nq.last (kern w) .... (glue/kern g) Np.first
535 local function real_insert(w, g)
537 head = insert_before(head, Np.first, g)
543 -------------------- 和文文字間空白量の決定
548 local function get_kanjiskip_normal()
549 if Np.auto_kspc or Nq.auto_kspc then
550 return node_copy(kanji_skip)
552 local g = node_new(id_glue); --copy_attr(g, Nq.nuc)
553 g.spec = node_copy(spec_zero_glue)
554 set_attr(g, attr_icflag, KANJI_SKIP)
558 local function get_kanjiskip_jfm()
559 local g = node_new(id_glue); --copy_attr(g, Nq.nuc)
560 if Np.auto_kspc or Nq.auto_kspc then
561 local gx = node_new(id_glue_spec);
562 gx.stretch_order, gx.shrink_order = 0, 0
563 local pm, qm = Np.met, Nq.met
564 local bk = qm.size_cache.kanjiskip or {0, 0, 0}
565 if (pm.size_cache==qm.size_cache) and (qm.var==pm.var) then
566 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
568 local ak = pm.size_cache.kanjiskip or {0, 0, 0}
569 gx.width = round(diffmet_rule(bk[1], ak[1]))
570 gx.stretch = round(diffmet_rule(bk[2], ak[2]))
571 gx.shrink = -round(diffmet_rule(-bk[3], -ak[3]))
575 g.spec = node_copy(spec_zero_glue)
577 set_attr(g, attr_icflag, KANJI_SKIP)
581 local function calc_ja_ja_aux(gb,ga, db, da)
582 local rbb, rab = (1-db)/2, (1-da)/2 -- 「前の文字」由来のグルーの割合
583 local rba, raa = (1+db)/2, (1+da)/2 -- 「前の文字」由来のグルーの割合
584 if diffmet_rule ~= math.two_pleft and diffmet_rule ~= math.two_pright
585 and diffmet_rule ~= math.two_paverage then
586 rbb, rab, rba, raa = 1,0,0,1
589 if ga then gb = node_new(id_kern); gb.kern = 0 else return nil end
591 ga = node_new(id_kern); ga.kern = 0
594 local k = node.type(gb.id) .. node.type(ga.id)
595 if k == 'glueglue' then
597 gb.spec.width = round(diffmet_rule(
598 rbb*gb.spec.width + rba*ga.spec.width,
599 rab*gb.spec.width + raa*ga.spec.width ))
600 gb.spec.stretch = round(diffmet_rule(
601 rbb*gb.spec.stretch + rba*ga.spec.stretch,
602 rab*gb.spec.stretch + raa*ga.spec.stretch ))
603 gb.spec.shrink = -round(diffmet_rule(
604 -rbb*gb.spec.shrink - rba*ga.spec.shrink,
605 -rab*gb.spec.shrink - raa*ga.spec.shrink ))
608 elseif k == 'kernkern' then
610 gb.kern = round(diffmet_rule(
611 rbb*gb.kern + rba*ga.kern,
612 rab*gb.kern + raa*ga.kern ))
615 elseif k == 'kernglue' then
616 -- gb: kern, ga: glue
617 ga.spec.width = round(diffmet_rule(
618 rbb*gb.kern + rba*ga.spec.width,
619 rab*gb.kern + raa*ga.spec.width ))
620 ga.spec.stretch = round(diffmet_rule(
621 rba*ga.spec.stretch, raa*ga.spec.stretch ))
622 ga.spec.shrink = -round(diffmet_rule(
623 -rba*ga.spec.shrink,-raa*ga.spec.shrink ))
627 -- gb: glue, ga: kern
628 gb.spec.width = round(diffmet_rule(
629 rba*ga.kern + rbb*gb.spec.width,
630 raa*ga.kern + rab*gb.spec.width ))
631 gb.spec.stretch = round(diffmet_rule(
632 rbb*gb.spec.stretch, rab*gb.spec.stretch ))
633 gb.spec.shrink = -round(diffmet_rule(
634 -rbb*gb.spec.shrink,-rab*gb.spec.shrink ))
640 local function calc_ja_ja_glue()
641 if ihb_flag then return nil
643 local qm, pm = Nq.met, Np.met
644 if (qm.size_cache==pm.size_cache) and (qm.var==pm.var) then
645 return new_jfm_glue(qm, Nq.class, Np.class)
647 local npn, nqn = Np.nuc, Nq.nuc
648 local gb, db = new_jfm_glue(qm, Nq.class,
649 slow_find_char_class(has_attr(npn, attr_orig_char), qm, npn.char))
650 local ga, da = new_jfm_glue(pm,
651 slow_find_char_class(has_attr(nqn, attr_orig_char), pm, nqn.char),
653 return calc_ja_ja_aux(gb, ga, db, da);
658 -------------------- 和欧文間空白量の決定
662 local function get_xkanjiskip_normal(Nn)
663 if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
664 return node_copy(xkanji_skip)
666 local g = node_new(id_glue); --copy_attr(g, Nn.nuc)
667 g.spec = node_copy(spec_zero_glue)
668 set_attr(g, attr_icflag, XKANJI_SKIP)
672 local function get_xkanjiskip_jfm(Nn)
673 local g = node_new(id_glue); --copy_attr(g, Nn.nuc)
674 if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
675 local gx = node_new(id_glue_spec);
676 gx.stretch_order, gx.shrink_order = 0, 0
677 local bk = Nn.met.size_cache.xkanjiskip or {0, 0, 0}
678 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
681 g.spec = node_copy(spec_zero_glue)
683 set_attr(g, attr_icflag, XKANJI_SKIP)
689 -------------------- 隣接した「塊」間の処理
691 local function get_OA_skip()
694 return new_jfm_glue(pm,
695 fast_find_char_class(((Nq.id == id_math and -1) or 'jcharbdd'), pm), Np.class)
699 local function get_OB_skip()
702 return new_jfm_glue(qm, Nq.class,
703 fast_find_char_class(((Np.id == id_math and -1) or'jcharbdd'), qm))
708 -- (anything) .. jachar
709 local function handle_np_jachar(mode)
711 if qid==id_jglyph or ((qid==id_pbox or qid==id_pbox_w) and Nq.met) then
712 local g = calc_ja_ja_glue() or get_kanjiskip() -- M->K
713 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
714 elseif Nq.met then -- qid==id_hlist
715 local g = get_OA_skip() or get_kanjiskip() -- O_A->K
716 handle_penalty_normal(0, Np.pre, g); real_insert(0, g)
718 local g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
719 if qid==id_hlist then Nq.post = 0 end
720 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
722 local g = get_OA_skip() -- O_A
723 if qid==id_glue then handle_penalty_normal(0, Np.pre, g)
724 elseif qid==id_kern then handle_penalty_suppress(0, Np.pre, g)
725 else handle_penalty_always(0, Np.pre, g)
729 if mode and (ltjs_fast_get_penalty_table('kcat', Np.char) or 0)%2~=1 then
730 widow_Np.first, widow_Bp, Bp = Np.first, Bp, widow_Bp
735 -- jachar .. (anything)
736 local function handle_nq_jachar()
738 if Np.id==id_hlist then Np.pre = 0 end
739 local g = get_OB_skip() or get_xkanjiskip(Nq) -- O_B->X
740 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
742 local g = get_OB_skip() -- O_B
743 if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
744 elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
745 else handle_penalty_always(Nq.post, 0, g)
751 -- (anything) .. (和文文字で始まる hlist)
752 local function handle_np_ja_hlist()
754 if qid==id_jglyph or ((qid==id_pbox or Nq.id == id_pbox_w) and Nq.met) then
755 local g = get_OB_skip() or get_kanjiskip() -- O_B->K
756 handle_penalty_normal(Nq.post, 0, g); real_insert(0, g)
757 elseif Nq.met then -- Nq.id==id_hlist
758 local g = get_kanjiskip() -- K
759 handle_penalty_suppress(0, 0, g); real_insert(0, g)
761 local g = get_xkanjiskip(Np) -- X
762 handle_penalty_suppress(0, 0, g); real_insert(0, g)
766 -- (和文文字で終わる hlist) .. (anything)
767 local function handle_nq_ja_hlist()
769 local g = get_xkanjiskip(Nq) -- X
770 handle_penalty_suppress(0, 0, g); real_insert(0, g)
774 -- Nq が前側のクラスタとなることによる修正
775 local function adjust_nq()
776 if Nq.id==id_glyph then after_alchar(Nq)
777 elseif Nq.id==id_hlist or Nq.id==id_pbox or Nq.id==id_disc then after_hlist(Nq)
778 elseif Nq.id == id_pbox_w then
779 luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
784 -------------------- 開始・終了時の処理
787 local function handle_list_tail(mode)
790 -- the current list is to be line-breaked.
791 -- Insert \jcharwidowpenalty
792 Bp = widow_Bp; Np = widow_Np
794 handle_penalty_normal(0,
795 ltjs_fast_get_penalty_table('jwp', 0) or 0)
798 -- the current list is the contents of a hbox
799 local npi, pm = Np.id, Np.met
800 if npi == id_jglyph or (npi==id_pbox and pm) then
801 local g = new_jfm_glue(pm, Np.class, fast_find_char_class('boxbdd', pm))
803 set_attr(g, attr_icflag, BOXBDD)
804 head = node.insert_after(head, Np.last, g)
811 local function handle_list_head(par_indented)
812 local npi, pm = Np.id, Np.met
813 if npi == id_jglyph or (npi==id_pbox and pm) then
815 local g = new_jfm_glue(pm, fast_find_char_class(par_indented, pm), Np.class)
817 set_attr(g, attr_icflag, BOXBDD)
818 if g.id==id_glue and #Bp==0 then
819 local h = node_new(id_penalty); --copy_attr(h, Np.nuc)
820 h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
822 head = insert_before(head, Np.first, g)
829 -- return value: (the initial cursor lp), (last node)
830 local function init_var(mode)
831 Bp, widow_Bp, widow_Np = {}, {}, {first = nil}
833 kanji_skip = node_new(id_glue)
834 kanji_skip.spec = skip_table_to_spec('kanjiskip')
835 set_attr(kanji_skip, attr_icflag, KANJI_SKIP)
836 get_kanjiskip = (kanji_skip.spec.width == max_dimen)
837 and get_kanjiskip_jfm or get_kanjiskip_normal
839 xkanji_skip = node_new(id_glue)
840 xkanji_skip.spec = skip_table_to_spec('xkanjiskip')
841 set_attr(xkanji_skip, attr_icflag, XKANJI_SKIP)
842 get_xkanjiskip = (xkanji_skip.spec.width == max_dimen)
843 and get_xkanjiskip_jfm or get_xkanjiskip_normal
846 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
847 first=nil, id=nil, last=nil, met=nil, nuc=nil,
848 post=nil, pre=nil, xspc=nil,
851 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
852 first=nil, id=nil, last=nil, met=nil, nuc=nil,
853 post=nil, pre=nil, xspc=nil,
856 -- the current list is to be line-breaked:
857 -- hbox from \parindent is skipped.
858 local lp, par_indented, lpi, lps = head, 'boxbdd', head.id, head.subtype
859 while lp and ((lpi==id_whatsit and lps~=sid_user)
860 or ((lpi==id_hlist) and (lps==3))) do
861 if (lpi==id_hlist) and (lps==3) then par_indented = 'parbdd' end
862 lp=node_next(lp); lpi, lps = lp.id, lp.subtype end
863 return lp, node.tail(head), par_indented
865 -- the current list is the contents of a hbox:
867 local g = node_new(id_kern)
868 node.insert_after(head, node.tail(head), g); last = g
869 return head, g, 'boxbdd'
873 local function cleanup(mode, last)
874 -- adjust attr_icflag for avoiding error
875 tex.setattribute('global', attr_icflag, 0)
876 node.free(kanji_skip); node.free(xkanji_skip)
878 local h = node_next(head)
879 if h.id == id_penalty and h.penalty == 10000 then
881 if h.id == id_glue and h.subtype == 15 and not h.next then
887 head = node.remove(head, last); node.free(last);-- remove the sentinel
888 set_attr(head, attr_icflag,
889 get_attr_icflag(head) + PROCESSED_BEGIN_FLAG);
893 -------------------- 外部から呼ばれる関数
896 function main(ahead, mode)
897 if not ahead then return ahead end
899 local lp, last, par_indented = init_var(mode);
900 lp = calc_np(lp, last)
902 extract_np(); handle_list_head(par_indented)
904 return cleanup(mode, last)
906 lp = calc_np(lp, last)
908 extract_np(); adjust_nq()
909 local pid, pm = Np.id, Np.met
911 if pid == id_jglyph then
912 handle_np_jachar(mode)
914 if pid==id_hlist then handle_np_ja_hlist()
915 else handle_np_jachar() end
917 if Nq.id==id_hlist then handle_nq_ja_hlist()
918 else handle_nq_jachar() end
920 lp = calc_np(lp, last)
922 handle_list_tail(mode)
923 return cleanup(mode, last)
928 function create_inhibitglue_node()
929 local tn = node_new(id_whatsit, sid_user)
930 tn.user_id=30111; tn.type=100; tn.value=1
934 -- Node for indicating beginning of a paragraph
936 function create_beginpar_node()
937 local tn = node_new(id_whatsit, sid_user)
938 tn.user_id=30114; tn.type=100; tn.value=1
944 local function whatsit_callback(Np, lp, Nq)
945 if Np and Np.nuc then return Np
946 elseif Np and lp.user_id == 30114 then
947 Np.first = lp; Np.nuc = lp; Np.last = lp
950 Np.pre = 0; Np.post = 0
958 local function whatsit_after_callback(s, Nq, Np)
959 if not s and Nq.nuc.user_id == 30114 then
960 local x, y = node.prev(Nq.nuc), Nq.nuc
961 Nq.first, Nq.nuc, Nq.last = x, x, x
962 head = node.remove(head, y)
967 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
968 "luatexja.beginpar.np_info", 1)
969 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
970 "luatexja.beginpar.np_info_after", 1)