OSDN Git Service

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