OSDN Git Service

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