Convert src/core/opentype_file_builder.js
to use standard classes
This commit is contained in:
parent
1808b2dc96
commit
8c1d1a58f7
@ -16,45 +16,43 @@
|
|||||||
import { readUint32 } from "./core_utils.js";
|
import { readUint32 } from "./core_utils.js";
|
||||||
import { string32 } from "../shared/util.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] = (num >> 8) & 0xff;
|
dest[offset + 1] = num & 0xff;
|
||||||
dest[offset + 1] = num & 0xff;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function writeInt32(dest, offset, num) {
|
function writeInt32(dest, offset, num) {
|
||||||
dest[offset] = (num >> 24) & 0xff;
|
dest[offset] = (num >> 24) & 0xff;
|
||||||
dest[offset + 1] = (num >> 16) & 0xff;
|
dest[offset + 1] = (num >> 16) & 0xff;
|
||||||
dest[offset + 2] = (num >> 8) & 0xff;
|
dest[offset + 2] = (num >> 8) & 0xff;
|
||||||
dest[offset + 3] = num & 0xff;
|
dest[offset + 3] = num & 0xff;
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeData(dest, offset, data) {
|
function writeData(dest, offset, data) {
|
||||||
let i, ii;
|
if (data instanceof Uint8Array) {
|
||||||
if (data instanceof Uint8Array) {
|
dest.set(data, offset);
|
||||||
dest.set(data, offset);
|
} else if (typeof data === "string") {
|
||||||
} else if (typeof data === "string") {
|
for (let i = 0, ii = data.length; i < ii; i++) {
|
||||||
for (i = 0, ii = data.length; i < ii; i++) {
|
dest[offset++] = data.charCodeAt(i) & 0xff;
|
||||||
dest[offset++] = data.charCodeAt(i) & 0xff;
|
}
|
||||||
}
|
} else {
|
||||||
} else {
|
// treating everything else as array
|
||||||
// treating everything else as array
|
for (let i = 0, ii = data.length; i < ii; i++) {
|
||||||
for (i = 0, ii = data.length; i < ii; i++) {
|
dest[offset++] = data[i] & 0xff;
|
||||||
dest[offset++] = data[i] & 0xff;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line no-shadow
|
const OTF_HEADER_SIZE = 12;
|
||||||
function OpenTypeFileBuilder(sfnt) {
|
const OTF_TABLE_ENTRY_SIZE = 16;
|
||||||
|
|
||||||
|
class OpenTypeFileBuilder {
|
||||||
|
constructor(sfnt) {
|
||||||
this.sfnt = sfnt;
|
this.sfnt = sfnt;
|
||||||
this.tables = Object.create(null);
|
this.tables = Object.create(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
OpenTypeFileBuilder.getSearchParams = function OpenTypeFileBuilder_getSearchParams(
|
static getSearchParams(entriesCount, entrySize) {
|
||||||
entriesCount,
|
|
||||||
entrySize
|
|
||||||
) {
|
|
||||||
let maxPower2 = 1,
|
let maxPower2 = 1,
|
||||||
log2 = 0;
|
log2 = 0;
|
||||||
while ((maxPower2 ^ entriesCount) > maxPower2) {
|
while ((maxPower2 ^ entriesCount) > maxPower2) {
|
||||||
@ -67,97 +65,90 @@ const OpenTypeFileBuilder = (function OpenTypeFileBuilderClosure() {
|
|||||||
entry: log2,
|
entry: log2,
|
||||||
rangeShift: entrySize * entriesCount - searchRange,
|
rangeShift: entrySize * entriesCount - searchRange,
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
const OTF_HEADER_SIZE = 12;
|
toArray() {
|
||||||
const OTF_TABLE_ENTRY_SIZE = 16;
|
let sfnt = this.sfnt;
|
||||||
|
|
||||||
OpenTypeFileBuilder.prototype = {
|
// Tables needs to be written by ascendant alphabetic order
|
||||||
toArray: function OpenTypeFileBuilder_toArray() {
|
const tables = this.tables;
|
||||||
let sfnt = this.sfnt;
|
const tablesNames = Object.keys(tables);
|
||||||
|
tablesNames.sort();
|
||||||
|
const numTables = tablesNames.length;
|
||||||
|
|
||||||
// Tables needs to be written by ascendant alphabetic order
|
let i, j, jj, table, tableName;
|
||||||
const tables = this.tables;
|
// layout the tables data
|
||||||
const tablesNames = Object.keys(tables);
|
let offset = OTF_HEADER_SIZE + numTables * OTF_TABLE_ENTRY_SIZE;
|
||||||
tablesNames.sort();
|
const tableOffsets = [offset];
|
||||||
const numTables = tablesNames.length;
|
for (i = 0; i < numTables; i++) {
|
||||||
|
table = tables[tablesNames[i]];
|
||||||
|
const paddedLength = ((table.length + 3) & ~3) >>> 0;
|
||||||
|
offset += paddedLength;
|
||||||
|
tableOffsets.push(offset);
|
||||||
|
}
|
||||||
|
|
||||||
let i, j, jj, table, tableName;
|
const file = new Uint8Array(offset);
|
||||||
// layout the tables data
|
// write the table data first (mostly for checksum)
|
||||||
let offset = OTF_HEADER_SIZE + numTables * OTF_TABLE_ENTRY_SIZE;
|
for (i = 0; i < numTables; i++) {
|
||||||
const tableOffsets = [offset];
|
table = tables[tablesNames[i]];
|
||||||
for (i = 0; i < numTables; i++) {
|
writeData(file, tableOffsets[i], table);
|
||||||
table = tables[tablesNames[i]];
|
}
|
||||||
const paddedLength = ((table.length + 3) & ~3) >>> 0;
|
|
||||||
offset += paddedLength;
|
// sfnt version (4 bytes)
|
||||||
tableOffsets.push(offset);
|
if (sfnt === "true") {
|
||||||
|
// Windows hates the Mac TrueType sfnt version number
|
||||||
|
sfnt = string32(0x00010000);
|
||||||
|
}
|
||||||
|
file[0] = sfnt.charCodeAt(0) & 0xff;
|
||||||
|
file[1] = sfnt.charCodeAt(1) & 0xff;
|
||||||
|
file[2] = sfnt.charCodeAt(2) & 0xff;
|
||||||
|
file[3] = sfnt.charCodeAt(3) & 0xff;
|
||||||
|
|
||||||
|
// numTables (2 bytes)
|
||||||
|
writeInt16(file, 4, numTables);
|
||||||
|
|
||||||
|
const searchParams = OpenTypeFileBuilder.getSearchParams(numTables, 16);
|
||||||
|
|
||||||
|
// searchRange (2 bytes)
|
||||||
|
writeInt16(file, 6, searchParams.range);
|
||||||
|
// entrySelector (2 bytes)
|
||||||
|
writeInt16(file, 8, searchParams.entry);
|
||||||
|
// rangeShift (2 bytes)
|
||||||
|
writeInt16(file, 10, searchParams.rangeShift);
|
||||||
|
|
||||||
|
offset = OTF_HEADER_SIZE;
|
||||||
|
// writing table entries
|
||||||
|
for (i = 0; i < numTables; i++) {
|
||||||
|
tableName = tablesNames[i];
|
||||||
|
file[offset] = tableName.charCodeAt(0) & 0xff;
|
||||||
|
file[offset + 1] = tableName.charCodeAt(1) & 0xff;
|
||||||
|
file[offset + 2] = tableName.charCodeAt(2) & 0xff;
|
||||||
|
file[offset + 3] = tableName.charCodeAt(3) & 0xff;
|
||||||
|
|
||||||
|
// checksum
|
||||||
|
let checksum = 0;
|
||||||
|
for (j = tableOffsets[i], jj = tableOffsets[i + 1]; j < jj; j += 4) {
|
||||||
|
const quad = readUint32(file, j);
|
||||||
|
checksum = (checksum + quad) >>> 0;
|
||||||
}
|
}
|
||||||
|
writeInt32(file, offset + 4, checksum);
|
||||||
|
|
||||||
const file = new Uint8Array(offset);
|
// offset
|
||||||
// write the table data first (mostly for checksum)
|
writeInt32(file, offset + 8, tableOffsets[i]);
|
||||||
for (i = 0; i < numTables; i++) {
|
// length
|
||||||
table = tables[tablesNames[i]];
|
writeInt32(file, offset + 12, tables[tableName].length);
|
||||||
writeData(file, tableOffsets[i], table);
|
|
||||||
}
|
|
||||||
|
|
||||||
// sfnt version (4 bytes)
|
offset += OTF_TABLE_ENTRY_SIZE;
|
||||||
if (sfnt === "true") {
|
}
|
||||||
// Windows hates the Mac TrueType sfnt version number
|
return file;
|
||||||
sfnt = string32(0x00010000);
|
}
|
||||||
}
|
|
||||||
file[0] = sfnt.charCodeAt(0) & 0xff;
|
|
||||||
file[1] = sfnt.charCodeAt(1) & 0xff;
|
|
||||||
file[2] = sfnt.charCodeAt(2) & 0xff;
|
|
||||||
file[3] = sfnt.charCodeAt(3) & 0xff;
|
|
||||||
|
|
||||||
// numTables (2 bytes)
|
addTable(tag, data) {
|
||||||
writeInt16(file, 4, numTables);
|
if (tag in this.tables) {
|
||||||
|
throw new Error("Table " + tag + " already exists");
|
||||||
const searchParams = OpenTypeFileBuilder.getSearchParams(numTables, 16);
|
}
|
||||||
|
this.tables[tag] = data;
|
||||||
// searchRange (2 bytes)
|
}
|
||||||
writeInt16(file, 6, searchParams.range);
|
}
|
||||||
// entrySelector (2 bytes)
|
|
||||||
writeInt16(file, 8, searchParams.entry);
|
|
||||||
// rangeShift (2 bytes)
|
|
||||||
writeInt16(file, 10, searchParams.rangeShift);
|
|
||||||
|
|
||||||
offset = OTF_HEADER_SIZE;
|
|
||||||
// writing table entries
|
|
||||||
for (i = 0; i < numTables; i++) {
|
|
||||||
tableName = tablesNames[i];
|
|
||||||
file[offset] = tableName.charCodeAt(0) & 0xff;
|
|
||||||
file[offset + 1] = tableName.charCodeAt(1) & 0xff;
|
|
||||||
file[offset + 2] = tableName.charCodeAt(2) & 0xff;
|
|
||||||
file[offset + 3] = tableName.charCodeAt(3) & 0xff;
|
|
||||||
|
|
||||||
// checksum
|
|
||||||
let checksum = 0;
|
|
||||||
for (j = tableOffsets[i], jj = tableOffsets[i + 1]; j < jj; j += 4) {
|
|
||||||
const quad = readUint32(file, j);
|
|
||||||
checksum = (checksum + quad) >>> 0;
|
|
||||||
}
|
|
||||||
writeInt32(file, offset + 4, checksum);
|
|
||||||
|
|
||||||
// offset
|
|
||||||
writeInt32(file, offset + 8, tableOffsets[i]);
|
|
||||||
// length
|
|
||||||
writeInt32(file, offset + 12, tables[tableName].length);
|
|
||||||
|
|
||||||
offset += OTF_TABLE_ENTRY_SIZE;
|
|
||||||
}
|
|
||||||
return file;
|
|
||||||
},
|
|
||||||
|
|
||||||
addTable: function OpenTypeFileBuilder_addTable(tag, data) {
|
|
||||||
if (tag in this.tables) {
|
|
||||||
throw new Error("Table " + tag + " already exists");
|
|
||||||
}
|
|
||||||
this.tables[tag] = data;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return OpenTypeFileBuilder;
|
|
||||||
})();
|
|
||||||
|
|
||||||
export { OpenTypeFileBuilder };
|
export { OpenTypeFileBuilder };
|
||||||
|
Loading…
Reference in New Issue
Block a user