Remove unnecessary closure in the src/core/font_renderer.js file

With modern JavaScript modules, where you explicitly list the properties that should be exported, it's no longer necessary to wrap *all* of the code within one file into a top-level closure.[1]

This patch reduces the size, of even the *built* `pdf.worker.js` file, since there's now a lot less unnecessary whitespace.

---
[1] For files which contain *different* functionality, some closures may however still make sense in order to separate the code.
It might be possible to remove some of those cases later, e.g. once private class fields becomes generally available/usable in browsers.
This commit is contained in:
Jonas Jenwald 2021-05-05 22:20:10 +02:00
parent afb8c4fd25
commit 0a32ad3e42

View File

@ -25,21 +25,20 @@ import { getGlyphsUnicode } from "./glyphlist.js";
import { StandardEncoding } from "./encodings.js";
import { Stream } from "./stream.js";
const FontRendererFactory = (function FontRendererFactoryClosure() {
function getLong(data, offset) {
function getLong(data, offset) {
return (
(data[offset] << 24) |
(data[offset + 1] << 16) |
(data[offset + 2] << 8) |
data[offset + 3]
);
}
}
function getUshort(data, offset) {
function getUshort(data, offset) {
return (data[offset] << 8) | data[offset + 1];
}
}
function getSubroutineBias(subrs) {
function getSubroutineBias(subrs) {
const numSubrs = subrs.length;
let bias = 32768;
if (numSubrs < 1240) {
@ -48,9 +47,9 @@ const FontRendererFactory = (function FontRendererFactoryClosure() {
bias = 1131;
}
return bias;
}
}
function parseCmap(data, start, end) {
function parseCmap(data, start, end) {
const offset =
getUshort(data, start + 2) === 1
? getLong(data, start + 8)
@ -100,9 +99,9 @@ const FontRendererFactory = (function FontRendererFactoryClosure() {
return ranges;
}
throw new FormatError(`unsupported cmap: ${format}`);
}
}
function parseCff(data, start, end, seacAnalysisEnabled) {
function parseCff(data, start, end, seacAnalysisEnabled) {
const properties = {};
const parser = new CFFParser(
new Stream(data, start, end - start),
@ -121,9 +120,9 @@ const FontRendererFactory = (function FontRendererFactoryClosure() {
fdSelect: cff.fdSelect,
fdArray: cff.fdArray,
};
}
}
function parseGlyfTable(glyf, loca, isGlyphLocationsLong) {
function parseGlyfTable(glyf, loca, isGlyphLocationsLong) {
let itemSize, itemDecode;
if (isGlyphLocationsLong) {
itemSize = 4;
@ -149,9 +148,9 @@ const FontRendererFactory = (function FontRendererFactoryClosure() {
startOffset = endOffset;
}
return glyphs;
}
}
function lookupCmap(ranges, unicode) {
function lookupCmap(ranges, unicode) {
const code = unicode.codePointAt(0);
let gid = 0,
l = 0,
@ -174,9 +173,9 @@ const FontRendererFactory = (function FontRendererFactoryClosure() {
charCode: code,
glyphId: gid,
};
}
}
function compileGlyf(code, cmds, font) {
function compileGlyf(code, cmds, font) {
function moveTo(x, y) {
cmds.push({ cmd: "moveTo", args: [x, y] });
}
@ -220,8 +219,7 @@ const FontRendererFactory = (function FontRendererFactoryClosure() {
scale01 = 0,
scale10 = 0;
if (flags & 0x08) {
scaleX = scaleY =
((code[i] << 24) | (code[i + 1] << 16)) / 1073741824;
scaleX = scaleY = ((code[i] << 24) | (code[i + 1] << 16)) / 1073741824;
i += 2;
} else if (flags & 0x40) {
scaleX = ((code[i] << 24) | (code[i + 1] << 16)) / 1073741824;
@ -343,9 +341,9 @@ const FontRendererFactory = (function FontRendererFactoryClosure() {
startPoint = endPoint + 1;
}
}
}
}
function compileCharString(charStringCode, cmds, font, glyphId) {
function compileCharString(charStringCode, cmds, font, glyphId) {
function moveTo(x, y) {
cmds.push({ cmd: "moveTo", args: [x, y] });
}
@ -720,11 +718,11 @@ const FontRendererFactory = (function FontRendererFactoryClosure() {
}
}
parse(charStringCode);
}
}
const NOOP = [];
const NOOP = [];
class CompiledFont {
class CompiledFont {
constructor(fontMatrix) {
if (this.constructor === CompiledFont) {
unreachable("Cannot initialize CompiledFont.");
@ -789,9 +787,9 @@ const FontRendererFactory = (function FontRendererFactoryClosure() {
this.compiledCharCodeToGlyphId[cmap.charCode] !== undefined
);
}
}
}
class TrueTypeCompiled extends CompiledFont {
class TrueTypeCompiled extends CompiledFont {
constructor(glyphs, cmap, fontMatrix) {
super(fontMatrix || [0.000488, 0, 0, 0.000488, 0, 0]);
@ -802,9 +800,9 @@ const FontRendererFactory = (function FontRendererFactoryClosure() {
compileGlyphImpl(code, cmds) {
compileGlyf(code, cmds, this);
}
}
}
class Type2Compiled extends CompiledFont {
class Type2Compiled extends CompiledFont {
constructor(cffInfo, cmap, fontMatrix, glyphNameMap) {
super(fontMatrix || [0.001, 0, 0, 0.001, 0, 0]);
@ -825,10 +823,10 @@ const FontRendererFactory = (function FontRendererFactoryClosure() {
compileGlyphImpl(code, cmds, glyphId) {
compileCharString(code, cmds, this, glyphId);
}
}
}
return {
create: function FontRendererFactory_create(font, seacAnalysisEnabled) {
class FontRendererFactory {
static create(font, seacAnalysisEnabled) {
const data = new Uint8Array(font.data);
let cmap, glyf, loca, cff, indexToLocFormat, unitsPerEm;
const numTables = getUshort(data, 4);
@ -867,8 +865,7 @@ const FontRendererFactory = (function FontRendererFactoryClosure() {
);
}
return new Type2Compiled(cff, cmap, font.fontMatrix, font.glyphNameMap);
},
};
})();
}
}
export { FontRendererFactory };