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')
65 local IC_PROCESSED = 9
67 local PROCESSED_BEGIN_FLAG = 16
72 local attr_orig_char = luatexbase.attributes['ltj@origchar']
73 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
74 local attr_icflag = luatexbase.attributes['ltj@icflag']
75 local attr_autospc = luatexbase.attributes['ltj@autospc']
76 local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
77 local max_dimen = 1073741823
79 local function get_attr_icflag(p)
80 return (has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
83 -------------------- Helper functions
85 local function copy_attr(new, old)
89 -- This function is called only for acquiring `special' characters.
90 local function fast_find_char_class(c,m)
91 return m.size_cache.chars[c] or 0
94 local spec_zero_glue = node_new(id_glue_spec)
95 spec_zero_glue.width = 0; spec_zero_glue.stretch_order = 0; spec_zero_glue.stretch = 0
96 spec_zero_glue.shrink_order = 0; spec_zero_glue.shrink = 0
98 local function get_zero_spec()
99 return node_copy(spec_zero_glue)
102 local function skip_table_to_spec(n)
103 local g, st = node_new(id_glue_spec), ltjs.fast_get_skip_table(n)
104 g.width = st.width; g.stretch = st.stretch; g.shrink = st.shrink
105 g.stretch_order = st.stretch_order; g.shrink_order = st.shrink_order
111 local function add_penalty(p,e)
112 if p.penalty>=10000 then
113 if e<=-10000 then p.penalty = 0 end
114 elseif p.penalty<=-10000 then
115 if e>=10000 then p.penalty = 0 end
117 p.penalty = p.penalty + e
118 if p.penalty>=10000 then p.penalty = 10000
119 elseif p.penalty<=-10000 then p.penalty = -10000 end
125 diffmet_rule = math.two_average
126 function math.two_add(a,b) return a+b end
127 function math.two_average(a,b) return (a+b)*0.5 end
129 -------------------- idea
130 -- 2 node の間に glue/kern/penalty を挿入する.
131 -- 基本方針: char node q と char node p の間
134 -- first: 最初の node,nuc: p,last: 最後の node
137 -- 実際の glue は Np.last, Nq.first の間に挿入される
138 -- Bp: Np.last, Nq.first の間の penalty node 達の配列
140 -- Np, Nq, Bp, widow_Bp について
142 -- 1回のループごとに Nq = Np, Np = (new table) となるのは効率が悪いので,
143 -- Np <-> Nq 入れ替え,その後 Np をクリアすることでテーブルを再利用.
144 -- 同様の関係は Bp, widow_Bp にも.
148 -- node x が non-char node のときは,x のみ
149 -- x が char_node のときは,
150 -- - x が \accent の第二引数だったとき
151 -- [kern2 kern y kern2] x の 3 node が核に加わる
152 -- - x の直後に \/ 由来 kern があったとき
153 -- その \/ 由来の kern が核に加わる
155 -- ins, mark, adjust, whatsit, penalty
157 -- Nq.last .. + .. Bp.first .... Bp[last] .... * .. Np.first
158 -- +: kern from LINEEND はここに入る
159 -- *: jfm glue はここに入る
161 local head -- the head of current 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
181 if pid==id_kern and p.subtype==2 then
182 p = node_next(node_next(node_next(p))); pid = p.id -- p must be glyph_node
184 if pid==id_glyph then
186 if find_first_char then
187 first_char = p; find_first_char = false
189 last_char = p; found_visible_node = true; p=node_next(p)
190 if (not p) or p==box_end then return found_visible_node end
192 pid = p.id -- p must be non-nil
194 if pid==id_kern and get_attr_icflag(p)==IC_PROCESSED then
196 elseif pid==id_hlist then
197 if PACKED == get_attr_icflag(p) 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) or -1) 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)
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) return false end)
256 local function set_attr_icflag_processed(p)
257 if get_attr_icflag(p)<= ITALIC then
258 set_attr(p, attr_icflag, PROCESSED)
262 local function check_next_ickern(lp)
263 if lp.id == id_kern and ITALIC == get_attr_icflag(lp) then
264 set_attr(lp, attr_icflag, IC_PROCESSED)
265 Np.last = lp; return node_next(lp)
267 Np.last = Np.nuc; return lp
271 local function calc_np_pbox(lp, last)
272 Np.first = Np.first or lp; Np.id = id_pbox
273 local lpa = KINSOKU -- dummy=
274 set_attr(lp, attr_icflag, get_attr_icflag(lp));
275 while lp~=last and lpa>=PACKED and lpa<BOXBDD do
277 lp = node_next(lp); lpa = has_attr(lp, attr_icflag) or 0
278 -- get_attr_icflag() ではいけない!
280 return check_next_ickern(lp)
284 local calc_np_auxtable = {
285 [id_glyph] = function (lp)
286 Np.first, Np.nuc = (Np.first or lp), lp;
287 Np.id = (lp.font == (has_attr(lp, attr_curjfnt) or -1)) and id_jglyph or id_glyph
288 --set_attr_icflag_processed(lp) treated in ltj-setwidth.lua
289 return true, check_next_ickern(node_next(lp));
291 [id_hlist] = function(lp)
292 Np.first = Np.first or lp; Np.last = lp; Np.nuc = lp;
293 set_attr_icflag_processed(lp)
294 Np.id = (lp.shift~=0) and id_box_like or id_hlist
295 return true, node_next(lp)
297 box_like = function(lp)
298 Np.first = Np.first or lp; Np.nuc = lp; Np.last = lp;
299 Np.id = id_box_like; set_attr_icflag_processed(lp);
300 return true, node_next(lp);
303 set_attr_icflag_processed(lp); return false, node_next(lp)
305 [id_whatsit] = function(lp)
306 if lp.subtype==sid_user then
307 if lp.user_id==30111 then
308 local lq = node_next(lp);
309 head = node.remove(head, lp); node.free(lp); ihb_flag = true
312 set_attr_icflag_processed(lp)
313 luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo",
316 Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc;
317 return true, node_next(lp)
319 return false, node_next(lp)
323 -- we do special treatment for these whatsit nodes.
324 if lp.subtype == sid_start_link or lp.subtype == sid_start_thread then
326 elseif lp.subtype == sid_end_link or lp.subtype == sid_end_thread then
327 Np.first, Nq.last = nil, lp;
329 set_attr_icflag_processed(lp); return false, node_next(lp)
332 [id_math] = function(lp)
333 Np.first, Np.nuc = (Np.first or lp), lp;
334 set_attr_icflag_processed(lp); lp = node_next(lp)
335 while lp.id~=id_math do
336 set_attr_icflag_processed(lp); lp = node_next(lp)
338 set_attr_icflag_processed(lp);
339 Np.last, Np.id = lp, id_math;
340 return true, node_next(lp);
342 discglue = function(lp)
343 Np.first, Np.nuc, Np.last = (Np.first or lp), lp, lp;
344 Np.id = lp.id; set_attr_icflag_processed(lp); return true, node_next(lp)
346 [id_kern] = function(lp)
347 Np.first = Np.first or lp
348 if lp.subtype==2 then
349 set_attr_icflag_processed(lp); lp = node_next(lp)
350 set_attr_icflag_processed(lp); lp = node_next(lp)
351 set_attr_icflag_processed(lp); lp = node_next(lp)
352 set_attr_icflag_processed(lp); Np.nuc = lp
353 Np.id = (lp.font == (has_attr(lp, attr_curjfnt) or -1)) and id_jglyph or id_glyph
354 return true, check_next_ickern(node_next(lp));
356 Np.id = id_kern; set_attr_icflag_processed(lp);
357 Np.last = lp; return true, node_next(lp)
360 [id_penalty] = function(lp)
361 Bp[#Bp+1] = lp; set_attr_icflag_processed(lp);
362 return false, node_next(lp)
365 calc_np_auxtable[id_vlist] = calc_np_auxtable.box_like
366 calc_np_auxtable[id_rule] = calc_np_auxtable.box_like
367 calc_np_auxtable[13] = calc_np_auxtable.box_like
368 calc_np_auxtable[id_ins] = calc_np_auxtable.skip
369 calc_np_auxtable[id_mark] = calc_np_auxtable.skip
370 calc_np_auxtable[id_adjust] = calc_np_auxtable.skip
371 calc_np_auxtable[id_disc] = calc_np_auxtable.discglue
372 calc_np_auxtable[id_glue] = calc_np_auxtable.discglue
375 function calc_np(lp, last)
377 -- We assume lp = node_next(Np.last)
378 Np, Nq, ihb_flag = Nq, Np, false
379 -- We clear `predefined' entries of Np before pairs() loop,
380 -- because using only pairs() loop is slower.
381 Np.post, Np.pre, Np.xspc = nil, nil, nil
382 Np.first, Np.id, Np.last, Np.lend, Np.met = nil, nil, nil, nil
383 Np.auto_kspc, Np.auto_xspc, Np.char, Np.class, Np.nuc = nil, nil, nil, nil, nil
384 for k in pairs(Np) do Np[k] = nil end
386 for k = 1,#Bp do Bp[k] = nil end
388 local lpa = has_attr(lp, attr_icflag) or 0
391 if lpa == BOXBDD then
392 local lq = node_next(lp)
393 head = node.remove(head, lp); node.free(lp); lp = lq
394 else return calc_np_pbox(lp, last)
397 k, lp = calc_np_auxtable[lp.id](lp)
398 if k then return lp end
405 local calc_np = calc_np
407 -- extract informations from Np
408 -- We think that "Np is a Japanese character" if Np.met~=nil,
409 -- "Np is an alphabetic character" if Np.pre~=nil,
410 -- "Np is not a character" otherwise.
414 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
415 function set_np_xspc_jachar(Nx, x)
416 local m = ltjf_font_metric_table[x.font]
417 local c = has_attr(x, attr_orig_char) or x.char
418 local cls = ltjf_find_char_class(x.char, m)
419 if c ~= x.char and cls==0 then cls = ltjf_find_char_class(-c, m) end
420 Nx.class = cls; set_attr(x, attr_jchar_class, cls)
421 Nx.lend = m.size_cache.char_type[cls].kern[fast_find_char_class('lineend', m)] or 0
422 Nx.met, Nx.var, Nx.char = m, m.var, c
423 Nx.pre = ltjs_fast_get_penalty_table('pre', c) or 0
424 Nx.post = ltjs_fast_get_penalty_table('post', c) or 0
425 Nx.xspc = ltjs_fast_get_penalty_table('xsp', c) or 3
426 Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
428 local set_np_xspc_jachar = set_np_xspc_jachar
431 local floor = math.floor
432 function set_np_xspc_alchar(Nx, c,x, lig)
434 if lig == ligature_head then
435 while x.components and x.subtype and math.floor(x.subtype*0.5)%2==1 do
436 x = x.components; c = x.char
439 while x.components and x.subtype and math.floor(x.subtype*0.5)%2==1 do
440 x = node.tail(x.components); c = x.char
443 Nx.pre = ltjs_fast_get_penalty_table('pre', c) or 0
444 Nx.post = ltjs_fast_get_penalty_table('post', c) or 0
447 Nx.pre, Nx.post, Nx.char = 0, 0, -1
450 Nx.xspc = ltjs_fast_get_penalty_table('xsp', c) or 3
451 Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
453 local set_np_xspc_alchar = set_np_xspc_alchar
456 function extract_np()
457 local x, i = Np.nuc, Np.id;
458 if i == id_jglyph then return set_np_xspc_jachar(Np, x)
459 elseif i == id_glyph then return set_np_xspc_alchar(Np, x.char, x, ligature_head)
460 elseif i == id_hlist then Np.last_char = check_box_high(Np, x.head, nil)
461 elseif i == id_pbox then Np.last_char = check_box_high(Np, Np.first, node_next(Np.last))
462 elseif i == id_disc then Np.last_char = check_box_high(Np, x.replace, nil)
463 elseif i == id_math then return set_np_xspc_alchar(Np, -1, x)
467 -- change the information for the next loop
468 -- (will be done if Nx is an alphabetic character or a hlist)
469 function after_hlist(Nx)
470 local s = Nx.last_char
472 if s.font == (has_attr(s, attr_curjfnt) or -1) then
473 set_np_xspc_jachar(Nx, s)
475 set_np_xspc_alchar(Nx, s.char, s, ligature_tail)
478 Nx.pre, Nx.met = nil, nil
482 function after_alchar(Nx)
484 return set_np_xspc_alchar(Nx, x.char,x, ligature_tail)
488 local after_hlist, after_alchar, extract_np = after_hlist, after_alchar, extract_np
490 -------------------- 最下層の処理
492 local function lineend_fix(g)
493 if g and g.id==id_kern then
495 elseif Nq.lend~=0 then
497 g = node_new(id_kern); --copy_attr(g, Nq.nuc);
498 g.subtype = 1; g.kern = -Nq.lend;
499 set_attr(g, attr_icflag, LINEEND)
501 g.spec.width = g.spec.width - Nq.lend
507 -- change penalties (or create a new penalty, if needed)
508 local function handle_penalty_normal(post, pre, g)
509 local a = (pre or 0) + (post or 0)
511 if (a~=0 and not(g and g.id==id_kern)) or Nq.lend~=0 then
512 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
513 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
515 head = insert_before(head, Np.first, p)
517 set_attr(p, attr_icflag, KINSOKU)
519 else for _, v in pairs(Bp) do add_penalty(v,a) end
523 local function handle_penalty_always(post, pre, g)
524 local a = (pre or 0) + (post or 0)
526 if not (g and g.id==id_glue) or Nq.lend~=0 then
527 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
528 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
530 head = insert_before(head, Np.first, p)
532 set_attr(p, attr_icflag, KINSOKU)
534 else for _, v in pairs(Bp) do add_penalty(v,a) end
538 local function handle_penalty_suppress(post, pre, g)
539 local a = (pre or 0) + (post or 0)
541 if g and g.id==id_glue then
542 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
543 p.penalty = 10000; head = insert_before(head, Np.first, p)
545 set_attr(p, attr_icflag, KINSOKU)
547 else for _, v in pairs(Bp) do add_penalty(v,a) end
551 -- 和文文字間の JFM glue を node 化
552 local function new_jfm_glue(Nn, bc, ac)
553 -- bc, ac: char classes
554 local z = Nn.met.size_cache.char_type[bc]
557 g = node_copy(g); g.spec = node.copy(g.spec);
558 elseif z.kern[ac] then
559 g = node_new(id_kern); --copy_attr(g, Nn.nuc)
560 g.subtype = 1; g.kern = z.kern[ac]
561 set_attr(g, attr_icflag, FROM_JFM);
566 -- Nq.last (kern w) .... (glue/kern g) Np.first
567 local function real_insert(w, g)
569 local h = node_new(id_kern); --copy_attr(h, Nq.nuc)
570 set_attr(h, attr_icflag, LINE_END)
571 h.kern = w; h.subtype = 1
572 head = node.insert_after(head, Nq.last, h)
575 head = insert_before(head, Np.first, g)
581 -------------------- 和文文字間空白量の決定
586 local function get_kanjiskip_normal()
587 local g = node_new(id_glue); --copy_attr(g, Nq.nuc)
588 g.spec = (Np.auto_kspc or Nq.auto_kspc) and node_copy(kanji_skip) or get_zero_spec()
589 set_attr(g, attr_icflag, KANJI_SKIP)
592 local function get_kanjiskip_jfm()
593 local g = node_new(id_glue); --copy_attr(g, Nq.nuc)
594 if Np.auto_kspc or Nq.auto_kspc then
595 local gx = node_new(id_glue_spec);
596 gx.stretch_order, gx.shrink_order = 0, 0
597 local bk = Nq.met.size_cache.kanjiskip
599 if (Np.met.size_cache==Nq.met.size_cache) and (Nq.var==Np.var) then
602 ak = Np.met.size_cache.kanjiskip
606 gx.width = round(diffmet_rule(bk[1], ak[1]))
607 gx.stretch = round(diffmet_rule(bk[2], ak[2]))
608 gx.shrink = -round(diffmet_rule(-bk[3], -ak[3]))
610 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
613 gx.width = ak[1]; gx.stretch = ak[2]; gx.shrink = ak[3]
615 gx.width, gx.stretch, gx.shrink = 0, 0, 0
619 g.spec = get_zero_spec()
621 set_attr(g, attr_icflag, KANJI_SKIP)
625 local function calc_ja_ja_aux(gb,ga)
629 if not ga then return gb end
630 local k = node.type(gb.id) .. node.type(ga.id)
631 if k == 'glueglue' then
633 gb.spec.width = round(diffmet_rule(gb.spec.width, ga.spec.width))
634 gb.spec.stretch = round(diffmet_rule(gb.spec.stretch,ga.spec.shrink))
635 gb.spec.shrink = -round(diffmet_rule(-gb.spec.shrink, -ga.spec.shrink))
638 elseif k == 'kernkern' then
640 gb.kern = round(diffmet_rule(gb.kern, ga.kern))
643 elseif k == 'kernglue' then
644 -- gb: kern, ga: glue
645 ga.spec.width = round(diffmet_rule(gb.kern,ga.spec.width))
646 ga.spec.stretch = round(diffmet_rule(ga.spec.stretch, 0))
647 ga.spec.shrink = -round(diffmet_rule(-ga.spec.shrink, 0))
651 -- gb: glue, ga: kern
652 gb.spec.width = round(diffmet_rule(ga.kern, gb.spec.width))
653 gb.spec.stretch = round(diffmet_rule(gb.spec.stretch, 0))
654 gb.spec.shrink = -round(diffmet_rule(-gb.spec.shrink, 0))
661 local function calc_ja_ja_glue()
662 if ihb_flag then return nil
663 elseif (Nq.met.size_cache==Np.met.size_cache) and (Nq.var==Np.var) then
664 return new_jfm_glue(Nq, Nq.class, Np.class)
666 return calc_ja_ja_aux(new_jfm_glue(Nq, Nq.class, fast_find_char_class('diffmet',Nq.met)),
667 new_jfm_glue(Np, fast_find_char_class('diffmet',Np.met), Np.class))
671 -------------------- 和欧文間空白量の決定
675 local function get_xkanjiskip_normal(Nn)
676 local g = node_new(id_glue); --copy_attr(g, Nn.nuc)
677 local gx = node_new(id_glue_spec); g.spec = gx
678 if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
679 g.spec = node_copy(xkanji_skip)
681 g.spec = get_zero_spec()
683 set_attr(g, attr_icflag, XKANJI_SKIP)
686 local function get_xkanjiskip_jfm(Nn)
687 local g = node_new(id_glue); --copy_attr(g, Nn.nuc)
688 if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
689 local gx = node_new(id_glue_spec);
690 gx.stretch_order, gx.shrink_order = 0, 0
691 local bk = Nn.met.size_cache.xkanjiskip
693 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
695 gx.width, gx.stretch, gx.shrink = 0, 0, 0
699 g.spec = get_zero_spec()
701 set_attr(g, attr_icflag, XKANJI_SKIP)
707 -------------------- 隣接した「塊」間の処理
709 local function get_OA_skip()
711 return new_jfm_glue(Np,
712 fast_find_char_class(((Nq.id == id_math and -1) or 'jcharbdd'), Np.met), Np.class)
716 local function get_OB_skip()
718 return new_jfm_glue(Nq, Nq.class,
719 fast_find_char_class(((Np.id == id_math and -1) or'jcharbdd'), Nq.met))
724 -- (anything) .. jachar
725 local function handle_np_jachar(mode)
726 if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id==id_pbox_w) and Nq.met) then
727 local g = lineend_fix(calc_ja_ja_glue() or get_kanjiskip()) -- M->K
728 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
729 elseif Nq.met then -- Nq.id==id_hlist
730 local g = get_OA_skip() or get_kanjiskip() -- O_A->K
731 handle_penalty_normal(0, Np.pre, g); real_insert(0, g)
733 local g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
734 if Nq.id==id_hlist then Nq.post = 0 end
735 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
737 local g = get_OA_skip() -- O_A
738 if Nq.id==id_glue then handle_penalty_normal(0, Np.pre, g)
739 elseif Nq.id==id_kern then handle_penalty_suppress(0, Np.pre, g)
740 else handle_penalty_always(0, Np.pre, g)
744 if mode and (ltjs_fast_get_penalty_table('kcat', Np.char) or 0)%2~=1 then
745 widow_Np.first, widow_Bp, Bp = Np.first, Bp, widow_Bp
750 -- jachar .. (anything)
751 local function handle_nq_jachar()
753 if Np.id==id_hlist then Np.pre = 0 end
754 local g = lineend_fix(get_OB_skip() or get_xkanjiskip(Nq)) -- O_B->X
755 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
757 local g = lineend_fix(get_OB_skip()) -- O_B
758 if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
759 elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
760 else handle_penalty_always(Nq.post, 0, g)
762 real_insert(Nq.lend, g)
766 -- (anything) .. (和文文字で始まる hlist)
767 local function handle_np_ja_hlist()
768 if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id == id_pbox_w) and Nq.met) then
769 local g = lineend_fix(get_OB_skip() or get_kanjiskip()) -- O_B->K
770 handle_penalty_normal(Nq.post, 0, g); real_insert(Nq.lend, g)
771 elseif Nq.met then -- Nq.id==id_hlist
772 local g = get_kanjiskip() -- K
773 handle_penalty_suppress(0, 0, g); real_insert(0, g)
775 local g = get_xkanjiskip(Np) -- X
776 handle_penalty_suppress(0, 0, g); real_insert(0, g)
780 -- (和文文字で終わる hlist) .. (anything)
781 local function handle_nq_ja_hlist()
783 local g = get_xkanjiskip(Nq) -- X
784 handle_penalty_suppress(0, 0, g); real_insert(0, g)
788 -- Nq が前側のクラスタとなることによる修正
789 local function adjust_nq()
790 if Nq.id==id_glyph then after_alchar(Nq)
791 elseif Nq.id==id_hlist or Nq.id==id_pbox or Nq.id==id_disc then after_hlist(Nq)
792 elseif Nq.id == id_pbox_w then
793 luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
798 -------------------- 開始・終了時の処理
801 local function handle_list_tail(mode)
804 -- the current list is to be line-breaked:
805 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
807 g = node_new(id_kern); g.subtype = 0; g.kern = Np.lend
808 --copy_attr(g, Np.nuc);
809 set_attr(g, attr_icflag, BOXBDD)
810 node.insert_after(head, Np.last, g)
813 -- Insert \jcharwidowpenalty
814 Bp = widow_Bp; Np = widow_Np; Nq.lend = 0
816 handle_penalty_normal(0,
817 ltjs_fast_get_penalty_table('jwp', 0) or 0)
820 -- the current list is the contents of a hbox
821 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
822 local g = new_jfm_glue(Np, Np.class, fast_find_char_class('boxbdd',Np.met))
824 set_attr(g, attr_icflag, BOXBDD)
825 head = node.insert_after(head, Np.last, g)
832 local function handle_list_head(par_indented)
833 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
835 local g = new_jfm_glue(Np, fast_find_char_class(par_indented, Np.met), Np.class)
837 set_attr(g, attr_icflag, BOXBDD)
838 if g.id==id_glue and #Bp==0 then
839 local h = node_new(id_penalty); --copy_attr(h, Np.nuc)
840 h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
842 head = insert_before(head, Np.first, g)
849 -- return value: (the initial cursor lp), (last node)
850 local function init_var(mode)
851 Bp, widow_Bp, widow_Np = {}, {}, {first = nil}
852 kanji_skip=skip_table_to_spec('kanjiskip')
853 get_kanjiskip = (kanji_skip.width == max_dimen)
854 and get_kanjiskip_jfm or get_kanjiskip_normal
855 xkanji_skip=skip_table_to_spec('xkanjiskip')
856 get_xkanjiskip = (xkanji_skip.width == max_dimen)
857 and get_xkanjiskip_jfm or get_xkanjiskip_normal
859 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
860 first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil,
861 post=nil, pre=nil, xspc=nil,
864 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
865 first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil,
866 post=nil, pre=nil, xspc=nil,
869 -- the current list is to be line-breaked:
870 -- hbox from \parindent is skipped.
871 local lp, par_indented = head, 'boxbdd'
872 while lp and ((lp.id==id_whatsit and lp.subtype~=sid_user)
873 or ((lp.id==id_hlist) and (lp.subtype==3))) do
874 if (lp.id==id_hlist) and (lp.subtype==3) then par_indented = 'parbdd' end
876 return lp, node.tail(head), par_indented
878 -- the current list is the contents of a hbox:
880 local g = node_new(id_kern)
881 node.insert_after(head, node.tail(head), g); last = g
882 return head, g, 'boxbdd'
886 local function cleanup(mode, last)
887 -- adjust attr_icflag for avoiding error
888 tex.setattribute('global', attr_icflag, 0)
889 node.free(kanji_skip); node.free(xkanji_skip)
891 local h = node_next(head)
892 if h.id == id_penalty and h.penalty == 10000 then
894 if h.id == id_glue and h.subtype == 15 and not h.next then
900 head = node.remove(head, last); node.free(last);-- remove the sentinel
901 set_attr(head, attr_icflag,
902 get_attr_icflag(head) + PROCESSED_BEGIN_FLAG);
906 -------------------- 外部から呼ばれる関数
909 function main(ahead, mode)
910 if not ahead then return ahead end
912 local lp, last, par_indented = init_var(mode);
913 lp = calc_np(lp, last)
915 extract_np(); handle_list_head(par_indented)
917 return cleanup(mode, last)
919 lp = calc_np(lp, last)
921 extract_np(); adjust_nq()
923 if Np.id == id_jglyph then
924 handle_np_jachar(mode)
926 if Np.id==id_hlist then handle_np_ja_hlist()
927 else handle_np_jachar() end
929 if Nq.id==id_hlist then handle_nq_ja_hlist()
930 else handle_nq_jachar() end
932 lp = calc_np(lp, last)
934 handle_list_tail(mode)
935 return cleanup(mode, last)
940 function create_inhibitglue_node()
941 local tn = node_new(id_whatsit, sid_user)
942 tn.user_id=30111; tn.type=100; tn.value=1
946 -- Node for indicating beginning of a paragraph
948 function create_beginpar_node()
949 local tn = node_new(id_whatsit, sid_user)
950 tn.user_id=30114; tn.type=100; tn.value=1
956 local function whatsit_callback(Np, lp, Nq)
957 if Np and Np.nuc then return Np
958 elseif Np and lp.user_id == 30114 then
959 Np.first = lp; Np.nuc = lp; Np.last = lp
962 Np.pre = 0; Np.post = 0
968 local function whatsit_after_callback(s, Nq, Np)
969 if not s and Nq.nuc.user_id == 30114 then
970 local x, y = node.prev(Nq.nuc), Nq.nuc
971 Nq.first, Nq.nuc, Nq.last = x, x, x
972 head = node.remove(head, y)
977 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
978 "luatexja.beginpar.np_info", 1)
979 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
980 "luatexja.beginpar.np_info_after", 1)