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

Cache loaded span tag when using the same font #2890

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions player/js/utils/FontManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,37 @@ const FontManager = (function () {
}

function setUpNode(font, family) {
var fontSelector = (font + family).replace(/\s/g, '');
var appendedParentNode = document.querySelector('[data-lottie-font=' + fontSelector + ']');
var node;
var parentNode = createTag('span');
// Node is invisible to screen readers.
parentNode.setAttribute('aria-hidden', true);
parentNode.style.fontFamily = family;
var node = createTag('span');
// Characters that vary significantly among different fonts
node.innerText = 'giItT1WQy@!-/#';
// Visible - so we can measure it - but not on the screen
parentNode.style.position = 'absolute';
parentNode.style.left = '-10000px';
parentNode.style.top = '-10000px';
// Large font size makes even subtle changes obvious
parentNode.style.fontSize = '300px';
// Reset any font properties
parentNode.style.fontVariant = 'normal';
parentNode.style.fontStyle = 'normal';
parentNode.style.fontWeight = 'normal';
parentNode.style.letterSpacing = '0';
parentNode.appendChild(node);
document.body.appendChild(parentNode);

// Check if the font tag is already loaded to document body
if (!appendedParentNode) {
// Node is invisible to screen readers.
parentNode.setAttribute('aria-hidden', true);
parentNode.setAttribute('data-lottie-font', fontSelector);
parentNode.style.fontFamily = family;
node = createTag('span');
// Characters that vary significantly among different fonts
node.innerText = 'giItT1WQy@!-/#';
// Visible - so we can measure it - but not on the screen
parentNode.style.position = 'absolute';
parentNode.style.left = '-10000px';
parentNode.style.top = '-10000px';
// Large font size makes even subtle changes obvious
parentNode.style.fontSize = '300px';
// Reset any font properties
parentNode.style.fontVariant = 'normal';
parentNode.style.fontStyle = 'normal';
parentNode.style.fontWeight = 'normal';
parentNode.style.letterSpacing = '0';
parentNode.appendChild(node);
document.body.appendChild(parentNode);
} else {
parentNode = appendedParentNode;
node = appendedParentNode.children[0];
}

// Remember width with no applied web font
var width = node.offsetWidth;
Expand Down