OSDN Git Service

use node.direct.{g/s}etdata
[luatex-ja/luatexja.git] / src / ltj-lineskip.lua
1 --
2 -- ltj-lineskip.lua
3 --
4 luatexja.load_module 'base'; local ltjb = luatexja.base
5 luatexja.load_module 'direction'; local ltjd = luatexja.direction
6 luatexja.lineskip = luatexja.lineskip or {}
7
8 local to_direct = node.direct.todirect
9 local to_node = node.direct.tonode
10 local ltjl = luatexja.lineskip
11 local id_glue    = node.id 'glue'
12 local id_penalty = node.id 'penalty'
13 local id_hlist   = node.id 'hlist'
14 local getlist = node.direct.getlist
15 local node_new = node.direct.new
16 local node_prev = node.direct.getprev
17 local node_next = node.direct.getnext
18 local getid = node.direct.getid
19 local getsubtype = node.direct.getsubtype
20 local getdepth = node.direct.getdepth
21 local getheight = node.direct.getheight
22 local texget = tex.get
23
24 local node_getglue = node.getglue
25 local setglue = luatexja.setglue
26 local setsubtype = node.direct.setsubtype
27 local function copy_glue (new_glue, old_glue_name, subtype, new_w)
28    setsubtype(new_glue, subtype)
29    local w,st,sp,sto,spo = texget(old_glue_name, true)
30    setglue(new_glue, new_w or w, st, sp, sto, spo)
31 end
32 ltjl.copy_glue = copy_glue
33
34 function ltjl.p_dummy(before, after)
35    return nil, 0
36 end
37 function ltjl.l_dummy(dist, g, adj, normal, bw, loc)
38    if dist < tex.lineskiplimit then
39       copy_glue(g, 'lineskip', 1, texget('lineskip', false) + adj)
40    else
41       copy_glue(g, 'baselineskip', 2, normal)
42    end
43 end
44
45 local ltj_profiler, ltj_skip = ltjl.p_dummy, ltjl.l_dummy
46 function ltjl.setting(profiler, skip_method)
47    ltj_profiler = ltjl['p_'..tostring(profiler)] or ltjl.p_dummy
48    ltj_skip = ltjl['l_'..tostring(skip_method)] or ltjl.l_dummy
49 end
50
51 do
52 local traverse_id = node.direct.traverse_id
53 local function adjust_glue(nh)
54    local h = to_direct(nh)
55    local bw = texget('baselineskip',false)
56    for x in traverse_id(id_glue, h) do
57      local xs = getsubtype(x)
58      if (xs==1) or (xs==2) then
59         local p, n = node_prev(x), node_next(x)
60         if p then
61         local pid = getid(p)
62            while (id_glue<=pid) and (pid<=id_penalty) and node_prev(p) do
63              p = node_prev(p); pid = getid(p)
64            end
65            if pid==id_hlist and getid(n)==id_hlist then
66              local normal = bw - getdepth(p) - getheight(n)
67              local lmin, adj = ltj_profiler(p, n, false, bw)
68              ltj_skip(lmin or normal, x, adj, normal, bw)
69            end
70         end
71      end
72    end
73    return true
74 end
75 ltjb.add_to_callback('post_linebreak_filter', adjust_glue, 'ltj.lineskip', 10000)
76 end
77
78 do
79 local p_dummy = ltjl.p_dummy
80 local make_dir_whatsit = luatexja.direction.make_dir_whatsit
81 local get_dir_count = luatexja.direction.get_dir_count
82 local getwhd = node.direct.getwhd
83 local setnext = node.direct.setnext
84 local getnest = tex.getnest
85
86 local function dir_adjust_append_vlist(b, loc, prev, mirrored)
87    local old_b = to_direct(b)
88    local new_b = loc=='box' and
89       make_dir_whatsit(old_b, old_b, get_dir_count(), 'append_vlist') or old_b
90    local _, ht, dp = getwhd(new_b)
91    if prev > -65536000 then
92       local bw = texget('baselineskip', false)
93       local normal = bw - prev - (mirrored and dp or ht)
94       local lmin, adj = nil, 0
95       local tail = to_direct(getnest().tail)
96       if p_dummy~=ltj_profiler then
97          while tail and (id_glue<=getid(tail)) and (getid(tail)<=id_penalty) do
98             tail = node_prev(tail)
99          end
100       end
101       if tail then
102          if getid(tail)==id_hlist and getid(new_b)==id_hlist then
103             if getdepth(tail)==prev then
104                lmin, adj = ltj_profiler(tail, new_b, mirrored, bw)
105             end
106          end
107       end
108       local g = node_new(id_glue)
109       ltj_skip(lmin or normal, g, adj, normal, bw, loc)
110       setnext(g, new_b); return to_node(g), (mirrored and ht or dp)
111    else return to_node(new_b), (mirrored and ht or dp)
112    end
113 end
114 ltjb.add_to_callback('append_to_vlist_filter', dir_adjust_append_vlist, 'ltj.lineskip', 10000)
115 end
116