summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/typo-bld.lmt
blob: c0b42001e66b463d56bbd7c26d57d2811c7ab157 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
if modules then modules = { } end modules ['typo-bld'] = { -- was node-par
    version   = 1.001,
    comment   = "companion to typo-bld.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- no need for nuts in the one-line demo (that might move anyway)

local insert, remove = table.insert, table.remove

builders                 = builders or { }
local builders           = builders

builders.paragraphs      = builders.paragraphs or { }
local parbuilders        = builders.paragraphs

parbuilders.constructors = parbuilders.constructors or { }
local       constructors = parbuilders.constructors

builders.hpack           = builders.hpack or { }
builders.vpack           = builders.vpack or { }

local hpackbuilders      = builders.hpack
local vpackbuilders      = builders.vpack

constructors.names       = constructors.names or { }
local names              = constructors.names

constructors.numbers     = constructors.numbers or { }
local numbers            = constructors.numbers

constructors.methods     = constructors.methods or { }
local methods            = constructors.methods

local a_parbuilder       = attributes.numbers['parbuilder'] or 999 -- why 999
constructors.attribute   = a_parbuilder

local unsetvalue         = attributes.unsetvalue
local texsetattribute    = tex.setattribute
local texnest            = tex.nest
local texget             = tex.get
local texset             = tex.set

local getspeciallist     = nodes.nuts.getspeciallist
local setspeciallist     = nodes.nuts.setspeciallist

local nodes              = nodes
local nodeidstostring    = nodes.idstostring
local nodepool           = nodes.pool
local new_baselineskip   = nodepool.baselineskip
local new_lineskip       = nodepool.lineskip
local insertnodebefore   = nodes.insertbefore
local hpack_node         = nodes.hpack

local nuts               = nodes.nuts
local tonode             = nodes.tonode
local tonut              = nodes.tonut
local getattr            = nuts.getattr
local flush              = nuts.flush

local starttiming        = statistics.starttiming
local stoptiming         = statistics.stoptiming

local registercallback   = callbacks.register

storage.register("builders/paragraphs/constructors/names",   names,   "builders.paragraphs.constructors.names")
storage.register("builders/paragraphs/constructors/numbers", numbers, "builders.paragraphs.constructors.numbers")

local trace_page_builder = false  trackers.register("builders.page", function(v) trace_page_builder = v end)
local trace_vbox_builder = false  trackers.register("builders.vbox", function(v) trace_vbox_builder = v end)
local trace_post_builder = false  trackers.register("builders.post", function(v) trace_post_builder = v end)

local report_page_builder = logs.reporter("builders","page")
local report_vbox_builder = logs.reporter("builders","vbox")
local report_par_builder  = logs.reporter("builders","par")

local mainconstructor = nil -- not stored in format
local nofconstructors = 0
local stack           = { }

function constructors.define(name)
    nofconstructors = nofconstructors + 1
    names[nofconstructors] = name
    numbers[name] = nofconstructors
end

function constructors.set(name) --- will go
    if name then
        mainconstructor = numbers[name] or unsetvalue
    else
        mainconstructor = stack[#stack] or unsetvalue
    end
    texsetattribute(a_parbuilder,mainconstructor)
    if mainconstructor ~= unsetvalue then
        constructors.enable()
    end
end

function constructors.start(name)
    local number = numbers[name]
    insert(stack,number)
    mainconstructor = number or unsetvalue
    texsetattribute(a_parbuilder,mainconstructor)
    if mainconstructor ~= unsetvalue then
        constructors.enable()
    end
 -- report_par_builder("start %a",name)
end

function constructors.stop()
    remove(stack)
    mainconstructor = stack[#stack] or unsetvalue
    texsetattribute(a_parbuilder,mainconstructor)
    if mainconstructor == unsetvalue then
        constructors.disable()
    end
 -- report_par_builder("stop")
end

-- return values:
--
-- true  : tex will break itself
-- false : idem but dangerous
-- head  : list of valid vmode nodes with last being hlist

function constructors.handler(head,followed_by_display)
    if type(head) == "boolean" then
        return head
    else
        local attribute = getattr(head,a_parbuilder) -- or mainconstructor
        if attribute then
            local method = names[attribute]
            if method then
                local handler = methods[method]
                if handler then
                    return handler(head,followed_by_display)
                else
                    report_par_builder("contructor method %a is not defined",tostring(method))
                    return true -- let tex break
                end
            end
        end
        return true -- let tex break
    end
end

-- just for testing

function constructors.methods.default(head,followed_by_display)
    return true -- let tex break
end

-- also for testing (now also surrounding spacing done)

function parbuilders.constructors.methods.oneline(head,followed_by_display)
    -- when needed we will turn this into a helper
    local t = texnest[texnest.ptr]
    local h = hpack_node(head)
    local d = texget("baselineskip",false) - t.prevdepth - h.height
    t.prevdepth = h.depth
    t.prevgraf  = 1
    if d < texget("lineskiplimit") then
        return insertnodebefore(h,h,new_lineskip(texget("lineskip",false))) -- no stretch etc
    else
        return insertnodebefore(h,h,new_baselineskip(d))
    end
end

-- It makes no sense to have a sequence here as we already have
-- pre and post hooks and only one parbuilder makes sense, so no:
--
-- local actions = nodes.tasks.actions("parbuilders")
--
-- yet ... maybe some day.

local actions = constructors.handler
local enabled = false

local function processor(head,followed_by_display)
    -- todo: not again in otr so we need to flag
    if enabled then
        starttiming(parbuilders)
        head = tonode(actions(tonut(head),followed_by_display))
        stoptiming(parbuilders)
        return head
    else
        return true -- let tex do the work
    end
end

function constructors.enable () enabled = true  end
function constructors.disable() enabled = false end

registercallback("linebreak_filter", processor, "breaking paragraps into lines")

statistics.register("linebreak processing time", function()
    return statistics.elapsedseconds(parbuilders)
end)

-- todo: move from nodes.builders to builders

nodes.builders = nodes.builder or { }
local builders = nodes.builders

local vpackactions = nodes.tasks.actions("vboxbuilders")

function builders.vpack_filter(head,groupcode,size,packtype,maxdepth,direction)
    if head then
        starttiming(builders)
        head = vpackactions(head,groupcode)
        stoptiming(builders)
    end
    return head
end

-- This one is special in the sense that it has no head and we operate on the mlv. Also,
-- we need to do the vspacing last as it removes items from the mvl.

local pageactions = nodes.tasks.actions("mvlbuilders")
----- lineactions = nodes.tasks.actions("linebuilders")

local function report(pagecontext,head)
    report_page_builder("trigger: %s at level %i",pagecontext,texnest.ptr)
    report_page_builder("  vsize    : %p",texget("vsize"))
    report_page_builder("  pagegoal : %p",texget("pagegoal"))
    report_page_builder("  pagetotal: %p",texget("pagetotal"))
    report_page_builder("  list     : %s",head and nodeidstostring(head) or "<empty>")
end

-- Watch out: contributehead can be any head (kind of) not per se the page one
-- but that needs to be intercepted when needed by groupcode and level. At some
-- point this one might be split by group.

function builders.buildpage_filter(pagecontext)
    local head, tail = getspeciallist("contributehead")
    if head then
        if trace_page_builder then
            report(pagecontext,head)
        end
        starttiming(builders)
        head = pageactions(head,pagecontext) -- todo: tail
        stoptiming(builders)
        setspeciallist("contributehead", head)
    else
        if trace_page_builder then
            report(pagecontext)
        end
    end
end

registercallback("vpack_filter",     builders.vpack_filter,     "vertical spacing etc")
registercallback("buildpage_filter", builders.buildpage_filter, "vertical spacing etc (mvl)")

local vboxactions = nodes.tasks.actions("vboxhandlers")

function builders.vbox_filter(head,groupcode)
    if head then
        starttiming(builders)
        head = vboxactions(head,groupcode)
        stoptiming(builders)
    end
    return head
end

registercallback("packed_vbox_filter", builders.vbox_filter, "packed vbox treatments")

statistics.register("v-node processing time", function()
    return statistics.elapsedseconds(builders)
end)

local implement = interfaces.implement

implement { name = "defineparbuilder",  actions = constructors.define, arguments = "string" }
implement { name = "setparbuilder",     actions = constructors.set,    arguments = "string" }
implement { name = "startparbuilder",   actions = constructors.start,  arguments = "string" }
implement { name = "stopparbuilder",    actions = constructors.stop    }
implement { name = "enableparbuilder",  actions = constructors.enable  }
implement { name = "disableparbuilder", actions = constructors.disable }

-- Here are some tracers:

local nuts         = nodes.nuts
local tonut        = nodes.tonut
local setcolor     = nodes.tracers.colors.set
local listtoutf    = nodes.listtoutf
local new_kern     = nuts.pool.kern
local new_rule     = nuts.pool.rule
local hpack        = nuts.hpack
local getheight    = nuts.getheight
local getdepth     = nuts.getdepth
local getdirection = nuts.getdirection
local getlist      = nuts.getlist
local setwidth     = nuts.setwidth
local setdirection = nuts.setdirection
local setlink      = nuts.setlink
local tonode       = nuts.tonode

local list         = { }

local report_quality = logs.reporter("pack quality")

-- overflow|badness w h d dir

local report  = true   trackers.register("builders.vpack.quality", function(v) report  = v end)
local collect = false  trackers.register("builders.vpack.collect", function(v) collect = v end)
----  show    = false  trackers.register("builders.vpack.overflow",function(v) show    = v end)

function vpackbuilders.report(how,detail,n,first,last,filename) -- no return value
    if report then
        if last <= 0 then
            report_quality("%s vbox",how)
        elseif first > 0 and first < last then
            report_quality("%s vbox at line %i - %i in file %a",how,first,last,filename or "?")
        else
            report_quality("%s vbox at line %i in file %a",how,last,filename or "?")
        end
    end
    if collect then
        list[#list+1] = { "hbox", how, filename, first, last, how, detail }
    end
end

-- function builders.vpack.show(how,detail,n,first,last,filename) -- return value
--     if show then
--     end
-- end

local report  = true   trackers.register("builders.hpack.quality", function(v) report  = v end)
local collect = false  trackers.register("builders.hpack.collect", function(v) collect = v end)
local show    = false  trackers.register("builders.hpack.overflow",function(v) show    = v end)

function hpackbuilders.report(how,detail,n,first,last,filename) -- no return value
    local str = (report or collect) and listtoutf(getlist(n),"",true,nil,true)
    if report then
        if last <= 0 then
            report_quality("%s hbox: %s (%p)",how,str,detail)
        elseif first > 0 and first < last then
            report_quality("%s hbox at line %i - %i in file %a: %s (%p)",how,first,last,filename or "?",str,detail)
        else
            report_quality("%s hbox at line %i in file %a: %s (%p)",how,last,filename or "?",str,detail)
        end
    end
    if collect then
        list[#list+1] = { "hbox", how, filename, first, last, str, detail }
    end
end

function hpackbuilders.show(how,detail,n,first,last,filename,rule) -- return value
    if show then
        local width     = 2*65536
        local height    = getheight(n)
        local depth     = getdepth(n)
        local direction = getdirection(n)
        if height < 4*65526 then
            height = 4*65526
        end
        if depth < 2*65526 then
            depth = 2*65526
        end
        if rule then
            flush(rule)
        end
        rule = new_rule(width,height,depth)
        setdirection(rule,direction)
        if how == "overfull" then
            setcolor(rule,"red")
            local kern = new_kern(-detail)
            setlink(kern,rule)
            rule = kern
        elseif how == "underfull" then
            setcolor(rule,"blue")
        elseif how == "loose" then
            setcolor(rule,"magenta")
        elseif how == "tight" then
            setcolor(rule,"cyan")
        end
        rule = hpack(rule)
        setwidth(rule,0) -- maybe better whd all zero
        setdirection(rule,direction)
    else
        local width = texget("overfullrule")
        if width > 0 then
            rule = new_rule(width)
        end
    end
    return rule
end

--

local hqualityactions = nodes.tasks.actions("hquality")
local vqualityactions = nodes.tasks.actions("vquality")

function hpackbuilders.qualityactions(how,detail,n,first,last,filename)
    local rul = nil
 -- starttiming(builders)
    rul = hqualityactions(how,detail,n,first,last,filename)
 -- stoptiming(builders)
    return rul
end
function vpackbuilders.qualityactions(how,detail,n,first,last,filename)
    local rul = nil
 -- starttiming(builders)
    rul = vqualityactions(how,detail,n,first,last,filename)
 -- stoptiming(builders)
    return rul
end

registercallback("hpack_quality", hpackbuilders.qualityactions, "report horizontal packing quality")
registercallback("vpack_quality", vpackbuilders.qualityactions, "report vertical packing quality")


--

statistics.register("quality reports", function()
    local n = #list
    if n > 0 then
        local t = table.setmetatableindex("number")
        local fw = 0
        local hw = 0
        for i=1,n do
            local f = list[i][1]
            local h = list[i][2]
            if #f > fw then
                fw = #f
            end
            if #h > hw then
                hw = #h
            end
            t[h] = t[h] + 1
        end
        logs.startfilelogging(report_quality)
        for i=1,n do
            local l = list[i]
            report_quality("%-" .. fw .. "s [%04i - %04i] : %-" .. hw .. "s %s : %s (%p)",file.basename(l[3]),l[4],l[5],l[2],l[1],l[6],l[7])
        end
        logs.stopfilelogging()
        report_quality()
        report_quality("%i entries added to the log file : %s",n,table.sequenced(t))
        report_quality()
    end
end)