-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (56 loc) · 1.79 KB
/
Copy pathindex.js
File metadata and controls
67 lines (56 loc) · 1.79 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
'use strict';
var protobuf = require('protocol-buffers');
var fs = require('fs');
var path = require('path');
var messages = protobuf(fs.readFileSync(path.join(__dirname, './proto/glyphs.proto')));
function debug(buffer, decode) {
if (decode) buffer = messages.glyphs.decode(buffer);
return JSON.stringify(buffer, function(k, v) {
if (k !== 'bitmap') return v;
return v ? v.data.length : v;
}, 2);
}
/**
* Combine any number of glyph (SDF) PBFs.
* Returns a re-encoded PBF with the combined
* font faces, composited using array order
* to determine glyph priority.
* @param {array} buffers An array of SDF PBFs.
*/
function combine(buffers, fontstack) {
var result,
coverage = {};
if (!buffers || buffers.length === 0) return;
for (var i = 0, j; i < buffers.length; i++) {
var buf = buffers[i];
var decoded = messages.glyphs.decode(buf);
var glyphs = decoded.stacks[0].glyphs;
if (!result) {
for (j = 0; j < glyphs.length; j++) {
coverage[glyphs[j].id] = true;
}
result = decoded;
} else {
for (j = 0; j < glyphs.length; j++) {
var glyph = glyphs[j];
if (!coverage[glyph.id]) {
result.stacks[0].glyphs.push(glyph);
coverage[glyph.id] = true;
}
}
result.stacks[0].name += ', ' + decoded.stacks[0].name;
}
}
if (fontstack) result.stacks[0].name = fontstack;
result.stacks[0].glyphs.sort(compareId);
return messages.glyphs.encode(result);
}
function compareId(a, b) {
return a.id - b.id;
}
module.exports = {
combine: combine,
debug: debug,
encode: messages.glyphs.encode,
decode: messages.glyphs.decode
};