OSDN Git Service

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