-
Notifications
You must be signed in to change notification settings - Fork 3
/
OpenVG_Utils.lua
248 lines (190 loc) · 5.93 KB
/
OpenVG_Utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
local ffi = require "ffi"
local bit = require "bit"
local bor = bit.bor
require "OpenVG"
local OpenVG = {
Lib = ffi.load "OpenVG";
}
ffi.cdef[[
typedef struct {
VGPaint Handle;
} OVGPaint;
typedef struct {
VGPath Handle;
} OVGPath;
]]
--[[
Paint object
--]]
Paint = ffi.typeof("OVGPaint")
Paint_mt = {
__new = function(ct, paintMode)
local handle
if not paintMode then
handle = OpenVG.Lib.vgCreatePaint();
else
handle = OpenVG.Lib.vgGetPaint(paintMode);
end
if handle == VG_INVALID_HANDLE then
return nil
end
local obj = ffi.new(ct, handle);
return obj
end,
__gc = function(self)
OpenVG.Lib.vgDestroyPaint(self.Handle);
end,
__index = {
SetModes = function(self, paintModes)
OpenVG.Lib.vgSetPaint(self.Handle, paintModes);
end,
SetColor = function(self, rgba)
--OpenVG.Lib.vgSetColor(self.Handle, rgba);
OpenVG.Lib.vgSetParameterfv(self.Handle, ffi.C.VG_PAINT_COLOR, 4, rgba);
end,
GetColor = function(self)
return OpenVG.Lib.vgGetColor(self.Handle);
end,
SetPattern = function(self, pattern)
OpenVG.Lib.vgPaintPattern(self.Handle, pattern);
end,
SetType = function(self, pType)
OpenVG.Lib.vgSetParameteri(self.Handle, ffi.C.VG_PAINT_TYPE, pType);
end,
},
}
Paint = ffi.metatype(Paint, Paint_mt);
--[[
Paths
--]]
Path = ffi.typeof("OVGPath");
Path_mt = {
__new = function(ct, pathFormat, datatype, scale, bias, segmentCapacityHint, coordCapacityHint, capabilities)
pathFormat = pathFormat or VG_PATH_FORMAT_STANDARD;
datatype = datatype or ffi.C.VG_PATH_DATATYPE_F
scale = scale or 1
bias = bias or 0
segmentCapacityHint = segmentCapacityHint or 0
coordCapacityHint = coordCapacityHint or 0
capabilities = capabilities or ffi.C.VG_PATH_CAPABILITY_ALL
local handle = OpenVG.Lib.vgCreatePath(pathFormat, datatype, scale, bias, segmentCapacityHint, coordCapacityHint, capabilities);
--print("Path Handle: ", handle);
local obj = ffi.new(ct, handle);
return obj
end,
__gc = function(self)
OpenVG.Lib.vgDestroyPath(self.Handle);
end,
__index = {
Clear = function(self, capabilities)
capabilities = capabilities or ffi.C.VG_PATH_CAPABILITY_ALL
OpenVG.Lib.vgClearPath(self.Handle, capabilities);
end,
RemoveCapabilities = function(self, capabilities)
capabilities = capabilities or ffi.C.VG_PATH_CAPABILITY_ALL
OpenVG.Lib.vgRemovePathCapabilities(self.Handle, capabilities);
end,
GetCapabilities = function(self)
return OpenVG.Lib.vgGetPathCapabilities(self.Handle);
end,
AppendPath = function(self, path)
OpenVG.Lib.vgAppendPath(self.Handle, path.Handle);
end,
AppendPathData = function(self, numSegments, pathSegments, pathData)
OpenVG.Lib.vgAppendPathData(self.Handle, numSegments, pathSegments, pathData);
end,
ModifyPathCoords = function(self, startIndex, numSegments, pathData)
OpenVG.Lib.vgModifyPathCoords(self.Handle, startIndex, numSegments, pathData);
end,
TransformPath = function(self, srcPath)
OpenVG.Lib.vgTransformPath(self.Handle, srcPath.Handle);
end,
InterpolatePath = function(self, startPath, endPath, amount)
return OpenVG.Lib.vgInterpolatePath(self.Handle, startPath.Handle, endPath.Handle, amount);
end,
PathLength = function(self, startSegment, numSegments)
return OpenVG.Lib.vgPathLength(self.Handle, startSegment, numSegments);
end,
PointAlongPath = function(self, startSegment, numSegments, distance)
local x = ffi.new("VGfloat[1]");
local y = ffi.new("VGFloat[1]");
local tangentX = ffi.new("VGFloat[1]");
local tangentY = ffi.new("VGFloat[1]");
OpenVG.Lib.vgPointAlongPath(self.Handle, startSegment, numSegments, distance,
x, y, tangentX, tangentY);
return x[0], y[0], tangentX[0], tangentY[0];
end,
Bounds = function(self)
local minX = ffi.new("VGfloat[1]");
local minY = ffi.new("VGfloat[1]");
local width = ffi.new("VGfloat[1]");
local height = ffi.new("VGfloat[1]");
OpenVG.Lib.vgPathBounds(self.Handle,
minX, minY,
width, height);
return minX[0], minY[0], width[0], height[0];
end,
TransformedBounds = function(self)
local minX = ffi.new("VGfloat[1]");
local minY = ffi.new("VGfloat[1]");
local width = ffi.new("VGfloat[1]");
local height = ffi.new("VGfloat[1]");
OpenVG.Lib.vgPathTransformedBounds(self.Handle, minX, minY, width, height);
return minX[0], minY[0], width[0], height[0];
end,
Draw = function(self, paintModes)
paintModes = paintModes or bor(ffi.C.VG_FILL_PATH, ffi.C.VG_STROKE_PATH)
OpenVG.Lib.vgDrawPath(self.Handle, paintModes);
return self;
end,
},
}
Path = ffi.metatype(Path, Path_mt);
--[[
Shapes
A simple factory to create various types of path
objects.
--]]
local Paths = {
Line = function(x1, y1, x2, y2)
local path = Path();
OpenVG.Lib.vguLine(path.Handle, x1, y1, x2, y2);
return path;
end,
-- poly makes either a polygon or polyline
Poly = function(points, n)
local path = Path();
OpenVG.Lib.vguPolygon(path.Handle, points, n, ffi.C.VG_FALSE);
return path;
end,
-- Rect makes a rectangle at the specified location and dimensions
Rect = function(x, y, w, h)
local path = Path();
OpenVG.Lib.vguRect(path.Handle, x, y, w, h);
return path
end,
-- Roundrect makes an rounded rectangle at the specified location and dimensions
Roundrect = function(x, y, w, h, rw, rh)
local path = Path();
OpenVG.Lib.vguRoundRect(path.Handle, x, y, w, h, rw, rh);
return path;
end,
-- Ellipse makes an ellipse at the specified location and dimensions
Ellipse = function(x, y, w, h)
local path = Path();
OpenVG.Lib.vguEllipse(path.Handle, x, y, w, h);
return path;
end,
-- Arc makes an elliptical arc at the specified location and dimensions
Arc = function(x, y, w, h, sa, aext)
local path = Path();
OpenVG.Lib.vguArc(path.Handle, x, y, w, h, sa, aext, ffi.C.VGU_ARC_OPEN);
return path;
end,
}
return {
Lib = OpenVG.Lib;
Path = Path;
Paths = Paths;
Paint = Paint;
}