4 luatexbase.provides_module({
5 name = 'luatexja.adjust',
8 description = 'Advanced line adjustment for LuaTeX-ja',
10 module('luatexja.adjust', package.seeall)
12 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
13 luatexja.load_module('jfmglue'); local ltjj = luatexja.jfmglue
15 local id_glyph = node.id('glyph')
16 local id_kern = node.id('kern')
17 local id_hlist = node.id('hlist')
18 local id_glue = node.id('glue')
19 local id_glue_spec = node.id('glue_spec')
20 local has_attr = node.has_attribute
21 local set_attr = node.set_attribute
22 local attr_icflag = luatexbase.attributes['ltj@icflag']
23 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
24 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
25 local node_copy = node.copy
26 local node_next = node.next
27 local node_free = node.free
29 local ltjf_font_metric_table = ltjf.font_metric_table
30 local spec_zero_glue = ltjj.spec_zero_glue
35 local XKANJI_SKIP = 10
37 local priority_table = {
47 local PROCESSED_BEGIN_FLAG = 32
48 local function get_attr_icflag(p)
49 return (node.has_attribute(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
52 -- box 内で伸縮された glue の合計値を計算
54 local function get_stretched(q, go, gs)
56 if not qs.writable then return 0 end
57 if gs == 1 then -- stretching
58 if qs.stretch_order == go then return qs.stretch end
60 if qs.shrink_order == go then return qs.shrink end
65 local function get_total_stretched(p)
66 local go, gf, gs = p.glue_order, p.glue_set, p.glue_sign
69 glue_set = gf, name = (gs==1) and 'stretch' or 'shrink'
71 for i=1,#priority_table do res[priority_table[i]]=0 end
72 if go ~= 0 then return nil end
73 if gs ~= 1 and gs ~= 2 then return res end
76 --luatexja.ext_show_node_list(p.head, '>>> ', print)
79 local a, ic = get_stretched(q, go, gs), get_attr_icflag(q)
80 if type(res[ic]) == 'number' then
81 -- kanjiskip, xkanjiskip は段落内で spec を共有しているが,
83 -- 各行ごとに異なる spec を使うようにする.
84 -- しかしここでは面倒なので,各 glue ごとに別の spec を使っている.
86 -- JFM グルーはそれぞれ異なる glue_spec を用いているので,問題ない.
88 if ic == KANJI_SKIP then
89 if q.spec ~= spec_zero_glue then
91 local ts; q.spec, ts = node_copy(q.spec), q.spec
92 new_ks, q.spec = node.copy(q), ts
94 local g = node.copy(new_ks)
95 node.insert_before(head, q, g);
96 head = node.remove(head, q); node.free(q); q = g
98 elseif ic == XKANJI_SKIP then
99 if q.spec ~= spec_zero_glue then
101 local ts; q.spec, ts = node_copy(q.spec), q.spec
102 new_xs, q.spec = node.copy(q), ts
104 local g =node.copy(new_xs)
105 node.insert_before(head, q, g);
106 head = node.remove(head, q); node.free(q); q = g
118 local function clear_stretch(p, ic, name)
119 --print('clear ' .. ic)
120 for q in node.traverse_id(id_glue, p.head) do
121 if get_attr_icflag(q) == ic then
124 qs[name..'_order'], qs[name] = 0, 0
130 local set_stretch_table = {}
131 local function set_stretch(p, after, before, ic, name)
133 --print (ic, before, after)
134 local ratio = after/before
135 for i,_ in pairs(set_stretch_table) do
136 set_stretch_table[i] = nil
138 for q in node.traverse_id(id_glue, p.head) do
139 if get_attr_icflag(q) == ic then
140 local qs, do_flag = q.spec, true
141 for i=1,#set_stretch_table do
142 if set_stretch_table[i]==qs then do_flag = false end
144 if qs.writable and qs[name..'_order'] == 0 and do_flag then
145 qs[name] = qs[name]*ratio;
146 set_stretch_table[#set_stretch_table+1] = qs
153 -- step 1: 行末に kern を挿入(句読点,中点用)
154 local function aw_step1(p, res, total)
155 local x = node.tail(p.head); if not x then return false end
156 local x = node.prev(x) ; if not x then return false end
158 if x.id == id_glue and x.subtype == 15 then
159 -- 段落最終行のときは,\penalty10000 \parfillskip が入るので,
160 -- その前の node が本来の末尾文字となる
161 x = node.prev(node.prev(x))
165 if xi == id_glyph and has_attr(x, attr_curjfnt) == x.font then
168 elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
172 return false-- それ以外は対象外.
174 local xk = ltjf_font_metric_table --
175 [xc.font].size_cache.char_type[has_attr(xc, attr_jchar_class) or 0]
176 ['end_' .. res.name] or 0
177 --print(res.name, total, xk, unicode.utf8.char(xc.char))
179 if xk>0 and total>=xk then
182 local kn = node.new(id_kern)
183 kn.kern = (res.name=='shrink' and -1 or 1) * xk
184 set_attr(kn, attr_icflag, FROM_JFM)
185 node.insert_after(p.head, x, kn)
191 -- step 2: 行中の glue を変える
192 local function aw_step2(p, res, total, added_flag)
193 if total == 0 then -- もともと伸縮の必要なし
194 if added_flag then -- 行末に kern 追加したので,それによる補正
195 local f = node.hpack(p.head, p.width, 'exactly')
196 f.head, p.glue_set, p.glue_sign, p.glue_order
197 = nil, f.glue_set, f.glue_sign, f.glue_order
200 elseif total <= res[0] then -- 和文処理グルー以外で足りる
201 for _,v in pairs(priority_table) do clear_stretch(p, v, res.name) end
202 local f = node.hpack(p.head, p.width, 'exactly')
203 f.head, p.glue_set, p.glue_sign, p.glue_order
204 = nil, f.glue_set, f.glue_sign, f.glue_order
207 total, i = total - res[0], 1
208 while i <= #priority_table do
209 local v = priority_table[i]
210 if total <= res[v] then
211 for j = i+1,#priority_table do
212 clear_stretch(p, priority_table[j], res.name)
214 set_stretch(p, total, res[v], v, res.name)
215 i = #priority_table + 9 -- ループから抜けさせたいため
217 total, i= total - res[v], i+1
219 if i == #priority_table + 10 or added_flag then
220 local f = node.hpack(p.head, p.width, 'exactly')
221 f.head, p.glue_set, p.glue_sign, p.glue_order
222 = nil, f.glue_set, f.glue_sign, f.glue_order
229 function adjust_width(head)
230 if not head then return head end
231 for p in node.traverse_id(id_hlist, head) do
232 local res = get_total_stretched(p)
233 --print(table.serialize(res))
237 for i,v in pairs(res) do
238 if type(i)=='number' then
241 end; total = tex.round(total * res.glue_set)
242 local added_flag = aw_step1(p, res, total)
243 --print(total, res[0], res[KANJI_SKIP], res[FROM_JFM])
244 aw_step2(p, res, total, added_flag)
245 if new_ks then node_free(new_ks); new_ks = nil end
246 if new_xs then node_free(new_xs); new_xs = nil end
255 luatexbase.add_to_callback('post_linebreak_filter', adjust_width, 'Adjust width', 100)
259 function disable_cb()
261 luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')