glyph-usage.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. local canvas -- holds the current canvas (from startcanvas())
  21. --[[
  22. startcanvas() is called at the start of each picture file, passing the
  23. canvas that we will be drawing into, and the name of the file.
  24. Following this call, there will be some number of calls to accumulate(t)
  25. where t is a table of parameters that were passed to that draw-op.
  26. t.verb is a string holding the name of the draw-op (e.g. "drawRect")
  27. when a given picture is done, we call endcanvas(canvas, fileName)
  28. ]]
  29. function sk_scrape_startcanvas(c, fileName)
  30. canvas = c
  31. end
  32. --[[
  33. Called when the current canvas is done drawing.
  34. ]]
  35. function sk_scrape_endcanvas(c, fileName)
  36. canvas = nil
  37. end
  38. --[[
  39. Called with the parameters to each canvas.draw call, where canvas is the
  40. current canvas as set by startcanvas()
  41. ]]
  42. function round(x, mul)
  43. mul = mul or 1
  44. return math.floor(x * mul + 0.5) / mul
  45. end
  46. dump_glyph_array_p = false
  47. function dump_array_as_C(array)
  48. for k, v in next, array do
  49. io.write(tostring(v), ", ");
  50. end
  51. io.write("-1,\n")
  52. end
  53. local strikes = {} -- [fontID_pointsize] = [] unique glyphs
  54. function make_strike_key(paint)
  55. return paint:getFontID() * 1000 + paint:getTextSize()
  56. end
  57. -- array is an array of bools (true), using glyphID as the index
  58. -- other is just an array[1...N] of numbers (glyphIDs)
  59. function array_union(array, other)
  60. for k, v in next, other do
  61. array[v] = true;
  62. end
  63. end
  64. -- take a table of bools, indexed by values, and return a sorted table of values
  65. function bools_to_values(t)
  66. local array = {}
  67. for k, v in next, t do
  68. array[#array + 1] = k
  69. end
  70. table.sort(array)
  71. return array
  72. end
  73. function array_count(array)
  74. local n = 0
  75. for k in next, array do
  76. n = n + 1
  77. end
  78. return n
  79. end
  80. function sk_scrape_accumulate(t)
  81. verb = t.verb;
  82. if verb == "drawPosText" or verb == "drawPosTextH" then
  83. if t.glyphs then
  84. local key = make_strike_key(t.paint)
  85. strikes[key] = strikes[key] or {}
  86. array_union(strikes[key], t.glyphs)
  87. if dump_glyph_array_p then
  88. dump_array_as_C(t.glyphs)
  89. end
  90. end
  91. end
  92. end
  93. --[[
  94. lua_pictures will call this function after all of the pictures have been
  95. "accumulated".
  96. ]]
  97. function sk_scrape_summarize()
  98. local totalCount = 0
  99. local strikeCount = 0
  100. local min, max = 0, 0
  101. local histogram = {}
  102. for k, v in next, strikes do
  103. local fontID = round(k / 1000)
  104. local size = k - fontID * 1000
  105. local count = array_count(v)
  106. -- io.write("fontID,", fontID, ", size,", size, ", entries,", count, "\n");
  107. min = math.min(min, count)
  108. max = math.max(max, count)
  109. totalCount = totalCount + count
  110. strikeCount = strikeCount + 1
  111. histogram[count] = (histogram[count] or 0) + 1
  112. end
  113. local ave = round(totalCount / strikeCount)
  114. io.write("\n", "unique glyphs: min = ", min, ", max = ", max, ", ave = ", ave, "\n");
  115. for k, v in next, histogram do
  116. io.write("glyph_count,", k, ",frequency,", v, "\n")
  117. end
  118. end
  119. function test_summary()
  120. io.write("just testing test_summary\n")
  121. end
  122. function summarize_unique_glyphIDs()
  123. io.write("/* runs of unique glyph IDs, with a -1 sentinel between different runs */\n")
  124. io.write("static const int gUniqueGlyphIDs[] = {\n");
  125. for k, v in next, strikes do
  126. dump_array_as_C(bools_to_values(v))
  127. end
  128. io.write("-1 };\n")
  129. end