Convert CompiledFont, TrueTypeCompiled, and Type2Compiled to ES6 classes

Also changes `var` to `let`/`const` in code already touched in the patch.
This commit is contained in:
Jonas Jenwald 2018-07-08 22:16:05 +02:00
parent 775763a091
commit ba1af46709

View File

@ -14,7 +14,7 @@
*/ */
import { import {
bytesToString, FONT_IDENTITY_MATRIX, FormatError, unreachable, Util, warn bytesToString, FONT_IDENTITY_MATRIX, FormatError, unreachable, warn
} from '../shared/util'; } from '../shared/util';
import { CFFParser } from './cff_parser'; import { CFFParser } from './cff_parser';
import { getGlyphsUnicode } from './glyphlist'; import { getGlyphsUnicode } from './glyphlist';
@ -618,15 +618,20 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
const NOOP = []; const NOOP = [];
function CompiledFont(fontMatrix) { class CompiledFont {
constructor(fontMatrix) {
if (this.constructor === CompiledFont) {
unreachable('Cannot initialize CompiledFont.');
}
this.fontMatrix = fontMatrix;
this.compiledGlyphs = Object.create(null); this.compiledGlyphs = Object.create(null);
this.compiledCharCodeToGlyphId = Object.create(null); this.compiledCharCodeToGlyphId = Object.create(null);
this.fontMatrix = fontMatrix;
} }
CompiledFont.prototype = {
getPathJs(unicode) { getPathJs(unicode) {
var cmap = lookupCmap(this.cmap, unicode); const cmap = lookupCmap(this.cmap, unicode);
var fn = this.compiledGlyphs[cmap.glyphId]; let fn = this.compiledGlyphs[cmap.glyphId];
if (!fn) { if (!fn) {
fn = this.compileGlyph(this.glyphs[cmap.glyphId], cmap.glyphId); fn = this.compileGlyph(this.glyphs[cmap.glyphId], cmap.glyphId);
this.compiledGlyphs[cmap.glyphId] = fn; this.compiledGlyphs[cmap.glyphId] = fn;
@ -635,7 +640,7 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
this.compiledCharCodeToGlyphId[cmap.charCode] = cmap.glyphId; this.compiledCharCodeToGlyphId[cmap.charCode] = cmap.glyphId;
} }
return fn; return fn;
}, }
compileGlyph(code, glyphId) { compileGlyph(code, glyphId) {
if (!code || code.length === 0 || code[0] === 14) { if (!code || code.length === 0 || code[0] === 14) {
@ -655,7 +660,7 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
} }
} }
var cmds = []; const cmds = [];
cmds.push({ cmd: 'save', }); cmds.push({ cmd: 'save', });
cmds.push({ cmd: 'transform', args: fontMatrix.slice(), }); cmds.push({ cmd: 'transform', args: fontMatrix.slice(), });
cmds.push({ cmd: 'scale', args: ['size', '-size'], }); cmds.push({ cmd: 'scale', args: ['size', '-size'], });
@ -665,36 +670,35 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
cmds.push({ cmd: 'restore', }); cmds.push({ cmd: 'restore', });
return cmds; return cmds;
}, }
compileGlyphImpl() { compileGlyphImpl() {
unreachable('Children classes should implement this.'); unreachable('Children classes should implement this.');
}, }
hasBuiltPath(unicode) { hasBuiltPath(unicode) {
var cmap = lookupCmap(this.cmap, unicode); const cmap = lookupCmap(this.cmap, unicode);
return (this.compiledGlyphs[cmap.glyphId] !== undefined && return (this.compiledGlyphs[cmap.glyphId] !== undefined &&
this.compiledCharCodeToGlyphId[cmap.charCode] !== undefined); this.compiledCharCodeToGlyphId[cmap.charCode] !== undefined);
}, }
}; }
function TrueTypeCompiled(glyphs, cmap, fontMatrix) { class TrueTypeCompiled extends CompiledFont {
fontMatrix = fontMatrix || [0.000488, 0, 0, 0.000488, 0, 0]; constructor(glyphs, cmap, fontMatrix) {
CompiledFont.call(this, fontMatrix); super(fontMatrix || [0.000488, 0, 0, 0.000488, 0, 0]);
this.glyphs = glyphs; this.glyphs = glyphs;
this.cmap = cmap; this.cmap = cmap;
} }
Util.inherit(TrueTypeCompiled, CompiledFont, {
compileGlyphImpl(code, cmds) { compileGlyphImpl(code, cmds) {
compileGlyf(code, cmds, this); compileGlyf(code, cmds, this);
}, }
}); }
function Type2Compiled(cffInfo, cmap, fontMatrix, glyphNameMap) { class Type2Compiled extends CompiledFont {
fontMatrix = fontMatrix || [0.001, 0, 0, 0.001, 0, 0]; constructor(cffInfo, cmap, fontMatrix, glyphNameMap) {
CompiledFont.call(this, fontMatrix); super(fontMatrix || [0.001, 0, 0, 0.001, 0, 0]);
this.glyphs = cffInfo.glyphs; this.glyphs = cffInfo.glyphs;
this.gsubrs = cffInfo.gsubrs || []; this.gsubrs = cffInfo.gsubrs || [];
@ -712,11 +716,10 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
this.fdArray = cffInfo.fdArray; this.fdArray = cffInfo.fdArray;
} }
Util.inherit(Type2Compiled, CompiledFont, {
compileGlyphImpl(code, cmds, glyphId) { compileGlyphImpl(code, cmds, glyphId) {
compileCharString(code, cmds, this, glyphId); compileCharString(code, cmds, this, glyphId);
}, }
}); }
return { return {
create: function FontRendererFactory_create(font, seacAnalysisEnabled) { create: function FontRendererFactory_create(font, seacAnalysisEnabled) {