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