-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutil.js
More file actions
executable file
·209 lines (172 loc) · 4.57 KB
/
util.js
File metadata and controls
executable file
·209 lines (172 loc) · 4.57 KB
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
// Utility functions shared by examples
//
// Based on https://github.com/ajstarks/openvg libshapes.c/go
//
'use strict';
const openVG = require('../../index');
const text = require('./text');
const {
VG_CLEAR_COLOR,
VG_STROKE_CAP_STYLE,
VG_STROKE_JOIN_STYLE,
} = openVG.VGParamType;
const {VG_PAINT_COLOR, VG_PAINT_TYPE} = openVG.VGPaintParamType;
const {VG_PAINT_TYPE_COLOR} = openVG.VGPaintType;
const {VG_CAP_BUTT} = openVG.VGCapStyle;
const {VG_JOIN_MITER} = openVG.VGJoinStyle;
const {VG_FILL_PATH, VG_STROKE_PATH} = openVG.VGPaintMode;
const VG_FILL_AND_STROKE_PATH = VG_FILL_PATH | VG_STROKE_PATH;
const fonts = {
sansTypeface: undefined,
serifTypeface: undefined,
sansMonoTypeface: undefined,
};
function start() {
const color = new Float32Array([255, 255, 255, 0]);
openVG.setFVOL(VG_CLEAR_COLOR, color, 0, 4);
openVG.clear(0, 0, openVG.screen.width, openVG.screen.height);
(color[0] = 0), (color[1] = 0), (color[2] = 0);
setFill(color);
setStroke(color);
strokeWidth(0);
openVG.loadIdentity();
}
function end() {
openVG.egl.swapBuffers(openVG.screen.display, openVG.screen.surface);
}
function init(options) {
if (options === undefined) {
options = {};
}
if (options.loadFonts === undefined) {
options.loadFonts = true;
}
openVG.init();
if (options.loadFonts) {
fonts.sansTypeface = text.loadFont('examples/fonts/sans.json');
fonts.serifTypeface = text.loadFont('examples/fonts/serif.json');
fonts.sansMonoTypeface = text.loadFont('examples/fonts/sans-mono.json');
}
}
function deinit() {
if (fonts.sansTypeface) {
text.unloadFont(fonts.sansTypeface);
}
if (fonts.serifTypeface) {
text.unloadFont(fonts.serifTypeface);
}
if (fonts.sansMonoypeface) {
text.unloadFont(fonts.sansMonoTypeface);
}
openVG.deinit();
}
function capRGB(rgb) {
return (rgb > 255 ? 255 : rgb < 0 ? 0 : rgb) / 255.0;
}
function capAlpha(a) {
return a < 0.0 || a > 1.0 ? 1.0 : a;
}
// RGBA fills a color vectors from a RGBA quad.
function RGBA(r, g, b, a, color) {
r = capRGB(r);
g = capRGB(g);
b = capRGB(b);
a = capAlpha(a);
color[0] = r;
color[1] = g;
color[2] = b;
color[3] = a;
}
function setFill(color) {
const fillPaint = openVG.createPaint();
openVG.setParameterI(fillPaint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
openVG.setParameterFV(fillPaint, VG_PAINT_COLOR, color);
openVG.setPaint(fillPaint, VG_FILL_PATH);
openVG.destroyPaint(fillPaint);
}
function setStroke(color) {
const strokePaint = openVG.createPaint();
openVG.setParameterI(strokePaint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
openVG.setParameterFV(strokePaint, VG_PAINT_COLOR, color);
openVG.setPaint(strokePaint, VG_STROKE_PATH);
openVG.destroyPaint(strokePaint);
}
function strokeWidth(width) {
openVG.setF(openVG.VGParamType.VG_STROKE_LINE_WIDTH, width);
openVG.setI(VG_STROKE_CAP_STYLE, VG_CAP_BUTT);
openVG.setI(VG_STROKE_JOIN_STYLE, VG_JOIN_MITER);
}
function newPath() {
return openVG.createPath(
openVG.VG_PATH_FORMAT_STANDARD,
openVG.VGPathDatatype.VG_PATH_DATATYPE_F,
1.0,
0.0,
0,
0,
openVG.VGPathCapabilities.VG_PATH_CAPABILITY_ALL
);
}
// Line makes a line at connecting the specified locations
function line(x0, y0, x1, y1) {
const path = newPath();
openVG.vgu.line(path, x0, y0, x1, y1);
openVG.drawPath(path, VG_FILL_AND_STROKE_PATH);
openVG.destroyPath(path);
}
// Rect makes a rectangle at the specified location and dimensions
function rect(x, y, w, h) {
const path = newPath();
openVG.vgu.rect(path, x, y, w, h);
openVG.drawPath(path, VG_FILL_AND_STROKE_PATH);
openVG.destroyPath(path);
}
// Ellipse makes an ellipse at the specified location and dimensions
function ellipse(x, y, w, h) {
const path = newPath();
openVG.vgu.ellipse(path, x, y, w, h);
openVG.drawPath(path, VG_FILL_AND_STROKE_PATH);
openVG.destroyPath(path);
}
function circle(x, y, r) {
ellipse(x, y, r, r);
}
function background(r, g, b) {
fill(r, g, b, 1.0);
rect(0, 0, openVG.screen.width, openVG.screen.height);
}
function fill(r, g, b, a) {
const color = new Float32Array(4);
RGBA(r, g, b, a, color);
setFill(color);
}
function stroke(r, g, b, a) {
const color = new Float32Array(4);
RGBA(r, g, b, a, color);
setStroke(color);
}
function checkVGError(msg) {
const err = openVG.getError();
if (err !== 0) {
console.log('vgError: ' + msg + ' : ' + openVG.VGErrorCodeReverse[err]);
}
}
module.exports = {
fonts,
start,
end,
init,
deinit,
RGBA,
setFill,
setStroke,
strokeWidth,
line,
rect,
ellipse,
circle,
background,
fill,
stroke,
checkVGError,
};