OSDN Git Service

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