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 require('luatexja.base'); local ltjb = luatexja.base
14 require('luatexja.stack'); local ltjs = luatexja.stack
15 require('luatexja.jfont'); local ltjf = luatexja.jfont
16 require('luatexja.pretreat'); local ltjp = luatexja.pretreat
18 local node_type = node.type
19 local node_new = node.new
20 local node_remove = node.remove
21 local node_prev = node.prev
22 local node_next = node.next
23 local node_copy = node.copy
24 local node_tail = node.tail
25 local node_free = node.free
26 local has_attr = node.has_attribute
27 local set_attr = node.set_attribute
28 local node_insert_before = node.insert_before
29 local node_insert_after = node.insert_after
30 local round = tex.round
31 local table_insert = table.insert
33 local id_glyph = node.id('glyph')
34 local id_hlist = node.id('hlist')
35 local id_vlist = node.id('vlist')
36 local id_rule = node.id('rule')
37 local id_ins = node.id('ins')
38 local id_mark = node.id('mark')
39 local id_adjust = node.id('adjust')
40 local id_disc = node.id('disc')
41 local id_whatsit = node.id('whatsit')
42 local id_math = node.id('math')
43 local id_glue = node.id('glue')
44 local id_kern = node.id('kern')
45 local id_penalty = node.id('penalty')
47 local id_glue_spec = node.id('glue_spec')
48 local id_jglyph = node.id('glyph') + 256
49 local id_box_like = node.id('hlist') + 256
50 local id_pbox = node.id('hlist') + 512
51 local sid_user = node.subtype('user_defined')
61 local IC_PROCESSED = 9
67 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
68 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
69 local attr_icflag = luatexbase.attributes['ltj@icflag']
70 local attr_autospc = luatexbase.attributes['ltj@autospc']
71 local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
72 local max_dimen = 1073741823
74 local ltjs_get_penalty_table = ltjs.get_penalty_table
75 local ltjs_get_skip_table = ltjs.get_skip_table
76 local ltjf_font_metric_table = ltjf.font_metric_table
77 local ltjf_metrics = ltjf.metrics
80 -------------------- Helper functions
82 -- This function is called only for acquiring `special' characters.
83 local function find_char_class(c,m)
84 return m.chars[c] or 0
87 local spec_zero_glue = node_new(id_glue_spec)
88 spec_zero_glue.width = 0; spec_zero_glue.stretch_order = 0; spec_zero_glue.stretch = 0
89 spec_zero_glue.shrink_order = 0; spec_zero_glue.shrink = 0
91 local function get_zero_glue()
92 return node_copy(spec_zero_glue)
95 local function skip_table_to_spec(n)
96 local g = node_new(id_glue_spec)
97 local st = ltjs_get_skip_table(n, box_stack_level)
98 g.width = st.width; g.stretch = st.stretch; g.shrink = st.shrink
99 g.stretch_order = st.stretch_order; g.shrink_order = st.shrink_order
104 local function add_penalty(p,e)
105 if p.penalty>=10000 then
106 if e<=-10000 then p.penalty = 0 end
107 elseif p.penalty<=-10000 then
108 if e>=10000 then p.penalty = 0 end
110 p.penalty = p.penalty + e
111 if p.penalty>=10000 then p.penalty = 10000
112 elseif p.penalty<=-10000 then p.penalty = -10000 end
118 diffmet_rule = math.two_average
119 function math.two_add(a,b) return a+b end
120 function math.two_average(a,b) return (a+b)/2 end
122 -------------------- idea
123 -- 2 node の間に glue/kern/penalty を挿入する.
124 -- 基本方針: char node q と char node p の間
127 -- first: 最初の node,nuc: p,last: 最後の node
130 -- 実際の glue は Np.last, Nq.first の間に挿入される
131 -- Bp: Np.last, Nq.first の間の penalty node 達の配列
133 -- Np, Nq, Bp, widow_Bp について
135 -- 1回のループごとに Nq = Np, Np = (new table) となるのは効率が悪いので,
136 -- Np <-> Nq 入れ替え,その後 Np をクリアすることでテーブルを再利用.
137 -- 同様の関係は Bp, widow_Bp にも.
141 -- node x が non-char node のときは,x のみ
142 -- x が char_node のときは,
143 -- - x が \accent の第二引数だったとき
144 -- [kern2 kern y kern2] x の 3 node が核に加わる
145 -- - x の直後に \/ 由来 kern があったとき
146 -- その \/ 由来の kern が核に加わる
148 -- ins, mark, adjust, whatsit, penalty
150 -- Nq.last .. + .. Bp.first .... Bp[last] .... * .. Np.first
151 -- +: kern from LINEEND はここに入る
152 -- *: jfm glue はここに入る
154 local head -- the head of current list
155 local last -- the last node of current list
156 local lp -- 外側での list 走査時のカーソル
159 local widow_Bp, widow_Np -- \jcharwidowpenalty 挿入位置管理用
161 local ihb_flag -- JFM グルー挿入抑止用 flag
162 -- on: \inhibitglue 指定時,hlist の周囲
164 -------------------- hlist 内の文字の検索
166 local first_char, last_char, find_first_char
168 local function check_box(box_ptr, box_end)
169 local p = box_ptr; local found_visible_node = false
171 find_first_char = false; first_char = nil; last_char = nil
174 while p and p~=box_end do
178 p = node_next(node_next(node_next(p))); pid = p.id
179 elseif has_attr(p, attr_icflag)==IC_PROCESSED then
180 p = node_next(p); pid = p.id
183 if pid==id_glyph then
185 if find_first_char then
186 first_char = p; find_first_char = false
188 last_char = p; found_visible_node = true; p=node_next(p)
189 if (not p) or p==box_end then return found_visible_node end
192 if pid==id_hlist then
193 if has_attr(p, attr_icflag)==PACKED then
194 for q in node.traverse_id(id_glyph, p.head) do
195 if find_first_char then
196 first_char = q; find_first_char = false
198 last_char = q; found_visible_node = true; break
202 if check_box(p.head, nil) then found_visible_node = true end
203 else if find_first_char then
204 find_first_char = false
210 elseif not (pid==id_ins or pid==id_mark
211 or pid==id_adjust or pid==id_whatsit
212 or pid==id_penalty) then
213 found_visible_node = true
214 if find_first_char then
215 find_first_char = false
222 return found_visible_node
225 -------------------- Np の計算と情報取得
227 local function set_attr_icflag_processed(p)
228 local a = has_attr(p, attr_icflag) or 0
229 if a<=1 then set_attr(p, attr_icflag, PROCESSED) end
232 local function check_next_ickern()
233 if lp.id == id_kern and has_attr(lp, attr_icflag)==ITALIC then
234 set_attr(lp, attr_icflag, IC_PROCESSED); Np.last = lp; lp = node_next(lp)
235 else Np.last = Np.nuc end
238 local function calc_np_pbox()
239 Np.first = lp; Np.id = id_pbox
240 lpa = KINSOKU -- dummy
241 while lp~=last and lpa>=PACKED and lpa~=BOXBDD do
242 Np.nuc = lp; lp = node_next(lp); lpa = has_attr(lp, attr_icflag) or 0
247 local function calc_np()
248 -- We assume lp = node_next(Np.last)
250 Nr = Nq; for k in pairs(Nr) do Nr[k] = nil end
252 for k in pairs(Bp) do Bp[k] = nil end
255 lpi = lp.id; lpa = has_attr(lp, attr_icflag) or 0
256 if lp==last then Np = nil; return
257 elseif lpa>=PACKED then
258 if lpa == BOXBDD then
259 local lq = node_next(lp)
260 head = node_remove(head, lp); node_free(lp); lp = lq
261 else calc_np_pbox(); return end -- id_pbox
262 elseif lpi == id_ins or lpi == id_mark or lpi == id_adjust then
263 set_attr_icflag_processed(lp); lp = node_next(lp)
264 elseif lpi == id_penalty then
265 table_insert(Bp, lp); set_attr_icflag_processed(lp); lp = node_next(lp)
266 elseif lpi == id_whatsit then
267 if lp.subtype==sid_user and lp.user_id==30111 then
268 local lq = node_next(lp)
269 head = node_remove(head, lp); node_free(lp); lp = lq; ihb_flag = true
271 set_attr_icflag_processed(lp); lp = node_next(lp)
273 else -- a `cluster' is found
275 if lpi == id_glyph then -- id_[j]glyph
276 if lp.font == has_attr(lp, attr_curjfnt) then Np.id = id_jglyph
277 else Np.id = id_glyph end
278 Np.nuc = lp; set_attr_icflag_processed(lp)
279 lp = node_next(lp); check_next_ickern(); return
280 elseif lpi == id_hlist then -- hlist
281 Np.last = lp; Np.nuc = lp; set_attr_icflag_processed(lp)
282 if lp.shift~=0 then Np.id = id_box_like
284 lp = node_next(lp); return
285 elseif lpi == id_vlist or lpi == id_rule then -- id_box_like
286 Np.nuc = lp; Np.last = lp; Np.id = id_box_like; break
287 elseif lpi == id_math then -- id_math
288 Np.nuc = lp; lp = node_next(lp)
289 while lp.id~=id_math do
290 set_attr_icflag_processed(lp); lp = node_next(lp)
292 elseif lpi == id_kern and lp.subtype==2 then -- id_kern
293 set_attr_icflag_processed(lp); lp = node_next(lp)
294 set_attr_icflag_processed(lp); lp = node_next(lp)
295 set_attr_icflag_processed(lp); lp = node_next(lp)
296 set_attr_icflag_processed(lp); Np.nuc = lp
297 if lp.font == has_attr(lp, attr_curjfnt) then Np.id = id_jglyph
298 else Np.id = id_glyph end
299 lp = node_next(lp); check_next_ickern(); return
300 else -- id_disc, id_glue, id_kern
305 set_attr_icflag_processed(lp); Np.last = lp; Np.id = lpi; lp = node_next(lp)
308 -- extract informations from Np
309 -- We think that "Np is a Japanese character" if Np.met~=nil,
310 -- "Np is an alphabetic character" if Np.pre~=nil,
311 -- "Np is not a character" otherwise.
314 local function set_np_xspc_jachar(c,x)
315 Np.class = has_attr(x, attr_jchar_class)
317 local z = ltjf_font_metric_table[x.font]
319 Np.met = ltjf_metrics[z.jfm]
321 Np.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
322 Np.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
323 z = find_char_class('lineend', Np.met)
324 local y = Np.met.size_cache[Np.size].char_type[Np.class]
325 if y.kern and y.kern[z] then
330 y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
331 Np.xspc_before = (y>=2)
332 Np.xspc_after = (y%2==1)
333 Np.auto_kspc = (has_attr(x, attr_autospc)==1)
334 Np.auto_xspc = (has_attr(x, attr_autoxspc)==1)
338 local ligature_head = 1
339 local ligature_tail = 2
340 local function set_np_xspc_alchar(c,x, lig)
342 if lig == ligature_head then
343 while x.components and x.subtype and math.floor(x.subtype/2)%2==1 do
344 x = x.components; c = x.char
347 while x.components and x.subtype and math.floor(x.subtype/2)%2==1 do
348 x = node_tail(x.components); c = x.char
351 Np.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
352 Np.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
354 Np.pre = 0; Np.post = 0
357 local y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
358 Np.xspc_before = (y%2==1)
359 Np.xspc_after = (y>=2)
360 Np.auto_xspc = (has_attr(x, attr_autoxspc)==1)
364 local function extract_np()
366 if Np.id == id_jglyph then
367 set_np_xspc_jachar(x.char, x)
368 elseif Np.id == id_glyph then
369 set_np_xspc_alchar(x.char, x, ligature_head)
370 elseif Np.id == id_hlist then
371 find_first_char = true; first_char = nil; last_char = nil
372 if check_box(x.head, nil) then
374 if first_char.font == has_attr(first_char, attr_curjfnt) then
375 set_np_xspc_jachar(first_char.char,first_char)
377 set_np_xspc_alchar(first_char.char,first_char, ligature_head)
381 elseif Np.id == id_pbox then -- mikann
382 find_first_char = true; first_char = nil; last_char = nil
383 if check_box(Np.first, node_next(Np.last)) then
385 if first_char.font == has_attr(first_char, attr_curjfnt) then
386 set_np_xspc_jachar(first_char.char,first_char)
388 set_np_xspc_alchar(first_char.char,first_char, ligature_head)
392 elseif Np.id == id_disc then
393 find_first_char = true; first_char = nil; last_char = nil
394 if check_box(x.replace, nil) then
396 if first_char.font == has_attr(first_char, attr_curjfnt) then
397 set_np_xspc_jachar(first_char.char,first_char)
399 set_np_xspc_alchar(first_char.char,first_char, ligature_head)
403 elseif Np.id == id_math then
404 set_np_xspc_alchar(-1, x)
408 -- change the information for the next loop
409 -- (will be done if Np is an alphabetic character or a hlist)
410 local function after_hlist()
412 if last_char.font == has_attr(last_char, attr_curjfnt) then
413 set_np_xspc_jachar(last_char.char,last_char, ligature_after)
415 set_np_xspc_alchar(last_char.char,last_char, ligature_after)
418 Np.pre = nil; Np.met = nil
421 local function after_alchar()
423 set_np_xspc_alchar(x.char,x, ligature_after)
427 -------------------- 最下層の処理
429 local function lineend_fix(g)
430 if g and g.id==id_kern then
432 elseif Nq.lend~=0 then
434 g = node_new(id_kern); g.subtype = 1
435 g.kern = -Nq.lend; set_attr(g, attr_icflag, LINEEND)
436 elseif g.id==id_kern then
437 g.kern = g.kern - Nq.lend
439 g.spec.width = g.spec.width - Nq.lend
445 -- change penalties (or create a new penalty, if needed)
446 local function handle_penalty_normal(post, pre, g)
447 local a = (pre or 0) + (post or 0)
449 if (a~=0 and not(g and g.id==id_kern)) or Nq.lend~=0 then
450 local p = node_new(id_penalty)
451 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
453 head = node_insert_before(head, Np.first, p)
454 Bp[1] = p; set_attr(p, attr_icflag, KINSOKU)
456 else for i, v in pairs(Bp) do add_penalty(v,a) end
460 local function handle_penalty_always(post, pre, g)
461 local a = (pre or 0) + (post or 0)
463 if not (g and g.id==id_glue) or Nq.lend~=0 then
464 local p = node_new(id_penalty)
465 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
467 head = node_insert_before(head, Np.first, p)
468 Bp[1] = p; set_attr(p, attr_icflag, KINSOKU)
470 else for i, v in pairs(Bp) do add_penalty(v,a) end
474 local function handle_penalty_suppress(post, pre, g)
475 local a = (pre or 0) + (post or 0)
477 if g and g.id==id_glue then
478 local p = node_new(id_penalty)
479 p.penalty = 10000; head = node_insert_before(head, Np.first, p)
480 Bp[1] = p; set_attr(p, attr_icflag, KINSOKU)
482 else for i, v in pairs(Bp) do add_penalty(v,a) end
486 -- 和文文字間の JFM glue を node 化
487 local function new_jfm_glue(Nn, bc, ac)
488 -- bc, ac: char classes
490 local z = Nn.met.size_cache[Nn.size].char_type[bc]
491 if z.glue and z.glue[ac] then
492 local h = node_new(id_glue_spec)
493 h.width = z.glue[ac][1]
494 h.stretch = z.glue[ac][2]
495 h.shrink = z.glue[ac][3]
496 h.stretch_order=0; h.shrink_order=0
497 g = node_new(id_glue)
498 g.subtype = 0; g.spec = h
499 elseif z.kern and z.kern[ac] then
500 g = node_new(id_kern)
501 g.subtype = 1; g.kern = z.kern[ac]
503 if g then set_attr(g, attr_icflag, FROM_JFM) end
507 -- Nq.last (kern w) .... (glue/kern g) Np.first
508 local function real_insert(w, g)
510 local h = node_new(id_kern)
511 set_attr(h, attr_icflag, LINE_END)
512 h.kern = w; h.subtype = 1
513 head = node_insert_after(head, Nq.last, h)
516 head = node_insert_before(head, Np.first, g)
521 -------------------- 和文文字間空白量の決定
524 local function get_kanji_skip_from_jfm(Nn)
525 local i = Nn.met.size_cache[Nn.size].kanjiskip
527 return { i[1], i[2], i[3] }
531 local function get_kanjiskip()
532 local g = node_new(id_glue)
533 if Np.auto_kspc or Nq.auto_kspc then
534 if kanji_skip.width == max_dimen then
535 local gx = node_new(id_glue_spec);
536 gx.stretch_order = 0; gx.shrink_order = 0
537 local bk = get_kanji_skip_from_jfm(Nq)
539 if (Np.met==Nq.met) and (Nq.size==Np.size) and (Nq.var==Np.var) then
542 ak = get_kanji_skip_from_jfm(Np)
546 gx.width = round(diffmet_rule(bk[1], ak[1]))
547 gx.stretch = round(diffmet_rule(bk[2], ak[2]))
548 gx.shrink = -round(diffmet_rule(-bk[3], -ak[3]))
550 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
553 gx.width = ak[1]; gx.stretch = ak[2]; gx.shrink = ak[3]
554 else node_free(gx); gx = get_zero_glue() -- fallback
557 else g.spec=node_copy(kanji_skip); node_free(gx) end
559 g.spec = get_zero_glue(); node_free(gx)
561 set_attr(g, attr_icflag, KANJI_SKIP)
565 local function calc_ja_ja_aux(gb,ga)
569 if not ga then return gb end
570 local k = node.type(gb.id) .. node.type(ga.id)
571 if k == 'glueglue' then
573 gb.spec.width = round(diffmet_rule(gb.spec.width, ga.spec.width))
574 gb.spec.stretch = round(diffmet_rule(gb.spec.stretch,ga.spec.shrink))
575 gb.spec.shrink = -round(diffmet_rule(-gb.spec.shrink, -ga.spec.shrink))
578 elseif k == 'kernkern' then
580 gb.kern = round(diffmet_rule(gb.kern, ga.kern))
583 elseif k == 'kernglue' then
584 -- gb: kern, ga: glue
585 ga.spec.width = round(diffmet_rule(gb.kern,ga.spec.width))
586 ga.spec.stretch = round(diffmet_rule(ga.spec.stretch, 0))
587 ga.spec.shrink = -round(diffmet_rule(-ga.spec.shrink, 0))
591 -- gb: glue, ga: kern
592 gb.spec.width = round(diffmet_rule(ga.kern, gb.spec.width))
593 gb.spec.stretch = round(diffmet_rule(gb.spec.stretch, 0))
594 gb.spec.shrink = -round(diffmet_rule(-gb.spec.shrink, 0))
601 local function calc_ja_ja_glue()
602 if ihb_flag then return nil
603 elseif (Nq.size==Np.size) and (Nq.met==Np.met) and (Nq.var==Np.var) then
604 return new_jfm_glue(Nq, Nq.class, Np.class)
606 local g = new_jfm_glue(Nq, Nq.class,
607 find_char_class('diffmet',Nq.met))
608 local h = new_jfm_glue(Np, find_char_class('diffmet',Np.met),
610 return calc_ja_ja_aux(g,h)
614 -------------------- 和欧文間空白量の決定
617 local function get_xkanji_skip_from_jfm(Nn)
618 local i = Nn.met.size_cache[Nn.size].xkanjiskip
620 return { i[1], i[2], i[3] }
624 local function get_xkanjiskip(Nn)
625 local g = node_new(id_glue)
626 if Nq.xspc_after and Np.xspc_before and (Nq.auto_xspc or Np.auto_xspc) then
627 if xkanji_skip.width == max_dimen then
628 local gx = node_new(id_glue_spec);
629 gx.stretch_order = 0; gx.shrink_order = 0
630 local bk = get_xkanji_skip_from_jfm(Nn)
632 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
633 else node_free(gx); gx = get_zero_glue() -- fallback
636 else g.spec=node_copy(xkanji_skip) end
638 g.spec = get_zero_glue()
640 set_attr(g, attr_icflag, XKANJI_SKIP)
645 -------------------- 隣接した「塊」間の処理
647 local function get_OA_skip()
650 if Nq.id == id_math then c = -1 else c = 'jcharbdd' end
651 return new_jfm_glue(Np, find_char_class(c,Np.met), Np.class)
655 local function get_OB_skip()
658 if Np.id == id_math then c = -1 else c = 'jcharbdd' end
659 return new_jfm_glue(Nq, Nq.class, find_char_class(c,Nq.met))
664 -- (anything) .. jachar
665 local function handle_np_jachar()
667 if Nq.id==id_jglyph or (Nq.id==id_pbox and Nq.met) then
668 g = calc_ja_ja_glue() or get_kanjiskip() -- M->K
670 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
671 elseif Nq.met then -- Nq.id==id_hlist
672 g = get_OA_skip() or get_kanjiskip() -- O_A->K
673 handle_penalty_normal(0, Np.pre, g); real_insert(0, g)
675 g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
676 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
678 g = get_OA_skip() -- O_A
679 if Nq.id==id_glue then handle_penalty_normal(0, Np.pre, g)
680 elseif Nq.id==id_kern then handle_penalty_suppress(0, Np.pre, g)
681 else handle_penalty_always(0, Np.pre, g)
685 -- \jcharwidowpenalty 挿入予定箇所更新
686 if mode and ltjs_get_penalty_table('kcat', Np.char, 0, box_stack_level)%2~=1 then
687 widow_Np.first = Np.first;
688 local Bpr = widow_Bp; widow_Bp = Bp; Bp = Bpr
692 -- jachar .. (anything)
693 local function handle_nq_jachar()
696 g = get_OB_skip() or get_xkanjiskip(Nq) -- O_B->X
698 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
700 g = get_OB_skip(); g = lineend_fix(g) -- O_B
701 if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
702 elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
703 else handle_penalty_always(Nq.post, 0, g)
705 real_insert(Nq.lend, g)
709 -- (anything) .. (和文文字で終わる hlist)
710 local function handle_np_ja_hlist()
712 if Nq.id==id_jglyph or (Nq.id==id_pbox and Nq.met) then
713 g = get_OB_skip() or get_kanjiskip() -- O_B->K
715 handle_penalty_normal(Nq.post, 0, g); real_insert(Nq.lend, g)
716 elseif Nq.met then -- Nq.id==id_hlist
717 g = get_kanjiskip() -- K
718 handle_penalty_suppress(0, 0, g); real_insert(0, g)
720 g = get_xkanjiskip(Np) -- X
721 handle_penalty_suppress(0, 0, g); real_insert(0, g)
725 -- (和文文字で終わる hlist) .. (anything)
726 local function handle_nq_ja_hlist()
729 g = get_xkanjiskip(Nq) -- X
730 handle_penalty_suppress(0, 0, g); real_insert(0, g)
734 -------------------- 開始・終了時の処理
737 local function handle_list_tail()
740 -- the current list is to be line-breaked:
741 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
743 g = node_new(id_kern); g.subtype = 0; g.kern = Np.lend
744 set_attr(g, attr_icflag, BOXBDD)
745 node_insert_after(head, Np.last, g)
748 -- Insert \jcharwidowpenalty
749 Bp = widow_Bp; Np = widow_Np; Nq.lend = 0
751 handle_penalty_normal(0,
752 ltjs_get_penalty_table('jwp', 0, 0, box_stack_level))
755 -- the current list is the contents of a hbox
756 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
757 local g = new_jfm_glue(Np, Np.class, find_char_class('boxbdd',Np.met))
759 set_attr(g, attr_icflag, BOXBDD)
760 head = node_insert_after(head, Np.last, g)
763 head = node_remove(head, last); node_free(last);-- remove the sentinel
765 node_free(kanji_skip); node_free(xkanji_skip)
769 local function handle_list_head()
770 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
774 g = new_jfm_glue(Np, find_char_class('parbdd',Np.met), Np.class)
776 g = new_jfm_glue(Np, find_char_class('boxbdd',Np.met), Np.class)
779 set_attr(g, attr_icflag, BOXBDD)
780 if g.id==id_glue and #Bp==0 then
781 local h = node_new(id_penalty)
782 h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
784 head = node_insert_before(head, Np.first, g)
791 local function init_var()
792 lp = head; Bp = {}; widow_Bp = {}; widow_Np = {first = nil}
793 box_stack_level = ltjp.box_stack_level
794 kanji_skip=skip_table_to_spec('kanjiskip')
795 xkanji_skip=skip_table_to_spec('xkanjiskip')
797 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
798 first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil,
799 post=nil, pre=nil, var=nil, xspc_after=nil, xspc_before=nil,
802 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
803 first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil,
804 post=nil, pre=nil, var=nil, xspc_after=nil, xspc_before=nil,
807 -- the current list is to be line-breaked:
808 -- hbox from \parindent is skipped.
809 while lp and ((lp.id==id_whatsit and lp.subtype~=sid_user)
810 or ((lp.id==id_hlist) and (lp.subtype==3))) do
814 -- the current list is the contents of a hbox:
816 last=node.tail(head); local g = node_new(id_kern)
817 node_insert_after(head, last, g); last = g
821 -------------------- 外部から呼ばれる関数
824 function main(ahead, amode)
825 if not ahead then return ahead end
826 head = ahead; mode = amode; init_var(); calc_np()
828 extract_np(); handle_list_head()
829 if Np.id==id_glyph then after_alchar()
830 elseif Np.id==id_hlist or Np.id==id_pbox or Np.id==id_disc then after_hlist()
833 if not mode then head = node_remove(head, last); node_free(last) end
840 if Np.id == id_jglyph then
843 if Np.id==id_hlist then handle_np_ja_hlist()
844 else handle_np_jachar() end
846 if Nq.id==id_hlist then handle_nq_ja_hlist()
847 else handle_nq_jachar() end
850 if Np.id==id_glyph then after_alchar()
851 elseif Np.id==id_hlist or Np.id==id_pbox or Np.id==id_disc then after_hlist()
856 -- adjust attr_icflag
857 tex.attribute[attr_icflag] = -(0x7FFFFFFF)
862 local inhibitglue_node=node_new(id_whatsit, sid_user)
863 inhibitglue_node.user_id=30111; inhibitglue_node.type=100; inhibitglue_node.value=1
865 function create_inhibitglue_node()
866 node.write(node.copy(inhibitglue_node))