1-- Experimental helpers for skia --
2
3function string.startsWith(String,Start)
4   return string.sub(String,1,string.len(Start))==Start
5end
6
7function string.endsWith(String,End)
8   return End=='' or string.sub(String,-string.len(End))==End
9end
10
11
12Sk = {}
13
14function Sk.isFinite(x)
15    return x * 0 == 0
16end
17
18-------------------------------------------------------------------------------
19
20Sk.Rect = { left = 0, top = 0, right = 0, bottom = 0 }
21Sk.Rect.__index = Sk.Rect
22
23function 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;
37end
38
39function Sk.Rect:width()
40    return self.right - self.left
41end
42
43function Sk.Rect:height()
44    return self.bottom - self.top
45end
46
47function Sk.Rect:isEmpty()
48    return self:width() <= 0 or self:height() <= 0
49end
50
51function 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
57end
58
59function Sk.Rect:setEmpty()
60    self.left = 0
61    self.top = 0
62    self.right = 0
63    self.bottom = 0
64end
65
66function Sk.Rect:set(l, t, r, b)
67    self.left = l
68    self.top = t
69    self.right = r
70    self.bottom = b
71end
72
73function 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
80end
81
82function 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
89end
90
91-------------------------------------------------------------------------------
92