Make font loading work in workers

This commit is contained in:
Andreas Hocevar
2020-03-16 23:23:34 +01:00
parent bb1ca76bcc
commit 06f6ba13c8
2 changed files with 53 additions and 20 deletions

View File

@@ -7,6 +7,7 @@
* @property {Array<string>} families
* @property {string} style
* @property {string} weight
* @property {string} lineHeight
*/
@@ -68,8 +69,9 @@ export const CLASS_COLLAPSED = 'ol-collapsed';
/**
* Get the list of font families from a font spec. Note that this doesn't work
* for font families that have commas in them.
* @param {string} The CSS font property.
* @return {FontParameters} The font families (or null if the input spec is invalid).
* @param {string} fontSpec The CSS font property.
* @param {function(FontParameters):void} callback Called with the font families
* (or null if the input spec is invalid).
*/
export const getFontParameters = (function() {
/**
@@ -80,26 +82,25 @@ export const getFontParameters = (function() {
* @type {Object<string, FontParameters>}
*/
const cache = {};
return function(font) {
return function(fontSpec, callback) {
if (!style) {
style = document.createElement('div').style;
}
if (!(font in cache)) {
style.font = font;
if (!(fontSpec in cache)) {
style.font = fontSpec;
const family = style.fontFamily;
const fontWeight = style.fontWeight;
const fontStyle = style.fontStyle;
style.font = '';
if (!family) {
return null;
callback(null);
}
const families = family.split(/,\s?/);
cache[font] = {
cache[fontSpec] = {
families: families,
weight: fontWeight,
style: fontStyle
weight: style.fontWeight,
style: style.fontStyle,
lineHeight: style.lineHeight
};
style.font = '';
}
return cache[font];
callback(cache[fontSpec]);
};
})();