1 -- Experimental helpers for skia -- 2 3 function string.startsWith(String,Start) 4 return string.sub(String,1,string.len(Start))==Start 5 end 6 7 function string.endsWith(String,End) 8 return End=='' or string.sub(String,-string.len(End))==End 9 end 10 11 12 Sk = {} 13 14 function Sk.isFinite(x) 15 return x * 0 == 0 16 end 17 18 ------------------------------------------------------------------------------- 19 20 Sk.Rect = { left = 0, top = 0, right = 0, bottom = 0 } 21 Sk.Rect.__index = Sk.Rect 22 23 function Sk.Rect.new(l, t, r, b) 24 local rect 25 if r then 26 -- 4 arguments 27 rect = { left = l, top = t, right = r, bottom = b } 28 elseif l then 29 -- 2 arguments 30 rect = { right = l, bottom = t } 31 else 32 -- 0 arguments 33 rect = {} 34 end 35 setmetatable(rect, Sk.Rect) 36 return rect; 37 end 38 39 function Sk.Rect:width() 40 return self.right - self.left 41 end 42 43 function Sk.Rect:height() 44 return self.bottom - self.top 45 end 46 47 function Sk.Rect:isEmpty() 48 return self:width() <= 0 or self:height() <= 0 49 end 50 51 function Sk.Rect:isFinite() 52 local value = self.left * 0 53 value = value * self.top 54 value = value * self.right 55 value = value * self.bottom 56 return 0 == value 57 end 58 59 function Sk.Rect:setEmpty() 60 self.left = 0 61 self.top = 0 62 self.right = 0 63 self.bottom = 0 64 end 65 66 function Sk.Rect:set(l, t, r, b) 67 self.left = l 68 self.top = t 69 self.right = r 70 self.bottom = b 71 end 72 73 function Sk.Rect:offset(dx, dy) 74 dy = dy or dx 75 76 self.left = self.left + dx 77 self.top = self.top + dy 78 self.right = self.right + dx 79 self.bottom = self.bottom + dy 80 end 81 82 function Sk.Rect:inset(dx, dy) 83 dy = dy or dx 84 85 self.left = self.left + dx 86 self.top = self.top + dy 87 self.right = self.right - dx 88 self.bottom = self.bottom - dy 89 end 90 91 ------------------------------------------------------------------------------- 92