4 luatexbase.provides_module({
5 name = 'luatexja.adjust',
7 description = 'Advanced line adjustment for LuaTeX-ja',
9 module('luatexja.adjust', package.seeall)
11 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
12 luatexja.load_module('jfmglue'); local ltjj = luatexja.jfmglue
13 luatexja.load_module('stack'); local ltjs = luatexja.stack
15 local Dnode = node.direct or node
17 local nullfunc = function(n) return n end
18 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
19 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
21 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
22 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
23 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
24 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
25 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
26 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
28 local node_traverse_id = Dnode.traverse_id
29 local node_new = Dnode.new
30 local node_copy = Dnode.copy
31 local node_hpack = Dnode.hpack
32 local node_next = Dnode.getnext
33 local node_free = Dnode.free
34 local node_prev = Dnode.getprev
35 local node_tail = Dnode.tail
36 local has_attr = Dnode.has_attribute
37 local set_attr = Dnode.set_attribute
38 local insert_after = Dnode.insert_after
40 local id_glyph = node.id('glyph')
41 local id_kern = node.id('kern')
42 local id_hlist = node.id('hlist')
43 local id_glue = node.id('glue')
44 local id_glue_spec = node.id('glue_spec')
45 local id_whatsit = node.id('whatsit')
46 local attr_icflag = luatexbase.attributes['ltj@icflag']
47 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
48 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
50 local ltjf_font_metric_table = ltjf.font_metric_table
51 local spec_zero_glue = ltjj.spec_zero_glue
52 local round = tex.round
54 local PACKED = luatexja.icflag_table.PACKED
55 local FROM_JFM = luatexja.icflag_table.FROM_JFM
56 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
57 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
59 local priority_table = {
69 local PROCESSED_BEGIN_FLAG = 32
70 local function get_attr_icflag(p)
71 return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
74 -- box 内で伸縮された glue の合計値を計算
76 local function get_stretched(q, go, gs)
77 local qs = getfield(q, 'spec')
78 if not getfield(qs, 'writable') then return 0 end
79 if gs == 1 then -- stretching
80 if getfield(qs, 'stretch_order') == go then
81 return getfield(qs, 'stretch')
84 if getfield(qs, 'shrink_order') == go then
85 return getfield(qs, 'shrink')
91 local is_skip_normal = {}
92 local function get_total_stretched(p)
94 = getfield(p, 'glue_order'), getfield(p, 'glue_set'), getfield(p, 'glue_sign')
95 res[0], res.glue_set, res.name = 0, gf, (gs==1) and 'stretch' or 'shrink'
96 for i=1,#priority_table do res[priority_table[i]]=0 end
97 if go ~= 0 then return nil end
98 if gs ~= 1 and gs ~= 2 then return res end
99 for q in node_traverse_id(id_glue, getlist(p)) do
100 local a, ic = get_stretched(q, go, gs), get_attr_icflag(q)
101 if type(res[ic]) == 'number' then
102 -- kanjiskip, xkanjiskip は段落内で spec を共有しているが,
103 -- それはここでは望ましくないので,各 glue ごとに異なる spec を使う.
104 -- (この仮定でメモリリークを起こしている!)
105 -- JFM グルーはそれぞれ異なる glue_spec を用いているので,問題ない.
106 res[ic] = res[ic] + a
107 if (ic == KANJI_SKIP or ic == XKANJI_SKIP) and getsubtype(q)==0 then
108 local qs = getfield(q, 'spec')
109 if is_skip_normal[ic] and qs ~= spec_zero_glue then
110 setfield(q, 'spec', node_copy(qs))
120 local function clear_stretch(p, ic, name)
121 for q in node_traverse_id(id_glue, getlist(p)) do
122 if get_attr_icflag(q) == ic then
123 local qs = getfield(q, 'spec')
124 if getfield(qs, 'writable') then
125 setfield(qs, name..'_order', 0)
126 setfield(qs, name, 0)
132 local set_stretch_table = {}
133 local function set_stretch(p, after, before, ic, name)
135 --print (ic, before, after)
136 local ratio = after/before
137 for i,_ in pairs(set_stretch_table) do
138 set_stretch_table[i] = nil
140 for q in node_traverse_id(id_glue, getlist(p)) do
141 if get_attr_icflag(q) == ic then
142 local qs, do_flag = getfield(q, 'spec'), true
143 for i=1,#set_stretch_table do
144 if set_stretch_table[i]==qs then do_flag = false end
146 if getfield(qs, 'writable') and getfield(qs, name..'_order')==0 and do_flag then
147 setfield(qs, name, getfield(qs, name)*ratio)
148 set_stretch_table[#set_stretch_table+1] = qs
155 -- step 1: 行末に kern を挿入(句読点,中点用)
156 local function aw_step1(p, res, total)
157 local head = getlist(p)
158 local x = node_tail(head); if not x then return false end
159 x = node_prev(x); if not x then return false end
161 if getid(x) == id_glue and getsubtype(x) == 15 then
162 -- 段落最終行のときは,\penalty10000 \parfillskip が入るので,
163 -- その前の node が本来の末尾文字となる
164 x = node_prev(node_prev(x))
166 local xi, xc = getid(x)
167 if xi == id_glyph and has_attr(x, attr_curjfnt) == getfont(x) then
170 elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
173 while getid(xc) == id_whatsit do xc = node_next(xc) end
175 return false-- それ以外は対象外.
177 local xk = ltjf_font_metric_table[getfont(xc)]
178 xk = xk.char_type[has_attr(xc, attr_jchar_class) or 0]
179 xk = xk['end_' .. res.name] or 0
181 if xk>0 and total>=xk then
183 local kn = node_new(id_kern)
184 setfield(kn, 'kern', (res.name=='shrink' and -1 or 1) * xk)
185 set_attr(kn, attr_icflag, FROM_JFM)
186 insert_after(head, x, kn)
192 -- step 2: 行中の glue を変える
193 local function aw_step2(p, res, total, added_flag)
194 if total == 0 then -- もともと伸縮の必要なし
195 if added_flag then -- 行末に kern 追加したので,それによる補正
196 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
197 setfield(f, 'head', nil)
198 setfield(p, 'glue_set', getfield(f, 'glue_set'))
199 setfield(p, 'glue_order', getfield(f, 'glue_order'))
200 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
204 elseif total <= res[0] then -- 和文処理グルー以外で足りる
205 for _,v in pairs(priority_table) do clear_stretch(p, v, res.name) end
206 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
207 setfield(f, 'head', nil)
208 setfield(p, 'glue_set', getfield(f, 'glue_set'))
209 setfield(p, 'glue_order', getfield(f, 'glue_order'))
210 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
213 local orig_total, avail = total, res[0]
214 total, i = total - res[0], 1
215 while i <= #priority_table do
216 local v = priority_table[i]
217 if total <= res[v] then
218 for j = i+1,#priority_table do
219 clear_stretch(p, priority_table[j], res.name)
221 set_stretch(p, total, res[v], v, res.name)
222 avail = avail + total
223 i = #priority_table + 9 -- ループから抜けさせたいため
225 total, i, avail = total - res[v], i+1, avail + res[v]
227 if i == #priority_table + 10 or added_flag then
228 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
229 setfield(f, 'head', nil)
230 setfield(p, 'glue_set', getfield(f, 'glue_set'))
231 setfield(p, 'glue_order', getfield(f, 'glue_order'))
232 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
239 function adjust_width(head)
240 if not head then return head end
241 is_skip_normal[KANJI_SKIP] = (ltjs.fast_get_stack_skip('kanjiskip').width ~= 1073741823)
242 is_skip_normal[XKANJI_SKIP] = (ltjs.fast_get_stack_skip('xkanjiskip').width ~= 1073741823)
243 for p in node_traverse_id(id_hlist, to_direct(head)) do
244 local res = get_total_stretched(p)
248 for i,v in pairs(res) do
249 if type(i)=='number' then
252 end; total = round(total * res.glue_set)
253 local added_flag = aw_step1(p, res, total)
254 --print(total, res[0], res[KANJI_SKIP], res[FROM_JFM])
255 aw_step2(p, res, total, added_flag)
264 luatexbase.add_to_callback('post_linebreak_filter', adjust_width, 'Adjust width', 100)
268 function disable_cb()
270 luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
275 luatexja.unary_pars.adjust = function(t)
276 return is_reg and 1 or 0