OSDN Git Service

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