trac-vsp.lmt /size: 5829 b    last modification: 2025-02-21 11:03
1if not modules then modules = { } end modules ['trac-vsp'] = {
2    version   = 1.001,
3    optimize  = true,
4    comment   = "companion to trac-vsp.mkxl",
5    author    = "Hans Hagen & Mikael Sundqvist",
6    copyright = "PRAGMA ADE / ConTeXt Development Team",
7    license   = "see context related readme files"
8}
9
10-- This will likely change but for now we only have a simple tracer.
11
12local tonut       = nodes.tonut
13local nuts        = nodes.nuts
14local getid       = nuts.getid
15local insertafter = nuts.insertafter
16
17local nodecodes   = nodes.nodecodes
18
19local splitcodes  = tex.splitcodes
20
21local splitpoints   = { }
22tracers.splitpoints = splitpoints
23
24local collected = { }
25
26local splitshow = {
27    [splitcodes.initialize] = function(where,checks,current,height,depth,extra)
28        collected = {
29            height = height,
30            depth  = depth,
31            extra  = extra,
32        }
33    end,
34    [splitcodes.continue] = function(where,checks,current,height,depth,total,penalty)
35        current = tonut(current)
36        collected[#collected+1] = {
37            where    = splitcodes[where],
38            current  = current,
39            location = nodecodes[getid(current)],
40            height   = height,
41            depth    = depth,
42            total    = total,
43            penalty  = penalty,
44        }
45    end,
46    [splitcodes.check] = function(where,checks,current,height,depth,total,penalty,badness,costs)
47        current = tonut(current)
48        collected[#collected+1] = {
49            where    = splitcodes[where],
50            current  = current,
51            location = nodecodes[getid(current)],
52            height   = height,
53            depth    = depth,
54            total    = total,
55            penalty  = penalty,
56            badness  = badness,
57            costs    = costs,
58        }
59    end,
60    [splitcodes.quit] = function(where,checks,current,height,depth,total,penalty,badness,costs)
61        current = tonut(current)
62        collected[#collected+1] = {
63            where    = splitcodes[where],
64            current  = current,
65            location = nodecodes[getid(current)],
66            height   = height,
67            depth    = depth,
68            total    = total,
69            penalty  = penalty,
70            badness  = badness,
71            costs    = costs,
72        }
73    end,
74    [splitcodes.wrapup] = function(where,checks,best,height,depth,stretch,shrink)
75        best = tonut(best)
76--        local wipe = false
77--        for i=1,#collected do
78--            local c = collected[i]
79--            if wipe then
80--                c.current = false
81--            elseif c.current == best then
82--                wipe = true
83--            end
84--        end
85        collected.result  = {
86            best    = best,
87            height  = height,
88            depth   = depth,
89            stretch = stretch,
90            shrink  = shrink,
91        }
92    end,
93}
94
95local visualizevsplit = false
96local f_detail        = string.formatters["%i:%s"]
97
98function splitpoints.start(options)
99    collected = { }
100end
101
102function splitpoints.stop()
103    if not visualizevsplit then
104        visualizevsplit = nodes.visualizers.register("vsplit","trace:dr","trace:dr",-1.5)
105        nodes.visualizers.definelayer("vsplit")
106    end
107    local last = nil
108    for i=1,#collected do
109        local c = collected[i]
110        local current = c.current or last
111        last = current
112        if current then
113            local n = visualizevsplit(f_detail(i,nodecodes[getid(current)]))
114            insertafter(current,current,n)
115        end
116    end
117end
118
119function splitpoints.typesetresult(options)
120    if #collected > 0 then
121        local context, ctx_NC, ctx_BC, ctx_NR, ctx_color = context, context.NC, context.BC, context.NR, context.color
122        local awfulbad = tex.magicconstants.awful_bad
123        local maxdimen = tex.magicconstants.max_dimen
124        context.starttabulate { "|r|l|l|r|r|r|r|r|r|" }
125        ctx_BC() context("n")
126        ctx_BC() context("where")
127        ctx_BC() context("location")
128        ctx_BC() context("height")
129        ctx_BC() context("depth")
130        ctx_BC() context("total")
131        ctx_BC() context("penalty")
132        ctx_BC() context("badness")
133        ctx_BC() context("costs")
134        ctx_NC() ctx_NR()
135        for i=1,#collected do
136            local c = collected[i]
137            ctx_NC() context(i)
138            ctx_NC() context(c.where)
139            ctx_NC() context(c.location)
140            ctx_NC() context("%p",c.height)
141            ctx_NC() context("%p",c.depth)
142            ctx_NC() context("%p",c.total)
143            ctx_NC() context(c.penalty)
144            ctx_NC() context(c.badness == awfulbad and "awful" or c.badness)
145            ctx_NC() context(c.costs   == awfulbad and "awful" or c.costs)
146            ctx_NC() ctx_NR()
147        end
148        context.stoptabulate()
149        context.starttabulate { "|l|r|i3l|r|" }
150        ctx_BC() context("result height")  ctx_NC() context("%p",collected.result.height)
151        ctx_BC() context("target  height") ctx_NC() context("%p",collected.height)         ctx_NC() ctx_NR()
152        ctx_BC() context("result depth")   ctx_NC() context("%p",collected.result.depth)
153        ctx_BC() context("maximum depth")  ctx_NC() if collected.depth == maxdimen then context("max") else context("%p",collected.depth) end ctx_NC() ctx_NR()
154        ctx_BC() context("result stretch") ctx_NC() context("%p",collected.result.stretch)
155        ctx_BC() context("extra height")   ctx_NC() context("%p",collected.extra)          ctx_NC() ctx_NR()
156        ctx_BC() context("result shrink")  ctx_NC() context("%p",collected.result.shrink)
157        ctx_BC()                           ctx_NC()                                        ctx_NC() ctx_NR()
158        context.stoptabulate()
159    end
160end
161
162callback.register("show_vsplit", function(where,...) splitshow[where](where,...) end)
163