4 luatexja.load_module 'base'; local ltjb = luatexja.base
5 luatexja.load_module 'jfont'; local ltjf = luatexja.jfont
6 luatexja.load_module 'jfmglue'; local ltjj = luatexja.jfmglue
7 luatexja.load_module 'stack'; local ltjs = luatexja.stack
8 luatexja.load_module 'direction'; local ltjd = luatexja.direction
9 luatexja.load_module 'lineskip'; local ltjl = luatexja.lineskip
10 luatexja.adjust = luatexja.adjust or {}
13 local combine_voiced_kana
15 local uchar = utf.char
16 local cd, cp = uchar(0x3099), uchar(0x309A)
17 local substituter = (utf.substituter or utf.subtituter) -- typo in lualibs?
19 ['ウ'..cd] = 'ヴ', ['う'..cd] = uchar(0x30F4),
20 ['か'..cd] = 'が', ['カ'..cd] = 'ガ',
21 ['き'..cd] = 'ぎ', ['キ'..cd] = 'ギ',
22 ['く'..cd] = 'ぐ', ['ク'..cd] = 'グ',
23 ['け'..cd] = 'げ', ['ケ'..cd] = 'ゲ',
24 ['こ'..cd] = 'ご', ['コ'..cd] = 'ゴ',
26 ['さ'..cd] = 'ざ', ['サ'..cd] = 'ザ',
27 ['し'..cd] = 'じ', ['シ'..cd] = 'ジ',
28 ['す'..cd] = 'ず', ['ス'..cd] = 'ズ',
29 ['せ'..cd] = 'ぜ', ['セ'..cd] = 'ゼ',
30 ['そ'..cd] = 'ぞ', ['ソ'..cd] = 'ゾ',
32 ['た'..cd] = 'だ', ['タ'..cd] = 'ダ',
33 ['ち'..cd] = 'ぢ', ['チ'..cd] = 'ヂ',
34 ['つ'..cd] = 'づ', ['ツ'..cd] = 'ヅ',
35 ['て'..cd] = 'で', ['テ'..cd] = 'デ',
36 ['と'..cd] = 'ど', ['ト'..cd] = 'ド',
38 ['は'..cd] = 'ば', ['ハ'..cd] = 'バ', ['は'..cp] = 'ぱ', ['ハ'..cp] = 'パ',
39 ['ひ'..cd] = 'び', ['ヒ'..cd] = 'ビ', ['ひ'..cp] = 'ぴ', ['ヒ'..cp] = 'ピ',
40 ['ふ'..cd] = 'ぶ', ['フ'..cd] = 'ブ', ['ふ'..cp] = 'ぷ', ['フ'..cp] = 'プ',
41 ['へ'..cd] = 'べ', ['ヘ'..cd] = 'ベ', ['へ'..cp] = 'ぺ', ['ヘ'..cp] = 'ペ',
42 ['ほ'..cd] = 'ぼ', ['ホ'..cd] = 'ボ', ['ほ'..cp] = 'ぽ', ['ホ'..cp] = 'ポ',
44 ['ゝ'..cd] = 'ゞ', ['ヽ'..cd] = 'ヾ',
45 ['ワ'..cd] = uchar(0x30F7), ['ヰ'..cd] = uchar(0x30F8),
46 ['ヱ'..cd] = uchar(0x30F9), ['ヲ'..cd] = uchar(0x30FA),
48 combine_voiced_kana = function(buffer) return substituter(buffer) end
52 local to_node = node.direct.tonode
53 local to_direct = node.direct.todirect
55 local setfield = node.direct.setfield
56 local setglue = luatexja.setglue
57 local getfield = node.direct.getfield
58 local getlist = node.direct.getlist
59 local getid = node.direct.getid
60 local getfont = node.direct.getfont
61 local getsubtype = node.direct.getsubtype
63 local node_traverse_id = node.direct.traverse_id
64 local node_new = node.direct.new
65 local node_next = node.direct.getnext
66 local node_free = node.direct.free
67 local node_prev = node.direct.getprev
68 local node_tail = node.direct.tail
69 local has_attr = node.direct.has_attribute
70 local set_attr = node.direct.set_attribute
71 local insert_after = node.direct.insert_after
73 local id_glyph = node.id 'glyph'
74 local id_kern = node.id 'kern'
75 local id_hlist = node.id 'hlist'
76 local id_glue = node.id 'glue'
77 local id_whatsit = node.id 'whatsit'
78 local id_penalty = node.id 'penalty'
79 local attr_icflag = luatexbase.attributes['ltj@icflag']
80 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
81 local lang_ja = luatexja.lang_ja
83 local ltjf_font_metric_table = ltjf.font_metric_table
84 local ipairs, pairs = ipairs, pairs
86 local PACKED = luatexja.icflag_table.PACKED
87 local LINEEND = luatexja.icflag_table.LINEEND
88 local FROM_JFM = luatexja.icflag_table.FROM_JFM
89 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
90 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
91 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
92 local XKANJI_SKIP_JFM = luatexja.icflag_table.XKANJI_SKIP_JFM
96 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
97 get_attr_icflag = function(p)
98 return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
102 local priority_num = { 0, 0 }
103 local at2pr = { {}, {} }
104 local at2pr_st, at2pr_sh = at2pr[1], at2pr[2]
106 local priority_table = {{},{}}
107 luatexja.adjust.priority_table = priority_table
109 local function cmp(a,b) return a[1]>b[1] end -- 大きいほうが先!
110 local function make_priority_table(glue_sign)
111 for i,_ in pairs(tmp) do tmp[i]=nil end
112 if glue_sign==2 then -- shrink
113 for i=0,63 do tmp[#tmp+1] = { (i%8)-4, FROM_JFM+i } end
115 for i=0,63 do tmp[#tmp+1] = { math.floor(i/8)-4, FROM_JFM+i } end
117 local pt = priority_table[glue_sign]
118 tmp[#tmp+1] = { pt[2]/10, XKANJI_SKIP }
119 tmp[#tmp+1] = { pt[2]/10, XKANJI_SKIP_JFM }
120 tmp[#tmp+1] = { pt[1]/10, KANJI_SKIP }
121 tmp[#tmp+1] = { pt[1]/10, KANJI_SKIP_JFM }
122 tmp[#tmp+1] = { pt[3]/10, -1 }
124 local a, m, n = at2pr[glue_sign], 10000000, 0
126 if tmp[i][1]<m then n,m = n+1,tmp[i][1] end
130 priority_num[glue_sign] = n
131 setmetatable(a, {__index = function () return o end })
133 luatexja.adjust.make_priority_table = make_priority_table
136 -- box 内で伸縮された glue の合計値を計算
138 local total_stsh = {{},{}}
139 local total_st, total_sh = total_stsh[1], total_stsh[2]
140 local get_total_stretched
142 local dimensions = node.direct.dimensions
143 function get_total_stretched(p)
144 -- return value: <補正値(sp)>
145 local ph = getlist(p)
146 if not ph then return 0 end
147 for i,_ in pairs(total_st) do total_st[i]=nil; total_sh[i]=nil end
148 for i=1,priority_num[1] do total_st[i]=0 end
149 for i=1,priority_num[2] do total_sh[i]=0 end
150 for i=0,4 do total_st[i*65536]=0; total_sh[i*65536]=0 end
151 for q in node_traverse_id(id_glue, ph) do
152 local a = getfield(q, 'stretch_order')
154 local b = at2pr_st[get_attr_icflag(q)];
155 total_st[b] = total_st[b]+getfield(q, 'stretch')
157 total_st[a*65536] = total_st[a]+getfield(q, 'stretch')
158 local a = getfield(q, 'shrink_order')
160 local b = at2pr_sh[get_attr_icflag(q)];
161 total_sh[b] = total_sh[b]+getfield(q, 'shrink')
163 total_sh[a*65536] = total_sh[a]+getfield(q, 'shrink')
165 for i=4,1,-1 do if total_st[i*65536]~=0 then total_st.order=i; break end; end
166 if not total_st.order then
167 total_st.order, total_st[-65536] = -1,0.1 -- dummy
169 for i=4,1,-1 do if total_sh[i*65536]~=0 then total_sh.order=i; break end; end
170 if not total_sh.order then
171 total_sh.order, total_sh[-65536] = -1,0.1 -- dummy
173 return getfield(p,'width') - dimensions(ph)
177 -- step 1: 行末に kern を挿入(句読点,中点用)
179 local ltjd_glyph_from_packed = ltjd.glyph_from_packed
180 local function aw_step1(p, total)
181 local head = getlist(p)
182 local x = node_tail(head); if not x then return total, false end
184 x = node_prev(x); if not x then return total, false end
185 local xi, xc = getid(x)
187 while xi==id_penalty do
188 x = node_prev(x); if not x then return total, false end
191 if (total>0 and total_st.order>0) or (total<0 and total_sh.order>0) then
192 -- 無限大のグルーで処理が行われているときは処理中止.
195 if xi == id_glyph and getfield(x, 'lang')==lang_ja then
198 elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
200 xc = ltjd_glyph_from_packed(x)
201 while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
203 return total, false-- それ以外は対象外.
205 local eadt = ltjf_font_metric_table[getfont(xc)]
206 .char_type[has_attr(xc, attr_jchar_class) or 0].end_adjust
210 local eadt_ratio = {}
211 for i, v in ipairs(eadt) do
214 eadt_ratio[i] = {i, t/total_st[65536*total_st.order], t, v}
216 eadt_ratio[i] = {i, t/total_sh[65536*total_sh.order], t, v}
219 table.sort(eadt_ratio,
222 local at, bt = abs(a[i]), abs(b[i])
223 if at~=bt then return at<bt end
227 if eadt[eadt_ratio[1][1]]~=0 then
228 local kn = node_new(id_kern, 1)
229 setfield(kn, 'kern', eadt[eadt_ratio[1][1]]); set_attr(kn, attr_icflag, LINEEND)
230 insert_after(head, x, kn)
231 return eadt_ratio[1][3], true
238 local min, max = math.min, math.max
239 local function aw_step1_last(p, total)
240 local head = getlist(p)
241 local x = node_tail(head); if not x then return total, false end
243 local pf = node_prev(x); if not x then return total, false end
244 if getid(pf) ~= id_glue or getsubtype(pf) ~= 15 then return total, false end
245 x = node_prev(node_prev(pf))
246 local xi, xc = getid(x)
247 if xi == id_glyph and getfield(x, 'lang')==lang_ja then
250 elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
252 xc = ltjd_glyph_from_packed(x)
253 while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
255 return total, false-- それ以外は対象外.
257 -- 続行条件1:無限の伸縮度を持つグルーは \parfillskipのみ
258 if total>0 and total_st.order>0 then
259 if total_st.order ~= getfield(pf, 'stretch_order') then return total, false end
260 if total_st[total_st.order*65536] ~= getfield(pf, 'stretch') then return total, false end
261 for i=total_st.order-1, 1, -1 do
262 if total_st[i*65536] ~= 0 then return total, false end
265 if total<0 and total_sh.order>0 then
266 if total_sh.order ~= getfield(pf, 'shrink_order') then return total, false end
267 if total_sh[total_sh.order*65536] ~= getfield(pf, 'shrink') then return total, false end
268 for i=total_sh.order-1, 1, -1 do
269 if total_sh[i*65536] ~= 0 then return total, false end
272 local eadt = ltjf_font_metric_table[getfont(xc)]
273 .char_type[has_attr(xc, attr_jchar_class) or 0].end_adjust
277 -- 続行条件2: min(eadt[1], 0)<= \parfillskip <= max(eadt[#eadt], 0)
278 local pfw = getfield(pf, 'width')
279 + (total>0 and getfield(pf, 'stretch') or -getfield(pf, 'shrink')) *getfield(p, 'glue_set')
280 if pfw<min(0,eadt[1]) or max(0,eadt[#eadt])<pfw then return total, false end
281 -- \parfillskip を 0 にする
282 total = total + getfield(pf, 'width')
283 total_st.order, total_sh.order = 0, 0
284 if getfield(pf, 'stretch_order')==0 then
285 local i = at2pr_st[-1]
286 total_st[0] = total_st[0] - getfield(pf, 'stretch')
287 total_st[i] = total_st[i] - getfield(pf, 'stretch')
288 total_st.order = (total_st[0]==0) and -1 or 0
290 if getfield(pf, 'shrink_order')==0 then
291 local i = at2pr_sh[-1]
292 total_sh[0] = total_sh[0] - getfield(pf, 'shrink')
293 total_sh[i] = total_sh[i] - getfield(pf, 'shrink')
294 total_sh.order = (total_sh[0]==0) and -1 or 0
296 setfield(pf, 'subtype', 1); setglue(pf)
297 local eadt_ratio = {}
298 for i, v in ipairs(eadt) do
301 eadt_ratio[i] = {i, t/total_st[65536*total_st.order], t, v}
303 eadt_ratio[i] = {i, t/total_sh[65536*total_sh.order], t, v}
306 table.sort(eadt_ratio,
309 local at, bt = abs(a[i]), abs(b[i])
310 if at~=bt then return at<bt end
314 if eadt[eadt_ratio[1][1]]~=0 then
315 local kn = node_new(id_kern, 1)
316 setfield(kn, 'kern', eadt[eadt_ratio[1][1]]); set_attr(kn, attr_icflag, LINEEND)
317 insert_after(head, x, kn)
318 return eadt_ratio[1][3], true
325 -- step 2: 行中の glue を変える
326 local aw_step2, aw_step2_dummy
328 local node_hpack = node.direct.hpack
329 local function repack(p)
330 local orig_of, orig_hfuzz, orig_hbad = tex.overfullrule, tex.hfuzz, tex.hbadness
331 tex.overfullrule=0; tex.hfuzz=1073741823; tex.hbadness=10000
332 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
333 tex.overfullrule=orig_of; tex.hfuzz=orig_hfuzz; tex.hbadness=orig_hbad
334 setfield(f, 'head', nil)
335 setfield(p, 'glue_set', getfield(f, 'glue_set'))
336 setfield(p, 'glue_order', getfield(f, 'glue_order'))
337 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
341 function aw_step2_dummy(p, _, added_flag)
342 if added_flag then return repack(p) end
345 local function clear_stretch(p, ind, ap, name)
346 for q in node_traverse_id(id_glue, getlist(p)) do
347 local f = ap[get_attr_icflag(q)]
349 setfield(q, name..'_order', 0); setfield(q, name, 0)
354 local function set_stretch(p, after, before, ind, ap, name)
356 local ratio = after/before
357 for q in node_traverse_id(id_glue, getlist(p)) do
358 local f = ap[get_attr_icflag(q)]
359 if (f==ind) and getfield(q, name..'_order')==0 then
360 setfield(q, name, getfield(q, name)*ratio)
366 function aw_step2(p, total, added_flag)
367 local name = (total>0) and 'stretch' or 'shrink'
368 local id = (total>0) and 1 or 2
369 local res = total_stsh[id]
370 local pnum = priority_num[id]
371 if total==0 or res.order > 0 then
372 -- もともと伸縮の必要なしか,残りの伸縮量は無限大
373 if added_flag then return repack(p) end
377 if total <= res[i] then
380 clear_stretch(p, j, a, name)
382 set_stretch(p, total, res[i], i, a, name); break
384 total = total - res[i]
390 -- step 1': lineend=extended の場合(行分割時に考慮))
391 local insert_lineend_kern
393 local insert_before = node.direct.insert_before
394 local KINSOKU = luatexja.icflag_table.KINSOKU
395 insert_lineend_kern = function (head, nq, np, Bp)
397 local eadt = nq.met.char_type[nq.class].end_adjust
398 if not eadt then return end
400 local x = node_new(id_kern, 1)
401 setfield(x, 'kern', eadt[1]); set_attr(x, attr_icflag, LINEEND)
402 insert_before(head, np.first, x)
404 local eadt_num = #eadt
406 local x = node_new(id_penalty)
407 setfield(x, 'penalty', 0); set_attr(x, attr_icflag, KINSOKU)
408 insert_before(head, np.first, x); Bp[#Bp+1] = x
409 local x = node_new(id_kern, 1)
410 setfield(x, 'kern', eadt[i]-eadt[i-1]); set_attr(x, attr_icflag, LINEEND)
411 insert_before(head, np.first, x)
413 if eadt_num>1 or eadt[1]~=0 then
414 local x = node_new(id_penalty)
415 setfield(x, 'penalty', 0); set_attr(x, attr_icflag, KINSOKU)
416 insert_before(head, np.first, x); Bp[#Bp+1] = x
417 local x = node_new(id_kern, 1)
418 setfield(x, 'kern', -eadt[eadt_num]); set_attr(x, attr_icflag, LINEEND)
419 insert_before(head, np.first, x)
420 local x = node_new(id_penalty)
421 setfield(x, 'penalty', 10000); set_attr(x, attr_icflag, KINSOKU)
422 insert_before(head, np.first, x); Bp[#Bp+1] = x
430 local myaw_step1, myaw_step2, myaw_step1_last
431 local dummy = function(p,t,n) return t, false end
432 local ltjs_fast_get_stack_skip = ltjs.fast_get_stack_skip
433 function adjust_width(head)
434 if not head then return head end
436 for p in node_traverse_id(id_hlist, to_direct(head)) do
438 myaw_step2(last_p, myaw_step1(last_p, get_total_stretched(last_p)))
443 myaw_step2(last_p, myaw_step1_last(last_p, get_total_stretched(last_p)))
448 local function enable_cb(status_le, status_pr, status_lp, status_ls, status_cd)
449 if (status_le>0 or status_pr>0) and (not is_reg) then
450 ltjb.add_to_callback('post_linebreak_filter',
451 adjust_width, 'Adjust width',
452 luatexbase.priority_in_callback('post_linebreak_filter', 'ltj.lineskip')-1)
454 elseif is_reg and (status_le==0 and status_pr==0) then
455 luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
459 if not luatexbase.in_callback('luatexja.adjust_jfmglue', 'luatexja.adjust') then
460 ltjb.add_to_callback('luatexja.adjust_jfmglue', insert_lineend_kern, 'luatexja.adjust')
462 myaw_step1, myaw_step1_last = dummy, aw_step1_last
465 myaw_step1, myaw_step1_last = dummy, dummy
467 myaw_step1, myaw_step1_last = aw_step1, aw_step1_last
469 if luatexbase.in_callback('luatexja.adjust_jfmglue', 'luatexja.adjust') then
470 luatexbase.remove_from_callback('luatexja.adjust_jfmglue', 'luatexja.adjust')
473 myaw_step2 = (status_pr>0) and aw_step2 or aw_step2_dummy
474 luatexja.lineskip.setting(
475 status_lp>0 and 'profile' or 'dummy',
476 status_ls>0 and 'step' or 'dummy'
479 if not luatexbase.in_callback('process_input_buffer', 'Combine voiced kana-syllables') then
480 ltjb.add_to_callback('process_input_buffer', combine_voiced_kana, 'Combine voiced kana-syllables')
483 if luatexbase.in_callback('process_input_buffer', 'Combine voiced kana-syllables') then
484 luatexbase.remove_from_callback('process_input_buffer', 'Combine voiced kana-syllables')
488 local function disable_cb() -- only for compatibility
491 luatexja.adjust.enable_cb=enable_cb
492 luatexja.adjust.disable_cb=disable_cb
495 luatexja.unary_pars.adjust = function(t)
496 return is_reg and 1 or 0
499 -- ----------------------------------
502 local max, ins, sort = math.max, table.insert, table.sort
503 local function insert(package, ind, d, b, e)
504 local bound = package[2]
505 bound[b], bound[e]=true, true
506 ins(package[1], {b,e,[ind]=d})
508 local function flatten(package)
509 local bd = {} for i,_ in pairs(package[2]) do ins(bd,{i}) end
510 sort(bd, function (a,b) return a[1]<b[1] end)
511 local bdc=#bd; local t = package[1]
512 sort(t, function (a,b) return a[1]<b[1] end)
515 while bd[bdi][1]<t[i][1] do bdi=bdi+1 end
517 while j<bdc and bd[j+1][1]<=t[i][2] do
518 for k,w in pairs(t[i]) do
520 bd[j][k]=bd[j][k] and max(bd[j][k],w) or w
526 package[2]=nil; package[1]=nil; package.flatten, package.insert=nil, nil
530 init_range = function ()
531 return {{},{}, insert=insert, flatten=flatten}
535 -- -----------------------------------
536 luatexja.adjust.step_factor = 0.5
537 luatexja.unary_pars.linestep_factor = function(t)
538 return luatexja.adjust.step_factor
540 luatexja.adjust.profile_hgap_factor = 1
541 luatexja.unary_pars.profile_hgap_factor = function(t)
542 return luatexja.adjust.profile_hgap_factor
545 local insert = table.insert
546 local rangedimensions, max = node.direct.rangedimensions, math.max
547 local function profile_inner(box, range, ind, vmirrored, adj)
548 local w_acc, d_before = getfield(box,'shift'), 0
549 local x = getlist(box); local xn = node_next(x)
552 if xn then w, h, d= rangedimensions(box,x,xn)
553 else w, h, d= rangedimensions(box,x) end
554 if vmirrored then h=d end
555 local w_new = w_acc + w
557 range:insert(ind, h, w_acc-adj, w_new)
559 range:insert(ind, h, w_new-adj, w_acc)
561 w_acc = w_new; x = xn; if x then xn = node_next(x) end
564 function ltjl.p_profile(before, after, mirrored, bw)
566 = init_range(), luatexja.adjust.profile_hgap_factor*tex.lineskip.width
567 profile_inner(before, range, 3, true, tls)
568 profile_inner(after, range, 4, mirrored, tls)
569 range = range:flatten()
571 local dmax, d, hmax, h, lmin = 0, 0, 0, 0, 1/0
572 for i,v in ipairs(range) do
573 d, h = (v[3] or 0), (v[4] or 0)
574 if d>dmax then dmax=d end
575 if h>hmax then hmax=h end
576 if bw-h-d<lmin then lmin=bw-h-d end
578 if lmin==1/0 then lmin = bw end
580 bw - lmin - getfield(before, 'depth')
581 - getfield(after, mirrored and 'depth' or 'height')
587 local ltja = luatexja.adjust
588 local copy_glue = ltjl.copy_glue
589 local floor, max = math.floor, math.max
590 function ltjl.l_step(dist, g, adj, normal, bw, loc)
591 if loc=='alignment' then
592 return ltjl.l_dummy(dist, g, adj, normal, bw, loc)
594 if dist < tex.lineskiplimit then
595 local f = max(1, bw*ltja.step_factor)
596 copy_glue(g, tex.baselineskip, 1, normal - f * floor((dist-tex.lineskip.width)/f))
598 copy_glue(g, tex.baselineskip, 2, normal)