OSDN Git Service

db92b2a345f0b2e564a3d48c5d4035ea6d4ff911
[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       = luatexja.icflag_table.PACKED
33 local FROM_JFM     = luatexja.icflag_table.FROM_JFM
34 local KANJI_SKIP   = luatexja.icflag_table.KANJI_SKIP
35 local XKANJI_SKIP  = luatexja.icflag_table.XKANJI_SKIP
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             -- それはここでは望ましくないので,各 glue ごとに異なる spec を使う.
83             -- JFM グルーはそれぞれ異なる glue_spec を用いているので,問題ない.
84             res[ic] = res[ic] + a
85             if ic == KANJI_SKIP or ic == XKANJI_SKIP  then
86                if q.spec ~= spec_zero_glue then
87                   local ts, g; 
88                   q.spec, ts = node_copy(q.spec), q.spec
89                   g = node.copy(q); q.spec = ts
90                   node.insert_before(head, q, g);
91                   head = node.remove(head, q); node.free(q); q = g
92                end
93             end
94          else 
95             res[0]  = res[0]  + a
96          end
97       end
98       q = node_next(q)
99    end
100    return res
101 end
102
103 local function clear_stretch(p, ic, name)
104    --print('clear ' .. ic)
105    for q in node.traverse_id(id_glue, p.head) do
106       if get_attr_icflag(q) == ic then
107          local qs = q.spec
108          if qs.writable then
109             qs[name..'_order'], qs[name] = 0, 0
110          end
111       end
112    end
113 end
114
115 local set_stretch_table = {}
116 local function set_stretch(p, after, before, ic, name)
117    if before > 0 then
118       --print (ic, before, after)
119       local ratio = after/before
120       for i,_ in pairs(set_stretch_table) do
121          set_stretch_table[i] = nil
122       end
123       for q in node.traverse_id(id_glue, p.head) do
124          if get_attr_icflag(q) == ic then
125             local qs, do_flag = q.spec, true
126             for i=1,#set_stretch_table do 
127                if set_stretch_table[i]==qs then do_flag = false end 
128             end
129             if qs.writable and qs[name..'_order'] == 0 and do_flag then
130                qs[name] = qs[name]*ratio; 
131                set_stretch_table[#set_stretch_table+1] = qs
132             end
133          end
134       end
135    end
136 end
137
138 -- step 1: 行末に kern を挿入(句読点,中点用)
139 local function aw_step1(p, res, total)
140    local x = node.tail(p.head); if not x then return false end
141    local x = node.prev(x)     ; if not x then return false end
142    -- 本当の行末の node を格納
143    if x.id == id_glue and x.subtype == 15 then 
144       -- 段落最終行のときは,\penalty10000 \parfillskip が入るので,
145       -- その前の node が本来の末尾文字となる
146       x = node.prev(node.prev(x)) 
147    end
148
149    local xi, xc = x.id
150    if xi == id_glyph and has_attr(x, attr_curjfnt) == x.font then
151       -- 和文文字
152       xc = x
153    elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
154       -- packed JAchar
155       xc = x.head
156    else
157      return false-- それ以外は対象外.
158    end
159    local xk = ltjf_font_metric_table -- 
160      [xc.font].size_cache.char_type[has_attr(xc, attr_jchar_class) or 0]
161      ['end_' .. res.name] or 0
162      --print(res.name, total, xk, unicode.utf8.char(xc.char))
163
164    if xk>0 and total>=xk then
165       --print("ADDED")
166       total = total - xk
167       local kn = node.new(id_kern)
168       kn.kern = (res.name=='shrink' and -1 or 1) * xk
169       set_attr(kn, attr_icflag, FROM_JFM)
170       node.insert_after(p.head, x, kn)
171       return true
172    else return false
173    end
174 end
175
176 -- step 2: 行中の glue を変える
177 local function aw_step2(p, res, total, added_flag)
178    if total == 0 then -- もともと伸縮の必要なし
179       if added_flag then -- 行末に kern 追加したので,それによる補正
180          local f = node.hpack(p.head, p.width, 'exactly')
181          f.head, p.glue_set, p.glue_sign, p.glue_order 
182             = nil, f.glue_set, f.glue_sign, f.glue_order
183          node.free(f); return
184       end
185    elseif total <= res[0] then -- 和文処理グルー以外で足りる
186       for _,v in pairs(priority_table) do clear_stretch(p, v, res.name) end
187       local f = node.hpack(p.head, p.width, 'exactly')
188       f.head, p.glue_set, p.glue_sign, p.glue_order 
189          = nil, f.glue_set, f.glue_sign, f.glue_order
190       node.free(f)
191    else
192       total, i = total - res[0], 1
193       while i <= #priority_table do
194          local v = priority_table[i]
195          if total <= res[v] then
196             for j = i+1,#priority_table do
197                clear_stretch(p, priority_table[j], res.name)
198             end
199             set_stretch(p, total, res[v], v, res.name)
200             i = #priority_table + 9 -- ループから抜けさせたいため
201          end
202          total, i= total - res[v], i+1
203       end
204       if i == #priority_table + 10 or added_flag then
205          local f = node.hpack(p.head, p.width, 'exactly')
206          f.head, p.glue_set, p.glue_sign, p.glue_order 
207             = nil, f.glue_set, f.glue_sign, f.glue_order
208          node.free(f)
209       end
210    end
211 end
212
213
214 function adjust_width(head) 
215    if not head then return head end
216    for p in node.traverse_id(id_hlist, head) do
217       local res = get_total_stretched(p)
218       --print(table.serialize(res))
219       if res then
220          -- 調整量の合計
221          local total = 0
222          for i,v in pairs(res) do 
223             if type(i)=='number' then
224                total = total + v
225             end
226          end; total = tex.round(total * res.glue_set)
227          local added_flag = aw_step1(p, res, total)
228          --print(total, res[0], res[KANJI_SKIP], res[FROM_JFM])
229          aw_step2(p, res, total, added_flag)
230       end
231    end
232    return head
233 end
234
235 local is_reg = false
236 function enable_cb()
237    if not is_reg then
238       luatexbase.add_to_callback('post_linebreak_filter', adjust_width, 'Adjust width', 100)
239       is_reg = true
240    end
241 end
242 function disable_cb()
243    if is_reg then
244       luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
245       is_reg = false
246    end
247 end