OSDN Git Service

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