OSDN Git Service

use getFOO/setFOO helpers instead of getfield/setfield
[luatex-ja/luatexja.git] / src / ltj-jfmglue.lua
1 --
2 -- ltj-jfmglue.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.jfmglue',
6   date = '2022-08-16',
7   description = 'Insertion process of JFM glues, [x]kanjiskip and others',
8 })
9 luatexja.jfmglue = luatexja.jfmglue or {}
10
11 luatexja.load_module 'base';      local ltjb = luatexja.base
12 luatexja.load_module 'stack';     local ltjs = luatexja.stack
13 luatexja.load_module 'jfont';     local ltjf = luatexja.jfont
14 luatexja.load_module 'direction'; local ltjd = luatexja.direction
15 luatexja.load_module 'setwidth';  local ltjw = luatexja.setwidth
16 luatexja.load_module 'lotf_aux';  local ltju = luatexja.lotf_aux
17 local pairs = pairs
18
19 --local to_node = node.direct.tonode
20 --local to_direct = node.direct.todirect
21
22 local getfield = node.direct.getfield
23 local getid = node.direct.getid
24 local getfont = node.direct.getfont
25 local getlist = node.direct.getlist
26 local getchar = node.direct.getchar
27 local getglue = luatexja.getglue
28 local getsubtype = node.direct.getsubtype
29 local getshift = node.direct.getshift
30 local getwidth = node.direct.getwidth
31 local getdepth = node.direct.getdepth
32 local getpenalty = node.direct.getpenalty
33 local setfield = node.direct.setfield
34 local setglue = luatexja.setglue
35 local setshift = node.direct.setshift
36 local if_lang_ja
37 do
38     local lang_ja = luatexja.lang_ja
39     local getlang = node.direct.getlang
40     -- glyph with font number 0 (\nullfont) is always considered an ALchar node
41     if_lang_ja = function (n) return (getlang(n)==lang_ja)and(getfont(n)~=0) end
42 end
43 local setpenalty = node.direct.setpenalty
44 local setkern = node.direct.setkern
45   
46 local has_attr = node.direct.has_attribute
47 local set_attr = node.direct.set_attribute
48 local insert_before = node.direct.insert_before
49 local insert_after = node.direct.insert_after
50 local node_next = node.direct.getnext
51 local ltjd_make_dir_whatsit = ltjd.make_dir_whatsit
52 local ltjf_font_metric_table = ltjf.font_metric_table
53 local ltjf_find_char_class = ltjf.find_char_class
54 local node_new = luatexja.dnode_new
55 local node_copy = node.direct.copy
56 local node_tail = node.direct.tail
57 local node_free = node.direct.flush_node or node.direct.free
58 local node_remove = node.direct.remove
59 local node_inherit_attr = luatexja.node_inherit_attr
60
61 local id_glyph = node.id 'glyph'
62 local id_hlist = node.id 'hlist'
63 local id_vlist = node.id 'vlist'
64 local id_rule  = node.id 'rule'
65 local id_ins   = node.id 'ins'
66 local id_mark  = node.id 'mark'
67 local id_adjust = node.id 'adjust'
68 local id_disc  = node.id 'disc'
69 local id_whatsit = node.id 'whatsit'
70 local id_math  = node.id 'math'
71 local id_glue  = node.id 'glue'
72 local id_kern  = node.id 'kern'
73 local id_penalty = node.id 'penalty'
74
75 local id_jglyph    = 512 -- Japanese character
76 local id_box_like  = 256 -- vbox, shifted hbox
77 local id_pbox      = 257 -- already processed nodes (by \unhbox)
78 local id_pbox_w    = 258 -- cluster which consists of a whatsit
79 local sid_user = node.subtype 'user_defined'
80
81 local ITALIC       = luatexja.icflag_table.ITALIC
82 local PACKED       = luatexja.icflag_table.PACKED
83 local KINSOKU      = luatexja.icflag_table.KINSOKU
84 local FROM_JFM     = luatexja.icflag_table.FROM_JFM
85 local PROCESSED    = luatexja.icflag_table.PROCESSED
86 local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
87 local BOXBDD       = luatexja.icflag_table.BOXBDD
88 local SPECIAL_JAGLUE = luatexja.icflag_table.SPECIAL_JAGLUE
89 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
90
91 local attr_icflag = luatexbase.attributes['ltj@icflag']
92 local kanji_skip
93 local xkanji_skip
94 local table_current_stack
95 local list_dir
96 local capsule_glyph
97 local tex_dir
98 local attr_ablshift
99 local set_np_xspc_jachar, set_np_xspc_alchar
100 local set_np_xspc_jachar_hbox
101
102 local ltjs_orig_char_table = ltjs.orig_char_table
103
104 local function get_attr_icflag(p)
105    return (has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
106 end
107
108 -------------------- Helper functions
109
110 -- This function is called only for acquiring `special' characters.
111 local function fast_find_char_class(c,m)
112    return m.chars[c] or 0
113 end
114
115 -- 文字クラスの決定
116 local slow_find_char_class
117 do
118    local start_time_measure = ltjb.start_time_measure
119    local stop_time_measure = ltjb.stop_time_measure
120    slow_find_char_class = function (c, m, oc)
121       local cls = ltjf_find_char_class(oc, m)
122       if oc~=c and c and cls==0 then
123          return ltjf_find_char_class(c, m)
124       else
125          return cls
126       end
127    end
128 end
129
130 local function skip_table_to_glue(n)
131    local g, st = node_new(id_glue), ltjs.fast_get_stack_skip(n)
132    setglue(g, st.width, st.stretch, st.shrink, st.stretch_order, st.shrink_order)
133    return g, (st.width==1073741823)
134 end
135
136
137 -- penalty 値の計算
138 local add_penalty
139 do
140 function add_penalty(p,e)
141    local pp = getpenalty(p)
142    if (pp>-10000) and (pp<10000) then
143       if e>=10000 then       setpenalty(p, 10000)
144       elseif e<=-10000 then  setpenalty(p, -10000)
145       else
146          pp = pp + e
147          if pp>=10000 then      setpenalty(p, 10000)
148          elseif pp<=-10000 then setpenalty(p, -10000)
149          else                   setpenalty(p, pp) end
150       end
151    end
152 end
153 end
154
155 -- 「異なる JFM」の間の調整方法
156 luatexja.jfmglue.diffmet_rule = math.two_paverage
157 function math.two_add(a,b) return a+b end
158 function math.two_average(a,b) return (a+b)*0.5 end
159 function math.two_paverage(a,b) return (a+b)/2 end
160 function math.two_pleft(a,b) return a end
161 function math.two_pright(a,b) return b end
162
163 local head -- the head of current list
164
165 local Np, Nq, Bp
166 local widow_Bp, widow_Np -- \jcharwidowpenalty 挿入位置管理用
167
168 local non_ihb_flag -- JFM グルー挿入抑止用 flag
169 -- false: \inhibitglue 指定時 true: それ以外
170
171 -------------------- hlist 内の文字の検索
172
173 local first_char, last_char, find_first_char
174 local check_box_high
175 do
176 local ltjd_glyph_from_packed = ltjd.glyph_from_packed
177 local function check_box(box_ptr, box_end)
178    local p = box_ptr; local found_visible_node = false
179    if not p then
180       find_first_char = false; last_char = nil
181       return true
182    end
183    while p and p~=box_end do
184       local pid = getid(p)
185       if pid==id_kern and getsubtype(p)==2 then
186          p = node_next(node_next(node_next(p))); pid = getid(p) -- p must be glyph_node
187       end
188       if pid==id_glyph then
189          repeat
190             if find_first_char then first_char = p; find_first_char = false end
191             last_char = p; found_visible_node = true; p=node_next(p)
192             if (not p) or p==box_end then return found_visible_node end
193          until getid(p)~=id_glyph
194          pid = getid(p) -- p must be non-nil
195       end
196       if pid==id_kern then
197          local pa = get_attr_icflag(p)
198          if pa==IC_PROCESSED then
199             -- do nothing
200          elseif getsubtype(p)==2 then
201             p = node_next(node_next(p));
202             -- Note that another node_next will be executed outside this if-statement.
203          else
204             found_visible_node = true
205             find_first_char = false; last_char = nil
206          end
207       elseif pid==id_hlist then
208          if PACKED == get_attr_icflag(p) then
209             local s = ltjd_glyph_from_packed(p)
210             if find_first_char then first_char = s; find_first_char = false end
211             last_char = s; found_visible_node = true
212          else
213             if getshift(p)==0 then
214                last_char = nil
215                if check_box(getlist(p), nil) then found_visible_node = true end
216                find_first_char = false
217             else
218                find_first_char = false; last_char = nil
219             end
220          end
221       elseif pid==id_math then
222          if find_first_char then first_char = p; find_first_char = false end
223          last_char = p; found_visible_node = true
224       elseif pid==id_rule and get_attr_icflag(p)==PACKED then
225          -- do nothing
226       elseif not (pid==id_ins   or pid==id_mark
227                   or pid==id_adjust or pid==id_whatsit
228                   or pid==id_penalty) then
229          found_visible_node = true
230          find_first_char = false; last_char = nil
231       end
232       p = node_next(p)
233    end
234    return found_visible_node
235 end
236
237 check_box_high = function (Nx, box_ptr, box_end)
238    first_char = nil;  last_char = nil;  find_first_char = true
239    if check_box(box_ptr, box_end) then
240       local first_char = first_char
241       if first_char then
242          if getid(first_char)==id_glyph then
243             if if_lang_ja(first_char) then
244                set_np_xspc_jachar_hbox(Nx, first_char)
245             else
246                set_np_xspc_alchar(Nx, getchar(first_char),first_char, 1)
247             end
248          else -- math_node
249             set_np_xspc_alchar(Nx, -1,first_char)
250          end
251       end
252    end
253    return last_char
254 end
255 end
256 -------------------- Np の計算と情報取得
257
258 luatexbase.create_callback("luatexja.jfmglue.whatsit_getinfo", "data",
259                            function (Np, lp, Nq)
260                               if Np.nuc then return Np
261                               else
262                                  return Np  -- your code
263                               end
264                            end)
265 luatexbase.create_callback("luatexja.jfmglue.whatsit_after", "data",
266                            function (stat, Nq, Np) return false end)
267 luatexbase.create_callback("luatexja.jfmglue.whatsit_last_minute", "data",
268                            function (stat, Nq, Np) return false end)
269
270 -- calc next Np
271 local calc_np 
272 do -- 001 -----------------------------------------------
273
274 local traverse = node.direct.traverse
275 local function check_next_ickern(lp)
276    local lx = Np.nuc
277    while lp and getid(lp) == id_kern and ( getsubtype(lp)==0 or 
278      getsubtype(lp)==3 or ITALIC == get_attr_icflag(lp)) do
279      set_attr(lp, attr_icflag, IC_PROCESSED)
280      lx, lp = lp, node_next(lp)
281    end
282    Np.last = lx; return lp
283 end
284
285 local function calc_np_pbox(lp, last)
286    local first, nc = (not Np.first), nil
287    --local lpa = get_attr_icflag(lp)==PACKED and PACKED or KINSOKU -- KINSOKU: dummy
288    local lpa = get_attr_icflag(lp)
289    Np.first = Np.first or lp; Np.id = id_pbox
290    set_attr(lp, attr_icflag, get_attr_icflag(lp))
291    while lp ~=last and (lpa>=PACKED) and (lpa<BOXBDD) do
292       local lpi = getid(lp)
293       if lpa==PACKED then
294          if lpi==id_rule then lp = node_next(lp) end
295          nc, lp = lp, node_next(lp)
296       elseif lpi==id_hlist or lpi==id_vlist then
297          head, lp, nc = ltjd_make_dir_whatsit(head, lp, list_dir, 'jfm pbox')
298          Np.first = first and nc or Np.first
299       else
300          nc, lp = lp, node_next(lp)
301       end
302       first, lpa = false, (lp and has_attr(lp, attr_icflag) or 0)
303      -- get_attr_icflag() ではいけない!
304    end
305    Np.nuc = nc
306    lp = check_next_ickern(lp)
307    Np.last_char = check_box_high(Np, Np.first, lp)
308    return lp
309 end
310
311 local calc_np_aux_glyph_common
312 do -- 002 ---------------------------------------
313    local min, max = math.min, math.max
314    local getwhd = node.direct.getwhd
315    local attr_jchar_class = luatexbase.attributes['ltj@charclass']
316    local attr_jchar_code = luatexbase.attributes['ltj@charcode']
317    local font_getfont = font.getfont
318    local setwhd = node.direct.setwhd
319    local setdir = node.direct.setdir
320    local function calc_np_notdef(lp)
321       if not font_getfont(getfont(lp)).characters[getchar(lp)] then
322          local ln = node_next(lp)
323          if ltju.specified_feature(getfont(lp), 'notdef') and ln and getid(ln)==id_glyph then 
324             set_attr(lp, attr_icflag, PROCESSED)
325             set_attr(ln, attr_jchar_code, has_attr(lp, attr_jchar_code) or getchar(lp))
326             set_attr(ln, attr_jchar_class, has_attr(lp, attr_jchar_class) or 0)
327             Np.nuc, lp = ln, ln
328          end
329       end
330       return lp
331    end 
332 function calc_np_aux_glyph_common(lp, acc_flag)
333    Np.nuc, Np.first = lp, (Np.first or lp)
334    if if_lang_ja(lp) then -- JAchar
335       Np.id = id_jglyph
336       local m, mc, cls = set_np_xspc_jachar(Np, lp)
337       local npi, npf
338       local w, h, d = getwhd(lp)
339       if w==0 and h==0 and d==0 then lp = calc_np_notdef(lp) end
340       lp, head, npi, npf = capsule_glyph(lp, m, mc[cls], head, tex_dir)
341       Np.first = (Np.first~=Np.nuc) and Np.first or npf or npi
342       Np.nuc = npi
343       return true, check_next_ickern(lp);
344    else --ALchar
345       Np.id = id_glyph
346       set_np_xspc_alchar(Np, getchar(lp), lp, 1)
347       -- loop
348       local first_glyph, last_glyph = lp
349       set_attr(lp, attr_icflag, PROCESSED); Np.last = lp
350       local y_adjust = has_attr(lp,attr_ablshift) or 0
351       local node_depth = getdepth(lp) + min(y_adjust, 0)
352       local adj_depth = (y_adjust>0) and (getdepth(lp) + y_adjust) or 0
353       setfield(lp, 'yoffset', getfield(lp, 'yoffset') - y_adjust); lp = node_next(lp)
354       local lx=lp
355       while lx do
356          local lai = get_attr_icflag(lx)
357          if lx==last or  lai>=PACKED then break
358          else
359             local lid = getid(lx)
360             if lid==id_glyph and not if_lang_ja(lx) then
361                -- 欧文文字
362                last_glyph = lx; set_attr(lx, attr_icflag, PROCESSED); Np.last = lx
363                y_adjust = has_attr(lx,attr_ablshift) or 0
364                node_depth = max(getdepth(lx) + min(y_adjust, 0), node_depth)
365                adj_depth = (y_adjust>0) and max(getdepth(lx) + y_adjust, adj_depth) or adj_depth
366                setfield(lx, 'yoffset', getfield(lx, 'yoffset') - y_adjust); lx = node_next(lx)
367             elseif lid==id_kern then
368                local ls = getsubtype(lx)
369                if ls==2 then -- アクセント用の kern
370                   set_attr(lx, attr_icflag, PROCESSED)
371                   lx = node_next(lx) -- lx: アクセント本体
372                   if getid(lx)==id_glyph then
373                      setfield(lx, 'yoffset', getfield(lx, 'yoffset') - (has_attr(lx,attr_ablshift) or 0))
374                   else -- アクセントは上下にシフトされている
375                      setshift(lx, getshift(lx) + (has_attr(lx,attr_ablshift) or 0))
376                   end
377                   set_attr(lx, attr_icflag, PROCESSED)
378                   lx = node_next(lx); set_attr(lx, attr_icflag, PROCESSED)
379                   lx = node_next(lx); set_attr(lx, attr_icflag, PROCESSED)
380                elseif ls==0  then
381                   Np.last = lx; lx = node_next(lx)
382                elseif (ls==3) or (lai==ITALIC) then
383                   Np.last = lx; set_attr(lx, attr_icflag, IC_PROCESSED); lx = node_next(lx)
384                else break
385                end
386             else break
387             end
388          end
389       end
390       lp=lx
391       local r
392       if adj_depth>node_depth then
393             r = node_new(id_rule,3,first_glyph)
394             setwhd(r, 0, 0, adj_depth); setdir(r, tex_dir)
395             set_attr(r, attr_icflag, PROCESSED)
396       end
397       if last_glyph then
398          Np.last_char = last_glyph
399          if r then insert_after(head, first_glyph, r) end
400       else
401          local npn = Np.nuc
402          Np.last_char = npn
403          if r then
404             local nf, nc = getfont(npn), getchar(npn)
405             local ct = (font.getfont(nf) or font.fonts[nf] ).characters[nc]
406             if not ct then -- variation selector
407                node_free(r)
408             elseif (ct.left_protruding or 0) == 0 then
409                head = insert_before(head, npn, r)
410                Np.first = acc_flag and Np.first or ((Np.first==npn) and r or npn)
411             elseif (ct.right_protruding or 0) == 0 then
412                insert_after(head, npn, r); Np.last, lp = r, r
413             else
414                ltjb.package_warning_no_line(
415                   'luatexja',
416                   'Check depth of glyph node ' .. tostring(npn) .. '(font=' .. nf
417                      .. ', char=' .. nc .. '),    because its \\lpcode is ' .. tostring(ct.left_protruding)
418                      .. ' and its \\rpcode is ' .. tostring(ct.right_protruding)
419                ); node_free(r)
420             end
421          end
422       end
423       return true, lp
424    end
425 end
426 end -- 002 ---------------------------------------
427 local calc_np_auxtable
428 do  -- 002 ---------------------------------------
429 local ltjw_apply_ashift_math = ltjw.apply_ashift_math
430 local ltjw_apply_ashift_disc = ltjw.apply_ashift_disc
431 local node_end_of_math = node.direct.end_of_math
432 local dir_tate = luatexja.dir_table.dir_tate
433 local sid_start_link   = node.subtype 'pdf_start_link'
434 local sid_start_thread = node.subtype 'pdf_start_thread'
435 local sid_end_link     = node.subtype 'pdf_end_link'
436 local sid_end_thread   = node.subtype 'pdf_end_thread'
437 calc_np_auxtable = {
438    [id_glyph] = calc_np_aux_glyph_common,
439    [id_hlist] = function(lp)
440       local op, flag
441       head, lp, op, flag = ltjd_make_dir_whatsit(head, lp, list_dir, 'jfm hlist')
442       set_attr(op, attr_icflag, PROCESSED)
443       Np.first = Np.first or op; Np.last = op; Np.nuc = op;
444       if (flag or getshift(op)~=0) then
445          Np.id = id_box_like
446       else
447          Np.id = id_hlist
448          Np.last_char = check_box_high(Np, getlist(op), nil)
449       end
450       return true, lp
451    end,
452    [id_vlist] =  function(lp)
453       local op
454       head, lp, op = ltjd_make_dir_whatsit(head, lp, list_dir, 'jfm:' .. getid(lp))
455       Np.first = Np.first or op; Np.last = op; Np.nuc = op;
456       Np.id = id_box_like;
457       return true, lp
458    end,
459    box_like = function(lp)
460       Np.first = Np.first or lp; Np.last = lp; Np.nuc = lp;
461       Np.id = id_box_like;
462       return true, node_next(lp)
463    end,
464    [id_whatsit] = function(lp)
465       local lps = getsubtype(lp)
466       if lps==sid_user then
467          if getfield(lp, 'user_id')==luatexja.userid_table.IHB then
468             local lq = node_next(lp);
469             head = node_remove(head, lp); node_free(lp); non_ihb_flag = getfield(lp, 'value')~=1
470             return false, lq;
471          elseif getfield(lp, 'user_id')==luatexja.userid_table.JA_AL_BDD then
472             local lq = node_next(lp);
473             head = node_remove(head, lp); node_free(lp)
474             return false, lq;
475          else
476             set_attr(lp, attr_icflag, PROCESSED)
477             luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo",
478                                      Np, lp, Nq)
479             if Np.nuc then
480                Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc;
481                return true, node_next(lp)
482             else
483                return false, node_next(lp)
484             end
485          end
486       else
487          -- we do special treatment for these whatsit nodes.
488          if lps == sid_start_link or lps == sid_start_thread then
489             Np.first = lp
490          elseif lps == sid_end_link or lps == sid_end_thread then
491             Np.first, Nq.last = nil, lp;
492          end
493          set_attr(lp, attr_icflag, PROCESSED)
494          return false, node_next(lp)
495       end
496    end,
497    [id_math] = function(lp)
498       Np.first, Np.nuc = (Np.first or lp), lp;
499       set_attr(lp, attr_icflag, PROCESSED)
500       set_np_xspc_alchar(Np, -1, lp)
501       local end_math  = node_end_of_math(lp)
502       ltjw_apply_ashift_math(lp, end_math, attr_ablshift)
503       set_attr(end_math, attr_icflag, PROCESSED)
504       Np.last, Np.id = end_math, id_math;
505       return true, node_next(end_math);
506    end,
507    [id_glue] = function(lp)
508       Np.first, Np.nuc, Np.last = (Np.first or lp), lp, lp;
509       Np.id = getid(lp); 
510       local f = luatexbase.call_callback("luatexja.jfmglue.special_jaglue", lp)
511       if f then
512          set_attr(lp, attr_icflag, PROCESSED)
513       end
514       return true, node_next(lp)
515    end,
516    [id_disc] = function(lp)
517       Np.first, Np.nuc, Np.last = (Np.first or lp), lp, lp;
518       Np.id = getid(lp); set_attr(lp, attr_icflag, PROCESSED)
519       ltjw_apply_ashift_disc(lp, (list_dir==dir_tate), tex_dir)
520       Np.last_char = check_box_high(Np, getfield(lp, 'replace'), nil)
521       return true, node_next(lp)
522    end,
523    [id_kern] = function(lp)
524       if getsubtype(lp)==2 then
525          Np.first = Np.first or lp
526          set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
527          if getid(lp)==id_glyph then -- アクセント本体
528             setfield(lp, 'yoffset', getfield(lp, 'yoffset') - (has_attr(lp,attr_ablshift) or 0))
529          else -- アクセントは上下にシフトされている
530             setshift(lp, getshift(lp) + (has_attr(lp,attr_ablshift) or 0))
531          end
532          set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
533          set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
534          set_attr(lp, attr_icflag, PROCESSED);
535          return calc_np_aux_glyph_common(lp, true)
536       else
537          Np.first = Np.first or lp
538          Np.id = id_kern; set_attr(lp, attr_icflag, PROCESSED)
539          Np.last = lp; return true, node_next(lp)
540       end
541    end,
542    [id_penalty] = function(lp)
543       Bp[#Bp+1] = lp; set_attr(lp, attr_icflag, PROCESSED)
544       return false, node_next(lp)
545    end,
546 }
547 end -- 002 ---------------------------------------
548 calc_np_auxtable[id_rule]   = calc_np_auxtable.box_like
549 calc_np_auxtable[15]        = calc_np_auxtable.box_like
550
551 local function calc_np_aux_skip (lp)
552    set_attr(lp, attr_icflag, PROCESSED)
553    return false, node_next(lp)
554 end
555
556 function calc_np(last, lp)
557    local k
558    -- We assume lp = node_next(Np.last)
559    if Nq and Nq.id==id_pbox_w then
560       luatexbase.call_callback("luatexja.jfmglue.whatsit_last_minute", false, Nq, Np)
561    end
562    Np, Nq, non_ihb_flag = Nq, Np, true
563    -- We clear `predefined' entries of Np before pairs() loop,
564    -- because using only pairs() loop is slower.
565    Np.post, Np.pre, Np.xspc, Np.gk = nil, nil, nil, nil
566    Np.first, Np.id, Np.last, Np.met, Np.class= nil, nil, nil, nil
567    Np.auto_kspc, Np.auto_xspc, Np.char, Np.nuc = nil, nil, nil, nil
568    -- auto_kspc, auto_xspc: normally true/false, 
569    -- but the number 0 when Np is ''the beginning of the box/paragraph''.
570    for k in pairs(Np) do Np[k] = nil end
571
572    for k = 1,#Bp do Bp[k] = nil end
573    while lp ~= last  do
574       local lpa = has_attr(lp, attr_icflag) or 0
575       -- unbox 由来ノードの検出
576       if (lpa>=PACKED) and (lpa%PROCESSED_BEGIN_FLAG<=BOXBDD) then
577          if lpa%PROCESSED_BEGIN_FLAG == BOXBDD then
578             local lq = node_next(lp)
579             head = node_remove(head, lp); node_free(lp); lp = lq
580          else
581             return calc_np_pbox(lp, last)
582          end -- id_pbox
583       else
584          k, lp = (calc_np_auxtable[getid(lp)] or calc_np_aux_skip)(lp)
585          if k then return lp end
586       end
587    end
588    Np=nil
589 end
590 end -- 001 -----------------------------------------------
591
592 -- extract informations from Np
593 -- We think that "Np is a Japanese character" if Np.met~=nil,
594 --            "Np is an alphabetic character" if Np.pre~=nil,
595 --            "Np is not a character" otherwise.
596 local after_hlist = nil -- global
597 local after_alchar, extract_np
598 do
599   local PRE  = luatexja.stack_table_index.PRE
600   local POST = luatexja.stack_table_index.POST
601   local KCAT = luatexja.stack_table_index.KCAT
602   local XSP  = luatexja.stack_table_index.XSP
603   local dir_tate = luatexja.dir_table.dir_tate
604
605 -- 和文文字のデータを取得
606    local attr_jchar_class = luatexbase.attributes['ltj@charclass']
607    local attr_jchar_code = luatexbase.attributes['ltj@charcode']
608    local attr_autospc = luatexbase.attributes['ltj@autospc']
609    local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
610    local getcomponents = node.direct.getcomponents
611    --local ltjf_get_vert_glyph = ltjf.get_vert_glyph
612    function set_np_xspc_jachar(Nx, x)
613       local m = ltjf_font_metric_table[getfont(x)]
614       local c, c_glyph = (not getcomponents(x) and ltjs_orig_char_table[x]), getchar(x)
615       if c and c~=c_glyph then set_attr(x, attr_jchar_code, c) end
616       c = c or c_glyph
617       local cls = slow_find_char_class(c, m, c_glyph)
618       Nx.met, Nx.class, Nx.char = m, cls, c;
619       local mc = m.char_type; Nx.char_type = mc
620       if cls~=0 then set_attr(x, attr_jchar_class, cls) end
621       Nx.pre  = table_current_stack[PRE + c]  or 0
622       Nx.post = table_current_stack[POST + c] or 0
623       Nx.xspc = table_current_stack[XSP  + c] or 3
624       Nx.kcat = table_current_stack[KCAT + c] or 0
625       Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
626       return m, mc, cls
627    end
628    function set_np_xspc_jachar_hbox(Nx, x)
629       local m = ltjf_font_metric_table[getfont(x)]
630       local c = has_attr(x, attr_jchar_code) or getchar(x)
631       Nx.met, Nx.char  = m, c; Nx.class = has_attr(x, attr_jchar_class) or 0;
632       local mc = m.char_type; Nx.char_type = mc
633       Nx.pre  = table_current_stack[PRE + c]  or 0
634       Nx.post = table_current_stack[POST + c] or 0
635       Nx.xspc = table_current_stack[XSP  + c] or 3
636       Nx.kcat = table_current_stack[KCAT + c] or 0
637       Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
638    end
639
640 -- 欧文文字のデータを取得
641    local floor = math.floor
642    local nullfunc = function(n) return n end
643    function set_np_xspc_alchar(Nx, c,x, lig)
644       if c~=-1 then
645          local f = (lig ==1) and nullfunc or node_tail
646          local xc, xs = getcomponents(x), getsubtype(x)
647          while xc and xs and xs%4>=2 do
648             x = f(xc);
649             if getid(x)==id_disc then x, xc, xs = nil, getfield(x,'replace'), 2
650             else xc, xs = getcomponents(x), getsubtype(x) end
651          end
652          c = x and getchar(x) or c
653          Nx.pre  = table_current_stack[PRE + c]  or 0
654          Nx.post = table_current_stack[POST + c] or 0
655       else
656          Nx.pre, Nx.post = 0, 0
657       end
658       Nx.met = nil
659       Nx.xspc = table_current_stack[XSP  + c] or 3
660       Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
661    end
662    local set_np_xspc_alchar = set_np_xspc_alchar
663    -- change the information for the next loop
664    -- (will be done if Nx is an alphabetic character or a hlist)
665    after_hlist = function (Nx)
666       local s = Nx.last_char
667       if s then
668          if getid(s)==id_glyph then
669             if if_lang_ja(s) then
670                set_np_xspc_jachar_hbox(Nx, s)
671             else
672                set_np_xspc_alchar(Nx, getchar(s), s, 2)
673             end
674          else
675             set_np_xspc_alchar(Nx, -1, s)
676          end
677       else
678          Nx.pre, Nx.met = nil, nil
679       end
680    end
681    after_alchar = function (Nx)
682       local x = Nx.last_char
683       return set_np_xspc_alchar(Nx, getchar(x), x, 2)
684    end
685
686 end
687
688 -------------------- 最下層の処理
689
690 luatexbase.create_callback('luatexja.adjust_jfmglue', 'simple', function(n) return n end)
691
692 -- change penalties (or create a new penalty, if needed)
693 local function handle_penalty_normal(post, pre, g)
694    luatexbase.call_callback('luatexja.adjust_jfmglue', head, Nq, Np, Bp)
695    local a = (pre or 0) + (post or 0)
696    if #Bp == 0 then
697       if (a~=0 and not(g and getid(g)==id_kern)) then
698          local p = node_new(id_penalty, nil, Nq.nuc, Np.nuc)
699          if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
700          setpenalty(p, a); head = insert_before(head, Np.first, p)
701          Bp[1]=p; set_attr(p, attr_icflag, KINSOKU)
702       end
703    else for _, v in pairs(Bp) do add_penalty(v,a) end
704    end
705 end
706
707 local function handle_penalty_always(post, pre, g)
708    luatexbase.call_callback('luatexja.adjust_jfmglue', head, Nq, Np, Bp)
709    local a = (pre or 0) + (post or 0)
710    if #Bp == 0 then
711       if not (g and getid(g)==id_glue) or a~=0 then
712          local p = node_new(id_penalty, nil, Nq.nuc, Np.nuc)
713          if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
714          setpenalty(p, a); head = insert_before(head, Np.first, p)
715          Bp[1]=p; set_attr(p, attr_icflag, KINSOKU)
716       end
717    else for _, v in pairs(Bp) do add_penalty(v,a) end
718    end
719 end
720
721 local function handle_penalty_suppress(post, pre, g)
722    luatexbase.call_callback('luatexja.adjust_jfmglue', head, Nq, Np, Bp)
723    if #Bp == 0 then
724       if g and getid(g)==id_glue then
725          local p = node_new(id_penalty, nil, Nq.nuc, Np.nuc)
726          setpenalty(p, 10000); head = insert_before(head, Np.first, p)
727          Bp[1]=p; set_attr(p, attr_icflag, KINSOKU)
728       end
729    else 
730       local a = (pre or 0) + (post or 0)
731       for _, v in pairs(Bp) do add_penalty(v,a) end
732    end
733 end
734
735 local function handle_penalty_jwp()
736    local a = table_current_stack[luatexja.stack_table_index.JWP]
737    if #widow_Bp == 0 then
738       if a~=0 then
739          local p = node_new(id_penalty, widow_Np.nuc)
740          if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
741          setpenalty(p, a); head = insert_before(head, widow_Np.first, p)
742          widow_Bp[1]=p; set_attr(p, attr_icflag, KINSOKU)
743       end
744    else for _, v in pairs(widow_Bp) do add_penalty(v,a) end
745    end
746 end
747
748 -- 和文文字間の JFM glue を node 化
749 local function new_jfm_glue(mc, bc, ac)
750 -- bc, ac: char classes
751    local g = mc[bc][ac]
752    if g then
753        if g[1] then
754           local k = node_new(id_kern, 1); setkern(k, g[1]) 
755           set_attr(k, attr_icflag, FROM_JFM)
756           return k, g.ratio, false, false, false
757        else
758           local f = node_new(id_glue)
759           set_attr(f, attr_icflag, g.priority)
760           setglue(f, g.width, g.stretch, g.shrink)
761           return f, g.ratio, g.kanjiskip_natural, g.kanjiskip_stretch, g.kanjiskip_shrink
762       end
763    end
764    return false, 0
765 end
766
767 -- Nq.last (kern w) .... (glue/kern g) Np.first
768 local function real_insert(g)
769    if g then
770       head, Np.first = insert_before(head, Np.first, node_inherit_attr(g, Nq.nuc, Np.nuc))
771       local ngk = Np.gk
772       if not ngk then Np.gk = g
773       elseif type(ngk)=="table" then ngk[#ngk+1]=g
774       else  Np.gk = { ngk, g } end
775    end
776 end
777
778
779 -------------------- 和文文字間空白量の決定
780 local calc_ja_ja_aux
781 do
782    local round = tex.round
783    local bg_ag = 2*id_glue - id_glue
784    local bg_ak = 2*id_glue - id_kern
785    local bk_ag = 2*id_kern - id_glue
786    local bk_ak = 2*id_kern - id_kern
787    local getkern = node.direct.getkern
788
789    local function blend_diffmet(b, a, rb, ra)
790       return round(luatexja.jfmglue.diffmet_rule((1-rb)*b+rb*a, (1-ra)*b+ra*a))
791    end
792    local blend_diffmet_inf
793    do
794       local abs, log, log264, floor = math.abs, math.log, math.log(2)*64, math.floor
795       blend_diffmet_inf = function (b, a, bo, ao, rb, ra)
796          local nb, na = (bo and b*2.0^(64*bo) or 0), (ao and a*2.0^(64*ao) or 0)
797          local r = luatexja.jfmglue.diffmet_rule((1-rb)*nb+rb*na, (1-ra)*nb+ra*na)
798          local ro = (r~=0) and floor(log(abs(r))/log264+0.0625) or 0
799          return round(r/2.^(64*ro)), ro
800       end
801    end
802    local getglue = luatexja.getglue
803    calc_ja_ja_aux = function (gb, ga, db, da)
804       if luatexja.jfmglue.diffmet_rule ~= math.two_pleft and diffmet_rule ~= math.two_pright
805           and luatexja.jfmglue.diffmet_rule ~= math.two_paverage then
806          db, da = 0, 1
807       end
808       if not gb then
809          if ga then gb = node_new(id_kern, 1); setkern(gb, 0)
810          else return nil end
811       elseif not ga then
812          ga = node_new(id_kern, 1); setkern(ga, 0)
813       end
814       local gbw, gaw, gbst, gast, gbsto, gasto, gbsh, gash, gbsho, gasho
815       if getid(gb)==id_glue then
816          gbw, gbst, gbsh, gbsto, gbsho = getglue(gb)
817       else
818          gbw = getkern(gb)
819       end
820       if getid(ga)==id_glue then
821          gaw, gast, gash, gasto, gasho = getglue(ga)
822       else
823          gaw = getkern(ga)
824       end
825       if not (gbst or gast) then -- 両方とも kern
826          setkern(gb, blend_diffmet(gbw, gaw, db, da))
827          node_free(ga); return gb
828       else
829          local gr = gb
830          if not gbst then gr = ga; node_free(gb) else node_free(ga) end
831          gbw = blend_diffmet(gbw or 0, gaw or 0, db, da) -- 結果の自然長
832          gbst, gbsto = blend_diffmet_inf(gbst, gast, gbsto, gasto, db, da) -- 伸び
833          gbsh, gbsho = blend_diffmet_inf(-(gbsh or 0), -(gash or 0), gbsho, gasho, db, da) -- -(縮み)
834          setglue(gr, gbw, gbst, -gbsh, gbsto, gbsho)
835          return gr
836       end
837    end
838 end
839
840 local null_skip_table = {0, 0, 0}
841 -- get kanjiskip
842 local get_kanjiskip, kanjiskip_jfm_flag
843 local get_kanjiskip_low
844 local calc_ja_ja_glue
845 do
846    local KANJI_SKIP   = luatexja.icflag_table.KANJI_SKIP
847    local KANJI_SKIP_JFM   = luatexja.icflag_table.KANJI_SKIP_JFM
848
849    get_kanjiskip_low = function(flag, qm, bn, bp, bh)
850    -- flag = false: kanjiskip そのもの(パラメータ or JFM)
851    --               ノード kanji_skip のコピーで良い場合は nil が帰る
852    -- flag = true: JFM グルーに付随する kanjiskip 自然長/伸び/縮み分
853       if qm.with_kanjiskip and (bn or bp or bh) then
854          if kanjiskip_jfm_flag then
855             local g = node_new(id_glue);
856             local bk = qm.kanjiskip or null_skip_table
857             setglue(g, bn and (bn*bk[1]) or 0, 
858                        bp and (bp*bk[2]) or 0, 
859                        bh and (bh*bk[3]) or 0, 0, 0)
860             set_attr(g, attr_icflag, KANJI_SKIP_JFM)
861             return g
862          elseif flag then
863             local g = node_new(id_glue)
864             local st = bp and (bp*getfield(kanji_skip, 'stretch')) or 0
865             local sh = bh and (bh*getfield(kanji_skip, 'shrink')) or 0
866             setglue(g,
867                bn and (bn*getwidth(kanji_skip)) or 0,
868                st, sh, 
869                (st==0) and 0 or getfield(kanji_skip, 'stretch_order'),
870                (sh==0) and 0 or getfield(kanji_skip, 'shrink_order'))
871             set_attr(g, attr_icflag, KANJI_SKIP_JFM)
872             return g
873          end
874       end
875    end
876    
877    get_kanjiskip = function()
878       if Np.auto_kspc==0 or Nq.auto_kspc==0 then return nil 
879       elseif Np.auto_kspc or Nq.auto_kspc then
880          local pm, qm = Np.met, Nq.met
881          if (pm.char_type==qm.char_type) and (qm.var==pm.var) then
882              return get_kanjiskip_low(false, qm, 1, 1, 1) or node_copy(kanji_skip)
883          else
884             local gb = get_kanjiskip_low(false, qm, 1, 1, 1)
885             if gb then
886                return calc_ja_ja_aux(gb, 
887                  get_kanjiskip_low(false, pm, 1, 1, 1) or node_copy(kanji_skip), 0, 1) 
888             else
889                local ga = get_kanjiskip_low(false, pm, 1, 1, 1)
890                return (ga and calc_ja_ja_aux(node_copy(kanji_skip), ga, 0, 1))
891                  or node_copy(kanji_skip)
892             end
893          end
894       else   
895          local g = node_new(id_glue)
896          set_attr(g, attr_icflag, kanjiskip_jfm_flag and KANJI_SKIP_JFM or KANJI_SKIP)
897          return g
898       end
899    end
900
901    calc_ja_ja_glue = function ()
902       local qm, pm = Nq.met, Np.met
903       local qmc, pmc = qm.char_type, pm.char_type
904       if (qmc==pmc) and (qm.var==pm.var) then
905          local g, _, kn, kp, kh = new_jfm_glue(qmc, Nq.class, Np.class)
906          return g, (Np.auto_kspc or Nq.auto_kspc) and get_kanjiskip_low(true, qm, kn, kp, kh)
907       else
908          local npn, nqn = Np.nuc, Nq.nuc
909          local gb, db, bn, bp, bh 
910             = new_jfm_glue(qmc, Nq.class,
911                            slow_find_char_class(Np.char,
912                                                 qm, getchar(npn)))
913          local ga, da, an, ap, ah 
914             = new_jfm_glue(pmc,
915                            slow_find_char_class(Nq.char,
916                                                 pm, getchar(nqn)),
917                            Np.class)
918          local g = calc_ja_ja_aux(gb, ga, db, da)
919          local k
920          gb = get_kanjiskip_low(true, qm, bn, bp, bh)
921          ga = get_kanjiskip_low(true, pm, an, ap, ah)
922          k = calc_ja_ja_aux(gb, ga, db, da)
923          return g, k
924       end
925    end
926 end
927
928 -------------------- 和欧文間空白量の決定
929
930 -- get xkanjiskip
931 local get_xkanjiskip, xkanjiskip_jfm_flag
932 local get_xkanjiskip_normal, get_xkanjiskip_jfm
933 local get_xkanjiskip_low
934 do
935    local XKANJI_SKIP   = luatexja.icflag_table.XKANJI_SKIP
936    local XKANJI_SKIP_JFM   = luatexja.icflag_table.XKANJI_SKIP_JFM
937
938    get_xkanjiskip_low = function(flag, qm, bn, bp, bh)
939       if flag or (qm.with_kanjiskip and (bn or bp or bh)) then
940          if xkanjiskip_jfm_flag then
941             local g = node_new(id_glue);
942             local bk = qm.xkanjiskip or null_skip_table
943             setglue(g, bn and bk[1] or 0,
944                        bp and bk[2] or 0,
945                        bh and bk[3] or 0, 0, 0)
946             set_attr(g, attr_icflag, XKANJI_SKIP_JFM)
947             return g
948          elseif flag then
949             return node_copy(xkanji_skip)
950          else
951             local g = node_new(id_glue)
952             local w, st, sh, sto, sho = getglue(xkanji_skip) 
953             setglue(g,
954                bn and (bn*w) or 0,
955                bp and (bp*st) or 0,
956                bh and (bh*sh) or 0,
957                bp and sto or 0,
958                bh and sho or 0)
959             set_attr(g, attr_icflag, XKANJI_SKIP_JFM)
960             return g
961          end
962       end
963    end
964    
965    get_xkanjiskip = function(Nn)
966       if Np.auto_xspc==0 or Nq.auto_xspc==0 then
967         return nil 
968       elseif (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
969          return get_xkanjiskip_low(true, Nn.met, 1, 1, 1)
970       else
971          local g = node_new(id_glue)
972          set_attr(g, attr_icflag, xkanjiskip_jfm_flag and XKANJI_SKIP_JFM or XKANJI_SKIP)
973          return g
974       end
975    end
976 end
977
978 -------------------- 隣接した「塊」間の処理
979
980 local function combine_spc(name)
981    return (Np[name] or Nq[name]) and ((Np[name]~=0) and (Nq[name]~=0))
982 end
983
984 -- NA, NB: alchar or math
985 local function get_NA_skip()
986    local pm = Np.met
987    local qclass = fast_find_char_class(
988       (Nq.id == id_math and -1 or (Nq.xspc>=2 and 'alchar' or 'nox_alchar')), pm)
989    local g, _, kn, kp, kh = new_jfm_glue(pm.char_type, qclass, Np.class)
990    local k = g and (Nq.xspc>=2) and (Np.xspc%2==1) and combine_spc 'auto_xspc'
991       and get_kanjiskip_low(true, pm, kn, kp, kh)
992    return g, k
993 end
994 local function get_NB_skip()
995    local qm = Nq.met
996    local pclass = fast_find_char_class(
997       (Np.id == id_math and -1 or (Np.xspc%2==1 and 'alchar' or 'nox_alchar')), qm)
998    local g, _, kn, kp, kh = new_jfm_glue(qm.char_type, Nq.class, pclass)
999    local k = g and (Nq.xspc>=2) and (Np.xspc%2==1) and combine_spc 'auto_xspc'
1000       and get_kanjiskip_low(true, qm, kn, kp, kh)
1001    return g, k
1002 end
1003
1004 local function get_OA_skip(insert_ksp)
1005    local pm = Np.met
1006    local g, _, kn, kp, kh = new_jfm_glue(
1007       pm.char_type,
1008       fast_find_char_class(
1009         (((Nq.id==id_glue)or(Nq.id==id_kern)) and 'glue' or 'jcharbdd'), pm), 
1010       Np.class)
1011    local k
1012    if insert_ksp then
1013       k = (combine_spc 'auto_kspc') and get_kanjiskip_low(true, pm, kn, kp, kh)
1014    end
1015    return g, k
1016 end
1017 local function get_OB_skip(insert_ksp)
1018    local qm = Nq.met
1019    local g, _, kn, kp, kh = new_jfm_glue(
1020       qm.char_type, Nq.class,
1021       fast_find_char_class(
1022         (((Np.id==id_glue)or(Np.id==id_kern)) and 'glue' or 'jcharbdd'), qm))
1023    local k
1024    if insert_ksp then
1025       k = (combine_spc 'auto_kspc') and get_kanjiskip_low(true, qm, kn, kp, kh)
1026    end
1027    return g, k
1028 end
1029
1030 -- (anything) .. jachar
1031 local function handle_np_jachar(mode)
1032    local qid = Nq.id
1033    if qid==id_jglyph or ((qid==id_pbox or qid==id_pbox_w) and Nq.met) then
1034       local g, k
1035       if non_ihb_flag then g, k = calc_ja_ja_glue() end -- M->K
1036       if not g then g = get_kanjiskip() end
1037       handle_penalty_normal(Nq.post, Np.pre, g); 
1038       real_insert(g); real_insert(k)
1039    elseif Nq.met then  -- qid==id_hlist
1040       local g, k
1041       if non_ihb_flag then g, k = get_OA_skip(true) end -- O_A->K
1042       if not g then g = get_kanjiskip() end
1043       handle_penalty_normal(0, Np.pre, g); real_insert(g); real_insert(k)
1044    elseif Nq.pre then
1045       local g, k; if non_ihb_flag then g, k = get_NA_skip() end -- N_A->X
1046       if not g then g = get_xkanjiskip(Np) end
1047       handle_penalty_normal((qid==id_hlist and 0 or Nq.post), Np.pre, g); 
1048       real_insert(g); real_insert(k)
1049    else
1050       local g = non_ihb_flag and (get_OA_skip()) -- O_A
1051       if qid==id_glue then handle_penalty_normal(0, Np.pre, g)
1052       elseif qid==id_kern then handle_penalty_suppress(0, Np.pre, g)
1053       else handle_penalty_always(0, Np.pre, g)
1054       end
1055       real_insert(g)
1056    end
1057    if mode and Np.kcat%2~=1 then
1058       widow_Np.first, widow_Bp, Bp = Np.first, Bp, widow_Bp
1059    end
1060 end
1061
1062
1063 -- jachar .. (anything)
1064 local function handle_nq_jachar()
1065     if Np.pre then
1066       local g, k; if non_ihb_flag then g, k =  get_NB_skip()end -- N_B->X
1067       if not g then g = get_xkanjiskip(Nq) end
1068       handle_penalty_normal(Nq.post, (Np.id==id_hlist and 0 or Np.pre), g);
1069       real_insert(g); real_insert(k)
1070    else
1071       local g =non_ihb_flag and  (get_OB_skip()) -- O_B
1072       if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
1073       elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
1074       else handle_penalty_always(Nq.post, 0, g)
1075       end
1076       real_insert(g)
1077    end
1078 end
1079
1080 -- (anything) .. (和文文字で始まる hlist)
1081 local function handle_np_ja_hlist()
1082    local qid = Nq.id
1083    if qid==id_jglyph or ((qid==id_pbox or qid == id_pbox_w) and Nq.met) then
1084       local g = non_ihb_flag and get_OB_skip(true) or get_kanjiskip() -- O_B->K
1085       handle_penalty_normal(Nq.post, 0, g); real_insert(g)
1086    elseif Nq.met then  -- Nq.id==id_hlist
1087       local g = get_kanjiskip() -- K
1088       handle_penalty_suppress(0, 0, g); real_insert(g)
1089    elseif Nq.pre then
1090       local g = get_xkanjiskip(Np) -- X
1091       handle_penalty_suppress(0, 0, g); real_insert(g)
1092    end
1093 end
1094
1095 -- (和文文字で終わる hlist) .. (anything)
1096 local function handle_nq_ja_hlist()
1097    if Np.pre then
1098       local g = get_xkanjiskip(Nq) -- X
1099       handle_penalty_suppress(0, 0, g); real_insert(g)
1100    end
1101 end
1102
1103
1104 -- Nq が前側のクラスタとなることによる修正
1105 local adjust_nq
1106 do
1107    local adjust_nq_aux = {
1108       [id_glyph] = function() after_alchar(Nq) end, -- after_alchar(Nq)
1109       [id_hlist] = function() after_hlist(Nq) end,
1110       [id_pbox]  = function() after_hlist(Nq) end,
1111       [id_disc]  = function() after_hlist(Nq) end,
1112       [id_glue]  = function() 
1113                       luatexbase.call_callback("luatexja.jfmglue.special_jaglue_after", Nq.nuc)
1114                    end,
1115       [id_pbox_w]= function()
1116                       local hh = luatexbase.call_callback("luatexja.jfmglue.whatsit_after", false, Nq, Np, head)
1117                       -- hh: new head of false (nott processed)
1118                       if hh then head = hh end
1119                    end,
1120    }
1121
1122    adjust_nq = function()
1123       local x = adjust_nq_aux[Nq.id]
1124       if x then x()  end
1125    end
1126 end
1127
1128
1129 -------------------- 開始・終了時の処理
1130 do
1131 local node_prev = node.direct.getprev
1132 -- リスト末尾の処理
1133 local function handle_list_tail(mode, last)
1134    adjust_nq()
1135    if mode then
1136       -- the current list is to be line-breaked.
1137       -- Insert \jcharwidowpenalty
1138       if widow_Np.first then handle_penalty_jwp() end
1139    else
1140       Np = Nq          
1141       -- the current list is the contents of a hbox
1142       local npi, pm = Np.id, Np.met
1143       if npi == id_jglyph or (npi==id_pbox and pm) then
1144          local g = new_jfm_glue(pm.char_type, Np.class, fast_find_char_class('boxbdd', pm))
1145          if g then
1146             set_attr(g, attr_icflag, BOXBDD)
1147             head = insert_after(head, Np.last, g)
1148          end
1149       end
1150    end
1151 end
1152
1153 -- リスト先頭の処理
1154 local function handle_list_head(par_indented)
1155    local npi, pm = Np.id, Np.met
1156    if npi == id_jglyph or (npi==id_pbox and pm) then
1157       if non_ihb_flag then
1158          local g = new_jfm_glue(pm.char_type, fast_find_char_class(par_indented, pm), Np.class)
1159          if g then
1160             set_attr(g, attr_icflag, BOXBDD)
1161             if getid(g)==id_glue and #Bp==0 then
1162                local h = node_new(id_penalty, nil, Np.nuc)
1163                setpenalty(h, 10000); set_attr(h, attr_icflag, BOXBDD)
1164             end
1165             head = insert_before(head, Np.first, g)
1166          end
1167       end
1168    end
1169 end
1170
1171 -- initialize
1172 -- return value: (the initial cursor lp), (last node)
1173 local init_var
1174 do
1175    local id_local = node.id 'local_par'
1176    local KANJI_SKIP   = luatexja.icflag_table.KANJI_SKIP
1177    local XKANJI_SKIP   = luatexja.icflag_table.XKANJI_SKIP
1178    local KSK  = luatexja.stack_table_index.KSK
1179    local XSK  = luatexja.stack_table_index.XSK
1180    local dir_yoko = luatexja.dir_table.dir_yoko
1181    local dir_tate = luatexja.dir_table.dir_tate
1182    local attr_yablshift = luatexbase.attributes['ltj@yablshift']
1183    local attr_tablshift = luatexbase.attributes['ltj@tablshift']
1184    local table_pool = {
1185       {}, {}, {first=nil},
1186       { auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
1187         first=nil, id=nil, last=nil, met=nil, nuc=nil,
1188         post=nil, pre=nil, xspc=nil, gk=nil }, 
1189       { auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
1190         first=nil, id=nil, last=nil, met=nil, nuc=nil,
1191         post=nil, pre=nil, xspc=nil, gk=nil },
1192    }
1193    init_var = function (mode,dir)
1194       -- 1073741823: max_dimen
1195       Bp, widow_Bp, widow_Np, Np, Nq
1196          = table_pool[1], table_pool[2], table_pool[3], table_pool[4], table_pool[5]
1197       for i=1,5 do for j,_ in pairs(table_pool[i]) do table_pool[i][j]=nil end end
1198       table_current_stack = ltjs.table_current_stack
1199
1200       list_dir, tex_dir = (ltjs.list_dir or dir_yoko), (dir or 'TLT')
1201       local is_dir_tate = list_dir==dir_tate
1202       capsule_glyph = is_dir_tate and ltjw.capsule_glyph_tate or ltjw.capsule_glyph_yoko
1203       attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
1204       local TEMP = node_new(id_glue) 
1205       -- TEMP is a dummy node, which will be freed at the end of the callback. 
1206       -- Without this node, set_attr(kanji_skip, ...) somehow creates an "orphaned"  attribute list.
1207       do
1208           kanji_skip, kanjiskip_jfm_flag = skip_table_to_glue(KSK)
1209           set_attr(kanji_skip, attr_icflag, KANJI_SKIP)
1210       end
1211       do
1212           xkanji_skip, xkanjiskip_jfm_flag = skip_table_to_glue(XSK)
1213           set_attr(xkanji_skip, attr_icflag, XKANJI_SKIP)
1214       end
1215       if mode then
1216          -- the current list is to be line-breaked:
1217          -- hbox from \parindent is skipped.
1218          local lp, par_indented, lpi, lps  = head, 'boxbdd', getid(head), getsubtype(head)
1219          while lp and 
1220             ((lpi==id_whatsit and lps~=sid_user)
1221                or ((lpi==id_hlist) and (lps==3))
1222                or (lpi==id_local)) do
1223             if (lpi==id_hlist) and (lps==3) then
1224                Np.char, par_indented = 'parbdd', 'parbdd'
1225                Np.width = getwidth(lp)
1226             end
1227             lp=node_next(lp); lpi, lps = getid(lp), getsubtype(lp) end
1228          return lp, node_tail(head), par_indented, TEMP
1229       else
1230          return head, nil, 'boxbdd', TEMP
1231       end
1232    end
1233 end
1234
1235 -------------------- 外部から呼ばれる関数
1236
1237 local ensure_tex_attr = ltjb.ensure_tex_attr
1238 local tex_getattr = tex.getattribute
1239 -- main interface
1240 function luatexja.jfmglue.main(ahead, mode, dir)
1241    if not ahead then return ahead end
1242    head = ahead;
1243    local lp, last, par_indented, TEMP = init_var(mode,dir)
1244    lp = calc_np(last, lp)
1245    if Np then
1246       handle_list_head(par_indented)
1247       lp = calc_np(last,lp); 
1248       while Np do
1249          adjust_nq();
1250          local pid, pm = Np.id, Np.met
1251          -- 挿入部
1252          if pid == id_jglyph then
1253             handle_np_jachar(mode)
1254          elseif pm then
1255             if pid==id_hlist then handle_np_ja_hlist()
1256             else handle_np_jachar() end
1257          elseif Nq.met then
1258             if Nq.id==id_hlist then handle_nq_ja_hlist()
1259             else handle_nq_jachar() end
1260          end
1261          lp = calc_np(last,lp)
1262       end
1263       handle_list_tail(mode, last)
1264    end
1265    -- adjust attr_icflag for avoiding error
1266    if tex_getattr(attr_icflag)~=0 then ensure_tex_attr(attr_icflag, 0) end
1267    node_free(kanji_skip); 
1268    node_free(xkanji_skip); node_free(TEMP)
1269    return head
1270 end
1271 end
1272
1273 do
1274    local IHB  = luatexja.userid_table.IHB 
1275    local BPAR = luatexja.userid_table.BPAR
1276    local BOXB = luatexja.userid_table.BOXB
1277    local node_prev = node.direct.getprev
1278    local node_write = node.direct.write
1279
1280    -- \inhibitglue, \disinhibitglue
1281    local function ihb_node(v)
1282       local tn = node_new(id_whatsit, sid_user)
1283       setfield(tn, 'user_id', IHB)
1284       setfield(tn, 'type', 100)
1285       setfield(tn, 'value', v)
1286       node_write(tn)
1287    end
1288    function luatexja.jfmglue.create_inhibitglue_node()
1289       ihb_node(1)
1290    end
1291    function luatexja.jfmglue.create_disinhibitglue_node()
1292       ihb_node(0)
1293    end
1294
1295    -- Node for indicating beginning of a paragraph
1296    -- (for ltjsclasses)
1297    function luatexja.jfmglue.create_beginpar_node()
1298       local tn = node_new(id_whatsit, sid_user)
1299       setfield(tn, 'user_id', BPAR)
1300       setfield(tn, 'type', 100)
1301       setfield(tn, 'value', 1)
1302       node_write(tn)
1303    end
1304
1305    -- Node for indicating a head/end of a box
1306    function luatexja.jfmglue.create_boxbdd_node()
1307       local tn = node_new(id_whatsit, sid_user)
1308       setfield(tn, 'user_id', BOXB)
1309       setfield(tn, 'type', 100)
1310       setfield(tn, 'value', 1)
1311       node_write(tn)
1312    end
1313
1314    local function whatsit_callback(Np, lp, Nq)
1315       if Np and Np.nuc then return Np
1316       elseif Np and getfield(lp, 'user_id') == BPAR then
1317          Np.first = lp; Np.nuc = lp; Np.last = lp
1318          return Np
1319       elseif Np and getfield(lp, 'user_id') == BOXB then
1320          Np.first = lp; Np.nuc = lp; Np.last = lp
1321          if Nq then
1322             if Nq.met then
1323                Np.class = fast_find_char_class('boxbdd', Nq.met)
1324             end
1325             Np.met = Nq.met; Np.pre = 0; Np.post = 0; Np.xspc = 0
1326             Np.auto_xspc, Np.auto_kspc = 0, 0
1327          end         
1328          return Np
1329       else
1330          return Np
1331       end
1332    end
1333
1334     local function whatsit_after_callback(s, Nq, Np, head)
1335        if not s and getfield(Nq.nuc, 'user_id') == BPAR then
1336          local x, y = node_prev(Nq.nuc), Nq.nuc
1337          Nq.first, Nq.nuc, Nq.last = x, x, x
1338          if Np then
1339             if Np.met then
1340                Nq.class = fast_find_char_class('parbdd', Np.met)
1341             end
1342             Nq.met = Np.met; Nq.pre = 0; Nq.post = 0; Nq.xspc = 0
1343             Nq.auto_xspc, Nq.auto_kspc = 0, 0
1344          end
1345          s = node_remove(head, y)
1346          node_free(y)
1347        elseif not s and getfield(Nq.nuc, 'user_id') == BOXB then
1348          local x, y = node_prev(Nq.nuc), Nq.nuc
1349          Nq.first, Nq.nuc, Nq.last = x, x, x
1350          if Np then
1351             if Np.met then
1352                Nq.class = fast_find_char_class('boxbdd', Np.met)
1353             end
1354             Nq.met = Np.met; Nq.pre = 0; Nq.post = 0; Nq.xspc = 0
1355             Nq.auto_xspc, Nq.auto_kspc = 0, 0
1356          end
1357          s = node_remove(head, y)
1358          node_free(y)
1359       end
1360       return s
1361    end
1362
1363    luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
1364                               "luatexja.beginpar.np_info", 1)
1365    luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
1366                               "luatexja.beginpar.np_info_after", 1)
1367 end
1368
1369 do
1370    local node_prev = node.direct.getprev
1371    local node_write = node.direct.write
1372    local XKANJI_SKIP   = luatexja.icflag_table.XKANJI_SKIP
1373    local XKANJI_SKIP_JFM   = luatexja.icflag_table.XKANJI_SKIP_JFM
1374    local KANJI_SKIP   = luatexja.icflag_table.KANJI_SKIP
1375    local KANJI_SKIP_JFM   = luatexja.icflag_table.KANJI_SKIP_JFM
1376    local XSK  = luatexja.stack_table_index.XSK
1377    local KSK  = luatexja.stack_table_index.KSK
1378    local attr_yablshift = luatexbase.attributes['ltj@yablshift']
1379    local attr_tablshift = luatexbase.attributes['ltj@tablshift']
1380    local getcount, abs, scan_keyword = tex.getcount, math.abs, token.scan_keyword
1381    local tex_nest = tex.nest
1382    local tex_getattr = tex.getattribute
1383    local get_current_jfont
1384    do
1385        local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
1386        local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
1387        local dir_tate = luatexja.dir_table.dir_tate
1388        local get_dir_count = ltjd.get_dir_count        
1389        function get_current_jfont()
1390            return tex_getattr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
1391        end
1392    end
1393    -- \insertxkanjiskip
1394    -- SPECIAL_JAGLUE のノード:
1395    -- * (X)KANJI_SKIP(_JFM): その場で値が決まっている
1396    -- * PROCESSED_BEGIN_FLAG + (X)KANJI_SKIP: 段落終了時に決める
1397    local function insert_k_skip_common(ind, name, ica, icb)
1398        if abs(tex_nest[tex_nest.ptr].mode) ~= ltjs.hmode then return end
1399        local g = node_new(id_glue); set_attr(g, attr_icflag, SPECIAL_JAGLUE)
1400        local is_late = scan_keyword("late")
1401        if not is_late then
1402            local st = ltjs.get_stack_skip(ind, getcount('ltj@@stack'))
1403            if st.width==1073741823 then
1404                local bk = ltjf_font_metric_table[get_current_jfont()][name]
1405                if bk then
1406                    setglue(g, bk[1] or 0, bk[2] or 0, bk[3] or 0, 0, 0)
1407                end
1408                set_attr(g, attr_yablshift, icb); node_write(g); return
1409            end
1410            setglue(g, st.width, st.stretch, st.shrink, st.stretch_order, st.shrink_order)
1411            set_attr(g, attr_yablshift, ica)
1412        else
1413            set_attr(g, attr_yablshift, PROCESSED_BEGIN_FLAG + ica)
1414            set_attr(g, attr_tablshift, get_current_jfont())               
1415        end
1416        node_write(g)
1417    end
1418    function luatexja.jfmglue.insert_xk_skip()
1419        insert_k_skip_common(XSK, "xkanjiskip", XKANJI_SKIP, XKANJI_SKIP_JFM)
1420    end
1421    function luatexja.jfmglue.insert_k_skip()
1422        insert_k_skip_common(KSK, "kanjiskip", KANJI_SKIP, KANJI_SKIP_JFM)
1423    end
1424    -- callback
1425    local function special_jaglue(lx)
1426        local lxi = get_attr_icflag(lx)
1427        if lxi==SPECIAL_JAGLUE then
1428            non_ihb_flag = false; return false
1429        else
1430            return lx
1431        end
1432    end
1433    local function special_jaglue_after_inner(lx, lxi, lxi_jfm, kn, bk)
1434        local w, st, sh, sto, sho = getglue(kn)
1435        if w~=1073741823 then
1436            setglue(lx, w, st, sh, sto, sho); set_attr(lx, attr_icflag, lxi)
1437        else
1438            local m = ltjf_font_metric_table[has_attr(lx, attr_tablshift)]
1439            setglue(lx, bk[1], bk[2], bk[3], 0, 0)
1440            set_attr(lx, attr_icflag, lxi_jfm)
1441        end
1442    end
1443    local function special_jaglue_after(lx)
1444        if get_attr_icflag(lx)==SPECIAL_JAGLUE then
1445            lxi=has_attr(lx, attr_yablshift)
1446            if lxi>=PROCESSED_BEGIN_FLAG then
1447                lxi = lxi%PROCESSED_BEGIN_FLAG
1448                if lxi == KANJI_SKIP then
1449                    special_jaglue_after_inner(lx, lxi, KANJI_SKIP_JFM, kanji_skip, 
1450                      ltjf_font_metric_table[has_attr(lx, attr_tablshift)].kanjiskip or null_skip_table)
1451                else --  lxi == XKANJI_SKIP
1452                    special_jaglue_after_inner(lx, lxi, XKANJI_SKIP_JFM, xkanji_skip, 
1453                      ltjf_font_metric_table[has_attr(lx, attr_tablshift)].xkanjiskip or null_skip_table)
1454                end
1455            else
1456                set_attr(lx, attr_icflag, lxi)
1457            end
1458            Np.first = lx
1459            if node_prev(lx) then
1460                local lxp = node_prev(lx)
1461                if lxp and getid(lxp)==id_penalty and get_attr_icflag(lxp)==KINSOKU then
1462                    Bp[#Bp+1]=lxp
1463                end
1464            end
1465            non_ihb_flag = false; return false
1466        end
1467        return true
1468    end
1469    luatexbase.create_callback("luatexja.jfmglue.special_jaglue", "list",
1470                               special_jaglue)
1471    luatexbase.create_callback("luatexja.jfmglue.special_jaglue_after", "list",
1472                               special_jaglue_after)
1473 end
1474
1475
1476 luatexja.jfmglue.after_hlist = after_hlist
1477 luatexja.jfmglue.check_box_high = check_box_high