1- var fontWeights = {
1+ const fontWeights = {
22 thin : 100 ,
33 hairline : 100 ,
44 'ultra-light' : 200 ,
@@ -22,46 +22,57 @@ var fontWeights = {
2222 fat : 900 ,
2323 poster : 900 ,
2424 'ultra-black' : 950 ,
25- 'extra-black' : 950
25+ 'extra-black' : 950 ,
2626} ;
27- var sp = ' ' ;
28- var italicRE = / ( i t a l i c | o b l i q u e ) $ / i;
27+ const sp = ' ' ;
28+ const italicRE = / ( i t a l i c | o b l i q u e ) $ / i;
2929
30- var fontCache = { } ;
30+ const fontCache = { } ;
3131
3232/**
3333 * @param {string|Array<string> } fonts Mapbox GL Style fontstack or single font, e.g. `['Open Sans Regular', 'Arial Unicode MS Regular']` or `'Open Sans Regular'`.
3434 * @param {number } size Font size in pixels.
3535 * @param {string|number } [lineHeight] Line height as css line-height.
36- * @returns {string } CSS font definition, e.g. `'normal 400 16px/1.2 "Open Sans"'`.
36+ * @return {string } CSS font definition, e.g. `'normal 400 16px/1.2 "Open Sans"'`.
3737 */
38- export default function ( fonts , size , lineHeight ) {
39- var cssData = fontCache [ fonts ] ;
38+ export default function ( fonts , size , lineHeight ) {
39+ let cssData = fontCache [ fonts ] ;
4040 if ( ! cssData ) {
4141 if ( ! Array . isArray ( fonts ) ) {
4242 fonts = [ fonts ] ;
4343 }
44- var weight = 400 ;
45- var style = 'normal' ;
46- var fontFamilies = [ ] ;
47- var haveWeight , haveStyle ;
48- for ( var i = 0 , ii = fonts . length ; i < ii ; ++ i ) {
49- var font = fonts [ i ] ;
50- var parts = font . split ( ' ' ) ;
51- var maybeWeight = parts [ parts . length - 1 ] . toLowerCase ( ) ;
52- if ( maybeWeight == 'normal' || maybeWeight == 'italic' || maybeWeight == 'oblique' ) {
44+ let weight = 400 ;
45+ let style = 'normal' ;
46+ const fontFamilies = [ ] ;
47+ let haveWeight , haveStyle ;
48+ for ( let i = 0 , ii = fonts . length ; i < ii ; ++ i ) {
49+ const font = fonts [ i ] ;
50+ const parts = font . split ( ' ' ) ;
51+ let maybeWeight = parts [ parts . length - 1 ] . toLowerCase ( ) ;
52+ if (
53+ maybeWeight == 'normal' ||
54+ maybeWeight == 'italic' ||
55+ maybeWeight == 'oblique'
56+ ) {
5357 style = haveStyle ? style : maybeWeight ;
5458 haveStyle = true ;
5559 parts . pop ( ) ;
5660 maybeWeight = parts [ parts . length - 1 ] . toLowerCase ( ) ;
5761 } else if ( italicRE . test ( maybeWeight ) ) {
5862 maybeWeight = maybeWeight . replace ( italicRE , '' ) ;
59- style = haveStyle ? style : parts [ parts . length - 1 ] . replace ( maybeWeight , '' ) ;
63+ style = haveStyle
64+ ? style
65+ : parts [ parts . length - 1 ] . replace ( maybeWeight , '' ) ;
6066 haveStyle = true ;
6167 }
62- for ( var w in fontWeights ) {
63- var previousPart = parts . length > 1 ? parts [ parts . length - 2 ] . toLowerCase ( ) : '' ;
64- if ( maybeWeight == w || maybeWeight == w . replace ( '-' , '' ) || previousPart + '-' + maybeWeight == w ) {
68+ for ( const w in fontWeights ) {
69+ const previousPart =
70+ parts . length > 1 ? parts [ parts . length - 2 ] . toLowerCase ( ) : '' ;
71+ if (
72+ maybeWeight == w ||
73+ maybeWeight == w . replace ( '-' , '' ) ||
74+ previousPart + '-' + maybeWeight == w
75+ ) {
6576 weight = haveWeight ? weight : fontWeights [ w ] ;
6677 parts . pop ( ) ;
6778 if ( previousPart && w . startsWith ( previousPart ) ) {
@@ -74,7 +85,8 @@ export default function(fonts, size, lineHeight) {
7485 weight = maybeWeight ;
7586 haveWeight = true ;
7687 }
77- var fontFamily = parts . join ( sp )
88+ let fontFamily = parts
89+ . join ( sp )
7890 . replace ( 'Klokantech Noto Sans' , 'Noto Sans' )
7991 . replace ( 'DIN Pro' , 'Barlow' )
8092 . replace ( 'Arial Unicode MS' , 'Arial' ) ;
@@ -84,7 +96,18 @@ export default function(fonts, size, lineHeight) {
8496 fontFamilies . push ( fontFamily ) ;
8597 }
8698 // CSS font property: font-style font-weight font-size/line-height font-family
87- cssData = fontCache [ fonts ] = [ style , weight , fontFamilies ] ;
99+ cssData = [ style , weight , fontFamilies ] ;
100+ fontCache [ fonts ] = cssData ;
88101 }
89- return cssData [ 0 ] + sp + cssData [ 1 ] + sp + size + 'px' + ( lineHeight ? '/' + lineHeight : '' ) + sp + cssData [ 2 ] ;
102+ return (
103+ cssData [ 0 ] +
104+ sp +
105+ cssData [ 1 ] +
106+ sp +
107+ size +
108+ 'px' +
109+ ( lineHeight ? '/' + lineHeight : '' ) +
110+ sp +
111+ cssData [ 2 ]
112+ ) ;
90113}
0 commit comments