-
Notifications
You must be signed in to change notification settings - Fork 6
/
SvgTable.js
180 lines (165 loc) · 5.38 KB
/
SvgTable.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
function SvgTable() {
this.documents = [];
return this;
}
SvgTable.prototype.getDocumentCount = function() {
return this.documents.length;
};
SvgTable.prototype.getDocumentText = function (index) {
if (index >= this.documents.length) {
throw "Invalid document index";
}
return this.documents[index].text;
};
SvgTable.prototype.toArrayBuffer = function () {
var numDocuments = this.documents.length;
if (numDocuments > 0xFFFF) {
throw "Too many documents";
}
var docIndexOffset = 10;
var docOffset = 2 + 12*numDocuments;
var encodedTexts = [];
var encoder = new TextEncoder();
var docOffsets = [];
var docLengths = [];
for (var i = 0; i < numDocuments; ++i) {
for (var j = 0; j < i; ++j) {
if (this.documents[j].text == this.documents[i].text) {
break;
}
}
if (j < i) {
// Reuse existing document text
docOffsets.push(docOffsets[j]);
docLengths.push(docLengths[j]);
encodedTexts.push(null);
} else {
docOffsets.push(docOffset);
encodedTexts.push(encoder.encode(this.documents[i].text));
docOffset += encodedTexts[i].byteLength;
docLengths.push(encodedTexts[i].byteLength);
}
}
if (length > 0xFFFFFFFF) {
throw "Table size overflow";
}
var buf = new ArrayBuffer(docIndexOffset + docOffset);
var headerView = new DataView(buf);
headerView.setUint16(0, 0);
headerView.setUint32(2, docIndexOffset);
headerView.setUint32(6, 0);
headerView.setUint16(docIndexOffset, numDocuments);
var offset = docIndexOffset + 2;
for (var i = 0; i < numDocuments; ++i) {
headerView.setUint16(offset, this.documents[i].startGlyphId);
headerView.setUint16(offset + 2, this.documents[i].endGlyphId);
headerView.setUint32(offset + 4, docOffsets[i]);
var textLength = docLengths[i];
headerView.setUint32(offset + 8, textLength);
if (encodedTexts[i]) {
(new Uint8Array(buf, docIndexOffset + docOffsets[i], textLength)).set(encodedTexts[i]);
}
offset += 12;
}
return buf;
};
SvgTable.fromDocuments = function (documents, options) {
if (documents.length == 0) {
throw "Must have at least one document";
}
var glyphMapping = [];
var parser = new DOMParser();
for (var i = 0; i < documents.length; ++i) {
var doc = parser.parseFromString(documents[i], "image/svg+xml");
var parseErrs = doc.getElementsByTagName("parsererror");
if (parseErrs.length > 0) {
throw "Parse error: " + parseErrs[0].textContent;
}
var elementsWithGlyphIds = doc.querySelectorAll("[id]");
var glyphIds = [];
for (var j = 0; j < elementsWithGlyphIds.length; ++j) {
var m = /^glyph([1-9][0-9]*)$/.exec(elementsWithGlyphIds[j].getAttribute("id"));
if (m) {
glyphMapping.push({id:m[1], doc:documents[i]});
}
}
}
if (glyphMapping.length == 0) {
throw "No elements with id 'glyphNNN' found";
}
glyphMapping.sort(function (a, b) {
return a.id - b.id;
});
var table = new SvgTable();
var startOfRun = 0;
for (var j = 0; j < glyphMapping.length; ++j) {
if (j > 0 && glyphMapping[j].id == glyphMapping[j - 1].id) {
if (options && options.ignoreDuplicateGlyphIds) {
glyphMapping.splice(j, 1);
continue;
}
throw "Duplicate glyphs found for glyph ID " + glyphMapping[j].id;
}
if (j == glyphMapping.length - 1 ||
glyphMapping[j + 1].id != glyphMapping[j].id + 1 ||
glyphMapping[j + 1].doc != glyphMapping[j].doc) {
// End of a run
table.documents.push({
startGlyphId:glyphMapping[startOfRun].id,
endGlyphId:glyphMapping[j].id,
text:glyphMapping[j].doc
});
startOfRun = j + 1;
}
}
return table;
};
SvgTable.fromTable = function (dataView) {
if (dataView.getUint16(0) != 0) {
throw "Unknown table version";
}
if (dataView.getUint32(6) != 0) {
throw "Color palettes not supported yet";
}
var docIndexOffset = dataView.getUint32(2);
if (docIndexOffset + 2 > dataView.byteLength) {
throw "Document index out of range";
}
var numEntries = dataView.getUint16(docIndexOffset);
if (docIndexOffset + 2 + numEntries*12 > dataView.byteLength) {
throw "Document index out of range";
}
if (numEntries == 0) {
throw "Must have at least one document";
}
var table = new SvgTable();
var offset = docIndexOffset + 2;
var textDecoder = new TextDecoder();
for (var i = 0; i < numEntries; ++i) {
var startGlyphId = dataView.getUint16(offset);
var endGlyphId = dataView.getUint16(offset + 2);
if (startGlyphId > endGlyphId) {
throw "Misordered glyph ids";
}
if (i > 0 && table.documents[i - 1].endGlyphId >= startGlyphId) {
throw "Misordered glyph ids with previous document";
}
var docOffset = dataView.getUint32(offset + 4);
var docLength = dataView.getUint32(offset + 8);
if (docOffset + docLength > dataView.byteLength) {
throw "Document end out of range";
}
var docText = textDecoder.decode(
new DataView(dataView.buffer, dataView.byteOffset + docIndexOffset + docOffset, docLength));
table.documents.push({
startGlyphId:startGlyphId,
endGlyphId:endGlyphId,
text:docText
});
offset += 12;
}
return table;
};