OSDN Git Service

Dnode -> node.direct, stopped to use 'spec'
[luatex-ja/luatexja.git] / src / ltj-adjust_85.lua
1 --
2 -- luatexja/otf.lua
3 --
4 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
5 luatexja.load_module('jfmglue');   local ltjj = luatexja.jfmglue
6 luatexja.load_module('stack');     local ltjs = luatexja.stack
7 luatexja.load_module('direction'); local ltjd = luatexja.direction
8
9 local to_node = node.direct.tonode
10 local to_direct = node.direct.todirect
11
12 local setfield = node.direct.setfield
13 local setglue = luatexja.setglue
14 local getfield = node.direct.getfield
15 local is_zero_glue = node.direct.is_zero_glue or
16    function(g)
17       return (getfield(g,'width')==0)and (getfield(g,'stretch')==0)and(getfield(g,'shrink')==0)
18    end
19 local getlist = node.direct.getlist
20 local getid = node.direct.getid
21 local getfont = node.direct.getfont
22 local getsubtype = node.direct.getsubtype
23
24 local node_traverse_id = node.direct.traverse_id
25 local node_new = node.direct.new
26 local node_copy = node.direct.copy
27 local node_hpack = node.direct.hpack
28 local node_next = node.direct.getnext
29 local node_free = node.direct.free
30 local node_prev = node.direct.getprev
31 local node_tail = node.direct.tail
32 local has_attr = node.direct.has_attribute
33 local set_attr = node.direct.set_attribute
34 local insert_after = node.direct.insert_after
35
36 local id_glyph = node.id('glyph')
37 local id_kern = node.id('kern')
38 local id_hlist = node.id('hlist')
39 local id_glue  = node.id('glue')
40 local id_whatsit = node.id('whatsit')
41 local attr_icflag = luatexbase.attributes['ltj@icflag']
42 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
43 local lang_ja = luatexja.lang_ja
44
45 local ltjf_font_metric_table = ltjf.font_metric_table
46 local round, pairs = tex.round, pairs
47
48 local PACKED       = luatexja.icflag_table.PACKED
49 local FROM_JFM     = luatexja.icflag_table.FROM_JFM
50 local KANJI_SKIP   = luatexja.icflag_table.KANJI_SKIP
51 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
52 local XKANJI_SKIP  = luatexja.icflag_table.XKANJI_SKIP
53 local XKANJI_SKIP_JFM  = luatexja.icflag_table.XKANJI_SKIP_JFM
54
55 local priority_table = {
56    FROM_JFM + 2,
57    FROM_JFM + 1,
58    FROM_JFM,
59    FROM_JFM - 1,
60    FROM_JFM - 2,
61    XKANJI_SKIP,
62    KANJI_SKIP
63 }
64
65 local get_attr_icflag
66 do
67    local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
68    get_attr_icflag = function(p)
69       return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
70    end
71 end
72
73 -- box 内で伸縮された glue の合計値を計算
74
75 local function get_stretched(q, go, gs)
76    if gs == 1 then -- stretching
77       if getfield(q, 'stretch_order') == go then
78          return getfield(q, 'stretch')
79       else return 0
80       end
81    else -- shrinking
82       if getfield(q, 'shrink_order') == go then
83          return getfield(q, 'shrink')
84       else return 0
85       end
86    end
87 end
88
89 local res = {}
90 local gs_used_line = {}
91 local function get_total_stretched(p, line)
92    local go, gf, gs
93       = getfield(p, 'glue_order'), getfield(p, 'glue_set'), getfield(p, 'glue_sign')
94    if go ~= 0 then return nil end
95    res[0], res.glue_set, res.name = 0, gf, (gs==1) and 'stretch' or 'shrink'
96    for i=1,#priority_table do res[priority_table[i]]=0 end
97    if gs ~= 1 and gs ~= 2 then return res, 0 end
98    local total = 0
99    for q in node_traverse_id(id_glue, getlist(p)) do
100       local a, ic = get_stretched(q, go, gs), get_attr_icflag(q)
101       if ic == KANJI_SKIP_JFM  then ic = KANJI_SKIP
102       elseif ic == XKANJI_SKIP_JFM  then ic = XKANJI_SKIP
103       end
104       if   type(res[ic]) == 'number' then
105          -- kanjiskip, xkanjiskip は段落内で spec を共有しているが,
106          -- それはここでは望ましくないので,各 glue ごとに異なる spec を使う.
107          -- 本当は各行ごとに glue_spec を共有させたかったが,安直にやると
108          -- ref_count が 0 なので Double-free が発生する.どうする?
109          -- JFM グルーはそれぞれ異なる glue_spec を用いているので,問題ない.
110          if (ic == KANJI_SKIP or ic == XKANJI_SKIP) and getsubtype(q)==0 then
111             local qs = getfield(q, 'spec')
112             if is_zero_glue(q) then
113                if (gs_used_line[qs] or 0)<line  then
114                   setfield(q, 'spec', node_copy(qs))
115                   local f = node_new(id_glue); setfield(f, 'spec', qs); node_free(f)
116                   -- decrese qs's reference count
117                else
118                   gs_used_line[qs] = line
119                end
120             end
121          end
122          res[ic], total = res[ic] + a, total + a
123       else
124          res[0], total = res[0]  + a, total + a
125       end
126    end
127    return res, total
128 end
129
130 local function clear_stretch(p, ic, name)
131    for q in node_traverse_id(id_glue, getlist(p)) do
132       local f = get_attr_icflag(q)
133       if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM))
134            or ((ic ==XKANJI_SKIP) and (f == XKANJI_SKIP_JFM)) then
135          local qs = getfield(q, 'spec')
136          if getfield(qs, 'writable') then
137             setfield(qs, name..'_order', 0)
138             setfield(qs, name, 0)
139          end
140       end
141    end
142 end
143
144 local set_stretch_table = {}
145 local function set_stretch(p, after, before, ic, name)
146    if before > 0 then
147       local ratio = after/before
148       for i,_ in pairs(set_stretch_table) do
149          set_stretch_table[i] = nil
150       end
151       for q in node_traverse_id(id_glue, getlist(p)) do
152          local f = get_attr_icflag(q)
153          if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM))
154            or ((ic ==XKANJI_SKIP) and (f == XKANJI_SKIP_JFM)) then
155             local qs, do_flag = getfield(q, 'spec'), true
156             for i=1,#set_stretch_table do
157                if set_stretch_table[i]==qs then do_flag = false end
158             end
159             if getfield(qs, 'writable') and getfield(qs, name..'_order')==0 and do_flag then
160                setfield(q, name, getfield(qs, name)*ratio)
161                set_stretch_table[#set_stretch_table+1] = qs
162             end
163          end
164       end
165    end
166 end
167
168 -- step 1: 行末に kern を挿入(句読点,中点用)
169 local ltjd_glyph_from_packed = ltjd.glyph_from_packed
170 local function aw_step1(p, res, total)
171    local head = getlist(p)
172    local x = node_tail(head); if not x then return false end
173    -- x: \rightskip
174    x = node_prev(x); if not x then return false end
175    local xi, xc = getid(x)
176    if xi == id_glue and getsubtype(x) == 15 then
177       -- 段落最終行のときは,\penalty10000 \parfillskip が入るので,
178       -- その前の node が本来の末尾文字となる
179       x = node_prev(node_prev(x)); xi = getid(x)
180    end
181    -- local xi = getid(x)
182    -- while (get_attr_icflag(x) == PACKED)
183    --    and  ((xi == id_penalty) or (xi == id_kern) or (xi == id_kern)) do
184    --       x = node_prev(x); xi = getid(x)
185    -- end
186    if xi == id_glyph and getfield(x, 'lang')==lang_ja then
187       -- 和文文字
188       xc = x
189    elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
190       -- packed JAchar
191       xc = ltjd_glyph_from_packed(x)
192       while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
193    else
194      return false-- それ以外は対象外.
195    end
196    local xk = ltjf_font_metric_table[getfont(xc)]
197      .char_type[has_attr(xc, attr_jchar_class) or 0]['end_' .. res.name] or 0
198
199    if xk>0 and total>=xk then
200       total = total - xk
201       local kn = node_new(id_kern)
202       setfield(kn, 'kern', (res.name=='shrink' and -1 or 1) * xk)
203       set_attr(kn, attr_icflag, FROM_JFM)
204       insert_after(head, x, kn)
205       return true
206    else return false
207    end
208 end
209
210 -- step 2: 行中の glue を変える
211 local function aw_step2(p, res, total, added_flag)
212    if total == 0 then -- もともと伸縮の必要なし
213       if added_flag then -- 行末に kern 追加したので,それによる補正
214          local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
215          setfield(f, 'head', nil)
216          setfield(p, 'glue_set', getfield(f, 'glue_set'))
217          setfield(p, 'glue_order', getfield(f, 'glue_order'))
218          setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
219          node_free(f)
220          return
221       end
222    elseif total <= res[0] then -- 和文処理グルー以外で足りる
223       for _,v in pairs(priority_table) do clear_stretch(p, v, res.name) end
224       local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
225       setfield(f, 'head', nil)
226       setfield(p, 'glue_set', getfield(f, 'glue_set'))
227       setfield(p, 'glue_order', getfield(f, 'glue_order'))
228       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
229       node_free(f)
230    else
231       total = total - res[0]
232       for i = 1, #priority_table do
233          local v = priority_table[i]
234          if total <= res[v] then
235             for j = i+1,#priority_table do
236                clear_stretch(p, priority_table[j], res.name)
237             end
238             set_stretch(p, total, res[v], v, res.name); break
239          end
240          total = total - res[v]
241       end
242       local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
243       setfield(f, 'head', nil)
244       setfield(p, 'glue_set', getfield(f, 'glue_set'))
245       setfield(p, 'glue_order', getfield(f, 'glue_order'))
246       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
247       node_free(f)
248    end
249 end
250
251
252 local ltjs_fast_get_stack_skip = ltjs.fast_get_stack_skip
253 local function adjust_width(head)
254    if not head then return head end
255    local line = 1
256    for p in node_traverse_id(id_hlist, to_direct(head)) do
257       line = line + 1
258       local res, total = get_total_stretched(p, line)
259         -- this is the same table as the table which is def'd in l. 92
260       if res and res.glue_set<1 then
261          total = round(total * res.glue_set)
262          aw_step2(p, res, total, aw_step1(p, res, total))
263       end
264    end
265    for i,_ in pairs(gs_used_line) do
266       gs_used_line[i]  = nil
267    end
268    return to_node(head)
269 end
270
271 do
272    luatexja.adjust = luatexja.adjust or {}
273    local is_reg = false
274    function luatexja.adjust.enable_cb()
275       if not is_reg then
276          luatexbase.add_to_callback('post_linebreak_filter',
277                                     adjust_width, 'Adjust width', 100)
278          is_reg = true
279       end
280    end
281    function luatexja.adjust.disable_cb()
282       if is_reg then
283          luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
284          is_reg = false
285       end
286    end
287 end
288
289 luatexja.unary_pars.adjust = function(t)
290    return is_reg and 1 or 0
291 end