OSDN Git Service

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