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 attr_icflag = luatexbase.attributes['ltj@icflag']
40 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
41 local lang_ja = luatexja.lang_ja
43 local ltjf_font_metric_table = ltjf.font_metric_table
44 local round, pairs = tex.round, pairs
46 local PACKED = luatexja.icflag_table.PACKED
47 local LINEEND = luatexja.icflag_table.LINEEND
48 local FROM_JFM = luatexja.icflag_table.FROM_JFM
49 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
50 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
51 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
52 local XKANJI_SKIP_JFM = luatexja.icflag_table.XKANJI_SKIP_JFM
54 local priority_table = {
66 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
67 get_attr_icflag = function(p)
68 return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
72 -- box 内で伸縮された glue の合計値を計算
74 local total_stsh = {{},{}}
75 local total_st, total_sh = total_stsh[1], total_stsh[2]
76 local function get_total_stretched(p)
77 -- return value: <補正値(sp)>
79 = getfield(p, 'glue_order'), getfield(p, 'glue_set'), getfield(p, 'glue_sign')
80 for i,_ in pairs(total_st) do total_st[i]=nil; total_sh[i]=nil end
81 for i=1,#priority_table do
82 total_st[priority_table[i]]=0; total_sh[priority_table[i]]=0;
84 for i=0,4 do total_st[i*65536]=0; total_sh[i*65536]=0 end
85 total_st[-1]=0; total_sh[-1]=0;
87 for q in node_traverse_id(id_glue, getlist(p)) do
88 local a = getfield(q, 'stretch_order')
89 if a>0 then a=a*65536 else
90 total_st[0] = total_st[0]+getfield(q, 'stretch')
91 a = get_attr_icflag(q)
92 if a == KANJI_SKIP_JFM then a = KANJI_SKIP
93 elseif a == XKANJI_SKIP_JFM then a = XKANJI_SKIP
94 elseif type(total_st[a])~='number' then a = -1 end
96 total_st[a] = total_st[a]+getfield(q, 'stretch')
97 local a = getfield(q, 'shrink_order')
98 if a>0 then a=a*65536 else
99 total_sh[0] = total_sh[0]+getfield(q, 'shrink')
100 a = get_attr_icflag(q)
101 if a == KANJI_SKIP_JFM then a = KANJI_SKIP
102 elseif a == XKANJI_SKIP_JFM then a = XKANJI_SKIP
103 elseif type(total_sh[a])~='number' then a = -1 end
105 total_sh[a] = total_sh[a]+getfield(q, 'shrink')
107 for i=4,1,-1 do if total_st[i*65536]~=0 then total_st.order=i; break end; end
108 if not total_st.order then
109 total_st.order, total_st[-65536] = -1,0.1 -- dummy
111 for i=4,1,-1 do if total_sh[i*65536]~=0 then total_sh.order=i; break end; end
112 if not total_sh.order then
113 total_sh.order, total_sh[-65536] = -1,0.1 -- dummy
118 return round((3-2*gs)*total_stsh[gs][go*65536]*gf)
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)
157 if (total>0 and total_st.order>0) or (total<0 and total_sh.order>0) then
158 -- 無限大のグルーで処理が行われているときは処理中止.
161 if xi == id_glyph and getfield(x, 'lang')==lang_ja then
164 elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
166 xc = ltjd_glyph_from_packed(x)
167 while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
169 return total, false-- それ以外は対象外.
171 local eadt = ltjf_font_metric_table[getfont(xc)]
172 .char_type[has_attr(xc, attr_jchar_class) or 0].end_adjust
176 local eadt_ratio = {}
177 for i, v in ipairs(eadt) do
180 eadt_ratio[i] = {i, t/total_st[65536*total_st.order], t, v}
182 eadt_ratio[i] = {i, t/total_sh[65536*total_sh.order], t, v}
185 table.sort(eadt_ratio,
188 local at, bt = abs(a[i]), abs(b[i])
189 if at~=bt then return at<bt end
193 if eadt[eadt_ratio[1][1]]~=0 then
194 local kn = node_new(id_kern, 1)
195 setfield(kn, 'kern', eadt[eadt_ratio[1][1]]); set_attr(kn, attr_icflag, LINEEND)
196 insert_after(head, x, kn)
197 return eadt_ratio[1][3], true
204 local function aw_step1_last(p, total)
205 local head = getlist(p)
206 local x = node_tail(head); if not x then return total, false end
208 pf = node_prev(x); if not x then return total, false end
209 if getid(pf) ~= id_glue or getsubtype(pf) ~= 15 then return total, false end
210 x = node_prev(node_prev(pf)); xi = getid(x)
211 local xi, xc = getid(x)
212 if xi == id_glyph and getfield(x, 'lang')==lang_ja then
215 elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
217 xc = ltjd_glyph_from_packed(x)
218 while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
220 return total, false-- それ以外は対象外.
222 -- 続行条件1:無限の伸縮度を持つグルーは \parfillskipのみ
223 if total>0 and total_st.order>0 then
224 if total_st.order ~= getfield(pf, 'stretch_order') then return total, false end
225 if total_st[total_st.order*65536] ~= getfield(pf, 'stretch') then return total, false end
226 for i=total_st.order-1, 1, -1 do
227 if total_st[i*65536] ~= 0 then return total, false end
230 if total<0 and total_sh.order>0 then
231 if total_sh.order ~= getfield(pf, 'shrink_order') then return total, false end
232 if total_sh[total_sh.order*65536] ~= getfield(pf, 'shrink') then return total, false end
233 for i=total_sh.order-1, 1, -1 do
234 if total_sh[i*65536] ~= 0 then return total, false end
237 local eadt = ltjf_font_metric_table[getfont(xc)]
238 .char_type[has_attr(xc, attr_jchar_class) or 0].end_adjust
242 -- 続行条件2: eadt[1]<= \parfillskip <= eadt[#eadt]
243 local pfw = getfield(pf, 'width')
244 + (total>0 and getfield(pf, 'stretch') or -getfield(pf, 'shrink')) *getfield(p, 'glue_set')
245 if pfw<eadt[1] or eadt[#eadt]<pfw then return total, false end
246 -- \parfillskip を 0 にする
247 total = total + getfield(pf, 'width')
248 total_st.order, total_sh.order = 0, 0
249 if getfield(pf, 'stretch_order')==0 then
250 total_st[0] = total_st[0] - getfield(pf, 'stretch')
251 total_st[-1] = total_st[-1] - getfield(pf, 'stretch')
252 total_st.order = (total_st[0]==0) and -1 or 0
254 if getfield(pf, 'shrink_order')==0 then
255 total_sh[0] = total_sh[0] - getfield(pf, 'shrink')
256 total_sh[-1] = total_sh[-1] - getfield(pf, 'shrink')
257 total_sh.order = (total_sh[0]==0) and -1 or 0
259 setfield(pf, 'subtype', 1); setglue(pf)
260 local eadt_ratio = {}
261 for i, v in ipairs(eadt) do
264 eadt_ratio[i] = {i, t/total_st[65536*total_st.order], t, v}
266 eadt_ratio[i] = {i, t/total_sh[65536*total_sh.order], t, v}
269 table.sort(eadt_ratio,
272 local at, bt = abs(a[i]), abs(b[i])
273 if at~=bt then return at<bt end
277 if eadt[eadt_ratio[1][1]]~=0 then
278 local kn = node_new(id_kern, 1)
279 setfield(kn, 'kern', eadt[eadt_ratio[1][1]]); set_attr(kn, attr_icflag, LINEEND)
280 insert_after(head, x, kn)
281 return eadt_ratio[1][3], true
288 -- step 2: 行中の glue を変える
289 local function aw_step2_dummy(p, _, added_flag)
290 if added_flag then -- 行末に kern 追加したので,それによる補正
291 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
292 setfield(f, 'head', nil)
293 setfield(p, 'glue_set', getfield(f, 'glue_set'))
294 setfield(p, 'glue_order', getfield(f, 'glue_order'))
295 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
300 local function aw_step2(p, total, added_flag)
301 local name = (total>0) and 'stretch' or 'shrink'
302 local res = total_stsh[(total>0) and 1 or 2]
303 if total==0 or res.order > 0 then
304 -- もともと伸縮の必要なしか,残りの伸縮量は無限大
305 if added_flag then -- 行末に kern 追加したので,それによる補正
306 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
307 setfield(f, 'head', nil)
308 setfield(p, 'glue_set', getfield(f, 'glue_set'))
309 setfield(p, 'glue_order', getfield(f, 'glue_order'))
310 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
315 total = math.abs(total)
316 if total <= res[-1] then -- 和文処理グルー以外で足りる
317 for _,v in pairs(priority_table) do clear_stretch(p, v, name) end
318 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
319 setfield(f, 'head', nil)
320 setfield(p, 'glue_set', getfield(f, 'glue_set'))
321 setfield(p, 'glue_order', getfield(f, 'glue_order'))
322 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
325 total = total - res[-1];
326 for i = 1, #priority_table do
327 local v = priority_table[i]
328 if total <= res[v] then
329 for j = i+1,#priority_table do
330 clear_stretch(p, priority_table[j], name)
332 set_stretch(p, total, res[v], v, name); break
334 total = total - res[v]
336 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
337 setfield(f, 'head', nil)
338 setfield(p, 'glue_set', getfield(f, 'glue_set'))
339 setfield(p, 'glue_order', getfield(f, 'glue_order'))
340 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
346 local insert_lineend_kern
348 local id_penalty = node.id('penalty')
349 local insert_before = node.direct.insert_before
350 local KINSOKU = luatexja.icflag_table.KINSOKU
351 function insert_lineend_kern(head, nq, np, Bp)
353 local eadt = nq.met.char_type[nq.class].end_adjust
354 if not eadt then return end
356 local x = node_new(id_kern, 1)
357 setfield(x, 'kern', eadt[1]); set_attr(x, attr_icflag, LINEEND)
358 insert_before(head, np.first, x)
361 local x = node_new(id_penalty)
362 setfield(x, 'penalty', 0); set_attr(x, attr_icflag, KINSOKU)
363 insert_before(head, np.first, x); Bp[#Bp+1] = x
364 local x = node_new(id_kern, 1)
365 setfield(x, 'kern', eadt[i]-eadt[i-1]); set_attr(x, attr_icflag, LINEEND)
366 insert_before(head, np.first, x)
368 if #eadt>1 or eadt[1]~=0 then
369 local x = node_new(id_penalty)
370 setfield(x, 'penalty', 0); set_attr(x, attr_icflag, KINSOKU)
371 insert_before(head, np.first, x); Bp[#Bp+1] = x
372 local x = node_new(id_kern, 1)
373 setfield(x, 'kern', -eadt[#eadt]); set_attr(x, attr_icflag, LINEEND)
374 insert_before(head, np.first, x)
375 local x = node_new(id_penalty)
376 setfield(x, 'penalty', 10000); set_attr(x, attr_icflag, KINSOKU)
377 insert_before(head, np.first, x); Bp[#Bp+1] = x
385 local myaw_atep1, myaw_step2, myaw_step1_last
386 local dummy = function(p,t,n) return t, false end
387 local ltjs_fast_get_stack_skip = ltjs.fast_get_stack_skip
388 function adjust_width(head)
389 if not head then return head end
391 for p in node_traverse_id(id_hlist, to_direct(head)) do
393 myaw_step2(last_p, myaw_step1(last_p, get_total_stretched(last_p)))
398 myaw_step2(last_p, myaw_step1_last(last_p, get_total_stretched(last_p)))
403 function enable_cb(status_le, status_pr)
404 if (status_le>0 or status_pr>0) and (not is_reg) then
405 luatexbase.add_to_callback('post_linebreak_filter',
406 adjust_width, 'Adjust width', 100)
408 elseif is_reg and (status_le==0 and status_pr==0) then
409 luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
412 myaw_step1 = dummy --(status%2>0) and aw_step1 or dummy
414 if not luatexbase.in_callback('luatexja.adjust_jfmglue', 'luatexja.adjust') then
415 luatexbase.add_to_callback('luatexja.adjust_jfmglue', insert_lineend_kern, 'luatexja.adjust')
417 myaw_step1, myaw_step1_last = dummy, aw_step1_last
420 myaw_step1, myaw_step1_last = dummy, dummy
422 myaw_step1, myaw_step1_last = aw_step1, aw_step1_last
424 if luatexbase.in_callback('luatexja.adjust_jfmglue', 'luatexja.adjust') then
425 luatexbase.remove_from_callback('luatexja.adjust_jfmglue', 'luatexja.adjust')
428 myaw_step2 = (status_pr>0) and aw_step2 or aw_step2_dummy
430 function disable_cb() -- only for compatibility
433 luatexja.adjust = luatexja.adjust or {enable_cb=enable_cb, disable_cb=disable_cb}
436 luatexja.unary_pars.adjust = function(t)
437 return is_reg and 1 or 0