Merge pull request #13095 from Snuffleupagus/eslint-cmapscompress

Enable linting of the `external/cmapscompress/` folder
This commit is contained in:
Tim van der Meij 2021-03-14 11:40:50 +01:00 committed by GitHub
commit 67b866b9ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 329 additions and 247 deletions

View File

@ -4,7 +4,6 @@ docs/
node_modules/ node_modules/
external/bcmaps/ external/bcmaps/
external/webL10n/ external/webL10n/
external/cmapscompress/
external/builder/fixtures/ external/builder/fixtures/
external/builder/fixtures_esprima/ external/builder/fixtures_esprima/
external/quickjs/ external/quickjs/

10
external/cmapscompress/.eslintrc vendored Normal file
View File

@ -0,0 +1,10 @@
{
"extends": [
"../.eslintrc"
],
"rules": {
// ECMAScript 6
"no-var": "error",
},
}

View File

@ -13,161 +13,195 @@
* limitations under the License. * limitations under the License.
*/ */
var fs = require('fs'); const fs = require("fs");
var path = require('path'); const path = require("path");
var parseAdobeCMap = require('./parse.js').parseAdobeCMap; const parseAdobeCMap = require("./parse.js").parseAdobeCMap;
var optimizeCMap = require('./optimize.js').optimizeCMap; const optimizeCMap = require("./optimize.js").optimizeCMap;
function compressCmap(srcPath, destPath, verify) { function compressCmap(srcPath, destPath, verify) {
var content = fs.readFileSync(srcPath).toString(); const content = fs.readFileSync(srcPath).toString();
var inputData = parseAdobeCMap(content); const inputData = parseAdobeCMap(content);
optimizeCMap(inputData); optimizeCMap(inputData);
var out = writeByte((inputData.type << 1) | inputData.wmode); let out = writeByte((inputData.type << 1) | inputData.wmode);
if (inputData.comment) { if (inputData.comment) {
out += writeByte(0xE0) + writeString(inputData.comment); out += writeByte(0xe0) + writeString(inputData.comment);
} }
if (inputData.usecmap) { if (inputData.usecmap) {
out += writeByte(0xE1) + writeString(inputData.usecmap); out += writeByte(0xe1) + writeString(inputData.usecmap);
} }
var i = 0; let i = 0;
while (i < inputData.body.length) { while (i < inputData.body.length) {
var item = inputData.body[i++], subitems = item.items; const item = inputData.body[i++],
var first = item.items[0]; subitems = item.items;
var sequence = item.sequence === true; const first = item.items[0];
var flags = (item.type << 5) | (sequence ? 0x10 : 0); const sequence = item.sequence === true;
var nextStart, nextCode; const flags = (item.type << 5) | (sequence ? 0x10 : 0);
let nextStart, nextCode;
switch (item.type) { switch (item.type) {
case 0: case 0:
out += writeByte(flags | getHexSize(first.start)) + writeNumber(subitems.length); out +=
writeByte(flags | getHexSize(first.start)) +
writeNumber(subitems.length);
out += first.start + writeNumber(subHex(first.end, first.start)); out += first.start + writeNumber(subHex(first.end, first.start));
nextStart = incHex(first.end); nextStart = incHex(first.end);
for (var j = 1; j < subitems.length; j++) { for (let j = 1; j < subitems.length; j++) {
out += writeNumber(subHex(subitems[j].start, nextStart)) + out +=
writeNumber(subHex(subitems[j].start, nextStart)) +
writeNumber(subHex(subitems[j].end, subitems[j].start)); writeNumber(subHex(subitems[j].end, subitems[j].start));
nextStart = incHex(subitems[j].end); nextStart = incHex(subitems[j].end);
} }
break; break;
case 1: case 1:
out += writeByte(flags | getHexSize(first.start)) + writeNumber(subitems.length); out +=
out += first.start + writeNumber(subHex(first.end, first.start)) + writeNumber(first.code); writeByte(flags | getHexSize(first.start)) +
writeNumber(subitems.length);
out +=
first.start +
writeNumber(subHex(first.end, first.start)) +
writeNumber(first.code);
nextStart = incHex(first.end); nextStart = incHex(first.end);
for (var j = 1; j < subitems.length; j++) { for (let j = 1; j < subitems.length; j++) {
out += writeNumber(subHex(subitems[j].start, nextStart)) + out +=
writeNumber(subHex(subitems[j].start, nextStart)) +
writeNumber(subHex(subitems[j].end, subitems[j].start)) + writeNumber(subHex(subitems[j].end, subitems[j].start)) +
writeNumber(subitems[j].code); writeNumber(subitems[j].code);
nextStart = incHex(subitems[j].end); nextStart = incHex(subitems[j].end);
} }
break; break;
case 2: case 2:
out += writeByte(flags | getHexSize(first.char)) + writeNumber(subitems.length); out +=
writeByte(flags | getHexSize(first.char)) +
writeNumber(subitems.length);
out += first.char + writeNumber(first.code); out += first.char + writeNumber(first.code);
nextStart = incHex(first.char); nextStart = incHex(first.char);
nextCode = first.code + 1; nextCode = first.code + 1;
for (var j = 1; j < subitems.length; j++) { for (let j = 1; j < subitems.length; j++) {
out += (sequence ? '' : writeNumber(subHex(subitems[j].char, nextStart))) + out +=
(sequence ? "" : writeNumber(subHex(subitems[j].char, nextStart))) +
writeSigned(subitems[j].code - nextCode); writeSigned(subitems[j].code - nextCode);
nextStart = incHex(subitems[j].char); nextStart = incHex(subitems[j].char);
nextCode = item.items[j].code + 1; nextCode = item.items[j].code + 1;
} }
break; break;
case 3: case 3:
out += writeByte(flags | getHexSize(first.start)) + writeNumber(subitems.length); out +=
out += first.start + writeNumber(subHex(first.end, first.start)) + writeNumber(first.code); writeByte(flags | getHexSize(first.start)) +
writeNumber(subitems.length);
out +=
first.start +
writeNumber(subHex(first.end, first.start)) +
writeNumber(first.code);
nextStart = incHex(first.end); nextStart = incHex(first.end);
for (var j = 1; j < subitems.length; j++) { for (let j = 1; j < subitems.length; j++) {
out += (sequence ? '' : writeNumber(subHex(subitems[j].start, nextStart))) + out +=
(sequence
? ""
: writeNumber(subHex(subitems[j].start, nextStart))) +
writeNumber(subHex(subitems[j].end, subitems[j].start)) + writeNumber(subHex(subitems[j].end, subitems[j].start)) +
writeNumber(subitems[j].code); writeNumber(subitems[j].code);
nextStart = incHex(subitems[j].end); nextStart = incHex(subitems[j].end);
} }
break; break;
case 4: case 4:
out += writeByte(flags | getHexSize(first.code)) + writeNumber(subitems.length); out +=
writeByte(flags | getHexSize(first.code)) +
writeNumber(subitems.length);
out += first.char + first.code; out += first.char + first.code;
nextStart = incHex(first.char); nextStart = incHex(first.char);
nextCode = incHex(first.code); nextCode = incHex(first.code);
for (var j = 1; j < subitems.length; j++) { for (let j = 1; j < subitems.length; j++) {
out += (sequence ? '' : writeNumber(subHex(subitems[j].char, nextStart))) + out +=
(sequence ? "" : writeNumber(subHex(subitems[j].char, nextStart))) +
writeSigned(subHex(subitems[j].code, nextCode)); writeSigned(subHex(subitems[j].code, nextCode));
nextStart = incHex(subitems[j].char); nextStart = incHex(subitems[j].char);
nextCode = incHex(subitems[j].code); nextCode = incHex(subitems[j].code);
} }
break; break;
case 5: case 5:
out += writeByte(flags | getHexSize(first.code)) + writeNumber(subitems.length); out +=
out += first.start + writeNumber(subHex(first.end, first.start)) + first.code; writeByte(flags | getHexSize(first.code)) +
writeNumber(subitems.length);
out +=
first.start +
writeNumber(subHex(first.end, first.start)) +
first.code;
nextStart = incHex(first.end); nextStart = incHex(first.end);
for (var j = 1; j < subitems.length; j++) { for (let j = 1; j < subitems.length; j++) {
out += (sequence ? '' : writeNumber(subHex(subitems[j].start, nextStart))) + out +=
(sequence
? ""
: writeNumber(subHex(subitems[j].start, nextStart))) +
writeNumber(subHex(subitems[j].end, subitems[j].start)) + writeNumber(subHex(subitems[j].end, subitems[j].start)) +
subitems[j].code; subitems[j].code;
nextStart = incHex(subitems[j].end); nextStart = incHex(subitems[j].end);
} }
break; break;
} }
} }
fs.writeFileSync(destPath, new Buffer(out, 'hex')); fs.writeFileSync(destPath, Buffer.from(out, "hex"));
if (verify) { if (verify) {
var result2 = parseCMap(out); const result2 = parseCMap(out);
var isGood = JSON.stringify(inputData) == JSON.stringify(result2); const isGood = JSON.stringify(inputData) === JSON.stringify(result2);
if (!isGood) { if (!isGood) {
throw new Error('Extracted data does not match the expected result'); throw new Error("Extracted data does not match the expected result");
} }
} }
return { return {
orig: fs.statSync(srcPath).size, orig: fs.statSync(srcPath).size,
packed: out.length >> 1 packed: out.length >> 1,
}; };
} }
function parseCMap(binaryData) { function parseCMap(binaryData) {
var reader = { const reader = {
buffer: binaryData, buffer: binaryData,
pos: 0, pos: 0,
end: binaryData.length, end: binaryData.length,
readByte: function () { readByte() {
if (this.pos >= this.end) { if (this.pos >= this.end) {
return -1; return -1;
} }
var d1 = fromHexDigit(this.buffer[this.pos]); const d1 = fromHexDigit(this.buffer[this.pos]);
var d2 = fromHexDigit(this.buffer[this.pos + 1]); const d2 = fromHexDigit(this.buffer[this.pos + 1]);
this.pos += 2; this.pos += 2;
return (d1 << 4) | d2; return (d1 << 4) | d2;
}, },
readNumber: function () { readNumber() {
var n = 0; let n = 0;
var last; let last;
do { do {
var b = this.readByte(); const b = this.readByte();
last = !(b & 0x80); last = !(b & 0x80);
n = (n << 7) | (b & 0x7F); n = (n << 7) | (b & 0x7f);
} while (!last); } while (!last);
return n; return n;
}, },
readSigned: function () { readSigned() {
var n = this.readNumber(); const n = this.readNumber();
return (n & 1) ? -(n >>> 1) - 1 : n >>> 1; return n & 1 ? -(n >>> 1) - 1 : n >>> 1;
}, },
readHex: function (size) { readHex(size) {
var lengthInChars = (size + 1) << 1; const lengthInChars = (size + 1) << 1;
var s = this.buffer.substring(this.pos, this.pos + lengthInChars); const s = this.buffer.substring(this.pos, this.pos + lengthInChars);
this.pos += lengthInChars; this.pos += lengthInChars;
return s; return s;
}, },
readHexNumber: function (size) { readHexNumber(size) {
var lengthInChars = (size + 1) << 1; const lengthInChars = (size + 1) << 1,
var stack = []; stack = [];
let last;
do { do {
var b = this.readByte(); const b = this.readByte();
last = !(b & 0x80); last = !(b & 0x80);
stack.push(b & 0x7F); stack.push(b & 0x7f);
} while (!last); } while (!last);
var s = '', buffer = 0, bufferSize = 0; let s = "",
buffer = 0,
bufferSize = 0;
while (s.length < lengthInChars) { while (s.length < lengthInChars) {
while (bufferSize < 4 && stack.length > 0) { while (bufferSize < 4 && stack.length > 0) {
buffer = (stack.pop() << bufferSize) | buffer; buffer = (stack.pop() << bufferSize) | buffer;
@ -179,42 +213,42 @@ function parseCMap(binaryData) {
} }
return s; return s;
}, },
readHexSigned: function (size) { readHexSigned(size) {
var num = this.readHexNumber(size); const num = this.readHexNumber(size);
var sign = fromHexDigit(num[num.length - 1]) & 1 ? 15 : 0; const sign = fromHexDigit(num[num.length - 1]) & 1 ? 15 : 0;
var c = 0; let c = 0;
var result = ''; let result = "";
for (var i = 0; i < num.length; i++) { for (let i = 0; i < num.length; i++) {
c = (c << 4) | fromHexDigit(num[i]); c = (c << 4) | fromHexDigit(num[i]);
result += toHexDigit(sign ? (c >> 1) ^ sign : (c >> 1)); result += toHexDigit(sign ? (c >> 1) ^ sign : c >> 1);
c &= 1; c &= 1;
} }
return result; return result;
}, },
readString: function () { readString() {
var len = this.readNumber(); const len = this.readNumber();
var s = ''; let s = "";
for (var i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
s += String.fromCharCode(this.readNumber()); s += String.fromCharCode(this.readNumber());
} }
return s; return s;
} },
}; };
var header = reader.readByte(); const header = reader.readByte();
var result = { const result = {
type: header >> 1, type: header >> 1,
wmode: header & 1, wmode: header & 1,
comment: null, comment: null,
usecmap: null, usecmap: null,
body: [] body: [],
}; };
var b; let b;
while ((b = reader.readByte()) >= 0) { while ((b = reader.readByte()) >= 0) {
var type = b >> 5; const type = b >> 5;
if (type === 7) { if (type === 7) {
switch (b & 0x1F) { switch (b & 0x1f) {
case 0: case 0:
result.comment = reader.readString(); result.comment = reader.readString();
break; break;
@ -224,88 +258,96 @@ function parseCMap(binaryData) {
} }
continue; continue;
} }
var sequence = !!(b & 0x10); const sequence = !!(b & 0x10);
var dataSize = b & 15; const dataSize = b & 15;
var subitems = []; const subitems = [];
var item = { const item = {
type: type, type,
items: subitems items: subitems,
}; };
if (sequence) { if (sequence) {
item.sequence = true; item.sequence = true;
} }
var ucs2DataSize = 1; const ucs2DataSize = 1;
var subitemsCount = reader.readNumber(); const subitemsCount = reader.readNumber();
var start, end, code, char; let start, end, code, char;
switch (type) { switch (type) {
case 0: case 0:
start = reader.readHex(dataSize); start = reader.readHex(dataSize);
end = addHex(reader.readHexNumber(dataSize), start); end = addHex(reader.readHexNumber(dataSize), start);
subitems.push({start: start, end: end}); subitems.push({ start, end });
for (var i = 1; i < subitemsCount; i++) { for (let i = 1; i < subitemsCount; i++) {
start = addHex(reader.readHexNumber(dataSize), incHex(end)); start = addHex(reader.readHexNumber(dataSize), incHex(end));
end = addHex(reader.readHexNumber(dataSize), start); end = addHex(reader.readHexNumber(dataSize), start);
subitems.push({start: start, end: end}); subitems.push({ start, end });
} }
break; break;
case 1: case 1:
start = reader.readHex(dataSize); start = reader.readHex(dataSize);
end = addHex(reader.readHexNumber(dataSize), start); end = addHex(reader.readHexNumber(dataSize), start);
code = reader.readNumber(); code = reader.readNumber();
subitems.push({start: start, end: end, code: code}); subitems.push({ start, end, code });
for (var i = 1; i < subitemsCount; i++) { for (let i = 1; i < subitemsCount; i++) {
start = addHex(reader.readHexNumber(dataSize), incHex(end)); start = addHex(reader.readHexNumber(dataSize), incHex(end));
end = addHex(reader.readHexNumber(dataSize), start); end = addHex(reader.readHexNumber(dataSize), start);
code = reader.readNumber(); code = reader.readNumber();
subitems.push({start: start, end: end, code: code}); subitems.push({ start, end, code });
} }
break; break;
case 2: case 2:
char = reader.readHex(dataSize); char = reader.readHex(dataSize);
code = reader.readNumber(); code = reader.readNumber();
subitems.push({char: char, code: code}); subitems.push({ char, code });
for (var i = 1; i < subitemsCount; i++) { for (let i = 1; i < subitemsCount; i++) {
char = sequence ? incHex(char) : addHex(reader.readHexNumber(dataSize), incHex(char)); char = sequence
? incHex(char)
: addHex(reader.readHexNumber(dataSize), incHex(char));
code = reader.readSigned() + (code + 1); code = reader.readSigned() + (code + 1);
subitems.push({char: char, code: code}); subitems.push({ char, code });
} }
break; break;
case 3: case 3:
start = reader.readHex(dataSize); start = reader.readHex(dataSize);
end = addHex(reader.readHexNumber(dataSize), start); end = addHex(reader.readHexNumber(dataSize), start);
code = reader.readNumber(); code = reader.readNumber();
subitems.push({start: start, end: end, code: code}); subitems.push({ start, end, code });
for (var i = 1; i < subitemsCount; i++) { for (let i = 1; i < subitemsCount; i++) {
start = sequence ? incHex(end) : addHex(reader.readHexNumber(dataSize), incHex(end)); start = sequence
? incHex(end)
: addHex(reader.readHexNumber(dataSize), incHex(end));
end = addHex(reader.readHexNumber(dataSize), start); end = addHex(reader.readHexNumber(dataSize), start);
code = reader.readNumber(); code = reader.readNumber();
subitems.push({start: start, end: end, code: code}); subitems.push({ start, end, code });
} }
break; break;
case 4: case 4:
char = reader.readHex(ucs2DataSize); char = reader.readHex(ucs2DataSize);
code = reader.readHex(dataSize); code = reader.readHex(dataSize);
subitems.push({char: char, code: code}); subitems.push({ char, code });
for (var i = 1; i < subitemsCount; i++) { for (let i = 1; i < subitemsCount; i++) {
char = sequence ? incHex(char) : addHex(reader.readHexNumber(ucs2DataSize), incHex(char)); char = sequence
? incHex(char)
: addHex(reader.readHexNumber(ucs2DataSize), incHex(char));
code = addHex(reader.readHexSigned(dataSize), incHex(code)); code = addHex(reader.readHexSigned(dataSize), incHex(code));
subitems.push({char: char, code: code}); subitems.push({ char, code });
} }
break; break;
case 5: case 5:
start = reader.readHex(ucs2DataSize); start = reader.readHex(ucs2DataSize);
end = addHex(reader.readHexNumber(ucs2DataSize), start); end = addHex(reader.readHexNumber(ucs2DataSize), start);
code = reader.readHex(dataSize); code = reader.readHex(dataSize);
subitems.push({start: start, end: end, code: code}); subitems.push({ start, end, code });
for (var i = 1; i < subitemsCount; i++) { for (let i = 1; i < subitemsCount; i++) {
start = sequence ? incHex(end) : addHex(reader.readHexNumber(ucs2DataSize), incHex(end)); start = sequence
? incHex(end)
: addHex(reader.readHexNumber(ucs2DataSize), incHex(end));
end = addHex(reader.readHexNumber(ucs2DataSize), start); end = addHex(reader.readHexNumber(ucs2DataSize), start);
code = reader.readHex(dataSize); code = reader.readHex(dataSize);
subitems.push({start: start, end: end, code: code}); subitems.push({ start, end, code });
} }
break; break;
default: default:
throw new Error('Unknown type: ' + type) throw new Error("Unknown type: " + type);
} }
result.body.push(item); result.body.push(item);
} }
@ -326,9 +368,11 @@ function writeByte(b) {
return toHexDigit((b >> 4) & 15) + toHexDigit(b & 15); return toHexDigit((b >> 4) & 15) + toHexDigit(b & 15);
} }
function writeNumber(n) { function writeNumber(n) {
if (typeof n === 'string') { if (typeof n === "string") {
var s = '', buffer = 0, bufferSize = 0; let s = "",
var i = n.length; buffer = 0,
bufferSize = 0;
let i = n.length;
while (i > 0) { while (i > 0) {
--i; --i;
buffer = (fromHexDigit(n[i]) << bufferSize) | buffer; buffer = (fromHexDigit(n[i]) << bufferSize) | buffer;
@ -342,29 +386,28 @@ function writeNumber(n) {
if (buffer > 0) { if (buffer > 0) {
s = writeByte((buffer & 0x7f) | (s.length > 0 ? 0x80 : 0)) + s; s = writeByte((buffer & 0x7f) | (s.length > 0 ? 0x80 : 0)) + s;
} }
while (s.indexOf('80') === 0) { while (s.indexOf("80") === 0) {
s = s.substring(2); s = s.substring(2);
} }
return s; return s;
} else {
var s = writeByte(n & 0x7F);
n >>>= 7;
while (n > 0) {
s = writeByte((n & 0x7F) | 0x80) + s;
n >>>= 7;
}
return s;
} }
let s = writeByte(n & 0x7f);
n >>>= 7;
while (n > 0) {
s = writeByte((n & 0x7f) | 0x80) + s;
n >>>= 7;
}
return s;
} }
function writeSigned(n) { function writeSigned(n) {
if (typeof n === 'string') { if (typeof n === "string") {
var t = ''; let t = "";
var c = fromHexDigit(n[0]); let c = fromHexDigit(n[0]);
var neg = c >= 8; const neg = c >= 8;
c = neg ? (c ^ 15) : c; c = neg ? c ^ 15 : c;
for (var i = 1; i < n.length; i++) { for (let i = 1; i < n.length; i++) {
var d = fromHexDigit(n[i]); const d = fromHexDigit(n[i]);
c = (c << 4) | (neg ? (d ^ 15) : d); c = (c << 4) | (neg ? d ^ 15 : d);
t += toHexDigit(c >> 3); t += toHexDigit(c >> 3);
c = c & 7; c = c & 7;
} }
@ -374,15 +417,16 @@ function writeSigned(n) {
return n < 0 ? writeNumber(-2 * n - 1) : writeNumber(2 * n); return n < 0 ? writeNumber(-2 * n - 1) : writeNumber(2 * n);
} }
function writeString(s) { function writeString(s) {
var t = writeNumber(s.length); let t = writeNumber(s.length);
for (var i = 0; i < s.length; i++) { for (let i = 0; i < s.length; i++) {
t += writeNumber(s.charCodeAt(i)); t += writeNumber(s.charCodeAt(i));
} }
return t; return t;
} }
function addHex(a, b) { function addHex(a, b) {
var c = 0, s = ''; let c = 0,
for (var i = a.length - 1; i >= 0; i--) { s = "";
for (let i = a.length - 1; i >= 0; i--) {
c += fromHexDigit(a[i]) + fromHexDigit(b[i]); c += fromHexDigit(a[i]) + fromHexDigit(b[i]);
if (c >= 16) { if (c >= 16) {
s = toHexDigit(c - 16) + s; s = toHexDigit(c - 16) + s;
@ -395,8 +439,9 @@ function addHex(a, b) {
return s; return s;
} }
function subHex(a, b) { function subHex(a, b) {
var c = 0, s = ''; let c = 0,
for (var i = a.length - 1; i >= 0; i--) { s = "";
for (let i = a.length - 1; i >= 0; i--) {
c += fromHexDigit(a[i]) - fromHexDigit(b[i]); c += fromHexDigit(a[i]) - fromHexDigit(b[i]);
if (c < 0) { if (c < 0) {
s = toHexDigit(c + 16) + s; s = toHexDigit(c + 16) + s;
@ -409,8 +454,9 @@ function subHex(a, b) {
return s; return s;
} }
function incHex(a) { function incHex(a) {
var c = 1, s = ''; let c = 1,
for (var i = a.length - 1; i >= 0; i--) { s = "";
for (let i = a.length - 1; i >= 0; i--) {
c += fromHexDigit(a[i]); c += fromHexDigit(a[i]);
if (c >= 16) { if (c >= 16) {
s = toHexDigit(c - 16) + s; s = toHexDigit(c - 16) + s;
@ -424,14 +470,23 @@ function incHex(a) {
} }
exports.compressCmaps = function (src, dest, verify) { exports.compressCmaps = function (src, dest, verify) {
var files = fs.readdirSync(src).filter(function (fn) { const files = fs.readdirSync(src).filter(function (fn) {
return fn.indexOf('.') < 0; // skipping files with the extension return !fn.includes("."); // skipping files with the extension
}); });
files.forEach(function (fn) { files.forEach(function (fn) {
var srcPath = path.join(src, fn); const srcPath = path.join(src, fn);
var destPath = path.join(dest, fn + '.bcmap'); const destPath = path.join(dest, fn + ".bcmap");
var stats = compressCmap(srcPath, destPath, verify); const stats = compressCmap(srcPath, destPath, verify);
console.log('Compressing ' + fn + ': ' + stats.orig + ' vs ' + stats.packed + console.log(
' ' + (stats.packed / stats.orig * 100).toFixed(1) + '%'); "Compressing " +
fn +
": " +
stats.orig +
" vs " +
stats.packed +
" " +
((stats.packed / stats.orig) * 100).toFixed(1) +
"%"
);
}); });
}; };

View File

@ -14,27 +14,30 @@
*/ */
exports.optimizeCMap = function (data) { exports.optimizeCMap = function (data) {
var i = 1; let i = 1;
while (i < data.body.length) { while (i < data.body.length) {
if (data.body[i - 1].type === data.body[i].type) { if (data.body[i - 1].type === data.body[i].type) {
data.body[i - 1].items = data.body[i - 1].items.concat(data.body[i].items); data.body[i - 1].items = data.body[i - 1].items.concat(
data.body[i].items
);
data.body.splice(i, 1); data.body.splice(i, 1);
} else { } else {
i++; i++;
} }
} }
// split into groups with different lengths // split into groups with different lengths
var i = 0; i = 0;
while (i < data.body.length) { while (i < data.body.length) {
var item = data.body[i]; const item = data.body[i];
var keys = Object.keys(item.items[0]).filter(function (i) { const keys = Object.keys(item.items[0]).filter(function (val) {
return typeof item.items[0][i] === 'string'; return typeof item.items[0][val] === "string";
}); });
var j = 1; let j = 1;
while (j < item.items.length) { while (j < item.items.length) {
var different = false; let different = false;
for (var q = 0; q < keys.length && !different; q++) { for (let q = 0; q < keys.length && !different; q++) {
different = item.items[j - 1][keys[q]].length !== item.items[j][keys[q]].length; different =
item.items[j - 1][keys[q]].length !== item.items[j][keys[q]].length;
} }
if (different) { if (different) {
break; break;
@ -44,41 +47,44 @@ exports.optimizeCMap = function (data) {
if (j < item.items.length) { if (j < item.items.length) {
data.body.splice(i + 1, 0, { data.body.splice(i + 1, 0, {
type: item.type, type: item.type,
items: item.items.splice(j, item.items.length - j) items: item.items.splice(j, item.items.length - j),
}); });
} }
i++; i++;
} }
// find sequences of single char ranges // find sequences of single char ranges
var i = 0; i = 0;
while (i < data.body.length) { while (i < data.body.length) {
var item = data.body[i]; const item = data.body[i];
if (item.type === 3 || item.type === 5) { if (item.type === 3 || item.type === 5) {
var j = 0; let j = 0;
while (j < item.items.length) { while (j < item.items.length) {
var q = j; const q = j;
while (j < item.items.length && item.items[j].start === item.items[j].end) { while (
j < item.items.length &&
item.items[j].start === item.items[j].end
) {
j++; j++;
} }
if ((j - q) >= 9) { if (j - q >= 9) {
if (j < item.items.length) { if (j < item.items.length) {
data.body.splice(i + 1, 0, { data.body.splice(i + 1, 0, {
type: item.type, type: item.type,
items: item.items.splice(j, item.items.length - j) items: item.items.splice(j, item.items.length - j),
}); });
} }
if (q > 0) { if (q > 0) {
data.body.splice(i + 1, 0, { data.body.splice(i + 1, 0, {
type: item.type - 1, type: item.type - 1,
items: item.items.splice(q, j - q).map(function (i) { items: item.items.splice(q, j - q).map(function (val) {
return {char: i.start, code: i.code }; return { char: val.start, code: val.code };
}) }),
}); });
i++; i++;
} else { } else {
item.type -= 1; item.type -= 1;
item.items = item.items.map(function (i) { item.items = item.items.map(function (val) {
return {char: i.start, code: i.code }; return { char: val.start, code: val.code };
}); });
} }
continue; continue;
@ -89,31 +95,34 @@ exports.optimizeCMap = function (data) {
i++; i++;
} }
// find sequences of increasing code/ranges order // find sequences of increasing code/ranges order
var i = 0; i = 0;
while (i < data.body.length) { while (i < data.body.length) {
var item = data.body[i]; const item = data.body[i];
if (item.type >= 2 && item.type <= 5) { if (item.type >= 2 && item.type <= 5) {
var j = 1; let j = 1;
var startProp = item.type === 2 || item.type === 4 ? 'char' : 'start'; const startProp = item.type === 2 || item.type === 4 ? "char" : "start";
var endProp = item.type === 2 || item.type === 4 ? 'char' : 'end'; const endProp = item.type === 2 || item.type === 4 ? "char" : "end";
while (j < item.items.length) { while (j < item.items.length) {
var q = j - 1; const q = j - 1;
while (j < item.items.length && incHex(item.items[j - 1][endProp]) === item.items[j][startProp]) { while (
j < item.items.length &&
incHex(item.items[j - 1][endProp]) === item.items[j][startProp]
) {
j++; j++;
} }
if ((j - q) >= 9) { if (j - q >= 9) {
if (j < item.items.length) { if (j < item.items.length) {
data.body.splice(i + 1, 0, { data.body.splice(i + 1, 0, {
type: item.type, type: item.type,
items: item.items.splice(j, item.items.length - j) items: item.items.splice(j, item.items.length - j),
}); });
} }
if (q > 0) { if (q > 0) {
data.body.splice(i + 1, 0, { data.body.splice(i + 1, 0, {
type: item.type, type: item.type,
items: item.items.splice(q, j - q), items: item.items.splice(q, j - q),
sequence: true sequence: true,
}); });
i++; i++;
} else { } else {
@ -128,61 +137,68 @@ exports.optimizeCMap = function (data) {
} }
// split non-sequences two groups where codes are close // split non-sequences two groups where codes are close
var i = 0; i = 0;
while (i < data.body.length) { while (i < data.body.length) {
var item = data.body[i]; const item = data.body[i];
if (!item.sequence && (item.type === 2 || item.type === 3)) { if (!item.sequence && (item.type === 2 || item.type === 3)) {
var subitems = item.items; const subitems = item.items;
var codes = subitems.map(function (i) { const codes = subitems.map(function (val) {
return i.code; return val.code;
}); });
codes.sort(function (a, b) { codes.sort(function (a, b) {
return a - b; return a - b;
}); });
var maxDistance = 100, minItems = 10, itemsPerBucket = 50; const maxDistance = 100,
if (subitems.length > minItems && codes[codes.length - 1] - codes[0] > maxDistance) { minItems = 10,
var gapsCount = Math.max(2, (subitems.length / itemsPerBucket) | 0); itemsPerBucket = 50;
var gaps = []; if (
for (var q = 0; q < gapsCount; q++) { subitems.length > minItems &&
gaps.push({length: 0}); codes[codes.length - 1] - codes[0] > maxDistance
) {
const gapsCount = Math.max(2, (subitems.length / itemsPerBucket) | 0);
const gaps = [];
for (let q = 0; q < gapsCount; q++) {
gaps.push({ length: 0 });
} }
for (var j = 1; j < codes.length; j++) { for (let j = 1; j < codes.length; j++) {
var gapLength = codes[j] - codes[j - 1]; const gapLength = codes[j] - codes[j - 1];
var q = 0; let q = 0;
while (q < gaps.length && gaps[q].length > gapLength) { while (q < gaps.length && gaps[q].length > gapLength) {
q++; q++;
} }
if (q >= gaps.length) { if (q >= gaps.length) {
continue; continue;
} }
var q0 = q; let q0 = q;
while (q < gaps.length) { while (q < gaps.length) {
if (gaps[q].length < gaps[q0].length) { if (gaps[q].length < gaps[q0].length) {
q0 = q; q0 = q;
} }
q++; q++;
} }
gaps[q0] = {length: gapLength, boundary: codes[j]}; gaps[q0] = { length: gapLength, boundary: codes[j] };
} }
var groups = gaps.filter(function (g) { const groups = gaps
return g.length >= maxDistance; .filter(function (g) {
}).map(function (g) { return g.length >= maxDistance;
})
.map(function (g) {
return g.boundary; return g.boundary;
}); });
groups.sort(function (a, b) { groups.sort(function (a, b) {
return a - b; return a - b;
}); });
if (groups.length > 1) { if (groups.length > 1) {
var buckets = [item.items = []]; const buckets = [(item.items = [])];
for (var j = 0; j < groups.length; j++) { for (let j = 0; j < groups.length; j++) {
var newItem = {type: item.type, items: []} const newItem = { type: item.type, items: [] };
buckets.push(newItem.items); buckets.push(newItem.items);
i++; i++;
data.body.splice(i, 0, newItem); data.body.splice(i, 0, newItem);
} }
for (var j = 0; j < subitems.length; j++) { for (let j = 0; j < subitems.length; j++) {
var code = subitems[j].code; const code = subitems[j].code;
var q = 0; let q = 0;
while (q < groups.length && groups[q] <= code) { while (q < groups.length && groups[q] <= code) {
q++; q++;
} }
@ -196,11 +212,12 @@ exports.optimizeCMap = function (data) {
}; };
function incHex(a) { function incHex(a) {
var c = 1, s = ''; let c = 1,
for (var i = a.length - 1; i >= 0; i--) { s = "";
for (let i = a.length - 1; i >= 0; i--) {
c += parseInt(a[i], 16); c += parseInt(a[i], 16);
if (c >= 16) { if (c >= 16) {
s = '0' + s; s = "0" + s;
c = 1; c = 1;
} else { } else {
s = c.toString(16) + s; s = c.toString(16) + s;

View File

@ -14,88 +14,89 @@
*/ */
exports.parseAdobeCMap = function (content) { exports.parseAdobeCMap = function (content) {
var m = /(\bbegincmap\b[\s\S]*?)\bendcmap\b/.exec(content); let m = /(\bbegincmap\b[\s\S]*?)\bendcmap\b/.exec(content);
if (!m) { if (!m) {
throw new Error('cmap was not found'); throw new Error("cmap was not found");
} }
var body = m[1].replace(/\r\n?/g, '\n'); const body = m[1].replace(/\r\n?/g, "\n");
var result = { const result = {
type: 1, type: 1,
wmode: 0, wmode: 0,
comment: 'Copyright 1990-2009 Adobe Systems Incorporated.\nAll rights reserved.\nSee ./LICENSE', comment:
"Copyright 1990-2009 Adobe Systems Incorporated.\nAll rights reserved.\nSee ./LICENSE",
usecmap: null, usecmap: null,
body: [] body: [],
}; };
m = /\/CMapType\s+(\d+)+\s+def\b/.exec(body); m = /\/CMapType\s+(\d+)+\s+def\b/.exec(body);
result.type = +m[1]; result.type = +m[1];
m = /\/WMode\s+(\d+)+\s+def\b/.exec(body); m = /\/WMode\s+(\d+)+\s+def\b/.exec(body);
result.wmode = +m[1]; result.wmode = +m[1];
m = /\/([\w\-]+)\s+usecmap\b/.exec(body); m = /\/([\w-]+)\s+usecmap\b/.exec(body);
if (m) { if (m) {
result.usecmap = m[1]; result.usecmap = m[1];
} }
var re = /(\d+)\s+(begincodespacerange|beginnotdefrange|begincidchar|begincidrange|beginbfchar|beginbfrange)\n([\s\S]*?)\n(endcodespacerange|endnotdefrange|endcidchar|endcidrange|endbfchar|endbfrange)/g; const re = /(\d+)\s+(begincodespacerange|beginnotdefrange|begincidchar|begincidrange|beginbfchar|beginbfrange)\n([\s\S]*?)\n(endcodespacerange|endnotdefrange|endcidchar|endcidrange|endbfchar|endbfrange)/g;
while (m = re.exec(body)) { while ((m = re.exec(body))) {
var lines = m[3].toLowerCase().split('\n'); const lines = m[3].toLowerCase().split("\n");
var m2;
switch (m[2]) { switch (m[2]) {
case 'begincodespacerange': case "begincodespacerange":
result.body.push({ result.body.push({
type: 0, type: 0,
items: lines.map(function (line) { items: lines.map(function (line) {
var m = /<(\w+)>\s+<(\w+)>/.exec(line); const m2 = /<(\w+)>\s+<(\w+)>/.exec(line);
return {start: m[1], end: m[2]}; return { start: m2[1], end: m2[2] };
}) }),
}); });
break; break;
case 'beginnotdefrange': case "beginnotdefrange":
result.body.push({ result.body.push({
type: 1, type: 1,
items: lines.map(function (line) { items: lines.map(function (line) {
var m = /<(\w+)>\s+<(\w+)>\s+(\d+)/.exec(line); const m2 = /<(\w+)>\s+<(\w+)>\s+(\d+)/.exec(line);
return {start: m[1], end: m[2], code: +m[3]}; return { start: m2[1], end: m2[2], code: +m2[3] };
}) }),
}); });
break; break;
case 'begincidchar': case "begincidchar":
result.body.push({ result.body.push({
type: 2, type: 2,
items: lines.map(function (line) { items: lines.map(function (line) {
var m = /<(\w+)>\s+(\d+)/.exec(line); const m2 = /<(\w+)>\s+(\d+)/.exec(line);
return {char: m[1], code: +m[2]}; return { char: m2[1], code: +m2[2] };
}) }),
}); });
break; break;
case 'begincidrange': case "begincidrange":
result.body.push({ result.body.push({
type: 3, type: 3,
items: lines.map(function (line) { items: lines.map(function (line) {
var m = /<(\w+)>\s+<(\w+)>\s+(\d+)/.exec(line); const m2 = /<(\w+)>\s+<(\w+)>\s+(\d+)/.exec(line);
return {start: m[1], end: m[2], code: +m[3]}; return { start: m2[1], end: m2[2], code: +m2[3] };
}) }),
}); });
break; break;
case 'beginbfchar': case "beginbfchar":
result.body.push({ result.body.push({
type: 4, type: 4,
items: lines.map(function (line) { items: lines.map(function (line) {
var m = /<(\w+)>\s+<(\w+)>/.exec(line); const m2 = /<(\w+)>\s+<(\w+)>/.exec(line);
return {char: m[1], code: m[2]}; return { char: m2[1], code: m2[2] };
}) }),
}); });
break; break;
case 'beginbfrange': case "beginbfrange":
result.body.push({ result.body.push({
type: 5, type: 5,
items: lines.map(function (line) { items: lines.map(function (line) {
var m = /<(\w+)>\s+<(\w+)>\s+<(\w+)>/.exec(line); const m2 = /<(\w+)>\s+<(\w+)>\s+<(\w+)>/.exec(line);
return {start: m[1], end: m[2], code: m[3]}; return { start: m2[1], end: m2[2], code: m2[3] };
}) }),
}); });
break; break;
} }
} }
return result; return result;
}; };