-
Notifications
You must be signed in to change notification settings - Fork 0
/
svgMask.js
60 lines (49 loc) · 1.79 KB
/
svgMask.js
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
// svgMask.js
// ========
var Rsvg = require('librsvg-prebuilt').Rsvg;
var fs = require('fs');
var path = require('path');
module.exports = {
drawMaskAsync: async function(board, size, shapeFilename) {
var cWidth = size[0];
var cHeight = size[1];
return new Promise(resolve => {
if (!shapeFilename || shapeFilename.length===0){
console.log('(drawMaskAsync) no shape to mask');
resolve();
return;
}
console.log('(drawMaskAsync) reading shape '+shapeFilename);
var svg = new Rsvg();
svg.on('finish', function() {
console.log('(drawMaskAsync) SVG read (' + svg.width + ',' + svg.height + '), Screen (' + cWidth + ',' + cHeight + ')');
var buffer = svg.render({ format: 'raw', width: size[0], height: size[1] }).data;
var pixels = [...buffer];
// mask the board
var boardWidth = cWidth >> 5; // divide by 32
for (var yy = 0; yy < cHeight; yy++) {
for (var xx = 0; xx < cWidth; xx++) {
//var red = pixels[(cWidth * yy + xx) * 4];
//var green = pixels[(cWidth * yy + xx) * 4 + 1];
//var blue = pixels[(cWidth * yy + xx) * 4 + 2];
var alpha = pixels[(cWidth * yy + xx) * 4 + 3];
var isSet = alpha === 0;
if (isSet) {
var bit = xx - ((xx >> 5) << 5);
var bitVal = 1 << (31 - bit);
board[yy * boardWidth + (xx >> 5)] |= bitVal;
}
}
}
resolve();
});
var filename = path.join(__dirname, 'svg/fontawesome/svgs/solid', shapeFilename);
rs = fs.createReadStream(filename);
rs.on('error', e => {
console.log('(svgMask) - Failed to read file: ' + filename + '. ' + e);
resolve();
});
rs.pipe(svg);
});
}
};