UNPKG

3.19 kBJavaScriptView Raw
1"use strict";
2
3require("../vendor/FontLoader.js");
4
5function 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
23function 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 * Load fonts and lets you know when they're all available. An instance of FontLoader is available as {@link Splat.Game#fonts}.
38 * @constructor
39 */
40function FontLoader() {
41 /**
42 * The total number of fonts to be loaded.
43 * @member {number}
44 * @private
45 */
46 this.totalFonts = 0;
47 /**
48 * The number of fonts that have loaded completely.
49 * @member {number}
50 * @private
51 */
52 this.loadedFonts = 0;
53}
54/**
55 * Load a font.
56 * @param {object} fontFamilies A key-value object that maps css font-family names to another object that holds paths to the various font files in different formats.
57 * @example
58game.fonts.load({
59 "pixelade": {
60 "embedded-opentype": "pixelade/pixelade-webfont.eot",
61 "woff": "pixelade/pixelade-webfont.woff",
62 "truetype": "pixelade/pixelade-webfont.ttf",
63 "svg": "pixelade/pixelade-webfont.svg#pixeladeregular"
64 }
65});
66 */
67FontLoader.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 * Test if all font fonts have loaded.
88 * @returns {boolean}
89 */
90FontLoader.prototype.allLoaded = function() {
91 return this.totalFonts === this.loadedFonts;
92};
93
94/**
95 * An alternate {@link FontLoader} when the game is running inside [Ejecta]{@link http://impactjs.com/ejecta}. You shouldn't need to worry about this.
96 * @constructor
97 * @private
98 */
99function EjectaFontLoader() {
100 this.totalFonts = 0;
101 this.loadedFonts = 0;
102}
103/**
104 * See {@link FontLoader#load}.
105 */
106EjectaFontLoader.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 * See {@link FontLoader#allLoaded}.
118 */
119EjectaFontLoader.prototype.allLoaded = function() {
120 return true;
121};
122
123if (window.ejecta) {
124 module.exports = EjectaFontLoader;
125} else {
126 module.exports = FontLoader;
127}