UNPKG

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