OSDN Git Service

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