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