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')
61 -- FROM_JFM: 4, 5, 6, 7, 8 →優先度高
64 local XKANJI_SKIP = 10
66 local IC_PROCESSED = 12
68 local PROCESSED_BEGIN_FLAG = 32
73 local attr_orig_char = luatexbase.attributes['ltj@origchar']
74 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
75 local attr_icflag = luatexbase.attributes['ltj@icflag']
76 local attr_autospc = luatexbase.attributes['ltj@autospc']
77 local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
78 local max_dimen = 1073741823
80 local function get_attr_icflag(p)
81 return (has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
84 -------------------- Helper functions
86 local function copy_attr(new, old)
90 -- This function is called only for acquiring `special' characters.
91 local function fast_find_char_class(c,m)
92 return m.size_cache.chars[c] or 0
96 local function slow_find_char_class(c, m, oc)
98 local cls = ltjf_find_char_class(oc, m)
99 if xc ~= oc and cls==0 then cls, xc = ltjf_find_char_class(-xc, m) end
103 local spec_zero_glue = node_new(id_glue_spec)
104 spec_zero_glue.width = 0; spec_zero_glue.stretch_order = 0; spec_zero_glue.stretch = 0
105 spec_zero_glue.shrink_order = 0; spec_zero_glue.shrink = 0
107 local function get_zero_spec()
108 return node_copy(spec_zero_glue)
111 local function skip_table_to_spec(n)
112 local g, st = node_new(id_glue_spec), ltjs.fast_get_skip_table(n)
113 g.width = st.width; g.stretch = st.stretch; g.shrink = st.shrink
114 g.stretch_order = st.stretch_order; g.shrink_order = st.shrink_order
120 local function add_penalty(p,e)
121 if p.penalty>=10000 then
122 if e<=-10000 then p.penalty = 0 end
123 elseif p.penalty<=-10000 then
124 if e>=10000 then p.penalty = 0 end
126 p.penalty = p.penalty + e
127 if p.penalty>=10000 then p.penalty = 10000
128 elseif p.penalty<=-10000 then p.penalty = -10000 end
134 diffmet_rule = math.two_paverage
135 function math.two_add(a,b) return a+b end
136 function math.two_average(a,b) return (a+b)*0.5 end
137 function math.two_paverage(a,b) return (a+b)*0.5 end
138 function math.two_pleft(a,b) return a end
139 function math.two_pright(a,b) return b end
141 -------------------- idea
142 -- 2 node の間に glue/kern/penalty を挿入する.
143 -- 基本方針: char node q と char node p の間
146 -- first: 最初の node,nuc: p,last: 最後の node
149 -- 実際の glue は Np.last, Nq.first の間に挿入される
150 -- Bp: Np.last, Nq.first の間の penalty node 達の配列
152 -- Np, Nq, Bp, widow_Bp について
154 -- 1回のループごとに Nq = Np, Np = (new table) となるのは効率が悪いので,
155 -- Np <-> Nq 入れ替え,その後 Np をクリアすることでテーブルを再利用.
156 -- 同様の関係は Bp, widow_Bp にも.
160 -- node x が non-char node のときは,x のみ
161 -- x が char_node のときは,
162 -- - x が \accent の第二引数だったとき
163 -- [kern2 kern y kern2] x の 3 node が核に加わる
164 -- - x の直後に \/ 由来 kern があったとき
165 -- その \/ 由来の kern が核に加わる
167 -- ins, mark, adjust, whatsit, penalty
169 -- Nq.last .. Bp.first .... Bp[last] .... * .. Np.first
170 -- *: jfm glue はここに入る
172 local head -- the head of current list
175 local widow_Bp, widow_Np -- \jcharwidowpenalty 挿入位置管理用
177 local ihb_flag -- JFM グルー挿入抑止用 flag
178 -- on: \inhibitglue 指定時,hlist の周囲
180 -------------------- hlist 内の文字の検索
182 local first_char, last_char, find_first_char
184 local function check_box(box_ptr, box_end)
185 local p = box_ptr; local found_visible_node = false
187 find_first_char = false; first_char = nil; last_char = nil
190 while p and p~=box_end do
192 if pid==id_kern and p.subtype==2 then
193 p = node_next(node_next(node_next(p))); pid = p.id -- p must be glyph_node
195 if pid==id_glyph then
197 if find_first_char then
198 first_char = p; find_first_char = false
200 last_char = p; found_visible_node = true; p=node_next(p)
201 if (not p) or p==box_end then return found_visible_node end
203 pid = p.id -- p must be non-nil
205 if pid==id_kern and get_attr_icflag(p)==IC_PROCESSED then
207 elseif pid==id_hlist then
208 if PACKED == get_attr_icflag(p) then
209 if find_first_char then
210 first_char = p.head; find_first_char = false
212 last_char = p.head; found_visible_node = true
215 if check_box(p.head, nil) then found_visible_node = true end
216 else if find_first_char then
217 find_first_char = false
223 elseif not (pid==id_ins or pid==id_mark
224 or pid==id_adjust or pid==id_whatsit
225 or pid==id_penalty) then
226 found_visible_node = true
227 if find_first_char then
228 find_first_char = false
235 return found_visible_node
238 function check_box_high(Nx, box_ptr, box_end)
239 first_char = nil; last_char = nil; find_first_char = true
240 if check_box(box_ptr, box_end) then
242 if first_char.font == (has_attr(first_char, attr_curjfnt) or -1) then
243 set_np_xspc_jachar(Nx, first_char)
245 set_np_xspc_alchar(Nx, first_char.char,first_char, ligature_head)
252 -------------------- Np の計算と情報取得
254 luatexbase.create_callback("luatexja.jfmglue.whatsit_getinfo", "data",
255 function (Np, lp, Nq)
256 if Np.nuc then return Np
258 return Np -- your code
261 luatexbase.create_callback("luatexja.jfmglue.whatsit_after", "data",
262 function (stat, Nq, Np) return false end)
267 local function set_attr_icflag_processed(p)
268 if get_attr_icflag(p)<= ITALIC then
269 set_attr(p, attr_icflag, PROCESSED)
273 local function check_next_ickern(lp)
274 if lp.id == id_kern and ITALIC == get_attr_icflag(lp) then
275 set_attr(lp, attr_icflag, IC_PROCESSED)
276 Np.last = lp; return node_next(lp)
278 Np.last = Np.nuc; return lp
282 local function calc_np_pbox(lp, last)
283 Np.first = Np.first or lp; Np.id = id_pbox
284 local lpa = KINSOKU -- dummy=
285 set_attr(lp, attr_icflag, get_attr_icflag(lp));
286 while lp~=last and lpa>=PACKED and lpa<BOXBDD do
288 lp = node_next(lp); lpa = has_attr(lp, attr_icflag) or 0
289 -- get_attr_icflag() ではいけない!
291 return check_next_ickern(lp)
295 local calc_np_auxtable = {
296 [id_glyph] = function (lp)
297 Np.first, Np.nuc = (Np.first or lp), lp;
298 Np.id = (lp.font == (has_attr(lp, attr_curjfnt) or -1)) and id_jglyph or id_glyph
299 --set_attr_icflag_processed(lp) treated in ltj-setwidth.lua
300 return true, check_next_ickern(node_next(lp));
302 [id_hlist] = function(lp)
303 Np.first = Np.first or lp; Np.last = lp; Np.nuc = lp;
304 set_attr_icflag_processed(lp)
305 Np.id = (lp.shift~=0) and id_box_like or id_hlist
306 return true, node_next(lp)
308 box_like = function(lp)
309 Np.first = Np.first or lp; Np.nuc = lp; Np.last = lp;
310 Np.id = id_box_like; set_attr_icflag_processed(lp);
311 return true, node_next(lp);
314 set_attr_icflag_processed(lp); return false, node_next(lp)
316 [id_whatsit] = function(lp)
317 if lp.subtype==sid_user then
318 if lp.user_id==30111 then
319 local lq = node_next(lp);
320 head = node.remove(head, lp); node.free(lp); ihb_flag = true
323 set_attr_icflag_processed(lp)
324 luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo",
327 Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc;
328 return true, node_next(lp)
330 return false, node_next(lp)
334 -- we do special treatment for these whatsit nodes.
335 if lp.subtype == sid_start_link or lp.subtype == sid_start_thread then
337 elseif lp.subtype == sid_end_link or lp.subtype == sid_end_thread then
338 Np.first, Nq.last = nil, lp;
340 set_attr_icflag_processed(lp); return false, node_next(lp)
343 [id_math] = function(lp)
344 Np.first, Np.nuc = (Np.first or lp), lp;
345 set_attr_icflag_processed(lp); lp = node_next(lp)
346 while lp.id~=id_math do
347 set_attr_icflag_processed(lp); lp = node_next(lp)
349 set_attr_icflag_processed(lp);
350 Np.last, Np.id = lp, id_math;
351 return true, node_next(lp);
353 discglue = function(lp)
354 Np.first, Np.nuc, Np.last = (Np.first or lp), lp, lp;
355 Np.id = lp.id; set_attr_icflag_processed(lp); return true, node_next(lp)
357 [id_kern] = function(lp)
358 Np.first = Np.first or lp
359 if lp.subtype==2 then
360 set_attr_icflag_processed(lp); lp = node_next(lp)
361 set_attr_icflag_processed(lp); lp = node_next(lp)
362 set_attr_icflag_processed(lp); lp = node_next(lp)
363 set_attr_icflag_processed(lp); Np.nuc = lp
364 Np.id = (lp.font == (has_attr(lp, attr_curjfnt) or -1)) and id_jglyph or id_glyph
365 return true, check_next_ickern(node_next(lp));
367 Np.id = id_kern; set_attr_icflag_processed(lp);
368 Np.last = lp; return true, node_next(lp)
371 [id_penalty] = function(lp)
372 Bp[#Bp+1] = lp; set_attr_icflag_processed(lp);
373 return false, node_next(lp)
376 calc_np_auxtable[id_vlist] = calc_np_auxtable.box_like
377 calc_np_auxtable[id_rule] = calc_np_auxtable.box_like
378 calc_np_auxtable[13] = calc_np_auxtable.box_like
379 calc_np_auxtable[id_ins] = calc_np_auxtable.skip
380 calc_np_auxtable[id_mark] = calc_np_auxtable.skip
381 calc_np_auxtable[id_adjust] = calc_np_auxtable.skip
382 calc_np_auxtable[id_disc] = calc_np_auxtable.discglue
383 calc_np_auxtable[id_glue] = calc_np_auxtable.discglue
386 function calc_np(lp, last)
388 -- We assume lp = node_next(Np.last)
389 Np, Nq, ihb_flag = Nq, Np, false
390 -- We clear `predefined' entries of Np before pairs() loop,
391 -- because using only pairs() loop is slower.
392 Np.post, Np.pre, Np.xspc = nil, nil, nil
393 Np.first, Np.id, Np.last, Np.met = nil, nil, nil
394 Np.auto_kspc, Np.auto_xspc, Np.char, Np.class, Np.nuc = nil, nil, nil, nil, nil
395 for k in pairs(Np) do Np[k] = nil end
397 for k = 1,#Bp do Bp[k] = nil end
399 local lpa = has_attr(lp, attr_icflag) or 0
402 if lpa == BOXBDD then
403 local lq = node_next(lp)
404 head = node.remove(head, lp); node.free(lp); lp = lq
405 else return calc_np_pbox(lp, last)
408 k, lp = calc_np_auxtable[lp.id](lp)
409 if k then return lp end
416 local calc_np = calc_np
418 -- extract informations from Np
419 -- We think that "Np is a Japanese character" if Np.met~=nil,
420 -- "Np is an alphabetic character" if Np.pre~=nil,
421 -- "Np is not a character" otherwise.
425 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
426 function set_np_xspc_jachar(Nx, x)
427 local m = ltjf_font_metric_table[x.font]
429 cls, c = slow_find_char_class(has_attr(x, attr_orig_char), m, x.char)
430 Nx.class = cls; set_attr(x, attr_jchar_class, cls)
431 Nx.met, Nx.var, Nx.char = m, m.var, c
432 Nx.pre = ltjs_fast_get_penalty_table('pre', c) or 0
433 Nx.post = ltjs_fast_get_penalty_table('post', c) or 0
434 Nx.xspc = ltjs_fast_get_penalty_table('xsp', c) or 3
435 Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
437 local set_np_xspc_jachar = set_np_xspc_jachar
440 local floor = math.floor
441 function set_np_xspc_alchar(Nx, c,x, lig)
443 if lig == ligature_head then
444 while x.components and x.subtype and math.floor(x.subtype*0.5)%2==1 do
445 x = x.components; c = x.char
448 while x.components and x.subtype and math.floor(x.subtype*0.5)%2==1 do
449 x = node.tail(x.components); c = x.char
452 Nx.pre = ltjs_fast_get_penalty_table('pre', c) or 0
453 Nx.post = ltjs_fast_get_penalty_table('post', c) or 0
456 Nx.pre, Nx.post, Nx.char = 0, 0, -1
459 Nx.xspc = ltjs_fast_get_penalty_table('xsp', c) or 3
460 Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
462 local set_np_xspc_alchar = set_np_xspc_alchar
465 function extract_np()
466 local x, i = Np.nuc, Np.id;
467 if i == id_jglyph then return set_np_xspc_jachar(Np, x)
468 elseif i == id_glyph then return set_np_xspc_alchar(Np, x.char, x, ligature_head)
469 elseif i == id_hlist then Np.last_char = check_box_high(Np, x.head, nil)
470 elseif i == id_pbox then Np.last_char = check_box_high(Np, Np.first, node_next(Np.last))
471 elseif i == id_disc then Np.last_char = check_box_high(Np, x.replace, nil)
472 elseif i == id_math then return set_np_xspc_alchar(Np, -1, x)
476 -- change the information for the next loop
477 -- (will be done if Nx is an alphabetic character or a hlist)
478 function after_hlist(Nx)
479 local s = Nx.last_char
481 if s.font == (has_attr(s, attr_curjfnt) or -1) then
482 set_np_xspc_jachar(Nx, s)
484 set_np_xspc_alchar(Nx, s.char, s, ligature_tail)
487 Nx.pre, Nx.met = nil, nil
491 function after_alchar(Nx)
493 return set_np_xspc_alchar(Nx, x.char,x, ligature_tail)
497 local after_hlist, after_alchar, extract_np = after_hlist, after_alchar, extract_np
499 -------------------- 最下層の処理
501 -- change penalties (or create a new penalty, if needed)
502 local function handle_penalty_normal(post, pre, g)
503 local a = (pre or 0) + (post or 0)
505 if (a~=0 and not(g and g.id==id_kern)) then
506 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
507 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
509 head = insert_before(head, Np.first, p)
511 set_attr(p, attr_icflag, KINSOKU)
513 else for _, v in pairs(Bp) do add_penalty(v,a) end
517 local function handle_penalty_always(post, pre, g)
518 local a = (pre or 0) + (post or 0)
520 if not (g and g.id==id_glue) then
521 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
522 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
524 head = insert_before(head, Np.first, p)
526 set_attr(p, attr_icflag, KINSOKU)
528 else for _, v in pairs(Bp) do add_penalty(v,a) end
532 local function handle_penalty_suppress(post, pre, g)
533 local a = (pre or 0) + (post or 0)
535 if g and g.id==id_glue then
536 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
537 p.penalty = 10000; head = insert_before(head, Np.first, p)
539 set_attr(p, attr_icflag, KINSOKU)
541 else for _, v in pairs(Bp) do add_penalty(v,a) end
545 -- 和文文字間の JFM glue を node 化
546 local function new_jfm_glue(Nn, bc, ac)
547 -- bc, ac: char classes
548 local z = Nn.met.size_cache.char_type[bc]
549 local g, d = z.glue[ac], 0
551 g,d = node_copy(g[1]), g[2]; g.spec = node.copy(g.spec);
552 elseif z.kern[ac] then
553 g = node_new(id_kern); --copy_attr(g, Nn.nuc)
554 g.subtype = 1; g.kern = z.kern[ac][1]
555 set_attr(g, attr_icflag, FROM_JFM);
563 -- Nq.last (kern w) .... (glue/kern g) Np.first
564 local function real_insert(w, g)
566 head = insert_before(head, Np.first, g)
572 -------------------- 和文文字間空白量の決定
577 local function get_kanjiskip_normal()
578 local g = node_new(id_glue); --copy_attr(g, Nq.nuc)
579 g.spec = (Np.auto_kspc or Nq.auto_kspc) and node_copy(kanji_skip) or get_zero_spec()
580 set_attr(g, attr_icflag, KANJI_SKIP)
583 local function get_kanjiskip_jfm()
584 local g = node_new(id_glue); --copy_attr(g, Nq.nuc)
585 if Np.auto_kspc or Nq.auto_kspc then
586 local gx = node_new(id_glue_spec);
587 gx.stretch_order, gx.shrink_order = 0, 0
588 local bk = Nq.met.size_cache.kanjiskip
590 if (Np.met.size_cache==Nq.met.size_cache) and (Nq.var==Np.var) then
593 ak = Np.met.size_cache.kanjiskip
597 gx.width = round(diffmet_rule(bk[1], ak[1]))
598 gx.stretch = round(diffmet_rule(bk[2], ak[2]))
599 gx.shrink = -round(diffmet_rule(-bk[3], -ak[3]))
601 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
604 gx.width = ak[1]; gx.stretch = ak[2]; gx.shrink = ak[3]
606 gx.width, gx.stretch, gx.shrink = 0, 0, 0
610 g.spec = get_zero_spec()
612 set_attr(g, attr_icflag, KANJI_SKIP)
616 local function calc_ja_ja_aux(gb,ga, db, da)
617 local rbb, rab = (1-db)/2, (1-da)/2 -- 「前の文字」由来のグルーの割合
618 local rba, raa = (1+db)/2, (1+da)/2 -- 「前の文字」由来のグルーの割合
619 if diffmet_rule ~= math.two_pleft and diffmet_rule ~= math.two_pright
620 and diffmet_rule ~= math.two_paverage then
621 rbb, rab, rba, raa = 1,0,0,1
624 if ga then gb = node_new(id_kern); gb.kern = 0 else return nil end
626 ga = node_new(id_kern); ga.kern = 0
629 local k = node.type(gb.id) .. node.type(ga.id)
630 if k == 'glueglue' then
632 gb.spec.width = round(diffmet_rule(
633 rbb*gb.spec.width + rba*ga.spec.width,
634 rab*gb.spec.width + raa*ga.spec.width ))
635 gb.spec.stretch = round(diffmet_rule(
636 rbb*gb.spec.stretch + rba*ga.spec.stretch,
637 rab*gb.spec.stretch + raa*ga.spec.stretch ))
638 gb.spec.shrink = -round(diffmet_rule(
639 -rbb*gb.spec.shrink - rba*ga.spec.shrink,
640 -rab*gb.spec.shrink - raa*ga.spec.shrink ))
643 elseif k == 'kernkern' then
645 gb.kern = round(diffmet_rule(
646 rbb*gb.kern + rba*ga.kern,
647 rab*gb.kern + raa*ga.kern ))
650 elseif k == 'kernglue' then
651 -- gb: kern, ga: glue
652 ga.spec.width = round(diffmet_rule(
653 rbb*gb.kern + rba*ga.spec.width,
654 rab*gb.kern + raa*ga.spec.width ))
655 ga.spec.stretch = round(diffmet_rule(
656 rba*ga.spec.stretch, raa*ga.spec.stretch ))
657 ga.spec.shrink = -round(diffmet_rule(
658 -rba*ga.spec.shrink,-raa*ga.spec.shrink ))
662 -- gb: glue, ga: kern
663 gb.spec.width = round(diffmet_rule(
664 rba*ga.kern + rbb*gb.spec.width,
665 raa*ga.kern + rab*gb.spec.width ))
666 gb.spec.stretch = round(diffmet_rule(
667 rbb*gb.spec.stretch, rab*gb.spec.stretch ))
668 gb.spec.shrink = -round(diffmet_rule(
669 -rbb*gb.spec.shrink,-rab*gb.spec.shrink ))
675 local function calc_ja_ja_glue()
676 if ihb_flag then return nil
677 elseif (Nq.met.size_cache==Np.met.size_cache) and (Nq.var==Np.var) then
678 return new_jfm_glue(Nq, Nq.class, Np.class)
680 local gb, db = new_jfm_glue(Nq, Nq.class, ltjf_find_char_class(Np.char, Nq.met))
681 local ga, da = new_jfm_glue(Np, ltjf_find_char_class(Nq.char, Np.met), Np.class)
682 return calc_ja_ja_aux(gb, ga, db, da);
686 -------------------- 和欧文間空白量の決定
690 local function get_xkanjiskip_normal(Nn)
691 local g = node_new(id_glue); --copy_attr(g, Nn.nuc)
692 local gx = node_new(id_glue_spec); g.spec = gx
693 if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
694 g.spec = node_copy(xkanji_skip)
696 g.spec = get_zero_spec()
698 set_attr(g, attr_icflag, XKANJI_SKIP)
701 local function get_xkanjiskip_jfm(Nn)
702 local g = node_new(id_glue); --copy_attr(g, Nn.nuc)
703 if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
704 local gx = node_new(id_glue_spec);
705 gx.stretch_order, gx.shrink_order = 0, 0
706 local bk = Nn.met.size_cache.xkanjiskip
708 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
710 gx.width, gx.stretch, gx.shrink = 0, 0, 0
714 g.spec = get_zero_spec()
716 set_attr(g, attr_icflag, XKANJI_SKIP)
722 -------------------- 隣接した「塊」間の処理
724 local function get_OA_skip()
726 return new_jfm_glue(Np,
727 fast_find_char_class(((Nq.id == id_math and -1) or 'jcharbdd'), Np.met), Np.class)
731 local function get_OB_skip()
733 return new_jfm_glue(Nq, Nq.class,
734 fast_find_char_class(((Np.id == id_math and -1) or'jcharbdd'), Nq.met))
739 -- (anything) .. jachar
740 local function handle_np_jachar(mode)
741 if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id==id_pbox_w) and Nq.met) then
742 local g = calc_ja_ja_glue() or get_kanjiskip() -- M->K
743 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
744 elseif Nq.met then -- Nq.id==id_hlist
745 local g = get_OA_skip() or get_kanjiskip() -- O_A->K
746 handle_penalty_normal(0, Np.pre, g); real_insert(0, g)
748 local g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
749 if Nq.id==id_hlist then Nq.post = 0 end
750 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
752 local g = get_OA_skip() -- O_A
753 if Nq.id==id_glue then handle_penalty_normal(0, Np.pre, g)
754 elseif Nq.id==id_kern then handle_penalty_suppress(0, Np.pre, g)
755 else handle_penalty_always(0, Np.pre, g)
759 if mode and (ltjs_fast_get_penalty_table('kcat', Np.char) or 0)%2~=1 then
760 widow_Np.first, widow_Bp, Bp = Np.first, Bp, widow_Bp
765 -- jachar .. (anything)
766 local function handle_nq_jachar()
768 if Np.id==id_hlist then Np.pre = 0 end
769 local g = get_OB_skip() or get_xkanjiskip(Nq) -- O_B->X
770 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
772 local g = get_OB_skip() -- O_B
773 if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
774 elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
775 else handle_penalty_always(Nq.post, 0, g)
781 -- (anything) .. (和文文字で始まる hlist)
782 local function handle_np_ja_hlist()
783 if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id == id_pbox_w) and Nq.met) then
784 local g = get_OB_skip() or get_kanjiskip() -- O_B->K
785 handle_penalty_normal(Nq.post, 0, g); real_insert(0, g)
786 elseif Nq.met then -- Nq.id==id_hlist
787 local g = get_kanjiskip() -- K
788 handle_penalty_suppress(0, 0, g); real_insert(0, g)
790 local g = get_xkanjiskip(Np) -- X
791 handle_penalty_suppress(0, 0, g); real_insert(0, g)
795 -- (和文文字で終わる hlist) .. (anything)
796 local function handle_nq_ja_hlist()
798 local g = get_xkanjiskip(Nq) -- X
799 handle_penalty_suppress(0, 0, g); real_insert(0, g)
803 -- Nq が前側のクラスタとなることによる修正
804 local function adjust_nq()
805 if Nq.id==id_glyph then after_alchar(Nq)
806 elseif Nq.id==id_hlist or Nq.id==id_pbox or Nq.id==id_disc then after_hlist(Nq)
807 elseif Nq.id == id_pbox_w then
808 luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
813 -------------------- 開始・終了時の処理
816 local function handle_list_tail(mode)
819 -- the current list is to be line-breaked.
820 -- Insert \jcharwidowpenalty
821 Bp = widow_Bp; Np = widow_Np
823 handle_penalty_normal(0,
824 ltjs_fast_get_penalty_table('jwp', 0) or 0)
827 -- the current list is the contents of a hbox
828 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
829 local g = new_jfm_glue(Np, Np.class, fast_find_char_class('boxbdd',Np.met))
831 set_attr(g, attr_icflag, BOXBDD)
832 head = node.insert_after(head, Np.last, g)
839 local function handle_list_head(par_indented)
840 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
842 local g = new_jfm_glue(Np, fast_find_char_class(par_indented, Np.met), Np.class)
844 set_attr(g, attr_icflag, BOXBDD)
845 if g.id==id_glue and #Bp==0 then
846 local h = node_new(id_penalty); --copy_attr(h, Np.nuc)
847 h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
849 head = insert_before(head, Np.first, g)
856 -- return value: (the initial cursor lp), (last node)
857 local function init_var(mode)
858 Bp, widow_Bp, widow_Np = {}, {}, {first = nil}
859 kanji_skip=skip_table_to_spec('kanjiskip')
860 get_kanjiskip = (kanji_skip.width == max_dimen)
861 and get_kanjiskip_jfm or get_kanjiskip_normal
862 xkanji_skip=skip_table_to_spec('xkanjiskip')
863 get_xkanjiskip = (xkanji_skip.width == max_dimen)
864 and get_xkanjiskip_jfm or get_xkanjiskip_normal
866 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
867 first=nil, id=nil, last=nil, met=nil, nuc=nil,
868 post=nil, pre=nil, xspc=nil,
871 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
872 first=nil, id=nil, last=nil, met=nil, nuc=nil,
873 post=nil, pre=nil, xspc=nil,
876 -- the current list is to be line-breaked:
877 -- hbox from \parindent is skipped.
878 local lp, par_indented = head, 'boxbdd'
879 while lp and ((lp.id==id_whatsit and lp.subtype~=sid_user)
880 or ((lp.id==id_hlist) and (lp.subtype==3))) do
881 if (lp.id==id_hlist) and (lp.subtype==3) then par_indented = 'parbdd' end
883 return lp, node.tail(head), par_indented
885 -- the current list is the contents of a hbox:
887 local g = node_new(id_kern)
888 node.insert_after(head, node.tail(head), g); last = g
889 return head, g, 'boxbdd'
893 local function cleanup(mode, last)
894 -- adjust attr_icflag for avoiding error
895 tex.setattribute('global', attr_icflag, 0)
896 node.free(kanji_skip); node.free(xkanji_skip)
898 local h = node_next(head)
899 if h.id == id_penalty and h.penalty == 10000 then
901 if h.id == id_glue and h.subtype == 15 and not h.next then
907 head = node.remove(head, last); node.free(last);-- remove the sentinel
908 set_attr(head, attr_icflag,
909 get_attr_icflag(head) + PROCESSED_BEGIN_FLAG);
913 -------------------- 外部から呼ばれる関数
916 function main(ahead, mode)
917 if not ahead then return ahead end
919 local lp, last, par_indented = init_var(mode);
920 lp = calc_np(lp, last)
922 extract_np(); handle_list_head(par_indented)
924 return cleanup(mode, last)
926 lp = calc_np(lp, last)
928 extract_np(); adjust_nq()
930 if Np.id == id_jglyph then
931 handle_np_jachar(mode)
933 if Np.id==id_hlist then handle_np_ja_hlist()
934 else handle_np_jachar() end
936 if Nq.id==id_hlist then handle_nq_ja_hlist()
937 else handle_nq_jachar() end
939 lp = calc_np(lp, last)
941 handle_list_tail(mode)
942 return cleanup(mode, last)
947 function create_inhibitglue_node()
948 local tn = node_new(id_whatsit, sid_user)
949 tn.user_id=30111; tn.type=100; tn.value=1
953 -- Node for indicating beginning of a paragraph
955 function create_beginpar_node()
956 local tn = node_new(id_whatsit, sid_user)
957 tn.user_id=30114; tn.type=100; tn.value=1
963 local function whatsit_callback(Np, lp, Nq)
964 if Np and Np.nuc then return Np
965 elseif Np and lp.user_id == 30114 then
966 Np.first = lp; Np.nuc = lp; Np.last = lp
969 Np.pre = 0; Np.post = 0
975 local function whatsit_after_callback(s, Nq, Np)
976 if not s and Nq.nuc.user_id == 30114 then
977 local x, y = node.prev(Nq.nuc), Nq.nuc
978 Nq.first, Nq.nuc, Nq.last = x, x, x
979 head = node.remove(head, y)
984 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
985 "luatexja.beginpar.np_info", 1)
986 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
987 "luatexja.beginpar.np_info_after", 1)