1 /**
  2  * The Render Engine
  3  * BitmapFontLoader
  4  *
  5  * @fileoverview An extension of the image resource loader for handling bitmap
  6  *               fonts.
  7  *
  8  * @author: Brett Fattori (brettf@renderengine.com)
  9  * @author: $Author: bfattori $
 10  * @version: $Revision: 1555 $
 11  *
 12  * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com)
 13  *
 14  * Permission is hereby granted, free of charge, to any person obtaining a copy
 15  * of this software and associated documentation files (the "Software"), to deal
 16  * in the Software without restriction, including without limitation the rights
 17  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 18  * copies of the Software, and to permit persons to whom the Software is
 19  * furnished to do so, subject to the following conditions:
 20  *
 21  * The above copyright notice and this permission notice shall be included in
 22  * all copies or substantial portions of the Software.
 23  *
 24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 29  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 30  * THE SOFTWARE.
 31  *
 32  */
 33 
 34 // The class this file defines and its required classes
 35 R.Engine.define({
 36     "class":"R.resources.loaders.BitmapFontLoader",
 37     "requires":[
 38         "R.resources.loaders.ImageLoader"
 39     ]
 40 });
 41 
 42 /**
 43  * @class Loads bitmap fonts and makes them available to the system.
 44  *
 45  * @constructor
 46  * @param name {String=BitmapFontLoader} The name of the resource loader
 47  * @extends R.resources.loaders.ImageLoader
 48  */
 49 R.resources.loaders.BitmapFontLoader = function () {
 50     return R.resources.loaders.ImageLoader.extend(/** @scope R.resources.loaders.BitmapFontLoader.prototype */{
 51 
 52         fonts:null,
 53 
 54         /** @private */
 55         constructor:function (name) {
 56             this.base(name || "BitmapFontLoader");
 57             this.fonts = {};
 58         },
 59 
 60         /**
 61          * Load a font resource from a URL.
 62          *
 63          * @param name {String} The name of the resource
 64          * @param url {String} The URL where the resource is located
 65          */
 66         load:function (name, url /*, info */) {
 67 
 68             // The bitmap is in the same path
 69             var path = R.Engine.getEnginePath() + "/fonts/";
 70 
 71             if (url) {
 72                 var thisObj = this;
 73 
 74                 // Get the file from the server
 75                 R.engine.Script.loadJSON(path + url, function (fontInfo, status) {
 76                     if (status == 200) {
 77                         R.debug.Console.log("Acquired font info: ", fontInfo);
 78                         thisObj.load(name, null, fontInfo);
 79                     }
 80                     else {
 81                         R.debug.Console.error("Error loading font for name '" + name + "', due to: " + status);
 82                     }
 83                 });
 84             }
 85             else {
 86                 var info = arguments[2];
 87                 R.debug.Console.log("Loading font: " + name + " @ " + path + info.bitmapImage);
 88 
 89                 // Store the font info
 90                 this.fonts[name] = info;
 91 
 92                 // Load the bitmap file
 93                 this.base(name, path + info.bitmapImage, info.width, info.height);
 94             }
 95         },
 96 
 97         /**
 98          * Get the font with the specified name from the cache.  The
 99          * object returned contains the bitmap as <tt>image</tt> and
100          * the font definition as <tt>info</tt>.
101          *
102          * @param name {String} The name of the object to retrieve
103          * @return {Object} The object representing the named font
104          */
105         get:function (name) {
106             var bitmap = this.base(name);
107             var font = {
108                 image:bitmap,
109                 info:this.fonts[name]
110             };
111             return font;
112         },
113 
114         /**
115          * The name of the resource this loader will get.
116          * @return {String} The string "bitmap font"
117          */
118         getResourceType:function () {
119             return "bitmap font";
120         }
121 
122     }, /** @scope R.resources.loaders.BitmapFontLoader.prototype */{
123         /**
124          * Get the class name of this object
125          * @return {String} The string "R.resources.loaders.BitmapFontLoader"
126          */
127         getClassName:function () {
128             return "R.resources.loaders.BitmapFontLoader";
129         }
130     });
131 
132 }
133