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

This commit is contained in:
Jonas Jenwald 2021-05-02 16:41:45 +02:00
parent 542ee0d798
commit e803584fe7

View File

@ -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 };