OSDN Git Service

4e335289b74994f15a5cfba8f4dc32afe9cbb311
[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 -- 行末用
204 local function aw_step1_last(p, total)
205    local head = getlist(p)
206    local x = node_tail(head); if not x then return total, false end
207    -- x: \rightskip
208    pf = node_prev(x); if not x then return total, false end
209    if getid(pf) ~= id_glue or getsubtype(pf) ~= 15 then return total, false end
210    x = node_prev(node_prev(pf)); xi = getid(x)
211    local xi, xc = getid(x)
212    if xi == id_glyph and getfield(x, 'lang')==lang_ja then
213       -- 和文文字
214       xc = x
215    elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
216       -- packed JAchar
217       xc = ltjd_glyph_from_packed(x)
218       while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
219    else
220       return total, false-- それ以外は対象外.
221    end
222    -- 続行条件1:無限の伸縮度を持つグルーは \parfillskipのみ
223    if total>0 and total_st.order>0 then
224       if total_st.order ~= getfield(pf, 'stretch_order') then return total, false end
225       if total_st[total_st.order*65536] ~= getfield(pf, 'stretch') then return total, false end
226       for i=total_st.order-1, 1, -1 do
227          if total_st[i*65536] ~= 0 then return total, false end
228       end
229    end
230    if total<0 and total_sh.order>0 then
231       if total_sh.order ~= getfield(pf, 'shrink_order') then return total, false end
232       if total_sh[total_sh.order*65536] ~= getfield(pf, 'shrink') then return total, false end
233       for i=total_sh.order-1, 1, -1 do
234          if total_sh[i*65536] ~= 0 then return total, false end
235       end
236    end
237    local eadt = ltjf_font_metric_table[getfont(xc)]
238       .char_type[has_attr(xc, attr_jchar_class) or 0].end_adjust
239    if not eadt then 
240       return total, false
241    end
242    -- 続行条件2: eadt[1]<= \parfillskip <= eadt[#eadt]
243    local pfw = getfield(pf, 'width') 
244      + (total>0 and getfield(pf, 'stretch') or -getfield(pf, 'shrink')) *getfield(p, 'glue_set') 
245    if pfw<eadt[1] or eadt[#eadt]<pfw then return total, false end
246    -- \parfillskip を 0 にする
247    total = total + getfield(pf, 'width') 
248    total_st.order, total_sh.order = 0, 0
249    if getfield(pf, 'stretch_order')==0 then 
250       total_st[0] = total_st[0] - getfield(pf, 'stretch') 
251       total_st[-1] = total_st[-1] - getfield(pf, 'stretch') 
252       total_st.order = (total_st[0]==0) and -1 or 0
253    end
254    if getfield(pf, 'shrink_order')==0 then 
255       total_sh[0] = total_sh[0] - getfield(pf, 'shrink') 
256       total_sh[-1] = total_sh[-1] - getfield(pf, 'shrink') 
257       total_sh.order = (total_sh[0]==0) and -1 or 0
258    end
259    setfield(pf, 'subtype', 1); setglue(pf)
260    local eadt_ratio = {}
261    for i, v in ipairs(eadt) do
262       local t = total - v
263       if t>0 then
264          eadt_ratio[i] = {i, t/total_st[65536*total_st.order], t, v}
265       else
266          eadt_ratio[i] = {i, t/total_sh[65536*total_sh.order], t, v}
267       end
268    end
269    table.sort(eadt_ratio, 
270    function (a,b) 
271        for i=2,4 do
272            local at, bt = abs(a[i]), abs(b[i])
273            if at~=bt then return at<bt end
274        end
275        return a[4]<b[4]
276    end)
277    if eadt[eadt_ratio[1][1]]~=0 then
278       local kn = node_new(id_kern, 1)
279       setfield(kn, 'kern', eadt[eadt_ratio[1][1]]); set_attr(kn, attr_icflag, LINEEND)
280       insert_after(head, x, kn)
281       return eadt_ratio[1][3], true
282    else
283       return total, false
284    end
285 end
286
287
288 -- step 2: 行中の glue を変える
289 local function aw_step2_dummy(p, _, added_flag)
290    if added_flag then -- 行末に kern 追加したので,それによる補正
291       local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
292       setfield(f, 'head', nil)
293       setfield(p, 'glue_set', getfield(f, 'glue_set'))
294       setfield(p, 'glue_order', getfield(f, 'glue_order'))
295       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
296       node_free(f)
297       return
298    end
299 end
300 local function aw_step2(p, total, added_flag)
301    local name = (total>0) and 'stretch' or 'shrink'
302    local res = total_stsh[(total>0) and 1 or 2]
303    if total==0 or res.order > 0 then 
304       -- もともと伸縮の必要なしか,残りの伸縮量は無限大
305       if added_flag then -- 行末に kern 追加したので,それによる補正
306          local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
307          setfield(f, 'head', nil)
308          setfield(p, 'glue_set', getfield(f, 'glue_set'))
309          setfield(p, 'glue_order', getfield(f, 'glue_order'))
310          setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
311          node_free(f)
312          return
313       end
314    end
315    total = math.abs(total)
316    if total <= res[-1] then -- 和文処理グルー以外で足りる
317       for _,v in pairs(priority_table) do clear_stretch(p, v, name) end
318       local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
319       setfield(f, 'head', nil)
320       setfield(p, 'glue_set', getfield(f, 'glue_set'))
321       setfield(p, 'glue_order', getfield(f, 'glue_order'))
322       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
323       node_free(f)
324    else
325       total = total - res[-1]; 
326       for i = 1, #priority_table do
327          local v = priority_table[i]
328          if total <= res[v] then
329             for j = i+1,#priority_table do
330                clear_stretch(p, priority_table[j], name)
331             end
332             set_stretch(p, total, res[v], v, name); break
333          end
334          total = total - res[v]
335       end
336       local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
337       setfield(f, 'head', nil)
338       setfield(p, 'glue_set', getfield(f, 'glue_set'))
339       setfield(p, 'glue_order', getfield(f, 'glue_order'))
340       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
341       node_free(f)
342    end
343 end
344
345
346 local insert_lineend_kern
347 do
348    local id_penalty = node.id('penalty')
349    local insert_before = node.direct.insert_before
350    local KINSOKU      = luatexja.icflag_table.KINSOKU
351    function insert_lineend_kern(head, nq, np, Bp)
352       if nq.met then 
353          local eadt = nq.met.char_type[nq.class].end_adjust
354          if not eadt then return end
355          if eadt[1]~=0 then
356             local x = node_new(id_kern, 1)
357             setfield(x, 'kern', eadt[1]); set_attr(x, attr_icflag, LINEEND)
358             insert_before(head, np.first, x)
359          end
360          for i=2,#eadt do
361             local x = node_new(id_penalty)
362             setfield(x, 'penalty', 0); set_attr(x, attr_icflag, KINSOKU)
363             insert_before(head, np.first, x); Bp[#Bp+1] = x
364             local x = node_new(id_kern, 1)
365             setfield(x, 'kern', eadt[i]-eadt[i-1]); set_attr(x, attr_icflag, LINEEND)
366             insert_before(head, np.first, x)
367          end
368          if #eadt>1 or eadt[1]~=0 then
369             local x = node_new(id_penalty)
370             setfield(x, 'penalty', 0); set_attr(x, attr_icflag, KINSOKU)
371             insert_before(head, np.first, x); Bp[#Bp+1] = x
372             local x = node_new(id_kern, 1)
373             setfield(x, 'kern', -eadt[#eadt]); set_attr(x, attr_icflag, LINEEND)
374             insert_before(head, np.first, x)
375             local x = node_new(id_penalty)
376             setfield(x, 'penalty', 10000); set_attr(x, attr_icflag, KINSOKU)
377             insert_before(head, np.first, x); Bp[#Bp+1] = x
378          end
379       end
380    end
381 end
382
383 local adust_width
384 do
385    local myaw_atep1, myaw_step2, myaw_step1_last
386    local dummy =  function(p,t,n) return t, false end
387    local ltjs_fast_get_stack_skip = ltjs.fast_get_stack_skip
388    function adjust_width(head)
389       if not head then return head end
390       local last_p
391       for p in node_traverse_id(id_hlist, to_direct(head)) do
392          if last_p then
393             myaw_step2(last_p, myaw_step1(last_p, get_total_stretched(last_p)))
394          end
395          last_p = p
396       end
397       if last_p then
398          myaw_step2(last_p, myaw_step1_last(last_p, get_total_stretched(last_p)))
399       end
400       return to_node(head)
401    end
402    local is_reg = false
403    function enable_cb(status_le, status_pr)
404       if (status_le>0 or status_pr>0) and (not is_reg) then
405          luatexbase.add_to_callback('post_linebreak_filter',
406                                     adjust_width, 'Adjust width', 100)
407          is_reg = true
408       elseif is_reg and (status_le==0 and status_pr==0) then
409          luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
410          is_reg = false
411       end
412       myaw_step1 = dummy --(status%2>0) and aw_step1 or dummy
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