OSDN Git Service

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