From e803584fe7aef105fd0c137742825f070134a84b Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 2 May 2021 16:41:45 +0200 Subject: [PATCH] Convert `src/core/cff_font.js` to use standard classes --- src/core/cff_font.js | 134 +++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 68 deletions(-) diff --git a/src/core/cff_font.js b/src/core/cff_font.js index 4fc8e6146..e7caa8afa 100644 --- a/src/core/cff_font.js +++ b/src/core/cff_font.js @@ -17,9 +17,8 @@ import { CFFCompiler, CFFParser } from "./cff_parser.js"; import { SEAC_ANALYSIS_ENABLED, type1FontGlyphMapping } from "./fonts_utils.js"; import { warn } from "../shared/util.js"; -const CFFFont = (function CFFFontClosure() { - // eslint-disable-next-line no-shadow - function CFFFont(file, properties) { +class CFFFont { + constructor(file, properties) { this.properties = properties; const parser = new CFFParser(file, properties, SEAC_ANALYSIS_ENABLED); @@ -38,78 +37,77 @@ const CFFFont = (function CFFFontClosure() { this._createBuiltInEncoding(); } - CFFFont.prototype = { - get numGlyphs() { - return this.cff.charStrings.count; - }, - getCharset: function CFFFont_getCharset() { - return this.cff.charset.charset; - }, - getGlyphMapping: function CFFFont_getGlyphMapping() { - const cff = this.cff; - const properties = this.properties; - const charsets = cff.charset.charset; - let charCodeToGlyphId; - let glyphId; + get numGlyphs() { + return this.cff.charStrings.count; + } - if (properties.composite) { - charCodeToGlyphId = Object.create(null); - let charCode; - if (cff.isCIDFont) { - // If the font is actually a CID font then we should use the charset - // to map CIDs to GIDs. - for (glyphId = 0; glyphId < charsets.length; glyphId++) { - const cid = charsets[glyphId]; - charCode = properties.cMap.charCodeOf(cid); - charCodeToGlyphId[charCode] = glyphId; - } - } else { - // If it is NOT actually a CID font then CIDs should be mapped - // directly to GIDs. - for (glyphId = 0; glyphId < cff.charStrings.count; glyphId++) { - charCode = properties.cMap.charCodeOf(glyphId); - charCodeToGlyphId[charCode] = glyphId; - } + getCharset() { + return this.cff.charset.charset; + } + + getGlyphMapping() { + const cff = this.cff; + const properties = this.properties; + const charsets = cff.charset.charset; + let charCodeToGlyphId; + let glyphId; + + if (properties.composite) { + charCodeToGlyphId = Object.create(null); + let charCode; + if (cff.isCIDFont) { + // If the font is actually a CID font then we should use the charset + // to map CIDs to GIDs. + for (glyphId = 0; glyphId < charsets.length; glyphId++) { + const cid = charsets[glyphId]; + charCode = properties.cMap.charCodeOf(cid); + charCodeToGlyphId[charCode] = glyphId; + } + } else { + // If it is NOT actually a CID font then CIDs should be mapped + // directly to GIDs. + for (glyphId = 0; glyphId < cff.charStrings.count; glyphId++) { + charCode = properties.cMap.charCodeOf(glyphId); + charCodeToGlyphId[charCode] = glyphId; } - return charCodeToGlyphId; } - - const encoding = cff.encoding ? cff.encoding.encoding : null; - charCodeToGlyphId = type1FontGlyphMapping(properties, encoding, charsets); return charCodeToGlyphId; - }, - hasGlyphId: function CFFFont_hasGlyphID(id) { - return this.cff.hasGlyphId(id); - }, + } - /** - * @private - */ - _createBuiltInEncoding() { - const { charset, encoding } = this.cff; - if (!charset || !encoding) { - return; - } - const charsets = charset.charset, - encodings = encoding.encoding; - const map = []; + const encoding = cff.encoding ? cff.encoding.encoding : null; + charCodeToGlyphId = type1FontGlyphMapping(properties, encoding, charsets); + return charCodeToGlyphId; + } - for (const charCode in encodings) { - const glyphId = encodings[charCode]; - if (glyphId >= 0) { - const glyphName = charsets[glyphId]; - if (glyphName) { - map[charCode] = glyphName; - } + hasGlyphId(id) { + return this.cff.hasGlyphId(id); + } + + /** + * @private + */ + _createBuiltInEncoding() { + const { charset, encoding } = this.cff; + if (!charset || !encoding) { + return; + } + const charsets = charset.charset, + encodings = encoding.encoding; + const map = []; + + for (const charCode in encodings) { + const glyphId = encodings[charCode]; + if (glyphId >= 0) { + const glyphName = charsets[glyphId]; + if (glyphName) { + map[charCode] = glyphName; } } - if (map.length > 0) { - this.properties.builtInEncoding = map; - } - }, - }; - - return CFFFont; -})(); + } + if (map.length > 0) { + this.properties.builtInEncoding = map; + } + } +} export { CFFFont };