OSDN Git Service

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