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

This commit is contained in:
Jonas Jenwald 2021-05-02 16:54:45 +02:00
parent 4bd69556ab
commit f64b7922b3

View File

@ -30,9 +30,7 @@ import { Stream } from "./stream.js";
import { Type1Parser } from "./type1_parser.js"; import { Type1Parser } from "./type1_parser.js";
import { warn } from "../shared/util.js"; import { warn } from "../shared/util.js";
// Type1Font is also a CIDFontType0. function findBlock(streamBytes, signature, startIndex) {
const Type1Font = (function Type1FontClosure() {
function findBlock(streamBytes, signature, startIndex) {
const streamBytesLength = streamBytes.length; const streamBytesLength = streamBytes.length;
const signatureLength = signature.length; const signatureLength = signature.length;
const scanLength = streamBytesLength - signatureLength; const scanLength = streamBytesLength - signatureLength;
@ -59,9 +57,9 @@ const Type1Font = (function Type1FontClosure() {
found, found,
length: i, length: i,
}; };
} }
function getHeaderBlock(stream, suggestedLength) { function getHeaderBlock(stream, suggestedLength) {
const EEXEC_SIGNATURE = [0x65, 0x65, 0x78, 0x65, 0x63]; const EEXEC_SIGNATURE = [0x65, 0x65, 0x78, 0x65, 0x63];
const streamStartPos = stream.pos; // Save the initial stream position. const streamStartPos = stream.pos; // Save the initial stream position.
@ -128,9 +126,9 @@ const Type1Font = (function Type1FontClosure() {
stream: new Stream(stream.getBytes(suggestedLength)), stream: new Stream(stream.getBytes(suggestedLength)),
length: suggestedLength, length: suggestedLength,
}; };
} }
function getEexecBlock(stream, suggestedLength) { function getEexecBlock(stream, suggestedLength) {
// We should ideally parse the eexec block to ensure that `suggestedLength` // We should ideally parse the eexec block to ensure that `suggestedLength`
// is correct, so we don't truncate the block data if it's too small. // is correct, so we don't truncate the block data if it's too small.
// However, this would also require checking if the fixed-content portion // However, this would also require checking if the fixed-content portion
@ -149,10 +147,13 @@ const Type1Font = (function Type1FontClosure() {
stream: new Stream(eexecBytes), stream: new Stream(eexecBytes),
length: eexecBytes.length, length: eexecBytes.length,
}; };
} }
// eslint-disable-next-line no-shadow /**
function Type1Font(name, file, properties) { * Type1Font is also a CIDFontType0.
*/
class Type1Font {
constructor(name, file, properties) {
// Some bad generators embed pfb file as is, we have to strip 6-byte header. // Some bad generators embed pfb file as is, we have to strip 6-byte header.
// Also, length1 and length2 might be off by 6 bytes as well. // Also, length1 and length2 might be off by 6 bytes as well.
// http://www.math.ubc.ca/~cass/piscript/type1.pdf // http://www.math.ubc.ca/~cass/piscript/type1.pdf
@ -215,21 +216,20 @@ const Type1Font = (function Type1FontClosure() {
this.seacs = this.getSeacs(data.charstrings); this.seacs = this.getSeacs(data.charstrings);
} }
Type1Font.prototype = {
get numGlyphs() { get numGlyphs() {
return this.charstrings.length + 1; return this.charstrings.length + 1;
}, }
getCharset: function Type1Font_getCharset() { getCharset() {
const charset = [".notdef"]; const charset = [".notdef"];
const charstrings = this.charstrings; const charstrings = this.charstrings;
for (let glyphId = 0; glyphId < charstrings.length; glyphId++) { for (let glyphId = 0; glyphId < charstrings.length; glyphId++) {
charset.push(charstrings[glyphId].glyphName); charset.push(charstrings[glyphId].glyphName);
} }
return charset; return charset;
}, }
getGlyphMapping: function Type1Font_getGlyphMapping(properties) { getGlyphMapping(properties) {
const charstrings = this.charstrings; const charstrings = this.charstrings;
if (properties.composite) { if (properties.composite) {
@ -264,9 +264,9 @@ const Type1Font = (function Type1FontClosure() {
} }
return type1FontGlyphMapping(properties, builtInEncoding, glyphNames); return type1FontGlyphMapping(properties, builtInEncoding, glyphNames);
}, }
hasGlyphId: function Type1Font_hasGlyphID(id) { hasGlyphId(id) {
if (id < 0 || id >= this.numGlyphs) { if (id < 0 || id >= this.numGlyphs) {
return false; return false;
} }
@ -276,12 +276,11 @@ const Type1Font = (function Type1FontClosure() {
} }
const glyph = this.charstrings[id - 1]; const glyph = this.charstrings[id - 1];
return glyph.charstring.length > 0; return glyph.charstring.length > 0;
}, }
getSeacs: function Type1Font_getSeacs(charstrings) { getSeacs(charstrings) {
let i, ii;
const seacMap = []; const seacMap = [];
for (i = 0, ii = charstrings.length; i < ii; i++) { for (let i = 0, ii = charstrings.length; i < ii; i++) {
const charstring = charstrings[i]; const charstring = charstrings[i];
if (charstring.seac) { if (charstring.seac) {
// Offset by 1 for .notdef // Offset by 1 for .notdef
@ -289,19 +288,17 @@ const Type1Font = (function Type1FontClosure() {
} }
} }
return seacMap; return seacMap;
}, }
getType2Charstrings: function Type1Font_getType2Charstrings( getType2Charstrings(type1Charstrings) {
type1Charstrings
) {
const type2Charstrings = []; const type2Charstrings = [];
for (let i = 0, ii = type1Charstrings.length; i < ii; i++) { for (let i = 0, ii = type1Charstrings.length; i < ii; i++) {
type2Charstrings.push(type1Charstrings[i].charstring); type2Charstrings.push(type1Charstrings[i].charstring);
} }
return type2Charstrings; return type2Charstrings;
}, }
getType2Subrs: function Type1Font_getType2Subrs(type1Subrs) { getType2Subrs(type1Subrs) {
let bias = 0; let bias = 0;
const count = type1Subrs.length; const count = type1Subrs.length;
if (count < 1133) { if (count < 1133) {
@ -324,15 +321,9 @@ const Type1Font = (function Type1FontClosure() {
} }
return type2Subrs; return type2Subrs;
}, }
wrap: function Type1Font_wrap( wrap(name, glyphs, charstrings, subrs, properties) {
name,
glyphs,
charstrings,
subrs,
properties
) {
const cff = new CFF(); const cff = new CFF();
cff.header = new CFFHeader(1, 0, 4, 4); cff.header = new CFFHeader(1, 0, 4, 4);
@ -427,10 +418,7 @@ const Type1Font = (function Type1FontClosure() {
const compiler = new CFFCompiler(cff); const compiler = new CFFCompiler(cff);
return compiler.compile(); return compiler.compile();
}, }
}; }
return Type1Font;
})();
export { Type1Font }; export { Type1Font };