OSDN Git Service

Another optimization in jfmglue.lua.
[luatex-ja/luatexja.git] / src / luatexja / jfmglue.lua
1 --
2 -- luatexja/jfmglue.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.jfmglue',
6   date = '2011/06/27',
7   version = '0.2',
8   description = 'Insertion process of JFM glues and kanjiskip',
9 })
10 module('luatexja.jfmglue', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12
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
17
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
32 local id_glyph = node.id('glyph')
33 local id_hlist = node.id('hlist')
34 local id_vlist = node.id('vlist')
35 local id_rule = node.id('rule')
36 local id_ins = node.id('ins')
37 local id_mark = node.id('mark')
38 local id_adjust = node.id('adjust')
39 local id_disc = node.id('disc')
40 local id_whatsit = node.id('whatsit')
41 local id_math = node.id('math')
42 local id_glue = node.id('glue')
43 local id_kern = node.id('kern')
44 local id_penalty = node.id('penalty')
45
46 local id_glue_spec = node.id('glue_spec')
47 local id_jglyph = node.id('glyph') + 256
48 local id_box_like = node.id('hlist') + 256
49 local id_pbox = node.id('hlist') + 512
50 local sid_user = node.subtype('user_defined')
51
52 local ITALIC = 1
53 local PACKED = 2
54 local KINSOKU = 3
55 local FROM_JFM = 4
56 local LINE_END = 5
57 local KANJI_SKIP = 6
58 local XKANJI_SKIP = 7
59 local PROCESSED = 8
60 local IC_PROCESSED = 9
61 local BOXBDD = 15
62
63 local kanji_skip
64 local xkanji_skip
65
66 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
67 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
68 local attr_icflag = luatexbase.attributes['ltj@icflag']
69 local attr_autospc = luatexbase.attributes['ltj@autospc']
70 local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
71 local max_dimen = 1073741823
72
73
74
75 -------------------- Helper functions
76
77 local function find_char_class(c,m)
78    return m.chars[c] or 0
79 end
80
81 local function get_zero_glue()
82    local g = node_new(id_glue_spec)
83    g.width = 0; g.stretch_order = 0; g.stretch = 0
84    g.shrink_order = 0; g.shrink = 0
85    return g
86 end
87
88 local function skip_table_to_spec(n)
89    local g = node_new(id_glue_spec)
90    local st = ltjs.get_skip_table(n, ltjp.box_stack_level)
91    g.width = st.width; g.stretch = st.stretch; g.shrink = st.shrink
92    g.stretch_order = st.stretch_order; g.shrink_order = st.shrink_order
93    return g
94 end
95
96 -- penalty 値の計算
97 local function add_penalty(p,e)
98    if p.penalty>=10000 then
99       if e<=-10000 then p.penalty = 0 end
100    elseif p.penalty<=-10000 then
101       if e>=10000 then p.penalty = 0 end
102    else
103       p.penalty = p.penalty + e
104       if p.penalty>=10000 then p.penalty = 10000
105       elseif p.penalty<=-10000 then p.penalty = -10000 end
106    end
107    return
108 end
109
110 -- 「異なる JFM」の間の調整方法
111 diffmet_rule = math.two_average
112 function math.two_add(a,b) return a+b end
113 function math.two_average(a,b) return (a+b)/2 end
114
115 -------------------- idea
116 -- 2 node の間に glue/kern/penalty を挿入する.
117 -- 基本方針: char node q と char node p の間
118
119 --  Np: 「p を核とする塊」
120 --   first: 最初の node,nuc: p,last: 最後の node
121 --   id: 核 node の種類
122 --  Nq: 「q を核とする塊」
123 --   実際の glue は Np.last, Nq.first の間に挿入される
124 --  Bp: Np.last, Nq.first の間の penalty node 達の配列
125
126 -- 核の定義:
127 --  node x が non-char node のときは,x のみ
128 --  x が char_node のときは,
129 --  - x が \accent の第二引数だったとき
130 --    [kern2 kern y kern2] x の 3 node が核に加わる
131 --  - x の直後に \/ 由来 kern があったとき
132 --    その \/ 由来の kern が核に加わる
133 -- p, q の走査で無視するもの:
134 --  ins, mark, adjust, whatsit, penalty
135 --
136 -- Nq.last .. + .. Bp.first .... Bp[last] .... * .. Np.first
137 -- +: kern from LINEEND はここに入る
138 -- *: jfm glue はここに入る
139
140 local head -- the head of current list
141 local last -- the last node of current list
142 local lp   -- 外側での list 走査時のカーソル
143
144 local Np, Nq, Bp, Bpn
145 local widow_Bp, widow_Bpn, widow_Np -- \jcharwidowpenalty 挿入位置管理用
146
147 local ihb_flag -- JFM グルー挿入抑止用 flag
148                -- on: \inhibitglue 指定時,hlist の周囲
149
150 -------------------- hlist 内の文字の検索
151
152 local first_char, last_char, find_first_char
153
154 local function check_box(box_ptr, box_end)
155    local p = box_ptr; local found_visible_node = false
156    if not p then 
157       find_first_char = false; first_char = nil; last_char = nil
158       return true
159    end
160    while p and p~=box_end do
161       local pid = p.id
162       if pid==id_kern then
163          if p.subtype==2 then
164             p = node_next(node_next(node_next(p))); pid = p.id
165          elseif has_attr(p, attr_icflag)==IC_PROCESSED then
166             p = node_next(p); pid = p.id
167          end
168       end
169       if pid==id_glyph then
170          repeat 
171             if find_first_char then 
172                first_char = p; find_first_char = false
173             end
174             last_char = p; found_visible_node = true; p=node_next(p)
175             if (not p) or p==box_end then return found_visible_node end
176          until p.id~=id_glyph
177       end
178       if pid==id_hlist then
179          if has_attr(p, attr_icflag)==PACKED then
180             for q in node.traverse_id(id_glyph, p.head) do
181                if find_first_char then
182                   first_char = q; find_first_char = false
183                end
184                last_char = q; found_visible_node = true; break
185             end
186          else
187             if p.shift==0 then
188                if check_box(p.head, nil) then found_visible_node = true end
189             else if find_first_char then 
190                   find_first_char = false
191                else 
192                   last_char = nil
193                end
194             end
195          end
196       elseif not (pid==id_ins   or pid==id_mark
197                   or pid==id_adjust or pid==id_whatsit
198                   or pid==id_penalty) then
199          found_visible_node = true
200          if find_first_char then 
201             find_first_char = false
202          else 
203             last_char = nil
204          end
205       end
206       p = node_next(p)
207    end
208    return found_visible_node
209 end 
210
211 -------------------- Np の計算と情報取得
212 -- calc next Np
213 local function set_attr_icflag_processed(p)
214    local a = has_attr(p, attr_icflag) or 0
215    if a<=1 then set_attr(p, attr_icflag, PROCESSED) end
216 end
217
218 local function check_next_ickern()
219    if lp.id == id_kern and has_attr(lp, attr_icflag)==ITALIC then
220       set_attr(lp, attr_icflag, IC_PROCESSED); Np.last = lp; lp = node_next(lp)
221    else Np.last = Np.nuc end
222 end
223
224 local function calc_np_pbox()
225    Np.first = lp; Np.id = id_pbox
226    lpa = KINSOKU -- dummy
227    while lp~=last and lpa>=PACKED and lpa~=BOXBDD do
228       Np.nuc = lp; lp = node_next(lp); lpa = has_attr(lp, attr_icflag) or 0
229    end
230    check_next_ickern()
231 end
232
233 local function calc_np()
234    -- We assume lp = node_next(Np.last)
235    local pBp = Bp; local lpi, lpa
236    Nq = Np; ihb_flag = false 
237    for k in pairs(Np) do Np[k]=nil end
238    for k in pairs(Bp) do Bp[k]=nil end; Bpn = 0
239    while true do
240       lpi = lp.id; lpa = has_attr(lp, attr_icflag) or 0
241       if lp==last then Np = nil; return
242       elseif lpa>=PACKED then
243          if lpa == BOXBDD then
244             local lq = node_next(lp)
245             head = node_remove(head, lp); lp = lq
246          else calc_np_pbox(); return end -- id_pbox
247       elseif lpi == id_ins or lpi == id_mark or lpi == id_adjust then
248          set_attr_icflag_processed(lp); lp = node_next(lp)
249       elseif lpi == id_penalty then
250          table.insert(Bp, lp); Bpn = Bpn + 1
251          set_attr_icflag_processed(lp); lp = node_next(lp)
252       elseif lpi == id_whatsit then
253          if lp.subtype==sid_user and lp.user_id==30111 then
254             local lq = node_next(lp)
255             head = node_remove(head, lp); lp = lq; ihb_flag = true
256          else
257             set_attr_icflag_processed(lp); lp = node_next(lp)
258          end
259       else -- a `cluster' is found
260          Np.first = lp
261          if lpi == id_glyph then -- id_[j]glyph
262             if lp.font == has_attr(lp, attr_curjfnt) then Np.id = id_jglyph 
263             else Np.id = id_glyph end
264             Np.nuc = lp; set_attr_icflag_processed(lp)
265             lp = node_next(lp); check_next_ickern(); return
266          elseif lpi == id_hlist then -- hlist
267             Np.last = lp; Np.nuc = lp; set_attr_icflag_processed(lp)
268             if lp.shift~=0 then Np.id = id_box_like
269             else Np.id = lpi end
270             lp = node_next(lp); return
271          elseif lpi == id_vlist or lpi == id_rule then -- id_box_like
272             Np.nuc = lp; Np.last = lp; Np.id = id_box_like; break
273          elseif lpi == id_math then -- id_math
274             Np.nuc = lp; lp  = node_next(lp) 
275             while lp.id~=id_math do 
276                set_attr_icflag_processed(lp); lp  = node_next(lp) 
277             end; break
278          elseif lpi == id_kern and lp.subtype==2 then -- id_kern
279             set_attr_icflag_processed(lp); lp = node_next(lp)
280             set_attr_icflag_processed(lp); lp = node_next(lp)
281             set_attr_icflag_processed(lp); lp = node_next(lp)
282             set_attr_icflag_processed(lp); Np.nuc = lp
283             if lp.font == has_attr(lp, attr_curjfnt) then Np.id = id_jglyph 
284             else Np.id = id_glyph end
285             lp = node_next(lp); check_next_ickern(); return
286          else -- id_disc, id_glue, id_kern
287             Np.nuc = lp; break
288          end
289       end
290    end
291    set_attr_icflag_processed(lp); Np.last = lp; Np.id = lpi; lp = node_next(lp)
292 end
293
294 -- extract informations from Np
295 -- We think that "Np is a Japanese character" if Np.met~=nil,
296 --            "Np is an alphabetic character" if Np.pre~=nil,
297 --            "Np is not a character" otherwise.
298
299 -- 和文文字のデータを取得
300 local function set_np_xspc_jachar(c,x)
301    Np.class = has_attr(x, attr_jchar_class)
302    Np.char = c
303    local z = ltjf.font_metric_table[x.font]
304    Np.size= z.size
305    Np.met = ltjf.metrics[z.jfm]
306    Np.var = z.var
307    Np.pre = ltjs.get_penalty_table('pre', c, 0, ltjp.box_stack_level)
308    Np.post = ltjs.get_penalty_table('post', c, 0, ltjp.box_stack_level)
309    z = find_char_class('lineend', Np.met)
310    local y = Np.met.size_cache[Np.size].char_type[Np.class]
311    if y.kern and y.kern[z] then 
312       Np.lend = y.kern[z]
313    else 
314       Np.lend = 0 
315    end
316    y = ltjs.get_penalty_table('xsp', c, 3, ltjp.box_stack_level)
317    Np.xspc_before = (y>=2)
318    Np.xspc_after  = (y%2==1)
319    Np.auto_kspc = (has_attr(x, attr_autospc)==1)
320    Np.auto_xspc = (has_attr(x, attr_autoxspc)==1)
321 end
322
323 -- 欧文文字のデータを取得
324 local ligature_head = 1
325 local ligature_tail = 2
326 local function set_np_xspc_alchar(c,x, lig)
327    if c~=-1 then
328       if lig == ligature_head then
329          while x.components and x.subtype and math.floor(x.subtype/2)%2==1 do
330             x = x.components; c = x.char
331          end
332       else
333          while x.components and x.subtype and math.floor(x.subtype/2)%2==1 do
334             x = node_tail(x.components); c = x.char
335          end
336       end
337       Np.pre = ltjs.get_penalty_table('pre', c, 0, ltjp.box_stack_level)
338       Np.post = ltjs.get_penalty_table('post', c, 0, ltjp.box_stack_level)
339    else
340       Np.pre = 0; Np.post = 0
341    end
342    Np.met = nil
343    local y = ltjs.get_penalty_table('xsp', c, 3, ltjp.box_stack_level)
344    Np.xspc_before = (y%2==1)
345    Np.xspc_after  = (y>=2)
346    Np.auto_xspc = (has_attr(x, attr_autoxspc)==1)
347 end
348
349 -- Np の情報取得メインルーチン
350 local function extract_np()
351    local x = Np.nuc
352    if Np.id ==  id_jglyph then
353       set_np_xspc_jachar(x.char, x)
354    elseif Np.id == id_glyph then
355       set_np_xspc_alchar(x.char, x, ligature_head)
356    elseif Np.id == id_hlist then
357       find_first_char = true; first_char = nil; last_char = nil
358       if check_box(x.head, nil) then
359          if first_char then
360             if first_char.font == has_attr(first_char, attr_curjfnt) then 
361                set_np_xspc_jachar(first_char.char,first_char)
362             else
363                set_np_xspc_alchar(first_char.char,first_char, ligature_head)
364             end
365          end
366       end
367    elseif Np.id == id_pbox then --  mikann 
368       find_first_char = true; first_char = nil; last_char = nil
369       if check_box(Np.first, node_next(Np.last)) then
370          if first_char then
371             if first_char.font == has_attr(first_char, attr_curjfnt) then 
372                set_np_xspc_jachar(first_char.char,first_char)
373             else
374                set_np_xspc_alchar(first_char.char,first_char, ligature_head)
375             end
376          end
377       end
378    elseif Np.id == id_disc then 
379       find_first_char = true; first_char = nil; last_char = nil
380       if check_box(x.replace, nil) then
381          if first_char then
382             if first_char.font == has_attr(first_char, attr_curjfnt) then 
383                set_np_xspc_jachar(first_char.char,first_char)
384             else
385                set_np_xspc_alchar(first_char.char,first_char, ligature_head)
386             end
387          end
388       end
389    elseif Np.id == id_math then
390       set_np_xspc_alchar(-1, x)
391    end
392 end
393
394 -- change the information for the next loop
395 -- (will be done if Np is an alphabetic character or a hlist)
396 local function after_hlist()
397    if last_char then
398       if last_char.font == has_attr(last_char, attr_curjfnt) then 
399          set_np_xspc_jachar(last_char.char,last_char, ligature_after)
400       else
401          set_np_xspc_alchar(last_char.char,last_char, ligature_after)
402       end
403    else
404       Np.pre = nil; Np.met = nil
405    end
406 end
407 local function after_alchar()
408    local x = Np.nuc
409    set_np_xspc_alchar(x.char,x, ligature_after)
410 end
411
412
413 -------------------- 最下層の処理
414
415 local function lineend_fix(g)
416    if g and g.id==id_kern then 
417       Nq.lend = 0
418    elseif Nq.lend and Nq.lend~=0 then
419       if not g then
420          g = node_new(id_kern); g.subtype = 1
421          g.kern = -Nq.lend; set_attr(g, attr_icflag, LINEEND)
422       elseif g.id==id_kern then
423          g.kern = g.kern - Nq.lend
424       else
425          g.spec.width = g.spec.width - Nq.lend
426       end
427    end
428    return g
429 end
430
431 -- change penalties (or create a new penalty, if needed)
432 local function handle_penalty_normal(post, pre, g)
433    local a = (pre or 0) + (post or 0)
434    if Bpn == 0 then
435       if (a~=0 and not(g and g.id==id_kern)) or (Nq.lend and Nq.lend~=0) then
436          local p = node_new(id_penalty)
437          if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
438          p.penalty = a
439          head = node_insert_before(head, Np.first, p)
440          Bp[1] = p; Bpn = 1; set_attr(p, attr_icflag, KINSOKU)
441       end
442    else for i, v in ipairs(Bp) do add_penalty(v,a) end
443    end
444 end
445
446 local function handle_penalty_always(post, pre, g)
447    local a = (pre or 0) + (post or 0)
448    if Bpn == 0 then
449       if not (g and g.id==id_glue) or (Nq.lend and 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
452          p.penalty = a
453          head = node_insert_before(head, Np.first, p)
454          Bp[1] = p; Bpn = 1; set_attr(p, attr_icflag, KINSOKU)
455       end
456    else for i, v in ipairs(Bp) do add_penalty(v,a) end
457    end
458 end
459
460 local function handle_penalty_suppress(post, pre, g)
461    local a = (pre or 0) + (post or 0)
462    if Bpn == 0 then
463       if g and g.id==id_glue then
464          local p = node_new(id_penalty)
465          p.penalty = 10000; head = node_insert_before(head, Np.first, p)
466          Bp[1] = p; Bpn = 1; set_attr(p, attr_icflag, KINSOKU)
467       end
468    else for i, v in ipairs(Bp) do add_penalty(v,a) end
469    end
470 end
471
472 -- 和文文字間の JFM glue を node 化
473 local function new_jfm_glue(Nn, bc, ac)
474 -- bc, ac: char classes
475    local g = nil
476    local z = Nn.met.size_cache[Nn.size].char_type[bc]
477    if z.glue and z.glue[ac] then
478       local h = node_new(id_glue_spec)
479       h.width   = z.glue[ac][1]
480       h.stretch = z.glue[ac][2]
481       h.shrink  = z.glue[ac][3]
482       h.stretch_order=0; h.shrink_order=0
483       g = node_new(id_glue)
484       g.subtype = 0; g.spec = h
485    elseif z.kern and z.kern[ac] then
486       g = node_new(id_kern)
487       g.subtype = 1; g.kern = z.kern[ac]
488    end
489    if g then set_attr(g, attr_icflag, FROM_JFM) end
490    return g
491 end
492
493 -- Nq.last (kern w) .... (glue/kern g) Np.first
494 local function real_insert(w, g)
495    if w~=0 then
496       local h = node_new(id_kern)
497       set_attr(h, attr_icflag, LINE_END)
498       h.kern = Nq.lend; h.subtype = 1
499       head = node_insert_after(head, Nq.last, h)
500    end
501    if g then
502       head = node_insert_before(head, Np.first, g)
503       Np.first = g
504    end
505 end
506
507 -------------------- 和文文字間空白量の決定
508
509 -- get kanjiskip
510 local function get_kanji_skip_from_jfm(Nn)
511    local i = Nn.met.size_cache[Nn.size].kanjiskip
512    if i then
513       return { i[1], i[2], i[3] }
514    else return nil
515    end
516 end
517 local function get_kanjiskip()
518    local g = node_new(id_glue)
519    if Np.auto_kspc or Nq.auto_kspc then
520       if kanji_skip.width == max_dimen then
521          local gx = node_new(id_glue_spec);
522          gx.stretch_order = 0; gx.shrink_order = 0
523          local bk = get_kanji_skip_from_jfm(Nq)
524          local ak
525          if (Np.met==Nq.met) and (Nq.size==Np.size) and (Nq.var==Np.var) then
526             ak = nil
527          else
528             ak = get_kanji_skip_from_jfm(Np)
529          end
530          if bk then
531             if ak then
532                gx.width = round(diffmet_rule(bk[1], ak[1]))
533                gx.stretch = round(diffmet_rule(bk[2], ak[2]))
534                gx.shrink = -round(diffmet_rule(-bk[3], -ak[3]))
535             else
536                gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
537             end
538          elseif ak then
539             gx.width = ak[1]; gx.stretch = ak[2]; gx.shrink = ak[3]
540          else gx = get_zero_glue() -- fallback
541          end
542          g.spec = gx
543       else g.spec=node_copy(kanji_skip) end
544    else
545       local gx = get_zero_glue()
546       g.spec = gx
547    end
548    set_attr(g, attr_icflag, KANJI_SKIP)
549    return g
550 end
551
552 local function calc_ja_ja_aux(gb,ga)
553    if not gb then 
554       return ga
555    else
556       if not ga then return gb end
557       local k = node.type(gb.id) .. node.type(ga.id)
558       if k == 'glueglue' then 
559          -- 両方とも glue.
560          gb.spec.width   = round(diffmet_rule(gb.spec.width, ga.spec.width))
561          gb.spec.stretch = round(diffmet_rule(gb.spec.stretch,ga.spec.shrink))
562          gb.spec.shrink  = -round(diffmet_rule(-gb.spec.shrink, -ga.spec.shrink))
563          node_free(ga)
564          return gb
565       elseif k == 'kernkern' then
566          -- 両方とも kern.
567          gb.kern = round(diffmet_rule(gb.kern, ga.kern))
568          node_free(ga)
569          return gb
570       elseif k == 'kernglue' then 
571          -- gb: kern, ga: glue
572          ga.spec.width   = round(diffmet_rule(gb.kern,ga.spec.width))
573          ga.spec.stretch = round(diffmet_rule(ga.spec.stretch, 0))
574          ga.spec.shrink  = -round(diffmet_rule(-ga.spec.shrink, 0))
575          node_free(gb)
576          return ga
577       else
578          -- gb: glue, ga: kern
579          gb.spec.width   = round(diffmet_rule(ga.kern, gb.spec.width))
580          gb.spec.stretch = round(diffmet_rule(gb.spec.stretch, 0))
581          gb.spec.shrink  = -round(diffmet_rule(-gb.spec.shrink, 0))
582          node_free(ga)
583          return gb
584       end
585    end
586 end
587
588 local function calc_ja_ja_glue()
589    if  ihb_flag then return nil
590    elseif (Nq.size==Np.size) and (Nq.met==Np.met) and (Nq.var==Np.var) then
591       return new_jfm_glue(Nq, Nq.class, Np.class)
592    else
593       local g = new_jfm_glue(Nq, Nq.class,
594                              find_char_class('diffmet',Nq.met))
595       local h = new_jfm_glue(Np, find_char_class('diffmet',Np.met),
596                              Np.class)
597       return calc_ja_ja_aux(g,h)
598    end
599 end
600
601 -------------------- 和欧文間空白量の決定
602
603 -- get xkanjiskip
604 local function get_xkanji_skip_from_jfm(Nn)
605    local i = Nn.met.size_cache[Nn.size].xkanjiskip
606    if i then
607       return { i[1], i[2], i[3] }
608    else return nil
609    end
610 end
611 local function get_xkanjiskip(Nn)
612    local g = node_new(id_glue)
613    if Nq.xspc_after and Np.xspc_before and (Nq.auto_xspc or Np.auto_xspc) then
614       if xkanji_skip.width == max_dimen then
615          local gx = node_new(id_glue_spec);
616          gx.stretch_order = 0; gx.shrink_order = 0
617          local bk = get_xkanji_skip_from_jfm(Nn)
618          if bk then
619             gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
620          else gx = get_zero_glue() -- fallback
621          end
622          g.spec = gx
623       else g.spec=node_copy(xkanji_skip) end
624    else
625       local gx = get_zero_glue()
626       g.spec = gx
627    end
628    set_attr(g, attr_icflag, XKANJI_SKIP)
629    return g
630 end
631
632
633 -------------------- 隣接した「塊」間の処理
634
635 local function get_OA_skip()
636    if not ihb_flag then
637       return new_jfm_glue(Np, find_char_class('jcharbdd',Np.met), Np.class)
638    else return nil
639    end
640 end
641 local function get_OB_skip()
642    if not ihb_flag then
643       return new_jfm_glue(Nq, Nq.class, find_char_class('jcharbdd',Nq.met))
644    else return nil
645    end
646 end
647
648 -- (anything) .. jachar
649 local function handle_np_jachar()
650    local g = nil
651    if Nq.id==id_jglyph or (Nq.id==id_pbox and Nq.met) then 
652       g = calc_ja_ja_glue() or get_kanjiskip() -- M->K
653       g = lineend_fix(g)
654       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
655    elseif Nq.met then  -- Nq.id==id_hlist
656       g = get_OA_skip() or get_kanjiskip() -- O_A->K
657       handle_penalty_normal(0, Np.pre, g); real_insert(0, g)
658    elseif Nq.pre then 
659       g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
660       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
661    else
662       g = get_OA_skip() -- O_A
663       if Nq.id==id_glue then handle_penalty_normal(0, Np.pre, g)
664       elseif Nq.id==id_kern then handle_penalty_suppress(0, Np.pre, g)
665       else handle_penalty_always(0, Np.pre, g)
666       end
667       real_insert(0, g)
668    end
669    -- \jcharwidowpenalty 挿入予定箇所更新
670    if ltjs.get_penalty_table('kcat', Np.char, 0, ltjp.box_stack_level)%2~=1 then
671       widow_Np = Np; widow_Bp = Bp; widow_Bpn = Bpn
672    end
673 end
674
675 -- jachar .. (anything)
676 local function handle_nq_jachar()
677    local g = nil
678    if Np.pre then 
679       g = get_OB_skip() or get_xkanjiskip(Nq) -- O_B->X
680       g = lineend_fix(g)
681       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
682    else
683       g = get_OB_skip(); g = lineend_fix(g) -- O_B
684       if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
685       elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
686       else handle_penalty_always(Nq.post, 0, g)
687       end
688       real_insert(Nq.lend, g)
689    end
690 end
691
692 -- (anything) .. (和文文字で終わる hlist)
693 local function handle_np_ja_hlist()
694    local g = nil
695    if Nq.id==id_jglyph or (Nq.id==id_pbox and Nq.met) then 
696       g = get_OB_skip() or get_kanjiskip() -- O_B->K
697       g = lineend_fix(g)
698       handle_penalty_normal(Nq.post, 0, g); real_insert(Nq.lend, g)
699    elseif Nq.met then  -- Nq.id==id_hlist
700       g = get_kanjiskip() -- K
701       handle_penalty_suppress(0, 0, g); real_insert(0, g)
702    elseif Nq.pre then 
703       g = get_xkanjiskip(Np) -- X
704       handle_penalty_suppress(0, 0, g); real_insert(0, g)
705    end
706 end
707
708 -- (和文文字で終わる hlist) .. (anything)
709 local function handle_nq_ja_hlist()
710    local g = nil
711    if Np.pre then 
712       g = get_xkanjiskip(Nq) -- X
713       handle_penalty_suppress(0, 0, g); real_insert(0, g)
714    end
715 end
716
717 -------------------- 開始・終了時の処理
718
719 -- リスト末尾の処理
720 local function handle_list_tail()
721    Np = Nq
722    if mode then
723       -- the current list is to be line-breaked:
724       if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then 
725          if Np.lend~=0 then
726             g = node_new(id_kern); g.subtype = 0; g.kern = Np.lend
727             set_attr(g, attr_icflag, BOXBDD)
728             node_insert_after(head, Np.last, g)
729          end
730       end
731       -- Insert \jcharwidowpenalty
732       Bp = widow_Bp; Np = widow_Np; Bpn = widow_Bpn
733       if Np then
734          handle_penalty_normal(0,
735                                ltjs.get_penalty_table('jwp', 0, 0, ltjp.box_stack_level))
736       end
737    else
738       -- the current list is the contents of a hbox
739       if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then 
740          local g = new_jfm_glue(Np, Np.class, find_char_class('boxbdd',Np.met))
741          if g then
742             set_attr(g, attr_icflag, BOXBDD)
743             head = node_insert_after(head, Np.last, g)
744          end
745       end
746       head = node_remove(head, last) -- remove the sentinel
747    end
748 end
749
750 -- リスト先頭の処理
751 local function handle_list_head()
752    if Np.id ==  id_jglyph or (Np.id==id_pbox and Np.met) then 
753       local g = new_jfm_glue(Np, find_char_class('boxbdd',Np.met), Np.class)
754       if g then
755          set_attr(g, attr_icflag, BOXBDD)
756          if g.id==id_glue and Bpn==0 then
757             local h = node_new(id_penalty)
758             h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
759          end
760          head = node_insert_before(head, Np.first, g)
761       end
762    end
763 end
764
765 -- initialize
766 local function init_var()
767    lp = head; widow_Bp = nil; widow_Np = nil; Np = {}; Bp = {}
768    kanji_skip=skip_table_to_spec('kanjiskip')
769    xkanji_skip=skip_table_to_spec('xkanjiskip')
770    if mode then 
771       -- the current list is to be line-breaked:
772       -- hbox from \parindent is skipped.
773       while lp and (lp.id==id_whatsit or ((lp.id==id_hlist) and (lp.subtype==3))) do
774          lp=node_next(lp) end
775       last=node.tail(head)
776    else 
777       -- the current list is the contents of a hbox:
778       -- insert a sentinel
779       last=node.tail(head); local g = node_new(id_kern)
780       node_insert_after(head, last, g); last = g
781    end
782 end
783
784 -------------------- 外部から呼ばれる関数
785
786 -- main interface
787 function main(ahead, amode)
788    if not ahead then return ahead end
789    head = ahead; mode = amode; init_var(); calc_np()
790    if Np then 
791       extract_np(); handle_list_head()
792       if Np.id==id_glyph then after_alchar()
793       elseif Np.id==id_hlist or Np.id==id_pbox or Np.id==id_disc then after_hlist()
794       end
795    else
796       if not mode then head = node_remove(head, last) end
797       return head
798    end
799    calc_np()
800    while Np do
801       extract_np()
802       -- 挿入部
803       if Np.id == id_jglyph then 
804          handle_np_jachar()
805       elseif Np.met then 
806          if Np.id==id_hlist then handle_np_ja_hlist()
807          else handle_np_jachar() end
808       elseif Nq.met then 
809          if Nq.id==id_hlist then handle_nq_ja_hlist()
810          else handle_nq_jachar() end
811       end
812       -- Np の後処理
813       if Np.id==id_glyph then after_alchar()
814       elseif Np.id==id_hlist or Np.id==id_pbox or Np.id==id_disc then after_hlist()
815       end
816       calc_np()
817    end
818    handle_list_tail()
819    -- adjust attr_icflag
820    tex.attribute[attr_icflag] = -(0x7FFFFFFF)
821    return head
822 end
823
824 -- \inhibitglue
825 function create_inhibitglue_node()
826    local g=node_new(id_whatsit, sid_user)
827    g.user_id=30111; g.type=100; g.value=1; node.write(g)
828 end