OSDN Git Service

6c554c9c332dc40b544b6ee0c5353be9964fb19e
[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 function get_total_stretched(p)
65    local go, gf, gs = p.glue_order, p.glue_set, p.glue_sign
66    local new_ks, new_xs
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    if new_ks then node_free(new_ks); new_ks = nil end
116    if new_xs then node_free(new_xs); new_xs = nil end
117    return res
118 end
119
120 local function clear_stretch(p, ic, name)
121    --print('clear ' .. ic)
122    for q in node.traverse_id(id_glue, p.head) do
123       if get_attr_icflag(q) == ic then
124          local qs = q.spec
125          if qs.writable then
126             qs[name..'_order'], qs[name] = 0, 0
127          end
128       end
129    end
130 end
131
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 q in node.traverse_id(id_glue, p.head) do
137          if get_attr_icflag(q) == ic then
138             local qs = q.spec
139             if qs.writable and qs[name..'_order'] == 0 then
140                qs[name] = qs[name]*ratio
141             end
142          end
143       end
144    end
145 end
146
147 -- step 1: 行末に kern を挿入(句読点,中点用)
148 local function aw_step1(p, res, total)
149    local x = node.tail(p.head); if not x then return false end
150    local x = node.prev(x)     ; if not x then return false end
151    -- 本当の行末の node を格納
152    if x.id == id_glue and x.subtype == 15 then 
153       -- 段落最終行のときは,\penalty10000 \parfillskip が入るので,
154       -- その前の node が本来の末尾文字となる
155       x = node.prev(node.prev(x)) 
156    end
157
158    local xi, xc = x.id
159    if xi == id_glyph and has_attr(x, attr_curjfnt) == x.font then
160       -- 和文文字
161       xc = x
162    elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
163       -- packed JAchar
164       xc = x.head
165    else
166      return false-- それ以外は対象外.
167    end
168    local xk = ltjf_font_metric_table -- 
169      [xc.font].size_cache.char_type[has_attr(xc, attr_jchar_class) or 0]
170      ['end_' .. res.name] or 0
171      --print(res.name, total, xk, unicode.utf8.char(xc.char))
172
173    if xk>0 and total>=xk then
174       --print("ADDED")
175       total = total - xk
176       local kn = node.new(id_kern)
177       kn.kern = (res.name=='shrink' and -1 or 1) * xk
178       set_attr(kn, attr_icflag, FROM_JFM)
179       node.insert_after(p.head, x, kn)
180       return true
181    else return false
182    end
183 end
184
185 -- step 2: 行中の glue を変える
186 local function aw_step2(p, res, total, added_flag)
187    if total == 0 then -- もともと伸縮の必要なし
188       if added_flag then -- 行末に kern 追加したので,それによる補正
189          local f = node.hpack(p.head, p.width, 'exactly')
190          f.head, p.glue_set, p.glue_sign, p.glue_order 
191             = nil, f.glue_set, f.glue_sign, f.glue_order
192          node.free(f); return
193       end
194    elseif total <= res[0] then -- 和文処理グルー以外で足りる
195       for _,v in pairs(priority_table) do clear_stretch(p, v, res.name) end
196       local f = node.hpack(p.head, p.width, 'exactly')
197       f.head, p.glue_set, p.glue_sign, p.glue_order 
198          = nil, f.glue_set, f.glue_sign, f.glue_order
199       node.free(f)
200    else
201       total, i = total - res[0], 1
202       while i <= #priority_table do
203          local v = priority_table[i]
204          if total <= res[v] then
205             for j = i+1,#priority_table do
206                clear_stretch(p, priority_table[j], res.name)
207             end
208             set_stretch(p, total, res[v], v, res.name)
209             i = #priority_table + 9 -- ループから抜けさせたいため
210          end
211          total, i= total - res[v], i+1
212       end
213       if i == #priority_table + 10 or added_flag then
214          local f = node.hpack(p.head, p.width, 'exactly')
215          f.head, p.glue_set, p.glue_sign, p.glue_order 
216             = nil, f.glue_set, f.glue_sign, f.glue_order
217          node.free(f)
218       end
219    end
220 end
221
222
223 function adjust_width(head) 
224    if not head then return head end
225    for p in node.traverse_id(id_hlist, head) do
226       local res = get_total_stretched(p)
227       --print(table.serialize(res))
228       if res then
229          -- 調整量の合計
230          local total = 0
231          for i,v in pairs(res) do 
232             if type(i)=='number' then
233                total = total + v
234             end
235          end; total = tex.round(total * res.glue_set)
236          local added_flag = aw_step1(p, res, total)
237          --print(total, res[0], res[KANJI_SKIP], res[FROM_JFM])
238          aw_step2(p, res, total, added_flag)
239       end
240    end
241    return head
242 end
243
244 local is_reg = false
245 function enable_cb()
246    if not is_reg then
247       luatexbase.add_to_callback('post_linebreak_filter', adjust_width, 'Adjust width', 100)
248       is_reg = true
249    end
250 end
251 function disable_cb()
252    if is_reg then
253       luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
254       is_reg = false
255    end
256 end