OSDN Git Service

0692e3c8899583d257036ead3fcae42431d44a4c
[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       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); lp = node_next(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       Nx.char = 'jcharbdd'
465    else
466       Nx.pre = 0; Nx.post = 0; Nx.char = -1
467    end
468    Nx.met = nil
469    local y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
470    Nx.xspc_before = (y%2==1)
471    Nx.xspc_after  = (y>=2)
472    Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
473 end
474
475 -- Np の情報取得メインルーチン
476 local function extract_np()
477    local x = Np.nuc;
478    if Np.id ==  id_jglyph then set_np_xspc_jachar(Np, x)
479    elseif Np.id == id_glyph then set_np_xspc_alchar(Np, x.char, x, ligature_head)
480    elseif Np.id == id_hlist then Np.last_char = check_box_high(Np, x.head, nil)
481    elseif Np.id == id_pbox then Np.last_char = check_box_high(Np, Np.first, node.next(Np.last))
482    elseif Np.id == id_disc then Np.last_char = check_box_high(Np, x.replace, nil)
483    elseif Np.id == id_math then set_np_xspc_alchar(Np, -1, x)
484    end
485 end
486
487 -- change the information for the next loop
488 -- (will be done if Nx is an alphabetic character or a hlist)
489 function after_hlist(Nx)
490    if Nx.last_char then
491       if Nx.last_char.font == has_attr(Nx.last_char, attr_curjfnt) then 
492          set_np_xspc_jachar(Nx, Nx.last_char);
493       else
494          set_np_xspc_alchar(Nx, Nx.last_char.char,Nx.last_char, ligature_tail)
495       end
496    else
497       Nx.pre = nil; Nx.met = nil
498    end
499 end
500
501 local function after_alchar(Nx)
502    local x = Nx.nuc
503    set_np_xspc_alchar(Nx, x.char,x, ligature_tail)
504 end
505
506
507 -------------------- 最下層の処理
508
509 local function lineend_fix(g)
510    if g and g.id==id_kern then 
511       Nq.lend = 0
512    elseif Nq.lend~=0 then
513       if not g then
514          g = node_new(id_kern); g.subtype = 1; g.kern = -Nq.lend;
515      set_attr(g, attr_icflag, LINEEND)
516      set_attr(g, attr_uniqid, uniq_id) 
517       elseif g.id==id_kern then
518          g.kern = g.kern - Nq.lend
519       else
520          g.spec.width = g.spec.width - Nq.lend
521       end
522    end
523    return g
524 end
525
526 -- change penalties (or create a new penalty, if needed)
527 local function handle_penalty_normal(post, pre, g)
528    local a = (pre or 0) + (post or 0)
529    if #Bp == 0 then
530       if (a~=0 and not(g and g.id==id_kern)) or Nq.lend~=0 then
531          local p = node_new(id_penalty)
532          if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
533          p.penalty = a
534          head = node_insert_before(head, Np.first, p)
535      table_insert(Bp, p); 
536      set_attr(p, attr_icflag, KINSOKU)
537      set_attr(p, attr_uniqid, uniq_id) 
538       end
539    else for i, v in pairs(Bp) do add_penalty(v,a) end
540    end
541 end
542
543 local function handle_penalty_always(post, pre, g)
544    local a = (pre or 0) + (post or 0)
545    if #Bp == 0 then
546       if not (g and g.id==id_glue) or Nq.lend~=0 then
547          local p = node_new(id_penalty)
548          if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
549          p.penalty = a
550          head = node_insert_before(head, Np.first, p)
551          table_insert(Bp, p)
552      set_attr(p, attr_icflag, KINSOKU)
553      set_attr(p, attr_uniqid, uniq_id) 
554       end
555    else for i, v in pairs(Bp) do add_penalty(v,a) end
556    end
557 end
558
559 local function handle_penalty_suppress(post, pre, g)
560    local a = (pre or 0) + (post or 0)
561    if #Bp == 0 then
562       if g and g.id==id_glue then
563          local p = node_new(id_penalty)
564          p.penalty = 10000; head = node_insert_before(head, Np.first, p)
565          table_insert(Bp, p); 
566      set_attr(p, attr_icflag, KINSOKU)
567      set_attr(p, attr_uniqid, uniq_id) 
568       end
569    else for i, v in pairs(Bp) do add_penalty(v,a) end
570    end
571 end
572
573 -- 和文文字間の JFM glue を node 化
574 local function new_jfm_glue(Nn, bc, ac)
575 -- bc, ac: char classes
576    local g = nil
577    local z = Nn.met.size_cache[Nn.size].char_type[bc]
578    if z.glue and z.glue[ac] then
579       local h = node_new(id_glue_spec)
580       h.width   = z.glue[ac][1]
581       h.stretch = z.glue[ac][2]
582       h.shrink  = z.glue[ac][3]
583       h.stretch_order=0; h.shrink_order=0
584       g = node_new(id_glue)
585       g.subtype = 0; g.spec = h
586    elseif z.kern and z.kern[ac] then
587       g = node_new(id_kern)
588       g.subtype = 1; g.kern = z.kern[ac]
589    end
590    if g then 
591       set_attr(g, attr_icflag, FROM_JFM); set_attr(g, attr_uniqid, uniq_id) 
592    end
593    return g
594 end
595
596 -- Nq.last (kern w) .... (glue/kern g) Np.first
597 local function real_insert(w, g)
598    if w~=0 then
599       local h = node_new(id_kern)
600       set_attr(h, attr_icflag, LINE_END)
601       set_attr(h, attr_uniqid, uniq_id) 
602       h.kern = w; h.subtype = 1
603       head = node_insert_after(head, Nq.last, h)
604    end
605    if g then
606       head = node_insert_before(head, Np.first, g)
607       Np.first = g
608    end
609 end
610
611 -------------------- 和文文字間空白量の決定
612
613 -- get kanjiskip
614 local function get_kanji_skip_from_jfm(Nn)
615    local i = Nn.met.size_cache[Nn.size].kanjiskip
616    if i then
617       return { i[1], i[2], i[3] }
618    else return nil
619    end
620 end
621 local function get_kanjiskip()
622    local g = node_new(id_glue)
623    if Np.auto_kspc or Nq.auto_kspc then
624       if kanji_skip.width == max_dimen then
625          local gx = node_new(id_glue_spec);
626          gx.stretch_order = 0; gx.shrink_order = 0
627          local bk = get_kanji_skip_from_jfm(Nq)
628          local ak
629          if (Np.met==Nq.met) and (Nq.size==Np.size) and (Nq.var==Np.var) then
630             ak = nil
631          else
632             ak = get_kanji_skip_from_jfm(Np)
633          end
634          if bk then
635             if ak then
636                gx.width = round(diffmet_rule(bk[1], ak[1]))
637                gx.stretch = round(diffmet_rule(bk[2], ak[2]))
638                gx.shrink = -round(diffmet_rule(-bk[3], -ak[3]))
639             else
640                gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
641             end
642          elseif ak then
643             gx.width = ak[1]; gx.stretch = ak[2]; gx.shrink = ak[3]
644          else node_free(gx); gx = get_zero_spec() -- fallback
645          end
646          g.spec = gx
647       else g.spec=node_copy(kanji_skip); node_free(gx) end
648    else
649       g.spec =  get_zero_spec(); node_free(gx)
650    end
651    set_attr(g, attr_icflag, KANJI_SKIP)
652    set_attr(g, attr_uniqid, uniq_id) 
653    return g
654 end
655
656 local function calc_ja_ja_aux(gb,ga)
657    if not gb then 
658       return ga
659    else
660       if not ga then return gb end
661       local k = node.type(gb.id) .. node.type(ga.id)
662       if k == 'glueglue' then 
663          -- 両方とも glue.
664          gb.spec.width   = round(diffmet_rule(gb.spec.width, ga.spec.width))
665          gb.spec.stretch = round(diffmet_rule(gb.spec.stretch,ga.spec.shrink))
666          gb.spec.shrink  = -round(diffmet_rule(-gb.spec.shrink, -ga.spec.shrink))
667          node_free(ga)
668          return gb
669       elseif k == 'kernkern' then
670          -- 両方とも kern.
671          gb.kern = round(diffmet_rule(gb.kern, ga.kern))
672          node_free(ga)
673          return gb
674       elseif k == 'kernglue' then 
675          -- gb: kern, ga: glue
676          ga.spec.width   = round(diffmet_rule(gb.kern,ga.spec.width))
677          ga.spec.stretch = round(diffmet_rule(ga.spec.stretch, 0))
678          ga.spec.shrink  = -round(diffmet_rule(-ga.spec.shrink, 0))
679          node_free(gb)
680          return ga
681       else
682          -- gb: glue, ga: kern
683          gb.spec.width   = round(diffmet_rule(ga.kern, gb.spec.width))
684          gb.spec.stretch = round(diffmet_rule(gb.spec.stretch, 0))
685          gb.spec.shrink  = -round(diffmet_rule(-gb.spec.shrink, 0))
686          node_free(ga)
687          return gb
688       end
689    end
690 end
691
692 local function calc_ja_ja_glue()
693    if  ihb_flag then return nil
694    elseif (Nq.size==Np.size) and (Nq.met==Np.met) and (Nq.var==Np.var) then
695       return new_jfm_glue(Nq, Nq.class, Np.class)
696    else
697       local g = new_jfm_glue(Nq, Nq.class,
698                              fast_find_char_class('diffmet',Nq.met))
699       local h = new_jfm_glue(Np, fast_find_char_class('diffmet',Np.met),
700                              Np.class)
701       return calc_ja_ja_aux(g,h)
702    end
703 end
704
705 -------------------- 和欧文間空白量の決定
706
707 -- get xkanjiskip
708 local function get_xkanji_skip_from_jfm(Nn)
709    local i = Nn.met.size_cache[Nn.size].xkanjiskip
710    if i then
711       return { i[1], i[2], i[3] }
712    else return nil
713    end
714 end
715 local function get_xkanjiskip(Nn)
716    local g = node_new(id_glue)
717    if Nq.xspc_after and Np.xspc_before and (Nq.auto_xspc or Np.auto_xspc) then
718       if xkanji_skip.width == max_dimen then
719          local gx = node_new(id_glue_spec);
720          gx.stretch_order = 0; gx.shrink_order = 0
721          local bk = get_xkanji_skip_from_jfm(Nn)
722          if bk then
723             gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
724          else node_free(gx); gx = get_zero_spec() -- fallback
725          end
726          g.spec = gx
727       else g.spec=node_copy(xkanji_skip) end
728    else
729       g.spec = get_zero_spec()
730    end
731    set_attr(g, attr_icflag, XKANJI_SKIP)
732    set_attr(g, attr_uniqid, uniq_id) 
733    return g
734 end
735
736
737 -------------------- 隣接した「塊」間の処理
738
739 local function get_OA_skip()
740    if not ihb_flag then
741       local c = Nq.char or 'jcharbdd'
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 = Np.char or 'jcharbdd'
749       return new_jfm_glue(Nq, Nq.class, fast_find_char_class(c,Nq.met))
750    else return nil
751    end
752 end
753
754 -- (anything) .. jachar
755 local function handle_np_jachar()
756    local g
757    if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id==id_pbox_w) and Nq.met) then 
758       g = calc_ja_ja_glue() or get_kanjiskip() -- M->K
759       g = lineend_fix(g)
760       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
761    elseif Nq.met then  -- Nq.id==id_hlist
762       g = get_OA_skip() or get_kanjiskip() -- O_A->K
763       handle_penalty_normal(0, Np.pre, g); real_insert(0, g)
764    elseif Nq.pre then 
765       g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
766       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
767    else
768       g = get_OA_skip() -- O_A
769       if Nq.id==id_glue then handle_penalty_normal(0, Np.pre, g)
770       elseif Nq.id==id_kern then handle_penalty_suppress(0, Np.pre, g)
771       else handle_penalty_always(0, Np.pre, g)
772       end
773       real_insert(0, g)
774    end
775    -- \jcharwidowpenalty 挿入予定箇所更新
776    if mode and ltjs_get_penalty_table('kcat', Np.char, 0, box_stack_level)%2~=1 then
777       widow_Np.first = Np.first; 
778       local Bpr = widow_Bp; widow_Bp = Bp; Bp = Bpr
779    end
780 end
781
782 -- jachar .. (anything)
783 local function handle_nq_jachar()
784    local g
785    if Np.pre then 
786       g = get_OB_skip() or get_xkanjiskip(Nq) -- O_B->X
787       g = lineend_fix(g)
788       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
789    else
790       g = get_OB_skip(); g = lineend_fix(g) -- O_B
791       if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
792       elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
793       else handle_penalty_always(Nq.post, 0, g)
794       end
795       real_insert(Nq.lend, g)
796    end
797 end
798
799 -- (anything) .. (和文文字で終わる hlist)
800 local function handle_np_ja_hlist()
801    local g
802    if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id == id_pbox_w) and Nq.met) then 
803       g = get_OB_skip() or get_kanjiskip() -- O_B->K
804       g = lineend_fix(g)
805       handle_penalty_normal(Nq.post, 0, g); real_insert(Nq.lend, g)
806    elseif Nq.met then  -- Nq.id==id_hlist
807       g = get_kanjiskip() -- K
808       handle_penalty_suppress(0, 0, g); real_insert(0, g)
809    elseif Nq.pre then 
810       g = get_xkanjiskip(Np) -- X
811       handle_penalty_suppress(0, 0, g); real_insert(0, g)
812    end
813 end
814
815 -- (和文文字で終わる hlist) .. (anything)
816 local function handle_nq_ja_hlist()
817    local g = nil
818    if Np.pre then 
819       g = get_xkanjiskip(Nq) -- X
820       handle_penalty_suppress(0, 0, g); real_insert(0, g)
821    end
822 end
823
824 -- Nq が前側のクラスタとなることによる修正
825 local function adjust_nq()
826    if Nq.id==id_glyph then after_alchar(Nq)
827    elseif Nq.id==id_hlist or Nq.id==id_pbox or Nq.id==id_disc then after_hlist(Nq)
828    elseif Nq.id == id_pbox_w then 
829       luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
830                                false, Nq, Np, box_stack_level)
831    end
832 end
833
834 -------------------- 開始・終了時の処理
835
836 -- リスト末尾の処理
837 local function handle_list_tail()
838    adjust_nq(); Np = Nq
839    if mode then
840       -- the current list is to be line-breaked:
841       if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then 
842          if Np.lend~=0 then
843             g = node_new(id_kern); g.subtype = 0; g.kern = Np.lend
844             set_attr(g, attr_icflag, BOXBDD)
845             node_insert_after(head, Np.last, g)
846          end
847       end
848       -- Insert \jcharwidowpenalty
849       Bp = widow_Bp; Np = widow_Np; Nq.lend = 0
850       if Np.first then
851          handle_penalty_normal(0,
852                                ltjs_get_penalty_table('jwp', 0, 0, box_stack_level))
853       end
854    else
855       -- the current list is the contents of a hbox
856       if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then 
857          local g = new_jfm_glue(Np, Np.class, fast_find_char_class('boxbdd',Np.met))
858          if g then
859             set_attr(g, attr_icflag, BOXBDD)
860             head = node_insert_after(head, Np.last, g)
861          end
862       end
863    end
864 end
865
866 -- リスト先頭の処理
867 local function handle_list_head()
868    if Np.id ==  id_jglyph or (Np.id==id_pbox and Np.met) then 
869       if not ihb_flag then
870          local g
871          if par_indented then
872             g = new_jfm_glue(Np, fast_find_char_class('parbdd',Np.met), Np.class)
873          else
874             g = new_jfm_glue(Np, fast_find_char_class('boxbdd',Np.met), Np.class)
875          end
876          if g then
877             set_attr(g, attr_icflag, BOXBDD)
878             if g.id==id_glue and #Bp==0 then
879                local h = node_new(id_penalty)
880                h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
881             end
882             head = node_insert_before(head, Np.first, g)
883          end
884       end
885    end
886 end
887
888 -- initialize
889 local function init_var()
890    uniq_id = uniq_id +1
891    if uniq_id == 0x7FFFFFF then uniq_id = 0 end
892    lp = head; Bp = {}; widow_Bp = {}; widow_Np = {first = nil}
893    par_indented = false 
894    box_stack_level = ltjp.box_stack_level
895    kanji_skip=skip_table_to_spec('kanjiskip')
896    xkanji_skip=skip_table_to_spec('xkanjiskip')
897    Np = {
898       auto_kspc=nil, auto_xspc=nil, char=nil, class=nil, 
899       first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil, 
900       post=nil, pre=nil, var=nil, xspc_after=nil, xspc_before=nil, 
901    }
902    Nq = {
903       auto_kspc=nil, auto_xspc=nil, char=nil, class=nil, 
904       first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil, 
905       post=nil, pre=nil, var=nil, xspc_after=nil, xspc_before=nil, 
906    }
907    if mode then 
908       -- the current list is to be line-breaked:
909       -- hbox from \parindent is skipped.
910       while lp and ((lp.id==id_whatsit and lp.subtype~=sid_user) 
911                  or ((lp.id==id_hlist) and (lp.subtype==3))) do
912          if (lp.id==id_hlist) and (lp.subtype==3) then par_indented = true end
913          lp=node_next(lp) end
914      last=node.tail(head)
915    else 
916       -- the current list is the contents of a hbox:
917       -- insert a sentinelEG
918       last=node.tail(head); local g = node_new(id_kern)
919       node_insert_after(head, last, g); last = g
920    end
921 end
922
923 local function cleanup()
924    -- adjust attr_icflag for avoiding error
925    tex.attribute[attr_icflag] = -(0x7FFFFFFF)
926    node_free(kanji_skip); node_free(xkanji_skip)
927    if mode then
928       local h = node_next(head)
929       if h.id == id_penalty and h.penalty == 10000 then
930          h = h.next
931          if h.id == id_glue and h.subtype == 15 and not h.next then
932             return false
933          end
934       end
935       return head
936    else
937       head = node_remove(head, last); node_free(last);-- remove the sentinel
938       return head
939    end
940 end
941 -------------------- 外部から呼ばれる関数
942
943 -- main interface
944 function main(ahead, amode)
945    if not ahead then return ahead end
946    head = ahead; mode = amode; init_var(); calc_np()
947    if Np then 
948       extract_np(); handle_list_head()
949    else
950       return cleanup()
951    end
952    calc_np()
953    while Np do
954       extract_np(); adjust_nq()
955      -- 挿入部
956       if Np.id == id_jglyph then 
957          handle_np_jachar()
958       elseif Np.met then 
959          if Np.id==id_hlist then handle_np_ja_hlist()
960          else handle_np_jachar() end
961       elseif Nq.met then 
962          if Nq.id==id_hlist then handle_nq_ja_hlist()
963          else handle_nq_jachar() end
964       end
965       calc_np()
966    end
967    handle_list_tail()
968    return cleanup()
969 end
970
971 -- \inhibitglue
972
973 function create_inhibitglue_node()
974    local tn = node_new(id_whatsit, sid_user)
975    tn.user_id=30111; tn.type=100; tn.value=1
976    node.write(tn)
977 end
978
979 -- Node for indicating beginning of a paragraph
980 -- (for ltjsclasses)
981 function create_beginpar_node()
982    local tn = node_new(id_whatsit, sid_user)
983    tn.user_id=30114; tn.type=100; tn.value=1
984    node.write(tn)
985 end
986
987 local function whatsit_callback(Np, lp, Nq, bsl)
988    if Np.nuc then return Np 
989    elseif lp.user_id == 30114 then
990       Np.first = lp; Np.nuc = lp; Np.last = lp
991       Np.char = 'parbdd'
992       Np.met = nil
993       Np.pre = 0; Np.post = 0
994       Np.xspc_before = false
995       Np.xspc_after  = false
996       Np.auto_xspc = false
997       return Np
998    end
999 end
1000 local function whatsit_after_callback(s, Nq, Np, bsl)
1001    if not s and Nq.nuc.user_id == 30114 then
1002       local x, y = node.prev(Nq.nuc), Nq.nuc
1003       Nq.first, Nq.nuc, Nq.last = x, x, x
1004       head = node_remove(head, y)
1005    end
1006    return s
1007 end
1008
1009 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
1010                            "luatexja.beginpar.np_info", 1)
1011 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
1012                            "luatexja.beginpar.np_info_after", 1)
1013