OSDN Git Service

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