OSDN Git Service

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