OSDN Git Service

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