OSDN Git Service

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