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 = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
628 function set_np_xspc_jachar_hbox(Nx, x)
629 local m = ltjf_font_metric_table[getfont(x)]
630 local c = has_attr(x, attr_jchar_code) or getchar(x)
631 Nx.met, Nx.char = m, c; Nx.class = has_attr(x, attr_jchar_class) or 0;
632 local mc = m.char_type; Nx.char_type = mc
633 Nx.pre = table_current_stack[PRE + c] or 0
634 Nx.post = table_current_stack[POST + c] or 0
635 Nx.xspc = table_current_stack[XSP + c] or 3
636 Nx.kcat = table_current_stack[KCAT + c] or 0
637 Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
641 local floor = math.floor
642 local nullfunc = function(n) return n end
643 function set_np_xspc_alchar(Nx, c,x, lig)
645 local f = (lig ==1) and nullfunc or node_tail
646 local xc, xs = getcomponents(x), getsubtype(x)
647 while xc and xs and xs%4>=2 do
649 if getid(x)==id_disc then x, xc, xs = nil, getfield(x,'replace'), 2
650 else xc, xs = getcomponents(x), getsubtype(x) end
652 c = x and getchar(x) or c
653 Nx.pre = table_current_stack[PRE + c] or 0
654 Nx.post = table_current_stack[POST + c] or 0
656 Nx.pre, Nx.post = 0, 0
659 Nx.xspc = table_current_stack[XSP + c] or 3
660 Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
662 local set_np_xspc_alchar = set_np_xspc_alchar
663 -- change the information for the next loop
664 -- (will be done if Nx is an alphabetic character or a hlist)
665 after_hlist = function (Nx)
666 local s = Nx.last_char
668 if getid(s)==id_glyph then
669 if if_lang_ja(s) then
670 set_np_xspc_jachar_hbox(Nx, s)
672 set_np_xspc_alchar(Nx, getchar(s), s, 2)
675 set_np_xspc_alchar(Nx, -1, s)
678 Nx.pre, Nx.met = nil, nil
681 after_alchar = function (Nx)
682 local x = Nx.last_char
683 return set_np_xspc_alchar(Nx, getchar(x), x, 2)
688 -------------------- 最下層の処理
690 luatexbase.create_callback('luatexja.adjust_jfmglue', 'simple', function(n) return n end)
692 -- change penalties (or create a new penalty, if needed)
693 local function handle_penalty_normal(post, pre, g)
694 luatexbase.call_callback('luatexja.adjust_jfmglue', head, Nq, Np, Bp)
695 local a = (pre or 0) + (post or 0)
697 if (a~=0 and not(g and getid(g)==id_kern)) then
698 local p = node_new(id_penalty, nil, Nq.nuc, Np.nuc)
699 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
700 setpenalty(p, a); head = insert_before(head, Np.first, p)
701 Bp[1]=p; set_attr(p, attr_icflag, KINSOKU)
703 else for _, v in pairs(Bp) do add_penalty(v,a) end
707 local function handle_penalty_always(post, pre, g)
708 luatexbase.call_callback('luatexja.adjust_jfmglue', head, Nq, Np, Bp)
709 local a = (pre or 0) + (post or 0)
711 if not (g and getid(g)==id_glue) or a~=0 then
712 local p = node_new(id_penalty, nil, Nq.nuc, Np.nuc)
713 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
714 setpenalty(p, a); head = insert_before(head, Np.first, p)
715 Bp[1]=p; set_attr(p, attr_icflag, KINSOKU)
717 else for _, v in pairs(Bp) do add_penalty(v,a) end
721 local function handle_penalty_suppress(post, pre, g)
722 luatexbase.call_callback('luatexja.adjust_jfmglue', head, Nq, Np, Bp)
724 if g and getid(g)==id_glue then
725 local p = node_new(id_penalty, nil, Nq.nuc, Np.nuc)
726 setpenalty(p, 10000); head = insert_before(head, Np.first, p)
727 Bp[1]=p; set_attr(p, attr_icflag, KINSOKU)
730 local a = (pre or 0) + (post or 0)
731 for _, v in pairs(Bp) do add_penalty(v,a) end
735 local function handle_penalty_jwp()
736 local a = table_current_stack[luatexja.stack_table_index.JWP]
737 if #widow_Bp == 0 then
739 local p = node_new(id_penalty, widow_Np.nuc)
740 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
741 setpenalty(p, a); head = insert_before(head, widow_Np.first, p)
742 widow_Bp[1]=p; set_attr(p, attr_icflag, KINSOKU)
744 else for _, v in pairs(widow_Bp) do add_penalty(v,a) end
748 -- 和文文字間の JFM glue を node 化
749 local function new_jfm_glue(mc, bc, ac)
750 -- bc, ac: char classes
754 local k = node_new(id_kern, 1); setkern(k, g[1])
755 set_attr(k, attr_icflag, FROM_JFM)
756 return k, g.ratio, false, false, false
758 local f = node_new(id_glue)
759 set_attr(f, attr_icflag, g.priority)
760 setglue(f, g.width, g.stretch, g.shrink)
761 return f, g.ratio, g.kanjiskip_natural, g.kanjiskip_stretch, g.kanjiskip_shrink
767 -- Nq.last (kern w) .... (glue/kern g) Np.first
768 local function real_insert(g)
770 head, Np.first = insert_before(head, Np.first, node_inherit_attr(g, Nq.nuc, Np.nuc))
772 if not ngk then Np.gk = g
773 elseif type(ngk)=="table" then ngk[#ngk+1]=g
774 else Np.gk = { ngk, g } end
779 -------------------- 和文文字間空白量の決定
782 local round = tex.round
783 local bg_ag = 2*id_glue - id_glue
784 local bg_ak = 2*id_glue - id_kern
785 local bk_ag = 2*id_kern - id_glue
786 local bk_ak = 2*id_kern - id_kern
787 local getkern = node.direct.getkern
789 local function blend_diffmet(b, a, rb, ra)
790 return round(luatexja.jfmglue.diffmet_rule((1-rb)*b+rb*a, (1-ra)*b+ra*a))
792 local blend_diffmet_inf
794 local abs, log, log264, floor = math.abs, math.log, math.log(2)*64, math.floor
795 blend_diffmet_inf = function (b, a, bo, ao, rb, ra)
796 local nb, na = (bo and b*2.0^(64*bo) or 0), (ao and a*2.0^(64*ao) or 0)
797 local r = luatexja.jfmglue.diffmet_rule((1-rb)*nb+rb*na, (1-ra)*nb+ra*na)
798 local ro = (r~=0) and floor(log(abs(r))/log264+0.0625) or 0
799 return round(r/2.^(64*ro)), ro
802 local getglue = luatexja.getglue
803 calc_ja_ja_aux = function (gb, ga, db, da)
804 if luatexja.jfmglue.diffmet_rule ~= math.two_pleft and diffmet_rule ~= math.two_pright
805 and luatexja.jfmglue.diffmet_rule ~= math.two_paverage then
809 if ga then gb = node_new(id_kern, 1); setkern(gb, 0)
812 ga = node_new(id_kern, 1); setkern(ga, 0)
814 local gbw, gaw, gbst, gast, gbsto, gasto, gbsh, gash, gbsho, gasho
815 if getid(gb)==id_glue then
816 gbw, gbst, gbsh, gbsto, gbsho = getglue(gb)
820 if getid(ga)==id_glue then
821 gaw, gast, gash, gasto, gasho = getglue(ga)
825 if not (gbst or gast) then -- 両方とも kern
826 setkern(gb, blend_diffmet(gbw, gaw, db, da))
827 node_free(ga); return gb
830 if not gbst then gr = ga; node_free(gb) else node_free(ga) end
831 gbw = blend_diffmet(gbw or 0, gaw or 0, db, da) -- 結果の自然長
832 gbst, gbsto = blend_diffmet_inf(gbst, gast, gbsto, gasto, db, da) -- 伸び
833 gbsh, gbsho = blend_diffmet_inf(-(gbsh or 0), -(gash or 0), gbsho, gasho, db, da) -- -(縮み)
834 setglue(gr, gbw, gbst, -gbsh, gbsto, gbsho)
840 local null_skip_table = {0, 0, 0}
842 local get_kanjiskip, kanjiskip_jfm_flag
843 local get_kanjiskip_low
844 local calc_ja_ja_glue
846 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
847 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
849 get_kanjiskip_low = function(flag, qm, bn, bp, bh)
850 -- flag = false: kanjiskip そのもの(パラメータ or JFM)
851 -- ノード kanji_skip のコピーで良い場合は nil が帰る
852 -- flag = true: JFM グルーに付随する kanjiskip 自然長/伸び/縮み分
853 if qm.with_kanjiskip and (bn or bp or bh) then
854 if kanjiskip_jfm_flag then
855 local g = node_new(id_glue);
856 local bk = qm.kanjiskip or null_skip_table
857 setglue(g, bn and (bn*bk[1]) or 0,
858 bp and (bp*bk[2]) or 0,
859 bh and (bh*bk[3]) or 0, 0, 0)
860 set_attr(g, attr_icflag, KANJI_SKIP_JFM)
863 local g = node_new(id_glue)
864 local st = bp and (bp*getfield(kanji_skip, 'stretch')) or 0
865 local sh = bh and (bh*getfield(kanji_skip, 'shrink')) or 0
867 bn and (bn*getwidth(kanji_skip)) or 0,
869 (st==0) and 0 or getfield(kanji_skip, 'stretch_order'),
870 (sh==0) and 0 or getfield(kanji_skip, 'shrink_order'))
871 set_attr(g, attr_icflag, KANJI_SKIP_JFM)
877 get_kanjiskip = function()
878 if Np.auto_kspc==0 or Nq.auto_kspc==0 then return nil
879 elseif Np.auto_kspc or Nq.auto_kspc then
880 local pm, qm = Np.met, Nq.met
881 if (pm.char_type==qm.char_type) and (qm.var==pm.var) then
882 return get_kanjiskip_low(false, qm, 1, 1, 1) or node_copy(kanji_skip)
884 local gb = get_kanjiskip_low(false, qm, 1, 1, 1)
886 return calc_ja_ja_aux(gb,
887 get_kanjiskip_low(false, pm, 1, 1, 1) or node_copy(kanji_skip), 0, 1)
889 local ga = get_kanjiskip_low(false, pm, 1, 1, 1)
890 return (ga and calc_ja_ja_aux(node_copy(kanji_skip), ga, 0, 1))
891 or node_copy(kanji_skip)
895 local g = node_new(id_glue)
896 set_attr(g, attr_icflag, kanjiskip_jfm_flag and KANJI_SKIP_JFM or KANJI_SKIP)
901 calc_ja_ja_glue = function ()
902 local qm, pm = Nq.met, Np.met
903 local qmc, pmc = qm.char_type, pm.char_type
904 if (qmc==pmc) and (qm.var==pm.var) then
905 local g, _, kn, kp, kh = new_jfm_glue(qmc, Nq.class, Np.class)
906 return g, (Np.auto_kspc or Nq.auto_kspc) and get_kanjiskip_low(true, qm, kn, kp, kh)
908 local npn, nqn = Np.nuc, Nq.nuc
909 local gb, db, bn, bp, bh
910 = new_jfm_glue(qmc, Nq.class,
911 slow_find_char_class(Np.char,
913 local ga, da, an, ap, ah
915 slow_find_char_class(Nq.char,
918 local g = calc_ja_ja_aux(gb, ga, db, da)
920 gb = get_kanjiskip_low(true, qm, bn, bp, bh)
921 ga = get_kanjiskip_low(true, pm, an, ap, ah)
922 k = calc_ja_ja_aux(gb, ga, db, da)
928 -------------------- 和欧文間空白量の決定
931 local get_xkanjiskip, xkanjiskip_jfm_flag
932 local get_xkanjiskip_normal, get_xkanjiskip_jfm
933 local get_xkanjiskip_low
935 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
936 local XKANJI_SKIP_JFM = luatexja.icflag_table.XKANJI_SKIP_JFM
938 get_xkanjiskip_low = function(flag, qm, bn, bp, bh)
939 if flag or (qm.with_kanjiskip and (bn or bp or bh)) then
940 if xkanjiskip_jfm_flag then
941 local g = node_new(id_glue);
942 local bk = qm.xkanjiskip or null_skip_table
943 setglue(g, bn and bk[1] or 0,
945 bh and bk[3] or 0, 0, 0)
946 set_attr(g, attr_icflag, XKANJI_SKIP_JFM)
949 return node_copy(xkanji_skip)
951 local g = node_new(id_glue)
952 local w, st, sh, sto, sho = getglue(xkanji_skip)
959 set_attr(g, attr_icflag, XKANJI_SKIP_JFM)
965 get_xkanjiskip = function(Nn)
966 if Np.auto_xspc==0 or Nq.auto_xspc==0 then
968 elseif (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
969 return get_xkanjiskip_low(true, Nn.met, 1, 1, 1)
971 local g = node_new(id_glue)
972 set_attr(g, attr_icflag, xkanjiskip_jfm_flag and XKANJI_SKIP_JFM or XKANJI_SKIP)
978 -------------------- 隣接した「塊」間の処理
980 local function combine_spc(name)
981 return (Np[name] or Nq[name]) and ((Np[name]~=0) and (Nq[name]~=0))
984 -- NA, NB: alchar or math
985 local function get_NA_skip()
987 local qclass = fast_find_char_class(
988 (Nq.id == id_math and -1 or (Nq.xspc>=2 and 'alchar' or 'nox_alchar')), pm)
989 local g, _, kn, kp, kh = new_jfm_glue(pm.char_type, qclass, Np.class)
990 local k = g and (Nq.xspc>=2) and (Np.xspc%2==1) and combine_spc 'auto_xspc'
991 and get_kanjiskip_low(true, pm, kn, kp, kh)
994 local function get_NB_skip()
996 local pclass = fast_find_char_class(
997 (Np.id == id_math and -1 or (Np.xspc%2==1 and 'alchar' or 'nox_alchar')), qm)
998 local g, _, kn, kp, kh = new_jfm_glue(qm.char_type, Nq.class, pclass)
999 local k = g and (Nq.xspc>=2) and (Np.xspc%2==1) and combine_spc 'auto_xspc'
1000 and get_kanjiskip_low(true, qm, kn, kp, kh)
1004 local function get_OA_skip(insert_ksp)
1006 local g, _, kn, kp, kh = new_jfm_glue(
1008 fast_find_char_class(
1009 (((Nq.id==id_glue)or(Nq.id==id_kern)) and 'glue' or 'jcharbdd'), pm),
1013 k = (combine_spc 'auto_kspc') and get_kanjiskip_low(true, pm, kn, kp, kh)
1017 local function get_OB_skip(insert_ksp)
1019 local g, _, kn, kp, kh = new_jfm_glue(
1020 qm.char_type, Nq.class,
1021 fast_find_char_class(
1022 (((Np.id==id_glue)or(Np.id==id_kern)) and 'glue' or 'jcharbdd'), qm))
1025 k = (combine_spc 'auto_kspc') and get_kanjiskip_low(true, qm, kn, kp, kh)
1030 -- (anything) .. jachar
1031 local function handle_np_jachar(mode)
1033 if qid==id_jglyph or ((qid==id_pbox or qid==id_pbox_w) and Nq.met) then
1035 if non_ihb_flag then g, k = calc_ja_ja_glue() end -- M->K
1036 if not g then g = get_kanjiskip() end
1037 handle_penalty_normal(Nq.post, Np.pre, g);
1038 real_insert(g); real_insert(k)
1039 elseif Nq.met then -- qid==id_hlist
1041 if non_ihb_flag then g, k = get_OA_skip(true) end -- O_A->K
1042 if not g then g = get_kanjiskip() end
1043 handle_penalty_normal(0, Np.pre, g); real_insert(g); real_insert(k)
1045 local g, k; if non_ihb_flag then g, k = get_NA_skip() end -- N_A->X
1046 if not g then g = get_xkanjiskip(Np) end
1047 handle_penalty_normal((qid==id_hlist and 0 or Nq.post), Np.pre, g);
1048 real_insert(g); real_insert(k)
1050 local g = non_ihb_flag and (get_OA_skip()) -- O_A
1051 if qid==id_glue then handle_penalty_normal(0, Np.pre, g)
1052 elseif qid==id_kern then handle_penalty_suppress(0, Np.pre, g)
1053 else handle_penalty_always(0, Np.pre, g)
1057 if mode and Np.kcat%2~=1 then
1058 widow_Np.first, widow_Bp, Bp = Np.first, Bp, widow_Bp
1063 -- jachar .. (anything)
1064 local function handle_nq_jachar()
1066 local g, k; if non_ihb_flag then g, k = get_NB_skip()end -- N_B->X
1067 if not g then g = get_xkanjiskip(Nq) end
1068 handle_penalty_normal(Nq.post, (Np.id==id_hlist and 0 or Np.pre), g);
1069 real_insert(g); real_insert(k)
1071 local g =non_ihb_flag and (get_OB_skip()) -- O_B
1072 if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
1073 elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
1074 else handle_penalty_always(Nq.post, 0, g)
1080 -- (anything) .. (和文文字で始まる hlist)
1081 local function handle_np_ja_hlist()
1083 if qid==id_jglyph or ((qid==id_pbox or qid == id_pbox_w) and Nq.met) then
1084 local g = non_ihb_flag and get_OB_skip(true) or get_kanjiskip() -- O_B->K
1085 handle_penalty_normal(Nq.post, 0, g); real_insert(g)
1086 elseif Nq.met then -- Nq.id==id_hlist
1087 local g = get_kanjiskip() -- K
1088 handle_penalty_suppress(0, 0, g); real_insert(g)
1090 local g = get_xkanjiskip(Np) -- X
1091 handle_penalty_suppress(0, 0, g); real_insert(g)
1095 -- (和文文字で終わる hlist) .. (anything)
1096 local function handle_nq_ja_hlist()
1098 local g = get_xkanjiskip(Nq) -- X
1099 handle_penalty_suppress(0, 0, g); real_insert(g)
1104 -- Nq が前側のクラスタとなることによる修正
1107 local adjust_nq_aux = {
1108 [id_glyph] = function() after_alchar(Nq) end, -- after_alchar(Nq)
1109 [id_hlist] = function() after_hlist(Nq) end,
1110 [id_pbox] = function() after_hlist(Nq) end,
1111 [id_disc] = function() after_hlist(Nq) end,
1112 [id_glue] = function()
1113 luatexbase.call_callback("luatexja.jfmglue.special_jaglue_after", Nq.nuc)
1115 [id_pbox_w]= function()
1116 local hh = luatexbase.call_callback("luatexja.jfmglue.whatsit_after", false, Nq, Np, head)
1117 -- hh: new head of false (nott processed)
1118 if hh then head = hh end
1122 adjust_nq = function()
1123 local x = adjust_nq_aux[Nq.id]
1129 -------------------- 開始・終了時の処理
1131 local node_prev = node.direct.getprev
1133 local function handle_list_tail(mode, last)
1136 -- the current list is to be line-breaked.
1137 -- Insert \jcharwidowpenalty
1138 if widow_Np.first then handle_penalty_jwp() end
1141 -- the current list is the contents of a hbox
1142 local npi, pm = Np.id, Np.met
1143 if npi == id_jglyph or (npi==id_pbox and pm) then
1144 local g = new_jfm_glue(pm.char_type, Np.class, fast_find_char_class('boxbdd', pm))
1146 set_attr(g, attr_icflag, BOXBDD)
1147 head = insert_after(head, Np.last, g)
1154 local function handle_list_head(par_indented)
1155 local npi, pm = Np.id, Np.met
1156 if npi == id_jglyph or (npi==id_pbox and pm) then
1157 if non_ihb_flag then
1158 local g = new_jfm_glue(pm.char_type, fast_find_char_class(par_indented, pm), Np.class)
1160 set_attr(g, attr_icflag, BOXBDD)
1161 if getid(g)==id_glue and #Bp==0 then
1162 local h = node_new(id_penalty, nil, Np.nuc)
1163 setpenalty(h, 10000); set_attr(h, attr_icflag, BOXBDD)
1165 head = insert_before(head, Np.first, g)
1172 -- return value: (the initial cursor lp), (last node)
1175 local id_local = node.id 'local_par'
1176 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
1177 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
1178 local KSK = luatexja.stack_table_index.KSK
1179 local XSK = luatexja.stack_table_index.XSK
1180 local dir_yoko = luatexja.dir_table.dir_yoko
1181 local dir_tate = luatexja.dir_table.dir_tate
1182 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
1183 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
1184 local table_pool = {
1185 {}, {}, {first=nil},
1186 { auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
1187 first=nil, id=nil, last=nil, met=nil, nuc=nil,
1188 post=nil, pre=nil, xspc=nil, gk=nil },
1189 { auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
1190 first=nil, id=nil, last=nil, met=nil, nuc=nil,
1191 post=nil, pre=nil, xspc=nil, gk=nil },
1193 init_var = function (mode,dir)
1194 -- 1073741823: max_dimen
1195 Bp, widow_Bp, widow_Np, Np, Nq
1196 = table_pool[1], table_pool[2], table_pool[3], table_pool[4], table_pool[5]
1197 for i=1,5 do for j,_ in pairs(table_pool[i]) do table_pool[i][j]=nil end end
1198 table_current_stack = ltjs.table_current_stack
1200 list_dir, tex_dir = (ltjs.list_dir or dir_yoko), (dir or 'TLT')
1201 local is_dir_tate = list_dir==dir_tate
1202 capsule_glyph = is_dir_tate and ltjw.capsule_glyph_tate or ltjw.capsule_glyph_yoko
1203 attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
1204 local TEMP = node_new(id_glue)
1205 -- TEMP is a dummy node, which will be freed at the end of the callback.
1206 -- Without this node, set_attr(kanji_skip, ...) somehow creates an "orphaned" attribute list.
1208 kanji_skip, kanjiskip_jfm_flag = skip_table_to_glue(KSK)
1209 set_attr(kanji_skip, attr_icflag, KANJI_SKIP)
1212 xkanji_skip, xkanjiskip_jfm_flag = skip_table_to_glue(XSK)
1213 set_attr(xkanji_skip, attr_icflag, XKANJI_SKIP)
1216 -- the current list is to be line-breaked:
1217 -- hbox from \parindent is skipped.
1218 local lp, par_indented, lpi, lps = head, 'boxbdd', getid(head), getsubtype(head)
1220 ((lpi==id_whatsit and lps~=sid_user)
1221 or ((lpi==id_hlist) and (lps==3))
1222 or (lpi==id_local)) do
1223 if (lpi==id_hlist) and (lps==3) then
1224 Np.char, par_indented = 'parbdd', 'parbdd'
1225 Np.width = getwidth(lp)
1227 lp=node_next(lp); lpi, lps = getid(lp), getsubtype(lp) end
1228 return lp, node_tail(head), par_indented, TEMP
1230 return head, nil, 'boxbdd', TEMP
1235 -------------------- 外部から呼ばれる関数
1237 local ensure_tex_attr = ltjb.ensure_tex_attr
1238 local tex_getattr = tex.getattribute
1240 function luatexja.jfmglue.main(ahead, mode, dir)
1241 if not ahead then return ahead end
1243 local lp, last, par_indented, TEMP = init_var(mode,dir)
1244 lp = calc_np(last, lp)
1246 handle_list_head(par_indented)
1247 lp = calc_np(last,lp);
1250 local pid, pm = Np.id, Np.met
1252 if pid == id_jglyph then
1253 handle_np_jachar(mode)
1255 if pid==id_hlist then handle_np_ja_hlist()
1256 else handle_np_jachar() end
1258 if Nq.id==id_hlist then handle_nq_ja_hlist()
1259 else handle_nq_jachar() end
1261 lp = calc_np(last,lp)
1263 handle_list_tail(mode, last)
1265 -- adjust attr_icflag for avoiding error
1266 if tex_getattr(attr_icflag)~=0 then ensure_tex_attr(attr_icflag, 0) end
1267 node_free(kanji_skip);
1268 node_free(xkanji_skip); node_free(TEMP)
1274 local IHB = luatexja.userid_table.IHB
1275 local BPAR = luatexja.userid_table.BPAR
1276 local BOXB = luatexja.userid_table.BOXB
1277 local node_prev = node.direct.getprev
1278 local node_write = node.direct.write
1280 -- \inhibitglue, \disinhibitglue
1281 local function ihb_node(v)
1282 local tn = node_new(id_whatsit, sid_user)
1283 setfield(tn, 'user_id', IHB)
1284 setfield(tn, 'type', 100)
1285 setfield(tn, 'value', v)
1288 function luatexja.jfmglue.create_inhibitglue_node()
1291 function luatexja.jfmglue.create_disinhibitglue_node()
1295 -- Node for indicating beginning of a paragraph
1296 -- (for ltjsclasses)
1297 function luatexja.jfmglue.create_beginpar_node()
1298 local tn = node_new(id_whatsit, sid_user)
1299 setfield(tn, 'user_id', BPAR)
1300 setfield(tn, 'type', 100)
1301 setfield(tn, 'value', 1)
1305 -- Node for indicating a head/end of a box
1306 function luatexja.jfmglue.create_boxbdd_node()
1307 local tn = node_new(id_whatsit, sid_user)
1308 setfield(tn, 'user_id', BOXB)
1309 setfield(tn, 'type', 100)
1310 setfield(tn, 'value', 1)
1314 local function whatsit_callback(Np, lp, Nq)
1315 if Np and Np.nuc then return Np
1316 elseif Np and getfield(lp, 'user_id') == BPAR then
1317 Np.first = lp; Np.nuc = lp; Np.last = lp
1319 elseif Np and getfield(lp, 'user_id') == BOXB then
1320 Np.first = lp; Np.nuc = lp; Np.last = lp
1323 Np.class = fast_find_char_class('boxbdd', Nq.met)
1325 Np.met = Nq.met; Np.pre = 0; Np.post = 0; Np.xspc = 0
1326 Np.auto_xspc, Np.auto_kspc = 0, 0
1334 local function whatsit_after_callback(s, Nq, Np, head)
1335 if not s and getfield(Nq.nuc, 'user_id') == BPAR then
1336 local x, y = node_prev(Nq.nuc), Nq.nuc
1337 Nq.first, Nq.nuc, Nq.last = x, x, x
1340 Nq.class = fast_find_char_class('parbdd', Np.met)
1342 Nq.met = Np.met; Nq.pre = 0; Nq.post = 0; Nq.xspc = 0
1343 Nq.auto_xspc, Nq.auto_kspc = 0, 0
1345 s = node_remove(head, y)
1347 elseif not s and getfield(Nq.nuc, 'user_id') == BOXB then
1348 local x, y = node_prev(Nq.nuc), Nq.nuc
1349 Nq.first, Nq.nuc, Nq.last = x, x, x
1352 Nq.class = fast_find_char_class('boxbdd', Np.met)
1354 Nq.met = Np.met; Nq.pre = 0; Nq.post = 0; Nq.xspc = 0
1355 Nq.auto_xspc, Nq.auto_kspc = 0, 0
1357 s = node_remove(head, y)
1363 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
1364 "luatexja.beginpar.np_info", 1)
1365 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
1366 "luatexja.beginpar.np_info_after", 1)
1370 local node_prev = node.direct.getprev
1371 local node_write = node.direct.write
1372 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
1373 local XKANJI_SKIP_JFM = luatexja.icflag_table.XKANJI_SKIP_JFM
1374 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
1375 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
1376 local XSK = luatexja.stack_table_index.XSK
1377 local KSK = luatexja.stack_table_index.KSK
1378 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
1379 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
1380 local getcount, abs, scan_keyword = tex.getcount, math.abs, token.scan_keyword
1381 local tex_nest = tex.nest
1382 local tex_getattr = tex.getattribute
1383 local get_current_jfont
1385 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
1386 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
1387 local dir_tate = luatexja.dir_table.dir_tate
1388 local get_dir_count = ltjd.get_dir_count
1389 function get_current_jfont()
1390 return tex_getattr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
1393 -- \insertxkanjiskip
1394 -- SPECIAL_JAGLUE のノード:
1395 -- * (X)KANJI_SKIP(_JFM): その場で値が決まっている
1396 -- * PROCESSED_BEGIN_FLAG + (X)KANJI_SKIP: 段落終了時に決める
1397 local function insert_k_skip_common(ind, name, ica, icb)
1398 if abs(tex_nest[tex_nest.ptr].mode) ~= ltjs.hmode then return end
1399 local g = node_new(id_glue); set_attr(g, attr_icflag, SPECIAL_JAGLUE)
1400 local is_late = scan_keyword("late")
1402 local st = ltjs.get_stack_skip(ind, getcount('ltj@@stack'))
1403 if st.width==1073741823 then
1404 local bk = ltjf_font_metric_table[get_current_jfont()][name]
1406 setglue(g, bk[1] or 0, bk[2] or 0, bk[3] or 0, 0, 0)
1408 set_attr(g, attr_yablshift, icb); node_write(g); return
1410 setglue(g, st.width, st.stretch, st.shrink, st.stretch_order, st.shrink_order)
1411 set_attr(g, attr_yablshift, ica)
1413 set_attr(g, attr_yablshift, PROCESSED_BEGIN_FLAG + ica)
1414 set_attr(g, attr_tablshift, get_current_jfont())
1418 function luatexja.jfmglue.insert_xk_skip()
1419 insert_k_skip_common(XSK, "xkanjiskip", XKANJI_SKIP, XKANJI_SKIP_JFM)
1421 function luatexja.jfmglue.insert_k_skip()
1422 insert_k_skip_common(KSK, "kanjiskip", KANJI_SKIP, KANJI_SKIP_JFM)
1425 local function special_jaglue(lx)
1426 local lxi = get_attr_icflag(lx)
1427 if lxi==SPECIAL_JAGLUE then
1428 non_ihb_flag = false; return false
1433 local function special_jaglue_after_inner(lx, lxi, lxi_jfm, kn, bk)
1434 local w, st, sh, sto, sho = getglue(kn)
1435 if w~=1073741823 then
1436 setglue(lx, w, st, sh, sto, sho); set_attr(lx, attr_icflag, lxi)
1438 local m = ltjf_font_metric_table[has_attr(lx, attr_tablshift)]
1439 setglue(lx, bk[1], bk[2], bk[3], 0, 0)
1440 set_attr(lx, attr_icflag, lxi_jfm)
1443 local function special_jaglue_after(lx)
1444 if get_attr_icflag(lx)==SPECIAL_JAGLUE then
1445 lxi=has_attr(lx, attr_yablshift)
1446 if lxi>=PROCESSED_BEGIN_FLAG then
1447 lxi = lxi%PROCESSED_BEGIN_FLAG
1448 if lxi == KANJI_SKIP then
1449 special_jaglue_after_inner(lx, lxi, KANJI_SKIP_JFM, kanji_skip,
1450 ltjf_font_metric_table[has_attr(lx, attr_tablshift)].kanjiskip or null_skip_table)
1451 else -- lxi == XKANJI_SKIP
1452 special_jaglue_after_inner(lx, lxi, XKANJI_SKIP_JFM, xkanji_skip,
1453 ltjf_font_metric_table[has_attr(lx, attr_tablshift)].xkanjiskip or null_skip_table)
1456 set_attr(lx, attr_icflag, lxi)
1459 if node_prev(lx) then
1460 local lxp = node_prev(lx)
1461 if lxp and getid(lxp)==id_penalty and get_attr_icflag(lxp)==KINSOKU then
1465 non_ihb_flag = false; return false
1469 luatexbase.create_callback("luatexja.jfmglue.special_jaglue", "list",
1471 luatexbase.create_callback("luatexja.jfmglue.special_jaglue_after", "list",
1472 special_jaglue_after)
1476 luatexja.jfmglue.after_hlist = after_hlist
1477 luatexja.jfmglue.check_box_high = check_box_high