1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports["default"] = fontFace;
|
5 | var _errors = _interopRequireDefault(require("../internalHelpers/_errors"));
|
6 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
7 | var dataURIRegex = /^\s*data:([a-z]+\/[a-z-]+(;[a-z-]+=[a-z-]+)?)?(;charset=[a-z0-9-]+)?(;base64)?,[a-z0-9!$&',()*+,;=\-._~:@/?%\s]*\s*$/i;
|
8 | var formatHintMap = {
|
9 | woff: 'woff',
|
10 | woff2: 'woff2',
|
11 | ttf: 'truetype',
|
12 | otf: 'opentype',
|
13 | eot: 'embedded-opentype',
|
14 | svg: 'svg',
|
15 | svgz: 'svg'
|
16 | };
|
17 | function generateFormatHint(format, formatHint) {
|
18 | if (!formatHint) return '';
|
19 | return " format(\"" + formatHintMap[format] + "\")";
|
20 | }
|
21 | function isDataURI(fontFilePath) {
|
22 | return !!fontFilePath.replace(/\s+/g, ' ').match(dataURIRegex);
|
23 | }
|
24 | function 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 | }
|
33 | function generateLocalReferences(localFonts) {
|
34 | var localFontReferences = localFonts.map(function (font) {
|
35 | return "local(\"" + font + "\")";
|
36 | });
|
37 | return localFontReferences.join(', ');
|
38 | }
|
39 | function 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 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | function 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 |
|
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 |
|
120 | return JSON.parse(JSON.stringify(fontFaceDeclaration));
|
121 | }
|
122 | module.exports = exports.default; |
\ | No newline at end of file |