OSDN Git Service

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