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
14 local id_glyph = node.id('glyph')
15 local id_kern = node.id('kern')
16 local id_hlist = node.id('hlist')
17 local id_glue = node.id('glue')
18 local id_glue_spec = node.id('glue_spec')
19 local has_attr = node.has_attribute
20 local set_attr = node.set_attribute
21 local attr_icflag = luatexbase.attributes['ltj@icflag']
22 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
23 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
24 local node_copy = node.copy
25 local node_next = node.next
26 local node_free = node.free
28 local ltjf_font_metric_table = ltjf.font_metric_table
29 local spec_zero_glue = ltjj.spec_zero_glue
31 local PACKED = luatexja.icflag_table.PACKED
32 local FROM_JFM = luatexja.icflag_table.FROM_JFM
33 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
34 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
36 local priority_table = {
46 local PROCESSED_BEGIN_FLAG = 32
47 local function get_attr_icflag(p)
48 return (node.has_attribute(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
51 -- box 内で伸縮された glue の合計値を計算
53 local function get_stretched(q, go, gs)
55 if not qs.writable then return 0 end
56 if gs == 1 then -- stretching
57 if qs.stretch_order == go then return qs.stretch end
59 if qs.shrink_order == go then return qs.shrink end
63 -- local new_ks, new_xs
64 local function get_total_stretched(p)
65 local go, gf, gs = p.glue_order, p.glue_set, p.glue_sign
68 glue_set = gf, name = (gs==1) and 'stretch' or 'shrink'
70 for i=1,#priority_table do res[priority_table[i]]=0 end
71 if go ~= 0 then return nil end
72 if gs ~= 1 and gs ~= 2 then return res end
75 --luatexja.ext_show_node_list(p.head, '>>> ', print)
78 local a, ic = get_stretched(q, go, gs), get_attr_icflag(q)
79 if type(res[ic]) == 'number' then
80 -- kanjiskip, xkanjiskip は段落内で spec を共有しているが,
81 -- それはここでは望ましくないので,各 glue ごとに異なる spec を使う.
82 -- JFM グルーはそれぞれ異なる glue_spec を用いているので,問題ない.
84 if ic == KANJI_SKIP or ic == XKANJI_SKIP then
85 if q.spec ~= spec_zero_glue then
87 q.spec, ts = node_copy(q.spec), q.spec
88 g = node.copy(q); q.spec = ts
89 node.insert_before(head, q, g);
90 head = node.remove(head, q); node.free(q); q = g
102 local function clear_stretch(p, ic, name)
103 --print('clear ' .. ic)
104 for q in node.traverse_id(id_glue, p.head) do
105 if get_attr_icflag(q) == ic then
108 qs[name..'_order'], qs[name] = 0, 0
114 local set_stretch_table = {}
115 local function set_stretch(p, after, before, ic, name)
117 --print (ic, before, after)
118 local ratio = after/before
119 for i,_ in pairs(set_stretch_table) do
120 set_stretch_table[i] = nil
122 for q in node.traverse_id(id_glue, p.head) do
123 if get_attr_icflag(q) == ic then
124 local qs, do_flag = q.spec, true
125 for i=1,#set_stretch_table do
126 if set_stretch_table[i]==qs then do_flag = false end
128 if qs.writable and qs[name..'_order'] == 0 and do_flag then
129 qs[name] = qs[name]*ratio;
130 set_stretch_table[#set_stretch_table+1] = qs
137 -- step 1: 行末に kern を挿入(句読点,中点用)
138 local function aw_step1(p, res, total)
139 local x = node.tail(p.head); if not x then return false end
140 local x = node.prev(x) ; if not x then return false end
142 if x.id == id_glue and x.subtype == 15 then
143 -- 段落最終行のときは,\penalty10000 \parfillskip が入るので,
144 -- その前の node が本来の末尾文字となる
145 x = node.prev(node.prev(x))
149 if xi == id_glyph and has_attr(x, attr_curjfnt) == x.font then
152 elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
156 return false-- それ以外は対象外.
158 local xk = ltjf_font_metric_table --
159 [xc.font].char_type[has_attr(xc, attr_jchar_class) or 0]
160 ['end_' .. res.name] or 0
161 --print(res.name, total, xk, unicode.utf8.char(xc.char))
163 if xk>0 and total>=xk then
166 local kn = node.new(id_kern)
167 kn.kern = (res.name=='shrink' and -1 or 1) * xk
168 set_attr(kn, attr_icflag, FROM_JFM)
169 node.insert_after(p.head, x, kn)
175 -- step 2: 行中の glue を変える
176 local function aw_step2(p, res, total, added_flag)
177 if total == 0 then -- もともと伸縮の必要なし
178 if added_flag then -- 行末に kern 追加したので,それによる補正
179 local f = node.hpack(p.head, p.width, 'exactly')
180 f.head, p.glue_set, p.glue_sign, p.glue_order
181 = nil, f.glue_set, f.glue_sign, f.glue_order
184 elseif total <= res[0] then -- 和文処理グルー以外で足りる
185 for _,v in pairs(priority_table) do clear_stretch(p, v, res.name) end
186 local f = node.hpack(p.head, p.width, 'exactly')
187 f.head, p.glue_set, p.glue_sign, p.glue_order
188 = nil, f.glue_set, f.glue_sign, f.glue_order
191 total, i = total - res[0], 1
192 while i <= #priority_table do
193 local v = priority_table[i]
194 if total <= res[v] then
195 for j = i+1,#priority_table do
196 clear_stretch(p, priority_table[j], res.name)
198 set_stretch(p, total, res[v], v, res.name)
199 i = #priority_table + 9 -- ループから抜けさせたいため
201 total, i= total - res[v], i+1
203 if i == #priority_table + 10 or added_flag then
204 local f = node.hpack(p.head, p.width, 'exactly')
205 f.head, p.glue_set, p.glue_sign, p.glue_order
206 = nil, f.glue_set, f.glue_sign, f.glue_order
213 function adjust_width(head)
214 if not head then return head end
215 for p in node.traverse_id(id_hlist, head) do
216 local res = get_total_stretched(p)
217 --print(table.serialize(res))
221 for i,v in pairs(res) do
222 if type(i)=='number' then
225 end; total = tex.round(total * res.glue_set)
226 local added_flag = aw_step1(p, res, total)
227 --print(total, res[0], res[KANJI_SKIP], res[FROM_JFM])
228 aw_step2(p, res, total, added_flag)
237 luatexbase.add_to_callback('post_linebreak_filter', adjust_width, 'Adjust width', 100)
241 function disable_cb()
243 luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')