4 luatexbase.provides_module({
5 name = 'luatexja.jfmglue',
7 description = 'Insertion process of JFM glues, [x]kanjiskip and others',
9 luatexja.jfmglue = luatexja.jfmglue or {}
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
19 --local to_node = node.direct.tonode
20 --local to_direct = node.direct.todirect
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
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
43 local setpenalty = node.direct.setpenalty
44 local setkern = node.direct.setkern
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
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'
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'
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
91 local attr_icflag = luatexbase.attributes['ltj@icflag']
94 local table_current_stack
99 local set_np_xspc_jachar, set_np_xspc_alchar
100 local set_np_xspc_jachar_hbox
102 local ltjs_orig_char_table = ltjs.orig_char_table
104 local function get_attr_icflag(p)
105 return (has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
108 -------------------- Helper functions
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
116 local slow_find_char_class
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)
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)
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)
147 if pp>=10000 then setpenalty(p, 10000)
148 elseif pp<=-10000 then setpenalty(p, -10000)
149 else setpenalty(p, pp) end
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
163 local head -- the head of current list
166 local widow_Bp, widow_Np -- \jcharwidowpenalty 挿入位置管理用
168 local non_ihb_flag -- JFM グルー挿入抑止用 flag
169 -- false: \inhibitglue 指定時 true: それ以外
171 -------------------- hlist 内の文字の検索
173 local first_char, last_char, find_first_char
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
180 find_first_char = false; last_char = nil
183 while p and p~=box_end do
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
188 if pid==id_glyph then
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
197 local pa = get_attr_icflag(p)
198 if pa==IC_PROCESSED then
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.
204 found_visible_node = true
205 find_first_char = false; last_char = nil
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
213 if getshift(p)==0 then
215 if check_box(getlist(p), nil) then found_visible_node = true end
216 find_first_char = false
218 find_first_char = false; last_char = nil
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
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
234 return found_visible_node
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
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)
246 set_np_xspc_alchar(Nx, getchar(first_char),first_char, 1)
249 set_np_xspc_alchar(Nx, -1,first_char)
256 -------------------- Np の計算と情報取得
258 luatexbase.create_callback("luatexja.jfmglue.whatsit_getinfo", "data",
259 function (Np, lp, Nq)
260 if Np.nuc then return Np
262 return Np -- your code
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)
272 do -- 001 -----------------------------------------------
274 local traverse = node.direct.traverse
275 local function check_next_ickern(lp)
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)
282 Np.last = lx; return lp
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)
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
300 nc, lp = lp, node_next(lp)
302 first, lpa = false, (lp and has_attr(lp, attr_icflag) or 0)
303 -- get_attr_icflag() ではいけない!
306 lp = check_next_ickern(lp)
307 Np.last_char = check_box_high(Np, Np.first, lp)
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)
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
336 local m, mc, cls = set_np_xspc_jachar(Np, lp)
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
343 return true, check_next_ickern(lp);
346 set_np_xspc_alchar(Np, getchar(lp), lp, 1)
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)
356 local lai = get_attr_icflag(lx)
357 if lx==last or lai>=PACKED then break
359 local lid = getid(lx)
360 if lid==id_glyph and not if_lang_ja(lx) then
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))
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)
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)
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)
398 Np.last_char = last_glyph
399 if r then insert_after(head, first_glyph, r) end
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
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
414 ltjb.package_warning_no_line(
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)
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'
438 [id_glyph] = calc_np_aux_glyph_common,
439 [id_hlist] = function(lp)
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
448 Np.last_char = check_box_high(Np, getlist(op), nil)
452 [id_vlist] = function(lp)
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;
459 box_like = function(lp)
460 Np.first = Np.first or lp; Np.last = lp; Np.nuc = lp;
462 return true, node_next(lp)
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
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)
476 set_attr(lp, attr_icflag, PROCESSED)
477 luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo",
480 Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc;
481 return true, node_next(lp)
483 return false, node_next(lp)
487 -- we do special treatment for these whatsit nodes.
488 if lps == sid_start_link or lps == sid_start_thread then
490 elseif lps == sid_end_link or lps == sid_end_thread then
491 Np.first, Nq.last = nil, lp;
493 set_attr(lp, attr_icflag, PROCESSED)
494 return false, node_next(lp)
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);
507 [id_glue] = function(lp)
508 Np.first, Np.nuc, Np.last = (Np.first or lp), lp, lp;
510 local f = luatexbase.call_callback("luatexja.jfmglue.special_jaglue", lp)
512 set_attr(lp, attr_icflag, PROCESSED)
514 return true, node_next(lp)
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)
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))
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)
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)
542 [id_penalty] = function(lp)
543 Bp[#Bp+1] = lp; set_attr(lp, attr_icflag, PROCESSED)
544 return false, node_next(lp)
547 end -- 002 ---------------------------------------
548 calc_np_auxtable[id_rule] = calc_np_auxtable.box_like
549 calc_np_auxtable[15] = calc_np_auxtable.box_like
551 local function calc_np_aux_skip (lp)
552 set_attr(lp, attr_icflag, PROCESSED)
553 return false, node_next(lp)
556 function calc_np(last, lp)
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)
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
572 for k = 1,#Bp do Bp[k] = nil end
574 local lpa = has_attr(lp, attr_icflag) or 0
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
581 return calc_np_pbox(lp, last)
584 k, lp = (calc_np_auxtable[getid(lp)] or calc_np_aux_skip)(lp)
585 if k then return lp end
590 end -- 001 -----------------------------------------------
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
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
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
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
626 = not has_attr(x, attr_autospc, 0), not has_attr(x, attr_autoxspc, 0)
629 function set_np_xspc_jachar_hbox(Nx, x)
630 local m = ltjf_font_metric_table[getfont(x)]
631 local c = has_attr(x, attr_jchar_code) or getchar(x)
632 Nx.met, Nx.char = m, c; Nx.class = has_attr(x, attr_jchar_class) or 0;
633 local mc = m.char_type; Nx.char_type = mc
634 Nx.pre = table_current_stack[PRE + c] or 0
635 Nx.post = table_current_stack[POST + c] or 0
636 Nx.xspc = table_current_stack[XSP + c] or 3
637 Nx.kcat = table_current_stack[KCAT + c] or 0
638 Nx.auto_kspc, Nx.auto_xspc
639 = not has_attr(x, attr_autospc, 0), not has_attr(x, attr_autoxspc, 0)
643 local floor = math.floor
644 local nullfunc = function(n) return n end
645 function set_np_xspc_alchar(Nx, c,x, lig)
647 local f = (lig ==1) and nullfunc or node_tail
648 local xc, xs = getcomponents(x), getsubtype(x)
649 while xc and xs and xs%4>=2 do
651 if getid(x)==id_disc then x, xc, xs = nil, getfield(x,'replace'), 2
652 else xc, xs = getcomponents(x), getsubtype(x) end
654 c = x and getchar(x) or c
655 Nx.pre = table_current_stack[PRE + c] or 0
656 Nx.post = table_current_stack[POST + c] or 0
658 Nx.pre, Nx.post = 0, 0
661 Nx.xspc = table_current_stack[XSP + c] or 3
662 Nx.auto_xspc = not has_attr(x, attr_autoxspc, 0)
664 local set_np_xspc_alchar = set_np_xspc_alchar
665 -- change the information for the next loop
666 -- (will be done if Nx is an alphabetic character or a hlist)
667 after_hlist = function (Nx)
668 local s = Nx.last_char
670 if getid(s)==id_glyph then
671 if if_lang_ja(s) then
672 set_np_xspc_jachar_hbox(Nx, s)
674 set_np_xspc_alchar(Nx, getchar(s), s, 2)
677 set_np_xspc_alchar(Nx, -1, s)
680 Nx.pre, Nx.met = nil, nil
683 after_alchar = function (Nx)
684 local x = Nx.last_char
685 return set_np_xspc_alchar(Nx, getchar(x), x, 2)
690 -------------------- 最下層の処理
692 luatexbase.create_callback('luatexja.adjust_jfmglue', 'simple', function(n) return n end)
694 -- change penalties (or create a new penalty, if needed)
695 local function handle_penalty_normal(post, pre, g)
696 luatexbase.call_callback('luatexja.adjust_jfmglue', head, Nq, Np, Bp)
697 local a = (pre or 0) + (post or 0)
699 if (a~=0 and not(g and getid(g)==id_kern)) then
700 local p = node_new(id_penalty, nil, Nq.nuc, Np.nuc)
701 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
702 setpenalty(p, a); head = insert_before(head, Np.first, p)
703 Bp[1]=p; set_attr(p, attr_icflag, KINSOKU)
705 else for _, v in pairs(Bp) do add_penalty(v,a) end
709 local function handle_penalty_always(post, pre, g)
710 luatexbase.call_callback('luatexja.adjust_jfmglue', head, Nq, Np, Bp)
711 local a = (pre or 0) + (post or 0)
713 if not (g and getid(g)==id_glue) or a~=0 then
714 local p = node_new(id_penalty, nil, Nq.nuc, Np.nuc)
715 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
716 setpenalty(p, a); head = insert_before(head, Np.first, p)
717 Bp[1]=p; set_attr(p, attr_icflag, KINSOKU)
719 else for _, v in pairs(Bp) do add_penalty(v,a) end
723 local function handle_penalty_suppress(post, pre, g)
724 luatexbase.call_callback('luatexja.adjust_jfmglue', head, Nq, Np, Bp)
726 if g and getid(g)==id_glue then
727 local p = node_new(id_penalty, nil, Nq.nuc, Np.nuc)
728 setpenalty(p, 10000); head = insert_before(head, Np.first, p)
729 Bp[1]=p; set_attr(p, attr_icflag, KINSOKU)
732 local a = (pre or 0) + (post or 0)
733 for _, v in pairs(Bp) do add_penalty(v,a) end
737 local function handle_penalty_jwp()
738 local a = table_current_stack[luatexja.stack_table_index.JWP]
739 if #widow_Bp == 0 then
741 local p = node_new(id_penalty, widow_Np.nuc)
742 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
743 setpenalty(p, a); head = insert_before(head, widow_Np.first, p)
744 widow_Bp[1]=p; set_attr(p, attr_icflag, KINSOKU)
746 else for _, v in pairs(widow_Bp) do add_penalty(v,a) end
750 -- 和文文字間の JFM glue を node 化
751 local function new_jfm_glue(mc, bc, ac)
752 -- bc, ac: char classes
756 local k = node_new(id_kern, 1); setkern(k, g[1])
757 set_attr(k, attr_icflag, FROM_JFM)
758 return k, g.ratio, false, false, false
760 local f = node_new(id_glue)
761 set_attr(f, attr_icflag, g.priority)
762 setglue(f, g.width, g.stretch, g.shrink)
763 return f, g.ratio, g.kanjiskip_natural, g.kanjiskip_stretch, g.kanjiskip_shrink
769 -- Nq.last (kern w) .... (glue/kern g) Np.first
770 local function real_insert(g)
772 head, Np.first = insert_before(head, Np.first, node_inherit_attr(g, Nq.nuc, Np.nuc))
774 if not ngk then Np.gk = g
775 elseif type(ngk)=="table" then ngk[#ngk+1]=g
776 else Np.gk = { ngk, g } end
781 -------------------- 和文文字間空白量の決定
784 local round = tex.round
785 local bg_ag = 2*id_glue - id_glue
786 local bg_ak = 2*id_glue - id_kern
787 local bk_ag = 2*id_kern - id_glue
788 local bk_ak = 2*id_kern - id_kern
789 local getkern = node.direct.getkern
791 local function blend_diffmet(b, a, rb, ra)
792 return round(luatexja.jfmglue.diffmet_rule((1-rb)*b+rb*a, (1-ra)*b+ra*a))
794 local blend_diffmet_inf
796 local abs, log, log264, floor = math.abs, math.log, math.log(2)*64, math.floor
797 blend_diffmet_inf = function (b, a, bo, ao, rb, ra)
798 local nb, na = (bo and b*2.0^(64*bo) or 0), (ao and a*2.0^(64*ao) or 0)
799 local r = luatexja.jfmglue.diffmet_rule((1-rb)*nb+rb*na, (1-ra)*nb+ra*na)
800 local ro = (r~=0) and floor(log(abs(r))/log264+0.0625) or 0
801 return round(r/2.^(64*ro)), ro
804 local getglue = luatexja.getglue
805 calc_ja_ja_aux = function (gb, ga, db, da)
806 if luatexja.jfmglue.diffmet_rule ~= math.two_pleft and diffmet_rule ~= math.two_pright
807 and luatexja.jfmglue.diffmet_rule ~= math.two_paverage then
811 if ga then gb = node_new(id_kern, 1); setkern(gb, 0)
814 ga = node_new(id_kern, 1); setkern(ga, 0)
816 local gbw, gaw, gbst, gast, gbsto, gasto, gbsh, gash, gbsho, gasho
817 if getid(gb)==id_glue then
818 gbw, gbst, gbsh, gbsto, gbsho = getglue(gb)
822 if getid(ga)==id_glue then
823 gaw, gast, gash, gasto, gasho = getglue(ga)
827 if not (gbst or gast) then -- 両方とも kern
828 setkern(gb, blend_diffmet(gbw, gaw, db, da))
829 node_free(ga); return gb
832 if not gbst then gr = ga; node_free(gb) else node_free(ga) end
833 gbw = blend_diffmet(gbw or 0, gaw or 0, db, da) -- 結果の自然長
834 gbst, gbsto = blend_diffmet_inf(gbst, gast, gbsto, gasto, db, da) -- 伸び
835 gbsh, gbsho = blend_diffmet_inf(-(gbsh or 0), -(gash or 0), gbsho, gasho, db, da) -- -(縮み)
836 setglue(gr, gbw, gbst, -gbsh, gbsto, gbsho)
842 local null_skip_table = {0, 0, 0}
844 local get_kanjiskip, kanjiskip_jfm_flag
845 local get_kanjiskip_low
846 local calc_ja_ja_glue
848 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
849 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
851 get_kanjiskip_low = function(flag, qm, bn, bp, bh)
852 -- flag = false: kanjiskip そのもの(パラメータ or JFM)
853 -- ノード kanji_skip のコピーで良い場合は nil が帰る
854 -- flag = true: JFM グルーに付随する kanjiskip 自然長/伸び/縮み分
855 if qm.with_kanjiskip and (bn or bp or bh) then
856 if kanjiskip_jfm_flag then
857 local g = node_new(id_glue);
858 local bk = qm.kanjiskip or null_skip_table
859 setglue(g, bn and (bn*bk[1]) or 0,
860 bp and (bp*bk[2]) or 0,
861 bh and (bh*bk[3]) or 0, 0, 0)
862 set_attr(g, attr_icflag, KANJI_SKIP_JFM)
865 local g = node_new(id_glue)
866 local st = bp and (bp*getfield(kanji_skip, 'stretch')) or 0
867 local sh = bh and (bh*getfield(kanji_skip, 'shrink')) or 0
869 bn and (bn*getwidth(kanji_skip)) or 0,
871 (st==0) and 0 or getfield(kanji_skip, 'stretch_order'),
872 (sh==0) and 0 or getfield(kanji_skip, 'shrink_order'))
873 set_attr(g, attr_icflag, KANJI_SKIP_JFM)
879 get_kanjiskip = function()
880 if Np.auto_kspc==0 or Nq.auto_kspc==0 then return nil
881 elseif Np.auto_kspc or Nq.auto_kspc then
882 local pm, qm = Np.met, Nq.met
883 if (pm.char_type==qm.char_type) and (qm.var==pm.var) then
884 return get_kanjiskip_low(false, qm, 1, 1, 1) or node_copy(kanji_skip)
886 local gb = get_kanjiskip_low(false, qm, 1, 1, 1)
888 return calc_ja_ja_aux(gb,
889 get_kanjiskip_low(false, pm, 1, 1, 1) or node_copy(kanji_skip), 0, 1)
891 local ga = get_kanjiskip_low(false, pm, 1, 1, 1)
892 return (ga and calc_ja_ja_aux(node_copy(kanji_skip), ga, 0, 1))
893 or node_copy(kanji_skip)
897 local g = node_new(id_glue)
898 set_attr(g, attr_icflag, kanjiskip_jfm_flag and KANJI_SKIP_JFM or KANJI_SKIP)
903 calc_ja_ja_glue = function ()
904 local qm, pm = Nq.met, Np.met
905 local qmc, pmc = qm.char_type, pm.char_type
906 if (qmc==pmc) and (qm.var==pm.var) then
907 local g, _, kn, kp, kh = new_jfm_glue(qmc, Nq.class, Np.class)
908 return g, (Np.auto_kspc or Nq.auto_kspc) and get_kanjiskip_low(true, qm, kn, kp, kh)
910 local npn, nqn = Np.nuc, Nq.nuc
911 local gb, db, bn, bp, bh
912 = new_jfm_glue(qmc, Nq.class,
913 slow_find_char_class(Np.char,
915 local ga, da, an, ap, ah
917 slow_find_char_class(Nq.char,
920 local g = calc_ja_ja_aux(gb, ga, db, da)
922 gb = get_kanjiskip_low(true, qm, bn, bp, bh)
923 ga = get_kanjiskip_low(true, pm, an, ap, ah)
924 k = calc_ja_ja_aux(gb, ga, db, da)
930 -------------------- 和欧文間空白量の決定
933 local get_xkanjiskip, xkanjiskip_jfm_flag
934 local get_xkanjiskip_normal, get_xkanjiskip_jfm
935 local get_xkanjiskip_low
937 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
938 local XKANJI_SKIP_JFM = luatexja.icflag_table.XKANJI_SKIP_JFM
940 get_xkanjiskip_low = function(flag, qm, bn, bp, bh)
941 if flag or (qm.with_kanjiskip and (bn or bp or bh)) then
942 if xkanjiskip_jfm_flag then
943 local g = node_new(id_glue);
944 local bk = qm.xkanjiskip or null_skip_table
945 setglue(g, bn and bk[1] or 0,
947 bh and bk[3] or 0, 0, 0)
948 set_attr(g, attr_icflag, XKANJI_SKIP_JFM)
951 return node_copy(xkanji_skip)
953 local g = node_new(id_glue)
954 local w, st, sh, sto, sho = getglue(xkanji_skip)
961 set_attr(g, attr_icflag, XKANJI_SKIP_JFM)
967 get_xkanjiskip = function(Nn)
968 if Np.auto_xspc==0 or Nq.auto_xspc==0 then
970 elseif (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
971 return get_xkanjiskip_low(true, Nn.met, 1, 1, 1)
973 local g = node_new(id_glue)
974 set_attr(g, attr_icflag, xkanjiskip_jfm_flag and XKANJI_SKIP_JFM or XKANJI_SKIP)
980 -------------------- 隣接した「塊」間の処理
982 local function combine_spc(name)
983 return (Np[name] or Nq[name]) and ((Np[name]~=0) and (Nq[name]~=0))
986 -- NA, NB: alchar or math
987 local function get_NA_skip()
989 local qclass = fast_find_char_class(
990 (Nq.id == id_math and -1 or (Nq.xspc>=2 and 'alchar' or 'nox_alchar')), pm)
991 local g, _, kn, kp, kh = new_jfm_glue(pm.char_type, qclass, Np.class)
992 local k = g and (Nq.xspc>=2) and (Np.xspc%2==1) and combine_spc 'auto_xspc'
993 and get_kanjiskip_low(true, pm, kn, kp, kh)
996 local function get_NB_skip()
998 local pclass = fast_find_char_class(
999 (Np.id == id_math and -1 or (Np.xspc%2==1 and 'alchar' or 'nox_alchar')), qm)
1000 local g, _, kn, kp, kh = new_jfm_glue(qm.char_type, Nq.class, pclass)
1001 local k = g and (Nq.xspc>=2) and (Np.xspc%2==1) and combine_spc 'auto_xspc'
1002 and get_kanjiskip_low(true, qm, kn, kp, kh)
1006 local function get_OA_skip(insert_ksp)
1008 local g, _, kn, kp, kh = new_jfm_glue(
1010 fast_find_char_class(
1011 (((Nq.id==id_glue)or(Nq.id==id_kern)) and 'glue' or 'jcharbdd'), pm),
1015 k = (combine_spc 'auto_kspc') and get_kanjiskip_low(true, pm, kn, kp, kh)
1019 local function get_OB_skip(insert_ksp)
1021 local g, _, kn, kp, kh = new_jfm_glue(
1022 qm.char_type, Nq.class,
1023 fast_find_char_class(
1024 (((Np.id==id_glue)or(Np.id==id_kern)) and 'glue' or 'jcharbdd'), qm))
1027 k = (combine_spc 'auto_kspc') and get_kanjiskip_low(true, qm, kn, kp, kh)
1032 -- (anything) .. jachar
1033 local function handle_np_jachar(mode)
1035 if qid==id_jglyph or ((qid==id_pbox or qid==id_pbox_w) and Nq.met) then
1037 if non_ihb_flag then g, k = calc_ja_ja_glue() end -- M->K
1038 if not g then g = get_kanjiskip() end
1039 handle_penalty_normal(Nq.post, Np.pre, g);
1040 real_insert(g); real_insert(k)
1041 elseif Nq.met then -- qid==id_hlist
1043 if non_ihb_flag then g, k = get_OA_skip(true) end -- O_A->K
1044 if not g then g = get_kanjiskip() end
1045 handle_penalty_normal(0, Np.pre, g); real_insert(g); real_insert(k)
1047 local g, k; if non_ihb_flag then g, k = get_NA_skip() end -- N_A->X
1048 if not g then g = get_xkanjiskip(Np) end
1049 handle_penalty_normal((qid==id_hlist and 0 or Nq.post), Np.pre, g);
1050 real_insert(g); real_insert(k)
1052 local g = non_ihb_flag and (get_OA_skip()) -- O_A
1053 if qid==id_glue then handle_penalty_normal(0, Np.pre, g)
1054 elseif qid==id_kern then handle_penalty_suppress(0, Np.pre, g)
1055 else handle_penalty_always(0, Np.pre, g)
1059 if mode and Np.kcat%2~=1 then
1060 widow_Np.first, widow_Bp, Bp = Np.first, Bp, widow_Bp
1065 -- jachar .. (anything)
1066 local function handle_nq_jachar()
1068 local g, k; if non_ihb_flag then g, k = get_NB_skip()end -- N_B->X
1069 if not g then g = get_xkanjiskip(Nq) end
1070 handle_penalty_normal(Nq.post, (Np.id==id_hlist and 0 or Np.pre), g);
1071 real_insert(g); real_insert(k)
1073 local g =non_ihb_flag and (get_OB_skip()) -- O_B
1074 if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
1075 elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
1076 else handle_penalty_always(Nq.post, 0, g)
1082 -- (anything) .. (和文文字で始まる hlist)
1083 local function handle_np_ja_hlist()
1085 if qid==id_jglyph or ((qid==id_pbox or qid == id_pbox_w) and Nq.met) then
1086 local g = non_ihb_flag and get_OB_skip(true) or get_kanjiskip() -- O_B->K
1087 handle_penalty_normal(Nq.post, 0, g); real_insert(g)
1088 elseif Nq.met then -- Nq.id==id_hlist
1089 local g = get_kanjiskip() -- K
1090 handle_penalty_suppress(0, 0, g); real_insert(g)
1092 local g = get_xkanjiskip(Np) -- X
1093 handle_penalty_suppress(0, 0, g); real_insert(g)
1097 -- (和文文字で終わる hlist) .. (anything)
1098 local function handle_nq_ja_hlist()
1100 local g = get_xkanjiskip(Nq) -- X
1101 handle_penalty_suppress(0, 0, g); real_insert(g)
1106 -- Nq が前側のクラスタとなることによる修正
1109 local adjust_nq_aux = {
1110 [id_glyph] = function() after_alchar(Nq) end, -- after_alchar(Nq)
1111 [id_hlist] = function() after_hlist(Nq) end,
1112 [id_pbox] = function() after_hlist(Nq) end,
1113 [id_disc] = function() after_hlist(Nq) end,
1114 [id_glue] = function()
1115 luatexbase.call_callback("luatexja.jfmglue.special_jaglue_after", Nq.nuc)
1117 [id_pbox_w]= function()
1118 local hh = luatexbase.call_callback("luatexja.jfmglue.whatsit_after", false, Nq, Np, head)
1119 -- hh: new head of false (nott processed)
1120 if hh then head = hh end
1124 adjust_nq = function()
1125 local x = adjust_nq_aux[Nq.id]
1131 -------------------- 開始・終了時の処理
1133 local node_prev = node.direct.getprev
1135 local function handle_list_tail(mode, last)
1138 -- the current list is to be line-breaked.
1139 -- Insert \jcharwidowpenalty
1140 if widow_Np.first then handle_penalty_jwp() end
1143 -- the current list is the contents of a hbox
1144 local npi, pm = Np.id, Np.met
1145 if npi == id_jglyph or (npi==id_pbox and pm) then
1146 local g = new_jfm_glue(pm.char_type, Np.class, fast_find_char_class('boxbdd', pm))
1148 set_attr(g, attr_icflag, BOXBDD)
1149 head = insert_after(head, Np.last, g)
1156 local function handle_list_head(par_indented)
1157 local npi, pm = Np.id, Np.met
1158 if npi == id_jglyph or (npi==id_pbox and pm) then
1159 if non_ihb_flag then
1160 local g = new_jfm_glue(pm.char_type, fast_find_char_class(par_indented, pm), Np.class)
1162 set_attr(g, attr_icflag, BOXBDD)
1163 if getid(g)==id_glue and #Bp==0 then
1164 local h = node_new(id_penalty, nil, Np.nuc)
1165 setpenalty(h, 10000); set_attr(h, attr_icflag, BOXBDD)
1167 head = insert_before(head, Np.first, g)
1174 -- return value: (the initial cursor lp), (last node)
1177 local id_local = node.id 'local_par'
1178 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
1179 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
1180 local KSK = luatexja.stack_table_index.KSK
1181 local XSK = luatexja.stack_table_index.XSK
1182 local dir_yoko = luatexja.dir_table.dir_yoko
1183 local dir_tate = luatexja.dir_table.dir_tate
1184 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
1185 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
1186 local table_pool = {
1187 {}, {}, {first=nil},
1188 { auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
1189 first=nil, id=nil, last=nil, met=nil, nuc=nil,
1190 post=nil, pre=nil, xspc=nil, gk=nil },
1191 { auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
1192 first=nil, id=nil, last=nil, met=nil, nuc=nil,
1193 post=nil, pre=nil, xspc=nil, gk=nil },
1195 init_var = function (mode,dir)
1196 -- 1073741823: max_dimen
1197 Bp, widow_Bp, widow_Np, Np, Nq
1198 = table_pool[1], table_pool[2], table_pool[3], table_pool[4], table_pool[5]
1199 for i=1,5 do for j,_ in pairs(table_pool[i]) do table_pool[i][j]=nil end end
1200 table_current_stack = ltjs.table_current_stack
1202 list_dir, tex_dir = (ltjs.list_dir or dir_yoko), (dir or 'TLT')
1203 local is_dir_tate = list_dir==dir_tate
1204 capsule_glyph = is_dir_tate and ltjw.capsule_glyph_tate or ltjw.capsule_glyph_yoko
1205 attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
1206 local TEMP = node_new(id_glue)
1207 -- TEMP is a dummy node, which will be freed at the end of the callback.
1208 -- Without this node, set_attr(kanji_skip, ...) somehow creates an "orphaned" attribute list.
1210 kanji_skip, kanjiskip_jfm_flag = skip_table_to_glue(KSK)
1211 set_attr(kanji_skip, attr_icflag, KANJI_SKIP)
1214 xkanji_skip, xkanjiskip_jfm_flag = skip_table_to_glue(XSK)
1215 set_attr(xkanji_skip, attr_icflag, XKANJI_SKIP)
1218 -- the current list is to be line-breaked:
1219 -- hbox from \parindent is skipped.
1220 local lp, par_indented, lpi, lps = head, 'boxbdd', getid(head), getsubtype(head)
1222 ((lpi==id_whatsit and lps~=sid_user)
1223 or ((lpi==id_hlist) and (lps==3))
1224 or (lpi==id_local)) do
1225 if (lpi==id_hlist) and (lps==3) then
1226 Np.char, par_indented = 'parbdd', 'parbdd'
1227 Np.width = getwidth(lp)
1229 lp=node_next(lp); lpi, lps = getid(lp), getsubtype(lp) end
1230 return lp, node_tail(head), par_indented, TEMP
1232 return head, nil, 'boxbdd', TEMP
1237 -------------------- 外部から呼ばれる関数
1239 local ensure_tex_attr = ltjb.ensure_tex_attr
1240 local tex_getattr = tex.getattribute
1242 function luatexja.jfmglue.main(ahead, mode, dir)
1243 if not ahead then return ahead end
1245 local lp, last, par_indented, TEMP = init_var(mode,dir)
1246 lp = calc_np(last, lp)
1248 handle_list_head(par_indented)
1249 lp = calc_np(last,lp);
1252 local pid, pm = Np.id, Np.met
1254 if pid == id_jglyph then
1255 handle_np_jachar(mode)
1257 if pid==id_hlist then handle_np_ja_hlist()
1258 else handle_np_jachar() end
1260 if Nq.id==id_hlist then handle_nq_ja_hlist()
1261 else handle_nq_jachar() end
1263 lp = calc_np(last,lp)
1265 handle_list_tail(mode, last)
1267 -- adjust attr_icflag for avoiding error
1268 if tex_getattr(attr_icflag)~=0 then ensure_tex_attr(attr_icflag, 0) end
1269 node_free(kanji_skip);
1270 node_free(xkanji_skip); node_free(TEMP)
1276 local IHB = luatexja.userid_table.IHB
1277 local BPAR = luatexja.userid_table.BPAR
1278 local BOXB = luatexja.userid_table.BOXB
1279 local node_prev = node.direct.getprev
1280 local node_write = node.direct.write
1282 -- \inhibitglue, \disinhibitglue
1283 local function ihb_node(v)
1284 local tn = node_new(id_whatsit, sid_user)
1285 setfield(tn, 'user_id', IHB)
1286 setfield(tn, 'type', 100)
1287 setfield(tn, 'value', v)
1290 function luatexja.jfmglue.create_inhibitglue_node()
1293 function luatexja.jfmglue.create_disinhibitglue_node()
1297 -- Node for indicating beginning of a paragraph
1298 -- (for ltjsclasses)
1299 function luatexja.jfmglue.create_beginpar_node()
1300 local tn = node_new(id_whatsit, sid_user)
1301 setfield(tn, 'user_id', BPAR)
1302 setfield(tn, 'type', 100)
1303 setfield(tn, 'value', 1)
1307 -- Node for indicating a head/end of a box
1308 function luatexja.jfmglue.create_boxbdd_node()
1309 local tn = node_new(id_whatsit, sid_user)
1310 setfield(tn, 'user_id', BOXB)
1311 setfield(tn, 'type', 100)
1312 setfield(tn, 'value', 1)
1316 local function whatsit_callback(Np, lp, Nq)
1317 if Np and Np.nuc then return Np
1318 elseif Np and getfield(lp, 'user_id') == BPAR then
1319 Np.first = lp; Np.nuc = lp; Np.last = lp
1321 elseif Np and getfield(lp, 'user_id') == BOXB then
1322 Np.first = lp; Np.nuc = lp; Np.last = lp
1325 Np.class = fast_find_char_class('boxbdd', Nq.met)
1327 Np.met = Nq.met; Np.pre = 0; Np.post = 0; Np.xspc = 0
1328 Np.auto_xspc, Np.auto_kspc = 0, 0
1336 local function whatsit_after_callback(s, Nq, Np, head)
1337 if not s and getfield(Nq.nuc, 'user_id') == BPAR then
1338 local x, y = node_prev(Nq.nuc), Nq.nuc
1339 Nq.first, Nq.nuc, Nq.last = x, x, x
1342 Nq.class = fast_find_char_class('parbdd', Np.met)
1344 Nq.met = Np.met; Nq.pre = 0; Nq.post = 0; Nq.xspc = 0
1345 Nq.auto_xspc, Nq.auto_kspc = 0, 0
1347 s = node_remove(head, y)
1349 elseif not s and getfield(Nq.nuc, 'user_id') == BOXB then
1350 local x, y = node_prev(Nq.nuc), Nq.nuc
1351 Nq.first, Nq.nuc, Nq.last = x, x, x
1354 Nq.class = fast_find_char_class('boxbdd', Np.met)
1356 Nq.met = Np.met; Nq.pre = 0; Nq.post = 0; Nq.xspc = 0
1357 Nq.auto_xspc, Nq.auto_kspc = 0, 0
1359 s = node_remove(head, y)
1365 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
1366 "luatexja.beginpar.np_info", 1)
1367 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
1368 "luatexja.beginpar.np_info_after", 1)
1372 local node_prev = node.direct.getprev
1373 local node_write = node.direct.write
1374 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
1375 local XKANJI_SKIP_JFM = luatexja.icflag_table.XKANJI_SKIP_JFM
1376 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
1377 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
1378 local XSK = luatexja.stack_table_index.XSK
1379 local KSK = luatexja.stack_table_index.KSK
1380 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
1381 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
1382 local getcount, abs, scan_keyword = tex.getcount, math.abs, token.scan_keyword
1383 local tex_nest = tex.nest
1384 local tex_getattr = tex.getattribute
1385 local get_current_jfont
1387 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
1388 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
1389 local dir_tate = luatexja.dir_table.dir_tate
1390 local get_dir_count = ltjd.get_dir_count
1391 function get_current_jfont()
1392 return tex_getattr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
1395 -- \insertxkanjiskip
1396 -- SPECIAL_JAGLUE のノード:
1397 -- * (X)KANJI_SKIP(_JFM): その場で値が決まっている
1398 -- * PROCESSED_BEGIN_FLAG + (X)KANJI_SKIP: 段落終了時に決める
1399 local function insert_k_skip_common(ind, name, ica, icb)
1400 if abs(tex_nest[tex_nest.ptr].mode) ~= ltjs.hmode then return end
1401 local g = node_new(id_glue); set_attr(g, attr_icflag, SPECIAL_JAGLUE)
1402 local is_late = scan_keyword("late")
1404 local st = ltjs.get_stack_skip(ind, getcount('ltj@@stack'))
1405 if st.width==1073741823 then
1406 local bk = ltjf_font_metric_table[get_current_jfont()][name]
1408 setglue(g, bk[1] or 0, bk[2] or 0, bk[3] or 0, 0, 0)
1410 set_attr(g, attr_yablshift, icb); node_write(g); return
1412 setglue(g, st.width, st.stretch, st.shrink, st.stretch_order, st.shrink_order)
1413 set_attr(g, attr_yablshift, ica)
1415 set_attr(g, attr_yablshift, PROCESSED_BEGIN_FLAG + ica)
1416 set_attr(g, attr_tablshift, get_current_jfont())
1420 function luatexja.jfmglue.insert_xk_skip()
1421 insert_k_skip_common(XSK, "xkanjiskip", XKANJI_SKIP, XKANJI_SKIP_JFM)
1423 function luatexja.jfmglue.insert_k_skip()
1424 insert_k_skip_common(KSK, "kanjiskip", KANJI_SKIP, KANJI_SKIP_JFM)
1427 local function special_jaglue(lx)
1428 local lxi = get_attr_icflag(lx)
1429 if lxi==SPECIAL_JAGLUE then
1430 non_ihb_flag = false; return false
1435 local function special_jaglue_after_inner(lx, lxi, lxi_jfm, kn, bk)
1436 local w, st, sh, sto, sho = getglue(kn)
1437 if w~=1073741823 then
1438 setglue(lx, w, st, sh, sto, sho); set_attr(lx, attr_icflag, lxi)
1440 local m = ltjf_font_metric_table[has_attr(lx, attr_tablshift)]
1441 setglue(lx, bk[1], bk[2], bk[3], 0, 0)
1442 set_attr(lx, attr_icflag, lxi_jfm)
1445 local function special_jaglue_after(lx)
1446 if get_attr_icflag(lx)==SPECIAL_JAGLUE then
1447 lxi=has_attr(lx, attr_yablshift)
1448 if lxi>=PROCESSED_BEGIN_FLAG then
1449 lxi = lxi%PROCESSED_BEGIN_FLAG
1450 if lxi == KANJI_SKIP then
1451 special_jaglue_after_inner(lx, lxi, KANJI_SKIP_JFM, kanji_skip,
1452 ltjf_font_metric_table[has_attr(lx, attr_tablshift)].kanjiskip or null_skip_table)
1453 else -- lxi == XKANJI_SKIP
1454 special_jaglue_after_inner(lx, lxi, XKANJI_SKIP_JFM, xkanji_skip,
1455 ltjf_font_metric_table[has_attr(lx, attr_tablshift)].xkanjiskip or null_skip_table)
1458 set_attr(lx, attr_icflag, lxi)
1461 if node_prev(lx) then
1462 local lxp = node_prev(lx)
1463 if lxp and getid(lxp)==id_penalty and get_attr_icflag(lxp)==KINSOKU then
1467 non_ihb_flag = false; return false
1471 luatexbase.create_callback("luatexja.jfmglue.special_jaglue", "list",
1473 luatexbase.create_callback("luatexja.jfmglue.special_jaglue_after", "list",
1474 special_jaglue_after)
1478 luatexja.jfmglue.after_hlist = after_hlist
1479 luatexja.jfmglue.check_box_high = check_box_high