UNPKG

4.21 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = fontFace;
5var _errors = _interopRequireDefault(require("../internalHelpers/_errors"));
6function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
7var dataURIRegex = /^\s*data:([a-z]+\/[a-z-]+(;[a-z-]+=[a-z-]+)?)?(;charset=[a-z0-9-]+)?(;base64)?,[a-z0-9!$&',()*+,;=\-._~:@/?%\s]*\s*$/i;
8var formatHintMap = {
9 woff: 'woff',
10 woff2: 'woff2',
11 ttf: 'truetype',
12 otf: 'opentype',
13 eot: 'embedded-opentype',
14 svg: 'svg',
15 svgz: 'svg'
16};
17function generateFormatHint(format, formatHint) {
18 if (!formatHint) return '';
19 return " format(\"" + formatHintMap[format] + "\")";
20}
21function isDataURI(fontFilePath) {
22 return !!fontFilePath.replace(/\s+/g, ' ').match(dataURIRegex);
23}
24function generateFileReferences(fontFilePath, fileFormats, formatHint) {
25 if (isDataURI(fontFilePath)) {
26 return "url(\"" + fontFilePath + "\")" + generateFormatHint(fileFormats[0], formatHint);
27 }
28 var fileFontReferences = fileFormats.map(function (format) {
29 return "url(\"" + fontFilePath + "." + format + "\")" + generateFormatHint(format, formatHint);
30 });
31 return fileFontReferences.join(', ');
32}
33function generateLocalReferences(localFonts) {
34 var localFontReferences = localFonts.map(function (font) {
35 return "local(\"" + font + "\")";
36 });
37 return localFontReferences.join(', ');
38}
39function generateSources(fontFilePath, localFonts, fileFormats, formatHint) {
40 var fontReferences = [];
41 if (localFonts) fontReferences.push(generateLocalReferences(localFonts));
42 if (fontFilePath) {
43 fontReferences.push(generateFileReferences(fontFilePath, fileFormats, formatHint));
44 }
45 return fontReferences.join(', ');
46}
47
48/**
49 * CSS for a @font-face declaration. Defaults to check for local copies of the font on the user's machine. You can disable this by passing `null` to localFonts.
50 *
51 * @example
52 * // Styles as object basic usage
53 * const styles = {
54 * ...fontFace({
55 * 'fontFamily': 'Sans-Pro',
56 * 'fontFilePath': 'path/to/file'
57 * })
58 * }
59 *
60 * // styled-components basic usage
61 * const GlobalStyle = createGlobalStyle`${
62 * fontFace({
63 * 'fontFamily': 'Sans-Pro',
64 * 'fontFilePath': 'path/to/file'
65 * }
66 * )}`
67 *
68 * // CSS as JS Output
69 *
70 * '@font-face': {
71 * 'fontFamily': 'Sans-Pro',
72 * 'src': 'url("path/to/file.eot"), url("path/to/file.woff2"), url("path/to/file.woff"), url("path/to/file.ttf"), url("path/to/file.svg")',
73 * }
74 */
75
76function fontFace(_ref) {
77 var fontFamily = _ref.fontFamily,
78 fontFilePath = _ref.fontFilePath,
79 fontStretch = _ref.fontStretch,
80 fontStyle = _ref.fontStyle,
81 fontVariant = _ref.fontVariant,
82 fontWeight = _ref.fontWeight,
83 _ref$fileFormats = _ref.fileFormats,
84 fileFormats = _ref$fileFormats === void 0 ? ['eot', 'woff2', 'woff', 'ttf', 'svg'] : _ref$fileFormats,
85 _ref$formatHint = _ref.formatHint,
86 formatHint = _ref$formatHint === void 0 ? false : _ref$formatHint,
87 _ref$localFonts = _ref.localFonts,
88 localFonts = _ref$localFonts === void 0 ? [fontFamily] : _ref$localFonts,
89 unicodeRange = _ref.unicodeRange,
90 fontDisplay = _ref.fontDisplay,
91 fontVariationSettings = _ref.fontVariationSettings,
92 fontFeatureSettings = _ref.fontFeatureSettings;
93 // Error Handling
94 if (!fontFamily) throw new _errors["default"](55);
95 if (!fontFilePath && !localFonts) {
96 throw new _errors["default"](52);
97 }
98 if (localFonts && !Array.isArray(localFonts)) {
99 throw new _errors["default"](53);
100 }
101 if (!Array.isArray(fileFormats)) {
102 throw new _errors["default"](54);
103 }
104 var fontFaceDeclaration = {
105 '@font-face': {
106 fontFamily: fontFamily,
107 src: generateSources(fontFilePath, localFonts, fileFormats, formatHint),
108 unicodeRange: unicodeRange,
109 fontStretch: fontStretch,
110 fontStyle: fontStyle,
111 fontVariant: fontVariant,
112 fontWeight: fontWeight,
113 fontDisplay: fontDisplay,
114 fontVariationSettings: fontVariationSettings,
115 fontFeatureSettings: fontFeatureSettings
116 }
117 };
118
119 // Removes undefined fields for cleaner css object.
120 return JSON.parse(JSON.stringify(fontFaceDeclaration));
121}
122module.exports = exports.default;
\No newline at end of file