OSDN Git Service

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