OSDN Git Service

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