OSDN Git Service

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