OSDN Git Service

1887b78b4a2bf559a231b8b37c2fe33f69da5cd0
[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 then
180          if p.subtype==2 then
181             p = node_next(node_next(node_next(p))); pid = p.id
182          elseif IC_PROCESSED == has_attr(p, attr_icflag) then
183             p = node_next(p); pid = p.id
184          end
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
195       end
196       if pid==id_hlist then
197          if PACKED == has_attr(p, attr_icflag) 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) 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, box_stack_level) 
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, box_stack_level) return false end)
252
253 -- calc next Np
254 do
255
256 local function set_attr_icflag_processed(p)
257    if (has_attr(p, attr_icflag) or 0)<= ITALIC then 
258       set_attr(p, attr_uniqid, uniq_id) 
259       set_attr(p, attr_icflag, PROCESSED) 
260    end
261 end
262
263 local function check_next_ickern(lp)
264    if lp.id == id_kern and ITALIC == has_attr(lp, attr_icflag) then
265       set_attr(lp, attr_icflag, IC_PROCESSED) 
266       set_attr(lp, attr_uniqid, uniq_id) 
267       Np.last = lp; return node_next(lp)
268    else 
269       Np.last = Np.nuc; return lp
270    end
271 end
272
273 local function calc_np_pbox(lp, last)
274    local uid = has_attr(lp, attr_uniqid)
275    Np.first = Np.first or lp; Np.id = id_pbox
276    local lpa = KINSOKU -- dummy=
277    while lp~=last and lpa>=PACKED and lpa~=BOXBDD
278       and uid == has_attr(lp, attr_uniqid) do
279       Np.nuc = lp; set_attr(lp, attr_uniqid, uniq_id) 
280       lp = node_next(lp); lpa = has_attr(lp, attr_icflag) or 0
281    end
282    return check_next_ickern(lp)
283 end
284
285
286 local calc_np_auxtable = {
287    [id_glyph] = function(lp) 
288                    Np.first, Np.nuc = (Np.first or lp), lp;
289                    Np.id = (lp.font == has_attr(lp, attr_curjfnt)) and id_jglyph or id_glyph
290                    set_attr(lp, attr_uniqid, uniq_id) 
291                    --set_attr_icflag_processed(lp) treated in ltj-setwidth.lua
292                    return true, check_next_ickern(node_next(lp)); 
293                 end,
294    [id_hlist] = function(lp) 
295                    Np.first = Np.first or lp; Np.last = lp; Np.nuc = lp; 
296                    set_attr_icflag_processed(lp)
297                    Np.id = (lp.shift~=0) and id_box_like or id_hlist
298                    return true, node_next(lp)
299                 end,
300    box_like = function(lp)
301                  Np.first = Np.first or lp; Np.nuc = lp; Np.last = lp;
302                  Np.id = id_box_like; set_attr_icflag_processed(lp); 
303                  return true, node_next(lp);
304               end,
305    skip = function(lp) 
306              set_attr_icflag_processed(lp); return false, node_next(lp)
307           end,
308    [id_whatsit] = function(lp) 
309                   if lp.subtype==sid_user then
310                      if lp.user_id==30111 then
311                         local lq = node_next(lp); 
312                         head = node.remove(head, lp); node.free(lp); ihb_flag = true
313                         return false, lq;
314                      else
315                         set_attr_icflag_processed(lp)
316                         luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo",
317                                                  Np, lp, Nq, box_stack_level)
318                         if Np.nuc then 
319                            Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc; 
320                            return true, node_next(lp)
321                         else
322                            return false, node_next(lp)
323                         end
324                      end
325                   else
326                      -- we do special treatment for these whatsit nodes.
327                      if lp.subtype == sid_start_link or lp.subtype == sid_start_thread then
328                         Np.first = lp 
329                      elseif lp.subtype == sid_end_link or lp.subtype == sid_end_thread then
330                         Np.first, Nq.last = nil, lp;
331                      end
332                      set_attr_icflag_processed(lp); return false, node_next(lp)
333                   end
334                   end,
335    [id_math] = function(lp)
336                   Np.first, Np.nuc = (Np.first or lp), lp; 
337                   set_attr_icflag_processed(lp); lp  = node_next(lp) 
338                   while lp.id~=id_math do 
339                      set_attr_icflag_processed(lp); lp  = node_next(lp) 
340                   end
341                   set_attr_icflag_processed(lp); 
342                   Np.last, Np.id = lp, id_math;
343                   return true, node_next(lp); 
344                end,
345    discglue = function(lp)
346                  Np.first, Np.nuc, Np.last = (Np.first or lp), lp, lp; 
347                  Np.id = lp.id; set_attr_icflag_processed(lp); return true, node_next(lp)
348                end,
349    [id_kern] = function(lp) 
350                   Np.first = Np.first or lp
351                   if lp.subtype==2 then
352                      set_attr_icflag_processed(lp); lp = node_next(lp)
353                      set_attr_icflag_processed(lp); lp = node_next(lp)
354                      set_attr_icflag_processed(lp); lp = node_next(lp)
355                      set_attr_icflag_processed(lp); Np.nuc = lp
356                      Np.id = (lp.font == has_attr(lp, attr_curjfnt)) and id_jglyph or id_glyph
357                      return true, check_next_ickern(node_next(lp)); 
358                   else
359                      Np.id = id_kern; set_attr_icflag_processed(lp);
360                      Np.last = lp; return true, node_next(lp)
361                   end
362                end,
363    [id_penalty] = function(lp)
364                      Bp[#Bp+1] = lp; set_attr_icflag_processed(lp); 
365                      return false, node_next(lp)
366                   end,
367 }
368 calc_np_auxtable[id_vlist]  = calc_np_auxtable.box_like
369 calc_np_auxtable[id_rule]   = calc_np_auxtable.box_like
370 calc_np_auxtable[13]        = calc_np_auxtable.box_like
371 calc_np_auxtable[id_ins]    = calc_np_auxtable.skip
372 calc_np_auxtable[id_mark]   = calc_np_auxtable.skip
373 calc_np_auxtable[id_adjust] = calc_np_auxtable.skip
374 calc_np_auxtable[id_disc]   = calc_np_auxtable.discglue
375 calc_np_auxtable[id_glue]   = calc_np_auxtable.discglue
376
377 function calc_np(lp, last)
378    local k = nil
379    -- We assume lp = node_next(Np.last)
380    Np, Nq, ihb_flag = Nq, Np, false
381    for k in pairs(Np) do Np[k] = nil end
382    for k = 1,#Bp do Bp[k] = nil end
383    while lp ~= last do
384       local lpa = has_attr(lp, attr_icflag) or 0
385       if lpa>=PACKED then
386          if lpa == BOXBDD then
387             local lq = node_next(lp) 
388             head = node.remove(head, lp); node.free(lp); lp = lq
389          else return calc_np_pbox(lp, last)
390          end -- id_pbox
391       else
392          k, lp = calc_np_auxtable[lp.id](lp)
393          if k then return lp end
394       end
395    end
396    Np = nil; return lp
397    --Np = nil; return
398 end
399
400 end
401 local calc_np = calc_np
402
403 -- extract informations from Np
404 -- We think that "Np is a Japanese character" if Np.met~=nil,
405 --            "Np is an alphabetic character" if Np.pre~=nil,
406 --            "Np is not a character" otherwise.
407 do
408
409 -- 和文文字のデータを取得
410    local attr_jchar_class = luatexbase.attributes['ltj@charclass']
411    function set_np_xspc_jachar(Nx, x)
412       local m = ltjf_font_metric_table[x.font]
413       local c = has_attr(x, attr_orig_char) or 0
414       local cls = ltjf_find_char_class(x.char, m)
415       if cls==0 and c ~= x.char then cls = ltjf_find_char_class(-c, m) end
416       Nx.class = cls; set_attr(x, attr_jchar_class, cls)
417       Nx.lend = m.size_cache.char_type[cls].kern[fast_find_char_class('lineend', m)] or 0
418       Nx.met, Nx.var, Nx.char = m, m.var, c
419       Nx.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
420       Nx.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
421       local y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
422       Nx.xspc_before, Nx.xspc_after = (y%2==1), (y>=2)
423       Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
424    end
425    local set_np_xspc_jachar = set_np_xspc_jachar
426
427 -- 欧文文字のデータを取得
428    local floor = math.floor
429    function set_np_xspc_alchar(Nx, c,x, lig)
430       if c~=-1 then
431          if lig == ligature_head then
432             while x.components and x.subtype and math.floor(x.subtype*0.5)%2==1 do
433                x = x.components; c = x.char
434             end
435          else
436             while x.components and x.subtype and math.floor(x.subtype*0.5)%2==1 do
437                x = node.tail(x.components); c = x.char
438             end
439          end
440          Nx.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
441          Nx.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
442          Nx.char = 'jcharbdd'
443       else
444          Nx.pre, Nx.post, Nx.char = 0, 0, -1
445       end
446       Nx.met = nil
447       local y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
448       Nx.xspc_before, Nx.xspc_after = (y%2==1), (y>=2)
449       Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
450    end
451    local set_np_xspc_alchar = set_np_xspc_alchar
452
453 -- Np の情報取得メインルーチン
454    function extract_np()
455       local x, i = Np.nuc, Np.id;
456       if i ==  id_jglyph then return set_np_xspc_jachar(Np, x)
457       elseif i == id_glyph then return set_np_xspc_alchar(Np, x.char, x, ligature_head)
458       elseif i == id_hlist then Np.last_char = check_box_high(Np, x.head, nil)
459       elseif i == id_pbox then Np.last_char = check_box_high(Np, Np.first, node_next(Np.last))
460       elseif i == id_disc then Np.last_char = check_box_high(Np, x.replace, nil)
461       elseif i == id_math then return set_np_xspc_alchar(Np, -1, x)
462       end
463    end
464    
465    -- change the information for the next loop
466    -- (will be done if Nx is an alphabetic character or a hlist)
467    function after_hlist(Nx)
468       local s = Nx.last_char
469       if s then
470          if s.font == has_attr(s, attr_curjfnt) then 
471             set_np_xspc_jachar(Nx, s)
472          else
473             set_np_xspc_alchar(Nx, s.char, s, ligature_tail)
474          end
475       else
476          Nx.pre, Nx.met = nil, nil
477       end
478    end
479    
480    function after_alchar(Nx)
481       local x = Nx.nuc
482       return set_np_xspc_alchar(Nx, x.char,x, ligature_tail)
483    end
484
485 end
486 local after_hlist, after_alchar, extract_np = after_hlist, after_alchar, extract_np
487
488 -------------------- 最下層の処理
489
490 local function lineend_fix(g)
491    if g and g.id==id_kern then 
492       Nq.lend = 0
493    elseif Nq.lend~=0 then
494       if not g then
495          g = node.new(id_kern); copy_attr(g, Nq.nuc); 
496          g.subtype = 1; g.kern = -Nq.lend;
497          set_attr(g, attr_icflag, LINEEND)
498          set_attr(g, attr_uniqid, uniq_id) 
499       elseif g.id==id_kern then
500          g.kern = g.kern - Nq.lend
501       else
502          g.spec.width = g.spec.width - Nq.lend
503       end
504    end
505    return g
506 end
507
508 -- change penalties (or create a new penalty, if needed)
509 local function handle_penalty_normal(post, pre, g)
510    local a = (pre or 0) + (post or 0)
511    if #Bp == 0 then
512       if (a~=0 and not(g and g.id==id_kern)) or Nq.lend~=0 then
513          local p = node.new(id_penalty); copy_attr(p, Nq.nuc)
514          if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
515          p.penalty = a
516          head = insert_before(head, Np.first, p)
517          Bp[1]=p; 
518          set_attr(p, attr_icflag, KINSOKU)
519          set_attr(p, attr_uniqid, uniq_id) 
520       end
521    --else for _, v in pairs(Bp) do v.penalty = v.penalty + a end
522    else for _, v in pairs(Bp) do add_penalty(v,a) end
523    end
524 end
525
526 local function handle_penalty_always(post, pre, g)
527    local a = (pre or 0) + (post or 0)
528    if #Bp == 0 then
529       if not (g and g.id==id_glue) or Nq.lend~=0 then
530          local p = node.new(id_penalty); copy_attr(p, Nq.nuc)
531          if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
532          p.penalty = a
533          head = insert_before(head, Np.first, p)
534          Bp[1]=p
535      set_attr(p, attr_icflag, KINSOKU)
536      set_attr(p, attr_uniqid, uniq_id) 
537       end
538    else for _, v in pairs(Bp) do add_penalty(v,a) end
539    end
540 end
541
542 local function handle_penalty_suppress(post, pre, g)
543    local a = (pre or 0) + (post or 0)
544    if #Bp == 0 then
545       if g and g.id==id_glue then
546          local p = node.new(id_penalty); copy_attr(p, Nq.nuc)
547          p.penalty = 10000; head = insert_before(head, Np.first, p)
548          Bp[1]=p
549      set_attr(p, attr_icflag, KINSOKU)
550      set_attr(p, attr_uniqid, uniq_id) 
551       end
552    else for _, v in pairs(Bp) do add_penalty(v,a) end
553    end
554 end
555
556 -- 和文文字間の JFM glue を node 化
557 local function new_jfm_glue(Nn, bc, ac)
558 -- bc, ac: char classes
559    local g = nil
560    local z = Nn.met.size_cache.char_type[bc]
561    local gt = z.glue[ac]
562    if gt then
563       local h = node.new(id_glue_spec)
564       h.width, h.stretch, h.shrink = gt[1], gt[2], gt[3]
565       h.stretch_order, h.shrink_order = 0, 0
566       g = node.new(id_glue); copy_attr(g, Nn.nuc)
567       g.subtype = 0; g.spec = h
568       set_attr(g, attr_icflag, FROM_JFM); set_attr(g, attr_uniqid, uniq_id)
569    elseif z.kern[ac] then
570       g = node.new(id_kern); copy_attr(g, Nn.nuc)
571       g.subtype = 1; g.kern = z.kern[ac]
572       set_attr(g, attr_icflag, FROM_JFM); set_attr(g, attr_uniqid, uniq_id)
573    end
574    return g
575 end
576
577 -- Nq.last (kern w) .... (glue/kern g) Np.first
578 local function real_insert(w, g)
579    if w~=0 then
580       local h = node.new(id_kern); copy_attr(h, Nq.nuc)
581       set_attr(h, attr_icflag, LINE_END)
582       set_attr(h, attr_uniqid, uniq_id) 
583       h.kern = w; h.subtype = 1
584       head = node.insert_after(head, Nq.last, h)
585    end
586    if g then
587       head = insert_before(head, Np.first, g)
588       Np.first = g
589    end
590 end
591
592 -------------------- 和文文字間空白量の決定
593
594 -- get kanjiskip
595 local function get_kanji_skip_from_jfm(Nn)
596    local i = Nn.met.size_cache.kanjiskip
597    if i then
598       return { i[1], i[2], i[3] }
599    else return nil
600    end
601 end
602 local function get_kanjiskip()
603    local g = node.new(id_glue); copy_attr(g, Nq.nuc)
604    if Np.auto_kspc or Nq.auto_kspc then
605       if kanji_skip.width == max_dimen then
606          local gx = node.new(id_glue_spec);
607          gx.stretch_order = 0; gx.shrink_order = 0
608          local bk = get_kanji_skip_from_jfm(Nq)
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 = get_kanji_skip_from_jfm(Np)
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 node.free(gx); gx = get_zero_spec() -- fallback
626          end
627          g.spec = gx
628       else g.spec=node.copy(kanji_skip) end
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 function get_xkanji_skip_from_jfm(Nn)
687    local i = Nn.met.size_cache.xkanjiskip
688    if i then
689       return i and { i[1], i[2], i[3] }
690    else return nil
691    end
692 end
693 local function get_xkanjiskip(Nn)
694    local g = node.new(id_glue); copy_attr(g, Nn.nuc)
695    if Nq.xspc_after and Np.xspc_before and (Nq.auto_xspc or Np.auto_xspc) then
696       if xkanji_skip.width == max_dimen then
697          local gx = node.new(id_glue_spec);
698          gx.stretch_order = 0; gx.shrink_order = 0
699          local bk = get_xkanji_skip_from_jfm(Nn)
700          if bk then
701             gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
702          else node.free(gx); gx = get_zero_spec() -- fallback
703          end
704          g.spec = gx
705       else g.spec=node.copy(xkanji_skip) end
706    else
707       g.spec = get_zero_spec()
708    end
709    set_attr(g, attr_icflag, XKANJI_SKIP)
710    set_attr(g, attr_uniqid, uniq_id) 
711    return g
712 end
713
714
715 -------------------- 隣接した「塊」間の処理
716
717 local function get_OA_skip()
718    if not ihb_flag then
719       return new_jfm_glue(Np, fast_find_char_class(((Nq.id == id_math and -1) or 'jcharbdd'), Np.met), Np.class)
720    else return nil
721    end
722 end
723 local function get_OB_skip()
724    if not ihb_flag then
725       return new_jfm_glue(Nq, Nq.class, fast_find_char_class(((Np.id == id_math and -1) or'jcharbdd'), Nq.met))
726    else return nil
727    end
728 end
729
730 -- (anything) .. jachar
731 local function handle_np_jachar(mode)
732    if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id==id_pbox_w) and Nq.met) then 
733       local g = lineend_fix(calc_ja_ja_glue() or get_kanjiskip()) -- M->K
734       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
735    elseif Nq.met then  -- Nq.id==id_hlist
736       local g = get_OA_skip() or get_kanjiskip() -- O_A->K
737       handle_penalty_normal(0, Np.pre, g); real_insert(0, g)
738    elseif Nq.pre then 
739       local g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
740       if Nq.id==id_hlist then Nq.post = 0 end
741       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
742    else
743       local g = get_OA_skip() -- O_A
744       if Nq.id==id_glue then handle_penalty_normal(0, Np.pre, g)
745       elseif Nq.id==id_kern then handle_penalty_suppress(0, Np.pre, g)
746       else handle_penalty_always(0, Np.pre, g)
747       end
748       real_insert(0, g)
749    end
750    -- \jcharwidowpenalty 挿入予定箇所更新
751    if mode and ltjs_get_penalty_table('kcat', Np.char, 0, box_stack_level)%2~=1 then
752       widow_Np.first, widow_Bp, Bp = Np.first, Bp, widow_Bp
753    end
754 end
755
756 -- jachar .. (anything)
757 local function handle_nq_jachar()
758     if Np.pre then 
759       if Np.id==id_hlist then Np.pre = 0 end
760       local g = lineend_fix(get_OB_skip() or get_xkanjiskip(Nq)) -- O_B->X
761       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
762    else
763       local g = lineend_fix(get_OB_skip()) -- O_B
764       if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
765       elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
766       else handle_penalty_always(Nq.post, 0, g)
767       end
768       real_insert(Nq.lend, g)
769    end
770 end
771
772 -- (anything) .. (和文文字で始まる hlist)
773 local function handle_np_ja_hlist()
774    if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id == id_pbox_w) and Nq.met) then 
775       local g = lineend_fix(get_OB_skip() or get_kanjiskip()) -- O_B->K
776       handle_penalty_normal(Nq.post, 0, g); real_insert(Nq.lend, g)
777    elseif Nq.met then  -- Nq.id==id_hlist
778       local g = get_kanjiskip() -- K
779       handle_penalty_suppress(0, 0, g); real_insert(0, g)
780    elseif Nq.pre then 
781       local g = get_xkanjiskip(Np) -- X
782       handle_penalty_suppress(0, 0, g); real_insert(0, g)
783    end
784 end
785
786 -- (和文文字で終わる hlist) .. (anything)
787 local function handle_nq_ja_hlist()
788    if Np.pre then 
789       local g = get_xkanjiskip(Nq) -- X
790       handle_penalty_suppress(0, 0, g); real_insert(0, g)
791    end
792 end
793
794 -- Nq が前側のクラスタとなることによる修正
795 local function adjust_nq()
796    if Nq.id==id_glyph then after_alchar(Nq)
797    elseif Nq.id==id_hlist or Nq.id==id_pbox or Nq.id==id_disc then after_hlist(Nq)
798    elseif Nq.id == id_pbox_w then 
799       luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
800                                false, Nq, Np, box_stack_level)
801    end
802 end
803
804 -------------------- 開始・終了時の処理
805
806 -- リスト末尾の処理
807 local function handle_list_tail(mode)
808    adjust_nq(); Np = Nq
809    if mode then
810       -- the current list is to be line-breaked:
811       if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then 
812          if Np.lend~=0 then
813             g = node.new(id_kern); g.subtype = 0; g.kern = Np.lend
814             copy_attr(g, Np.nuc); set_attr(g, attr_icflag, BOXBDD)
815             node.insert_after(head, Np.last, g)
816          end
817       end
818       -- Insert \jcharwidowpenalty
819       Bp = widow_Bp; Np = widow_Np; Nq.lend = 0
820       if Np.first then
821          handle_penalty_normal(0,
822                                ltjs_get_penalty_table('jwp', 0, 0, box_stack_level))
823       end
824    else
825       -- the current list is the contents of a hbox
826       if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then 
827          local g = new_jfm_glue(Np, Np.class, fast_find_char_class('boxbdd',Np.met))
828          if g then
829             set_attr(g, attr_icflag, BOXBDD)
830             head = node.insert_after(head, Np.last, g)
831          end
832       end
833    end
834 end
835
836 -- リスト先頭の処理
837 local function handle_list_head(par_indented)
838    if Np.id ==  id_jglyph or (Np.id==id_pbox and Np.met) then 
839       if not ihb_flag then
840          local g
841          if par_indented then
842             g = new_jfm_glue(Np, fast_find_char_class('parbdd',Np.met), Np.class)
843          else
844             g = new_jfm_glue(Np, fast_find_char_class('boxbdd',Np.met), Np.class)
845          end
846          if g then
847             set_attr(g, attr_icflag, BOXBDD)
848             if g.id==id_glue and #Bp==0 then
849                local h = node.new(id_penalty); copy_attr(h, Np.nuc)
850                h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
851             end
852             head = insert_before(head, Np.first, g)
853          end
854       end
855    end
856 end
857
858 -- initialize
859 -- return value: (the initial cursor lp), (last node)
860 local function init_var(mode)
861    uniq_id = uniq_id +1
862    if uniq_id == 0x7FFFFFF then uniq_id = 0 end
863    Bp = {}; widow_Bp = {}; widow_Np = {first = nil}
864    box_stack_level = ltjp.box_stack_level
865    kanji_skip=skip_table_to_spec('kanjiskip')
866    xkanji_skip=skip_table_to_spec('xkanjiskip')
867    Np = {
868       auto_kspc=nil, auto_xspc=nil, char=nil, class=nil, 
869       first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil, 
870       post=nil, pre=nil, xspc_after=nil, xspc_before=nil, 
871    }
872    Nq = {
873       auto_kspc=nil, auto_xspc=nil, char=nil, class=nil, 
874       first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil, 
875       post=nil, pre=nil, xspc_after=nil, xspc_before=nil, 
876    }
877    if mode then 
878       -- the current list is to be line-breaked:
879       -- hbox from \parindent is skipped.
880       local lp, par_indented  = head, false
881       while lp and ((lp.id==id_whatsit and lp.subtype~=sid_user) 
882                  or ((lp.id==id_hlist) and (lp.subtype==3))) do
883          if (lp.id==id_hlist) and (lp.subtype==3) then par_indented = true end
884          lp=node_next(lp) end
885      return lp, node.tail(head), par_indented
886    else 
887       -- the current list is the contents of a hbox:
888       -- insert a sentinelEG
889       local g = node.new(id_kern)
890       node.insert_after(head, node.tail(head), g); last = g
891       return head, g, false
892    end
893 end
894
895 local function cleanup(mode, last)
896    -- adjust attr_icflag for avoiding error
897    tex.setattribute('global', attr_icflag, 0)
898    node.free(kanji_skip); node.free(xkanji_skip)
899    if mode then
900       local h = node_next(head)
901       if h.id == id_penalty and h.penalty == 10000 then
902          h = h.next
903          if h.id == id_glue and h.subtype == 15 and not h.next then
904             return false
905          end
906       end
907       return head
908    else
909       head = node.remove(head, last); node.free(last);-- remove the sentinel
910       return head
911    end
912 end
913 -------------------- 外部から呼ばれる関数
914
915 -- main interface
916 function main(ahead, mode)
917    if not ahead then return ahead end
918    head = ahead;
919    local lp, last, par_indented = init_var(mode); 
920    lp = calc_np(lp, last)
921    if Np then 
922       extract_np(); handle_list_head(par_indented)
923    else
924       return cleanup(mode, last)
925    end
926    lp = calc_np(lp, last)
927    while Np do
928       extract_np(); adjust_nq()
929       -- 挿入部
930       if Np.id == id_jglyph then 
931          handle_np_jachar(mode)
932       elseif Np.met then 
933          if Np.id==id_hlist then handle_np_ja_hlist()
934          else handle_np_jachar() end
935       elseif Nq.met then 
936          if Nq.id==id_hlist then handle_nq_ja_hlist()
937          else handle_nq_jachar() end
938       end
939       lp = calc_np(lp, last)
940    end
941    handle_list_tail(mode)
942    return cleanup(mode, last)
943 end
944
945 -- \inhibitglue
946
947 function create_inhibitglue_node()
948    local tn = node.new(id_whatsit, sid_user)
949    tn.user_id=30111; tn.type=100; tn.value=1
950    node.write(tn)
951 end
952
953 -- Node for indicating beginning of a paragraph
954 -- (for ltjsclasses)
955 function create_beginpar_node()
956    local tn = node.new(id_whatsit, sid_user)
957    tn.user_id=30114; tn.type=100; tn.value=1
958    node.write(tn)
959 end
960
961 do
962
963 local function whatsit_callback(Np, lp, Nq, bsl)
964    if Np and Np.nuc then return Np 
965    elseif Np and lp.user_id == 30114 then
966       Np.first = lp; Np.nuc = lp; Np.last = lp
967       Np.char = 'parbdd'
968       Np.met = nil
969       Np.pre = 0; Np.post = 0
970       Np.xspc_before = false
971       Np.xspc_after  = false
972       Np.auto_xspc = false
973       return Np
974    end
975 end
976 local function whatsit_after_callback(s, Nq, Np, bsl)
977    if not s and Nq.nuc.user_id == 30114 then
978       local x, y = node.prev(Nq.nuc), Nq.nuc
979       Nq.first, Nq.nuc, Nq.last = x, x, x
980       head = node.remove(head, y)
981    end
982    return s
983 end
984
985 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
986                            "luatexja.beginpar.np_info", 1)
987 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
988                            "luatexja.beginpar.np_info_after", 1)
989
990 end