OSDN Git Service

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