4 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
5 luatexja.load_module('jfmglue'); local ltjj = luatexja.jfmglue
6 luatexja.load_module('stack'); local ltjs = luatexja.stack
7 luatexja.load_module('direction'); local ltjd = luatexja.direction
9 local to_node = node.direct.tonode
10 local to_direct = node.direct.todirect
12 local setfield = node.direct.setfield
13 local setglue = luatexja.setglue
14 local getfield = node.direct.getfield
15 local is_zero_glue = node.direct.is_zero_glue or
17 return (getfield(g,'width')==0)and (getfield(g,'stretch')==0)and(getfield(g,'shrink')==0)
19 local getlist = node.direct.getlist
20 local getid = node.direct.getid
21 local getfont = node.direct.getfont
22 local getsubtype = node.direct.getsubtype
24 local node_traverse_id = node.direct.traverse_id
25 local node_new = node.direct.new
26 local node_copy = node.direct.copy
27 local node_hpack = node.direct.hpack
28 local node_next = node.direct.getnext
29 local node_free = node.direct.free
30 local node_prev = node.direct.getprev
31 local node_tail = node.direct.tail
32 local has_attr = node.direct.has_attribute
33 local set_attr = node.direct.set_attribute
34 local insert_after = node.direct.insert_after
36 local id_glyph = node.id('glyph')
37 local id_kern = node.id('kern')
38 local id_hlist = node.id('hlist')
39 local id_glue = node.id('glue')
40 local id_whatsit = node.id('whatsit')
41 local attr_icflag = luatexbase.attributes['ltj@icflag']
42 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
43 local lang_ja = luatexja.lang_ja
45 local ltjf_font_metric_table = ltjf.font_metric_table
46 local round, pairs = tex.round, pairs
48 local PACKED = luatexja.icflag_table.PACKED
49 local FROM_JFM = luatexja.icflag_table.FROM_JFM
50 local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
51 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
52 local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
53 local XKANJI_SKIP_JFM = luatexja.icflag_table.XKANJI_SKIP_JFM
55 local priority_table = {
67 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
68 get_attr_icflag = function(p)
69 return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
73 -- box 内で伸縮された glue の合計値を計算
75 local function get_stretched(q, go, gs)
76 if gs == 1 then -- stretching
77 if getfield(q, 'stretch_order') == go then
78 return getfield(q, 'stretch')
82 if getfield(q, 'shrink_order') == go then
83 return getfield(q, 'shrink')
90 local gs_used_line = {}
91 local function get_total_stretched(p, line)
93 = getfield(p, 'glue_order'), getfield(p, 'glue_set'), getfield(p, 'glue_sign')
94 if go ~= 0 then return nil end
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 gs ~= 1 and gs ~= 2 then return res, 0 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 ic == KANJI_SKIP_JFM then ic = KANJI_SKIP
102 elseif ic == XKANJI_SKIP_JFM then ic = XKANJI_SKIP
104 if type(res[ic]) == 'number' then
105 -- kanjiskip, xkanjiskip は段落内で spec を共有しているが,
106 -- それはここでは望ましくないので,各 glue ごとに異なる spec を使う.
107 -- 本当は各行ごとに glue_spec を共有させたかったが,安直にやると
108 -- ref_count が 0 なので Double-free が発生する.どうする?
109 -- JFM グルーはそれぞれ異なる glue_spec を用いているので,問題ない.
110 if (ic == KANJI_SKIP or ic == XKANJI_SKIP) and getsubtype(q)==0 then
111 local qs = getfield(q, 'spec')
112 if is_zero_glue(q) then
113 if (gs_used_line[qs] or 0)<line then
114 setfield(q, 'spec', node_copy(qs))
115 local f = node_new(id_glue); setfield(f, 'spec', qs); node_free(f)
116 -- decrese qs's reference count
118 gs_used_line[qs] = line
122 res[ic], total = res[ic] + a, total + a
124 res[0], total = res[0] + a, total + a
130 local function clear_stretch(p, ic, name)
131 for q in node_traverse_id(id_glue, getlist(p)) do
132 local f = get_attr_icflag(q)
133 if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM))
134 or ((ic ==XKANJI_SKIP) and (f == XKANJI_SKIP_JFM)) then
135 local qs = getfield(q, 'spec')
136 if getfield(qs, 'writable') then
137 setfield(qs, name..'_order', 0)
138 setfield(qs, name, 0)
144 local set_stretch_table = {}
145 local function set_stretch(p, after, before, ic, name)
147 local ratio = after/before
148 for i,_ in pairs(set_stretch_table) do
149 set_stretch_table[i] = nil
151 for q in node_traverse_id(id_glue, getlist(p)) do
152 local f = get_attr_icflag(q)
153 if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM))
154 or ((ic ==XKANJI_SKIP) and (f == XKANJI_SKIP_JFM)) then
155 local qs, do_flag = getfield(q, 'spec'), true
156 for i=1,#set_stretch_table do
157 if set_stretch_table[i]==qs then do_flag = false end
159 if getfield(qs, 'writable') and getfield(qs, name..'_order')==0 and do_flag then
160 setfield(q, name, getfield(qs, name)*ratio)
161 set_stretch_table[#set_stretch_table+1] = qs
168 -- step 1: 行末に kern を挿入(句読点,中点用)
169 local ltjd_glyph_from_packed = ltjd.glyph_from_packed
170 local function aw_step1(p, res, total)
171 local head = getlist(p)
172 local x = node_tail(head); if not x then return false end
174 x = node_prev(x); if not x then return false end
175 local xi, xc = getid(x)
176 if xi == id_glue and getsubtype(x) == 15 then
177 -- 段落最終行のときは,\penalty10000 \parfillskip が入るので,
178 -- その前の node が本来の末尾文字となる
179 x = node_prev(node_prev(x)); xi = getid(x)
181 -- local xi = getid(x)
182 -- while (get_attr_icflag(x) == PACKED)
183 -- and ((xi == id_penalty) or (xi == id_kern) or (xi == id_kern)) do
184 -- x = node_prev(x); xi = getid(x)
186 if xi == id_glyph and getfield(x, 'lang')==lang_ja then
189 elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
191 xc = ltjd_glyph_from_packed(x)
192 while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
194 return false-- それ以外は対象外.
196 local xk = ltjf_font_metric_table[getfont(xc)]
197 .char_type[has_attr(xc, attr_jchar_class) or 0]['end_' .. res.name] or 0
199 if xk>0 and total>=xk then
201 local kn = node_new(id_kern)
202 setfield(kn, 'kern', (res.name=='shrink' and -1 or 1) * xk)
203 set_attr(kn, attr_icflag, FROM_JFM)
204 insert_after(head, x, kn)
210 -- step 2: 行中の glue を変える
211 local function aw_step2(p, res, total, added_flag)
212 if total == 0 then -- もともと伸縮の必要なし
213 if added_flag then -- 行末に kern 追加したので,それによる補正
214 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
215 setfield(f, 'head', nil)
216 setfield(p, 'glue_set', getfield(f, 'glue_set'))
217 setfield(p, 'glue_order', getfield(f, 'glue_order'))
218 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
222 elseif total <= res[0] then -- 和文処理グルー以外で足りる
223 for _,v in pairs(priority_table) do clear_stretch(p, v, res.name) end
224 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
225 setfield(f, 'head', nil)
226 setfield(p, 'glue_set', getfield(f, 'glue_set'))
227 setfield(p, 'glue_order', getfield(f, 'glue_order'))
228 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
231 total = total - res[0]
232 for i = 1, #priority_table do
233 local v = priority_table[i]
234 if total <= res[v] then
235 for j = i+1,#priority_table do
236 clear_stretch(p, priority_table[j], res.name)
238 set_stretch(p, total, res[v], v, res.name); break
240 total = total - res[v]
242 local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
243 setfield(f, 'head', nil)
244 setfield(p, 'glue_set', getfield(f, 'glue_set'))
245 setfield(p, 'glue_order', getfield(f, 'glue_order'))
246 setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
252 local ltjs_fast_get_stack_skip = ltjs.fast_get_stack_skip
253 local function adjust_width(head)
254 if not head then return head end
256 for p in node_traverse_id(id_hlist, to_direct(head)) do
258 local res, total = get_total_stretched(p, line)
259 -- this is the same table as the table which is def'd in l. 92
260 if res and res.glue_set<1 then
261 total = round(total * res.glue_set)
262 aw_step2(p, res, total, aw_step1(p, res, total))
265 for i,_ in pairs(gs_used_line) do
266 gs_used_line[i] = nil
272 luatexja.adjust = luatexja.adjust or {}
274 function luatexja.adjust.enable_cb()
276 luatexbase.add_to_callback('post_linebreak_filter',
277 adjust_width, 'Adjust width', 100)
281 function luatexja.adjust.disable_cb()
283 luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
289 luatexja.unary_pars.adjust = function(t)
290 return is_reg and 1 or 0