-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
456 lines (436 loc) · 14.8 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
var lsi = require('line-segment-intersect-2d')
var vec2set = require('gl-vec2/set')
var vec2dist = require('gl-vec2/distance')
var varint = require('varint')
var magic = require('./lib/magic.js')
var bzri = require('./lib/bzri.js')
var mivxa = require('./lib/mivxa.js')
var raycast = require('./lib/raycast.js')
var bz = require('./lib/bz.js')
var bzli = require('./lib/bzli.js')
var ieee754 = require('ieee754')
var rect0 = [0,0,0,0], rect1 = [0,0,0,0]
var v0 = [0,0], v1 = [0,0], v2 = [0,0], v3 = [0,0], v4 = [0,0]
var l0 = [0,0,0,0], l1 = [0,0,0,0]
var origin = [0,0]
var padding0 = [0,0,0,0]
var defaultPadding = [0,0,0,0]
var defaultOffset = [0,0]
module.exports = QBZF
function QBZF(src, opts) {
if (!(this instanceof QBZF)) return new QBZF(src, opts)
if (!opts) opts = {}
this._glyphs = new Map
this._matches = new Map
this._iv = new Map
this._cells = new Map
this._index = 0
this._density = opts.density !== undefined ? opts.density : [200,200]
this.unitsPerEm = 0
this._epsilon = opts.epsilon !== undefined ? opts.epsilon : 1e-8
this._parse(src)
this.curves = this._buildCurves()
}
QBZF.prototype._parse = function (src) {
var offset = 0
for (; offset < src.length && src[offset] !== 0x0a; offset++);
if (!(vcmp(src,0,offset,magic,0,magic.length))) {
throw new Error('magic number not found. not a valid qbzf1 file.')
}
offset++
this.unitsPerEm = varint.decode(src, offset)
offset += varint.decode.bytes
while (offset < src.length) {
var xndigits = varint.decode(src, offset)
var ndigits = xndigits>>1
offset += varint.decode.bytes
var u0 = varint.decode(src, offset)
offset += varint.decode.bytes
var u1 = -1
var key = String(u0)
if (xndigits % 2 === 1) {
u1 = varint.decode(src, offset)
offset += varint.decode.bytes
key += ',' + String(u1)
}
var advanceWidth = decode(src, offset)
offset += varint.decode.bytes
var leftSideBearing = decode(src, offset)
offset += varint.decode.bytes
var xmin = decode(src, offset)
offset += varint.decode.bytes
var ymin = decode(src, offset)
offset += varint.decode.bytes
var xmax = decode(src, offset)
offset += varint.decode.bytes
var ymax = decode(src, offset)
offset += varint.decode.bytes
var px = 0, py = 0
var curves = [], indexes = []
for (var n = 0; n < ndigits; n++) {
var cx = decode(src, offset)
offset += varint.decode.bytes
var cxr = ((cx%3)+3)%3
cx = Math.floor(cx/3)
var cy = decode(src, offset)
offset += varint.decode.bytes
if (cxr === 0) { // M
px += cx
py += cy
} else if (cxr === 1) { // L
var c0 = px, c1 = py, c2 = cx+px, c3 = cy+py
if (Math.abs(c0-c2) < this._epsilon && Math.abs(c1-c3) < this._epsilon) continue
curves.push([c0,c1,c2,c3])
indexes.push(this._index++)
px += cx
py += cy
} else if (cxr === 2) { // Q
var nx = decode(src, offset)
offset += varint.decode.bytes
var ny = decode(src, offset)
offset += varint.decode.bytes
curves.push([ px, py, cx+px, cy+py, nx+px, ny+py ])
indexes.push(this._index++)
px += nx
py += ny
}
}
this._glyphs.set(key, {
curves,
indexes,
leftSideBearing,
advanceWidth,
bbox: [xmin,ymin,xmax,ymax],
})
}
}
QBZF.prototype._buildCurves = function () {
var w = Math.max(90,Math.floor(Math.sqrt(this._index)/3)*3)
var h = Math.max(1,Math.ceil(this._index/w))
var data = new Uint8Array(w*h*4)
for (var [key,g] of this._glyphs) {
for (var i = 0; i < g.curves.length; i++) {
var c = g.curves[i]
var offset = g.indexes[i]*3*4
if (c.length === 4) {
writeI16(data, offset+0, c[0]-g.bbox[0])
writeI16(data, offset+2, c[1]-g.bbox[1])
writeI16(data, offset+4, c[0]-g.bbox[0])
writeI16(data, offset+6, c[1]-g.bbox[1])
writeI16(data, offset+8, c[2]-g.bbox[0])
writeI16(data, offset+10, c[3]-g.bbox[1])
} else if (c.length === 6) {
writeI16(data, offset+0, c[0]-g.bbox[0])
writeI16(data, offset+2, c[1]-g.bbox[1])
writeI16(data, offset+4, c[2]-g.bbox[0])
writeI16(data, offset+6, c[3]-g.bbox[1])
writeI16(data, offset+8, c[4]-g.bbox[0])
writeI16(data, offset+10, c[5]-g.bbox[1])
}
}
}
return { data, width: w, height: h, size: [w,h] }
}
QBZF.prototype.measure = function (opts) {
var density = opts.density !== undefined ? opts.density : this._density
var padding = opts.padding !== undefined ? opts.padding : defaultPadding
var strokeWidth = opts.strokeWidth !== undefined ? opts.strokeWidth : 0
var xoffset = opts.offset !== undefined ? opts.offset : defaultOffset
if (padding.length === 2) {
padding = vec4set(padding0, padding[0], padding[1], padding[0], padding[1])
} else {
padding = vec4set(padding0, padding[0], padding[1], padding[2], padding[3])
}
padding[0] += strokeWidth
padding[1] += strokeWidth
padding[2] += strokeWidth
padding[3] += strokeWidth
var units = [0,0]
var bbox = [Infinity,Infinity,-Infinity,-Infinity]
var text = opts.text !== undefined ? opts.text : ''
for (var i = 0; i < text.length; i++) {
var code = text.charCodeAt(i)
var g = this._glyphs.get(String(code))
bbox[0] = Math.min(bbox[0], units[0] + g.bbox[0])
bbox[2] = Math.max(bbox[2], units[0] + g.bbox[2])
units[0] += g.advanceWidth
bbox[2] = Math.max(bbox[2], units[0])
bbox[1] = Math.min(bbox[1], g.bbox[1])
bbox[3] = Math.max(bbox[3], g.bbox[3])
}
units[0] = Math.max(units[0],bbox[2]) - bbox[0] + padding[0] + padding[2] + 2
units[1] = bbox[3] - bbox[1] + padding[1] + padding[3] + 2
var grid = [Math.ceil(units[0]/density[0]),Math.ceil(units[1]/density[1])]
var offset = [xoffset[0]+padding[0]-bbox[0]+1,xoffset[1]+padding[1]-bbox[1]+1]
return Object.assign({}, opts, { units, grid, offset, bbox })
}
QBZF.prototype.write = function (opts) {
opts = this.measure(opts)
var units = opts.units
var grid = opts.grid
var strokeWidth = opts.strokeWidth !== undefined ? opts.strokeWidth : 0
var text = opts.text
this._matches.clear()
var offset = opts.offset || origin
var x = offset[0]
var y = offset[1]
var cursor = { units, grid, strokeWidth, n: 0 }
for (var i = 0; i < text.length; i++) {
// todo: lookahead for multi-codepoint
var c = text.charCodeAt(i)
x += this._stamp(c, x, y, cursor)
}
var n = cursor.n
var q = n*3+2
var l = grid[0]*grid[1]*q
var width = Math.ceil(Math.sqrt(l)/q)*q
var height = Math.ceil(l/width)
var length = width * height * 4
var data = opts.data !== undefined ? opts.data : new Uint8Array(length)
if (data.length < length) {
throw new Error(`insufficient supplied data in qbzf.write. required: ${length} received: ${data.length}`)
}
if (data.length > length) {
data = data.subarray(0,length)
}
for (var gy = 0; gy < grid[1]; gy++) {
for (var gx = 0; gx < grid[0]; gx++) {
var gk = gx+gy*grid[0]
var cells = this._cells.get(gk)
if (cells !== undefined) {
for (var i = 0; i < cells.length; i++) {
var offset = (gk*(n*3+2)+2+i*3)*4
writeU24(data, offset+0, cells[i][0])
writeF32(data, offset+4, cells[i][1])
writeF32(data, offset+8, cells[i][2])
}
}
var iv = this._iv.get(gk)
if (iv !== undefined) {
var offset = (gx+gy*grid[0])*(n*3+2)*4
writeF32(data, offset+0, iv[0])
writeF32(data, offset+4, iv[1])
}
}
}
this._cells.clear()
this._iv.clear()
return {
data, width, height, dimension: [width,height],
units, grid, n, strokeWidth
}
}
QBZF.prototype._stamp = function (code, sx, sy, cursor) {
var units = cursor.units, grid = cursor.grid, cells = cursor.cells
var g = this._glyphs.get(String(code))
if (g === undefined) throw new Error(`todo: glyph or hook for code not found: ${code}`)
var px = sx + g.bbox[0], py = sy + g.bbox[1]
var swx = cursor.strokeWidth, swy = cursor.strokeWidth
var xstart = Math.max(0, Math.floor((px + g.bbox[0] - g.leftSideBearing - swx) / units[0] * grid[0]))
var xend = Math.ceil((px + g.bbox[2] - g.leftSideBearing + swx) / units[0] * grid[0])
var ystart = Math.max(0, Math.floor((sy + g.bbox[1] - swy) / units[1] * grid[1]))
var yend = Math.min(grid[1],Math.ceil((sy + g.bbox[3] + swy) / units[1] * grid[1]))
var sg0 = units[0]/grid[0]
var sg1 = units[1]/grid[1]
for (var gy = ystart; gy < yend; gy++) {
for (var gx = xstart; gx < xend; gx++) {
rect0[0] = gx/grid[0]*units[0]
rect0[1] = gy/grid[1]*units[1]
rect0[2] = (gx+1)/grid[0]*units[0]
rect0[3] = (gy+1)/grid[1]*units[1]
var r0 = rect0[0] - px + g.bbox[0]
var r1 = rect0[1] - py + g.bbox[1]
var r2 = rect0[2] - px + g.bbox[0]
var r3 = rect0[3] - py + g.bbox[1]
var gk = gx+gy*grid[0]
var m = this._matches.get(gk) || 0
var urc = 0
for (var i = 0; i < g.curves.length; i++) {
var c = g.curves[i]
urc += this._countRaycast(r2,r3,c,units[0]+1000)
}
var rc = []
for (var i = 0; i < g.curves.length; i++) {
var c = g.curves[i]
if (c.length === 4) {
vec2set(v1,c[0],c[1])
vec2set(v2,c[2],c[3])
vec2set(v3,r2,r1)
vec2set(v4,r2,r3)
if (Math.abs(r2-c[0]) < this._epsilon || Math.abs(r2-c[2]) < this._epsilon) {
v3[0] += this._epsilon
v4[0] += this._epsilon
}
if (lsi(v0,v1,v2,v3,v4)) {
rc.push(v0[1]+py-g.bbox[1])
}
} else {
vec4set(l0, r2, r1, r2, r3)
if (Math.abs(r2-c[0]) < this._epsilon || Math.abs(r2-c[4]) < this._epsilon) {
l0[0] += this._epsilon
l0[2] += this._epsilon
}
var ln = bzli(l1,c,l0,1e-8)
for (var j = 0; j < ln; j++) {
rc.push(l1[j*2+1]+py-g.bbox[1])
}
}
}
rc.sort(cmp)
var iv = this._iv.get(gk) || []
var q = 0
if (urc % 2 === 0 && rc.length === 0) {
q = 1
} else if (urc % 2 === 1 && rc.length === 0) {
q = 2
iv.push(rect0[1],rect0[3])
} else if (urc % 2 === 0 && rc.length % 2 === 0) {
q = 3
iv = iv.concat(rc)
} else if (urc % 2 === 0 && rc.length % 2 === 1) {
q = 4
iv.push(rect0[1])
iv = iv.concat(rc)
} else if (urc % 2 === 1 && rc.length % 2 === 0) {
q = 5
iv = iv.concat(rc)
iv.push(rect0[1],rect0[3])
} else if (urc % 2 === 1 && rc.length % 2 === 1) {
q = 6
iv = iv.concat(rc)
iv.push(rect0[3])
}
var cells = this._cells.get(gk)
if (cells === undefined) {
cells = []
this._cells.set(gk, cells)
}
vec4set(rect1, rect0[0]-swx, rect0[1]-swy, rect0[2]+swx, rect0[3]+swy)
for (var i = 0; i < g.curves.length; i++) {
var c = g.curves[i]
if (!curveRectIntersect(c,rect1,px-g.bbox[0],py-g.bbox[1])) {
continue
}
cells.push([g.indexes[i]+1,rect0[0]-px,rect0[1]-py])
cursor.n = Math.max(cursor.n, cells.length)
m++
}
this._matches.set(gk, m)
var y0 = rect0[1], y1 = rect0[1]
if (iv.length > 0) {
mivxa(iv, iv, vec2set(v0, rect0[1], rect0[3]), 1e-8)
if (iv.length === 2) {
y0 = iv[0] !== undefined ? iv[0] : rect0[1]
y1 = iv[1] !== undefined ? iv[1] : rect0[1]
} else if (iv.length === 4) {
iv.push(rect0[1], rect0[3])
mivxa(iv, iv, vec2set(v0, rect0[1], rect0[3]), 1e-8)
y1 = iv[0] !== undefined ? iv[0] : rect0[1]
y0 = iv[1] !== undefined ? iv[1] : rect0[1]
} else if (iv.length > 0) {
// console.log('TODO',gx,gy,iv)
}
}
this._iv.set(gk, [y0-rect0[1],y1-rect0[1]])
}
}
return g.advanceWidth
}
QBZF.prototype._countRaycast = function (x, y, c, xfar) {
vec2set(v0,x,y)
if (c.length === 6 && Math.abs(c[1]-v0[1]) < this._epsilon) {
v0[1] += this._epsilon
}
if (c.length === 6 && Math.abs(c[5]-v0[1]) < this._epsilon) {
v0[1] += this._epsilon
}
return countRaycast(v0, c, xfar, this._epsilon)
}
function decode(src, offset) {
var x = varint.decode(src, offset)
return x % 2 === 1 ? -(x-1)/2-1 : x/2
}
function vcmp(a,astart,aend,b,bstart,bend) {
if (bend-bstart !== aend-astart) return false
for (var i = 0; i < aend-astart; i++) {
if (a[i+astart] !== b[i+bstart]) return false
}
return true
}
function curveRectIntersect(c, rect, dx, dy) {
if (c.length === 4) { // line
var c0 = c[0]+dx, c1 = c[1]+dy, c2 = c[2]+dx, c3 = c[3]+dy
if (rect[0] <= c0 && c0 <= rect[2] && rect[1] <= c1 && c1 <= rect[3]) return true
if (rect[0] <= c2 && c2 <= rect[2] && rect[1] <= c3 && c3 <= rect[3]) return true
vec2set(v1, c0, c1)
vec2set(v2, c2, c3)
if (lsi(v0, v1, v2, vec2set(v3,rect[0],rect[1]), vec2set(v4,rect[0],rect[3]))) return true
if (lsi(v0, v1, v2, vec2set(v3,rect[0],rect[3]), vec2set(v4,rect[2],rect[3]))) return true
if (lsi(v0, v1, v2, vec2set(v3,rect[2],rect[3]), vec2set(v4,rect[2],rect[1]))) return true
if (lsi(v0, v1, v2, vec2set(v3,rect[2],rect[1]), vec2set(v4,rect[0],rect[1]))) return true
} else if (c.length === 6) { // quadratic bezier
return bzri(rect, c, dx, dy, 1e-8) // todo: padding for border width
}
return false
}
function writeU16(out, offset, x) {
out[offset+0] = (x >> 8) % 256
out[offset+1] = x % 256
}
function writeI16(out, offset, x) {
var ax = Math.abs(x)
out[offset+0] = (ax >> 8) % 128 + (x < 0 ? 128 : 0)
out[offset+1] = ax % 256
}
function writeU24(out, offset, x) {
out[offset+0] = (x >> 16) % 256
out[offset+1] = (x >> 8) % 256
out[offset+2] = x % 256
}
function writeI24(out, offset, x) {
var ax = Math.abs(x)
out[offset+0] = (ax >> 16) % 128 + (x < 0 ? 128 : 0)
out[offset+1] = ax >> 8
out[offset+2] = ax % 256
}
function writeF32(out, offset, x) {
ieee754.write(out, x, offset, false, 23, 4)
}
function countRaycast(p, c, xfar, epsilon) {
var x = p[0], y = p[1]
var count = 0
if (c.length === 4) {
vec2set(v1,c[0],c[1])
vec2set(v2,c[2],c[3])
vec2set(v3,p[0],p[1])
vec2set(v4,xfar,p[1])
if (collinear(v3,v4,v1,epsilon) || collinear(v3,v4,v2,epsilon)) {
// ...
} else if (lsi(v0,v1,v2,v3,v4)) count++
} else {
var n = raycast(v0, y, c[1], c[3], c[5])
if (n > 0) {
var x0 = bz(c[0],c[2],c[4],v0[0])
if (x0 > x) count++
}
if (n > 1) {
var x1 = bz(c[0],c[2],c[4],v0[1])
if (x1 > x) count++
}
}
return count
}
function collinear(a, b, c, epsilon) {
var ab = vec2dist(a,b)
var ac = vec2dist(a,c)
var bc = vec2dist(b,c)
return Math.abs(ab - ac - bc) < epsilon
}
function vec4set(out, a, b, c, d) {
out[0] = a
out[1] = b
out[2] = c
out[3] = d
return out
}
function cmp(a,b) { return a < b ? -1 : +1 }