gradients.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. filename = ""
  2. function sk_scrape_startcanvas(c, fileName)
  3. filename = fileName
  4. end
  5. function sk_scrape_endcanvas(c, fileName)
  6. end
  7. LuaDoubleNearlyZero = 1.0 / bit32.lshift(1.0, 12)
  8. function LuaDoubleNearlyEqual(a, b)
  9. return math.abs(a-b) <= LuaDoubleNearlyZero
  10. end
  11. function bounds(rect)
  12. local width = rect.right - rect.left
  13. local height = rect.bottom - rect.top
  14. return width, height
  15. end
  16. gradients = {}
  17. i = 1
  18. function sk_scrape_accumulate(t)
  19. local p = t.paint
  20. if p then
  21. local s = p:getShader()
  22. if s then
  23. local g = s:asAGradient()
  24. if g then
  25. gradients[i] = {}
  26. gradients[i].filename = filename
  27. local width, height = -1, -1
  28. if t.rect then
  29. width, height = bounds(t.rect)
  30. elseif t.rrect then
  31. width, height = bounds(t.rrect:rect())
  32. elseif t.path then
  33. width, height = bounds(t.path:getBounds())
  34. end
  35. gradients[i].boundsWidth = width
  36. gradients[i].boundsHeight = height
  37. gradients[i].colorCount = g.colorCount
  38. gradients[i].type = g.type
  39. gradients[i].tile = g.tile
  40. isEvenlySpaced = true
  41. for j = 1, g.colorCount, 1 do
  42. if not LuaDoubleNearlyEqual(g.positions[j], (j-1)/(g.colorCount-1)) then
  43. isEvenlySpaced = false
  44. end
  45. end
  46. gradients[i].isEvenlySpaced = isEvenlySpaced
  47. numHardStops = 0
  48. for j = 2, g.colorCount, 1 do
  49. if LuaDoubleNearlyEqual(g.positions[j], g.positions[j-1]) then
  50. numHardStops = numHardStops + 1
  51. end
  52. end
  53. gradients[i].numHardStops = numHardStops
  54. gradients[i].verb = t.verb
  55. gradients[i].positions = {}
  56. for j = 1, g.colorCount, 1 do
  57. gradients[i].positions[j] = g.positions[j]
  58. end
  59. i = i + 1
  60. end
  61. end
  62. end
  63. end
  64. function sk_scrape_summarize()
  65. for k, v in pairs(gradients) do
  66. local pos = ""
  67. for j = 1, v.colorCount , 1 do
  68. pos = pos .. v.positions[j]
  69. if j ~= v.colorCount then
  70. pos = pos .. ","
  71. end
  72. end
  73. io.write(string.format("%s %d %s %s %d %d %s %d %d %s\n",
  74. v.filename,
  75. v.colorCount,
  76. v.type,
  77. v.tile,
  78. tonumber(v.isEvenlySpaced and 1 or 0),
  79. v.numHardStops,
  80. v.verb,
  81. v.boundsWidth,
  82. v.boundsHeight,
  83. pos))
  84. end
  85. end