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

Obviously the `Font`-class is still *very* large, given particularly how TrueType fonts are handled, however this patch-series at least improves things by moving a number of functions/classes into their own files.
As a follow-up it might make sense to try and re-factor/extract the TrueType parsing into its own file, since all of this code is quite old, however that's probably best left for another time.

For e.g. `gulp mozcentral`, the *built* `pdf.worker.js` files decreases from `1 620 332` to `1 617 466` bytes with this patch-series.
This commit is contained in:
Jonas Jenwald 2021-05-02 17:42:48 +02:00
parent cadc20d8b9
commit b487edd05d

View File

@ -157,9 +157,8 @@ function adjustToUnicode(properties, builtInEncoding) {
properties.toUnicode.amend(toUnicode);
}
const Glyph = (function GlyphClosure() {
// eslint-disable-next-line no-shadow
function Glyph(
class Glyph {
constructor(
fontChar,
unicode,
accent,
@ -179,7 +178,7 @@ const Glyph = (function GlyphClosure() {
this.isInFont = isInFont;
}
Glyph.prototype.matchesForCache = function (
matchesForCache(
fontChar,
unicode,
accent,
@ -199,158 +198,7 @@ const Glyph = (function GlyphClosure() {
this.isSpace === isSpace &&
this.isInFont === isInFont
);
};
return Glyph;
})();
/**
* 'Font' is the class the outside world should use, it encapsulate all the font
* decoding logics whatever type it is (assuming the font type is supported).
*
* For example to read a Type1 font and to attach it to the document:
* var type1Font = new Font("MyFontName", binaryFile, propertiesObject);
* type1Font.bind();
*/
const Font = (function FontClosure() {
// eslint-disable-next-line no-shadow
function Font(name, file, properties) {
let charCode;
this.name = name;
this.loadedName = properties.loadedName;
this.isType3Font = properties.isType3Font;
this.missingFile = false;
this.cssFontInfo = properties.cssFontInfo;
this.glyphCache = Object.create(null);
this.isSerifFont = !!(properties.flags & FontFlags.Serif);
this.isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);
this.isMonospace = !!(properties.flags & FontFlags.FixedPitch);
let type = properties.type;
let subtype = properties.subtype;
this.type = type;
this.subtype = subtype;
let fallbackName = "sans-serif";
if (this.isMonospace) {
fallbackName = "monospace";
} else if (this.isSerifFont) {
fallbackName = "serif";
}
this.fallbackName = fallbackName;
this.differences = properties.differences;
this.widths = properties.widths;
this.defaultWidth = properties.defaultWidth;
this.composite = properties.composite;
this.cMap = properties.cMap;
this.capHeight = properties.capHeight / PDF_GLYPH_SPACE_UNITS;
this.ascent = properties.ascent / PDF_GLYPH_SPACE_UNITS;
this.descent = properties.descent / PDF_GLYPH_SPACE_UNITS;
this.fontMatrix = properties.fontMatrix;
this.bbox = properties.bbox;
this.defaultEncoding = properties.defaultEncoding;
this.toUnicode = properties.toUnicode;
this.fallbackToUnicode = properties.fallbackToUnicode || new ToUnicodeMap();
this.toFontChar = [];
if (properties.type === "Type3") {
for (charCode = 0; charCode < 256; charCode++) {
this.toFontChar[charCode] =
this.differences[charCode] || properties.defaultEncoding[charCode];
}
this.fontType = FontType.TYPE3;
return;
}
this.cidEncoding = properties.cidEncoding;
this.vertical = !!properties.vertical;
if (this.vertical) {
this.vmetrics = properties.vmetrics;
this.defaultVMetrics = properties.defaultVMetrics;
}
if (!file || file.isEmpty) {
if (file) {
// Some bad PDF generators will include empty font files,
// attempting to recover by assuming that no file exists.
warn('Font file is empty in "' + name + '" (' + this.loadedName + ")");
}
this.fallbackToSystemFont(properties);
return;
}
// Parse the font file to determine the correct type/subtype, rather than
// relying on the (often incorrect) data in the font dictionary; (see e.g.
// issue6782.pdf, issue7598.pdf, and issue9949.pdf).
[type, subtype] = getFontFileType(file, properties);
if (type !== this.type || subtype !== this.subtype) {
info(
"Inconsistent font file Type/SubType, expected: " +
`${this.type}/${this.subtype} but found: ${type}/${subtype}.`
);
}
let data;
try {
switch (type) {
case "MMType1":
info("MMType1 font (" + name + "), falling back to Type1.");
/* falls through */
case "Type1":
case "CIDFontType0":
this.mimetype = "font/opentype";
const cff =
subtype === "Type1C" || subtype === "CIDFontType0C"
? new CFFFont(file, properties)
: new Type1Font(name, file, properties);
adjustWidths(properties);
// Wrap the CFF data inside an OTF font file
data = this.convert(name, cff, properties);
break;
case "OpenType":
case "TrueType":
case "CIDFontType2":
this.mimetype = "font/opentype";
// Repair the TrueType file. It is can be damaged in the point of
// view of the sanitizer
data = this.checkAndRepair(name, file, properties);
if (this.isOpenType) {
adjustWidths(properties);
type = "OpenType";
}
break;
default:
throw new FormatError(`Font ${type} is not supported`);
}
} catch (e) {
warn(e);
this.fallbackToSystemFont(properties);
return;
}
this.data = data;
this.fontType = getFontType(type, subtype);
// Transfer some properties again that could change during font conversion
this.fontMatrix = properties.fontMatrix;
this.widths = properties.widths;
this.defaultWidth = properties.defaultWidth;
this.toUnicode = properties.toUnicode;
this.seacMap = properties.seacMap;
}
function int16(b0, b1) {
@ -939,16 +787,155 @@ const Font = (function FontClosure() {
return nameTable;
}
Font.prototype = {
name: null,
font: null,
mimetype: null,
disableFontFace: false,
/**
* 'Font' is the class the outside world should use, it encapsulate all the font
* decoding logics whatever type it is (assuming the font type is supported).
*/
class Font {
constructor(name, file, properties) {
this.name = name;
this.mimetype = null;
this.disableFontFace = false;
this.loadedName = properties.loadedName;
this.isType3Font = properties.isType3Font;
this.missingFile = false;
this.cssFontInfo = properties.cssFontInfo;
this.glyphCache = Object.create(null);
this.isSerifFont = !!(properties.flags & FontFlags.Serif);
this.isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);
this.isMonospace = !!(properties.flags & FontFlags.FixedPitch);
let type = properties.type;
let subtype = properties.subtype;
this.type = type;
this.subtype = subtype;
let fallbackName = "sans-serif";
if (this.isMonospace) {
fallbackName = "monospace";
} else if (this.isSerifFont) {
fallbackName = "serif";
}
this.fallbackName = fallbackName;
this.differences = properties.differences;
this.widths = properties.widths;
this.defaultWidth = properties.defaultWidth;
this.composite = properties.composite;
this.cMap = properties.cMap;
this.capHeight = properties.capHeight / PDF_GLYPH_SPACE_UNITS;
this.ascent = properties.ascent / PDF_GLYPH_SPACE_UNITS;
this.descent = properties.descent / PDF_GLYPH_SPACE_UNITS;
this.fontMatrix = properties.fontMatrix;
this.bbox = properties.bbox;
this.defaultEncoding = properties.defaultEncoding;
this.toUnicode = properties.toUnicode;
this.fallbackToUnicode = properties.fallbackToUnicode || new ToUnicodeMap();
this.toFontChar = [];
if (properties.type === "Type3") {
for (let charCode = 0; charCode < 256; charCode++) {
this.toFontChar[charCode] =
this.differences[charCode] || properties.defaultEncoding[charCode];
}
this.fontType = FontType.TYPE3;
return;
}
this.cidEncoding = properties.cidEncoding;
this.vertical = !!properties.vertical;
if (this.vertical) {
this.vmetrics = properties.vmetrics;
this.defaultVMetrics = properties.defaultVMetrics;
}
if (!file || file.isEmpty) {
if (file) {
// Some bad PDF generators will include empty font files,
// attempting to recover by assuming that no file exists.
warn('Font file is empty in "' + name + '" (' + this.loadedName + ")");
}
this.fallbackToSystemFont(properties);
return;
}
// Parse the font file to determine the correct type/subtype, rather than
// relying on the (often incorrect) data in the font dictionary; (see e.g.
// issue6782.pdf, issue7598.pdf, and issue9949.pdf).
[type, subtype] = getFontFileType(file, properties);
if (type !== this.type || subtype !== this.subtype) {
info(
"Inconsistent font file Type/SubType, expected: " +
`${this.type}/${this.subtype} but found: ${type}/${subtype}.`
);
}
let data;
try {
switch (type) {
case "MMType1":
info("MMType1 font (" + name + "), falling back to Type1.");
/* falls through */
case "Type1":
case "CIDFontType0":
this.mimetype = "font/opentype";
const cff =
subtype === "Type1C" || subtype === "CIDFontType0C"
? new CFFFont(file, properties)
: new Type1Font(name, file, properties);
adjustWidths(properties);
// Wrap the CFF data inside an OTF font file
data = this.convert(name, cff, properties);
break;
case "OpenType":
case "TrueType":
case "CIDFontType2":
this.mimetype = "font/opentype";
// Repair the TrueType file. It is can be damaged in the point of
// view of the sanitizer
data = this.checkAndRepair(name, file, properties);
if (this.isOpenType) {
adjustWidths(properties);
type = "OpenType";
}
break;
default:
throw new FormatError(`Font ${type} is not supported`);
}
} catch (e) {
warn(e);
this.fallbackToSystemFont(properties);
return;
}
this.data = data;
this.fontType = getFontType(type, subtype);
// Transfer some properties again that could change during font conversion
this.fontMatrix = properties.fontMatrix;
this.widths = properties.widths;
this.defaultWidth = properties.defaultWidth;
this.toUnicode = properties.toUnicode;
this.seacMap = properties.seacMap;
}
get renderer() {
const renderer = FontRendererFactory.create(this, SEAC_ANALYSIS_ENABLED);
return shadow(this, "renderer", renderer);
},
}
exportData(extraProperties = false) {
const exportDataProperties = extraProperties
@ -965,7 +952,7 @@ const Font = (function FontClosure() {
}
}
return data;
},
}
fallbackToSystemFont(properties) {
this.missingFile = true;
@ -985,8 +972,7 @@ const Font = (function FontClosure() {
fontName = stdFontMap[fontName] || nonStdFontMap[fontName] || fontName;
this.bold = fontName.search(/bold/gi) !== -1;
this.italic =
fontName.search(/oblique/gi) !== -1 ||
fontName.search(/italic/gi) !== -1;
fontName.search(/oblique/gi) !== -1 || fontName.search(/italic/gi) !== -1;
// Use 'name' instead of 'fontName' here because the original
// name ArialBlack for example will be replaced by Helvetica.
@ -1034,8 +1020,7 @@ const Font = (function FontClosure() {
}
}
const isIdentityUnicode =
this.toUnicode instanceof IdentityToUnicodeMap;
const isIdentityUnicode = this.toUnicode instanceof IdentityToUnicodeMap;
if (!isIdentityUnicode) {
this.toUnicode.forEach(function (charCode, unicodeCharCode) {
map[+charCode] = unicodeCharCode;
@ -1094,9 +1079,9 @@ const Font = (function FontClosure() {
}
this.loadedName = fontName.split("-")[0];
this.fontType = getFontType(type, subtype);
},
}
checkAndRepair: function Font_checkAndRepair(name, font, properties) {
checkAndRepair(name, font, properties) {
const VALID_TABLES = [
"OS/2",
"cmap",
@ -1496,13 +1481,7 @@ const Font = (function FontClosure() {
};
}
function sanitizeMetrics(
file,
header,
metrics,
numGlyphs,
dupFirstEntry
) {
function sanitizeMetrics(file, header, metrics, numGlyphs, dupFirstEntry) {
if (!header) {
if (metrics) {
metrics.data = null;
@ -1958,11 +1937,7 @@ const Font = (function FontClosure() {
const NAME_RECORD_LENGTH = 12;
let i, ii;
for (
i = 0;
i < numRecords && font.pos + NAME_RECORD_LENGTH <= end;
i++
) {
for (i = 0; i < numRecords && font.pos + NAME_RECORD_LENGTH <= end; i++) {
const r = {
platform: font.getUint16(),
encoding: font.getUint16(),
@ -2141,8 +2116,7 @@ const Font = (function FontClosure() {
funcId = functionsCalled.pop();
data = pc.data;
i = pc.i;
ttContext.functionsStackDeltas[funcId] =
stack.length - pc.stackTop;
ttContext.functionsStackDeltas[funcId] = stack.length - pc.stackTop;
}
} else if (op === 0x89) {
// IDEF - instruction definition
@ -2562,8 +2536,7 @@ const Font = (function FontClosure() {
} else if (cmapPlatformId === 0) {
// Default Unicode semantics, use the charcodes as is.
for (let i = 0; i < cmapMappingsLength; ++i) {
charCodeToGlyphId[cmapMappings[i].charCode] =
cmapMappings[i].glyphId;
charCodeToGlyphId[cmapMappings[i].charCode] = cmapMappings[i].glyphId;
}
} else {
// When there is only a (1, 0) cmap table, the char code is a single
@ -2687,9 +2660,9 @@ const Font = (function FontClosure() {
builder.addTable(tableTag, tables[tableTag].data);
}
return builder.toArray();
},
}
convert: function Font_convert(fontName, font, properties) {
convert(fontName, font, properties) {
// TODO: Check the charstring widths to determine this.
properties.fixedPitch = false;
@ -2882,7 +2855,7 @@ const Font = (function FontClosure() {
builder.addTable("post", createPostTable(properties));
return builder.toArray();
},
}
get spaceWidth() {
// trying to estimate space character width
@ -2918,7 +2891,7 @@ const Font = (function FontClosure() {
}
width = width || this.defaultWidth;
return shadow(this, "spaceWidth", width);
},
}
/**
* @private
@ -3012,9 +2985,9 @@ const Font = (function FontClosure() {
this.glyphCache[charcode] = glyph;
}
return glyph;
},
}
charsToGlyphs: function Font_charsToGlyphs(chars) {
charsToGlyphs(chars) {
let charsCache = this.charsCache;
let glyphs, glyph, charcode;
@ -3060,7 +3033,7 @@ const Font = (function FontClosure() {
// Enter the translated string into the cache
return (charsCache[charsCacheKey] = glyphs);
},
}
/**
* Chars can have different sizes (depends on the encoding).
@ -3088,11 +3061,11 @@ const Font = (function FontClosure() {
}
return positions;
},
}
get glyphCacheValues() {
return Object.values(this.glyphCache);
},
}
/**
* Encode a js string using font encoding.
@ -3129,9 +3102,7 @@ const Font = (function FontClosure() {
? this.cMap.getCharCodeLength(charCode)
: 1;
for (let j = charCodeLength - 1; j >= 0; j--) {
currentBuf.push(
String.fromCharCode((charCode >> (8 * j)) & 0xff)
);
currentBuf.push(String.fromCharCode((charCode >> (8 * j)) & 0xff));
}
continue;
}
@ -3148,33 +3119,27 @@ const Font = (function FontClosure() {
buffers.push(currentBuf.join(""));
return buffers;
},
};
}
}
return Font;
})();
const ErrorFont = (function ErrorFontClosure() {
// eslint-disable-next-line no-shadow
function ErrorFont(error) {
class ErrorFont {
constructor(error) {
this.error = error;
this.loadedName = "g_font_error";
this.missingFile = true;
}
ErrorFont.prototype = {
charsToGlyphs: function ErrorFont_charsToGlyphs() {
charsToGlyphs() {
return [];
},
encodeString: function ErrorFont_encodeString(chars) {
}
encodeString(chars) {
return [chars];
},
}
exportData(extraProperties = false) {
return { error: this.error };
},
};
return ErrorFont;
})();
}
}
export { ErrorFont, Font };