OSDN Git Service

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