OSDN Git Service

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