OSDN Git Service

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