count_dashes.lua 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. --
  2. -- Copyright 2016 Google Inc.
  3. --
  4. -- Use of this source code is governed by a BSD-style license that can be
  5. -- found in the LICENSE file.
  6. --
  7. -- Dashed path scraping script.
  8. -- This script is designed to count the total number of dashes in a dashed path
  9. -- by computing the fill path and then counting how many individual segments are
  10. -- inside the resulting fill path.
  11. dashes = 0
  12. pathPieces = {}
  13. function sk_scrape_startcanvas(c, fileName)
  14. end
  15. function sk_scrape_endcanvas(c, fileName)
  16. end
  17. function sk_scrape_accumulate(t)
  18. local paint = t.paint
  19. if paint then
  20. local pe = paint:getPathEffect()
  21. if pe then
  22. if t.verb == "drawPath" and pe:asADash() then
  23. dashes = dashes + 1
  24. pathPieces[dashes] = 0
  25. local path = t.path
  26. local fillpath = paint:getFillPath(path)
  27. local verbs = fillpath:getVerbs()
  28. for _, verb in ipairs(verbs) do
  29. if verb == "move" then
  30. pathPieces[dashes] = pathPieces[dashes] + 1
  31. end
  32. end
  33. end
  34. end
  35. end
  36. end
  37. -- We mulitply by two because for each segment of the dash, we do two measurements:
  38. -- One for the beginning and one for the end of each dash.
  39. function sk_scrape_summarize()
  40. local pieces5 = 0;
  41. local pieces10 = 0;
  42. for _, p in ipairs(pathPieces) do
  43. local pieces = 2*p
  44. if pieces < 5 then
  45. pieces5 = pieces5 + 1
  46. end
  47. if pieces > 5 and pieces < 10 then
  48. pieces10 = pieces10 + 1
  49. end
  50. end
  51. io.write(string.format("%d %d %d\n", 2*dashes, pieces5, pieces10))
  52. end