OSDN Git Service

Add the other CID key in function "cid(key)".
[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/25',
7   version = '0.4',
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          pid = p.id
197       end
198       if pid==id_hlist then
199          if has_attr(p, attr_icflag)==PACKED then
200             for q in node.traverse_id(id_glyph, p.head) do
201                if find_first_char then
202                   first_char = q; find_first_char = false
203                end
204                last_char = q; found_visible_node = true; break
205             end
206          else
207             if p.shift==0 then
208                if check_box(p.head, nil) then found_visible_node = true end
209             else if find_first_char then 
210                   find_first_char = false
211                else 
212                   last_char = nil
213                end
214             end
215          end
216       elseif not (pid==id_ins   or pid==id_mark
217                   or pid==id_adjust or pid==id_whatsit
218                   or pid==id_penalty) then
219          found_visible_node = true
220          if find_first_char then 
221             find_first_char = false
222          else 
223             last_char = nil
224          end
225       end
226       p = node_next(p)
227    end
228    return found_visible_node
229 end 
230
231 function check_box_high(Nx, box_ptr, box_end)
232    first_char = nil;  last_char = nil;  find_first_char = true
233    if check_box(box_ptr, box_end) then
234       if first_char then
235          if first_char.font == has_attr(first_char, attr_curjfnt) then 
236             set_np_xspc_jachar(Nx, first_char)
237          else
238             set_np_xspc_alchar(Nx, first_char.char,first_char, ligature_head)
239          end
240       end
241    end
242    return last_char
243 end
244
245 -------------------- Np の計算と情報取得
246
247 luatexbase.create_callback("luatexja.jfmglue.whatsit_getinfo", "data", 
248                            function (Np, lp, Nq, box_stack_level) 
249                               if Np.nuc then return Np 
250                               else 
251                                  return Np  -- your code
252                               end
253                            end)
254 luatexbase.create_callback("luatexja.jfmglue.whatsit_after", "data", 
255                            function (stat, Nq, Np, box_stack_level) return false end)
256
257 -- calc next Np
258 local function set_attr_icflag_processed(p)
259    local a = has_attr(p, attr_icflag) or 0
260    if a<= ITALIC then 
261       set_attr(p, attr_uniqid, uniq_id) 
262       set_attr(p, attr_icflag, PROCESSED) 
263    end
264 end
265
266 local function check_next_ickern()
267    if lp.id == id_kern and has_attr(lp, attr_icflag)==ITALIC then
268       set_attr(lp, attr_icflag, IC_PROCESSED) 
269       set_attr(lp, attr_uniqid, uniq_id) 
270       Np.last = lp; lp = node_next(lp)
271    else Np.last = Np.nuc end
272 end
273
274 local function calc_np_pbox()
275    local uid = has_attr(lp, attr_uniqid)
276    Np.first = lp; Np.id = id_pbox
277    lpa = KINSOKU -- dummy=
278    while lp~=last and lpa>=PACKED and lpa~=BOXBDD
279       and has_attr(lp, attr_uniqid) == uid do
280       Np.nuc = lp; set_attr(lp, attr_uniqid, uniq_id) 
281       lp = node_next(lp); lpa = has_attr(lp, attr_icflag) or 0
282    end
283    check_next_ickern()
284 end
285
286 local calc_np_auxtable = {
287    [id_glyph] = function() 
288                    Np.first = lp
289                    if lp.font == has_attr(lp, attr_curjfnt) then 
290                       Np.id = id_jglyph 
291                    else 
292                       Np.id = id_glyph 
293                    end
294                    Np.first = lp; Np.nuc = lp; set_attr_icflag_processed(lp)
295                    lp = node_next(lp); check_next_ickern(); return true
296                 end,
297    [id_hlist] = function() 
298                    Np.first = lp; Np.last = lp; Np.nuc = lp; 
299                    set_attr_icflag_processed(lp)
300                    if lp.shift~=0 then 
301                       Np.id = id_box_like
302                    else 
303                       Np.id = id_hlist 
304                    end
305                    lp = node_next(lp); return true
306                 end,
307    [id_vlist] = function()
308                    Np.first = lp; Np.nuc = lp; Np.last = lp;
309                    Np.id = id_box_like; set_attr_icflag_processed(lp); 
310                    lp = node_next(lp); return true
311                 end,
312    [id_rule] = function()
313                   Np.first = lp; Np.nuc = lp; Np.last = lp;
314                   Np.id = id_box_like; set_attr_icflag_processed(lp); 
315                   lp = node_next(lp); return true
316                end,
317    [id_ins] = function() 
318                  set_attr_icflag_processed(lp); lp = node_next(lp)
319                  return false
320               end,
321    [id_mark] = function() 
322                   set_attr_icflag_processed(lp); lp = node_next(lp)
323                   return false
324                end,
325    [id_adjust] = function() 
326                     set_attr_icflag_processed(lp); lp = node_next(lp)
327                     return false
328                  end,
329    [id_disc] = function()
330                   Np.first = lp; Np.nuc = lp; set_attr_icflag_processed(lp); 
331                   Np.last = lp; Np.id = id_disc; lp = node_next(lp); return true
332                end,
333    [id_whatsit] = function() 
334                   if lp.subtype==sid_user then
335                      if lp.user_id==30111 then
336                         local lq = node_next(lp)
337                         head = node_remove(head, lp); node_free(lp); lp = lq; ihb_flag = true
338                      else
339                         set_attr_icflag_processed(lp)
340                         luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo"
341                                                  , Np, lp, Nq, box_stack_level)
342                         lp = node_next(lp)
343                         if Np.nuc then 
344                            Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc; return true
345                         end
346                      end
347                   else
348                      set_attr_icflag_processed(lp); lp = node_next(lp)
349                   end
350                   return false
351                   end,
352    [id_math] = function()
353                   Np.first = lp; Np.nuc = lp; 
354                   set_attr_icflag_processed(lp); lp  = node_next(lp) 
355                   while lp.id~=id_math do 
356                      set_attr_icflag_processed(lp); lp  = node_next(lp) 
357                   end
358                   set_attr_icflag_processed(lp); 
359                   Np.last = lp; Np.id = id_math; lp = node_next(lp); 
360                   return true
361                end,
362    [id_glue] = function()
363                   Np.first = lp; Np.nuc = lp; set_attr_icflag_processed(lp); 
364                   Np.last = lp; Np.id = id_glue; lp = node_next(lp); return true
365                end,
366    [id_kern] = function() 
367                   Np.first = lp
368                   if lp.subtype==2 then
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); lp = node_next(lp)
372                      set_attr_icflag_processed(lp); Np.nuc = lp
373                      if lp.font == has_attr(lp, attr_curjfnt) then 
374                         Np.id = id_jglyph 
375                      else
376                         Np.id = id_glyph 
377                      end
378                      lp = node_next(lp); check_next_ickern(); 
379                   else
380                      Np.id = id_kern; set_attr_icflag_processed(lp);
381                      Np.last = lp; lp = node_next(lp)
382                   end
383                   return true
384                end,
385    [id_penalty] = function()
386                      Bp[#Bp+1] = lp; set_attr_icflag_processed(lp); 
387                      lp = node_next(lp); return false
388                   end,
389    [13] = function()
390                   Np.first = lp; Np.nuc = lp; Np.last = lp;
391                   Np.id = id_box_like; set_attr_icflag_processed(lp); 
392                   lp = node_next(lp); return true
393                end,
394 }
395
396 local function calc_np()
397    -- We assume lp = node_next(Np.last)
398    local lpi, lpa, Nr
399    Nr = Nq; for k in pairs(Nr) do Nr[k] = nil end
400    Nq = Np; Np = Nr
401    for k in pairs(Bp) do Bp[k] = nil end
402    ihb_flag = false 
403    while lp ~= last do
404       lpa = has_attr(lp, attr_icflag) or 0
405       if lpa>=PACKED then
406          if lpa == BOXBDD then
407             local lq = node_next(lp)
408             head = node_remove(head, lp); node_free(lp); lp = lq
409          else calc_np_pbox(); return 
410          end -- id_pbox
411       elseif calc_np_auxtable[lp.id]() then return end
412    end
413    Np = nil; return
414 end
415
416 -- extract informations from Np
417 -- We think that "Np is a Japanese character" if Np.met~=nil,
418 --            "Np is an alphabetic character" if Np.pre~=nil,
419 --            "Np is not a character" otherwise.
420
421 -- 和文文字のデータを取得
422 function set_np_xspc_jachar(Nx, x)
423    local z = ltjf_font_metric_table[x.font]
424    local c = x.char
425    local cls = ltjf_find_char_class(c, z)
426    local m = ltjf_metrics[z.jfm]
427    set_attr(x, attr_jchar_class, cls)
428    Nx.class = cls
429    Nx.char = c
430    Nx.size= z.size
431    Nx.met = m
432    Nx.var = z.var
433    Nx.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
434    Nx.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
435    z = fast_find_char_class('lineend', m)
436    local y = m.size_cache[Nx.size].char_type[Nx.class]
437    if y.kern and y.kern[z] then 
438       Nx.lend = y.kern[z]
439    else 
440       Nx.lend = 0 
441    end
442    y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
443    Nx.xspc_before = (y%2==1)
444    Nx.xspc_after  = (y>=2)
445    Nx.auto_kspc = (has_attr(x, attr_autospc)==1)
446    Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
447 end
448
449 -- 欧文文字のデータを取得
450 local ligature_head = 1
451 local ligature_tail = 2
452 function set_np_xspc_alchar(Nx, c,x, lig)
453    if c~=-1 then
454       if lig == ligature_head then
455          while x.components and x.subtype and math.floor(x.subtype/2)%2==1 do
456             x = x.components; c = x.char
457          end
458       else
459          while x.components and x.subtype and math.floor(x.subtype/2)%2==1 do
460             x = node_tail(x.components); c = x.char
461          end
462       end
463       Nx.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
464       Nx.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
465       Nx.char = 'jcharbdd'
466    else
467       Nx.pre = 0; Nx.post = 0; Nx.char = -1
468    end
469    Nx.met = nil
470    local y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
471    Nx.xspc_before = (y%2==1)
472    Nx.xspc_after  = (y>=2)
473    Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
474 end
475
476 -- Np の情報取得メインルーチン
477 local function extract_np()
478    local x = Np.nuc;
479    if Np.id ==  id_jglyph then set_np_xspc_jachar(Np, x)
480    elseif Np.id == id_glyph then set_np_xspc_alchar(Np, x.char, x, ligature_head)
481    elseif Np.id == id_hlist then Np.last_char = check_box_high(Np, x.head, nil)
482    elseif Np.id == id_pbox then Np.last_char = check_box_high(Np, Np.first, node.next(Np.last))
483    elseif Np.id == id_disc then Np.last_char = check_box_high(Np, x.replace, nil)
484    elseif Np.id == id_math then set_np_xspc_alchar(Np, -1, x)
485    end
486 end
487
488 -- change the information for the next loop
489 -- (will be done if Nx is an alphabetic character or a hlist)
490 function after_hlist(Nx)
491    if Nx.last_char then
492       if Nx.last_char.font == has_attr(Nx.last_char, attr_curjfnt) then 
493          set_np_xspc_jachar(Nx, Nx.last_char);
494       else
495          set_np_xspc_alchar(Nx, Nx.last_char.char,Nx.last_char, ligature_tail)
496       end
497    else
498       Nx.pre = nil; Nx.met = nil
499    end
500 end
501
502 local function after_alchar(Nx)
503    local x = Nx.nuc
504    set_np_xspc_alchar(Nx, x.char,x, ligature_tail)
505 end
506
507
508 -------------------- 最下層の処理
509
510 local function lineend_fix(g)
511    if g and g.id==id_kern then 
512       Nq.lend = 0
513    elseif Nq.lend~=0 then
514       if not g then
515          g = node_new(id_kern); g.subtype = 1; g.kern = -Nq.lend;
516      set_attr(g, attr_icflag, LINEEND)
517      set_attr(g, attr_uniqid, uniq_id) 
518       elseif g.id==id_kern then
519          g.kern = g.kern - Nq.lend
520       else
521          g.spec.width = g.spec.width - Nq.lend
522       end
523    end
524    return g
525 end
526
527 -- change penalties (or create a new penalty, if needed)
528 local function handle_penalty_normal(post, pre, g)
529    local a = (pre or 0) + (post or 0)
530    if #Bp == 0 then
531       if (a~=0 and not(g and g.id==id_kern)) or Nq.lend~=0 then
532          local p = node_new(id_penalty)
533          if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
534          p.penalty = a
535          head = node_insert_before(head, Np.first, p)
536      table_insert(Bp, p); 
537      set_attr(p, attr_icflag, KINSOKU)
538      set_attr(p, attr_uniqid, uniq_id) 
539       end
540    else for i, v in pairs(Bp) do add_penalty(v,a) end
541    end
542 end
543
544 local function handle_penalty_always(post, pre, g)
545    local a = (pre or 0) + (post or 0)
546    if #Bp == 0 then
547       if not (g and g.id==id_glue) or Nq.lend~=0 then
548          local p = node_new(id_penalty)
549          if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
550          p.penalty = a
551          head = node_insert_before(head, Np.first, p)
552          table_insert(Bp, p)
553      set_attr(p, attr_icflag, KINSOKU)
554      set_attr(p, attr_uniqid, uniq_id) 
555       end
556    else for i, v in pairs(Bp) do add_penalty(v,a) end
557    end
558 end
559
560 local function handle_penalty_suppress(post, pre, g)
561    local a = (pre or 0) + (post or 0)
562    if #Bp == 0 then
563       if g and g.id==id_glue then
564          local p = node_new(id_penalty)
565          p.penalty = 10000; head = node_insert_before(head, Np.first, p)
566          table_insert(Bp, p); 
567      set_attr(p, attr_icflag, KINSOKU)
568      set_attr(p, attr_uniqid, uniq_id) 
569       end
570    else for i, v in pairs(Bp) do add_penalty(v,a) end
571    end
572 end
573
574 -- 和文文字間の JFM glue を node 化
575 local function new_jfm_glue(Nn, bc, ac)
576 -- bc, ac: char classes
577    local g = nil
578    local z = Nn.met.size_cache[Nn.size].char_type[bc]
579    if z.glue and z.glue[ac] then
580       local h = node_new(id_glue_spec)
581       h.width   = z.glue[ac][1]
582       h.stretch = z.glue[ac][2]
583       h.shrink  = z.glue[ac][3]
584       h.stretch_order=0; h.shrink_order=0
585       g = node_new(id_glue)
586       g.subtype = 0; g.spec = h
587    elseif z.kern and z.kern[ac] then
588       g = node_new(id_kern)
589       g.subtype = 1; g.kern = z.kern[ac]
590    end
591    if g then 
592       set_attr(g, attr_icflag, FROM_JFM); set_attr(g, attr_uniqid, uniq_id) 
593    end
594    return g
595 end
596
597 -- Nq.last (kern w) .... (glue/kern g) Np.first
598 local function real_insert(w, g)
599    if w~=0 then
600       local h = node_new(id_kern)
601       set_attr(h, attr_icflag, LINE_END)
602       set_attr(h, attr_uniqid, uniq_id) 
603       h.kern = w; h.subtype = 1
604       head = node_insert_after(head, Nq.last, h)
605    end
606    if g then
607       head = node_insert_before(head, Np.first, g)
608       Np.first = g
609    end
610 end
611
612 -------------------- 和文文字間空白量の決定
613
614 -- get kanjiskip
615 local function get_kanji_skip_from_jfm(Nn)
616    local i = Nn.met.size_cache[Nn.size].kanjiskip
617    if i then
618       return { i[1], i[2], i[3] }
619    else return nil
620    end
621 end
622 local function get_kanjiskip()
623    local g = node_new(id_glue)
624    if Np.auto_kspc or Nq.auto_kspc then
625       if kanji_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_kanji_skip_from_jfm(Nq)
629          local ak
630          if (Np.met==Nq.met) and (Nq.size==Np.size) and (Nq.var==Np.var) then
631             ak = nil
632          else
633             ak = get_kanji_skip_from_jfm(Np)
634          end
635          if bk then
636             if ak then
637                gx.width = round(diffmet_rule(bk[1], ak[1]))
638                gx.stretch = round(diffmet_rule(bk[2], ak[2]))
639                gx.shrink = -round(diffmet_rule(-bk[3], -ak[3]))
640             else
641                gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
642             end
643          elseif ak then
644             gx.width = ak[1]; gx.stretch = ak[2]; gx.shrink = ak[3]
645          else node_free(gx); gx = get_zero_spec() -- fallback
646          end
647          g.spec = gx
648       else g.spec=node_copy(kanji_skip); node_free(gx) end
649    else
650       g.spec =  get_zero_spec(); node_free(gx)
651    end
652    set_attr(g, attr_icflag, KANJI_SKIP)
653    set_attr(g, attr_uniqid, uniq_id) 
654    return g
655 end
656
657 local function calc_ja_ja_aux(gb,ga)
658    if not gb then 
659       return ga
660    else
661       if not ga then return gb end
662       local k = node.type(gb.id) .. node.type(ga.id)
663       if k == 'glueglue' then 
664          -- 両方とも glue.
665          gb.spec.width   = round(diffmet_rule(gb.spec.width, ga.spec.width))
666          gb.spec.stretch = round(diffmet_rule(gb.spec.stretch,ga.spec.shrink))
667          gb.spec.shrink  = -round(diffmet_rule(-gb.spec.shrink, -ga.spec.shrink))
668          node_free(ga)
669          return gb
670       elseif k == 'kernkern' then
671          -- 両方とも kern.
672          gb.kern = round(diffmet_rule(gb.kern, ga.kern))
673          node_free(ga)
674          return gb
675       elseif k == 'kernglue' then 
676          -- gb: kern, ga: glue
677          ga.spec.width   = round(diffmet_rule(gb.kern,ga.spec.width))
678          ga.spec.stretch = round(diffmet_rule(ga.spec.stretch, 0))
679          ga.spec.shrink  = -round(diffmet_rule(-ga.spec.shrink, 0))
680          node_free(gb)
681          return ga
682       else
683          -- gb: glue, ga: kern
684          gb.spec.width   = round(diffmet_rule(ga.kern, gb.spec.width))
685          gb.spec.stretch = round(diffmet_rule(gb.spec.stretch, 0))
686          gb.spec.shrink  = -round(diffmet_rule(-gb.spec.shrink, 0))
687          node_free(ga)
688          return gb
689       end
690    end
691 end
692
693 local function calc_ja_ja_glue()
694    if  ihb_flag then return nil
695    elseif (Nq.size==Np.size) and (Nq.met==Np.met) and (Nq.var==Np.var) then
696       return new_jfm_glue(Nq, Nq.class, Np.class)
697    else
698       local g = new_jfm_glue(Nq, Nq.class,
699                              fast_find_char_class('diffmet',Nq.met))
700       local h = new_jfm_glue(Np, fast_find_char_class('diffmet',Np.met),
701                              Np.class)
702       return calc_ja_ja_aux(g,h)
703    end
704 end
705
706 -------------------- 和欧文間空白量の決定
707
708 -- get xkanjiskip
709 local function get_xkanji_skip_from_jfm(Nn)
710    local i = Nn.met.size_cache[Nn.size].xkanjiskip
711    if i then
712       return { i[1], i[2], i[3] }
713    else return nil
714    end
715 end
716 local function get_xkanjiskip(Nn)
717    local g = node_new(id_glue)
718    if Nq.xspc_after and Np.xspc_before and (Nq.auto_xspc or Np.auto_xspc) then
719       if xkanji_skip.width == max_dimen then
720          local gx = node_new(id_glue_spec);
721          gx.stretch_order = 0; gx.shrink_order = 0
722          local bk = get_xkanji_skip_from_jfm(Nn)
723          if bk then
724             gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
725          else node_free(gx); gx = get_zero_spec() -- fallback
726          end
727          g.spec = gx
728       else g.spec=node_copy(xkanji_skip) end
729    else
730       g.spec = get_zero_spec()
731    end
732    set_attr(g, attr_icflag, XKANJI_SKIP)
733    set_attr(g, attr_uniqid, uniq_id) 
734    return g
735 end
736
737
738 -------------------- 隣接した「塊」間の処理
739
740 local function get_OA_skip()
741    if not ihb_flag then
742       local c = Nq.char or 'jcharbdd'
743       return new_jfm_glue(Np, fast_find_char_class(c,Np.met), Np.class)
744    else return nil
745    end
746 end
747 local function get_OB_skip()
748    if not ihb_flag then
749       local c = Np.char or 'jcharbdd'
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       if Nq.id==id_hlist then Nq.post = 0 end
768       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
769    else
770       g = get_OA_skip() -- O_A
771       if Nq.id==id_glue then handle_penalty_normal(0, Np.pre, g)
772       elseif Nq.id==id_kern then handle_penalty_suppress(0, Np.pre, g)
773       else handle_penalty_always(0, Np.pre, g)
774       end
775       real_insert(0, g)
776    end
777    -- \jcharwidowpenalty 挿入予定箇所更新
778    if mode and ltjs_get_penalty_table('kcat', Np.char, 0, box_stack_level)%2~=1 then
779       widow_Np.first = Np.first; 
780       local Bpr = widow_Bp; widow_Bp = Bp; Bp = Bpr
781    end
782 end
783
784 -- jachar .. (anything)
785 local function handle_nq_jachar()
786    local g
787    if Np.pre then 
788       if Np.id==id_hlist then Np.pre = 0 end
789       g = get_OB_skip() or get_xkanjiskip(Nq) -- O_B->X
790       g = lineend_fix(g)
791       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
792    else
793       g = get_OB_skip(); g = lineend_fix(g) -- O_B
794       if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
795       elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
796       else handle_penalty_always(Nq.post, 0, g)
797       end
798       real_insert(Nq.lend, g)
799    end
800 end
801
802 -- (anything) .. (和文文字で終わる hlist)
803 local function handle_np_ja_hlist()
804    local g
805    if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id == id_pbox_w) and Nq.met) then 
806       g = get_OB_skip() or get_kanjiskip() -- O_B->K
807       g = lineend_fix(g)
808       handle_penalty_normal(Nq.post, 0, g); real_insert(Nq.lend, g)
809    elseif Nq.met then  -- Nq.id==id_hlist
810       g = get_kanjiskip() -- K
811       handle_penalty_suppress(0, 0, g); real_insert(0, g)
812    elseif Nq.pre then 
813       g = get_xkanjiskip(Np) -- X
814       handle_penalty_suppress(0, 0, g); real_insert(0, g)
815    end
816 end
817
818 -- (和文文字で終わる hlist) .. (anything)
819 local function handle_nq_ja_hlist()
820    local g = nil
821    if Np.pre then 
822       g = get_xkanjiskip(Nq) -- X
823       handle_penalty_suppress(0, 0, g); real_insert(0, g)
824    end
825 end
826
827 -- Nq が前側のクラスタとなることによる修正
828 local function adjust_nq()
829    if Nq.id==id_glyph then after_alchar(Nq)
830    elseif Nq.id==id_hlist or Nq.id==id_pbox or Nq.id==id_disc then after_hlist(Nq)
831    elseif Nq.id == id_pbox_w then 
832       luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
833                                false, Nq, Np, box_stack_level)
834    end
835 end
836
837 -------------------- 開始・終了時の処理
838
839 -- リスト末尾の処理
840 local function handle_list_tail()
841    adjust_nq(); Np = Nq
842    if mode then
843       -- the current list is to be line-breaked:
844       if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then 
845          if Np.lend~=0 then
846             g = node_new(id_kern); g.subtype = 0; g.kern = Np.lend
847             set_attr(g, attr_icflag, BOXBDD)
848             node_insert_after(head, Np.last, g)
849          end
850       end
851       -- Insert \jcharwidowpenalty
852       Bp = widow_Bp; Np = widow_Np; Nq.lend = 0
853       if Np.first then
854          handle_penalty_normal(0,
855                                ltjs_get_penalty_table('jwp', 0, 0, box_stack_level))
856       end
857    else
858       -- the current list is the contents of a hbox
859       if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then 
860          local g = new_jfm_glue(Np, Np.class, fast_find_char_class('boxbdd',Np.met))
861          if g then
862             set_attr(g, attr_icflag, BOXBDD)
863             head = node_insert_after(head, Np.last, g)
864          end
865       end
866    end
867 end
868
869 -- リスト先頭の処理
870 local function handle_list_head()
871    if Np.id ==  id_jglyph or (Np.id==id_pbox and Np.met) then 
872       if not ihb_flag then
873          local g
874          if par_indented then
875             g = new_jfm_glue(Np, fast_find_char_class('parbdd',Np.met), Np.class)
876          else
877             g = new_jfm_glue(Np, fast_find_char_class('boxbdd',Np.met), Np.class)
878          end
879          if g then
880             set_attr(g, attr_icflag, BOXBDD)
881             if g.id==id_glue and #Bp==0 then
882                local h = node_new(id_penalty)
883                h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
884             end
885             head = node_insert_before(head, Np.first, g)
886          end
887       end
888    end
889 end
890
891 -- initialize
892 local function init_var()
893    uniq_id = uniq_id +1
894    if uniq_id == 0x7FFFFFF then uniq_id = 0 end
895    lp = head; Bp = {}; widow_Bp = {}; widow_Np = {first = nil}
896    par_indented = false 
897    box_stack_level = ltjp.box_stack_level
898    kanji_skip=skip_table_to_spec('kanjiskip')
899    xkanji_skip=skip_table_to_spec('xkanjiskip')
900    Np = {
901       auto_kspc=nil, auto_xspc=nil, char=nil, class=nil, 
902       first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil, 
903       post=nil, pre=nil, var=nil, xspc_after=nil, xspc_before=nil, 
904    }
905    Nq = {
906       auto_kspc=nil, auto_xspc=nil, char=nil, class=nil, 
907       first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil, 
908       post=nil, pre=nil, var=nil, xspc_after=nil, xspc_before=nil, 
909    }
910    if mode then 
911       -- the current list is to be line-breaked:
912       -- hbox from \parindent is skipped.
913       while lp and ((lp.id==id_whatsit and lp.subtype~=sid_user) 
914                  or ((lp.id==id_hlist) and (lp.subtype==3))) do
915          if (lp.id==id_hlist) and (lp.subtype==3) then par_indented = true end
916          lp=node_next(lp) end
917      last=node.tail(head)
918    else 
919       -- the current list is the contents of a hbox:
920       -- insert a sentinelEG
921       last=node.tail(head); local g = node_new(id_kern)
922       node_insert_after(head, last, g); last = g
923    end
924 end
925
926 local function cleanup()
927    -- adjust attr_icflag for avoiding error
928    tex.attribute[attr_icflag] = -(0x7FFFFFFF)
929    node_free(kanji_skip); node_free(xkanji_skip)
930    if mode then
931       local h = node_next(head)
932       if h.id == id_penalty and h.penalty == 10000 then
933          h = h.next
934          if h.id == id_glue and h.subtype == 15 and not h.next then
935             return false
936          end
937       end
938       return head
939    else
940       head = node_remove(head, last); node_free(last);-- remove the sentinel
941       return head
942    end
943 end
944 -------------------- 外部から呼ばれる関数
945
946 -- main interface
947 function main(ahead, amode)
948    if not ahead then return ahead end
949    head = ahead; mode = amode; init_var(); calc_np()
950    if Np then 
951       extract_np(); handle_list_head()
952    else
953       return cleanup()
954    end
955    calc_np()
956    while Np do
957       extract_np(); adjust_nq()
958      -- 挿入部
959       if Np.id == id_jglyph then 
960          handle_np_jachar()
961       elseif Np.met then 
962          if Np.id==id_hlist then handle_np_ja_hlist()
963          else handle_np_jachar() end
964       elseif Nq.met then 
965          if Nq.id==id_hlist then handle_nq_ja_hlist()
966          else handle_nq_jachar() end
967       end
968       calc_np()
969    end
970    handle_list_tail()
971    return cleanup()
972 end
973
974 -- \inhibitglue
975
976 function create_inhibitglue_node()
977    local tn = node_new(id_whatsit, sid_user)
978    tn.user_id=30111; tn.type=100; tn.value=1
979    node.write(tn)
980 end
981
982 -- Node for indicating beginning of a paragraph
983 -- (for ltjsclasses)
984 function create_beginpar_node()
985    local tn = node_new(id_whatsit, sid_user)
986    tn.user_id=30114; tn.type=100; tn.value=1
987    node.write(tn)
988 end
989
990 local function whatsit_callback(Np, lp, Nq, bsl)
991    if Np.nuc then return Np 
992    elseif lp.user_id == 30114 then
993       Np.first = lp; Np.nuc = lp; Np.last = lp
994       Np.char = 'parbdd'
995       Np.met = nil
996       Np.pre = 0; Np.post = 0
997       Np.xspc_before = false
998       Np.xspc_after  = false
999       Np.auto_xspc = false
1000       return Np
1001    end
1002 end
1003 local function whatsit_after_callback(s, Nq, Np, bsl)
1004    if not s and Nq.nuc.user_id == 30114 then
1005       local x, y = node.prev(Nq.nuc), Nq.nuc
1006       Nq.first, Nq.nuc, Nq.last = x, x, x
1007       head = node_remove(head, y)
1008    end
1009    return s
1010 end
1011
1012 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
1013                            "luatexja.beginpar.np_info", 1)
1014 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
1015                            "luatexja.beginpar.np_info_after", 1)
1016