OSDN Git Service

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