trac-par.lua /size: 4496 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['trac-par'] = {
2    version   = 1.001,
3    comment   = "companion to node-par.mkiv",
4    author    = "Hans Hagen",
5    copyright = "ConTeXt Development Team",
6    license   = "see context related readme files",
7    comment   = "a translation of the built in parbuilder, initial convertsin by Taco Hoekwater",
8}
9
10-- todo: kern
11
12local utfchar = utf.char
13local concat = table.concat
14
15local nuts          = nodes.nuts
16local tonut         = nuts.tonut
17
18local getid         = nuts.getid
19local getnext       = nuts.getnext
20local getlist       = nuts.getlist
21local getwidth      = nuts.getwidth
22local getexpansion  = nuts.getexpansion
23
24local isglyph       = nuts.isglyph
25
26local nodecodes     = nodes.nodecodes
27local hlist_code    = nodecodes.hlist
28local vlist_code    = nodecodes.vlist
29local glyph_code    = nodecodes.glyph
30local setnodecolor  = nodes.tracers.colors.set
31local parameters    = fonts.hashes.parameters
32local basepoints    = number.basepoints
33
34local setaction     = nodes.tasks.setaction
35
36-- definecolor[hz:positive] [r=0.6]
37-- definecolor[hz:negative] [g=0.6]
38-- definecolor[hz:zero]     [b=0.6]
39
40-- scale = multiplier + ef/multiplier
41
42local trace_both    = false  trackers.register("builders.paragraphs.expansion.both",    function(v) trace_verbose = false trace_both  = v end)
43local trace_verbose = false  trackers.register("builders.paragraphs.expansion.verbose", function(v) trace_verbose = v     trace_color = v end)
44
45local report_verbose = logs.reporter("fonts","expansion")
46
47local function colorize(n)
48    local size, font, ef, width, list, flush, length
49    if trace_verbose then
50        width  = 0
51        length = 0
52        list   = { }
53        flush  = function()
54            if length > 0 then
55                report_verbose("%0.3f : %10s  %10s  %s",ef/1000,basepoints(width),basepoints(width*ef/1000000),concat(list,"",1,length))
56                width  = 0
57                length = 0
58            end
59        end
60    else
61        length = 0
62    end
63    -- tricky: the built-in method creates dummy fonts and the last line normally has the
64    -- original font and that one then has ex.auto set
65    while n do
66        local char, id = isglyph(n)
67        if char then
68            local ne = getexpansion(n)
69            if ne == 0 then
70                if length > 0 then flush() end
71                setnodecolor(n,"hz:zero")
72            else
73                -- id == font
74                if id ~= font then
75                    if length > 0 then
76                        flush()
77                    end
78                    local pf = parameters[id]
79                    local ex = pf.expansion
80                    if ex and ex.auto then
81                        size = pf.size
82                        font = id -- save lookups
83                    else
84                        size = false
85                    end
86                end
87                if size then
88                    if ne ~= ef then
89                        if length > 0 then
90                            flush()
91                        end
92                        ef = ne
93                    end
94                    if ef > 1 then
95                        setnodecolor(n,"hz:plus")
96                    elseif ef < 1 then
97                        setnodecolor(n,"hz:minus")
98                    else
99                        setnodecolor(n,"hz:zero")
100                    end
101                    if trace_verbose then
102                        length = length + 1
103                        list[length] = utfchar(char)
104                        width = width + getwidth(n) -- no kerning yet
105                    end
106                end
107            end
108        elseif id == hlist_code or id == vlist_code then
109            if length > 0 then
110                flush()
111            end
112            local list = getlist(n)
113            if list then
114                colorize(list,flush)
115            end
116        else -- nothing to show on kerns
117            if length > 0 then
118                flush()
119            end
120        end
121        n = getnext(n)
122    end
123    if length > 0 then
124        flush()
125    end
126end
127
128builders.paragraphs.expansion = builders.paragraphs.expansion or { }
129
130function builders.paragraphs.expansion.trace(head)
131    colorize(head,true)
132    return head
133end
134
135local function set(v)
136    setaction("shipouts","builders.paragraphs.expansion.trace",v)
137end
138
139trackers.register("builders.paragraphs.expansion.verbose",set)
140trackers.register("builders.paragraphs.expansion.both",set)
141