OSDN Git Service

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