1 | "use strict";
|
2 |
|
3 | require("../vendor/FontLoader.js");
|
4 |
|
5 | function buildFontFaceRule(family, urls) {
|
6 | var eot = urls["embedded-opentype"];
|
7 | var woff = urls.woff;
|
8 | var ttf = urls.truetype;
|
9 | var svg = urls.svg;
|
10 |
|
11 | var css = "\n";
|
12 | css += "@font-face {\n";
|
13 | css += " font-family: '" + family + "';\n";
|
14 | css += " src: url('" + eot + "');\n";
|
15 | css += " src: url('" + eot + "?iefix') format('embedded-opentype'),\n";
|
16 | css += " url('" + woff + "') format('woff'),\n";
|
17 | css += " url('" + ttf + "') format('ttf'),\n";
|
18 | css += " url('" + svg + "') format('svg');\n";
|
19 | css += "}\n";
|
20 | return css;
|
21 | }
|
22 |
|
23 | function createCssFontFaces(fontFamilies) {
|
24 | var style = document.createElement("style");
|
25 | style.setAttribute("type", "text/css");
|
26 | var css = "";
|
27 | for (var family in fontFamilies) {
|
28 | if (fontFamilies.hasOwnProperty(family)) {
|
29 | css += buildFontFaceRule(family, fontFamilies[family]);
|
30 | }
|
31 | }
|
32 | style.appendChild(document.createTextNode(css));
|
33 | document.head.appendChild(style);
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | function FontLoader() {
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 | this.totalFonts = 0;
|
47 | |
48 |
|
49 |
|
50 |
|
51 |
|
52 | this.loadedFonts = 0;
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | FontLoader.prototype.load = function(fontFamilies) {
|
68 | createCssFontFaces(fontFamilies);
|
69 |
|
70 | var families = [];
|
71 | for (var family in fontFamilies) {
|
72 | if (families.hasOwnProperty(family)) {
|
73 | families.push(family);
|
74 | }
|
75 | }
|
76 | this.totalFonts += families.length;
|
77 |
|
78 | var self = this;
|
79 | var loader = new window.FontLoader(families, {
|
80 | "fontLoaded": function() {
|
81 | self.loadedFonts++;
|
82 | }
|
83 | });
|
84 | loader.loadFonts();
|
85 | };
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | FontLoader.prototype.allLoaded = function() {
|
91 | return this.totalFonts === this.loadedFonts;
|
92 | };
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | function EjectaFontLoader() {
|
100 | this.totalFonts = 0;
|
101 | this.loadedFonts = 0;
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 | EjectaFontLoader.prototype.load = function(fontFamilies) {
|
107 | for (var family in fontFamilies) {
|
108 | if (fontFamilies.hasOwnProperty(family)) {
|
109 | var fontPath = fontFamilies[family].truetype;
|
110 | if (fontPath) {
|
111 | window.ejecta.loadFont(fontPath);
|
112 | }
|
113 | }
|
114 | }
|
115 | };
|
116 |
|
117 |
|
118 |
|
119 | EjectaFontLoader.prototype.allLoaded = function() {
|
120 | return true;
|
121 | };
|
122 |
|
123 | if (window.ejecta) {
|
124 | module.exports = EjectaFontLoader;
|
125 | } else {
|
126 | module.exports = FontLoader;
|
127 | }
|