OSDN Git Service

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