summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/pack-rul.lua
blob: a270bffe79708d72c3b96c7b6daecac0420f5ce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
if not modules then modules = { } end modules ['pack-rul'] = {
    version   = 1.001,
    comment   = "companion to pack-rul.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

--[[ldx--
<p>An explanation is given in the history document <t>mk</t>.</p>
--ldx]]--

-- we need to be careful with display math as it uses shifts

-- \framed[align={lohi,middle}]{$x$}
-- \framed[align={lohi,middle}]{$ $}
-- \framed[align={lohi,middle}]{\hbox{ }}
-- \framed[align={lohi,middle}]{\hbox{}}
-- \framed[align={lohi,middle}]{$\hskip2pt$}

local type = type

local context         = context

local hlist_code      = nodes.nodecodes.hlist
local vlist_code      = nodes.nodecodes.vlist
local box_code        = nodes.listcodes.box
local line_code       = nodes.listcodes.line
local equation_code   = nodes.listcodes.equation

local texsetdimen     = tex.setdimen
local texsetcount     = tex.setcount

local implement       = interfaces.implement

local nuts            = nodes.nuts

local getnext         = nuts.getnext
local getprev         = nuts.getprev
local getlist         = nuts.getlist
local setlist         = nuts.setlist
local getwhd          = nuts.getwhd
local getid           = nuts.getid
local getsubtype      = nuts.getsubtype
local getbox          = nuts.getbox
local getdir          = nuts.getdir
local setshift        = nuts.setshift
local setwidth        = nuts.setwidth
local getwidth        = nuts.getwidth
local setboxglue      = nuts.setboxglue
local getboxglue      = nuts.getboxglue

local hpack           = nuts.hpack
local list_dimensions = nuts.dimensions
local flush_node      = nuts.flush

local nexthlist       = nuts.traversers.hlist
local nextvlist       = nuts.traversers.vlist

local checkformath    = false

directives.register("framed.checkmath",function(v) checkformath = v end) -- experiment

-- beware: dir nodes and pseudostruts can end up on lines of their own

local function doreshapeframedbox(n)
    local box            = getbox(n)
    local noflines       = 0
    local nofnonzero     = 0
    local firstheight    = nil
    local lastdepth      = nil
    local lastlinelength = 0
    local minwidth       = 0
    local maxwidth       = 0
    local totalwidth     = 0
    local averagewidth   = 0
    local boxwidth       = getwidth(box)
    if boxwidth ~= 0 then -- and h.subtype == vlist_code
        local list = getlist(box)
        if list then
            local function check(n,repack)
                local width, height, depth = getwhd(n)
                if not firstheight then
                    firstheight = height
                end
                lastdepth = depth
                noflines  = noflines + 1
                local l   = getlist(n)
                if l then
                    if repack then
                        local subtype = getsubtype(n)
                        if subtype == box_code or subtype == line_code then
                            lastlinelength = list_dimensions(l,getdir(n))
                        else
                            lastlinelength = width
                        end
                    else
                        lastlinelength = width
                    end
                    if lastlinelength > maxwidth then
                        maxwidth = lastlinelength
                    end
                    if lastlinelength < minwidth or minwidth == 0 then
                        minwidth = lastlinelength
                    end
                    if lastlinelength > 0 then
                        nofnonzero = nofnonzero + 1
                    end
                    totalwidth = totalwidth + lastlinelength
                end
            end
            local hdone = false
            for h in nexthlist, list do -- no dir etc needed
                check(h,true)
                hdone = true
            end
         -- local vdone = false
            for v in nextvlist, list do -- no dir etc needed
                check(v,false)
             -- vdone = true
            end
            if not firstheight then
                -- done)
            elseif maxwidth ~= 0 then
                if hdone then
                    for h in nexthlist, list do
                        local l = getlist(h)
                        if l then
                            local subtype = getsubtype(h)
                            if subtype == box_code or subtype == line_code then
                                local p = hpack(l,maxwidth,'exactly',getdir(h)) -- multiple return value
                                local set, order, sign = getboxglue(p)
                                setboxglue(h,set,order,sign)
                                setlist(p)
                                flush_node(p)
                            elseif checkformath and subtype == equation_code then
                             -- display formulas use a shift
                                if nofnonzero == 1 then
                                    setshift(h,0)
                                end
                            end
                            setwidth(h,maxwidth)
                        end
                    end
                end
             -- if vdone then
             --     for v in nextvlist, list do
             --         local width = getwidth(n)
             --         if width > maxwidth then
             --             setwidth(v,maxwidth)
             --         end
             --     end
             -- end
                setwidth(box,maxwidth)
                averagewidth = noflines > 0 and totalwidth/noflines or 0
            else -- e.g. empty math {$ $} or \hbox{} or ...
                setwidth(box,0)
            end
        end
    end
    texsetcount("global","framednoflines",noflines)
    texsetdimen("global","framedfirstheight",firstheight or 0) -- also signal
    texsetdimen("global","framedlastdepth",lastdepth or 0)
    texsetdimen("global","framedminwidth",minwidth)
    texsetdimen("global","framedmaxwidth",maxwidth)
    texsetdimen("global","framedaveragewidth",averagewidth)
end

if LUATEXVERSION >= 1.090 then

    local traverse_list = node.direct.traverse_list

    local nextlist = nuts.traversers.list

 -- local function doreshapeframedbox(n)
    doreshapeframedbox = function(n)
        local box            = getbox(n)
        local noflines       = 0
        local nofnonzero     = 0
        local firstheight    = nil
        local lastdepth      = nil
        local lastlinelength = 0
        local minwidth       = 0
        local maxwidth       = 0
        local totalwidth     = 0
        local averagewidth   = 0
        local boxwidth       = getwidth(box)
        if boxwidth ~= 0 then -- and h.subtype == vlist_code
            local list = getlist(box)
            if list then
                local hdone = false
                for n, id, subtype, list in nextlist, list do -- no dir etc needed
                    local width, height, depth = getwhd(n)
                    if not firstheight then
                        firstheight = height
                    end
                    lastdepth = depth
                    noflines  = noflines + 1
                    if list then
                        if id == hlist_code then
                            if subtype == box_code or subtype == line_code then
                                lastlinelength = list_dimensions(list,getdir(n))
                            else
                                lastlinelength = width
                            end
                            hdone = true
                        else
                            lastlinelength = width
                         -- vdone = true
                        end
                        if lastlinelength > maxwidth then
                            maxwidth = lastlinelength
                        end
                        if lastlinelength < minwidth or minwidth == 0 then
                            minwidth = lastlinelength
                        end
                        if lastlinelength > 0 then
                            nofnonzero = nofnonzero + 1
                        end
                        totalwidth = totalwidth + lastlinelength
                    end
                end
                if not firstheight then
                    -- done)
                elseif maxwidth ~= 0 then
                    if hdone then
                        for h, id, subtype, list in nextlist, list do
                            if list and id == hlist_code then
                                if subtype == box_code or subtype == line_code then
                                    local p = hpack(list,maxwidth,'exactly',getdir(h)) -- multiple return value
                                    local set, order, sign = getboxglue(p)
                                    setboxglue(h,set,order,sign)
                                    setlist(p)
                                    flush_node(p)
                                elseif checkformath and subtype == equation_code then
                                 -- display formulas use a shift
                                    if nofnonzero == 1 then
                                        setshift(h,0)
                                    end
                                end
                                setwidth(h,maxwidth)
                            end
                        end
                    end
                 -- if vdone then
                 --     for v in nextvlist, list do
                 --         local width = getwidth(n)
                 --         if width > maxwidth then
                 --             setwidth(v,maxwidth)
                 --         end
                 --     end
                 -- end
                    setwidth(box,maxwidth)
                    averagewidth = noflines > 0 and totalwidth/noflines or 0
                else -- e.g. empty math {$ $} or \hbox{} or ...
                    setwidth(box,0)
                end
            end
        end
        texsetcount("global","framednoflines",noflines)
        texsetdimen("global","framedfirstheight",firstheight or 0) -- also signal
        texsetdimen("global","framedlastdepth",lastdepth or 0)
        texsetdimen("global","framedminwidth",minwidth)
        texsetdimen("global","framedmaxwidth",maxwidth)
        texsetdimen("global","framedaveragewidth",averagewidth)
    end

end

local function doanalyzeframedbox(n) -- traverse_list
    local box         = getbox(n)
    local noflines    = 0
    local firstheight = nil
    local lastdepth   = nil
    if getwidth(box) ~= 0 then
        local list = getlist(box)
        if list then
            local function check(n)
                local width, height, depth = getwhd(n)
                if not firstheight then
                    firstheight = height
                end
                lastdepth = depth
                noflines  = noflines + 1
            end
            for h in nexthlist, list do
                check(h)
            end
            for v in nextvlist, list do
                check(v)
            end
        end
    end
    texsetcount("global","framednoflines",noflines)
    texsetdimen("global","framedfirstheight",firstheight or 0)
    texsetdimen("global","framedlastdepth",lastdepth or 0)
end

implement { name = "doreshapeframedbox", actions = doreshapeframedbox, arguments = "integer" }
implement { name = "doanalyzeframedbox", actions = doanalyzeframedbox, arguments = "integer" }

local function maxboxwidth(box)
    local boxwidth = getwidth(box)
    if boxwidth == 0 then
        return 0
    end
    local list = getlist(box)
    if not list then
        return 0
    end
    if getid(box) == hlist_code then
        return boxwidth
    end
    local lastlinelength = 0
    local maxwidth       = 0
    local function check(n,repack)
        local l = getlist(n)
        if l then
            if repack then
                local subtype = getsubtype(n)
                if subtype == box_code or subtype == line_code then
                    lastlinelength = list_dimensions(l,getdir(n))
                else
                    lastlinelength = getwidth(n)
                end
            else
                lastlinelength = getwidth(n)
            end
            if lastlinelength > maxwidth then
                maxwidth = lastlinelength
            end
        end
    end
    for h in nexthlist, list do -- no dir etc needed
        check(h,true)
    end
    for v in nextvlist, list do -- no dir etc needed
        check(v,false)
    end
    return maxwidth
end

nodes.maxboxwidth = maxboxwidth

implement {
    name      = "themaxboxwidth",
    actions   = function(n) context("%rsp",maxboxwidth(getbox(n))) end, -- r = rounded
    arguments = "integer"
}