OSDN Git Service

ltj-adjust.lua: fix aw_step1 (as 10e5a41)
[luatex-ja/luatexja.git] / src / ltj-adjust.lua
1 --
2 -- luatexja/otf.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.adjust',
6   date = '2014/02/02',
7   description = 'Advanced line adjustment for LuaTeX-ja',
8 })
9 module('luatexja.adjust', package.seeall)
10
11 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
12 luatexja.load_module('jfmglue');   local ltjj = luatexja.jfmglue
13 luatexja.load_module('stack');     local ltjs = luatexja.stack
14
15 local Dnode = node.direct or node
16
17 local nullfunc = function(n) return n end
18 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
19 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
20
21 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
22 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
23 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
24 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
25 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
26 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
27
28 local node_traverse_id = Dnode.traverse_id
29 local node_new = Dnode.new
30 local node_copy = Dnode.copy
31 local node_hpack = Dnode.hpack
32 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
33 local node_free = Dnode.free
34 local node_prev = (Dnode ~= node) and Dnode.getprev or node.prev
35 local node_tail = Dnode.tail
36 local has_attr = Dnode.has_attribute
37 local set_attr = Dnode.set_attribute
38 local insert_after = Dnode.insert_after
39
40 local id_glyph = node.id('glyph')
41 local id_kern = node.id('kern')
42 local id_hlist = node.id('hlist')
43 local id_glue  = node.id('glue')
44 local id_glue_spec = node.id('glue_spec')
45 local id_whatsit = node.id('whatsit')
46 local attr_icflag = luatexbase.attributes['ltj@icflag']
47 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
48 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
49
50 local ltjf_font_metric_table = ltjf.font_metric_table
51 local spec_zero_glue = ltjj.spec_zero_glue
52 local round, pairs = tex.round, pairs
53
54 local PACKED       = luatexja.icflag_table.PACKED
55 local FROM_JFM     = luatexja.icflag_table.FROM_JFM
56 local KANJI_SKIP   = luatexja.icflag_table.KANJI_SKIP
57 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
58 local XKANJI_SKIP  = luatexja.icflag_table.XKANJI_SKIP
59 local XKANJI_SKIP_JFM  = luatexja.icflag_table.XKANJI_SKIP_JFM
60
61 local priority_table = {
62    FROM_JFM + 2,
63    FROM_JFM + 1,
64    FROM_JFM,
65    FROM_JFM - 1,
66    FROM_JFM - 2,
67    XKANJI_SKIP,
68    KANJI_SKIP
69 }
70
71 local get_attr_icflag
72 do
73    local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
74    get_attr_icflag = function(p)
75       return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
76    end
77 end
78
79 -- box 内で伸縮された glue の合計値を計算
80
81 local function get_stretched(q, go, gs)
82    local qs = getfield(q, 'spec')
83    if not getfield(qs, 'writable') then return 0 end
84    if gs == 1 then -- stretching
85       if getfield(qs, 'stretch_order') == go then
86          return getfield(qs, 'stretch')
87       else return 0
88       end
89    else -- shrinking
90       if getfield(qs, 'shrink_order') == go then 
91          return getfield(qs, 'shrink')
92       else return 0
93       end
94    end
95 end
96
97 local res = {}
98 local gs_used_line = {}
99 local function get_total_stretched(p, line)
100    local go, gf, gs 
101       = getfield(p, 'glue_order'), getfield(p, 'glue_set'), getfield(p, 'glue_sign')
102    if go ~= 0 then return nil end
103    res[0], res.glue_set, res.name = 0, gf, (gs==1) and 'stretch' or 'shrink'
104    for i=1,#priority_table do res[priority_table[i]]=0 end
105    if gs ~= 1 and gs ~= 2 then return res, 0 end
106    local total = 0
107    for q in node_traverse_id(id_glue, getlist(p)) do
108       local a, ic = get_stretched(q, go, gs), get_attr_icflag(q)
109       if   type(res[ic]) == 'number' then 
110          -- kanjiskip, xkanjiskip は段落内で spec を共有しているが,
111          -- それはここでは望ましくないので,各 glue ごとに異なる spec を使う.
112          -- 本当は各行ごとに glue_spec を共有させたかったが,安直にやると
113          -- ref_count が 0 なので Double-free が発生する.どうする?
114          -- JFM グルーはそれぞれ異なる glue_spec を用いているので,問題ない.
115          if (ic == KANJI_SKIP or ic == XKANJI_SKIP) and getsubtype(q)==0 then
116             local qs = getfield(q, 'spec')
117             if qs ~= spec_zero_glue then
118                if (gs_used_line[qs] or 0)<line  then
119                   setfield(q, 'spec', node_copy(qs))
120                   local f = node_new(id_glue); setfield(f, 'spec', qs); node_free(f)
121                   -- decrese qs's reference count
122                else
123                   gs_used_line[qs] = line
124                end
125             end
126          elseif ic == KANJI_SKIP_JFM  then ic = KANJI_SKIP
127          elseif ic == XKANJI_SKIP_JFM  then ic = XKANJI_SKIP
128          end
129          res[ic], total = res[ic] + a, total + a
130       else 
131          res[0], total = res[0]  + a, total + a
132       end
133    end
134    return res, total
135 end
136
137 local function clear_stretch(p, ic, name)
138    for q in node_traverse_id(id_glue, getlist(p)) do
139       if get_attr_icflag(q) == ic then
140          local qs = getfield(q, 'spec')
141          if getfield(qs, 'writable') then
142             setfield(qs, name..'_order', 0)
143             setfield(qs, name, 0)
144          end
145       end
146    end
147 end
148
149 local set_stretch_table = {}
150 local function set_stretch(p, after, before, ic, name)
151    if before > 0 then
152       local ratio = after/before
153       for i,_ in pairs(set_stretch_table) do
154          set_stretch_table[i] = nil
155       end
156       for q in node_traverse_id(id_glue, getlist(p)) do
157          local f = get_attr_icflag(q)
158          if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM)) 
159            or ((ic ==XKANJI_SKIP) and (f == XKANJI_SKIP_JFM)) then
160             local qs, do_flag = getfield(q, 'spec'), true
161             for i=1,#set_stretch_table do 
162                if set_stretch_table[i]==qs then do_flag = false end 
163             end
164             if getfield(qs, 'writable') and getfield(qs, name..'_order')==0 and do_flag then
165                setfield(qs, name, getfield(qs, name)*ratio)
166                set_stretch_table[#set_stretch_table+1] = qs
167             end
168          end
169       end
170    end
171 end
172
173 -- step 1: 行末に kern を挿入(句読点,中点用)
174 local function aw_step1(p, res, total)
175    local head = getlist(p)
176    local x = node_tail(head); if not x then return false end
177    -- x: \rightskip
178    x = node_prev(x); if not x then return false end
179    if getid(x) == id_glue and getsubtype(x) == 15 then 
180       -- 段落最終行のときは,\penalty10000 \parfillskip が入るので,
181       -- その前の node が本来の末尾文字となる
182       x = node_prev(node_prev(x)) 
183    end
184    -- local xi = getid(x)
185    -- while (get_attr_icflag(x) == PACKED) 
186    --    and  ((xi == id_penalty) or (xi == id_kern) or (xi == id_kern)) do
187    --       x = node_prev(x); xi = getid(x)
188    -- end
189    local xi, xc = getid(x)
190    if xi == id_glyph and has_attr(x, attr_curjfnt) == getfont(x) then
191       -- 和文文字
192       xc = x
193    elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
194       -- packed JAchar
195       xc = getlist(x)
196       while getid(xc) == id_whatsit do xc = node_next(xc) end
197    else
198      return false-- それ以外は対象外.
199    end
200    local xk = ltjf_font_metric_table[getfont(xc)]
201    xk = xk.char_type[has_attr(xc, attr_jchar_class) or 0]
202    xk = xk['end_' .. res.name] or 0
203
204    if xk>0 and total>=xk then
205       total = total - xk
206       local kn = node_new(id_kern)
207       setfield(kn, 'kern', (res.name=='shrink' and -1 or 1) * xk)
208       set_attr(kn, attr_icflag, FROM_JFM)
209       insert_after(head, x, kn)
210       return true
211    else return false
212    end
213 end
214
215 -- step 2: 行中の glue を変える
216 local function aw_step2(p, res, total, added_flag)
217    if total == 0 then -- もともと伸縮の必要なし
218       if added_flag then -- 行末に kern 追加したので,それによる補正
219          local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
220          setfield(f, 'head', nil)
221          setfield(p, 'glue_set', getfield(f, 'glue_set'))
222          setfield(p, 'glue_order', getfield(f, 'glue_order'))
223          setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
224          node_free(f)
225          return
226       end
227    elseif total <= res[0] then -- 和文処理グルー以外で足りる
228       for _,v in pairs(priority_table) do clear_stretch(p, v, res.name) end
229       local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
230       setfield(f, 'head', nil)
231       setfield(p, 'glue_set', getfield(f, 'glue_set'))
232       setfield(p, 'glue_order', getfield(f, 'glue_order'))
233       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
234       node_free(f)
235    else
236       total = total - res[0]
237       for i = 1, #priority_table do
238          local v = priority_table[i]
239          if total <= res[v] then
240             for j = i+1,#priority_table do
241                clear_stretch(p, priority_table[j], res.name)
242             end
243             set_stretch(p, total, res[v], v, res.name); break
244          end
245          total = total - res[v]
246       end
247       local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
248       setfield(f, 'head', nil)
249       setfield(p, 'glue_set', getfield(f, 'glue_set'))
250       setfield(p, 'glue_order', getfield(f, 'glue_order'))
251       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
252       node_free(f)
253    end
254 end
255
256
257 local ltjs_fast_get_stack_skip = ltjs.fast_get_stack_skip
258 local function adjust_width(head) 
259    if not head then return head end
260    local line = 1
261    for p in node_traverse_id(id_hlist, to_direct(head)) do
262       line = line + 1
263       local res, total = get_total_stretched(p, line) 
264         -- this is the same table as the table which is def'd in l. 92
265       if res and res.glue_set<1 then
266          total = round(total * res.glue_set)
267          aw_step2(p, res, total, aw_step1(p, res, total))
268       end
269    end
270    for i,_ in pairs(gs_used_line) do
271       gs_used_line[i]  = nil
272    end
273    return to_node(head)
274 end
275
276 do
277    local is_reg = false
278    function enable_cb()
279       if not is_reg then
280          luatexbase.add_to_callback('post_linebreak_filter', 
281                                     adjust_width, 'Adjust width', 100)
282          is_reg = true
283       end
284    end
285    function disable_cb()
286       if is_reg then
287          luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
288          is_reg = false
289       end
290    end
291 end
292
293 luatexja.unary_pars.adjust = function(t)
294    return is_reg and 1 or 0
295 end