forked from Flipboard/react-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFontFace.js
53 lines (44 loc) · 1.16 KB
/
FontFace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
var _fontFaces = {};
/**
* @param {String} family The CSS font-family value
* @param {String} url The remote URL for the font file
* @param {Object} attributes Font attributes supported: style, weight
* @return {Object}
*/
function FontFace (family, url, attributes) {
var fontFace;
var fontId;
attributes = attributes || {};
attributes.style = attributes.style || 'normal';
attributes.weight = attributes.weight || 400;
fontId = getCacheKey(family, url, attributes);
fontFace = _fontFaces[fontId];
if (!fontFace) {
fontFace = {};
fontFace.id = fontId;
fontFace.family = family;
fontFace.url = url;
fontFace.attributes = attributes;
_fontFaces[fontId] = fontFace;
}
return fontFace;
}
/**
* Helper for retrieving the default family by weight.
*
* @param {Number} fontWeight
* @return {FontFace}
*/
FontFace.Default = function (fontWeight) {
return FontFace('sans-serif', null, {weight: fontWeight});
};
/**
* @internal
*/
function getCacheKey (family, url, attributes) {
return family + url + Object.keys(attributes).sort().map(function (key) {
return attributes[key];
});
}
module.exports = FontFace;