reed@google.com | e3823fd | 2013-05-30 18:55:14 +0000 | [diff] [blame^] | 1 | function tostr(t) |
| 2 | local str = "" |
| 3 | for k, v in next, t do |
| 4 | if #str > 0 then |
| 5 | str = str .. ", " |
| 6 | end |
| 7 | if type(k) == "number" then |
| 8 | str = str .. "[" .. k .. "] = " |
| 9 | else |
| 10 | str = str .. tostring(k) .. " = " |
| 11 | end |
| 12 | if type(v) == "table" then |
| 13 | str = str .. "{ " .. tostr(v) .. " }" |
| 14 | else |
| 15 | str = str .. tostring(v) |
| 16 | end |
| 17 | end |
| 18 | return str |
| 19 | end |
| 20 | |
| 21 | local canvas -- holds the current canvas (from startcanvas()) |
| 22 | |
| 23 | --[[ |
| 24 | startcanvas() is called at the start of each picture file, passing the |
| 25 | canvas that we will be drawing into, and the name of the file. |
| 26 | |
| 27 | Following this call, there will be some number of calls to accumulate(t) |
| 28 | where t is a table of parameters that were passed to that draw-op. |
| 29 | |
| 30 | t.verb is a string holding the name of the draw-op (e.g. "drawRect") |
| 31 | |
| 32 | when a given picture is done, we call endcanvas(canvas, fileName) |
| 33 | ]] |
| 34 | function sk_scrape_startcanvas(c, fileName) |
| 35 | canvas = c |
| 36 | end |
| 37 | |
| 38 | --[[ |
| 39 | Called when the current canvas is done drawing. |
| 40 | ]] |
| 41 | function sk_scrape_endcanvas(c, fileName) |
| 42 | canvas = nil |
| 43 | end |
| 44 | |
| 45 | --[[ |
| 46 | Called with the parameters to each canvas.draw call, where canvas is the |
| 47 | current canvas as set by startcanvas() |
| 48 | ]] |
| 49 | |
| 50 | function round(x, mul) |
| 51 | mul = mul or 1 |
| 52 | return math.floor(x * mul + 0.5) / mul |
| 53 | end |
| 54 | |
| 55 | local strikes = {} -- [fontID_pointsize] = [] unique glyphs |
| 56 | |
| 57 | function make_strike_key(paint) |
| 58 | return paint:getFontID() * 1000 + paint:getTextSize() |
| 59 | end |
| 60 | |
| 61 | -- array is an array of bools (true), using glyphID as the index |
| 62 | -- other is just an array[1...N] of numbers (glyphIDs) |
| 63 | function array_union(array, other) |
| 64 | for k, v in next, other do |
| 65 | array[v] = true; |
| 66 | end |
| 67 | end |
| 68 | |
| 69 | function array_count(array) |
| 70 | local n = 0 |
| 71 | for k in next, array do |
| 72 | n = n + 1 |
| 73 | end |
| 74 | return n |
| 75 | end |
| 76 | |
| 77 | function sk_scrape_accumulate(t) |
| 78 | verb = t.verb; |
| 79 | if verb == "drawPosText" or verb == "drawPosTextH" then |
| 80 | if t.glyphs then |
| 81 | local key = make_strike_key(t.paint) |
| 82 | strikes[key] = strikes[key] or {} |
| 83 | array_union(strikes[key], t.glyphs) |
| 84 | end |
| 85 | end |
| 86 | end |
| 87 | |
| 88 | --[[ |
| 89 | lua_pictures will call this function after all of the pictures have been |
| 90 | "accumulated". |
| 91 | ]] |
| 92 | function sk_scrape_summarize() |
| 93 | local totalCount = 0 |
| 94 | local strikeCount = 0 |
| 95 | local min, max = 0, 0 |
| 96 | |
| 97 | for k, v in next, strikes do |
| 98 | local fontID = round(k / 1000) |
| 99 | local size = k - fontID * 1000 |
| 100 | local count = array_count(v) |
| 101 | |
| 102 | io.write("fontID = ", fontID, ", size = ", size, ", entries = ", count, "\n"); |
| 103 | |
| 104 | min = math.min(min, count) |
| 105 | max = math.max(max, count) |
| 106 | totalCount = totalCount + count |
| 107 | strikeCount = strikeCount + 1 |
| 108 | end |
| 109 | local ave = round(totalCount / strikeCount) |
| 110 | |
| 111 | io.write("\n", "unique glyphs: min = ", min, ", max = ", max, ", ave = ", ave, "\n"); |
| 112 | end |
| 113 | |