OSDN Git Service

a782a8e1b8f56ea975d8d072a4d17b91d12be5d7
[luatex-ja/luatexja.git] / src / ltj-adjust.lua
1 --
2 -- ltj-adjust.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 getglue = luatexja.getglue
15 local getfield = node.direct.getfield
16 local is_zero_glue = node.direct.is_zero_glue
17 local getlist = node.direct.getlist
18 local getid = node.direct.getid
19 local getfont = node.direct.getfont
20 local getsubtype = node.direct.getsubtype
21
22 local node_traverse_id = node.direct.traverse_id
23 local node_new = node.direct.new
24 local node_copy = node.direct.copy
25 local node_hpack = node.direct.hpack
26 local node_next = node.direct.getnext
27 local node_free = node.direct.free
28 local node_prev = node.direct.getprev
29 local node_tail = node.direct.tail
30 local has_attr = node.direct.has_attribute
31 local set_attr = node.direct.set_attribute
32 local insert_after = node.direct.insert_after
33
34 local id_glyph = node.id('glyph')
35 local id_kern = node.id('kern')
36 local id_hlist = node.id('hlist')
37 local id_glue  = node.id('glue')
38 local id_whatsit = node.id('whatsit')
39 local attr_icflag = luatexbase.attributes['ltj@icflag']
40 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
41 local lang_ja = luatexja.lang_ja
42
43 local ltjf_font_metric_table = ltjf.font_metric_table
44 local round, pairs = tex.round, pairs
45
46 local PACKED       = luatexja.icflag_table.PACKED
47 local LINEEND      = luatexja.icflag_table.LINEEND
48 local FROM_JFM     = luatexja.icflag_table.FROM_JFM
49 local KANJI_SKIP   = luatexja.icflag_table.KANJI_SKIP
50 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
51 local XKANJI_SKIP  = luatexja.icflag_table.XKANJI_SKIP
52 local XKANJI_SKIP_JFM  = luatexja.icflag_table.XKANJI_SKIP_JFM
53
54 local priority_table = {
55    FROM_JFM + 2,
56    FROM_JFM + 1,
57    FROM_JFM,
58    FROM_JFM - 1,
59    FROM_JFM - 2,
60    XKANJI_SKIP,
61    KANJI_SKIP
62 }
63
64 local get_attr_icflag
65 do
66    local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
67    get_attr_icflag = function(p)
68       return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
69    end
70 end
71
72 -- box 内で伸縮された glue の合計値を計算
73
74 local total_stsh = {{},{}}
75 local total_st, total_sh = total_stsh[1], total_stsh[2]
76 local function get_total_stretched(p)
77 -- return value: <補正値(sp)>
78    local go, gf, gs
79      = getfield(p, 'glue_order'), getfield(p, 'glue_set'), getfield(p, 'glue_sign')
80    for i,_ in pairs(total_st) do total_st[i]=nil; total_sh[i]=nil end
81    for i=1,#priority_table do 
82       total_st[priority_table[i]]=0; total_sh[priority_table[i]]=0; 
83    end
84    for i=0,4 do total_st[i*65536]=0; total_sh[i*65536]=0 end
85    total_st[-1]=0; total_sh[-1]=0;
86    local pf, pfw
87    for q in node_traverse_id(id_glue, getlist(p)) do
88       local a = getfield(q, 'stretch_order')
89       if a>0 then a=a*65536 else 
90          total_st[0] = total_st[0]+getfield(q, 'stretch')
91          a = get_attr_icflag(q)
92          if a == KANJI_SKIP_JFM  then a = KANJI_SKIP
93          elseif a == XKANJI_SKIP_JFM  then a = XKANJI_SKIP
94          elseif type(total_st[a])~='number' then a = -1 end
95       end
96       total_st[a] = total_st[a]+getfield(q, 'stretch')
97       local a = getfield(q, 'shrink_order')
98       if a>0 then a=a*65536 else 
99          total_sh[0] = total_sh[0]+getfield(q, 'shrink')
100          a = get_attr_icflag(q)
101          if a == KANJI_SKIP_JFM  then a = KANJI_SKIP
102          elseif a == XKANJI_SKIP_JFM  then a = XKANJI_SKIP
103          elseif type(total_sh[a])~='number' then a = -1 end
104       end
105       total_sh[a] = total_sh[a]+getfield(q, 'shrink')
106    end
107    for i=4,1,-1 do if total_st[i*65536]~=0 then total_st.order=i; break end; end
108    if not total_st.order then
109        total_st.order, total_st[-65536] = -1,0.1 -- dummy
110    end
111    for i=4,1,-1 do if total_sh[i*65536]~=0 then total_sh.order=i; break end; end
112    if not total_sh.order then
113        total_sh.order, total_sh[-65536] = -1,0.1 -- dummy
114    end
115    if gs==0 then
116       return 0
117    else 
118       return round((3-2*gs)*total_stsh[gs][go*65536]*gf)
119    end
120 end
121
122 local function clear_stretch(p, ic, name)
123    for q in node_traverse_id(id_glue, getlist(p)) do
124       local f = get_attr_icflag(q)
125       if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM))
126            or ((ic ==XKANJI_SKIP) and (f == XKANJI_SKIP_JFM)) then
127          setfield(q, name..'_order', 0)
128          setfield(q, name, 0)
129       end
130    end
131 end
132
133 local function set_stretch(p, after, before, ic, name)
134    if before > 0 then
135       local ratio = after/before
136       for q in node_traverse_id(id_glue, getlist(p)) do
137          local f = get_attr_icflag(q)
138          if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM))
139            or ((ic ==XKANJI_SKIP) and (f == XKANJI_SKIP_JFM)) then
140             if getfield(q, name..'_order')==0 then
141                setfield(q, name, getfield(q, name)*ratio)
142             end
143          end
144       end
145    end
146 end
147
148 -- step 1: 行末に kern を挿入(句読点,中点用)
149 local abs = math.abs
150 local ltjd_glyph_from_packed = ltjd.glyph_from_packed
151 local function aw_step1(p, total)
152    local head = getlist(p)
153    local x = node_tail(head); if not x then return total, false end
154    -- x: \rightskip
155    x = node_prev(x); if not x then return total, false end
156    local xi, xc = getid(x)
157    if (total>0 and total_st.order>0) or (total<0 and total_sh.order>0) then
158        -- 無限大のグルーで処理が行われているときは処理中止.
159        return total, false
160    end
161    if xi == id_glyph and getfield(x, 'lang')==lang_ja then
162       -- 和文文字
163       xc = x
164    elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
165       -- packed JAchar
166       xc = ltjd_glyph_from_packed(x)
167       while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
168    else
169       return total, false-- それ以外は対象外.
170    end
171    local eadt = ltjf_font_metric_table[getfont(xc)]
172       .char_type[has_attr(xc, attr_jchar_class) or 0].end_adjust
173    if not eadt then 
174       return total, false
175    end
176    local eadt_ratio = {}
177    for i, v in ipairs(eadt) do
178       local t = total - v
179       if t>0 then
180          eadt_ratio[i] = {i, t/total_st[65536*total_st.order], t, v}
181       else
182          eadt_ratio[i] = {i, t/total_sh[65536*total_sh.order], t, v}
183       end
184    end
185    table.sort(eadt_ratio, 
186    function (a,b) 
187        for i=2,4 do
188            local at, bt = abs(a[i]), abs(b[i])
189            if at~=bt then return at<bt end
190        end
191        return a[4]<b[4]
192    end)
193    if eadt[eadt_ratio[1][1]]~=0 then
194       local kn = node_new(id_kern, 1)
195       setfield(kn, 'kern', eadt[eadt_ratio[1][1]]); set_attr(kn, attr_icflag, LINEEND)
196       insert_after(head, x, kn)
197       return eadt_ratio[1][3], true
198    else
199       return total, false
200    end
201 end
202
203 -- step 1 行末用
204 local min = math.min
205 local function aw_step1_last(p, total)
206    local head = getlist(p)
207    local x = node_tail(head); if not x then return total, false end
208    -- x: \rightskip
209    pf = node_prev(x); if not x then return total, false end
210    if getid(pf) ~= id_glue or getsubtype(pf) ~= 15 then return total, false end
211    x = node_prev(node_prev(pf)); xi = getid(x)
212    local xi, xc = getid(x)
213    if xi == id_glyph and getfield(x, 'lang')==lang_ja then
214       -- 和文文字
215       xc = x
216    elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
217       -- packed JAchar
218       xc = ltjd_glyph_from_packed(x)
219       while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
220    else
221       return total, false-- それ以外は対象外.
222    end
223    -- 続行条件1:無限の伸縮度を持つグルーは \parfillskipのみ
224    if total>0 and total_st.order>0 then
225       if total_st.order ~= getfield(pf, 'stretch_order') then return total, false end
226       if total_st[total_st.order*65536] ~= getfield(pf, 'stretch') then return total, false end
227       for i=total_st.order-1, 1, -1 do
228          if total_st[i*65536] ~= 0 then return total, false end
229       end
230    end
231    if total<0 and total_sh.order>0 then
232       if total_sh.order ~= getfield(pf, 'shrink_order') then return total, false end
233       if total_sh[total_sh.order*65536] ~= getfield(pf, 'shrink') then return total, false end
234       for i=total_sh.order-1, 1, -1 do
235          if total_sh[i*65536] ~= 0 then return total, false end
236       end
237    end
238    local eadt = ltjf_font_metric_table[getfont(xc)]
239       .char_type[has_attr(xc, attr_jchar_class) or 0].end_adjust
240    if not eadt then 
241       return total, false
242    end
243    -- 続行条件2: eadt[1]<= \parfillskip <= eadt[#eadt]
244    local pfw = getfield(pf, 'width') 
245      + (total>0 and getfield(pf, 'stretch') or -getfield(pf, 'shrink')) *getfield(p, 'glue_set') 
246    if pfw<min(0,eadt[1]) or eadt[#eadt]<pfw then return total, false end
247    -- \parfillskip を 0 にする
248    total = total + getfield(pf, 'width') 
249    total_st.order, total_sh.order = 0, 0
250    if getfield(pf, 'stretch_order')==0 then 
251       total_st[0] = total_st[0] - getfield(pf, 'stretch') 
252       total_st[-1] = total_st[-1] - getfield(pf, 'stretch') 
253       total_st.order = (total_st[0]==0) and -1 or 0
254    end
255    if getfield(pf, 'shrink_order')==0 then 
256       total_sh[0] = total_sh[0] - getfield(pf, 'shrink') 
257       total_sh[-1] = total_sh[-1] - getfield(pf, 'shrink') 
258       total_sh.order = (total_sh[0]==0) and -1 or 0
259    end
260    setfield(pf, 'subtype', 1); setglue(pf)
261    local eadt_ratio = {}
262    for i, v in ipairs(eadt) do
263       local t = total - v
264       if t>0 then
265          eadt_ratio[i] = {i, t/total_st[65536*total_st.order], t, v}
266       else
267          eadt_ratio[i] = {i, t/total_sh[65536*total_sh.order], t, v}
268       end
269    end
270    table.sort(eadt_ratio, 
271    function (a,b) 
272        for i=2,4 do
273            local at, bt = abs(a[i]), abs(b[i])
274            if at~=bt then return at<bt end
275        end
276        return a[4]<b[4]
277    end)
278    if eadt[eadt_ratio[1][1]]~=0 then
279       local kn = node_new(id_kern, 1)
280       setfield(kn, 'kern', eadt[eadt_ratio[1][1]]); set_attr(kn, attr_icflag, LINEEND)
281       insert_after(head, x, kn)
282       return eadt_ratio[1][3], true
283    else
284       return total, false
285    end
286 end
287
288
289 -- step 2: 行中の glue を変える
290 local function aw_step2_dummy(p, _, added_flag)
291    if added_flag then -- 行末に kern 追加したので,それによる補正
292       local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
293       setfield(f, 'head', nil)
294       setfield(p, 'glue_set', getfield(f, 'glue_set'))
295       setfield(p, 'glue_order', getfield(f, 'glue_order'))
296       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
297       node_free(f)
298       return
299    end
300 end
301 local function aw_step2(p, total, added_flag)
302    local name = (total>0) and 'stretch' or 'shrink'
303    local res = total_stsh[(total>0) and 1 or 2]
304    if total==0 or res.order > 0 then 
305       -- もともと伸縮の必要なしか,残りの伸縮量は無限大
306       if added_flag then -- 行末に kern 追加したので,それによる補正
307          local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
308          setfield(f, 'head', nil)
309          setfield(p, 'glue_set', getfield(f, 'glue_set'))
310          setfield(p, 'glue_order', getfield(f, 'glue_order'))
311          setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
312          node_free(f)
313          return
314       end
315    end
316    total = abs(total)
317    if total <= res[-1] then -- 和文処理グルー以外で足りる
318       for _,v in pairs(priority_table) do clear_stretch(p, v, name) end
319       local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
320       setfield(f, 'head', nil)
321       setfield(p, 'glue_set', getfield(f, 'glue_set'))
322       setfield(p, 'glue_order', getfield(f, 'glue_order'))
323       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
324       node_free(f)
325    else
326       total = total - res[-1]; 
327       for i = 1, #priority_table do
328          local v = priority_table[i]
329          if total <= res[v] then
330             for j = i+1,#priority_table do
331                clear_stretch(p, priority_table[j], name)
332             end
333             set_stretch(p, total, res[v], v, name); break
334          end
335          total = total - res[v]
336       end
337       local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
338       setfield(f, 'head', nil)
339       setfield(p, 'glue_set', getfield(f, 'glue_set'))
340       setfield(p, 'glue_order', getfield(f, 'glue_order'))
341       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
342       node_free(f)
343    end
344 end
345
346 -- step 1': lineend=extended の場合(行分割時に考慮))
347 local insert_lineend_kern
348 do
349    local id_penalty = node.id('penalty')
350    local insert_before = node.direct.insert_before
351    local KINSOKU      = luatexja.icflag_table.KINSOKU
352    function insert_lineend_kern(head, nq, np, Bp)
353       if nq.met then 
354          local eadt = nq.met.char_type[nq.class].end_adjust
355          if not eadt then return end
356          if eadt[1]~=0 then
357             local x = node_new(id_kern, 1)
358             setfield(x, 'kern', eadt[1]); set_attr(x, attr_icflag, LINEEND)
359             insert_before(head, np.first, x)
360          end
361          for i=2,#eadt do
362             local x = node_new(id_penalty)
363             setfield(x, 'penalty', 0); set_attr(x, attr_icflag, KINSOKU)
364             insert_before(head, np.first, x); Bp[#Bp+1] = x
365             local x = node_new(id_kern, 1)
366             setfield(x, 'kern', eadt[i]-eadt[i-1]); set_attr(x, attr_icflag, LINEEND)
367             insert_before(head, np.first, x)
368          end
369          if #eadt>1 or eadt[1]~=0 then
370             local x = node_new(id_penalty)
371             setfield(x, 'penalty', 0); set_attr(x, attr_icflag, KINSOKU)
372             insert_before(head, np.first, x); Bp[#Bp+1] = x
373             local x = node_new(id_kern, 1)
374             setfield(x, 'kern', -eadt[#eadt]); set_attr(x, attr_icflag, LINEEND)
375             insert_before(head, np.first, x)
376             local x = node_new(id_penalty)
377             setfield(x, 'penalty', 10000); set_attr(x, attr_icflag, KINSOKU)
378             insert_before(head, np.first, x); Bp[#Bp+1] = x
379          end
380       end
381    end
382 end
383
384 local adust_width
385 do
386    local myaw_atep1, myaw_step2, myaw_step1_last
387    local dummy =  function(p,t,n) return t, false end
388    local ltjs_fast_get_stack_skip = ltjs.fast_get_stack_skip
389    function adjust_width(head)
390       if not head then return head end
391       local last_p
392       for p in node_traverse_id(id_hlist, to_direct(head)) do
393          if last_p then
394             myaw_step2(last_p, myaw_step1(last_p, get_total_stretched(last_p)))
395          end
396          last_p = p
397       end
398       if last_p then
399          myaw_step2(last_p, myaw_step1_last(last_p, get_total_stretched(last_p)))
400       end
401       return to_node(head)
402    end
403    local is_reg = false
404    function enable_cb(status_le, status_pr)
405       if (status_le>0 or status_pr>0) and (not is_reg) then
406          luatexbase.add_to_callback('post_linebreak_filter',
407                                     adjust_width, 'Adjust width', 100)
408          is_reg = true
409       elseif is_reg and (status_le==0 and status_pr==0) then
410          luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
411          is_reg = false
412       end
413       if status_le==2 then
414          if not luatexbase.in_callback('luatexja.adjust_jfmglue', 'luatexja.adjust') then
415             luatexbase.add_to_callback('luatexja.adjust_jfmglue', insert_lineend_kern, 'luatexja.adjust')
416          end
417          myaw_step1, myaw_step1_last = dummy, aw_step1_last
418       else
419          if status_le==0 then
420             myaw_step1, myaw_step1_last = dummy, dummy
421          else
422             myaw_step1, myaw_step1_last = aw_step1, aw_step1_last
423          end
424          if luatexbase.in_callback('luatexja.adjust_jfmglue', 'luatexja.adjust') then
425             luatexbase.remove_from_callback('luatexja.adjust_jfmglue', 'luatexja.adjust')
426          end
427       end
428       myaw_step2 = (status_pr>0) and aw_step2 or aw_step2_dummy
429    end
430    function disable_cb() -- only for compatibility
431        enable_cs(0)
432    end
433    luatexja.adjust = luatexja.adjust or {enable_cb=enable_cb, disable_cb=disable_cb}  
434 end
435
436 luatexja.unary_pars.adjust = function(t)
437    return is_reg and 1 or 0
438 end