-
Notifications
You must be signed in to change notification settings - Fork 58
/
index.js
92 lines (73 loc) · 3.12 KB
/
index.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
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
'use strict';
module.exports = TinySDF;
module.exports.default = TinySDF;
var INF = 1e20;
function TinySDF(fontSize, buffer, radius, cutoff, fontFamily, fontWeight) {
this.fontSize = fontSize || 24;
this.buffer = buffer === undefined ? 3 : buffer;
this.cutoff = cutoff || 0.25;
this.fontFamily = fontFamily || 'sans-serif';
this.fontWeight = fontWeight || 'normal';
this.radius = radius || 8;
var size = this.size = this.fontSize + this.buffer * 2;
this.canvas = document.createElement('canvas');
this.canvas.width = this.canvas.height = size;
this.ctx = this.canvas.getContext('2d');
this.ctx.font = this.fontWeight + ' ' + this.fontSize + 'px ' + this.fontFamily;
this.ctx.textBaseline = 'middle';
this.ctx.fillStyle = 'black';
// temporary arrays for the distance transform
this.gridOuter = new Float64Array(size * size);
this.gridInner = new Float64Array(size * size);
this.f = new Float64Array(size);
this.z = new Float64Array(size + 1);
this.v = new Uint16Array(size);
// hack around https://bugzilla.mozilla.org/show_bug.cgi?id=737852
this.middle = Math.round((size / 2) * (navigator.userAgent.indexOf('Gecko/') >= 0 ? 1.2 : 1));
}
TinySDF.prototype.draw = function (char) {
this.ctx.clearRect(0, 0, this.size, this.size);
this.ctx.fillText(char, this.buffer, this.middle);
var imgData = this.ctx.getImageData(0, 0, this.size, this.size);
var alphaChannel = new Uint8ClampedArray(this.size * this.size);
for (var i = 0; i < this.size * this.size; i++) {
var a = imgData.data[i * 4 + 3] / 255; // alpha value
this.gridOuter[i] = a === 1 ? 0 : a === 0 ? INF : Math.pow(Math.max(0, 0.5 - a), 2);
this.gridInner[i] = a === 1 ? INF : a === 0 ? 0 : Math.pow(Math.max(0, a - 0.5), 2);
}
edt(this.gridOuter, this.size, this.size, this.f, this.v, this.z);
edt(this.gridInner, this.size, this.size, this.f, this.v, this.z);
for (i = 0; i < this.size * this.size; i++) {
var d = Math.sqrt(this.gridOuter[i]) - Math.sqrt(this.gridInner[i]);
alphaChannel[i] = Math.round(255 - 255 * (d / this.radius + this.cutoff));
}
return alphaChannel;
};
// 2D Euclidean squared distance transform by Felzenszwalb & Huttenlocher https://cs.brown.edu/~pff/papers/dt-final.pdf
function edt(data, width, height, f, v, z) {
for (var x = 0; x < width; x++) edt1d(data, x, width, height, f, v, z);
for (var y = 0; y < height; y++) edt1d(data, y * width, 1, width, f, v, z);
}
// 1D squared distance transform
function edt1d(grid, offset, stride, length, f, v, z) {
var q, k, s, r;
v[0] = 0;
z[0] = -INF;
z[1] = INF;
for (q = 0; q < length; q++) f[q] = grid[offset + q * stride];
for (q = 1, k = 0, s = 0; q < length; q++) {
do {
r = v[k];
s = (f[q] - f[r] + q * q - r * r) / (q - r) / 2;
} while (s <= z[k] && --k > -1);
k++;
v[k] = q;
z[k] = s;
z[k + 1] = INF;
}
for (q = 0, k = 0; q < length; q++) {
while (z[k + 1] < q) k++;
r = v[k];
grid[offset + q * stride] = f[r] + (q - r) * (q - r);
}
}