trac-jus.lua /size: 4397 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['trac-jus'] = {
2    version   = 1.001,
3    comment   = "companion to trac-jus.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local checkers        = typesetters.checkers or { }
10typesetters.checkers  = checkers
11
12----- report_justification = logs.reporter("visualize","justification")
13
14local a_alignstate    = attributes.private("alignstate")
15local a_justification = attributes.private("justification")
16
17local nuts            = nodes.nuts
18
19local getfield        = nuts.getfield
20local getlist         = nuts.getlist
21local getattr         = nuts.getattr
22local setattr         = nuts.setattr
23local setlist         = nuts.setlist
24local setlink         = nuts.setlink
25local getwidth        = nuts.getwidth
26local findtail        = nuts.tail
27
28local nexthlist       = nuts.traversers.hlist
29
30local getdimensions   = nuts.dimensions
31local copylist        = nuts.copylist
32
33local tracedrule      = nodes.tracers.pool.nuts.rule
34
35local nodepool        = nuts.pool
36
37local new_hlist       = nodepool.hlist
38local new_kern        = nodepool.kern
39
40local hlist_code      = nodes.nodecodes.hlist
41
42local texsetattribute = tex.setattribute
43local unsetvalue      = attributes.unsetvalue
44
45local enableaction    = nodes.tasks.enableaction
46
47local min_threshold   = 0
48local max_threshold   = 0
49
50local function set(n)
51    enableaction("mvlbuilders", "typesetters.checkers.handler")
52    enableaction("vboxbuilders","typesetters.checkers.handler")
53    texsetattribute(a_justification,n or 1)
54    function typesetters.checkers.set(n)
55        texsetattribute(a_justification,n or 1)
56    end
57end
58
59local function reset()
60    texsetattribute(a_justification,unsetvalue)
61end
62
63checkers.set   = set
64checkers.reset = reset
65
66interfaces.implement {
67    name    = "showjustification",
68    actions = set
69}
70
71trackers.register("visualizers.justification", function(v)
72    if v then
73        set(1)
74    else
75        reset()
76    end
77end)
78
79function checkers.handler(head)
80    for current in nexthlist, head do
81        if getattr(current,a_justification) == 1 then
82            setattr(current,a_justification,0) -- kind of reset
83            local width = getwidth(current)
84            if width > 0 then
85                local list = getlist(current)
86                if list then
87                    local naturalwidth, naturalheight, naturaldepth = getdimensions(list)
88                    local delta = naturalwidth - width
89                    if naturalwidth == 0 or delta == 0 then
90                        -- special box
91                    elseif delta >= max_threshold then
92                        local rule = new_hlist(tracedrule(delta,naturalheight,naturaldepth,getfield(list,"glue_set") == 1 and "trace:dr" or "trace:db"))
93                        setlink(findtail(list),rule)
94                        setlist(current,list)
95                    elseif delta <= min_threshold then
96                        local alignstate = getattr(list,a_alignstate)
97                        if alignstate == 1 then
98                            local rule = new_hlist(tracedrule(-delta,naturalheight,naturaldepth,"trace:dc"))
99                            setlink(rule,list)
100                            setlist(current,rule)
101                        elseif alignstate == 2 then
102                            local lrule = new_hlist(tracedrule(-delta/2,naturalheight,naturaldepth,"trace:dy"))
103                            local rrule = copylist(lrule)
104                            setlink(lrule,list)
105                            setlink(findtail(list),new_kern(delta/2),rrule)
106                            setlist(current,lrule)
107                        elseif alignstate == 3 then
108                            local rule = new_hlist(tracedrule(-delta,naturalheight,naturaldepth,"trace:dm"))
109                            setlink(findtail(list),new_kern(delta),rule)
110                            setlist(current,list)
111                        else
112                            local rule = new_hlist(tracedrule(-delta,naturalheight,naturaldepth,"trace:dg"))
113                            setlink(findtail(list),new_kern(delta),rule)
114                            setlist(current,list)
115                        end
116                    end
117                end
118            end
119        end
120    end
121    return head
122end
123