Check font style and weight in addition to family

This commit is contained in:
ahocevar
2019-07-04 16:12:35 +02:00
parent ab2d97d49b
commit 4b48997a0b
4 changed files with 77 additions and 47 deletions

View File

@@ -2,6 +2,13 @@
* @module ol/css
*/
/**
* @typedef {Object} FontParameters
* @property {Array<string>} families
* @property {string} style
* @property {string} weight
*/
/**
* The CSS class for hidden feature.
@@ -62,10 +69,13 @@ 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 {Object<string>} The font families (or null if the input spec is invalid).
* @return {FontParameters} The font families (or null if the input spec is invalid).
*/
export const getFontFamilies = (function() {
export const getFontParameters = (function() {
let style;
/**
* @type {Object<string, FontParameters>}
*/
const cache = {};
return function(font) {
if (!style) {
@@ -74,11 +84,18 @@ export const getFontFamilies = (function() {
if (!(font in cache)) {
style.font = font;
const family = style.fontFamily;
const fontWeight = style.fontWeight;
const fontStyle = style.fontStyle;
style.font = '';
if (!family) {
return null;
}
cache[font] = family.split(/,\s?/);
const families = family.split(/,\s?/);
cache[font] = {
families: families,
weight: fontWeight,
style: fontStyle
};
}
return cache[font];
};