OSDN Git Service

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