UNPKG

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