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('base'); local ltjb = luatexja.base
13 luatexja.load_module('stack'); local ltjs = luatexja.stack
14 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
15 luatexja.load_module('direction'); local ltjd = luatexja.direction
18 local Dnode = node.direct or node
20 local nullfunc = function(n) return n end
21 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
22 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
24 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
25 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
26 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
27 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
28 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
29 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
30 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
32 local has_attr = Dnode.has_attribute
33 local set_attr = Dnode.set_attribute
34 local insert_before = Dnode.insert_before
35 local insert_after = Dnode.insert_after
36 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
37 local round = tex.round
38 local ltjd_make_dir_node = ltjd.make_dir_node
39 local ltjf_font_metric_table = ltjf.font_metric_table
40 local ltjf_find_char_class = ltjf.find_char_class
41 local node_new = Dnode.new
42 local node_copy = Dnode.copy
43 local node_remove = luatexja.Dnode_remove -- Dnode.remove
44 local node_tail = Dnode.tail
45 local node_free = Dnode.free
46 local node_end_of_math = Dnode.end_of_math
51 local id_glyph = node.id('glyph')
52 local id_hlist = node.id('hlist')
53 local id_vlist = node.id('vlist')
54 local id_rule = node.id('rule')
55 local id_ins = node.id('ins')
56 local id_mark = node.id('mark')
57 local id_adjust = node.id('adjust')
58 local id_disc = node.id('disc')
59 local id_whatsit = node.id('whatsit')
60 local id_math = node.id('math')
61 local id_glue = node.id('glue')
62 local id_kern = node.id('kern')
63 local id_penalty = node.id('penalty')
65 local id_glue_spec = node.id('glue_spec')
66 local id_jglyph = 512 -- Japanese character
67 local id_box_like = 256 -- vbox, shifted hbox
68 local id_pbox = 257 -- already processed nodes (by \unhbox)
69 local id_pbox_w = 258 -- cluster which consists of a whatsit
70 local sid_user = node.subtype('user_defined')
72 local sid_start_link = node.subtype('pdf_start_link')
73 local sid_start_thread = node.subtype('pdf_start_thread')
74 local sid_end_link = node.subtype('pdf_end_link')
75 local sid_end_thread = node.subtype('pdf_end_thread')
77 local ITALIC = luatexja.icflag_table.ITALIC
78 local PACKED = luatexja.icflag_table.PACKED
79 local KINSOKU = luatexja.icflag_table.KINSOKU
80 local FROM_JFM = luatexja.icflag_table.FROM_JFM
81 local PROCESSED = luatexja.icflag_table.PROCESSED
82 local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
83 local BOXBDD = luatexja.icflag_table.BOXBDD
84 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
87 local table_current_stack
90 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
91 local attr_dir = luatexbase.attributes['ltj@dir']
92 local attr_icflag = luatexbase.attributes['ltj@icflag']
94 local function get_attr_icflag(p)
95 return (has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
98 -------------------- Helper functions
100 local function copy_attr(new, old)
101 -- 仕様が決まるまで off にしておく
104 -- This function is called only for acquiring `special' characters.
105 local function fast_find_char_class(c,m)
106 return m.chars[c] or 0
110 local slow_find_char_class
112 local start_time_measure = ltjb.start_time_measure
113 local stop_time_measure = ltjb.stop_time_measure
114 slow_find_char_class = function (c, m, oc)
115 start_time_measure('slow_find_chr')
116 local cls = ltjf_find_char_class(oc, m)
117 if not c and cls==0 then
118 stop_time_measure('slow_find_chr')
119 return ltjf_find_char_class(-c, m), oc
121 stop_time_measure('slow_find_chr')
127 local zero_glue = node_new(id_glue)
128 spec_zero_glue = to_node(node_new(id_glue_spec))
129 -- must be public, since mentioned from other sources
130 local spec_zero_glue = to_direct(spec_zero_glue)
131 setfield(spec_zero_glue, 'width', 0)
132 setfield(spec_zero_glue, 'stretch', 0)
133 setfield(spec_zero_glue, 'shrink', 0)
134 setfield(spec_zero_glue, 'stretch_order', 0)
135 setfield(spec_zero_glue, 'shrink_order', 0)
136 setfield(zero_glue, 'spec', spec_zero_glue)
138 local function skip_table_to_spec(n)
139 local g, st = node_new(id_glue_spec), ltjs.fast_get_stack_skip(n)
140 setfield(g, 'width', st.width)
141 setfield(g, 'stretch', st.stretch)
142 setfield(g, 'shrink', st.shrink)
143 setfield(g, 'stretch_order', st.stretch_order)
144 setfield(g, 'shrink_order', st.shrink_order)
150 local function add_penalty(p,e)
151 local pp = getfield(p, 'penalty')
153 if e<=-10000 then pp = 0 end
154 elseif pp<=-10000 then
155 if e>=10000 then pp = 0 end
158 if pp>=10000 then setfield(p, 'penalty', 10000)
159 elseif pp<=-10000 then setfield(p, 'penalty', -10000)
160 else setfield(p, 'penalty', pp) end
166 diffmet_rule = math.two_paverage
167 function math.two_add(a,b) return a+b end
168 function math.two_average(a,b) return (a+b)*0.5 end
169 function math.two_paverage(a,b) return (a+b)/2 end
170 function math.two_pleft(a,b) return a end
171 function math.two_pright(a,b) return b end
173 local head -- the head of current list
176 local widow_Bp, widow_Np -- \jcharwidowpenalty 挿入位置管理用
178 local ihb_flag -- JFM グルー挿入抑止用 flag
179 -- on: \inhibitglue 指定時,hlist の周囲
181 -------------------- hlist 内の文字の検索
183 local first_char, last_char, find_first_char
185 local ltjd_glyph_from_packed = ltjd.glyph_from_packed
186 local function check_box(box_ptr, box_end)
187 local p = box_ptr; local found_visible_node = false
189 find_first_char = false; last_char = nil
192 while p and p~=box_end do
194 if pid==id_kern and getsubtype(p)==2 then
195 p = node_next(node_next(node_next(p))); pid = getid(p) -- p must be glyph_node
197 if pid==id_glyph then
199 if find_first_char then
200 first_char = p; find_first_char = false
202 last_char = p; found_visible_node = true; p=node_next(p)
203 if (not p) or p==box_end then
204 return found_visible_node
206 until getid(p)~=id_glyph
207 pid = getid(p) -- p must be non-nil
210 local pa = get_attr_icflag(p)
211 --if pa==IC_PROCESSED or pa == PACKED then
212 if pa==IC_PROCESSED then
214 elseif getsubtype(p)==2 then
215 p = node_next(node_next(p));
216 -- Note that another node_next will be executed outside this if-statement.
218 found_visible_node = true
219 find_first_char = false; last_char = nil
221 elseif pid==id_hlist then
222 if PACKED == get_attr_icflag(p) then
223 local s = ltjd_glyph_from_packed(p)
224 if find_first_char then
225 first_char = s; find_first_char = false
227 last_char = s; found_visible_node = true
229 if getfield(p, 'shift')==0 then
230 if check_box(getlist(p), nil) then found_visible_node = true end
232 find_first_char = false; last_char = nil
235 elseif pid==id_math then
236 if find_first_char then
237 first_char = p; find_first_char = false
239 last_char = p; found_visible_node = true
240 --elseif pid==id_rule and get_attr_icflag(p)==PACKED then -- do nothing
241 elseif not (pid==id_ins or pid==id_mark
242 or pid==id_adjust or pid==id_whatsit
243 or pid==id_penalty) then
244 found_visible_node = true
245 find_first_char = false; last_char = nil
249 return found_visible_node
252 function check_box_high(Nx, box_ptr, box_end)
253 first_char = nil; last_char = nil; find_first_char = true
254 if check_box(box_ptr, box_end) then
255 local first_char = first_char
257 if getid(first_char)==id_glyph then
258 if getfont(first_char) == (has_attr(first_char, attr_curjfnt) or -1) then
259 set_np_xspc_jachar(Nx, first_char)
261 set_np_xspc_alchar(Nx, getchar(first_char),first_char, 1)
264 set_np_xspc_alchar(Nx, -1,first_char)
271 -------------------- Np の計算と情報取得
273 luatexbase.create_callback("luatexja.jfmglue.whatsit_getinfo", "data",
274 function (Np, lp, Nq)
275 if Np.nuc then return Np
277 return Np -- your code
280 luatexbase.create_callback("luatexja.jfmglue.whatsit_after", "data",
281 function (stat, Nq, Np) return false end)
286 local function set_attr_icflag_processed(p)
287 if get_attr_icflag(p)<= ITALIC then
288 set_attr(p, attr_icflag, PROCESSED)
292 local function check_next_ickern(lp)
293 if lp and getid(lp) == id_kern and ITALIC == get_attr_icflag(lp) then
294 set_attr(lp, attr_icflag, IC_PROCESSED)
295 Np.last = lp; return node_next(lp)
297 Np.last = Np.nuc; return lp
301 local function calc_np_pbox(lp, last)
302 Np.first = Np.first or lp; Np.id = id_pbox
303 local lpa, nc = KINSOKU, nil
304 set_attr(lp, attr_icflag, get_attr_icflag(lp));
305 while lp ~=last and (lpa>=PACKED) and (lpa<BOXBDD) do
306 nc, lp = lp, node_next(lp); lpa = lp and has_attr(lp, attr_icflag) or 0
307 -- get_attr_icflag() ではいけない!
310 return check_next_ickern(lp)
314 local calc_np_auxtable = {
315 [id_glyph] = function (lp)
316 Np.first, Np.nuc = (Np.first or lp), lp;
317 Np.id = (getfont(lp) == (has_attr(lp, attr_curjfnt) or -1))
318 and id_jglyph or id_glyph
319 return true, check_next_ickern(node_next(lp));
321 [id_hlist] = function(lp)
323 head, lp, op, flag = ltjd_make_dir_node(head, lp, list_dir, 'jfm hlist')
324 set_attr(op, attr_icflag, PROCESSED)
325 Np.first = Np.first or op; Np.last = op; Np.nuc = op;
326 Np.id = (flag or getfield(op, 'shift')~=0) and id_box_like or id_hlist
329 [id_vlist] = function(lp)
331 head, lp, op = ltjd_make_dir_node(head, lp, list_dir, 'jfm:' .. getid(lp))
332 Np.first = Np.first or op; Np.last = op; Np.nuc = op;
336 box_like = function(lp)
337 Np.first = Np.first or lp; Np.last = lp; Np.nuc = lp;
339 return true, node_next(lp)
342 set_attr(lp, attr_icflag, PROCESSED)
343 return false, node_next(lp)
345 [id_whatsit] = function(lp)
346 local lps = getsubtype(lp)
347 if lps==sid_user then
348 if getfield(lp, 'user_id')==luatexja.userid_table.IHB then
349 local lq = node_next(lp);
350 head = node_remove(head, lp); node_free(lp); ihb_flag = true
353 set_attr(lp, attr_icflag, PROCESSED)
354 luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo",
357 Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc;
358 return true, node_next(lp)
360 return false, node_next(lp)
364 -- we do special treatment for these whatsit nodes.
365 if lps == sid_start_link or lps == sid_start_thread then
367 elseif lps == sid_end_link or lps == sid_end_thread then
368 Np.first, Nq.last = nil, lp;
370 set_attr(lp, attr_icflag, PROCESSED)
371 return false, node_next(lp)
374 [id_math] = function(lp)
375 Np.first, Np.nuc = (Np.first or lp), lp;
376 set_attr(lp, attr_icflag, PROCESSED)
377 lp = node_end_of_math(lp)
378 set_attr(lp, attr_icflag, PROCESSED)
379 Np.last, Np.id = lp, id_math;
380 return true, node_next(lp);
382 discglue = function(lp)
383 Np.first, Np.nuc, Np.last = (Np.first or lp), lp, lp;
384 Np.id = getid(lp); set_attr(lp, attr_icflag, PROCESSED)
385 return true, node_next(lp)
387 [id_kern] = function(lp)
388 Np.first = Np.first or lp
389 if getsubtype(lp)==2 then
390 set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
391 set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
392 set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
393 set_attr(lp, attr_icflag, PROCESSED); Np.nuc = lp
394 Np.id = (getfont(lp) == (has_attr(lp, attr_curjfnt) or -1)) and id_jglyph or id_glyph
395 return true, check_next_ickern(node_next(lp));
397 Np.id = id_kern; set_attr(lp, attr_icflag, PROCESSED)
398 Np.last = lp; return true, node_next(lp)
401 [id_penalty] = function(lp)
402 Bp[#Bp+1] = lp; set_attr(lp, attr_icflag, PROCESSED)
403 return false, node_next(lp)
406 calc_np_auxtable[id_rule] = calc_np_auxtable.box_like
407 calc_np_auxtable[13] = calc_np_auxtable.box_like
408 calc_np_auxtable[id_ins] = calc_np_auxtable.skip
409 calc_np_auxtable[id_mark] = calc_np_auxtable.skip
410 calc_np_auxtable[id_adjust] = calc_np_auxtable.skip
411 calc_np_auxtable[id_disc] = calc_np_auxtable.discglue
412 calc_np_auxtable[id_glue] = calc_np_auxtable.discglue
414 function calc_np(lp, last)
416 -- We assume lp = node_next(Np.last)
417 Np, Nq, ihb_flag = Nq, Np, nil
418 -- We clear `predefined' entries of Np before pairs() loop,
419 -- because using only pairs() loop is slower.
420 Np.post, Np.pre, Np.xspc = nil, nil, nil
421 Np.first, Np.id, Np.last, Np.met, Np.class= nil, nil, nil, nil
422 Np.auto_kspc, Np.auto_xspc, Np.char, Np.nuc = nil, nil, nil, nil
423 for k in pairs(Np) do Np[k] = nil end
425 for k = 1,#Bp do Bp[k] = nil end
427 local lpa = has_attr(lp, attr_icflag) or 0
430 if lpa%PROCESSED_BEGIN_FLAG == BOXBDD then
431 local lq = node_next(lp)
432 head = node_remove(head, lp); node_free(lp); lp = lq
433 else return calc_np_pbox(lp, last)
436 k, lp = calc_np_auxtable[getid(lp)](lp)
437 if k then return lp end
444 local calc_np = calc_np
446 -- extract informations from Np
447 -- We think that "Np is a Japanese character" if Np.met~=nil,
448 -- "Np is an alphabetic character" if Np.pre~=nil,
449 -- "Np is not a character" otherwise.
450 after_hlist = nil -- global
451 local after_alchar, extract_np
453 local PRE = luatexja.stack_table_index.PRE
454 local POST = luatexja.stack_table_index.POST
455 local KCAT = luatexja.stack_table_index.KCAT
456 local XSP = luatexja.stack_table_index.XSP
459 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
460 local attr_orig_char = luatexbase.attributes['ltj@origchar']
461 local attr_autospc = luatexbase.attributes['ltj@autospc']
462 local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
463 function set_np_xspc_jachar(Nx, x)
464 local m = ltjf_font_metric_table[getfont(x)]
466 if list_dir == dir_tate then
467 local c1, c2 = getchar(x), has_attr(x, attr_orig_char)
468 c = has_attr(x, attr_dir) or c1 or c2
469 cls = ltjf_find_char_class(c, m)
470 if cls==0 then cls = slow_find_char_class(c2, m, c1) end
472 cls, c = slow_find_char_class(has_attr(x, attr_orig_char), m, getchar(x))
474 Nx.met, Nx.char = m, c; Nx.class = cls;
475 if cls~=0 then set_attr(x, attr_jchar_class, cls) end
476 Nx.pre = table_current_stack[PRE + c] or 0
477 Nx.post = table_current_stack[POST + c] or 0
478 Nx.xspc = table_current_stack[XSP + c] or 3
479 Nx.kcat = table_current_stack[KCAT + c] or 0
480 Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
482 local set_np_xspc_jachar = set_np_xspc_jachar
485 local floor = math.floor
486 function set_np_xspc_alchar(Nx, c,x, lig)
488 local f = (lig ==1) and nullfunc or node_tail
489 local xc, xs = getfield(x, 'components'), getsubtype(x)
490 while xc and xs and xs%4>=2 do
491 x = f(xc); xc, xs = getfield(x, 'components'), getsubtype(x)
494 Nx.pre = table_current_stack[PRE + c] or 0
495 Nx.post = table_current_stack[POST + c] or 0
496 Nx.xspc = table_current_stack[XSP + c] or 3
499 Nx.pre, Nx.post, Nx.char = 0, 0, -1
500 Nx.xspc = table_current_stack[XSP - 1] or 3
503 Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
505 local set_np_xspc_alchar = set_np_xspc_alchar
508 extract_np = function ()
509 local x, i = Np.nuc, Np.id;
510 if i == id_jglyph then return set_np_xspc_jachar(Np, x)
511 elseif i == id_glyph then return set_np_xspc_alchar(Np, getchar(x), x, 1)
512 elseif i == id_hlist then Np.last_char = check_box_high(Np, getlist(x), nil)
513 elseif i == id_pbox then Np.last_char = check_box_high(Np, Np.first, node_next(Np.last))
514 elseif i == id_disc then Np.last_char = check_box_high(Np, getfield(x, 'replace'), nil)
515 elseif i == id_math then return set_np_xspc_alchar(Np, -1, x)
519 -- change the information for the next loop
520 -- (will be done if Nx is an alphabetic character or a hlist)
521 after_hlist = function (Nx)
522 local s = Nx.last_char
524 if getid(s)==id_glyph then
525 if getfont(s) == (has_attr(s, attr_curjfnt) or -1) then
526 set_np_xspc_jachar(Nx, s)
528 set_np_xspc_alchar(Nx, getchar(s), s, 2)
531 set_np_xspc_alchar(Nx, -1, s)
534 Nx.pre, Nx.met = nil, nil
538 after_alchar = function (Nx)
540 return set_np_xspc_alchar(Nx, getchar(x), x, 2)
545 -------------------- 最下層の処理
547 -- change penalties (or create a new penalty, if needed)
548 local function handle_penalty_normal(post, pre, g)
549 local a = (pre or 0) + (post or 0)
551 if (a~=0 and not(g and getid(g)==id_kern)) then
552 local p = node_new(id_penalty)
553 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
554 setfield(p, 'penalty', a)
555 head = insert_before(head, Np.first, p)
557 set_attr(p, attr_icflag, KINSOKU)
559 else for _, v in pairs(Bp) do add_penalty(v,a) end
563 local function handle_penalty_always(post, pre, g)
564 local a = (pre or 0) + (post or 0)
566 if not (g and getid(g)==id_glue) then
567 local p = node_new(id_penalty)
568 if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
569 setfield(p, 'penalty', a)
570 head = insert_before(head, Np.first, p)
572 set_attr(p, attr_icflag, KINSOKU)
574 else for _, v in pairs(Bp) do add_penalty(v,a) end
578 local function handle_penalty_suppress(post, pre, g)
579 local a = (pre or 0) + (post or 0)
581 if g and getid(g)==id_glue then
582 local p = node_new(id_penalty)
583 setfield(p, 'penalty', 10000); head = insert_before(head, Np.first, p)
585 set_attr(p, attr_icflag, KINSOKU)
587 else for _, v in pairs(Bp) do add_penalty(v,a) end
591 -- 和文文字間の JFM glue を node 化
592 local function new_jfm_glue(m, bc, ac)
593 -- bc, ac: char classes
594 local g, d = m.char_type[bc][ac], 0
597 n,d = node_copy(g[2]), g[3]
599 local f = node_new(id_glue)
600 set_attr(f, attr_icflag, g[4])
601 setfield(f, 'spec', n)
608 -- Nq.last (kern w) .... (glue/kern g) Np.first
609 local function real_insert(g)
611 head = insert_before(head, Np.first, g)
617 -------------------- 和文文字間空白量の決定
621 local get_kanjiskip_normal, get_kanjiskip_jfm
623 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
624 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
625 get_kanjiskip_normal = function ()
626 if Np.auto_kspc or Nq.auto_kspc then
627 return node_copy(kanji_skip)
629 local g = node_copy(zero_glue)
630 set_attr(g, attr_icflag, KANJI_SKIP)
635 get_kanjiskip_jfm = function ()
637 if Np.auto_kspc or Nq.auto_kspc then
638 g = node_new(id_glue); --copy_attr(g, Nq.nuc)
639 local gx = node_new(id_glue_spec);
640 setfield(gx, 'stretch_order', 0); setfield(gx, 'shrink_order', 0)
641 local pm, qm = Np.met, Nq.met
642 local bk = qm.kanjiskip or {0, 0, 0}
643 if (pm.char_type==qm.char_type) and (qm.var==pm.var) then
644 setfield(gx, 'width', bk[1])
645 setfield(gx, 'stretch', bk[2])
646 setfield(gx, 'shrink', bk[3])
648 local ak = pm.kanjiskip or {0, 0, 0}
649 setfield(gx, 'width', round(diffmet_rule(bk[1], ak[1])))
650 setfield(gx, 'stretch', round(diffmet_rule(bk[2], ak[2])))
651 setfield(gx, 'shrink', -round(diffmet_rule(-bk[3], -ak[3])))
653 setfield(g, 'spec', gx)
655 g = node_copy(zero_glue)
657 set_attr(g, attr_icflag, KANJI_SKIP_JFM)
664 local bg_ag = 2*id_glue - id_glue
665 local bg_ak = 2*id_glue - id_kern
666 local bk_ag = 2*id_kern - id_glue
667 local bk_ak = 2*id_kern - id_kern
669 calc_ja_ja_aux = function (gb,ga, db, da)
670 local rbb, rab = (1-db)/2, (1-da)/2 -- 「前の文字」由来のグルーの割合
671 local rba, raa = (1+db)/2, (1+da)/2 -- 「前の文字」由来のグルーの割合
672 if diffmet_rule ~= math.two_pleft and diffmet_rule ~= math.two_pright
673 and diffmet_rule ~= math.two_paverage then
674 rbb, rab, rba, raa = 1,0,0,1
678 gb = node_new(id_kern); setfield(gb, 'kern', 0)
681 ga = node_new(id_kern); setfield(ga, 'kern', 0)
684 local k = 2*getid(gb) - getid(ga)
686 local bs, as = getfield(gb, 'spec'), getfield(ga, 'spec')
688 local bd, ad = getfield(bs, 'width'), getfield(as, 'width')
689 setfield(bs, 'width', round(diffmet_rule(rbb*bd + rba*ad, rab*bd + raa*ad)))
690 bd, ad = getfield(bs, 'stretch'), getfield(as, 'stretch')
691 setfield(bs, 'stretch', round(diffmet_rule(rbb*bd + rba*ad, rab*bd + raa*ad)))
692 bd, ad = getfield(bs, 'shrink'), getfield(as, 'shrink')
693 setfield(bs, 'shrink', -round(diffmet_rule(-rbb*bd - rba*ad, -rab*bd - raa*ad)))
696 elseif k == bk_ak then
698 local bd, ad = getfield(gb, 'kern'), getfield(ga, 'kern')
699 setfield(gb, 'kern', round(diffmet_rule(rbb*bd + rba*ad, rab*bd + raa*ad)))
702 elseif k == bk_ag then
703 local as = getfield(ga, 'spec')
704 -- gb: kern, ga: glue
705 local bd, ad = getfield(gb, 'kern'), getfield(as, 'width')
706 setfield(as, 'width', round(diffmet_rule(rbb*bd + rba*ad, rab*bd + raa*ad)))
707 ad = getfield(as, 'stretch')
708 setfield(bs, 'stretch', round(diffmet_rule(rba*ad, raa*ad)))
709 ad = getfield(as, 'shrink')
710 setfield(bs, 'shrink', -round(diffmet_rule(-rba*ad, -raa*ad)))
714 local bs = getfield(gb, 'spec')
715 -- gb: glue, ga: kern
716 local bd, ad = getfield(bs, 'width'), getfield(ga, 'kern')
717 setfield(bs, 'width', round(diffmet_rule(rbb*bd + rba*ad, rab*bd + raa*ad)))
718 bd = getfield(bs, 'stretch')
719 setfield(bs, 'stretch', round(diffmet_rule(rbb*bd, rab*bd)))
720 bd = getfield(bs, 'shrink')
721 setfield(bs, 'shrink', -round(diffmet_rule(-rbb*bd, -rab*bd)))
728 local function calc_ja_ja_glue()
729 if ihb_flag then return nil
731 local qm, pm = Nq.met, Np.met
732 if (qm.char_type==pm.char_type) and (qm.var==pm.var) then
733 return new_jfm_glue(qm, Nq.class, Np.class)
735 local npn, nqn = Np.nuc, Nq.nuc
736 local gb, db = new_jfm_glue(qm, Nq.class,
737 slow_find_char_class(has_attr(npn, attr_orig_char),
739 local ga, da = new_jfm_glue(pm,
740 slow_find_char_class(has_attr(nqn, attr_orig_char),
743 return calc_ja_ja_aux(gb, ga, db, da);
748 -------------------- 和欧文間空白量の決定
752 local get_xkanjiskip_normal, get_xkanjiskip_jfm
754 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
755 local XKANJI_SKIP_JFM = luatexja.icflag_table.XKANJI_SKIP_JFM
756 get_xkanjiskip_normal = function (Nn)
757 if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
758 local f = node_copy(xkanji_skip)
761 local g = node_copy(zero_glue)
762 set_attr(g, attr_icflag, XKANJI_SKIP)
766 get_xkanjiskip_jfm = function (Nn)
768 if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
769 g = node_new(id_glue)
770 local gx = node_new(id_glue_spec);
771 setfield(gx, 'stretch_order', 0); setfield(gx, 'shrink_order', 0)
772 local bk = Nn.met.xkanjiskip or {0, 0, 0}
773 setfield(gx, 'width', bk[1])
774 setfield(gx, 'stretch', bk[2])
775 setfield(gx, 'shrink', bk[3])
776 setfield(g, 'spec', gx)
778 g = node_copy(zero_glue)
780 set_attr(g, attr_icflag, XKANJI_SKIP_JFM)
785 -------------------- 隣接した「塊」間の処理
787 local function get_OA_skip()
790 return new_jfm_glue(pm,
791 fast_find_char_class(((Nq.id == id_math and -1) or (type(Nq.char)=='string' and Nq.char or 'jcharbdd')), pm), Np.class)
795 local function get_OB_skip()
798 return new_jfm_glue(qm, Nq.class,
799 fast_find_char_class(((Np.id == id_math and -1) or'jcharbdd'), qm))
804 -- (anything) .. jachar
805 local function handle_np_jachar(mode)
807 if qid==id_jglyph or ((qid==id_pbox or qid==id_pbox_w) and Nq.met) then
808 local g = calc_ja_ja_glue() or get_kanjiskip() -- M->K
809 handle_penalty_normal(Nq.post, Np.pre, g); real_insert(g)
810 elseif Nq.met then -- qid==id_hlist
811 local g = get_OA_skip() or get_kanjiskip() -- O_A->K
812 handle_penalty_normal(0, Np.pre, g); real_insert(g)
814 local g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
815 handle_penalty_normal((qid==id_hlist and 0 or Nq.post), Np.pre, g); real_insert(g)
817 local g = get_OA_skip() -- O_A
818 if qid==id_glue then handle_penalty_normal(0, Np.pre, g)
819 elseif qid==id_kern then handle_penalty_suppress(0, Np.pre, g)
820 else handle_penalty_always(0, Np.pre, g)
824 if mode and Np.kcat%2~=1 then
825 widow_Np.first, widow_Bp, Bp = Np.first, Bp, widow_Bp
830 -- jachar .. (anything)
831 local function handle_nq_jachar()
833 local g = get_OB_skip() or get_xkanjiskip(Nq) -- O_B->X
834 handle_penalty_normal(Nq.post, (Np.id==id_hlist and 0 or Np.pre), g); real_insert(g)
836 local g = get_OB_skip() -- O_B
837 if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
838 elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
839 else handle_penalty_always(Nq.post, 0, g)
845 -- (anything) .. (和文文字で始まる hlist)
846 local function handle_np_ja_hlist()
848 if qid==id_jglyph or ((qid==id_pbox or Nq.id == id_pbox_w) and Nq.met) then
849 local g = get_OB_skip() or get_kanjiskip() -- O_B->K
850 handle_penalty_normal(Nq.post, 0, g); real_insert(g)
851 elseif Nq.met then -- Nq.id==id_hlist
852 local g = get_kanjiskip() -- K
853 handle_penalty_suppress(0, 0, g); real_insert(g)
855 local g = get_xkanjiskip(Np) -- X
856 handle_penalty_suppress(0, 0, g); real_insert(g)
860 -- (和文文字で終わる hlist) .. (anything)
861 local function handle_nq_ja_hlist()
863 local g = get_xkanjiskip(Nq) -- X
864 handle_penalty_suppress(0, 0, g); real_insert(g)
869 -- Nq が前側のクラスタとなることによる修正
871 local adjust_nq_aux = {
872 [id_glyph] = function()
874 return set_np_xspc_alchar(Nq, getchar(x),x, 2)
875 end, -- after_alchar(Nq)
876 [id_hlist] = function() after_hlist(Nq) end,
877 [id_pbox] = function() after_hlist(Nq) end,
878 [id_disc] = function() after_hlist(Nq) end,
879 [id_pbox_w] = function()
880 luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
886 local x = adjust_nq_aux[Nq.id]
892 -------------------- 開始・終了時の処理
896 local JWP = luatexja.stack_table_index.JWP
897 local function handle_list_tail(mode)
900 -- the current list is to be line-breaked.
901 -- Insert \jcharwidowpenalty
902 Bp = widow_Bp; Np = widow_Np
904 handle_penalty_normal(0,
905 table_current_stack[JWP] or 0)
908 -- the current list is the contents of a hbox
909 local npi, pm = Np.id, Np.met
910 if npi == id_jglyph or (npi==id_pbox and pm) then
911 local g = new_jfm_glue(pm, Np.class, fast_find_char_class('boxbdd', pm))
913 set_attr(g, attr_icflag, BOXBDD)
914 head = insert_after(head, Np.last, g)
921 local function handle_list_head(par_indented)
922 local npi, pm = Np.id, Np.met
923 if npi == id_jglyph or (npi==id_pbox and pm) then
925 local g = new_jfm_glue(pm, fast_find_char_class(par_indented, pm), Np.class)
927 set_attr(g, attr_icflag, BOXBDD)
928 if getid(g)==id_glue and #Bp==0 then
929 local h = node_new(id_penalty)
930 setfield(h, 'penalty', 10000); set_attr(h, attr_icflag, BOXBDD)
932 head = insert_before(head, Np.first, g)
939 -- return value: (the initial cursor lp), (last node)
942 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
943 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
944 local KSK = luatexja.stack_table_index.KSK
945 local XSK = luatexja.stack_table_index.XSK
946 local DIR = luatexja.stack_table_index.DIR
947 init_var = function (mode)
948 -- 1073741823: max_dimen
949 Bp, widow_Bp, widow_Np = {}, {}, {first = nil}
950 table_current_stack = ltjs.table_current_stack
952 list_dir = ltjs.list_dir or dir_yoko
953 kanji_skip = node_new(id_glue)
954 setfield(kanji_skip, 'spec', skip_table_to_spec(KSK))
955 set_attr(kanji_skip, attr_icflag, KANJI_SKIP)
956 get_kanjiskip = (getfield(getfield(kanji_skip, 'spec'), 'width') == 1073741823)
957 and get_kanjiskip_jfm or get_kanjiskip_normal
959 xkanji_skip = node_new(id_glue)
960 setfield(xkanji_skip, 'spec', skip_table_to_spec(XSK))
961 set_attr(xkanji_skip, attr_icflag, XKANJI_SKIP)
962 get_xkanjiskip = (getfield(getfield(xkanji_skip, 'spec'), 'width') == 1073741823)
963 and get_xkanjiskip_jfm or get_xkanjiskip_normal
966 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
967 first=nil, id=nil, last=nil, met=nil, nuc=nil,
968 post=nil, pre=nil, xspc=nil,
971 auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
972 first=nil, id=nil, last=nil, met=nil, nuc=nil,
973 post=nil, pre=nil, xspc=nil,
976 -- the current list is to be line-breaked:
977 -- hbox from \parindent is skipped.
978 local lp, par_indented, lpi, lps = head, 'boxbdd', getid(head), getsubtype(head)
979 while lp and ((lpi==id_whatsit and lps~=sid_user)
980 or ((lpi==id_hlist) and (lps==3))) do
981 if (lpi==id_hlist) and (lps==3) then
982 Np.char, par_indented = 'parbdd', 'parbdd'
983 Np.width = getfield(lp, 'width')
985 lp=node_next(lp); lpi, lps = getid(lp), getsubtype(lp) end
986 return lp, node_tail(head), par_indented
988 return head, nil, 'boxbdd'
993 local function cleanup(mode)
994 -- adjust attr_icflag for avoiding error
995 tex.setattribute('global', attr_icflag, 0)
996 node_free(kanji_skip); node_free(xkanji_skip)
998 local h = node_next(head)
999 if getid(h) == id_penalty and getfield(h, 'penalty') == 10000 then
1001 if getid(h) == id_glue and getsubtype(h) == 15 and not node_next(h) then
1007 set_attr(head, attr_icflag,
1008 get_attr_icflag(head) + PROCESSED_BEGIN_FLAG);
1012 -------------------- 外部から呼ばれる関数
1015 function main(ahead, mode)
1016 if not ahead then return ahead end
1018 local lp, last, par_indented = init_var(mode)
1019 lp = calc_np(lp, last)
1021 extract_np(); handle_list_head(par_indented)
1023 return cleanup(mode)
1025 lp = calc_np(lp, last)
1029 local pid, pm = Np.id, Np.met
1031 if pid == id_jglyph then
1032 handle_np_jachar(mode)
1034 if pid==id_hlist then handle_np_ja_hlist()
1035 else handle_np_jachar() end
1037 if Nq.id==id_hlist then handle_nq_ja_hlist()
1038 else handle_nq_jachar() end
1040 lp = calc_np(lp, last)
1042 handle_list_tail(mode)
1043 return cleanup(mode)
1048 local IHB = luatexja.userid_table.IHB
1049 local BPAR = luatexja.userid_table.BPAR
1050 local node_prev = (Dnode ~= node) and Dnode.getprev or node.prev
1051 local node_write = Dnode.write
1054 function create_inhibitglue_node()
1055 local tn = node_new(id_whatsit, sid_user)
1056 setfield(tn, 'user_id', IHB)
1057 setfield(tn, 'type', 100)
1058 setfield(tn, 'value', 1)
1062 -- Node for indicating beginning of a paragraph
1063 -- (for ltjsclasses)
1064 function create_beginpar_node()
1065 local tn = node_new(id_whatsit, sid_user)
1066 setfield(tn, 'user_id', BPAR)
1067 setfield(tn, 'type', 100)
1068 setfield(tn, 'value', 1)
1072 local function whatsit_callback(Np, lp, Nq)
1073 if Np and Np.nuc then return Np
1074 elseif Np and getfield(lp, 'user_id') == BPAR then
1075 Np.first = lp; Np.nuc = lp; Np.last = lp
1078 Np.pre = 0; Np.post = 0
1080 Np.auto_xspc = false
1085 local function whatsit_after_callback(s, Nq, Np)
1086 if not s and getfield(Nq.nuc, 'user_id') == BPAR then
1087 local x, y = node_prev(Nq.nuc), Nq.nuc
1088 Nq.first, Nq.nuc, Nq.last = x, x, x
1089 head = node_remove(head, y)
1095 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
1096 "luatexja.beginpar.np_info", 1)
1097 luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
1098 "luatexja.beginpar.np_info_after", 1)