Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-factor the charsCache on Font-instances #13443

Merged
merged 1 commit into from
May 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 25 additions & 35 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,8 @@ class Font {
this.missingFile = false;
this.cssFontInfo = properties.cssFontInfo;

this.glyphCache = Object.create(null);
this._charsCache = Object.create(null);
this._glyphCache = Object.create(null);

this.isSerifFont = !!(properties.flags & FontFlags.Serif);
this.isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);
Expand Down Expand Up @@ -2946,7 +2947,7 @@ class Font {
}
}

let glyph = this.glyphCache[charcode];
let glyph = this._glyphCache[charcode];
if (
!glyph ||
!glyph.matchesForCache(
Expand All @@ -2970,57 +2971,46 @@ class Font {
isSpace,
isInFont
);
this.glyphCache[charcode] = glyph;
this._glyphCache[charcode] = glyph;
}
return glyph;
}

charsToGlyphs(chars) {
let charsCache = this.charsCache;
let glyphs, glyph, charcode;

// if we translated this string before, just grab it from the cache
if (charsCache) {
glyphs = charsCache[chars];
if (glyphs) {
return glyphs;
}
}

// lazily create the translation cache
if (!charsCache) {
charsCache = this.charsCache = Object.create(null);
// If we translated this string before, just grab it from the cache.
let glyphs = this._charsCache[chars];
if (glyphs) {
return glyphs;
}

glyphs = [];
const charsCacheKey = chars;
let i = 0,
ii;

if (this.cMap) {
// composite fonts have multi-byte strings convert the string from
// single-byte to multi-byte
const c = Object.create(null);
while (i < chars.length) {
// Composite fonts have multi-byte strings, convert the string from
// single-byte to multi-byte.
const c = Object.create(null),
ii = chars.length;
let i = 0;
while (i < ii) {
this.cMap.readCharCode(chars, i, c);
charcode = c.charcode;
const length = c.length;
const { charcode, length } = c;
i += length;
// Space is char with code 0x20 and length 1 in multiple-byte codes.
const isSpace = length === 1 && chars.charCodeAt(i - 1) === 0x20;
glyph = this._charToGlyph(charcode, isSpace);
const glyph = this._charToGlyph(
charcode,
length === 1 && chars.charCodeAt(i - 1) === 0x20
);
glyphs.push(glyph);
}
} else {
for (i = 0, ii = chars.length; i < ii; ++i) {
charcode = chars.charCodeAt(i);
glyph = this._charToGlyph(charcode, charcode === 0x20);
for (let i = 0, ii = chars.length; i < ii; ++i) {
const charcode = chars.charCodeAt(i);
const glyph = this._charToGlyph(charcode, charcode === 0x20);
glyphs.push(glyph);
}
}

// Enter the translated string into the cache
return (charsCache[charsCacheKey] = glyphs);
// Enter the translated string into the cache.
return (this._charsCache[chars] = glyphs);
}

/**
Expand Down Expand Up @@ -3052,7 +3042,7 @@ class Font {
}

get glyphCacheValues() {
return Object.values(this.glyphCache);
return Object.values(this._glyphCache);
}

/**
Expand Down