OSDN Git Service

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