aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/lua/glyph-usage.lua
blob: 8715f726fe88a4b1d98b812db398e453d1ea544a (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
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 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 round(x, mul)
    mul = mul or 1
    return math.floor(x * mul + 0.5) / mul
end

dump_glyph_array_p = false

function dump_array_as_C(array)
    for k, v in next, array do
        io.write(tostring(v), ", ");
    end
    io.write("-1,\n")
end

local strikes = {}  -- [fontID_pointsize] = [] unique glyphs

function make_strike_key(paint)
    return paint:getFontID() * 1000 + paint:getTextSize()
end

-- array is an array of bools (true), using glyphID as the index
-- other is just an array[1...N] of numbers (glyphIDs)
function array_union(array, other)
    for k, v in next, other do
        array[v] = true;
    end
end

-- take a table of bools, indexed by values, and return a sorted table of values
function bools_to_values(t)
    local array = {}
    for k, v in next, t do
        array[#array + 1] = k
    end
    table.sort(array)
    return array
end

function array_count(array)
    local n = 0
    for k in next, array do
        n = n + 1
    end
    return n
end

function sk_scrape_accumulate(t)
    verb = t.verb;
    if verb == "drawPosText" or verb == "drawPosTextH" then
        if t.glyphs then
            local key = make_strike_key(t.paint)
            strikes[key] = strikes[key] or {}
            array_union(strikes[key], t.glyphs)
            
            if dump_glyph_array_p then
                dump_array_as_C(t.glyphs)
            end
        end
    end
end

--[[
    lua_pictures will call this function after all of the pictures have been
    "accumulated".
]]
function sk_scrape_summarize()
    local totalCount = 0
    local strikeCount = 0
    local min, max = 0, 0

    local histogram = {}

    for k, v in next, strikes do
        local fontID = round(k / 1000)
        local size = k - fontID * 1000
        local count = array_count(v)

--        io.write("fontID,", fontID, ", size,", size, ", entries,", count, "\n");
        
        min = math.min(min, count)
        max = math.max(max, count)
        totalCount = totalCount + count
        strikeCount = strikeCount + 1
        
        histogram[count] = (histogram[count] or 0) + 1
    end
    local ave = round(totalCount / strikeCount)

    io.write("\n", "unique glyphs: min = ", min, ", max = ", max, ", ave = ", ave, "\n");
    
    for k, v in next, histogram do
        io.write("glyph_count,", k, ",frequency,", v, "\n")
    end
end

function test_summary()
    io.write("just testing test_summary\n")
end

function summarize_unique_glyphIDs()
    io.write("/* runs of unique glyph IDs, with a -1 sentinel between different runs */\n")
    io.write("static const int gUniqueGlyphIDs[] = {\n");
    for k, v in next, strikes do
        dump_array_as_C(bools_to_values(v))
    end
    io.write("-1 };\n")
end