Re-factor the glyph-cache lookup in the Font._charToGlyph method

With the changes in the previous patch we can move the glyph-cache lookup to the top of the method and thus avoid a bunch of, in *almost* every case, completely unnecessary re-parsing for every `charCode`.
This commit is contained in:
Jonas Jenwald 2022-10-18 17:20:25 +02:00
parent 3e391aaed9
commit fd35cda8bc

View File

@ -3225,6 +3225,12 @@ class Font {
* @private * @private
*/ */
_charToGlyph(charcode, isSpace = false) { _charToGlyph(charcode, isSpace = false) {
let glyph = this._glyphCache[charcode];
// All `Glyph`-properties, except `isSpace` in multi-byte strings,
// depend indirectly on the `charcode`.
if (glyph && glyph.isSpace === isSpace) {
return glyph;
}
let fontCharCode, width, operatorListId; let fontCharCode, width, operatorListId;
let widthCode = charcode; let widthCode = charcode;
@ -3289,24 +3295,18 @@ class Font {
} }
} }
let glyph = this._glyphCache[charcode]; glyph = new Glyph(
// All `Glyph`-properties, except `isSpace` in multi-byte strings, charcode,
// depend indirectly on the `charcode`. fontChar,
if (!glyph || glyph.isSpace !== isSpace) { unicode,
glyph = new Glyph( accent,
charcode, width,
fontChar, vmetric,
unicode, operatorListId,
accent, isSpace,
width, isInFont
vmetric, );
operatorListId, return (this._glyphCache[charcode] = glyph);
isSpace,
isInFont
);
this._glyphCache[charcode] = glyph;
}
return glyph;
} }
charsToGlyphs(chars) { charsToGlyphs(chars) {