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
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']
25 local ltjf_font_metric_table = ltjf.font_metric_table
30 local XKANJI_SKIP = 10
32 local priority_table = {
42 local PROCESSED_BEGIN_FLAG = 32
43 local function get_attr_icflag(p)
44 return (node.has_attribute(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
47 -- box 内で伸縮された glue の合計値を計算
49 local function get_stretched(q, go, gs)
51 if not qs.writable then return 0 end
52 if gs == 1 then -- stretching
53 if qs.stretch_order == go then return qs.stretch end
55 if qs.shrink_order == go then return qs.shrink end
59 local function get_total_stretched(p)
60 local go, gf, gs = p.glue_order, p.glue_set, p.glue_sign
64 glue_set = gf, name = (gs==1) and 'stretch' or 'shrink'
66 for i=1,#priority_table do res[priority_table[i]]=0 end
67 if go ~= 0 then return nil end
68 if gs ~= 1 and gs ~= 2 then return res end
69 for q in node.traverse_id(id_glue, p.head) do
70 local a, ic = get_stretched(q, go, gs), get_attr_icflag(q)
72 if type(res[ic]) == 'number' then
74 if ic == KANJI_SKIP then
75 --if not new_ks then new_ks = node_copy(q.spec); end
77 q.spec = node_copy(q.spec);
78 elseif ic == XKANJI_SKIP then
79 q.spec = node.copy(q.spec)
81 else res[0] = res[0] + a
87 local function clear_stretch(p, ic, name)
88 --print('clear ' .. ic)
89 for q in node.traverse_id(id_glue, p.head) do
90 if get_attr_icflag(q) == ic then
93 qs[name..'_order'], qs[name] = 0, 0
99 local function set_stretch(p, after, before, ic, name)
101 --print (ic, before, after)
102 local ratio = after/before
103 for q in node.traverse_id(id_glue, p.head) do
104 if get_attr_icflag(q) == ic then
106 if qs.writable and qs[name..'_order'] == 0 then
107 qs[name] = qs[name]*ratio
114 -- step 1: 行末に kern を挿入(句読点,中点用)
115 local function aw_step1(p, res, total)
116 local x = node.tail(p.head); if not x then return false end
117 local x = node.prev(x) ; if not x then return false end
119 if x.id == id_glue and x.subtype == 15 then
120 -- 段落最終行のときは,\penalty10000 \parfillskip が入るので,
121 -- その前の node が本来の末尾文字となる
122 x = node.prev(node.prev(x))
126 if x.id == id_glyph and has_attr(x, attr_curjfnt) == x.font then
129 elseif x.id == id_hlist and get_attr_icflag(x) == PACKED then
133 return false-- それ以外は対象外.
135 local xk = ltjf_font_metric_table --
136 [xc.font].size_cache.char_type[has_attr(xc, attr_jchar_class) or 0]
137 ['end_' .. res.name] or 0
138 --print(res.name, total, xk, unicode.utf8.char(xc.char))
140 if xk>0 and total>=xk then
143 local kn = node.new(id_kern)
144 kn.kern = (res.name=='shrink' and -1 or 1) * xk
145 set_attr(kn, attr_icflag, FROM_JFM)
146 node.insert_after(p.head, x, kn)
152 -- step 2: 行中の glue を変える
153 local function aw_step2(p, res, total, added_flag)
154 if total == 0 then -- もともと伸縮の必要なし
155 if added_flag then -- 行末に kern 追加したので,それによる補正
156 local f = node.hpack(p.head, p.width, 'exactly')
157 f.head, p.glue_set, p.glue_sign, p.glue_order
158 = nil, f.glue_set, f.glue_sign, f.glue_order
161 elseif total <= res[0] then -- 和文処理グルー以外で足りる
162 for _,v in pairs(priority_table) do clear_stretch(p, v, res.name) end
163 local f = node.hpack(p.head, p.width, 'exactly')
164 f.head, p.glue_set, p.glue_sign, p.glue_order
165 = nil, f.glue_set, f.glue_sign, f.glue_order
168 total, i = total - res[0], 1
169 while i <= #priority_table do
170 local v = priority_table[i]
171 if total <= res[v] then
172 for j = i+1,#priority_table do
173 clear_stretch(p, priority_table[j], res.name)
175 set_stretch(p, total, res[v], v, res.name)
176 i = #priority_table + 9 -- ループから抜けさせたいため
178 total, i= total - res[v], i+1
180 if i == #priority_table + 10 or added_flag then
181 local f = node.hpack(p.head, p.width, 'exactly')
182 f.head, p.glue_set, p.glue_sign, p.glue_order
183 = nil, f.glue_set, f.glue_sign, f.glue_order
190 function adjust_width(head)
191 if not head then return head end
192 for p in node.traverse_id(id_hlist, head) do
193 local res = get_total_stretched(p)
194 --print(table.serialize(res))
198 for i,v in pairs(res) do
199 if type(i)=='number' then
202 end; total = tex.round(total * res.glue_set)
203 local added_flag = aw_step1(p, res, total)
204 --print(total, res[0], res[KANJI_SKIP], res[FROM_JFM])
205 aw_step2(p, res, total, added_flag)
214 luatexbase.add_to_callback('post_linebreak_filter', adjust_width, 'Adjust width', 100)
218 function disable_cb()
220 luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')