OSDN Git Service

typo
[luatex-ja/luatexja.git] / src / ltj-adjust.lua
1 --
2 -- ltj-adjust.lua
3 --
4 luatexja.load_module 'base';      local ltjb = luatexja.base
5 luatexja.load_module 'jfont';     local ltjf = luatexja.jfont
6 luatexja.load_module 'jfmglue';   local ltjj = luatexja.jfmglue
7 luatexja.load_module 'stack';     local ltjs = luatexja.stack
8 luatexja.load_module 'direction'; local ltjd = luatexja.direction
9 luatexja.load_module 'lineskip';  local ltjl = luatexja.lineskip
10 luatexja.adjust = luatexja.adjust or {}
11
12 ----
13 local combine_voiced_kana
14 do
15     local uchar = utf.char
16     local cd, cp = uchar(0x3099), uchar(0x309A)
17     local substituter = (utf.substituter or utf.subtituter)      -- typo in lualibs?
18     {
19       ['ウ'..cd] = 'ヴ', ['う'..cd] = uchar(0x30F4),
20       ['か'..cd] = 'が', ['カ'..cd] = 'ガ',
21       ['き'..cd] = 'ぎ', ['キ'..cd] = 'ギ',
22       ['く'..cd] = 'ぐ', ['ク'..cd] = 'グ',
23       ['け'..cd] = 'げ', ['ケ'..cd] = 'ゲ',
24       ['こ'..cd] = 'ご', ['コ'..cd] = 'ゴ',
25       --
26       ['さ'..cd] = 'ざ', ['サ'..cd] = 'ザ',
27       ['し'..cd] = 'じ', ['シ'..cd] = 'ジ',
28       ['す'..cd] = 'ず', ['ス'..cd] = 'ズ',
29       ['せ'..cd] = 'ぜ', ['セ'..cd] = 'ゼ',
30       ['そ'..cd] = 'ぞ', ['ソ'..cd] = 'ゾ',
31       --
32       ['た'..cd] = 'だ', ['タ'..cd] = 'ダ',
33       ['ち'..cd] = 'ぢ', ['チ'..cd] = 'ヂ',
34       ['つ'..cd] = 'づ', ['ツ'..cd] = 'ヅ',
35       ['て'..cd] = 'で', ['テ'..cd] = 'デ',
36       ['と'..cd] = 'ど', ['ト'..cd] = 'ド',
37       --
38       ['は'..cd] = 'ば', ['ハ'..cd] = 'バ', ['は'..cp] = 'ぱ', ['ハ'..cp] = 'パ',
39       ['ひ'..cd] = 'び', ['ヒ'..cd] = 'ビ', ['ひ'..cp] = 'ぴ', ['ヒ'..cp] = 'ピ',
40       ['ふ'..cd] = 'ぶ', ['フ'..cd] = 'ブ', ['ふ'..cp] = 'ぷ', ['フ'..cp] = 'プ',
41       ['へ'..cd] = 'べ', ['ヘ'..cd] = 'ベ', ['へ'..cp] = 'ぺ', ['ヘ'..cp] = 'ペ',
42       ['ほ'..cd] = 'ぼ', ['ホ'..cd] = 'ボ', ['ほ'..cp] = 'ぽ', ['ホ'..cp] = 'ポ',
43       --
44       ['ゝ'..cd] = 'ゞ', ['ヽ'..cd] = 'ヾ',
45       ['ワ'..cd] = uchar(0x30F7), ['ヰ'..cd] = uchar(0x30F8),
46       ['ヱ'..cd] = uchar(0x30F9), ['ヲ'..cd] = uchar(0x30FA),
47     }
48     combine_voiced_kana = function(buffer) return substituter(buffer) end
49 end
50 ----
51
52 local to_node = node.direct.tonode
53 local to_direct = node.direct.todirect
54
55 local setfield = node.direct.setfield
56 local setglue = luatexja.setglue
57 local getfield = node.direct.getfield
58 local getlist = node.direct.getlist
59 local getid = node.direct.getid
60 local getfont = node.direct.getfont
61 local getsubtype = node.direct.getsubtype
62
63 local node_traverse_id = node.direct.traverse_id
64 local node_new = node.direct.new
65 local node_next = node.direct.getnext
66 local node_free = node.direct.free
67 local node_prev = node.direct.getprev
68 local node_tail = node.direct.tail
69 local has_attr = node.direct.has_attribute
70 local set_attr = node.direct.set_attribute
71 local insert_after = node.direct.insert_after
72
73 local id_glyph   = node.id 'glyph'
74 local id_kern    = node.id 'kern'
75 local id_hlist   = node.id 'hlist'
76 local id_glue    = node.id 'glue'
77 local id_whatsit = node.id 'whatsit'
78 local id_penalty = node.id 'penalty'
79 local attr_icflag = luatexbase.attributes['ltj@icflag']
80 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
81 local lang_ja = luatexja.lang_ja
82
83 local ltjf_font_metric_table = ltjf.font_metric_table
84 local ipairs, pairs = ipairs, pairs
85
86 local PACKED       = luatexja.icflag_table.PACKED
87 local LINEEND      = luatexja.icflag_table.LINEEND
88 local FROM_JFM     = luatexja.icflag_table.FROM_JFM
89 local KANJI_SKIP   = luatexja.icflag_table.KANJI_SKIP
90 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
91 local XKANJI_SKIP  = luatexja.icflag_table.XKANJI_SKIP
92 local XKANJI_SKIP_JFM  = luatexja.icflag_table.XKANJI_SKIP_JFM
93
94 local get_attr_icflag
95 do
96    local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
97    get_attr_icflag = function(p)
98       return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
99    end
100 end
101
102 local priority_num = { 0, 0 }
103 local at2pr = { {}, {} }
104 local at2pr_st, at2pr_sh = at2pr[1], at2pr[2]
105 do
106    local priority_table = {{},{}}
107    luatexja.adjust.priority_table = priority_table
108    local tmp = {}
109    local function cmp(a,b) return a[1]>b[1] end -- 大きいほうが先!
110    local function make_priority_table(glue_sign)
111       for i,_ in pairs(tmp) do tmp[i]=nil end
112       if glue_sign==2 then -- shrink
113          for i=0,63 do tmp[#tmp+1] = { (i%8)-4, FROM_JFM+i } end
114       else -- stretch
115          for i=0,63 do tmp[#tmp+1] = { math.floor(i/8)-4, FROM_JFM+i } end
116       end    
117       local pt = priority_table[glue_sign]
118       tmp[#tmp+1] = { pt[2]/10, XKANJI_SKIP }
119       tmp[#tmp+1] = { pt[2]/10, XKANJI_SKIP_JFM }
120       tmp[#tmp+1] = { pt[1]/10, KANJI_SKIP }
121       tmp[#tmp+1] = { pt[1]/10, KANJI_SKIP_JFM }
122       tmp[#tmp+1] = { pt[3]/10, -1 }
123       table.sort(tmp, cmp)
124       local a, m, n = at2pr[glue_sign], 10000000, 0
125       for i=1,#tmp do
126          if tmp[i][1]<m then n,m = n+1,tmp[i][1] end
127          a[tmp[i][2]] = n
128       end
129       local o = a[-1]
130       priority_num[glue_sign] = n
131       setmetatable(a, {__index = function () return o end })
132    end
133    luatexja.adjust.make_priority_table = make_priority_table
134 end
135
136 -- box 内で伸縮された glue の合計値を計算
137
138 local total_stsh = {{},{}}
139 local total_st, total_sh = total_stsh[1], total_stsh[2]
140 local get_total_stretched
141 do
142 local dimensions = node.direct.dimensions
143 function get_total_stretched(p)
144 -- return value: <補正値(sp)>
145    local ph = getlist(p)
146    if not ph then return 0 end
147    for i,_ in pairs(total_st) do total_st[i]=nil; total_sh[i]=nil end
148    for i=1,priority_num[1] do total_st[i]=0 end
149    for i=1,priority_num[2] do total_sh[i]=0 end
150    for i=0,4 do total_st[i*65536]=0; total_sh[i*65536]=0 end
151    for q in node_traverse_id(id_glue, ph) do
152       local a = getfield(q, 'stretch_order')
153       if a==0 then
154          local b = at2pr_st[get_attr_icflag(q)]; 
155          total_st[b] = total_st[b]+getfield(q, 'stretch')
156       end
157       total_st[a*65536] = total_st[a]+getfield(q, 'stretch')
158       local a = getfield(q, 'shrink_order')
159       if a==0 then
160          local b = at2pr_sh[get_attr_icflag(q)]; 
161          total_sh[b] = total_sh[b]+getfield(q, 'shrink')
162       end
163       total_sh[a*65536] = total_sh[a]+getfield(q, 'shrink')
164    end
165    for i=4,1,-1 do if total_st[i*65536]~=0 then total_st.order=i; break end; end
166    if not total_st.order then
167        total_st.order, total_st[-65536] = -1,0.1 -- dummy
168    end
169    for i=4,1,-1 do if total_sh[i*65536]~=0 then total_sh.order=i; break end; end
170    if not total_sh.order then
171        total_sh.order, total_sh[-65536] = -1,0.1 -- dummy
172    end
173    return getfield(p,'width') - dimensions(ph)
174 end
175 end
176
177 -- step 1: 行末に kern を挿入(句読点,中点用)
178 local abs = math.abs
179 local ltjd_glyph_from_packed = ltjd.glyph_from_packed
180 local function aw_step1(p, total)
181    local head = getlist(p)
182    local x = node_tail(head); if not x then return total, false end
183    -- x: \rightskip
184    x = node_prev(x); if not x then return total, false end
185    local xi, xc = getid(x)
186    -- x may be penalty
187    while xi==id_penalty do
188       x = node_prev(x); if not x then return total, false end
189       xi = getid(x)
190    end
191    if (total>0 and total_st.order>0) or (total<0 and total_sh.order>0) then
192        -- 無限大のグルーで処理が行われているときは処理中止.
193        return total, false
194    end
195    if xi == id_glyph and getfield(x, 'lang')==lang_ja then
196       -- 和文文字
197       xc = x
198    elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
199       -- packed JAchar
200       xc = ltjd_glyph_from_packed(x)
201       while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
202    else
203       return total, false-- それ以外は対象外.
204    end
205    local eadt = ltjf_font_metric_table[getfont(xc)]
206       .char_type[has_attr(xc, attr_jchar_class) or 0].end_adjust
207    if not eadt then 
208       return total, false
209    end
210    local eadt_ratio = {}
211    for i, v in ipairs(eadt) do
212       local t = total - v
213       if t>0 then
214          eadt_ratio[i] = {i, t/total_st[65536*total_st.order], t, v}
215       else
216          eadt_ratio[i] = {i, t/total_sh[65536*total_sh.order], t, v}
217       end
218    end
219    table.sort(eadt_ratio, 
220    function (a,b) 
221        for i=2,4 do
222            local at, bt = abs(a[i]), abs(b[i])
223            if at~=bt then return at<bt end
224        end
225        return a[4]<b[4]
226    end)
227    if eadt[eadt_ratio[1][1]]~=0 then
228       local kn = node_new(id_kern, 1)
229       setfield(kn, 'kern', eadt[eadt_ratio[1][1]]); set_attr(kn, attr_icflag, LINEEND)
230       insert_after(head, x, kn)
231       return eadt_ratio[1][3], true
232    else
233       return total, false
234    end
235 end
236
237 -- step 1 最終行用
238 local min, max = math.min, math.max
239 local function aw_step1_last(p, total)
240    local head = getlist(p)
241    local x = node_tail(head); if not x then return total, false end
242    -- x: \rightskip
243    local pf = node_prev(x); if not x then return total, false end
244    if getid(pf) ~= id_glue or getsubtype(pf) ~= 15 then return total, false end
245    x = node_prev(node_prev(pf))
246    local xi, xc = getid(x)
247    if xi == id_glyph and getfield(x, 'lang')==lang_ja then
248       -- 和文文字
249       xc = x
250    elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
251       -- packed JAchar
252       xc = ltjd_glyph_from_packed(x)
253       while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
254    else
255       return total, false-- それ以外は対象外.
256    end
257    -- 続行条件1:無限の伸縮度を持つグルーは \parfillskipのみ
258    if total>0 and total_st.order>0 then
259       if total_st.order ~= getfield(pf, 'stretch_order') then return total, false end
260       if total_st[total_st.order*65536] ~= getfield(pf, 'stretch') then return total, false end
261       for i=total_st.order-1, 1, -1 do
262          if total_st[i*65536] ~= 0 then return total, false end
263       end
264    end
265    if total<0 and total_sh.order>0 then
266       if total_sh.order ~= getfield(pf, 'shrink_order') then return total, false end
267       if total_sh[total_sh.order*65536] ~= getfield(pf, 'shrink') then return total, false end
268       for i=total_sh.order-1, 1, -1 do
269          if total_sh[i*65536] ~= 0 then return total, false end
270       end
271    end
272    local eadt = ltjf_font_metric_table[getfont(xc)]
273       .char_type[has_attr(xc, attr_jchar_class) or 0].end_adjust
274    if not eadt then 
275       return total, false
276    end
277    -- 続行条件2: min(eadt[1], 0)<= \parfillskip <= max(eadt[#eadt], 0)
278    local pfw = getfield(pf, 'width') 
279      + (total>0 and getfield(pf, 'stretch') or -getfield(pf, 'shrink')) *getfield(p, 'glue_set') 
280    if pfw<min(0,eadt[1]) or max(0,eadt[#eadt])<pfw then return total, false end
281    -- \parfillskip を 0 にする
282    total = total + getfield(pf, 'width') 
283    total_st.order, total_sh.order = 0, 0
284    if getfield(pf, 'stretch_order')==0 then 
285       local i = at2pr_st[-1] 
286       total_st[0] = total_st[0] - getfield(pf, 'stretch') 
287       total_st[i] = total_st[i] - getfield(pf, 'stretch') 
288       total_st.order = (total_st[0]==0) and -1 or 0
289    end
290    if getfield(pf, 'shrink_order')==0 then 
291       local i = at2pr_sh[-1] 
292       total_sh[0] = total_sh[0] - getfield(pf, 'shrink') 
293       total_sh[i] = total_sh[i] - getfield(pf, 'shrink') 
294       total_sh.order = (total_sh[0]==0) and -1 or 0
295    end
296    setfield(pf, 'subtype', 1); setglue(pf)
297    local eadt_ratio = {}
298    for i, v in ipairs(eadt) do
299       local t = total - v
300       if t>0 then
301          eadt_ratio[i] = {i, t/total_st[65536*total_st.order], t, v}
302       else
303          eadt_ratio[i] = {i, t/total_sh[65536*total_sh.order], t, v}
304       end
305    end
306    table.sort(eadt_ratio, 
307    function (a,b) 
308        for i=2,4 do
309            local at, bt = abs(a[i]), abs(b[i])
310            if at~=bt then return at<bt end
311        end
312        return a[4]<b[4]
313    end)
314    if eadt[eadt_ratio[1][1]]~=0 then
315       local kn = node_new(id_kern, 1)
316       setfield(kn, 'kern', eadt[eadt_ratio[1][1]]); set_attr(kn, attr_icflag, LINEEND)
317       insert_after(head, x, kn)
318       return eadt_ratio[1][3], true
319    else
320       return total, false
321    end
322 end
323
324
325 -- step 2: 行中の glue を変える
326 local aw_step2, aw_step2_dummy
327 do
328 local node_hpack = node.direct.hpack
329 local function repack(p)
330    local orig_of, orig_hfuzz, orig_hbad = tex.overfullrule, tex.hfuzz, tex.hbadness
331    tex.overfullrule=0; tex.hfuzz=1073741823; tex.hbadness=10000
332    local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
333    tex.overfullrule=orig_of; tex.hfuzz=orig_hfuzz; tex.hbadness=orig_hbad
334    setfield(f, 'head', nil)
335    setfield(p, 'glue_set', getfield(f, 'glue_set'))
336    setfield(p, 'glue_order', getfield(f, 'glue_order'))
337    setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
338    node_free(f)
339    return
340 end
341 function aw_step2_dummy(p, _, added_flag)
342    if added_flag then return repack(p) end
343 end
344
345 local function clear_stretch(p, ind, ap, name)
346    for q in node_traverse_id(id_glue, getlist(p)) do
347       local f = ap[get_attr_icflag(q)]
348       if f == ind then
349          setfield(q, name..'_order', 0); setfield(q, name, 0)
350       end
351    end
352 end
353
354 local function set_stretch(p, after, before, ind, ap, name)
355    if before > 0 then
356       local ratio = after/before
357       for q in node_traverse_id(id_glue, getlist(p)) do
358          local f = ap[get_attr_icflag(q)]
359          if (f==ind) and getfield(q, name..'_order')==0 then
360             setfield(q, name, getfield(q, name)*ratio)
361          end
362       end
363    end
364 end
365
366 function aw_step2(p, total, added_flag)
367    local name = (total>0) and 'stretch' or 'shrink'
368    local id =  (total>0) and 1 or 2
369    local res = total_stsh[id]
370    local pnum = priority_num[id]
371    if total==0 or res.order > 0 then 
372       -- もともと伸縮の必要なしか,残りの伸縮量は無限大
373       if added_flag then return repack(p) end
374    end
375    total = abs(total)
376    for i = 1, pnum do
377       if total <= res[i] then
378          local a = at2pr[id]  
379          for j = i+1,pnum do
380             clear_stretch(p, j, a, name)
381          end
382          set_stretch(p, total, res[i], i, a, name); break
383       end
384       total = total - res[i]
385    end
386    return repack(p)
387 end
388 end
389
390 -- step 1': lineend=extended の場合(行分割時に考慮))
391 local insert_lineend_kern
392 do
393    local insert_before = node.direct.insert_before
394    local KINSOKU      = luatexja.icflag_table.KINSOKU
395    insert_lineend_kern = function (head, nq, np, Bp)
396       if nq.met then 
397          local eadt = nq.met.char_type[nq.class].end_adjust
398          if not eadt then return end
399          if eadt[1]~=0 then
400             local x = node_new(id_kern, 1)
401             setfield(x, 'kern', eadt[1]); set_attr(x, attr_icflag, LINEEND)
402             insert_before(head, np.first, x)
403          end
404          local eadt_num = #eadt
405          for i=2,eadt_num do
406             local x = node_new(id_penalty)
407             setfield(x, 'penalty', 0); set_attr(x, attr_icflag, KINSOKU)
408             insert_before(head, np.first, x); Bp[#Bp+1] = x
409             local x = node_new(id_kern, 1)
410             setfield(x, 'kern', eadt[i]-eadt[i-1]); set_attr(x, attr_icflag, LINEEND)
411             insert_before(head, np.first, x)
412          end
413          if eadt_num>1 or eadt[1]~=0 then
414             local x = node_new(id_penalty)
415             setfield(x, 'penalty', 0); set_attr(x, attr_icflag, KINSOKU)
416             insert_before(head, np.first, x); Bp[#Bp+1] = x
417             local x = node_new(id_kern, 1)
418             setfield(x, 'kern', -eadt[eadt_num]); set_attr(x, attr_icflag, LINEEND)
419             insert_before(head, np.first, x)
420             local x = node_new(id_penalty)
421             setfield(x, 'penalty', 10000); set_attr(x, attr_icflag, KINSOKU)
422             insert_before(head, np.first, x); Bp[#Bp+1] = x
423          end
424       end
425    end
426 end
427
428 local adjust_width
429 do
430    local myaw_step1, myaw_step2, myaw_step1_last
431    local dummy =  function(p,t,n) return t, false end
432    local ltjs_fast_get_stack_skip = ltjs.fast_get_stack_skip
433    function adjust_width(head)
434       if not head then return head end
435       local last_p
436       for p in node_traverse_id(id_hlist, to_direct(head)) do
437          if last_p then
438             myaw_step2(last_p, myaw_step1(last_p, get_total_stretched(last_p)))
439          end
440          last_p = p
441       end
442       if last_p then
443          myaw_step2(last_p, myaw_step1_last(last_p, get_total_stretched(last_p)))
444       end
445       return to_node(head)
446    end
447    local is_reg = false
448    local function enable_cb(status_le, status_pr, status_lp, status_ls, status_cd)
449       if (status_le>0 or status_pr>0) and (not is_reg) then
450          ltjb.add_to_callback('post_linebreak_filter',
451             adjust_width, 'Adjust width', 
452             luatexbase.priority_in_callback('post_linebreak_filter', 'ltj.lineskip')-1)
453          is_reg = true
454       elseif is_reg and (status_le==0 and status_pr==0) then
455          luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
456          is_reg = false
457       end
458       if status_le==2 then
459          if not luatexbase.in_callback('luatexja.adjust_jfmglue', 'luatexja.adjust') then
460             ltjb.add_to_callback('luatexja.adjust_jfmglue', insert_lineend_kern, 'luatexja.adjust')
461          end
462          myaw_step1, myaw_step1_last = dummy, aw_step1_last
463       else
464          if status_le==0 then
465             myaw_step1, myaw_step1_last = dummy, dummy
466          else
467             myaw_step1, myaw_step1_last = aw_step1, aw_step1_last
468          end
469          if luatexbase.in_callback('luatexja.adjust_jfmglue', 'luatexja.adjust') then
470                luatexbase.remove_from_callback('luatexja.adjust_jfmglue', 'luatexja.adjust')
471          end
472       end
473       myaw_step2 = (status_pr>0) and aw_step2 or aw_step2_dummy
474       luatexja.lineskip.setting(
475          status_lp>0 and 'profile' or 'dummy',
476          status_ls>0 and 'step' or 'dummy'
477       )
478       if status_cd==1 then
479          if not luatexbase.in_callback('process_input_buffer', 'Combine voiced kana-syllables') then
480             ltjb.add_to_callback('process_input_buffer', combine_voiced_kana, 'Combine voiced kana-syllables')
481          end
482       else
483          if luatexbase.in_callback('process_input_buffer', 'Combine voiced kana-syllables') then
484                luatexbase.remove_from_callback('process_input_buffer', 'Combine voiced kana-syllables')
485          end
486       end
487    end
488    local function disable_cb() -- only for compatibility
489        enable_cs(0,0,0,0,0)
490    end
491    luatexja.adjust.enable_cb=enable_cb
492    luatexja.adjust.disable_cb=disable_cb
493 end
494
495 luatexja.unary_pars.adjust = function(t)
496    return is_reg and 1 or 0
497 end
498
499 -- ----------------------------------
500 local init_range
501 do
502   local max, ins, sort = math.max, table.insert, table.sort
503   local function insert(package, ind, d, b, e)
504     local bound = package[2]
505     bound[b], bound[e]=true, true
506     ins(package[1], {b,e,[ind]=d})
507   end
508   local function flatten(package)
509     local bd = {} for i,_ in pairs(package[2]) do ins(bd,{i}) end
510     sort(bd, function (a,b) return a[1]<b[1] end)
511     local bdc=#bd; local t = package[1]
512     sort(t, function (a,b) return a[1]<b[1] end)
513     local bdi =1
514     for i=1,#t do
515       while bd[bdi][1]<t[i][1] do bdi=bdi+1 end
516       local j = bdi
517       while j<bdc and bd[j+1][1]<=t[i][2] do
518         for k,w in pairs(t[i]) do
519           if k>=3 then
520             bd[j][k]=bd[j][k] and max(bd[j][k],w) or w
521           end
522         end
523         j = j + 1
524       end
525     end
526     package[2]=nil; package[1]=nil; package.flatten, package.insert=nil, nil
527     bd[#bd]=nil
528     return bd
529   end
530   init_range = function ()
531     return {{},{}, insert=insert, flatten=flatten}
532   end
533 end
534
535 -- -----------------------------------
536 luatexja.adjust.step_factor = 0.5
537 luatexja.unary_pars.linestep_factor = function(t)
538    return luatexja.adjust.step_factor
539 end
540 luatexja.adjust.profile_hgap_factor = 1
541 luatexja.unary_pars.profile_hgap_factor = function(t)
542    return luatexja.adjust.profile_hgap_factor
543 end
544 do
545   local insert = table.insert
546   local rangedimensions, max = node.direct.rangedimensions, math.max
547   local function profile_inner(box, range, ind, vmirrored, adj)
548     local w_acc, d_before = getfield(box,'shift'), 0
549     local x = getlist(box); local xn = node_next(x)
550     while x do
551       local w, h, d
552       if xn then w, h, d= rangedimensions(box,x,xn)
553       else w, h, d= rangedimensions(box,x) end
554       if vmirrored then h=d end
555       local w_new = w_acc + w
556       if w>=0 then
557         range:insert(ind, h, w_acc-adj, w_new)
558       else
559         range:insert(ind, h, w_new-adj, w_acc)
560       end
561       w_acc = w_new; x = xn; if x then xn = node_next(x) end
562     end
563   end  
564   function ltjl.p_profile(before, after, mirrored, bw)
565     local range, tls 
566       = init_range(), luatexja.adjust.profile_hgap_factor*tex.lineskip.width
567     profile_inner(before, range, 3, true,     tls)
568     profile_inner(after,  range, 4, mirrored, tls)
569     range = range:flatten()
570     do
571       local dmax, d, hmax, h, lmin = 0, 0, 0, 0, 1/0
572       for i,v in ipairs(range) do
573         d, h = (v[3] or 0), (v[4] or 0)
574         if d>dmax then dmax=d end
575         if h>hmax then hmax=h end
576         if bw-h-d<lmin then lmin=bw-h-d end
577       end
578       if lmin==1/0 then lmin = bw end
579       return lmin, 
580          bw - lmin - getfield(before, 'depth')
581             - getfield(after, mirrored and 'depth' or 'height')
582     end
583   end
584 end
585
586 do
587   local ltja = luatexja.adjust
588   local copy_glue = ltjl.copy_glue
589   local floor, max = math.floor, math.max
590   function ltjl.l_step(dist, g, adj, normal, bw, loc)
591     if loc=='alignment' then
592       return ltjl.l_dummy(dist, g, adj, normal, bw, loc)
593     end
594     if dist < tex.lineskiplimit then
595     local f = max(1, bw*ltja.step_factor)
596        copy_glue(g, tex.baselineskip, 1, normal - f * floor((dist-tex.lineskip.width)/f))
597     else
598        copy_glue(g, tex.baselineskip, 2, normal)
599     end
600   end
601 end
602
603