Convert src/core/opentype_file_builder.js to use standard classes

This commit is contained in:
Jonas Jenwald 2021-05-02 11:54:05 +02:00
parent 1808b2dc96
commit 8c1d1a58f7

View File

@ -16,45 +16,43 @@
import { readUint32 } from "./core_utils.js";
import { string32 } from "../shared/util.js";
const OpenTypeFileBuilder = (function OpenTypeFileBuilderClosure() {
function writeInt16(dest, offset, num) {
function writeInt16(dest, offset, num) {
dest[offset] = (num >> 8) & 0xff;
dest[offset + 1] = num & 0xff;
}
}
function writeInt32(dest, offset, num) {
function writeInt32(dest, offset, num) {
dest[offset] = (num >> 24) & 0xff;
dest[offset + 1] = (num >> 16) & 0xff;
dest[offset + 2] = (num >> 8) & 0xff;
dest[offset + 3] = num & 0xff;
}
}
function writeData(dest, offset, data) {
let i, ii;
function writeData(dest, offset, data) {
if (data instanceof Uint8Array) {
dest.set(data, offset);
} else if (typeof data === "string") {
for (i = 0, ii = data.length; i < ii; i++) {
for (let i = 0, ii = data.length; i < ii; i++) {
dest[offset++] = data.charCodeAt(i) & 0xff;
}
} else {
// treating everything else as array
for (i = 0, ii = data.length; i < ii; i++) {
for (let i = 0, ii = data.length; i < ii; i++) {
dest[offset++] = data[i] & 0xff;
}
}
}
}
// eslint-disable-next-line no-shadow
function OpenTypeFileBuilder(sfnt) {
const OTF_HEADER_SIZE = 12;
const OTF_TABLE_ENTRY_SIZE = 16;
class OpenTypeFileBuilder {
constructor(sfnt) {
this.sfnt = sfnt;
this.tables = Object.create(null);
}
OpenTypeFileBuilder.getSearchParams = function OpenTypeFileBuilder_getSearchParams(
entriesCount,
entrySize
) {
static getSearchParams(entriesCount, entrySize) {
let maxPower2 = 1,
log2 = 0;
while ((maxPower2 ^ entriesCount) > maxPower2) {
@ -67,13 +65,9 @@ const OpenTypeFileBuilder = (function OpenTypeFileBuilderClosure() {
entry: log2,
rangeShift: entrySize * entriesCount - searchRange,
};
};
}
const OTF_HEADER_SIZE = 12;
const OTF_TABLE_ENTRY_SIZE = 16;
OpenTypeFileBuilder.prototype = {
toArray: function OpenTypeFileBuilder_toArray() {
toArray() {
let sfnt = this.sfnt;
// Tables needs to be written by ascendant alphabetic order
@ -147,17 +141,14 @@ const OpenTypeFileBuilder = (function OpenTypeFileBuilderClosure() {
offset += OTF_TABLE_ENTRY_SIZE;
}
return file;
},
}
addTable: function OpenTypeFileBuilder_addTable(tag, data) {
addTable(tag, data) {
if (tag in this.tables) {
throw new Error("Table " + tag + " already exists");
}
this.tables[tag] = data;
},
};
return OpenTypeFileBuilder;
})();
}
}
export { OpenTypeFileBuilder };