Add utility method for extracting font families from a font spec

This commit is contained in:
Tim Schaub
2017-10-14 13:17:28 -06:00
committed by Andreas Hocevar
parent 06728ab0fa
commit dea8a340a6
2 changed files with 74 additions and 0 deletions
+27
View File
@@ -43,3 +43,30 @@ ol.css.CLASS_UNSUPPORTED = 'ol-unsupported';
* @type {string}
*/
ol.css.CLASS_CONTROL = 'ol-control';
/**
* 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 {Array.<string>} The font families (or null if the input spec is invalid).
*/
ol.css.getFontFamilies = (function() {
var style;
var cache = {};
return function(font) {
if (!style) {
style = document.createElement('div').style;
}
if (!(font in cache)) {
style.font = font;
var family = style.fontFamily;
style.font = '';
if (!family) {
return null;
}
cache[font] = family.split(/,\s?/);
}
return cache[font];
};
})();