OSDN Git Service

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