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);
340 while lp.id~=id_math do
341 set_attr(lp, attr_icflag, PROCESSED) -- set_attr_icflag_processed(lp);
344 set_attr(lp, attr_icflag, PROCESSED) -- set_attr_icflag_processed(lp);
345 Np.last, Np.id = lp, id_math;
346 return true, node_next(lp);
348 discglue = function(lp)
349 Np.first, Np.nuc, Np.last = (Np.first or lp), lp, lp;
350 Np.id = lp.id; set_attr(lp, attr_icflag, PROCESSED) -- set_attr_icflag_processed(lp);
351 return true, node_next(lp)
353 [id_kern] = function(lp)
354 Np.first = Np.first or lp
355 if lp.subtype==2 then
356 set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
357 set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
358 set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
359 set_attr(lp, attr_icflag, PROCESSED); Np.nuc = lp
360 Np.id = (lp.font == (has_attr(lp, attr_curjfnt) or -1)) and id_jglyph or id_glyph
361 return true, check_next_ickern(node_next(lp));
363 Np.id = id_kern; set_attr(lp, attr_icflag, PROCESSED); -- set_attr_icflag_processed(lp);
364 Np.last = lp; return true, node_next(lp)
367 [id_penalty] = function(lp)
368 Bp[#Bp+1] = lp; set_attr(lp, attr_icflag, PROCESSED); -- set_attr_icflag_processed(lp);
369 return false, node_next(lp)
372 calc_np_auxtable[id_vlist] = calc_np_auxtable.box_like
373 calc_np_auxtable[id_rule] = calc_np_auxtable.box_like
374 calc_np_auxtable[13] = calc_np_auxtable.box_like
375 calc_np_auxtable[id_ins] = calc_np_auxtable.skip
376 calc_np_auxtable[id_mark] = calc_np_auxtable.skip
377 calc_np_auxtable[id_adjust] = calc_np_auxtable.skip
378 calc_np_auxtable[id_disc] = calc_np_auxtable.discglue
379 calc_np_auxtable[id_glue] = calc_np_auxtable.discglue
382 function calc_np(lp, last)
384 -- We assume lp = node_next(Np.last)
385 Np, Nq, ihb_flag = Nq, Np, nil
386 -- We clear `predefined' entries of Np before pairs() loop,
387 -- because using only pairs() loop is slower.
388 Np.post, Np.pre, Np.xspc = nil, nil, nil
389 Np.first, Np.id, Np.last, Np.met = nil, nil, nil
390 Np.auto_kspc, Np.auto_xspc, Np.char, Np.class, Np.nuc = nil, nil, nil, nil, nil
391 for k in pairs(Np) do Np[k] = nil end
393 for k = 1,#Bp do Bp[k] = nil end
395 local lpa = has_attr(lp, attr_icflag) or 0
398 if lpa%PROCESSED_BEGIN_FLAG == BOXBDD then
399 local lq = node_next(lp)
400 head = node.remove(head, lp); node.free(lp); lp = lq
401 else return calc_np_pbox(lp, last)
404 k, lp = calc_np_auxtable[lp.id](lp)
405 if k then return lp end
412 local calc_np = calc_np
414 -- extract informations from Np
415 -- We think that "Np is a Japanese character" if Np.met~=nil,
416 -- "Np is an alphabetic character" if Np.pre~=nil,
417 -- "Np is not a character" otherwise.
419 local PRE = luatexja.stack_table_index.PRE
420 local POST = luatexja.stack_table_index.POST
421 local KCAT = luatexja.stack_table_index.KCAT
422 local XSP = luatexja.stack_table_index.XSP
425 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
426 local attr_orig_char = luatexbase.attributes['ltj@origchar']
427 local attr_autospc = luatexbase.attributes['ltj@autospc']
428 local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
429 function set_np_xspc_jachar(Nx, x)
430 local m = ltjf_font_metric_table[x.font]
431 local cls, c = slow_find_char_class(has_attr(x, attr_orig_char), m, x.char)
432 Nx.class = cls; set_attr(x, attr_jchar_class, cls)
433 Nx.met, Nx.char = m, c
434 Nx.pre = table_current_stack[PRE + c] or 0
435 Nx.post = table_current_stack[POST + c] or 0
436 Nx.xspc = table_current_stack[XSP + c] or 3
437 Nx.kcat = table_current_stack[KCAT + c] or 0
438 Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
440 local set_np_xspc_jachar = set_np_xspc_jachar
443 local floor = math.floor
444 function set_np_xspc_alchar(Nx, c,x, lig)
446 local xc, xs = x.components, x.subtype
448 while xc and xs and xs%4>=2 do
449 x = xc; xc, xs = x.components, x.subtype
453 while xc and xs and xs%4>=2 do
454 x = node.tail(xc); xc, xs = x.components, x.subtype
458 Nx.pre = table_current_stack[PRE + c] or 0
459 Nx.post = table_current_stack[POST + c] or 0
460 Nx.xspc = table_current_stack[XSP + c] or 3
463 Nx.pre, Nx.post, Nx.char = 0, 0, -1
464 Nx.xspc = table_current_stack[XSP - 1] or 3
467 Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
469 local set_np_xspc_alchar = set_np_xspc_alchar
472 function extract_np()
473 local x, i = Np.nuc, Np.id;
474 if i == id_jglyph then return set_np_xspc_jachar(Np, x)
475 elseif i == id_glyph then return set_np_xspc_alchar(Np, x.char, x, ligature_head)
476 elseif i == id_hlist then Np.last_char = check_box_high(Np, x.head, nil)
477 elseif i == id_pbox then Np.last_char = check_box_high(Np, Np.first, node_next(Np.last))
478 elseif i == id_disc then Np.last_char = check_box_high(Np, x.replace, nil)
479 elseif i == id_math then return set_np_xspc_alchar(Np, -1, x)
483 -- change the information for the next loop
484 -- (will be done if Nx is an alphabetic character or a hlist)
485 function after_hlist(Nx)
486 local s = Nx.last_char
488 if s.id==id_glyph then
489 if s.font == (has_attr(s, attr_curjfnt) or -1) then
490 set_np_xspc_jachar(Nx, s)
492 set_np_xspc_alchar(Nx, s.char, s, ligature_tail)
495 set_np_xspc_alchar(Nx, -1, s)
498 Nx.pre, Nx.met = nil, nil
502 function after_alchar(Nx)
504 return set_np_xspc_alchar(Nx, x.char,x, ligature_tail)
508 local after_hlist, after_alchar, extract_np = after_hlist, after_alchar, extract_np
510 -------------------- 最下層の処理
512 -- change penalties (or create a new penalty, if needed)
513 local function handle_penalty_normal(post, pre, g)
514 local a = (pre or 0) + (post or 0)
516 if (a~=0 and not(g and g.id==id_kern)) then
517 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
518 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
520 head = insert_before(head, Np.first, p)
522 set_attr(p, attr_icflag, KINSOKU)
524 else for _, v in pairs(Bp) do add_penalty(v,a) end
528 local function handle_penalty_always(post, pre, g)
529 local a = (pre or 0) + (post or 0)
531 if not (g and g.id==id_glue) then
532 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
533 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
535 head = insert_before(head, Np.first, p)
537 set_attr(p, attr_icflag, KINSOKU)
539 else for _, v in pairs(Bp) do add_penalty(v,a) end
543 local function handle_penalty_suppress(post, pre, g)
544 local a = (pre or 0) + (post or 0)
546 if g and g.id==id_glue then
547 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
548 p.penalty = 10000; head = insert_before(head, Np.first, p)
550 set_attr(p, attr_icflag, KINSOKU)
552 else for _, v in pairs(Bp) do add_penalty(v,a) end
556 -- 和文文字間の JFM glue を node 化
557 local function new_jfm_glue(m, bc, ac)
558 -- bc, ac: char classes
559 local z = m.char_type[bc]
560 local g, d = z.glue[ac], 0
562 g,d = node_copy(g[1]), g[2];
563 g.spec = node_copy(g.spec); -- node_copy は spec をコピーする
567 g = node_new(id_kern); --copy_attr(g, Nn.nuc)
568 g.subtype = 1; g.kern, d = k[1], k[2]
569 set_attr(g, attr_icflag, FROM_JFM);
575 -- Nq.last (kern w) .... (glue/kern g) Np.first
576 local function real_insert(g)
578 head = insert_before(head, Np.first, g)
584 -------------------- 和文文字間空白量の決定
589 local function get_kanjiskip_normal()
590 if Np.auto_kspc or Nq.auto_kspc then
591 return node_copy(kanji_skip)
593 local g = node_copy(zero_glue)
594 set_attr(g, attr_icflag, KANJI_SKIP)
598 local function get_kanjiskip_jfm()
600 if Np.auto_kspc or Nq.auto_kspc then
601 g = node_new(id_glue); --copy_attr(g, Nq.nuc)
602 local gx = node_new(id_glue_spec);
603 gx.stretch_order, gx.shrink_order = 0, 0
604 local pm, qm = Np.met, Nq.met
605 local bk = qm.kanjiskip or {0, 0, 0}
606 if (pm.char_type==qm.char_type) and (qm.var==pm.var) then
607 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
609 local ak = pm.kanjiskip or {0, 0, 0}
610 gx.width = round(diffmet_rule(bk[1], ak[1]))
611 gx.stretch = round(diffmet_rule(bk[2], ak[2]))
612 gx.shrink = -round(diffmet_rule(-bk[3], -ak[3]))
616 g = node_copy(zero_glue)
618 set_attr(g, attr_icflag, KANJI_SKIP)
622 local function calc_ja_ja_aux(gb,ga, db, da)
623 local rbb, rab = (1-db)/2, (1-da)/2 -- 「前の文字」由来のグルーの割合
624 local rba, raa = (1+db)/2, (1+da)/2 -- 「前の文字」由来のグルーの割合
625 if diffmet_rule ~= math.two_pleft and diffmet_rule ~= math.two_pright
626 and diffmet_rule ~= math.two_paverage then
627 rbb, rab, rba, raa = 1,0,0,1
630 if ga then gb = node_new(id_kern); gb.kern = 0 else return nil end
632 ga = node_new(id_kern); ga.kern = 0
635 local k = node.type(gb.id) .. node.type(ga.id)
636 if k == 'glueglue' then
638 gb.spec.width = round(diffmet_rule(
639 rbb*gb.spec.width + rba*ga.spec.width,
640 rab*gb.spec.width + raa*ga.spec.width ))
641 gb.spec.stretch = round(diffmet_rule(
642 rbb*gb.spec.stretch + rba*ga.spec.stretch,
643 rab*gb.spec.stretch + raa*ga.spec.stretch ))
644 gb.spec.shrink = -round(diffmet_rule(
645 -rbb*gb.spec.shrink - rba*ga.spec.shrink,
646 -rab*gb.spec.shrink - raa*ga.spec.shrink ))
649 elseif k == 'kernkern' then
651 gb.kern = round(diffmet_rule(
652 rbb*gb.kern + rba*ga.kern,
653 rab*gb.kern + raa*ga.kern ))
656 elseif k == 'kernglue' then
657 -- gb: kern, ga: glue
658 ga.spec.width = round(diffmet_rule(
659 rbb*gb.kern + rba*ga.spec.width,
660 rab*gb.kern + raa*ga.spec.width ))
661 ga.spec.stretch = round(diffmet_rule(
662 rba*ga.spec.stretch, raa*ga.spec.stretch ))
663 ga.spec.shrink = -round(diffmet_rule(
664 -rba*ga.spec.shrink,-raa*ga.spec.shrink ))
668 -- gb: glue, ga: kern
669 gb.spec.width = round(diffmet_rule(
670 rba*ga.kern + rbb*gb.spec.width,
671 raa*ga.kern + rab*gb.spec.width ))
672 gb.spec.stretch = round(diffmet_rule(
673 rbb*gb.spec.stretch, rab*gb.spec.stretch ))
674 gb.spec.shrink = -round(diffmet_rule(
675 -rbb*gb.spec.shrink,-rab*gb.spec.shrink ))
681 local function calc_ja_ja_glue()
682 if ihb_flag then return nil
684 local qm, pm = Nq.met, Np.met
685 if (qm.char_type==pm.char_type) and (qm.var==pm.var) then
686 return new_jfm_glue(qm, Nq.class, Np.class)
688 local npn, nqn = Np.nuc, Nq.nuc
689 local gb, db = new_jfm_glue(qm, Nq.class,
690 slow_find_char_class(has_attr(npn, attr_orig_char), qm, npn.char))
691 local ga, da = new_jfm_glue(pm,
692 slow_find_char_class(has_attr(nqn, attr_orig_char), pm, nqn.char),
694 return calc_ja_ja_aux(gb, ga, db, da);
699 -------------------- 和欧文間空白量の決定
703 local function get_xkanjiskip_normal(Nn)
704 if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
705 return node_copy(xkanji_skip)
707 local g = node_copy(zero_glue)
708 set_attr(g, attr_icflag, XKANJI_SKIP)
712 local function get_xkanjiskip_jfm(Nn)
714 if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
715 g = node_new(id_glue); --copy_attr(g, Nn.nuc)
716 local gx = node_new(id_glue_spec);
717 gx.stretch_order, gx.shrink_order = 0, 0
718 local bk = Nn.met.xkanjiskip or {0, 0, 0}
719 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
722 g = node_copy(zero_glue)
724 set_attr(g, attr_icflag, XKANJI_SKIP)
730 -------------------- 隣接した「塊」間の処理
732 local function get_OA_skip()
735 return new_jfm_glue(pm,
736 fast_find_char_class(((Nq.id == id_math and -1) or (type(Nq.char)=='string' and Nq.char or 'jcharbdd')), pm), Np.class)
740 local function get_OB_skip()
743 return new_jfm_glue(qm, Nq.class,
744 fast_find_char_class(((Np.id == id_math and -1) or'jcharbdd'), qm))
749 -- (anything) .. jachar
750 local function handle_np_jachar(mode)
752 if qid==id_jglyph or ((qid==id_pbox or qid==id_pbox_w) and Nq.met) then
753 local g = calc_ja_ja_glue() or get_kanjiskip() -- M->K
754 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(g)
755 elseif Nq.met then -- qid==id_hlist
756 local g = get_OA_skip() or get_kanjiskip() -- O_A->K
757 handle_penalty_normal(0, Np.pre, g); real_insert(g)
759 local g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
760 handle_penalty_normal((qid==id_hlist and 0 or Nq.post), Np.pre, g); real_insert(g)
762 local g = get_OA_skip() -- O_A
763 if qid==id_glue then handle_penalty_normal(0, Np.pre, g)
764 elseif qid==id_kern then handle_penalty_suppress(0, Np.pre, g)
765 else handle_penalty_always(0, Np.pre, g)
769 if mode and Np.kcat%2~=1 then
770 widow_Np.first, widow_Bp, Bp = Np.first, Bp, widow_Bp
775 -- jachar .. (anything)
776 local function handle_nq_jachar()
778 local g = get_OB_skip() or get_xkanjiskip(Nq) -- O_B->X
779 handle_penalty_normal(Nq.post, (Np.id==id_hlist and 0 or Np.pre), g); real_insert(g)
781 local g = get_OB_skip() -- O_B
782 if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
783 elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
784 else handle_penalty_always(Nq.post, 0, g)
790 -- (anything) .. (和文文字で始まる hlist)
791 local function handle_np_ja_hlist()
793 if qid==id_jglyph or ((qid==id_pbox or Nq.id == id_pbox_w) and Nq.met) then
794 local g = get_OB_skip() or get_kanjiskip() -- O_B->K
795 handle_penalty_normal(Nq.post, 0, g); real_insert(g)
796 elseif Nq.met then -- Nq.id==id_hlist
797 local g = get_kanjiskip() -- K
798 handle_penalty_suppress(0, 0, g); real_insert(g)
800 local g = get_xkanjiskip(Np) -- X
801 handle_penalty_suppress(0, 0, g); real_insert(g)
805 -- (和文文字で終わる hlist) .. (anything)
806 local function handle_nq_ja_hlist()
808 local g = get_xkanjiskip(Nq) -- X
809 handle_penalty_suppress(0, 0, g); real_insert(g)
814 -- Nq が前側のクラスタとなることによる修正
816 local adjust_nq_aux = {
817 [id_glyph] = function()
819 return set_np_xspc_alchar(Nq, x.char,x, 2)
820 end, -- after_alchar(Nq)
821 [id_hlist] = function() after_hlist(Nq) end,
822 [id_pbox] = function() after_hlist(Nq) end,
823 [id_disc] = function() after_hlist(Nq) end,
824 [id_pbox_w] = function()
825 luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
831 local x = adjust_nq_aux[Nq.id]
837 -------------------- 開始・終了時の処理
840 local JWP = luatexja.stack_table_index.JWP
841 local function handle_list_tail(mode)
844 -- the current list is to be line-breaked.
845 -- Insert \jcharwidowpenalty
846 Bp = widow_Bp; Np = widow_Np
848 handle_penalty_normal(0,
849 table_current_stack[JWP] or 0)
852 -- the current list is the contents of a hbox
853 local npi, pm = Np.id, Np.met
854 if npi == id_jglyph or (npi==id_pbox and pm) then
855 local g = new_jfm_glue(pm, Np.class, fast_find_char_class('boxbdd', pm))
857 set_attr(g, attr_icflag, BOXBDD)
858 head = node.insert_after(head, Np.last, g)
865 local function handle_list_head(par_indented)
866 local npi, pm = Np.id, Np.met
867 if npi == id_jglyph or (npi==id_pbox and pm) then
869 local g = new_jfm_glue(pm, fast_find_char_class(par_indented, pm), Np.class)
871 set_attr(g, attr_icflag, BOXBDD)
872 if g.id==id_glue and #Bp==0 then
873 local h = node_new(id_penalty); --copy_attr(h, Np.nuc)
874 h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
876 head = insert_before(head, Np.first, g)
883 -- return value: (the initial cursor lp), (last node)
884 local function init_var(mode)
885 -- 1073741823: max_dimen
886 Bp, widow_Bp, widow_Np = {}, {}, {first = nil}
887 table_current_stack = ltjs.table_current_stack
889 kanji_skip = node_new(id_glue)
890 kanji_skip.spec = skip_table_to_spec('kanjiskip')
891 set_attr(kanji_skip, attr_icflag, KANJI_SKIP)
892 get_kanjiskip = (kanji_skip.spec.width == 1073741823)
893 and get_kanjiskip_jfm or get_kanjiskip_normal
895 xkanji_skip = node_new(id_glue)
896 xkanji_skip.spec = skip_table_to_spec('xkanjiskip')
897 set_attr(xkanji_skip, attr_icflag, XKANJI_SKIP)
898 get_xkanjiskip = (xkanji_skip.spec.width == 1073741823)
899 and get_xkanjiskip_jfm or get_xkanjiskip_normal
902 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
903 first=nil, id=nil, last=nil, met=nil, nuc=nil,
904 post=nil, pre=nil, xspc=nil,
907 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
908 first=nil, id=nil, last=nil, met=nil, nuc=nil,
909 post=nil, pre=nil, xspc=nil,
912 -- the current list is to be line-breaked:
913 -- hbox from \parindent is skipped.
914 local lp, par_indented, lpi, lps = head, 'boxbdd', head.id, head.subtype
915 while lp and ((lpi==id_whatsit and lps~=sid_user)
916 or ((lpi==id_hlist) and (lps==3))) do
917 if (lpi==id_hlist) and (lps==3) then par_indented = 'parbdd' end
918 lp=node_next(lp); lpi, lps = lp.id, lp.subtype end
919 return lp, node.tail(head), par_indented
921 -- the current list is the contents of a hbox:
923 local g = node_new(id_kern)
924 node.insert_after(head, node.tail(head), g); last = g
925 return head, g, 'boxbdd'
929 local function cleanup(mode, last)
930 -- adjust attr_icflag for avoiding error
931 tex.setattribute('global', attr_icflag, 0)
932 node.free(kanji_skip); node.free(xkanji_skip)
934 local h = node_next(head)
935 if h.id == id_penalty and h.penalty == 10000 then
937 if h.id == id_glue and h.subtype == 15 and not h.next then
943 head = node.remove(head, last); node.free(last);-- remove the sentinel
944 set_attr(head, attr_icflag,
945 get_attr_icflag(head) + PROCESSED_BEGIN_FLAG);
949 -------------------- 外部から呼ばれる関数
952 function main(ahead, mode)
953 if not ahead then return ahead end
955 local lp, last, par_indented = init_var(mode);
956 lp = calc_np(lp, last)
958 extract_np(); handle_list_head(par_indented)
960 return cleanup(mode, last)
962 lp = calc_np(lp, last)
966 local pid, pm = Np.id, Np.met
968 if pid == id_jglyph then
969 handle_np_jachar(mode)
971 if pid==id_hlist then handle_np_ja_hlist()
972 else handle_np_jachar() end
974 if Nq.id==id_hlist then handle_nq_ja_hlist()
975 else handle_nq_jachar() end
977 lp = calc_np(lp, last)
979 handle_list_tail(mode)
980 return cleanup(mode, last)
984 local IHB = luatexja.userid_table.IHB
985 local BPAR = luatexja.userid_table.BPAR
988 function create_inhibitglue_node()
989 local tn = node_new(id_whatsit, sid_user)
990 tn.user_id=IHB; tn.type=100; tn.value=1
994 -- Node for indicating beginning of a paragraph
996 function create_beginpar_node()
997 local tn = node_new(id_whatsit, sid_user)
998 tn.user_id=BPAR; tn.type=100; tn.value=1
1002 local function whatsit_callback(Np, lp, Nq)
1003 if Np and Np.nuc then return Np
1004 elseif Np and lp.user_id == BPAR then
1005 Np.first = lp; Np.nuc = lp; Np.last = lp
1008 Np.pre = 0; Np.post = 0
1010 Np.auto_xspc = false
1015 local function whatsit_after_callback(s, Nq, Np)
1016 if not s and Nq.nuc.user_id == BPAR then
1017 local x, y = node.prev(Nq.nuc), Nq.nuc
1018 Nq.first, Nq.nuc, Nq.last = x, x, x
1019 head = node.remove(head, y)
1024 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
1025 "luatexja.beginpar.np_info", 1)
1026 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
1027 "luatexja.beginpar.np_info_after", 1)