-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCmapTable.js
92 lines (80 loc) · 2.45 KB
/
CmapTable.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
/* 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/. */
// XXX this doesn't work yet
function CmapTable() {
return this;
}
function cmapFormat4CharsToGlyph(string, dataView) {
if (string.length > 1) {
return -1;
}
var charCode = string.charCodeAt(0);
var segCount = dataView.getUint16(6) >> 1;
var offset = 14;
for (var i = 0; i < segCount; ++i) {
if (dataView.getUint16(offset) >= charCode &&
dataView.getUint16(14 + segCount*2 + 2 + i*2) <= charCode) {
return -1; // TODO fix
}
offset += 2;
}
}
function cmapFormat6CharsToGlyph(string, dataView) {
if (string.length > 1) {
return -1;
}
return -1; // TODO fix
}
CmapTable.prototype.charsToGlyph = function (string) {
var encodings = this.dataView.getUint16(2);
if (4 + encodings*8 > this.dataView.byteLength) {
throw "cmap table too short";
}
var offset = 4;
for (var i = 0; i < encodings; ++i) {
var subtableOffset = this.dataView.getUint32(4 + i*8 + 4);
if (subtableOffset + 4 > this.dataView.byteLength) {
throw "cmap table too short";
}
var format = this.dataView.getUint16(subtableOffset);
switch (format) {
case 4:
var length = this.dataView.getUint16(subtableOffset + 2);
if (subtableOffset + length > this.dataView.byteLength) {
throw "cmap table too short";
}
var glyphId = cmapFormat4CharsToGlyph(string,
new DataView(this.dataView.buffer, this.dataView.byteOffset + subtableOffset, length));
if (glyphId >= 0) {
return glyphId;
}
break;
case 6:
var length = this.dataView.getUint16(subtableOffset + 2);
if (subtableOffset + length > this.dataView.byteLength) {
throw "cmap table too short";
}
var glyphId = cmapFormat6CharsToGlyph(string,
new DataView(this.dataView.buffer, this.dataView.byteOffset + subtableOffset, length));
if (glyphId >= 0) {
return glyphId;
}
break;
default: throw "cmap format " + format + " not supported";
}
offset += 8;
}
return -1;
};
CmapTable.fromTable = function (dataView) {
if (dataView.byteLength < 4) {
throw "cmap table too short";
}
if (dataView.getUint16(0) != 0) {
throw "Unknown cmap table version";
}
var table = new CmapTable();
table.dataView = dataView;
return table;
};