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
63 glue_set = gf, name = (gs==1) and 'stretch' or 'shrink'
65 for i=1,#priority_table do res[priority_table[i]]=0 end
66 if go ~= 0 then return nil end
67 if gs ~= 1 and gs ~= 2 then return res end
68 for q in node.traverse_id(id_glue, p.head) do
69 local a, ic = get_stretched(q, go, gs), get_attr_icflag(q)
71 if type(res[ic]) == 'number' then res[ic] = res[ic] + a
72 else res[0] = res[0] + a
78 local function clear_stretch(p, ic, name)
79 --print('clear ' .. ic)
80 for q in node.traverse_id(id_glue, p.head) do
81 if get_attr_icflag(q) == ic then
84 qs[name..'_order'], qs[name] = 0, 0
90 local function set_stretch(p, after, before, ic, name)
92 --print (ic, before, after)
93 local ratio = after/before
94 for q in node.traverse_id(id_glue, p.head) do
95 if get_attr_icflag(q) == ic then
97 if qs.writable and qs[name..'_order'] == 0 then
98 qs[name] = qs[name]*ratio
105 -- step 1: 行末に kern を挿入(句読点,中点用)
106 local function aw_step1(p, res, total)
107 local x = node.tail(p.head); if not x then return false end
108 local x = node.prev(x) ; if not x then return false end
110 if x.id == id_glue and x.subtype == 15 then
111 -- 段落最終行のときは,\penalty10000 \parfillskip が入るので,
112 -- その前の node が本来の末尾文字となる
113 x = node.prev(node.prev(x))
117 if x.id == id_glyph and has_attr(x, attr_curjfnt) == x.font then
120 elseif x.id == id_hlist and get_attr_icflag(x) == PACKED then
124 return false-- それ以外は対象外.
126 local xk = ltjf_font_metric_table --
127 [xc.font].size_cache.char_type[has_attr(xc, attr_jchar_class) or 0]
128 ['end_' .. res.name] or 0
129 --print(res.name, total, xk, unicode.utf8.char(xc.char))
131 if xk>0 and total>=xk then
134 local kn = node.new(id_kern)
135 kn.kern = (res.name=='shrink' and -1 or 1) * xk
136 set_attr(kn, attr_icflag, FROM_JFM)
137 node.insert_after(p.head, x, kn)
143 -- step 2: 行中の glue を変える
144 local function aw_step2(p, res, total, added_flag)
145 if total == 0 then -- もともと伸縮の必要なし
146 if added_flag then -- 行末に kern 追加したので,それによる補正
147 local f = node.hpack(p.head, p.width, 'exactly')
148 f.head, p.glue_set, p.glue_sign, p.glue_order
149 = nil, f.glue_set, f.glue_sign, f.glue_order
152 elseif total <= res[0] then -- 和文処理グルー以外で足りる
153 for _,v in pairs(priority_table) do clear_stretch(p, v, res.name) end
154 local f = node.hpack(p.head, p.width, 'exactly')
155 f.head, p.glue_set, p.glue_sign, p.glue_order
156 = nil, f.glue_set, f.glue_sign, f.glue_order
159 total, i = total - res[0], 1
160 while i <= #priority_table do
161 local v = priority_table[i]
162 if total <= res[v] then
163 for j = i+1,#priority_table do
164 clear_stretch(p, priority_table[j], res.name)
166 set_stretch(p, total, res[v], v, res.name)
167 i = #priority_table + 9 -- ループから抜けさせたいため
169 total, i= total - res[v], i+1
171 if i == #priority_table + 10 or added_flag then
172 local f = node.hpack(p.head, p.width, 'exactly')
173 f.head, p.glue_set, p.glue_sign, p.glue_order
174 = nil, f.glue_set, f.glue_sign, f.glue_order
181 function adjust_width(head)
182 if not head then return head end
183 for p in node.traverse_id(id_hlist, head) do
184 local res = get_total_stretched(p)
185 --print(table.serialize(res))
189 for i,v in pairs(res) do
190 if type(i)=='number' then
193 end; total = tex.round(total * res.glue_set)
194 local added_flag = aw_step1(p, res, total)
195 aw_step2(p, res, total, added_flag)
204 luatexbase.add_to_callback('post_linebreak_filter', adjust_width, 'Adjust width', 100)
208 function disable_cb()
210 luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')