OSDN Git Service

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