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