OSDN Git Service

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