|
@@ -7,7 +7,7 @@ fonts.forEach(function(font, i){
|
7
|
7
|
var extension = "",
|
8
|
8
|
split = font.file.split(".");
|
9
|
9
|
|
10
|
|
- // Use existing extension if there is one
|
|
10
|
+ // Use existing file extension if there is one
|
11
|
11
|
if (split.length > 1){
|
12
|
12
|
extension = "." + split.pop();
|
13
|
13
|
}
|
|
@@ -16,11 +16,19 @@ fonts.forEach(function(font, i){
|
16
|
16
|
|
17
|
17
|
});
|
18
|
18
|
|
|
19
|
+// Send a stylesheet with custom fonts
|
19
|
20
|
function sendCSS(req, res) {
|
20
|
21
|
res.set("Content-Type", "text/css")
|
21
|
22
|
.send(fonts.map(declaration).join("\n\n"));
|
22
|
23
|
}
|
23
|
24
|
|
|
25
|
+// Send JS that forces all custom fonts to download upfront
|
|
26
|
+function sendJS(req, res) {
|
|
27
|
+ res.set("Content-Type", "application/javascript")
|
|
28
|
+ .send("(function(){\n\n" + fonts.map(shim).join("\n\n") + "\n\n})();");
|
|
29
|
+}
|
|
30
|
+
|
|
31
|
+// Send custom file by its slug
|
24
|
32
|
function sendFont(req, res) {
|
25
|
33
|
|
26
|
34
|
var font = bySlug[req.params.font];
|
|
@@ -44,7 +52,20 @@ function declaration(font, i) {
|
44
|
52
|
].filter(function(d){ return d; }).join("\n");
|
45
|
53
|
}
|
46
|
54
|
|
|
55
|
+function shim(font, i) {
|
|
56
|
+ return [
|
|
57
|
+ " var font" + i + " = document.createElement(\"div\");",
|
|
58
|
+ " font" + i + ".innerHTML = '.';",
|
|
59
|
+ " font" + i + ".style.fontFamily = \"" + font.family + "\";",
|
|
60
|
+ font.weight ? " font" + i + ".style.fontWeight = \"" + font.weight + "\";" : "",
|
|
61
|
+ font.style ? " font" + i + ".style.fontStyle = \"" + font.style + "\";" : "",
|
|
62
|
+ " document.body.appendChild(font" + i + ");",
|
|
63
|
+ " setTimeout(function(){ font" + i + ".remove(); }, 0);"
|
|
64
|
+ ].filter(function(d){ return d; }).join("\n");
|
|
65
|
+}
|
|
66
|
+
|
47
|
67
|
module.exports = {
|
48
|
68
|
css: sendCSS,
|
|
69
|
+ js: sendJS,
|
49
|
70
|
font: sendFont
|
50
|
71
|
};
|