OSDN Git Service

0659fd01dd26868f9ecc4eb236e545306da81705
[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 table_insert = table.insert
32 local uniq_id = 0 -- unique id 
33
34 local id_glyph = node.id('glyph')
35 local id_hlist = node.id('hlist')
36 local id_vlist = node.id('vlist')
37 local id_rule = node.id('rule')
38 local id_ins = node.id('ins')
39 local id_mark = node.id('mark')
40 local id_adjust = node.id('adjust')
41 local id_disc = node.id('disc')
42 local id_whatsit = node.id('whatsit')
43 local id_math = node.id('math')
44 local id_glue = node.id('glue')
45 local id_kern = node.id('kern')
46 local id_penalty = node.id('penalty')
47
48 local id_glue_spec = node.id('glue_spec')
49 local id_jglyph = node.id('glyph') + 256      -- Japanese character
50 local id_box_like = node.id('hlist') + 256    -- vbox, shifted hbox
51 local id_pbox = node.id('hlist') + 512        -- already processed nodes (by \unhbox)
52 local id_pbox_w = node.id('hlist') + 513      -- cluster which consists of a whatsit
53 local sid_user = node.subtype('user_defined')
54
55 local sid_start_link = node.subtype('pdf_start_link')
56 local sid_start_thread = node.subtype('pdf_start_thread')
57 local sid_end_link = node.subtype('pdf_end_link')
58 local sid_end_thread = node.subtype('pdf_end_thread')
59
60 local ITALIC = 1
61 local PACKED = 2
62 local KINSOKU = 3
63 local FROM_JFM = 4
64 local LINE_END = 5
65 local KANJI_SKIP = 6
66 local XKANJI_SKIP = 7
67 local PROCESSED = 8
68 local IC_PROCESSED = 9
69 local BOXBDD = 15
70
71 local kanji_skip
72 local xkanji_skip
73
74 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
75 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
76 local attr_icflag = luatexbase.attributes['ltj@icflag']
77 local attr_autospc = luatexbase.attributes['ltj@autospc']
78 local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
79 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
80 local max_dimen = 1073741823
81
82 local ltjs_get_penalty_table = ltjs.get_penalty_table
83 local ltjs_get_skip_table = ltjs.get_skip_table
84 local ltjf_find_char_class = ltjf.find_char_class
85 local ltjf_font_metric_table = ltjf.font_metric_table
86 local ltjf_metrics = ltjf.metrics
87 local box_stack_level
88 local par_indented -- is the paragraph indented?
89
90 -------------------- Helper functions
91
92 -- This function is called only for acquiring `special' characters.
93 local function fast_find_char_class(c,m)
94    return m.chars[c] or 0
95 end
96
97 local spec_zero_glue = node_new(id_glue_spec)
98    spec_zero_glue.width = 0; spec_zero_glue.stretch_order = 0; spec_zero_glue.stretch = 0
99    spec_zero_glue.shrink_order = 0; spec_zero_glue.shrink = 0
100
101 local function get_zero_spec()
102    return node_copy(spec_zero_glue)
103 end
104
105 local function skip_table_to_spec(n)
106    local g = node_new(id_glue_spec)
107    local st = ltjs_get_skip_table(n, box_stack_level)
108    g.width = st.width; g.stretch = st.stretch; g.shrink = st.shrink
109    g.stretch_order = st.stretch_order; g.shrink_order = st.shrink_order
110    return g
111 end
112
113 -- penalty 値の計算
114 local function add_penalty(p,e)
115    if p.penalty>=10000 then
116       if e<=-10000 then p.penalty = 0 end
117    elseif p.penalty<=-10000 then
118       if e>=10000 then p.penalty = 0 end
119    else
120       p.penalty = p.penalty + e
121       if p.penalty>=10000 then p.penalty = 10000
122       elseif p.penalty<=-10000 then p.penalty = -10000 end
123    end
124    return
125 end
126
127 -- 「異なる JFM」の間の調整方法
128 diffmet_rule = math.two_average
129 function math.two_add(a,b) return a+b end
130 function math.two_average(a,b) return (a+b)/2 end
131
132 -------------------- idea
133 -- 2 node の間に glue/kern/penalty を挿入する.
134 -- 基本方針: char node q と char node p の間
135
136 --  Np: 「p を核とする塊」
137 --   first: 最初の node,nuc: p,last: 最後の node
138 --   id: 核 node の種類
139 --  Nq: 「q を核とする塊」
140 --   実際の glue は Np.last, Nq.first の間に挿入される
141 --  Bp: Np.last, Nq.first の間の penalty node 達の配列
142
143 -- Np, Nq, Bp, widow_Bp について
144 -- Np, Nq は別々のテーブル.
145 -- 1回のループごとに Nq = Np, Np = (new table) となるのは効率が悪いので,
146 -- Np <-> Nq 入れ替え,その後 Np をクリアすることでテーブルを再利用.
147 -- 同様の関係は Bp, widow_Bp にも.
148
149
150 -- 核の定義:
151 --  node x が non-char node のときは,x のみ
152 --  x が char_node のときは,
153 --  - x が \accent の第二引数だったとき
154 --    [kern2 kern y kern2] x の 3 node が核に加わる
155 --  - x の直後に \/ 由来 kern があったとき
156 --    その \/ 由来の kern が核に加わる
157 -- p, q の走査で無視するもの:
158 --  ins, mark, adjust, whatsit, penalty
159 --
160 -- Nq.last .. + .. Bp.first .... Bp[last] .... * .. Np.first
161 -- +: kern from LINEEND はここに入る
162 -- *: jfm glue はここに入る
163
164 local head -- the head of current list
165 local last -- the last node of current list
166 local lp   -- 外側での list 走査時のカーソル
167
168 local Np, Nq, Bp
169 local widow_Bp, widow_Np -- \jcharwidowpenalty 挿入位置管理用
170
171 local ihb_flag -- JFM グルー挿入抑止用 flag
172                -- on: \inhibitglue 指定時,hlist の周囲
173
174 -------------------- hlist 内の文字の検索
175
176 local first_char, last_char, find_first_char
177
178 local function check_box(box_ptr, box_end)
179    local p = box_ptr; local found_visible_node = false
180    if not p then 
181       find_first_char = false; first_char = nil; last_char = nil
182       return true
183    end
184    while p and p~=box_end do
185       local pid = p.id
186       if pid==id_kern and p.subtype==2 then
187             p = node_next(node_next(node_next(p))); pid = p.id -- p must be glyph_node
188       end
189       if pid==id_glyph then
190          repeat 
191             if find_first_char then 
192                first_char = p; find_first_char = false
193             end
194             last_char = p; found_visible_node = true; p=node_next(p)
195             if (not p) or p==box_end then return found_visible_node end
196          until p.id~=id_glyph
197          pid = p.id -- p must be non-nil
198       end
199       if pid==id_kern and has_attr(p, attr_icflag)==IC_PROCESSED then
200          p = node_next(p); 
201       elseif 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    local a = has_attr(p, attr_icflag) or 0
263    if a<= 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    check_next_ickern()
287 end
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 lpi, lpa, Nr
409    Nr = Nq; for k in pairs(Nr) do Nr[k] = nil end
410    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       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 -- extract informations from Np
427 -- We think that "Np is a Japanese character" if Np.met~=nil,
428 --            "Np is an alphabetic character" if Np.pre~=nil,
429 --            "Np is not a character" otherwise.
430
431 -- 和文文字のデータを取得
432 function set_np_xspc_jachar(Nx, x)
433    local z = ltjf_font_metric_table[x.font]
434    local c = x.char
435    local cls = ltjf_find_char_class(c, z)
436    local m = ltjf_metrics[z.jfm]
437    set_attr(x, attr_jchar_class, cls)
438    Nx.class = cls
439    Nx.char = c
440    Nx.size= z.size
441    Nx.met = m
442    Nx.var = z.var
443    Nx.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
444    Nx.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
445    z = fast_find_char_class('lineend', m)
446    local y = m.size_cache[Nx.size].char_type[Nx.class]
447    if y.kern and y.kern[z] then 
448       Nx.lend = y.kern[z]
449    else 
450       Nx.lend = 0 
451    end
452    y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
453    Nx.xspc_before = (y%2==1)
454    Nx.xspc_after  = (y>=2)
455    Nx.auto_kspc = (has_attr(x, attr_autospc)==1)
456    Nx.auto_xspc = (has_attr(x, attr_autoxspc)==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/2)%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/2)%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_autoxspc)==1)
484 end
485
486 -- Np の情報取得メインルーチン
487 local function extract_np()
488    local x = Np.nuc;
489    if Np.id ==  id_jglyph then set_np_xspc_jachar(Np, x)
490    elseif Np.id == id_glyph then set_np_xspc_alchar(Np, x.char, x, ligature_head)
491    elseif Np.id == id_hlist then Np.last_char = check_box_high(Np, x.head, nil)
492    elseif Np.id == id_pbox then Np.last_char = check_box_high(Np, Np.first, node.next(Np.last))
493    elseif Np.id == id_disc then Np.last_char = check_box_high(Np, x.replace, nil)
494    elseif Np.id == id_math then 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    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      table_insert(Bp, 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          table_insert(Bp, 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          table_insert(Bp, 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 and 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)
596       g.subtype = 0; g.spec = h
597    elseif z.kern and z.kern[ac] then
598       g = node_new(id_kern)
599       g.subtype = 1; g.kern = z.kern[ac]
600    end
601    if g then 
602       set_attr(g, attr_icflag, FROM_JFM); set_attr(g, attr_uniqid, uniq_id) 
603    end
604    return g
605 end
606
607 -- Nq.last (kern w) .... (glue/kern g) Np.first
608 local function real_insert(w, g)
609    if w~=0 then
610       local h = node_new(id_kern)
611       set_attr(h, attr_icflag, LINE_END)
612       set_attr(h, attr_uniqid, uniq_id) 
613       h.kern = w; h.subtype = 1
614       head = node_insert_after(head, Nq.last, h)
615    end
616    if g then
617       head = node_insert_before(head, Np.first, g)
618       Np.first = g
619    end
620 end
621
622 -------------------- 和文文字間空白量の決定
623
624 -- get kanjiskip
625 local function get_kanji_skip_from_jfm(Nn)
626    local i = Nn.met.size_cache[Nn.size].kanjiskip
627    if i then
628       return { i[1], i[2], i[3] }
629    else return nil
630    end
631 end
632 local function get_kanjiskip()
633    local g = node_new(id_glue)
634    if Np.auto_kspc or Nq.auto_kspc then
635       if kanji_skip.width == max_dimen then
636          local gx = node_new(id_glue_spec);
637          gx.stretch_order = 0; gx.shrink_order = 0
638          local bk = get_kanji_skip_from_jfm(Nq)
639          local ak
640          if (Np.met==Nq.met) and (Nq.size==Np.size) and (Nq.var==Np.var) then
641             ak = nil
642          else
643             ak = get_kanji_skip_from_jfm(Np)
644          end
645          if bk then
646             if ak then
647                gx.width = round(diffmet_rule(bk[1], ak[1]))
648                gx.stretch = round(diffmet_rule(bk[2], ak[2]))
649                gx.shrink = -round(diffmet_rule(-bk[3], -ak[3]))
650             else
651                gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
652             end
653          elseif ak then
654             gx.width = ak[1]; gx.stretch = ak[2]; gx.shrink = ak[3]
655          else node_free(gx); gx = get_zero_spec() -- fallback
656          end
657          g.spec = gx
658       else g.spec=node_copy(kanji_skip); node_free(gx) end
659    else
660       g.spec =  get_zero_spec(); node_free(gx)
661    end
662    set_attr(g, attr_icflag, KANJI_SKIP)
663    set_attr(g, attr_uniqid, uniq_id) 
664    return g
665 end
666
667 local function calc_ja_ja_aux(gb,ga)
668    if not gb then 
669       return ga
670    else
671       if not ga then return gb end
672       local k = node.type(gb.id) .. node.type(ga.id)
673       if k == 'glueglue' then 
674          -- 両方とも glue.
675          gb.spec.width   = round(diffmet_rule(gb.spec.width, ga.spec.width))
676          gb.spec.stretch = round(diffmet_rule(gb.spec.stretch,ga.spec.shrink))
677          gb.spec.shrink  = -round(diffmet_rule(-gb.spec.shrink, -ga.spec.shrink))
678          node_free(ga)
679          return gb
680       elseif k == 'kernkern' then
681          -- 両方とも kern.
682          gb.kern = round(diffmet_rule(gb.kern, ga.kern))
683          node_free(ga)
684          return gb
685       elseif k == 'kernglue' then 
686          -- gb: kern, ga: glue
687          ga.spec.width   = round(diffmet_rule(gb.kern,ga.spec.width))
688          ga.spec.stretch = round(diffmet_rule(ga.spec.stretch, 0))
689          ga.spec.shrink  = -round(diffmet_rule(-ga.spec.shrink, 0))
690          node_free(gb)
691          return ga
692       else
693          -- gb: glue, ga: kern
694          gb.spec.width   = round(diffmet_rule(ga.kern, gb.spec.width))
695          gb.spec.stretch = round(diffmet_rule(gb.spec.stretch, 0))
696          gb.spec.shrink  = -round(diffmet_rule(-gb.spec.shrink, 0))
697          node_free(ga)
698          return gb
699       end
700    end
701 end
702
703 local function calc_ja_ja_glue()
704    if  ihb_flag then return nil
705    elseif (Nq.size==Np.size) and (Nq.met==Np.met) and (Nq.var==Np.var) then
706       return new_jfm_glue(Nq, Nq.class, Np.class)
707    else
708       local g = new_jfm_glue(Nq, Nq.class,
709                              fast_find_char_class('diffmet',Nq.met))
710       local h = new_jfm_glue(Np, fast_find_char_class('diffmet',Np.met),
711                              Np.class)
712       return calc_ja_ja_aux(g,h)
713    end
714 end
715
716 -------------------- 和欧文間空白量の決定
717
718 -- get xkanjiskip
719 local function get_xkanji_skip_from_jfm(Nn)
720    local i = Nn.met.size_cache[Nn.size].xkanjiskip
721    if i then
722       return { i[1], i[2], i[3] }
723    else return nil
724    end
725 end
726 local function get_xkanjiskip(Nn)
727    local g = node_new(id_glue)
728    if Nq.xspc_after and Np.xspc_before and (Nq.auto_xspc or Np.auto_xspc) then
729       if xkanji_skip.width == max_dimen then
730          local gx = node_new(id_glue_spec);
731          gx.stretch_order = 0; gx.shrink_order = 0
732          local bk = get_xkanji_skip_from_jfm(Nn)
733          if bk then
734             gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
735          else node_free(gx); gx = get_zero_spec() -- fallback
736          end
737          g.spec = gx
738       else g.spec=node_copy(xkanji_skip) end
739    else
740       g.spec = get_zero_spec()
741    end
742    set_attr(g, attr_icflag, XKANJI_SKIP)
743    set_attr(g, attr_uniqid, uniq_id) 
744    return g
745 end
746
747
748 -------------------- 隣接した「塊」間の処理
749
750 local function get_OA_skip()
751    if not ihb_flag then
752       local c = Nq.char or 'jcharbdd'
753       return new_jfm_glue(Np, fast_find_char_class(c,Np.met), Np.class)
754    else return nil
755    end
756 end
757 local function get_OB_skip()
758    if not ihb_flag then
759       local c = Np.char or 'jcharbdd'
760       return new_jfm_glue(Nq, Nq.class, fast_find_char_class(c,Nq.met))
761    else return nil
762    end
763 end
764
765 -- (anything) .. jachar
766 local function handle_np_jachar()
767    local g
768    if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id==id_pbox_w) and Nq.met) then 
769       g = calc_ja_ja_glue() or get_kanjiskip() -- M->K
770       g = lineend_fix(g)
771       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
772    elseif Nq.met then  -- Nq.id==id_hlist
773       g = get_OA_skip() or get_kanjiskip() -- O_A->K
774       handle_penalty_normal(0, Np.pre, g); real_insert(0, g)
775    elseif Nq.pre then 
776       g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
777       if Nq.id==id_hlist then Nq.post = 0 end
778       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
779    else
780       g = get_OA_skip() -- O_A
781       if Nq.id==id_glue then handle_penalty_normal(0, Np.pre, g)
782       elseif Nq.id==id_kern then handle_penalty_suppress(0, Np.pre, g)
783       else handle_penalty_always(0, Np.pre, g)
784       end
785       real_insert(0, g)
786    end
787    -- \jcharwidowpenalty 挿入予定箇所更新
788    if mode and ltjs_get_penalty_table('kcat', Np.char, 0, box_stack_level)%2~=1 then
789       widow_Np.first = Np.first; 
790       local Bpr = widow_Bp; widow_Bp = Bp; Bp = Bpr
791    end
792 end
793
794 -- jachar .. (anything)
795 local function handle_nq_jachar()
796    local g
797    if Np.pre then 
798       if Np.id==id_hlist then Np.pre = 0 end
799       g = get_OB_skip() or get_xkanjiskip(Nq) -- O_B->X
800       g = lineend_fix(g)
801       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
802    else
803       g = get_OB_skip(); g = lineend_fix(g) -- O_B
804       if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
805       elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
806       else handle_penalty_always(Nq.post, 0, g)
807       end
808       real_insert(Nq.lend, g)
809    end
810 end
811
812 -- (anything) .. (和文文字で終わる hlist)
813 local function handle_np_ja_hlist()
814    local g
815    if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id == id_pbox_w) and Nq.met) then 
816       g = get_OB_skip() or get_kanjiskip() -- O_B->K
817       g = lineend_fix(g)
818       handle_penalty_normal(Nq.post, 0, g); real_insert(Nq.lend, g)
819    elseif Nq.met then  -- Nq.id==id_hlist
820       g = get_kanjiskip() -- K
821       handle_penalty_suppress(0, 0, g); real_insert(0, g)
822    elseif Nq.pre then 
823       g = get_xkanjiskip(Np) -- X
824       handle_penalty_suppress(0, 0, g); real_insert(0, g)
825    end
826 end
827
828 -- (和文文字で終わる hlist) .. (anything)
829 local function handle_nq_ja_hlist()
830    local g = nil
831    if Np.pre then 
832       g = get_xkanjiskip(Nq) -- X
833       handle_penalty_suppress(0, 0, g); real_insert(0, g)
834    end
835 end
836
837 -- Nq が前側のクラスタとなることによる修正
838 local function adjust_nq()
839    if Nq.id==id_glyph then after_alchar(Nq)
840    elseif Nq.id==id_hlist or Nq.id==id_pbox or Nq.id==id_disc then after_hlist(Nq)
841    elseif Nq.id == id_pbox_w then 
842       luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
843                                false, Nq, Np, box_stack_level)
844    end
845 end
846
847 -------------------- 開始・終了時の処理
848
849 -- リスト末尾の処理
850 local function handle_list_tail()
851    adjust_nq(); Np = Nq
852    if mode then
853       -- the current list is to be line-breaked:
854       if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then 
855          if Np.lend~=0 then
856             g = node_new(id_kern); g.subtype = 0; g.kern = Np.lend
857             set_attr(g, attr_icflag, BOXBDD)
858             node_insert_after(head, Np.last, g)
859          end
860       end
861       -- Insert \jcharwidowpenalty
862       Bp = widow_Bp; Np = widow_Np; Nq.lend = 0
863       if Np.first then
864          handle_penalty_normal(0,
865                                ltjs_get_penalty_table('jwp', 0, 0, box_stack_level))
866       end
867    else
868       -- the current list is the contents of a hbox
869       if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then 
870          local g = new_jfm_glue(Np, Np.class, fast_find_char_class('boxbdd',Np.met))
871          if g then
872             set_attr(g, attr_icflag, BOXBDD)
873             head = node_insert_after(head, Np.last, g)
874          end
875       end
876    end
877 end
878
879 -- リスト先頭の処理
880 local function handle_list_head()
881    if Np.id ==  id_jglyph or (Np.id==id_pbox and Np.met) then 
882       if not ihb_flag then
883          local g
884          if par_indented then
885             g = new_jfm_glue(Np, fast_find_char_class('parbdd',Np.met), Np.class)
886          else
887             g = new_jfm_glue(Np, fast_find_char_class('boxbdd',Np.met), Np.class)
888          end
889          if g then
890             set_attr(g, attr_icflag, BOXBDD)
891             if g.id==id_glue and #Bp==0 then
892                local h = node_new(id_penalty)
893                h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
894             end
895             head = node_insert_before(head, Np.first, g)
896          end
897       end
898    end
899 end
900
901 -- initialize
902 local function init_var()
903    uniq_id = uniq_id +1
904    if uniq_id == 0x7FFFFFF then uniq_id = 0 end
905    lp = head; Bp = {}; widow_Bp = {}; widow_Np = {first = nil}
906    par_indented = false 
907    box_stack_level = ltjp.box_stack_level
908    kanji_skip=skip_table_to_spec('kanjiskip')
909    xkanji_skip=skip_table_to_spec('xkanjiskip')
910    Np = {
911       auto_kspc=nil, auto_xspc=nil, char=nil, class=nil, 
912       first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil, 
913       post=nil, pre=nil, var=nil, xspc_after=nil, xspc_before=nil, 
914    }
915    Nq = {
916       auto_kspc=nil, auto_xspc=nil, char=nil, class=nil, 
917       first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil, 
918       post=nil, pre=nil, var=nil, xspc_after=nil, xspc_before=nil, 
919    }
920    if mode then 
921       -- the current list is to be line-breaked:
922       -- hbox from \parindent is skipped.
923       while lp and ((lp.id==id_whatsit and lp.subtype~=sid_user) 
924                  or ((lp.id==id_hlist) and (lp.subtype==3))) do
925          if (lp.id==id_hlist) and (lp.subtype==3) then par_indented = true end
926          lp=node_next(lp) end
927      last=node.tail(head)
928    else 
929       -- the current list is the contents of a hbox:
930       -- insert a sentinelEG
931       last=node.tail(head); local g = node_new(id_kern)
932       node_insert_after(head, last, g); last = g
933    end
934 end
935
936 local function cleanup()
937    -- adjust attr_icflag for avoiding error
938    tex.attribute[attr_icflag] = -(0x7FFFFFFF)
939    node_free(kanji_skip); node_free(xkanji_skip)
940    if mode then
941       local h = node_next(head)
942       if h.id == id_penalty and h.penalty == 10000 then
943          h = h.next
944          if h.id == id_glue and h.subtype == 15 and not h.next then
945             return false
946          end
947       end
948       return head
949    else
950       head = node_remove(head, last); node_free(last);-- remove the sentinel
951       return head
952    end
953 end
954 -------------------- 外部から呼ばれる関数
955
956 -- main interface
957 function main(ahead, amode)
958    if not ahead then return ahead end
959    head = ahead; mode = amode; init_var(); calc_np()
960    if Np then 
961       extract_np(); handle_list_head()
962    else
963       return cleanup()
964    end
965    calc_np()
966    while Np do
967       extract_np(); adjust_nq()
968      -- 挿入部
969       if Np.id == id_jglyph then 
970          handle_np_jachar()
971       elseif Np.met then 
972          if Np.id==id_hlist then handle_np_ja_hlist()
973          else handle_np_jachar() end
974       elseif Nq.met then 
975          if Nq.id==id_hlist then handle_nq_ja_hlist()
976          else handle_nq_jachar() end
977       end
978       calc_np()
979    end
980    handle_list_tail()
981    return cleanup()
982 end
983
984 -- \inhibitglue
985
986 function create_inhibitglue_node()
987    local tn = node_new(id_whatsit, sid_user)
988    tn.user_id=30111; tn.type=100; tn.value=1
989    node.write(tn)
990 end
991
992 -- Node for indicating beginning of a paragraph
993 -- (for ltjsclasses)
994 function create_beginpar_node()
995    local tn = node_new(id_whatsit, sid_user)
996    tn.user_id=30114; tn.type=100; tn.value=1
997    node.write(tn)
998 end
999
1000 local function whatsit_callback(Np, lp, Nq, bsl)
1001    if Np and Np.nuc then return Np 
1002    elseif Np and lp.user_id == 30114 then
1003       Np.first = lp; Np.nuc = lp; Np.last = lp
1004       Np.char = 'parbdd'
1005       Np.met = nil
1006       Np.pre = 0; Np.post = 0
1007       Np.xspc_before = false
1008       Np.xspc_after  = false
1009       Np.auto_xspc = false
1010       return Np
1011    end
1012 end
1013 local function whatsit_after_callback(s, Nq, Np, bsl)
1014    if not s and Nq.nuc.user_id == 30114 then
1015       local x, y = node.prev(Nq.nuc), Nq.nuc
1016       Nq.first, Nq.nuc, Nq.last = x, x, x
1017       head = node_remove(head, y)
1018    end
1019    return s
1020 end
1021
1022 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
1023                            "luatexja.beginpar.np_info", 1)
1024 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
1025                            "luatexja.beginpar.np_info_after", 1)
1026