2 -- luatexja/jfmglue.lua
4 luatexbase.provides_module({
5 name = 'luatexja.jfmglue',
8 description = 'Insertion process of JFM glues and kanjiskip',
10 module('luatexja.jfmglue', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
13 luatexja.load_module('stack'); local ltjs = luatexja.stack
14 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
15 luatexja.load_module('pretreat'); local ltjp = luatexja.pretreat
17 local has_attr = node.has_attribute
18 local set_attr = node.set_attribute
19 local insert_before = node.insert_before
20 local node_next = node.next
21 local round = tex.round
22 local uniq_id = 0 -- unique id
23 local ltjs_fast_get_penalty_table = ltjs.fast_get_penalty_table
24 local ltjf_font_metric_table = ltjf.font_metric_table
25 local ltjf_find_char_class = ltjf.find_char_class
26 local node_new = node.new
27 local node_copy = node.copy
29 local ligature_head = 1
30 local ligature_tail = 2
32 local id_glyph = node.id('glyph')
33 local id_hlist = node.id('hlist')
34 local id_vlist = node.id('vlist')
35 local id_rule = node.id('rule')
36 local id_ins = node.id('ins')
37 local id_mark = node.id('mark')
38 local id_adjust = node.id('adjust')
39 local id_disc = node.id('disc')
40 local id_whatsit = node.id('whatsit')
41 local id_math = node.id('math')
42 local id_glue = node.id('glue')
43 local id_kern = node.id('kern')
44 local id_penalty = node.id('penalty')
46 local id_glue_spec = node.id('glue_spec')
47 local id_jglyph = node.id('glyph') + 256 -- Japanese character
48 local id_box_like = node.id('hlist') + 256 -- vbox, shifted hbox
49 local id_pbox = node.id('hlist') + 512 -- already processed nodes (by \unhbox)
50 local id_pbox_w = node.id('hlist') + 513 -- cluster which consists of a whatsit
51 local sid_user = node.subtype('user_defined')
53 local sid_start_link = node.subtype('pdf_start_link')
54 local sid_start_thread = node.subtype('pdf_start_thread')
55 local sid_end_link = node.subtype('pdf_end_link')
56 local sid_end_thread = node.subtype('pdf_end_thread')
66 local IC_PROCESSED = 9
68 local PROCESSED_BEGIN_FLAG = 16
73 local attr_orig_char = luatexbase.attributes['ltj@origchar']
74 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
75 local attr_icflag = luatexbase.attributes['ltj@icflag']
76 local attr_autospc = luatexbase.attributes['ltj@autospc']
77 local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
78 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
79 local max_dimen = 1073741823
81 local function get_attr_icflag(p)
82 return (has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
85 -------------------- Helper functions
87 local function copy_attr(new, old)
91 -- This function is called only for acquiring `special' characters.
92 local function fast_find_char_class(c,m)
93 return m.size_cache.chars[c] or 0
96 local spec_zero_glue = node_new(id_glue_spec)
97 spec_zero_glue.width = 0; spec_zero_glue.stretch_order = 0; spec_zero_glue.stretch = 0
98 spec_zero_glue.shrink_order = 0; spec_zero_glue.shrink = 0
100 local function get_zero_spec()
101 return node_copy(spec_zero_glue)
104 local function skip_table_to_spec(n)
105 local g, st = node_new(id_glue_spec), ltjs.fast_get_skip_table(n)
106 g.width = st.width; g.stretch = st.stretch; g.shrink = st.shrink
107 g.stretch_order = st.stretch_order; g.shrink_order = st.shrink_order
113 local function add_penalty(p,e)
114 if p.penalty>=10000 then
115 if e<=-10000 then p.penalty = 0 end
116 elseif p.penalty<=-10000 then
117 if e>=10000 then p.penalty = 0 end
119 p.penalty = p.penalty + e
120 if p.penalty>=10000 then p.penalty = 10000
121 elseif p.penalty<=-10000 then p.penalty = -10000 end
127 diffmet_rule = math.two_average
128 function math.two_add(a,b) return a+b end
129 function math.two_average(a,b) return (a+b)*0.5 end
131 -------------------- idea
132 -- 2 node の間に glue/kern/penalty を挿入する.
133 -- 基本方針: char node q と char node p の間
136 -- first: 最初の node,nuc: p,last: 最後の node
139 -- 実際の glue は Np.last, Nq.first の間に挿入される
140 -- Bp: Np.last, Nq.first の間の penalty node 達の配列
142 -- Np, Nq, Bp, widow_Bp について
144 -- 1回のループごとに Nq = Np, Np = (new table) となるのは効率が悪いので,
145 -- Np <-> Nq 入れ替え,その後 Np をクリアすることでテーブルを再利用.
146 -- 同様の関係は Bp, widow_Bp にも.
150 -- node x が non-char node のときは,x のみ
151 -- x が char_node のときは,
152 -- - x が \accent の第二引数だったとき
153 -- [kern2 kern y kern2] x の 3 node が核に加わる
154 -- - x の直後に \/ 由来 kern があったとき
155 -- その \/ 由来の kern が核に加わる
157 -- ins, mark, adjust, whatsit, penalty
159 -- Nq.last .. + .. Bp.first .... Bp[last] .... * .. Np.first
160 -- +: kern from LINEEND はここに入る
161 -- *: jfm glue はここに入る
163 local head -- the head of current list
166 local widow_Bp, widow_Np -- \jcharwidowpenalty 挿入位置管理用
168 local ihb_flag -- JFM グルー挿入抑止用 flag
169 -- on: \inhibitglue 指定時,hlist の周囲
171 -------------------- hlist 内の文字の検索
173 local first_char, last_char, find_first_char
175 local function check_box(box_ptr, box_end)
176 local p = box_ptr; local found_visible_node = false
178 find_first_char = false; first_char = nil; last_char = nil
181 while p and p~=box_end do
183 if pid==id_kern and p.subtype==2 then
184 p = node_next(node_next(node_next(p))); pid = p.id -- p must be glyph_node
186 if pid==id_glyph then
188 if find_first_char then
189 first_char = p; find_first_char = false
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
194 pid = p.id -- p must be non-nil
196 if pid==id_kern and get_attr_icflag(p)==IC_PROCESSED then
198 elseif pid==id_hlist then
199 if PACKED == get_attr_icflag(p) then
200 if find_first_char then
201 first_char = p.head; find_first_char = false
203 last_char = p.head; found_visible_node = true
206 if check_box(p.head, nil) then found_visible_node = true end
207 else if find_first_char then
208 find_first_char = false
214 elseif not (pid==id_ins or pid==id_mark
215 or pid==id_adjust or pid==id_whatsit
216 or pid==id_penalty) then
217 found_visible_node = true
218 if find_first_char then
219 find_first_char = false
226 return found_visible_node
229 function check_box_high(Nx, box_ptr, box_end)
230 first_char = nil; last_char = nil; find_first_char = true
231 if check_box(box_ptr, box_end) then
233 if first_char.font == has_attr(first_char, attr_curjfnt) then
234 set_np_xspc_jachar(Nx, first_char)
236 set_np_xspc_alchar(Nx, first_char.char,first_char, ligature_head)
243 -------------------- Np の計算と情報取得
245 luatexbase.create_callback("luatexja.jfmglue.whatsit_getinfo", "data",
246 function (Np, lp, Nq)
247 if Np.nuc then return Np
249 return Np -- your code
252 luatexbase.create_callback("luatexja.jfmglue.whatsit_after", "data",
253 function (stat, Nq, Np) return false end)
258 local function set_attr_icflag_processed(p)
259 if get_attr_icflag(p)<= ITALIC then
260 --set_attr(p, attr_uniqid, uniq_id)
261 set_attr(p, attr_icflag, PROCESSED)
265 local function check_next_ickern(lp)
266 if lp.id == id_kern and ITALIC == get_attr_icflag(lp) then
267 set_attr(lp, attr_icflag, IC_PROCESSED)
268 --set_attr(lp, attr_uniqid, uniq_id)
269 Np.last = lp; return node_next(lp)
271 Np.last = Np.nuc; return lp
275 local function calc_np_pbox(lp, last)
276 local uid = has_attr(lp, attr_uniqid)
277 Np.first = Np.first or lp; Np.id = id_pbox
278 local lpa = KINSOKU -- dummy=
279 set_attr(lp, attr_icflag, get_attr_icflag(lp));
280 while lp~=last and lpa>=PACKED and lpa<BOXBDD
281 and uid == has_attr(lp, attr_uniqid) do
282 Np.nuc = lp; --set_attr(lp, attr_uniqid, uniq_id)
283 lp = node_next(lp); lpa = has_attr(lp, attr_icflag)
284 -- get_attr_icflag() ではいけない!
286 return check_next_ickern(lp)
290 local calc_np_auxtable = {
291 [id_glyph] = function (lp)
292 Np.first, Np.nuc = (Np.first or lp), lp;
293 Np.id = (lp.font == has_attr(lp, attr_curjfnt)) and id_jglyph or id_glyph
294 --set_attr(lp, attr_uniqid, uniq_id)
295 --set_attr_icflag_processed(lp) treated in ltj-setwidth.lua
296 return true, check_next_ickern(node_next(lp));
298 [id_hlist] = function(lp)
299 Np.first = Np.first or lp; Np.last = lp; Np.nuc = lp;
300 set_attr_icflag_processed(lp)
301 Np.id = (lp.shift~=0) and id_box_like or id_hlist
302 return true, node_next(lp)
304 box_like = function(lp)
305 Np.first = Np.first or lp; Np.nuc = lp; Np.last = lp;
306 Np.id = id_box_like; set_attr_icflag_processed(lp);
307 return true, node_next(lp);
310 set_attr_icflag_processed(lp); return false, node_next(lp)
312 [id_whatsit] = function(lp)
313 if lp.subtype==sid_user then
314 if lp.user_id==30111 then
315 local lq = node_next(lp);
316 head = node.remove(head, lp); node.free(lp); ihb_flag = true
319 set_attr_icflag_processed(lp)
320 luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo",
323 Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc;
324 return true, node_next(lp)
326 return false, node_next(lp)
330 -- we do special treatment for these whatsit nodes.
331 if lp.subtype == sid_start_link or lp.subtype == sid_start_thread then
333 elseif lp.subtype == sid_end_link or lp.subtype == sid_end_thread then
334 Np.first, Nq.last = nil, lp;
336 set_attr_icflag_processed(lp); return false, node_next(lp)
339 [id_math] = function(lp)
340 Np.first, Np.nuc = (Np.first or lp), lp;
341 set_attr_icflag_processed(lp); lp = node_next(lp)
342 while lp.id~=id_math do
343 set_attr_icflag_processed(lp); lp = node_next(lp)
345 set_attr_icflag_processed(lp);
346 Np.last, Np.id = lp, id_math;
347 return true, node_next(lp);
349 discglue = function(lp)
350 Np.first, Np.nuc, Np.last = (Np.first or lp), lp, lp;
351 Np.id = lp.id; set_attr_icflag_processed(lp); 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_icflag_processed(lp); lp = node_next(lp)
357 set_attr_icflag_processed(lp); lp = node_next(lp)
358 set_attr_icflag_processed(lp); lp = node_next(lp)
359 set_attr_icflag_processed(lp); Np.nuc = lp
360 Np.id = (lp.font == has_attr(lp, attr_curjfnt)) and id_jglyph or id_glyph
361 return true, check_next_ickern(node_next(lp));
363 Np.id = id_kern; 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_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, false
386 for k in pairs(Np) do Np[k] = nil end
387 for k = 1,#Bp do Bp[k] = nil end
389 local lpa = has_attr(lp, attr_icflag) or 0
392 if lpa == BOXBDD then
393 local lq = node_next(lp)
394 head = node.remove(head, lp); node.free(lp); lp = lq
395 else return calc_np_pbox(lp, last)
398 k, lp = calc_np_auxtable[lp.id](lp)
399 if k then return lp end
406 local calc_np = calc_np
408 -- extract informations from Np
409 -- We think that "Np is a Japanese character" if Np.met~=nil,
410 -- "Np is an alphabetic character" if Np.pre~=nil,
411 -- "Np is not a character" otherwise.
415 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
416 function set_np_xspc_jachar(Nx, x)
417 local m = ltjf_font_metric_table[x.font]
418 local c = has_attr(x, attr_orig_char) or x.char
419 local cls = ltjf_find_char_class(x.char, m)
420 if c ~= x.char and cls==0 then cls = ltjf_find_char_class(-c, m) end
421 Nx.class = cls; set_attr(x, attr_jchar_class, cls)
422 Nx.lend = m.size_cache.char_type[cls].kern[fast_find_char_class('lineend', m)] or 0
423 Nx.met, Nx.var, Nx.char = m, m.var, c
424 Nx.pre = ltjs_fast_get_penalty_table('pre', c, 0)
425 Nx.post = ltjs_fast_get_penalty_table('post', c, 0)
426 local y = ltjs_fast_get_penalty_table('xsp', c, 3)
427 Nx.xspc_before, Nx.xspc_after = (y%2==1), (y>=2)
428 Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
430 local set_np_xspc_jachar = set_np_xspc_jachar
433 local floor = math.floor
434 function set_np_xspc_alchar(Nx, c,x, lig)
436 if lig == ligature_head then
437 while x.components and x.subtype and math.floor(x.subtype*0.5)%2==1 do
438 x = x.components; c = x.char
441 while x.components and x.subtype and math.floor(x.subtype*0.5)%2==1 do
442 x = node.tail(x.components); c = x.char
445 Nx.pre = ltjs_fast_get_penalty_table('pre', c, 0)
446 Nx.post = ltjs_fast_get_penalty_table('post', c, 0)
449 Nx.pre, Nx.post, Nx.char = 0, 0, -1
452 local y = ltjs_fast_get_penalty_table('xsp', c, 3)
453 Nx.xspc_before, Nx.xspc_after = (y%2==1), (y>=2)
454 Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
456 local set_np_xspc_alchar = set_np_xspc_alchar
459 function extract_np()
460 local x, i = Np.nuc, Np.id;
461 if i == id_jglyph then return set_np_xspc_jachar(Np, x)
462 elseif i == id_glyph then return set_np_xspc_alchar(Np, x.char, x, ligature_head)
463 elseif i == id_hlist then Np.last_char = check_box_high(Np, x.head, nil)
464 elseif i == id_pbox then Np.last_char = check_box_high(Np, Np.first, node_next(Np.last))
465 elseif i == id_disc then Np.last_char = check_box_high(Np, x.replace, nil)
466 elseif i == id_math then return set_np_xspc_alchar(Np, -1, x)
470 -- change the information for the next loop
471 -- (will be done if Nx is an alphabetic character or a hlist)
472 function after_hlist(Nx)
473 local s = Nx.last_char
475 if s.font == has_attr(s, attr_curjfnt) then
476 set_np_xspc_jachar(Nx, s)
478 set_np_xspc_alchar(Nx, s.char, s, ligature_tail)
481 Nx.pre, Nx.met = nil, nil
485 function after_alchar(Nx)
487 return set_np_xspc_alchar(Nx, x.char,x, ligature_tail)
491 local after_hlist, after_alchar, extract_np = after_hlist, after_alchar, extract_np
493 -------------------- 最下層の処理
495 local function lineend_fix(g)
496 if g and g.id==id_kern then
498 elseif Nq.lend~=0 then
500 g = node_new(id_kern); --copy_attr(g, Nq.nuc);
501 g.subtype = 1; g.kern = -Nq.lend;
502 set_attr(g, attr_icflag, LINEEND)
503 --set_attr(g, attr_uniqid, uniq_id)
505 g.spec.width = g.spec.width - Nq.lend
511 -- change penalties (or create a new penalty, if needed)
512 local function handle_penalty_normal(post, pre, g)
513 local a = (pre or 0) + (post or 0)
515 if (a~=0 and not(g and g.id==id_kern)) or Nq.lend~=0 then
516 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
517 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
519 head = insert_before(head, Np.first, p)
521 set_attr(p, attr_icflag, KINSOKU)
522 --set_attr(p, attr_uniqid, uniq_id)
524 --else for _, v in pairs(Bp) do v.penalty = v.penalty + a end
525 else for _, v in pairs(Bp) do add_penalty(v,a) end
529 local function handle_penalty_always(post, pre, g)
530 local a = (pre or 0) + (post or 0)
532 if not (g and g.id==id_glue) or Nq.lend~=0 then
533 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
534 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
536 head = insert_before(head, Np.first, p)
538 set_attr(p, attr_icflag, KINSOKU)
539 --set_attr(p, attr_uniqid, uniq_id)
541 else for _, v in pairs(Bp) do add_penalty(v,a) end
545 local function handle_penalty_suppress(post, pre, g)
546 local a = (pre or 0) + (post or 0)
548 if g and g.id==id_glue then
549 local p = node_new(id_penalty); --copy_attr(p, Nq.nuc)
550 p.penalty = 10000; head = insert_before(head, Np.first, p)
552 set_attr(p, attr_icflag, KINSOKU)
553 --set_attr(p, attr_uniqid, uniq_id)
555 else for _, v in pairs(Bp) do add_penalty(v,a) end
559 -- 和文文字間の JFM glue を node 化
560 local function new_jfm_glue(Nn, bc, ac)
561 -- bc, ac: char classes
562 local z = Nn.met.size_cache.char_type[bc]
565 g = node_copy(g); g.spec = node.copy(g.spec);
566 --set_attr(g, attr_uniqid, uniq_id)
567 elseif z.kern[ac] then
568 g = node_new(id_kern); --copy_attr(g, Nn.nuc)
569 g.subtype = 1; g.kern = z.kern[ac]
570 set_attr(g, attr_icflag, FROM_JFM); --set_attr(g, attr_uniqid, uniq_id)
575 -- Nq.last (kern w) .... (glue/kern g) Np.first
576 local function real_insert(w, g)
578 local h = node_new(id_kern); --copy_attr(h, Nq.nuc)
579 set_attr(h, attr_icflag, LINE_END)
580 --set_attr(h, attr_uniqid, uniq_id)
581 h.kern = w; h.subtype = 1
582 head = node.insert_after(head, Nq.last, h)
585 head = insert_before(head, Np.first, g)
591 -------------------- 和文文字間空白量の決定
596 local function get_kanjiskip_normal()
597 local g = node_new(id_glue); --copy_attr(g, Nq.nuc)
598 g.spec = (Np.auto_kspc or Nq.auto_kspc) and node_copy(kanji_skip) or get_zero_spec()
599 set_attr(g, attr_icflag, KANJI_SKIP)
600 --set_attr(g, attr_uniqid, uniq_id)
603 local function get_kanjiskip_jfm()
604 local g = node_new(id_glue); --copy_attr(g, Nq.nuc)
605 if Np.auto_kspc or Nq.auto_kspc then
606 local gx = node_new(id_glue_spec);
607 gx.stretch_order, gx.shrink_order = 0, 0
608 local bk = Nq.met.size_cache.kanjiskip
610 if (Np.met.size_cache==Nq.met.size_cache) and (Nq.var==Np.var) then
613 ak = Np.met.size_cache.kanjiskip
617 gx.width = round(diffmet_rule(bk[1], ak[1]))
618 gx.stretch = round(diffmet_rule(bk[2], ak[2]))
619 gx.shrink = -round(diffmet_rule(-bk[3], -ak[3]))
621 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
624 gx.width = ak[1]; gx.stretch = ak[2]; gx.shrink = ak[3]
626 gx.width, gx.stretch, gx.shrink = 0, 0, 0
630 g.spec = get_zero_spec()
632 set_attr(g, attr_icflag, KANJI_SKIP)
633 --set_attr(g, attr_uniqid, uniq_id)
637 local function calc_ja_ja_aux(gb,ga)
641 if not ga then return gb end
642 local k = node.type(gb.id) .. node.type(ga.id)
643 if k == 'glueglue' then
645 gb.spec.width = round(diffmet_rule(gb.spec.width, ga.spec.width))
646 gb.spec.stretch = round(diffmet_rule(gb.spec.stretch,ga.spec.shrink))
647 gb.spec.shrink = -round(diffmet_rule(-gb.spec.shrink, -ga.spec.shrink))
650 elseif k == 'kernkern' then
652 gb.kern = round(diffmet_rule(gb.kern, ga.kern))
655 elseif k == 'kernglue' then
656 -- gb: kern, ga: glue
657 ga.spec.width = round(diffmet_rule(gb.kern,ga.spec.width))
658 ga.spec.stretch = round(diffmet_rule(ga.spec.stretch, 0))
659 ga.spec.shrink = -round(diffmet_rule(-ga.spec.shrink, 0))
663 -- gb: glue, ga: kern
664 gb.spec.width = round(diffmet_rule(ga.kern, gb.spec.width))
665 gb.spec.stretch = round(diffmet_rule(gb.spec.stretch, 0))
666 gb.spec.shrink = -round(diffmet_rule(-gb.spec.shrink, 0))
673 local function calc_ja_ja_glue()
674 if ihb_flag then return nil
675 elseif (Nq.met.size_cache==Np.met.size_cache) and (Nq.var==Np.var) then
676 return new_jfm_glue(Nq, Nq.class, Np.class)
678 return calc_ja_ja_aux(new_jfm_glue(Nq, Nq.class, fast_find_char_class('diffmet',Nq.met)),
679 new_jfm_glue(Np, fast_find_char_class('diffmet',Np.met), Np.class))
683 -------------------- 和欧文間空白量の決定
687 local function get_xkanjiskip_normal(Nn)
688 local g = node_new(id_glue); --copy_attr(g, Nn.nuc)
689 local gx = node_new(id_glue_spec); g.spec = gx
690 if Nq.xspc_after and Np.xspc_before and (Nq.auto_xspc or Np.auto_xspc) then
691 g.spec = node_copy(xkanji_skip)
693 g.spec = get_zero_spec()
695 set_attr(g, attr_icflag, XKANJI_SKIP)
696 --set_attr(g, attr_uniqid, uniq_id)
699 local function get_xkanjiskip_jfm(Nn)
700 local g = node_new(id_glue); --copy_attr(g, Nn.nuc)
701 if Nq.xspc_after and Np.xspc_before and (Nq.auto_xspc or Np.auto_xspc) then
702 local gx = node_new(id_glue_spec);
703 gx.stretch_order, gx.shrink_order = 0, 0
704 local bk = Nn.met.size_cache.xkanjiskip
706 gx.width = bk[1]; gx.stretch = bk[2]; gx.shrink = bk[3]
708 gx.width, gx.stretch, gx.shrink = 0, 0, 0
712 g.spec = get_zero_spec()
714 set_attr(g, attr_icflag, XKANJI_SKIP)
715 --set_attr(g, attr_uniqid, uniq_id)
721 -------------------- 隣接した「塊」間の処理
723 local function get_OA_skip()
725 return new_jfm_glue(Np,
726 fast_find_char_class(((Nq.id == id_math and -1) or 'jcharbdd'), Np.met), Np.class)
730 local function get_OB_skip()
732 return new_jfm_glue(Nq, Nq.class,
733 fast_find_char_class(((Np.id == id_math and -1) or'jcharbdd'), Nq.met))
738 -- (anything) .. jachar
739 local function handle_np_jachar(mode)
740 if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id==id_pbox_w) and Nq.met) then
741 local g = lineend_fix(calc_ja_ja_glue() or get_kanjiskip()) -- M->K
742 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
743 elseif Nq.met then -- Nq.id==id_hlist
744 local g = get_OA_skip() or get_kanjiskip() -- O_A->K
745 handle_penalty_normal(0, Np.pre, g); real_insert(0, g)
747 local g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
748 if Nq.id==id_hlist then Nq.post = 0 end
749 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(0, g)
751 local g = get_OA_skip() -- O_A
752 if Nq.id==id_glue then handle_penalty_normal(0, Np.pre, g)
753 elseif Nq.id==id_kern then handle_penalty_suppress(0, Np.pre, g)
754 else handle_penalty_always(0, Np.pre, g)
758 if mode and ltjs_fast_get_penalty_table('kcat', Np.char, 0)%2~=1 then
759 widow_Np.first, widow_Bp, Bp = Np.first, Bp, widow_Bp
764 -- jachar .. (anything)
765 local function handle_nq_jachar()
767 if Np.id==id_hlist then Np.pre = 0 end
768 local g = lineend_fix(get_OB_skip() or get_xkanjiskip(Nq)) -- O_B->X
769 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(Nq.lend, g)
771 local g = lineend_fix(get_OB_skip()) -- O_B
772 if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
773 elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
774 else handle_penalty_always(Nq.post, 0, g)
776 real_insert(Nq.lend, g)
780 -- (anything) .. (和文文字で始まる hlist)
781 local function handle_np_ja_hlist()
782 if Nq.id==id_jglyph or ((Nq.id==id_pbox or Nq.id == id_pbox_w) and Nq.met) then
783 local g = lineend_fix(get_OB_skip() or get_kanjiskip()) -- O_B->K
784 handle_penalty_normal(Nq.post, 0, g); real_insert(Nq.lend, g)
785 elseif Nq.met then -- Nq.id==id_hlist
786 local g = get_kanjiskip() -- K
787 handle_penalty_suppress(0, 0, g); real_insert(0, g)
789 local g = get_xkanjiskip(Np) -- X
790 handle_penalty_suppress(0, 0, g); real_insert(0, g)
794 -- (和文文字で終わる hlist) .. (anything)
795 local function handle_nq_ja_hlist()
797 local g = get_xkanjiskip(Nq) -- X
798 handle_penalty_suppress(0, 0, g); real_insert(0, g)
802 -- Nq が前側のクラスタとなることによる修正
803 local function adjust_nq()
804 if Nq.id==id_glyph then after_alchar(Nq)
805 elseif Nq.id==id_hlist or Nq.id==id_pbox or Nq.id==id_disc then after_hlist(Nq)
806 elseif Nq.id == id_pbox_w then
807 luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
812 -------------------- 開始・終了時の処理
815 local function handle_list_tail(mode)
818 -- the current list is to be line-breaked:
819 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
821 g = node_new(id_kern); g.subtype = 0; g.kern = Np.lend
822 --copy_attr(g, Np.nuc);
823 set_attr(g, attr_icflag, BOXBDD)
824 node.insert_after(head, Np.last, g)
827 -- Insert \jcharwidowpenalty
828 Bp = widow_Bp; Np = widow_Np; Nq.lend = 0
830 handle_penalty_normal(0,
831 ltjs_fast_get_penalty_table('jwp', 0, 0))
834 -- the current list is the contents of a hbox
835 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
836 local g = new_jfm_glue(Np, Np.class, fast_find_char_class('boxbdd',Np.met))
838 set_attr(g, attr_icflag, BOXBDD)
839 head = node.insert_after(head, Np.last, g)
846 local function handle_list_head(par_indented)
847 if Np.id == id_jglyph or (Np.id==id_pbox and Np.met) then
849 local g = new_jfm_glue(Np, fast_find_char_class(par_indented, Np.met), Np.class)
851 set_attr(g, attr_icflag, BOXBDD)
852 if g.id==id_glue and #Bp==0 then
853 local h = node_new(id_penalty); --copy_attr(h, Np.nuc)
854 h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
856 head = insert_before(head, Np.first, g)
863 -- return value: (the initial cursor lp), (last node)
864 local function init_var(mode)
866 if uniq_id == 0x7FFFFFF then uniq_id = 0 end
867 Bp, widow_Bp, widow_Np = {}, {}, {first = nil}
868 kanji_skip=skip_table_to_spec('kanjiskip')
869 get_kanjiskip = (kanji_skip.width == max_dimen)
870 and get_kanjiskip_jfm or get_kanjiskip_normal
871 xkanji_skip=skip_table_to_spec('xkanjiskip')
872 get_xkanjiskip = (xkanji_skip.width == max_dimen)
873 and get_xkanjiskip_jfm or get_xkanjiskip_normal
875 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
876 first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil,
877 post=nil, pre=nil, xspc_after=nil, xspc_before=nil,
880 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
881 first=nil, id=nil, last=nil, lend=0, met=nil, nuc=nil,
882 post=nil, pre=nil, xspc_after=nil, xspc_before=nil,
885 -- the current list is to be line-breaked:
886 -- hbox from \parindent is skipped.
887 local lp, par_indented = head, 'boxbdd'
888 while lp and ((lp.id==id_whatsit and lp.subtype~=sid_user)
889 or ((lp.id==id_hlist) and (lp.subtype==3))) do
890 if (lp.id==id_hlist) and (lp.subtype==3) then par_indented = 'parbdd' end
892 return lp, node.tail(head), par_indented
894 -- the current list is the contents of a hbox:
896 local g = node_new(id_kern)
897 node.insert_after(head, node.tail(head), g); last = g
898 return head, g, 'boxbdd'
902 local function cleanup(mode, last)
903 -- adjust attr_icflag for avoiding error
904 tex.setattribute('global', attr_icflag, 0)
905 node.free(kanji_skip); node.free(xkanji_skip)
907 local h = node_next(head)
908 if h.id == id_penalty and h.penalty == 10000 then
910 if h.id == id_glue and h.subtype == 15 and not h.next then
916 head = node.remove(head, last); node.free(last);-- remove the sentinel
917 set_attr(head, attr_icflag,
918 get_attr_icflag(head) + PROCESSED_BEGIN_FLAG);
922 -------------------- 外部から呼ばれる関数
925 function main(ahead, mode)
926 if not ahead then return ahead end
928 local lp, last, par_indented = init_var(mode);
929 lp = calc_np(lp, last)
931 extract_np(); handle_list_head(par_indented)
933 return cleanup(mode, last)
935 lp = calc_np(lp, last)
937 extract_np(); adjust_nq()
939 if Np.id == id_jglyph then
940 handle_np_jachar(mode)
942 if Np.id==id_hlist then handle_np_ja_hlist()
943 else handle_np_jachar() end
945 if Nq.id==id_hlist then handle_nq_ja_hlist()
946 else handle_nq_jachar() end
948 lp = calc_np(lp, last)
950 handle_list_tail(mode)
951 return cleanup(mode, last)
956 function create_inhibitglue_node()
957 local tn = node_new(id_whatsit, sid_user)
958 tn.user_id=30111; tn.type=100; tn.value=1
962 -- Node for indicating beginning of a paragraph
964 function create_beginpar_node()
965 local tn = node_new(id_whatsit, sid_user)
966 tn.user_id=30114; tn.type=100; tn.value=1
972 local function whatsit_callback(Np, lp, Nq)
973 if Np and Np.nuc then return Np
974 elseif Np and lp.user_id == 30114 then
975 Np.first = lp; Np.nuc = lp; Np.last = lp
978 Np.pre = 0; Np.post = 0
979 Np.xspc_before = false
980 Np.xspc_after = false
985 local function whatsit_after_callback(s, Nq, Np)
986 if not s and Nq.nuc.user_id == 30114 then
987 local x, y = node.prev(Nq.nuc), Nq.nuc
988 Nq.first, Nq.nuc, Nq.last = x, x, x
989 head = node.remove(head, y)
994 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
995 "luatexja.beginpar.np_info", 1)
996 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
997 "luatexja.beginpar.np_info_after", 1)