Skip to content

Commit 13e7adb

Browse files
committed
Support fontstack arrays in addition to single font names
1 parent fdefd89 commit 13e7adb

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

index.js

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,51 @@ var fontWeights = {
2525
poster: 900
2626
};
2727
var sp = ' ';
28+
var italicRE = /(italic|oblique)$/i;
2829

2930
var fontCache = {};
3031

31-
module.exports = function(font, size) {
32-
var cssData = fontCache[font];
32+
module.exports = function(fonts, size) {
33+
var cssData = fontCache[fonts];
3334
if (!cssData) {
34-
var parts = font.split(' ');
35-
var maybeWeight = parts[parts.length - 1].toLowerCase();
35+
if (!Array.isArray(fonts)) {
36+
fonts = [fonts];
37+
}
3638
var weight = 'normal';
3739
var style = 'normal';
38-
if (maybeWeight == 'normal' || maybeWeight == 'italic' || maybeWeight == 'oblique') {
39-
style = maybeWeight;
40-
parts.pop();
41-
maybeWeight = parts[parts.length - 1].toLowerCase();
42-
}
43-
for (var w in fontWeights) {
44-
if (maybeWeight == w || maybeWeight == w.replace('-', '') || maybeWeight == w.replace('-', ' ')) {
45-
weight = fontWeights[w];
40+
var fontFamilies = []
41+
var haveWeight, haveStyle;
42+
for (var i = 0, ii = fonts.length; i < ii; ++i) {
43+
var font = fonts[i];
44+
var parts = font.split(' ');
45+
var maybeWeight = parts[parts.length - 1].toLowerCase();
46+
if (maybeWeight == 'normal' || maybeWeight == 'italic' || maybeWeight == 'oblique') {
47+
style = haveStyle ? style : maybeWeight;
4648
parts.pop();
47-
break;
49+
maybeWeight = parts[parts.length - 1].toLowerCase();
50+
} else if (italicRE.test(maybeWeight)) {
51+
maybeWeight = maybeWeight.replace(italicRE, '');
52+
style = haveStyle ? style : parts[parts.length - 1].replace(maybeWeight, '');
4853
}
49-
}
50-
if (typeof maybeWeight == 'number') {
51-
weight = maybeWeight;
52-
}
53-
var fontFamily = parts.join(' ')
54-
.replace('Klokantech Noto Sans', 'Noto Sans');
55-
if (fontFamily.indexOf(' ') !== -1) {
56-
fontFamily = '"' + fontFamily + '"';
54+
for (var w in fontWeights) {
55+
if (maybeWeight == w || maybeWeight == w.replace('-', '') || maybeWeight == w.replace('-', sp)) {
56+
weight = haveWeight ? weight : fontWeights[w];
57+
parts.pop();
58+
break;
59+
}
60+
}
61+
if (!haveWeight && typeof maybeWeight == 'number') {
62+
weight = maybeWeight;
63+
}
64+
var fontFamily = parts.join(sp)
65+
.replace('Klokantech Noto Sans', 'Noto Sans');
66+
if (fontFamily.indexOf(sp) !== -1) {
67+
fontFamily = '"' + fontFamily + '"';
68+
}
69+
fontFamilies.push(fontFamily);
5770
}
5871
// CSS font property: font-style font-weight font-size font-family
59-
cssData = fontCache[font] = [style, weight, fontFamily];
72+
cssData = fontCache[fonts] = [style, weight, fontFamilies];
6073
}
6174
return cssData[0] + sp + cssData[1] + sp + size + 'px' + sp + cssData[2];
6275
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mapbox-to-css-font",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "Utility to convert Mapbox GL Style font names to CSS font definitions",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)