2 -- luatexja/ltj-jfmglue.lua
4 luatexbase.provides_module({
5 name = 'luatexja.jfmglue',
7 description = 'Insertion process of JFM glues and kanjiskip',
9 module('luatexja.jfmglue', package.seeall)
10 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12 luatexja.load_module('stack'); local ltjs = luatexja.stack
13 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
14 luatexja.load_module('pretreat'); local ltjp = luatexja.pretreat
16 local has_attr = node.has_attribute
17 local set_attr = node.set_attribute
18 local insert_before = node.insert_before
19 local node_next = node.next
20 local round = tex.round
21 local ltjf_font_metric_table = ltjf.font_metric_table
22 local ltjf_find_char_class = ltjf.find_char_class
23 local node_new = node.new
24 local node_copy = node.copy
26 local ligature_head = 1
27 local ligature_tail = 2
29 local id_glyph = node.id('glyph')
30 local id_hlist = node.id('hlist')
31 local id_vlist = node.id('vlist')
32 local id_rule = node.id('rule')
33 local id_ins = node.id('ins')
34 local id_mark = node.id('mark')
35 local id_adjust = node.id('adjust')
36 local id_disc = node.id('disc')
37 local id_whatsit = node.id('whatsit')
38 local id_math = node.id('math')
39 local id_glue = node.id('glue')
40 local id_kern = node.id('kern')
41 local id_penalty = node.id('penalty')
43 local id_glue_spec = node.id('glue_spec')
44 local id_jglyph = 512 -- Japanese character
45 local id_box_like = 256 -- vbox, shifted hbox
46 local id_pbox = 257 -- already processed nodes (by \unhbox)
47 local id_pbox_w = 258 -- cluster which consists of a whatsit
48 local sid_user = node.subtype('user_defined')
50 local sid_start_link = node.subtype('pdf_start_link')
51 local sid_start_thread = node.subtype('pdf_start_thread')
52 local sid_end_link = node.subtype('pdf_end_link')
53 local sid_end_thread = node.subtype('pdf_end_thread')
55 local ITALIC = luatexja.icflag_table.ITALIC
56 local PACKED = luatexja.icflag_table.PACKED
57 local KINSOKU = luatexja.icflag_table.KINSOKU
58 local FROM_JFM = luatexja.icflag_table.FROM_JFM
59 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
60 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
61 local PROCESSED = luatexja.icflag_table.PROCESSED
62 local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
63 local BOXBDD = luatexja.icflag_table.BOXBDD
64 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
67 local table_current_stack
69 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
70 local attr_icflag = luatexbase.attributes['ltj@icflag']
72 local function get_attr_icflag(p)
73 return (has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
76 -------------------- Helper functions
78 local function copy_attr(new, old)
82 -- This function is called only for acquiring `special' characters.
83 local function fast_find_char_class(c,m)
84 return m.chars[c] or 0
88 local function slow_find_char_class(c, m, oc)
90 local cls = ltjf_find_char_class(oc, m)
91 if xc ~= oc and cls==0 then cls = ltjf_find_char_class(-xc, m) end
95 local zero_glue = node_new(id_glue)
96 spec_zero_glue = node_new(id_glue_spec) -- must be public, since mentioned from other sources
97 local spec_zero_glue = spec_zero_glue
98 spec_zero_glue.width = 0; spec_zero_glue.stretch_order = 0; spec_zero_glue.stretch = 0
99 spec_zero_glue.shrink_order = 0; spec_zero_glue.shrink = 0
100 zero_glue.spec = spec_zero_glue
102 local function skip_table_to_spec(n)
103 local g, st = node_new(id_glue_spec), ltjs.fast_get_skip_table(n)
104 g.width = st.width; g.stretch = st.stretch; g.shrink = st.shrink
105 g.stretch_order = st.stretch_order; g.shrink_order = st.shrink_order
111 local function add_penalty(p,e)
114 if e<=-10000 then pp = 0 end
115 elseif pp<=-10000 then
116 if e>=10000 then pp = 0 end
119 if pp>=10000 then p.penalty = 10000
120 elseif pp<=-10000 then p.penalty = -10000
121 else p.penalty = pp end
127 diffmet_rule = math.two_paverage
128 function math.two_add(a,b) return a+b end
129 function math.two_average(a,b) return (a+b)*0.5 end
130 function math.two_paverage(a,b) return (a+b)*0.5 end
131 function math.two_pleft(a,b) return a end
132 function math.two_pright(a,b) return b end
134 local head -- the head of current list
137 local widow_Bp, widow_Np -- \jcharwidowpenalty 挿入位置管理用
139 local ihb_flag -- JFM グルー挿入抑止用 flag
140 -- on: \inhibitglue 指定時,hlist の周囲
142 -------------------- hlist 内の文字の検索
144 local first_char, last_char, find_first_char
146 local function check_box(box_ptr, box_end)
147 local p = box_ptr; local found_visible_node = false
149 find_first_char = false; last_char = nil
152 while p and p~=box_end do
154 if pid==id_kern and p.subtype==2 then
155 p = node_next(node_next(node_next(p))); pid = p.id -- p must be glyph_node
157 if pid==id_glyph then
159 if find_first_char then
160 first_char = p; find_first_char = false
162 last_char = p; found_visible_node = true; p=node_next(p)
163 if (not p) or p==box_end then
164 return found_visible_node
167 pid = p.id -- p must be non-nil
170 if get_attr_icflag(p)==IC_PROCESSED then
172 elseif p.subtype==2 then
173 p = node_next(node_next(p));
174 -- Note that another node_next will be executed outside this if-statement.
176 found_visible_node = true
177 if find_first_char then
178 find_first_char = false
183 elseif pid==id_hlist then
184 if PACKED == get_attr_icflag(p) then
185 if find_first_char then
186 first_char = p.head; find_first_char = false
188 last_char = p.head; found_visible_node = true
191 if check_box(p.head, nil) then found_visible_node = true end
192 else if find_first_char then
193 find_first_char = false
199 elseif pid==id_math then
200 if find_first_char then
201 first_char = p; find_first_char = false
203 last_char = p; found_visible_node = true
204 elseif not (pid==id_ins or pid==id_mark
205 or pid==id_adjust or pid==id_whatsit
206 or pid==id_penalty) then
207 found_visible_node = true
208 if find_first_char then
209 find_first_char = false
216 return found_visible_node
219 function check_box_high(Nx, box_ptr, box_end)
220 first_char = nil; last_char = nil; find_first_char = true
221 if check_box(box_ptr, box_end) then
222 local first_char = first_char
224 if first_char.id==id_glyph then
225 if first_char.font == (has_attr(first_char, attr_curjfnt) or -1) then
226 set_np_xspc_jachar(Nx, first_char)
228 set_np_xspc_alchar(Nx, first_char.char,first_char, ligature_head)
231 set_np_xspc_alchar(Nx, -1,first_char)
238 -------------------- Np の計算と情報取得
240 luatexbase.create_callback("luatexja.jfmglue.whatsit_getinfo", "data",
241 function (Np, lp, Nq)
242 if Np.nuc then return Np
244 return Np -- your code
247 luatexbase.create_callback("luatexja.jfmglue.whatsit_after", "data",
248 function (stat, Nq, Np) return false end)
253 local function set_attr_icflag_processed(p)
254 if get_attr_icflag(p)<= ITALIC then
255 set_attr(p, attr_icflag, PROCESSED)
259 local function check_next_ickern(lp)
260 if lp.id == id_kern and ITALIC == get_attr_icflag(lp) then
261 set_attr(lp, attr_icflag, IC_PROCESSED)
262 Np.last = lp; return node_next(lp)
264 Np.last = Np.nuc; return lp
268 local function calc_np_pbox(lp, last)
269 Np.first = Np.first or lp; Np.id = id_pbox
270 local lpa = KINSOKU -- dummy=
271 set_attr(lp, attr_icflag, get_attr_icflag(lp));
272 while lp~=last and lpa>=PACKED and lpa<BOXBDD do
274 lp = node_next(lp); lpa = has_attr(lp, attr_icflag) or 0
275 -- get_attr_icflag() ではいけない!
277 return check_next_ickern(lp)
281 local calc_np_auxtable = {
282 [id_glyph] = function (lp)
283 Np.first, Np.nuc = (Np.first or lp), lp;
284 Np.id = (lp.font == (has_attr(lp, attr_curjfnt) or -1)) and id_jglyph or id_glyph
285 --set_attr_icflag_processed(lp) treated in ltj-setwidth.lua
286 return true, check_next_ickern(node_next(lp));
288 [id_hlist] = function(lp)
289 Np.first = Np.first or lp; Np.last = lp; Np.nuc = lp;
290 set_attr(lp, attr_icflag, PROCESSED)
291 --set_attr_icflag_processed(lp)
292 Np.id = (lp.shift~=0) and id_box_like or id_hlist
293 return true, node_next(lp)
295 box_like = function(lp)
296 Np.first = Np.first or lp; Np.nuc = lp; Np.last = lp;
297 Np.id = id_box_like; set_attr(lp, attr_icflag, PROCESSED)
298 -- set_attr_icflag_processed(lp);
299 return true, node_next(lp);
302 set_attr(lp, attr_icflag, PROCESSED)
303 -- set_attr_icflag_processed(lp);
304 return false, node_next(lp)
306 [id_whatsit] = function(lp)
307 if lp.subtype==sid_user then
308 if lp.user_id==luatexja.userid_table.IHB then
309 local lq = node_next(lp);
310 head = node.remove(head, lp); node.free(lp); ihb_flag = true
313 set_attr(lp, attr_icflag, PROCESSED)
314 -- set_attr_icflag_processed(lp)
315 luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo",
318 Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc;
319 return true, node_next(lp)
321 return false, node_next(lp)
325 -- we do special treatment for these whatsit nodes.
326 if lp.subtype == sid_start_link or lp.subtype == sid_start_thread then
328 elseif lp.subtype == sid_end_link or lp.subtype == sid_end_thread then
329 Np.first, Nq.last = nil, lp;
331 set_attr(lp, attr_icflag, PROCESSED)
332 -- set_attr_icflag_processed(lp);
333 return false, node_next(lp)
336 [id_math] = function(lp)
337 Np.first, Np.nuc = (Np.first or lp), lp;
338 set_attr(lp, attr_icflag, PROCESSED) -- set_attr_icflag_processed(lp);
339 lp = node.end_of_math(lp)
340 set_attr(lp, attr_icflag, PROCESSED) -- set_attr_icflag_processed(lp);
341 Np.last, Np.id = lp, id_math;
342 return true, node_next(lp);
344 discglue = function(lp)
345 Np.first, Np.nuc, Np.last = (Np.first or lp), lp, lp;
346 Np.id = lp.id; set_attr(lp, attr_icflag, PROCESSED) -- set_attr_icflag_processed(lp);
347 return true, node_next(lp)
349 [id_kern] = function(lp)
350 Np.first = Np.first or lp
351 if lp.subtype==2 then
352 set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
353 set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
354 set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
355 set_attr(lp, attr_icflag, PROCESSED); Np.nuc = lp
356 Np.id = (lp.font == (has_attr(lp, attr_curjfnt) or -1)) and id_jglyph or id_glyph
357 return true, check_next_ickern(node_next(lp));
359 Np.id = id_kern; set_attr(lp, attr_icflag, PROCESSED); -- set_attr_icflag_processed(lp);
360 Np.last = lp; return true, node_next(lp)
363 [id_penalty] = function(lp)
364 Bp[#Bp+1] = lp; set_attr(lp, attr_icflag, PROCESSED); -- set_attr_icflag_processed(lp);
365 return false, node_next(lp)
368 calc_np_auxtable[id_vlist] = calc_np_auxtable.box_like
369 calc_np_auxtable[id_rule] = calc_np_auxtable.box_like
370 calc_np_auxtable[13] = calc_np_auxtable.box_like
371 calc_np_auxtable[id_ins] = calc_np_auxtable.skip
372 calc_np_auxtable[id_mark] = calc_np_auxtable.skip
373 calc_np_auxtable[id_adjust] = calc_np_auxtable.skip
374 calc_np_auxtable[id_disc] = calc_np_auxtable.discglue
375 calc_np_auxtable[id_glue] = calc_np_auxtable.discglue
378 function calc_np(lp, last)
380 -- We assume lp = node_next(Np.last)
381 Np, Nq, ihb_flag = Nq, Np, nil
382 -- We clear `predefined' entries of Np before pairs() loop,
383 -- because using only pairs() loop is slower.
384 Np.post, Np.pre, Np.xspc = nil, nil, nil
385 Np.first, Np.id, Np.last, Np.met = nil, nil, nil
386 Np.auto_kspc, Np.auto_xspc, Np.char, Np.class, Np.nuc = nil, nil, nil, nil, nil
387 for k in pairs(Np) do Np[k] = nil end
389 for k = 1,#Bp do Bp[k] = nil end
391 local lpa = has_attr(lp, attr_icflag) or 0
394 if lpa%PROCESSED_BEGIN_FLAG == BOXBDD then
395 local lq = node_next(lp)
396 head = node.remove(head, lp); node.free(lp); lp = lq
397 else return calc_np_pbox(lp, last)
400 k, lp = calc_np_auxtable[lp.id](lp)
401 if k then return lp end
408 local calc_np = calc_np
410 -- extract informations from Np
411 -- We think that "Np is a Japanese character" if Np.met~=nil,
412 -- "Np is an alphabetic character" if Np.pre~=nil,
413 -- "Np is not a character" otherwise.
415 local PRE = luatexja.stack_table_index.PRE
416 local POST = luatexja.stack_table_index.POST
417 local KCAT = luatexja.stack_table_index.KCAT
418 local XSP = luatexja.stack_table_index.XSP
421 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
422 local attr_orig_char = luatexbase.attributes['ltj@origchar']
423 local attr_autospc = luatexbase.attributes['ltj@autospc']
424 local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
425 function set_np_xspc_jachar(Nx, x)
426 local m = ltjf_font_metric_table[x.font]
427 local cls, c = slow_find_char_class(has_attr(x, attr_orig_char), m, x.char)
428 Nx.class = cls; set_attr(x, attr_jchar_class, cls)
429 Nx.met, Nx.char = m, c
430 Nx.pre = table_current_stack[PRE + c] or 0
431 Nx.post = table_current_stack[POST + c] or 0
432 Nx.xspc = table_current_stack[XSP + c] or 3
433 Nx.kcat = table_current_stack[KCAT + c] or 0
434 Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
436 local set_np_xspc_jachar = set_np_xspc_jachar
439 local floor = math.floor
440 function set_np_xspc_alchar(Nx, c,x, lig)
442 local xc, xs = x.components, x.subtype
444 while xc and xs and xs%4>=2 do
445 x = xc; xc, xs = x.components, x.subtype
449 while xc and xs and xs%4>=2 do
450 x = node.tail(xc); xc, xs = x.components, x.subtype
454 Nx.pre = table_current_stack[PRE + c] or 0
455 Nx.post = table_current_stack[POST + c] or 0
456 Nx.xspc = table_current_stack[XSP + c] or 3
459 Nx.pre, Nx.post, Nx.char = 0, 0, -1
460 Nx.xspc = table_current_stack[XSP - 1] or 3
463 Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
465 local set_np_xspc_alchar = set_np_xspc_alchar
468 function extract_np()
469 local x, i = Np.nuc, Np.id;
470 if i == id_jglyph then return set_np_xspc_jachar(Np, x)
471 elseif i == id_glyph then return set_np_xspc_alchar(Np, x.char, x, ligature_head)
472 elseif i == id_hlist then Np.last_char = check_box_high(Np, x.head, nil)
473 elseif i == id_pbox then Np.last_char = check_box_high(Np, Np.first, node_next(Np.last))
474 elseif i == id_disc then Np.last_char = check_box_high(Np, x.replace, nil)
475 elseif i == id_math then return set_np_xspc_alchar(Np, -1, x)
479 -- change the information for the next loop
480 -- (will be done if Nx is an alphabetic character or a hlist)
481 function after_hlist(Nx)
482 local s = Nx.last_char
484 if s.id==id_glyph then
485 if s.font == (has_attr(s, attr_curjfnt) or -1) then
486 set_np_xspc_jachar(Nx, s)
488 set_np_xspc_alchar(Nx, s.char, s, ligature_tail)
491 set_np_xspc_alchar(Nx, -1, s)
494 Nx.pre, Nx.met = nil, nil
498 function after_alchar(Nx)
500 return set_np_xspc_alchar(Nx, x.char,x, ligature_tail)
504 local after_hlist, after_alchar, extract_np = after_hlist, after_alchar, extract_np
506 -------------------- 最下層の処理
508 -- change penalties (or create a new penalty, if needed)
509 local function handle_penalty_normal(post, pre, g)
510 local a = (pre or 0) + (post or 0)
512 if (a~=0 and not(g and g.id==id_kern)) then
513 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
514 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
516 head = insert_before(head, Np.first, p)
518 set_attr(p, attr_icflag, KINSOKU)
520 else for _, v in pairs(Bp) do add_penalty(v,a) end
524 local function handle_penalty_always(post, pre, g)
525 local a = (pre or 0) + (post or 0)
527 if not (g and g.id==id_glue) then
528 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
529 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
531 head = insert_before(head, Np.first, p)
533 set_attr(p, attr_icflag, KINSOKU)
535 else for _, v in pairs(Bp) do add_penalty(v,a) end
539 local function handle_penalty_suppress(post, pre, g)
540 local a = (pre or 0) + (post or 0)
542 if g and g.id==id_glue then
543 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
544 p.penalty = 10000; head = insert_before(head, Np.first, p)
546 set_attr(p, attr_icflag, KINSOKU)
548 else for _, v in pairs(Bp) do add_penalty(v,a) end
552 -- 和文文字間の JFM glue を node 化
553 local function new_jfm_glue(m, bc, ac)
554 -- bc, ac: char classes
555 local z = m.char_type[bc]
556 local g, d = z.glue[ac], 0
558 g,d = node_copy(g[1]), g[2];
559 g.spec = node_copy(g.spec); -- node_copy は spec をコピーする
563 g = node_new(id_kern); --copy_attr(g, Nn.nuc)
564 g.subtype = 1; g.kern, d = k[1], k[2]
565 set_attr(g, attr_icflag, FROM_JFM);
571 -- Nq.last (kern w) .... (glue/kern g) Np.first
572 local function real_insert(g)
574 head = insert_before(head, Np.first, g)
580 -------------------- 和文文字間空白量の決定
585 local function get_kanjiskip_normal()
586 if Np.auto_kspc or Nq.auto_kspc then
587 return node_copy(kanji_skip)
589 local g = node_copy(zero_glue)
590 set_attr(g, attr_icflag, KANJI_SKIP)
594 local function get_kanjiskip_jfm()
596 if Np.auto_kspc or Nq.auto_kspc then
597 g = node_new(id_glue); --copy_attr(g, Nq.nuc)
598 local gx = node_new(id_glue_spec);
599 gx.stretch_order, gx.shrink_order = 0, 0
600 local pm, qm = Np.met, Nq.met
601 local bk = qm.kanjiskip or {0, 0, 0}
602 if (pm.char_type==qm.char_type) and (qm.var==pm.var) then
603 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
605 local ak = pm.kanjiskip or {0, 0, 0}
606 gx.width = round(diffmet_rule(bk[1], ak[1]))
607 gx.stretch = round(diffmet_rule(bk[2], ak[2]))
608 gx.shrink = -round(diffmet_rule(-bk[3], -ak[3]))
612 g = node_copy(zero_glue)
614 set_attr(g, attr_icflag, KANJI_SKIP)
618 local function calc_ja_ja_aux(gb,ga, db, da)
619 local rbb, rab = (1-db)/2, (1-da)/2 -- 「前の文字」由来のグルーの割合
620 local rba, raa = (1+db)/2, (1+da)/2 -- 「前の文字」由来のグルーの割合
621 if diffmet_rule ~= math.two_pleft and diffmet_rule ~= math.two_pright
622 and diffmet_rule ~= math.two_paverage then
623 rbb, rab, rba, raa = 1,0,0,1
626 if ga then gb = node_new(id_kern); gb.kern = 0 else return nil end
628 ga = node_new(id_kern); ga.kern = 0
631 local k = node.type(gb.id) .. node.type(ga.id)
632 if k == 'glueglue' then
634 gb.spec.width = round(diffmet_rule(
635 rbb*gb.spec.width + rba*ga.spec.width,
636 rab*gb.spec.width + raa*ga.spec.width ))
637 gb.spec.stretch = round(diffmet_rule(
638 rbb*gb.spec.stretch + rba*ga.spec.stretch,
639 rab*gb.spec.stretch + raa*ga.spec.stretch ))
640 gb.spec.shrink = -round(diffmet_rule(
641 -rbb*gb.spec.shrink - rba*ga.spec.shrink,
642 -rab*gb.spec.shrink - raa*ga.spec.shrink ))
645 elseif k == 'kernkern' then
647 gb.kern = round(diffmet_rule(
648 rbb*gb.kern + rba*ga.kern,
649 rab*gb.kern + raa*ga.kern ))
652 elseif k == 'kernglue' then
653 -- gb: kern, ga: glue
654 ga.spec.width = round(diffmet_rule(
655 rbb*gb.kern + rba*ga.spec.width,
656 rab*gb.kern + raa*ga.spec.width ))
657 ga.spec.stretch = round(diffmet_rule(
658 rba*ga.spec.stretch, raa*ga.spec.stretch ))
659 ga.spec.shrink = -round(diffmet_rule(
660 -rba*ga.spec.shrink,-raa*ga.spec.shrink ))
664 -- gb: glue, ga: kern
665 gb.spec.width = round(diffmet_rule(
666 rba*ga.kern + rbb*gb.spec.width,
667 raa*ga.kern + rab*gb.spec.width ))
668 gb.spec.stretch = round(diffmet_rule(
669 rbb*gb.spec.stretch, rab*gb.spec.stretch ))
670 gb.spec.shrink = -round(diffmet_rule(
671 -rbb*gb.spec.shrink,-rab*gb.spec.shrink ))
677 local function calc_ja_ja_glue()
678 if ihb_flag then return nil
680 local qm, pm = Nq.met, Np.met
681 if (qm.char_type==pm.char_type) and (qm.var==pm.var) then
682 return new_jfm_glue(qm, Nq.class, Np.class)
684 local npn, nqn = Np.nuc, Nq.nuc
685 local gb, db = new_jfm_glue(qm, Nq.class,
686 slow_find_char_class(has_attr(npn, attr_orig_char), qm, npn.char))
687 local ga, da = new_jfm_glue(pm,
688 slow_find_char_class(has_attr(nqn, attr_orig_char), pm, nqn.char),
690 return calc_ja_ja_aux(gb, ga, db, da);
695 -------------------- 和欧文間空白量の決定
699 local function get_xkanjiskip_normal(Nn)
700 if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
701 return node_copy(xkanji_skip)
703 local g = node_copy(zero_glue)
704 set_attr(g, attr_icflag, XKANJI_SKIP)
708 local function get_xkanjiskip_jfm(Nn)
710 if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
711 g = node_new(id_glue); --copy_attr(g, Nn.nuc)
712 local gx = node_new(id_glue_spec);
713 gx.stretch_order, gx.shrink_order = 0, 0
714 local bk = Nn.met.xkanjiskip or {0, 0, 0}
715 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
718 g = node_copy(zero_glue)
720 set_attr(g, attr_icflag, XKANJI_SKIP)
726 -------------------- 隣接した「塊」間の処理
728 local function get_OA_skip()
731 return new_jfm_glue(pm,
732 fast_find_char_class(((Nq.id == id_math and -1) or (type(Nq.char)=='string' and Nq.char or 'jcharbdd')), pm), Np.class)
736 local function get_OB_skip()
739 return new_jfm_glue(qm, Nq.class,
740 fast_find_char_class(((Np.id == id_math and -1) or'jcharbdd'), qm))
745 -- (anything) .. jachar
746 local function handle_np_jachar(mode)
748 if qid==id_jglyph or ((qid==id_pbox or qid==id_pbox_w) and Nq.met) then
749 local g = calc_ja_ja_glue() or get_kanjiskip() -- M->K
750 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(g)
751 elseif Nq.met then -- qid==id_hlist
752 local g = get_OA_skip() or get_kanjiskip() -- O_A->K
753 handle_penalty_normal(0, Np.pre, g); real_insert(g)
755 local g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
756 handle_penalty_normal((qid==id_hlist and 0 or Nq.post), Np.pre, g); real_insert(g)
758 local g = get_OA_skip() -- O_A
759 if qid==id_glue then handle_penalty_normal(0, Np.pre, g)
760 elseif qid==id_kern then handle_penalty_suppress(0, Np.pre, g)
761 else handle_penalty_always(0, Np.pre, g)
765 if mode and Np.kcat%2~=1 then
766 widow_Np.first, widow_Bp, Bp = Np.first, Bp, widow_Bp
771 -- jachar .. (anything)
772 local function handle_nq_jachar()
774 local g = get_OB_skip() or get_xkanjiskip(Nq) -- O_B->X
775 handle_penalty_normal(Nq.post, (Np.id==id_hlist and 0 or Np.pre), g); real_insert(g)
777 local g = get_OB_skip() -- O_B
778 if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
779 elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
780 else handle_penalty_always(Nq.post, 0, g)
786 -- (anything) .. (和文文字で始まる hlist)
787 local function handle_np_ja_hlist()
789 if qid==id_jglyph or ((qid==id_pbox or Nq.id == id_pbox_w) and Nq.met) then
790 local g = get_OB_skip() or get_kanjiskip() -- O_B->K
791 handle_penalty_normal(Nq.post, 0, g); real_insert(g)
792 elseif Nq.met then -- Nq.id==id_hlist
793 local g = get_kanjiskip() -- K
794 handle_penalty_suppress(0, 0, g); real_insert(g)
796 local g = get_xkanjiskip(Np) -- X
797 handle_penalty_suppress(0, 0, g); real_insert(g)
801 -- (和文文字で終わる hlist) .. (anything)
802 local function handle_nq_ja_hlist()
804 local g = get_xkanjiskip(Nq) -- X
805 handle_penalty_suppress(0, 0, g); real_insert(g)
810 -- Nq が前側のクラスタとなることによる修正
812 local adjust_nq_aux = {
813 [id_glyph] = function()
815 return set_np_xspc_alchar(Nq, x.char,x, 2)
816 end, -- after_alchar(Nq)
817 [id_hlist] = function() after_hlist(Nq) end,
818 [id_pbox] = function() after_hlist(Nq) end,
819 [id_disc] = function() after_hlist(Nq) end,
820 [id_pbox_w] = function()
821 luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
827 local x = adjust_nq_aux[Nq.id]
833 -------------------- 開始・終了時の処理
836 local JWP = luatexja.stack_table_index.JWP
837 local function handle_list_tail(mode)
840 -- the current list is to be line-breaked.
841 -- Insert \jcharwidowpenalty
842 Bp = widow_Bp; Np = widow_Np
844 handle_penalty_normal(0,
845 table_current_stack[JWP] or 0)
848 -- the current list is the contents of a hbox
849 local npi, pm = Np.id, Np.met
850 if npi == id_jglyph or (npi==id_pbox and pm) then
851 local g = new_jfm_glue(pm, Np.class, fast_find_char_class('boxbdd', pm))
853 set_attr(g, attr_icflag, BOXBDD)
854 head = node.insert_after(head, Np.last, g)
861 local function handle_list_head(par_indented)
862 local npi, pm = Np.id, Np.met
863 if npi == id_jglyph or (npi==id_pbox and pm) then
865 local g = new_jfm_glue(pm, fast_find_char_class(par_indented, pm), Np.class)
867 set_attr(g, attr_icflag, BOXBDD)
868 if g.id==id_glue and #Bp==0 then
869 local h = node_new(id_penalty); --copy_attr(h, Np.nuc)
870 h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
872 head = insert_before(head, Np.first, g)
879 -- return value: (the initial cursor lp), (last node)
880 local function init_var(mode)
881 -- 1073741823: max_dimen
882 Bp, widow_Bp, widow_Np = {}, {}, {first = nil}
883 table_current_stack = ltjs.table_current_stack
885 kanji_skip = node_new(id_glue)
886 kanji_skip.spec = skip_table_to_spec('kanjiskip')
887 set_attr(kanji_skip, attr_icflag, KANJI_SKIP)
888 get_kanjiskip = (kanji_skip.spec.width == 1073741823)
889 and get_kanjiskip_jfm or get_kanjiskip_normal
891 xkanji_skip = node_new(id_glue)
892 xkanji_skip.spec = skip_table_to_spec('xkanjiskip')
893 set_attr(xkanji_skip, attr_icflag, XKANJI_SKIP)
894 get_xkanjiskip = (xkanji_skip.spec.width == 1073741823)
895 and get_xkanjiskip_jfm or get_xkanjiskip_normal
898 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
899 first=nil, id=nil, last=nil, met=nil, nuc=nil,
900 post=nil, pre=nil, xspc=nil,
903 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
904 first=nil, id=nil, last=nil, met=nil, nuc=nil,
905 post=nil, pre=nil, xspc=nil,
908 -- the current list is to be line-breaked:
909 -- hbox from \parindent is skipped.
910 local lp, par_indented, lpi, lps = head, 'boxbdd', head.id, head.subtype
911 while lp and ((lpi==id_whatsit and lps~=sid_user)
912 or ((lpi==id_hlist) and (lps==3))) do
913 if (lpi==id_hlist) and (lps==3) then par_indented = 'parbdd' end
914 lp=node_next(lp); lpi, lps = lp.id, lp.subtype end
915 return lp, node.tail(head), par_indented
917 -- the current list is the contents of a hbox:
919 local g = node_new(id_kern)
920 node.insert_after(head, node.tail(head), g); last = g
921 return head, g, 'boxbdd'
925 local function cleanup(mode, last)
926 -- adjust attr_icflag for avoiding error
927 tex.setattribute('global', attr_icflag, 0)
928 node.free(kanji_skip); node.free(xkanji_skip)
930 local h = node_next(head)
931 if h.id == id_penalty and h.penalty == 10000 then
933 if h.id == id_glue and h.subtype == 15 and not h.next then
939 head = node.remove(head, last); node.free(last);-- remove the sentinel
940 set_attr(head, attr_icflag,
941 get_attr_icflag(head) + PROCESSED_BEGIN_FLAG);
945 -------------------- 外部から呼ばれる関数
948 function main(ahead, mode)
949 if not ahead then return ahead end
951 local lp, last, par_indented = init_var(mode);
952 lp = calc_np(lp, last)
954 extract_np(); handle_list_head(par_indented)
956 return cleanup(mode, last)
958 lp = calc_np(lp, last)
962 local pid, pm = Np.id, Np.met
964 if pid == id_jglyph then
965 handle_np_jachar(mode)
967 if pid==id_hlist then handle_np_ja_hlist()
968 else handle_np_jachar() end
970 if Nq.id==id_hlist then handle_nq_ja_hlist()
971 else handle_nq_jachar() end
973 lp = calc_np(lp, last)
975 handle_list_tail(mode)
976 return cleanup(mode, last)
980 local IHB = luatexja.userid_table.IHB
981 local BPAR = luatexja.userid_table.BPAR
984 function create_inhibitglue_node()
985 local tn = node_new(id_whatsit, sid_user)
986 tn.user_id=IHB; tn.type=100; tn.value=1
990 -- Node for indicating beginning of a paragraph
992 function create_beginpar_node()
993 local tn = node_new(id_whatsit, sid_user)
994 tn.user_id=BPAR; tn.type=100; tn.value=1
998 local function whatsit_callback(Np, lp, Nq)
999 if Np and Np.nuc then return Np
1000 elseif Np and lp.user_id == BPAR then
1001 Np.first = lp; Np.nuc = lp; Np.last = lp
1004 Np.pre = 0; Np.post = 0
1006 Np.auto_xspc = false
1011 local function whatsit_after_callback(s, Nq, Np)
1012 if not s and Nq.nuc.user_id == BPAR then
1013 local x, y = node.prev(Nq.nuc), Nq.nuc
1014 Nq.first, Nq.nuc, Nq.last = x, x, x
1015 head = node.remove(head, y)
1020 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
1021 "luatexja.beginpar.np_info", 1)
1022 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
1023 "luatexja.beginpar.np_info_after", 1)