aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/lua/scrape.lua
blob: 92636121da7405514b9187890d33b4ef00526d07 (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
-- just a helper function to dump the parameters, for debugging
function tostr(t)
    local str = ""
    for k, v in next, t do
        if #str > 0 then
            str = str .. ", "
        end
        if type(k) == "number" then
            str = str .. "[" .. k .. "] = "
        else
            str = str .. tostring(k) .. " = "
        end
        if type(v) == "table" then
            str = str .. "{ " .. tostr(v) .. " }"
        else
            str = str .. tostring(v)
        end
    end
    return str
end

local total = {}    -- accumulate() stores its data in here
local canvas        -- holds the current canvas (from startcanvas())

--[[
    startcanvas() is called at the start of each picture file, passing the
    canvas that we will be drawing into, and the name of the file.
    
    Following this call, there will be some number of calls to accumulate(t)
    where t is a table of parameters that were passed to that draw-op.
    
        t.verb is a string holding the name of the draw-op (e.g. "drawRect")
    
    when a given picture is done, we call endcanvas(canvas, fileName)
]]
function sk_scrape_startcanvas(c, fileName)
    canvas = c
end

--[[
    Called when the current canvas is done drawing.
]]
function sk_scrape_endcanvas(c, fileName)
    canvas = nil
end

--[[
    Called with the parameters to each canvas.draw call, where canvas is the
    current canvas as set by startcanvas()
]]
function sk_scrape_accumulate(t)
    local n = total[t.verb] or 0
    total[t.verb] = n + 1

    if false and t.verb == "drawRect" then
        local m = canvas:getTotalMatrix()
        print("... ", tostr(m), "\n")
    end

    if false and t.verb == "drawPath" then
        local pred, r1, r2, d1, d2 = t.path:isNestedRects()
        
        if pred then
            print("drawRect_Nested", tostr(r1), tostr(r2), d1, d2)
        else
            print("drawPath", "isEmpty", tostring(t.path:isEmpty()),
                    "isRect", tostring(t.path:isRect()), tostr(t.path:getBounds()))
        end
    end

    -- enable to dump all of the parameters we were sent
    if false then
        -- dump the params in t, specifically showing the verb first, which we
        -- then nil out so it doesn't appear in tostr()
        io.write(t.verb, " ")
        t.verb = nil
        io.write(tostr(t), "\n")
    end
end

--[[
    lua_pictures will call this function after all of the pictures have been
    "accumulated".
]]
function sk_scrape_summarize()
    io.write("\n{ ", tostr(total), " }\n")
end