OSDN Git Service

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