4 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
5 luatexja.load_module('jfmglue'); local ltjj = luatexja.jfmglue
6 luatexja.load_module('stack'); local ltjs = luatexja.stack
7 luatexja.load_module('direction'); local ltjd = luatexja.direction
9 local to_node = node.direct.tonode
10 local to_direct = node.direct.todirect
12 local setfield = node.direct.setfield
13 local setglue = luatexja.setglue
14 local getglue = luatexja.getglue
15 local getfield = node.direct.getfield
16 local is_zero_glue = node.direct.is_zero_glue
17 local getlist = node.direct.getlist
18 local getid = node.direct.getid
19 local getfont = node.direct.getfont
20 local getsubtype = node.direct.getsubtype
22 local node_traverse_id = node.direct.traverse_id
23 local node_new = node.direct.new
24 local node_copy = node.direct.copy
25 local node_hpack = node.direct.hpack
26 local node_next = node.direct.getnext
27 local node_free = node.direct.free
28 local node_prev = node.direct.getprev
29 local node_tail = node.direct.tail
30 local has_attr = node.direct.has_attribute
31 local set_attr = node.direct.set_attribute
32 local insert_after = node.direct.insert_after
34 local id_glyph = node.id('glyph')
35 local id_kern = node.id('kern')
36 local id_hlist = node.id('hlist')
37 local id_glue = node.id('glue')
38 local id_whatsit = node.id('whatsit')
39 local id_penalty = node.id('penalty')
40 local attr_icflag = luatexbase.attributes['ltj@icflag']
41 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
42 local lang_ja = luatexja.lang_ja
44 local ltjf_font_metric_table = ltjf.font_metric_table
45 local round, pairs = tex.round, pairs
47 local PACKED = luatexja.icflag_table.PACKED
48 local LINEEND = luatexja.icflag_table.LINEEND
49 local FROM_JFM = luatexja.icflag_table.FROM_JFM
50 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
51 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
52 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
53 local XKANJI_SKIP_JFM = luatexja.icflag_table.XKANJI_SKIP_JFM
55 local priority_table = {
67 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
68 get_attr_icflag = function(p)
69 return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
73 -- box 内で伸縮された glue の合計値を計算
75 local total_stsh = {{},{}}
76 local total_st, total_sh = total_stsh[1], total_stsh[2]
77 local get_total_stretched
79 local dimensions = node.direct.dimensions
80 function get_total_stretched(p)
81 -- return value: <補正値(sp)>
83 if not ph then return 0 end
84 for i,_ in pairs(total_st) do total_st[i]=nil; total_sh[i]=nil end
85 for i=1,#priority_table do
86 total_st[priority_table[i]]=0; total_sh[priority_table[i]]=0;
88 for i=0,4 do total_st[i*65536]=0; total_sh[i*65536]=0 end
89 total_st[-1]=0; total_sh[-1]=0;
90 for q in node_traverse_id(id_glue, ph) do
91 local a = getfield(q, 'stretch_order')
92 if a>0 then a=a*65536 else
93 total_st[0] = total_st[0]+getfield(q, 'stretch')
94 a = get_attr_icflag(q)
95 if a == KANJI_SKIP_JFM then a = KANJI_SKIP
96 elseif a == XKANJI_SKIP_JFM then a = XKANJI_SKIP
97 elseif type(total_st[a])~='number' then a = -1 end
99 total_st[a] = total_st[a]+getfield(q, 'stretch')
100 local a = getfield(q, 'shrink_order')
101 if a>0 then a=a*65536 else
102 total_sh[0] = total_sh[0]+getfield(q, 'shrink')
103 a = get_attr_icflag(q)
104 if a == KANJI_SKIP_JFM then a = KANJI_SKIP
105 elseif a == XKANJI_SKIP_JFM then a = XKANJI_SKIP
106 elseif type(total_sh[a])~='number' then a = -1 end
108 total_sh[a] = total_sh[a]+getfield(q, 'shrink')
110 for i=4,1,-1 do if total_st[i*65536]~=0 then total_st.order=i; break end; end
111 if not total_st.order then
112 total_st.order, total_st[-65536] = -1,0.1 -- dummy
114 for i=4,1,-1 do if total_sh[i*65536]~=0 then total_sh.order=i; break end; end
115 if not total_sh.order then
116 total_sh.order, total_sh[-65536] = -1,0.1 -- dummy
118 return getfield(p,'width') - dimensions(ph)
122 local function clear_stretch(p, ic, name)
123 for q in node_traverse_id(id_glue, getlist(p)) do
124 local f = get_attr_icflag(q)
125 if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM))
126 or ((ic ==XKANJI_SKIP) and (f == XKANJI_SKIP_JFM)) then
127 setfield(q, name..'_order', 0)
133 local function set_stretch(p, after, before, ic, name)
135 local ratio = after/before
136 for q in node_traverse_id(id_glue, getlist(p)) do
137 local f = get_attr_icflag(q)
138 if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM))
139 or ((ic ==XKANJI_SKIP) and (f == XKANJI_SKIP_JFM)) then
140 if getfield(q, name..'_order')==0 then
141 setfield(q, name, getfield(q, name)*ratio)
148 -- step 1: 行末に kern を挿入(句読点,中点用)
150 local ltjd_glyph_from_packed = ltjd.glyph_from_packed
151 local function aw_step1(p, total)
152 local head = getlist(p)
153 local x = node_tail(head); if not x then return total, false end
155 x = node_prev(x); if not x then return total, false end
156 local xi, xc = getid(x)
158 while xi==id_penalty do
159 x = node_prev(x); if not x then return total, false end
162 if (total>0 and total_st.order>0) or (total<0 and total_sh.order>0) then
163 -- 無限大のグルーで処理が行われているときは処理中止.
166 if xi == id_glyph and getfield(x, 'lang')==lang_ja then
169 elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
171 xc = ltjd_glyph_from_packed(x)
172 while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
174 return total, false-- それ以外は対象外.
176 local eadt = ltjf_font_metric_table[getfont(xc)]
177 .char_type[has_attr(xc, attr_jchar_class) or 0].end_adjust
181 local eadt_ratio = {}
182 for i, v in ipairs(eadt) do
185 eadt_ratio[i] = {i, t/total_st[65536*total_st.order], t, v}
187 eadt_ratio[i] = {i, t/total_sh[65536*total_sh.order], t, v}
190 table.sort(eadt_ratio,
193 local at, bt = abs(a[i]), abs(b[i])
194 if at~=bt then return at<bt end
198 if eadt[eadt_ratio[1][1]]~=0 then
199 local kn = node_new(id_kern, 1)
200 setfield(kn, 'kern', eadt[eadt_ratio[1][1]]); set_attr(kn, attr_icflag, LINEEND)
201 insert_after(head, x, kn)
202 return eadt_ratio[1][3], true
209 local min, max = math.min, math.max
210 local function aw_step1_last(p, total)
211 local head = getlist(p)
212 local x = node_tail(head); if not x then return total, false end
214 pf = node_prev(x); if not x then return total, false end
215 if getid(pf) ~= id_glue or getsubtype(pf) ~= 15 then return total, false end
216 x = node_prev(node_prev(pf)); xi = getid(x)
217 local xi, xc = getid(x)
218 if xi == id_glyph and getfield(x, 'lang')==lang_ja then
221 elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
223 xc = ltjd_glyph_from_packed(x)
224 while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
226 return total, false-- それ以外は対象外.
228 -- 続行条件1:無限の伸縮度を持つグルーは \parfillskipのみ
229 if total>0 and total_st.order>0 then
230 if total_st.order ~= getfield(pf, 'stretch_order') then return total, false end
231 if total_st[total_st.order*65536] ~= getfield(pf, 'stretch') then return total, false end
232 for i=total_st.order-1, 1, -1 do
233 if total_st[i*65536] ~= 0 then return total, false end
236 if total<0 and total_sh.order>0 then
237 if total_sh.order ~= getfield(pf, 'shrink_order') then return total, false end
238 if total_sh[total_sh.order*65536] ~= getfield(pf, 'shrink') then return total, false end
239 for i=total_sh.order-1, 1, -1 do
240 if total_sh[i*65536] ~= 0 then return total, false end
243 local eadt = ltjf_font_metric_table[getfont(xc)]
244 .char_type[has_attr(xc, attr_jchar_class) or 0].end_adjust
248 -- 続行条件2: min(eadt[1], 0)<= \parfillskip <= max(eadt[#eadt], 0)
249 local pfw = getfield(pf, 'width')
250 + (total>0 and getfield(pf, 'stretch') or -getfield(pf, 'shrink')) *getfield(p, 'glue_set')
251 if pfw<min(0,eadt[1]) or max(0,eadt[#eadt])<pfw then return total, false end
252 -- \parfillskip を 0 にする
253 total = total + getfield(pf, 'width')
254 total_st.order, total_sh.order = 0, 0
255 if getfield(pf, 'stretch_order')==0 then
256 total_st[0] = total_st[0] - getfield(pf, 'stretch')
257 total_st[-1] = total_st[-1] - getfield(pf, 'stretch')
258 total_st.order = (total_st[0]==0) and -1 or 0
260 if getfield(pf, 'shrink_order')==0 then
261 total_sh[0] = total_sh[0] - getfield(pf, 'shrink')
262 total_sh[-1] = total_sh[-1] - getfield(pf, 'shrink')
263 total_sh.order = (total_sh[0]==0) and -1 or 0
265 setfield(pf, 'subtype', 1); setglue(pf)
266 local eadt_ratio = {}
267 for i, v in ipairs(eadt) do
270 eadt_ratio[i] = {i, t/total_st[65536*total_st.order], t, v}
272 eadt_ratio[i] = {i, t/total_sh[65536*total_sh.order], t, v}
275 table.sort(eadt_ratio,
278 local at, bt = abs(a[i]), abs(b[i])
279 if at~=bt then return at<bt end
283 if eadt[eadt_ratio[1][1]]~=0 then
284 local kn = node_new(id_kern, 1)
285 setfield(kn, 'kern', eadt[eadt_ratio[1][1]]); set_attr(kn, attr_icflag, LINEEND)
286 insert_after(head, x, kn)
287 return eadt_ratio[1][3], true
294 -- step 2: 行中の glue を変える
295 local function aw_step2_dummy(p, _, added_flag)
296 if added_flag then -- 行末に kern 追加したので,それによる補正
297 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
298 setfield(f, 'head', nil)
299 setfield(p, 'glue_set', getfield(f, 'glue_set'))
300 setfield(p, 'glue_order', getfield(f, 'glue_order'))
301 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
306 local function aw_step2(p, total, added_flag)
307 local name = (total>0) and 'stretch' or 'shrink'
308 local res = total_stsh[(total>0) and 1 or 2]
309 if total==0 or res.order > 0 then
310 -- もともと伸縮の必要なしか,残りの伸縮量は無限大
311 if added_flag then -- 行末に kern 追加したので,それによる補正
312 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
313 setfield(f, 'head', nil)
314 setfield(p, 'glue_set', getfield(f, 'glue_set'))
315 setfield(p, 'glue_order', getfield(f, 'glue_order'))
316 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
322 if total <= res[-1] then -- 和文処理グルー以外で足りる
323 for _,v in pairs(priority_table) do clear_stretch(p, v, name) end
324 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
325 setfield(f, 'head', nil)
326 setfield(p, 'glue_set', getfield(f, 'glue_set'))
327 setfield(p, 'glue_order', getfield(f, 'glue_order'))
328 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
331 total = total - res[-1];
332 for i = 1, #priority_table do
333 local v = priority_table[i]
334 if total <= res[v] then
335 for j = i+1,#priority_table do
336 clear_stretch(p, priority_table[j], name)
338 set_stretch(p, total, res[v], v, name); break
340 total = total - res[v]
342 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
343 setfield(f, 'head', nil)
344 setfield(p, 'glue_set', getfield(f, 'glue_set'))
345 setfield(p, 'glue_order', getfield(f, 'glue_order'))
346 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
351 -- step 1': lineend=extended の場合(行分割時に考慮))
352 local insert_lineend_kern
354 local insert_before = node.direct.insert_before
355 local KINSOKU = luatexja.icflag_table.KINSOKU
356 function insert_lineend_kern(head, nq, np, Bp)
358 local eadt = nq.met.char_type[nq.class].end_adjust
359 if not eadt then return end
361 local x = node_new(id_kern, 1)
362 setfield(x, 'kern', eadt[1]); set_attr(x, attr_icflag, LINEEND)
363 insert_before(head, np.first, x)
365 local eadt_num = #eadt
367 local x = node_new(id_penalty)
368 setfield(x, 'penalty', 0); set_attr(x, attr_icflag, KINSOKU)
369 insert_before(head, np.first, x); Bp[#Bp+1] = x
370 local x = node_new(id_kern, 1)
371 setfield(x, 'kern', eadt[i]-eadt[i-1]); set_attr(x, attr_icflag, LINEEND)
372 insert_before(head, np.first, x)
374 if eadt_num>1 or eadt[1]~=0 then
375 local x = node_new(id_penalty)
376 setfield(x, 'penalty', 0); set_attr(x, attr_icflag, KINSOKU)
377 insert_before(head, np.first, x); Bp[#Bp+1] = x
378 local x = node_new(id_kern, 1)
379 setfield(x, 'kern', -eadt[eadt_num]); set_attr(x, attr_icflag, LINEEND)
380 insert_before(head, np.first, x)
381 local x = node_new(id_penalty)
382 setfield(x, 'penalty', 10000); set_attr(x, attr_icflag, KINSOKU)
383 insert_before(head, np.first, x); Bp[#Bp+1] = x
391 local myaw_atep1, myaw_step2, myaw_step1_last
392 local dummy = function(p,t,n) return t, false end
393 local ltjs_fast_get_stack_skip = ltjs.fast_get_stack_skip
394 function adjust_width(head)
395 if not head then return head end
397 for p in node_traverse_id(id_hlist, to_direct(head)) do
399 myaw_step2(last_p, myaw_step1(last_p, get_total_stretched(last_p)))
404 myaw_step2(last_p, myaw_step1_last(last_p, get_total_stretched(last_p)))
409 function enable_cb(status_le, status_pr)
410 if (status_le>0 or status_pr>0) and (not is_reg) then
411 luatexbase.add_to_callback('post_linebreak_filter',
412 adjust_width, 'Adjust width', 100)
414 elseif is_reg and (status_le==0 and status_pr==0) then
415 luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
419 if not luatexbase.in_callback('luatexja.adjust_jfmglue', 'luatexja.adjust') then
420 luatexbase.add_to_callback('luatexja.adjust_jfmglue', insert_lineend_kern, 'luatexja.adjust')
422 myaw_step1, myaw_step1_last = dummy, aw_step1_last
425 myaw_step1, myaw_step1_last = dummy, dummy
427 myaw_step1, myaw_step1_last = aw_step1, aw_step1_last
429 if luatexbase.in_callback('luatexja.adjust_jfmglue', 'luatexja.adjust') then
430 luatexbase.remove_from_callback('luatexja.adjust_jfmglue', 'luatexja.adjust')
433 myaw_step2 = (status_pr>0) and aw_step2 or aw_step2_dummy
435 function disable_cb() -- only for compatibility
438 luatexja.adjust = luatexja.adjust or {enable_cb=enable_cb, disable_cb=disable_cb}
441 luatexja.unary_pars.adjust = function(t)
442 return is_reg and 1 or 0