1 /****************************************************************************
  2  Copyright (c) 2011-2012 cocos2d-x.org
  3  Copyright (c) 2013-2014 Chukong Technologies Inc.
  4 
  5  http://www.cocos2d-x.org
  6 
  7  Permission is hereby granted, free of charge, to any person obtaining a copy
  8  of this software and associated documentation files (the "Software"), to deal
  9  in the Software without restriction, including without limitation the rights
 10  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  copies of the Software, and to permit persons to whom the Software is
 12  furnished to do so, subject to the following conditions:
 13 
 14  The above copyright notice and this permission notice shall be included in
 15  all copies or substantial portions of the Software.
 16 
 17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23  THE SOFTWARE.
 24  ****************************************************************************/
 25 
 26 cc._txtLoader = {
 27     load : function(realUrl, url, res, cb){
 28         cc.loader.loadTxt(realUrl, cb);
 29     }
 30 };
 31 cc.loader.register(["txt", "xml", "vsh", "fsh", "atlas"], cc._txtLoader);
 32 
 33 cc._jsonLoader = {
 34     load : function(realUrl, url, res, cb){
 35         cc.loader.loadJson(realUrl, cb);
 36     }
 37 };
 38 cc.loader.register(["json", "ExportJson"], cc._jsonLoader);
 39 
 40 cc._imgLoader = {
 41     load : function(realUrl, url, res, cb){
 42         cc.loader.cache[url] =  cc.loader.loadImg(realUrl, function(err, img){
 43             if(err)
 44                 return cb(err);
 45             cc.textureCache.handleLoadedTexture(url);
 46             cb(null, img);
 47         });
 48     }
 49 };
 50 cc.loader.register(["png", "jpg", "bmp","jpeg","gif"], cc._imgLoader);
 51 cc._serverImgLoader = {
 52     load : function(realUrl, url, res, cb){
 53         cc.loader.cache[url] =  cc.loader.loadImg(res.src, function(err, img){
 54             if(err)
 55                 return cb(err);
 56             cc.textureCache.handleLoadedTexture(url);
 57             cb(null, img);
 58         });
 59     }
 60 };
 61 cc.loader.register(["serverImg"], cc._serverImgLoader);
 62 
 63 cc._plistLoader = {
 64     load : function(realUrl, url, res, cb){
 65         cc.loader.loadTxt(realUrl, function(err, txt){
 66             if(err)
 67                 return cb(err);
 68             cb(null, cc.plistParser.parse(txt));
 69         });
 70     }
 71 };
 72 cc.loader.register(["plist"], cc._plistLoader);
 73 
 74 cc._fontLoader = {
 75     TYPE : {
 76         ".eot" : "embedded-opentype",
 77         ".ttf" : "truetype",
 78         ".woff" : "woff",
 79         ".svg" : "svg"
 80     },
 81     _loadFont : function(name, srcs, type){
 82         var doc = document, path = cc.path, TYPE = this.TYPE, fontStyle = cc.newElement("style");
 83         fontStyle.type = "text/css";
 84         doc.body.appendChild(fontStyle);
 85 
 86         var fontStr = "@font-face { font-family:" + name + "; src:";
 87         if(srcs instanceof Array){
 88             for(var i = 0, li = srcs.length; i < li; i++){
 89                 var src = srcs[i];
 90                 type = path.extname(src);
 91                 fontStr += "url('" + srcs[i] + "') format('" + TYPE[type] + "')";
 92                 fontStr += (i == li - 1) ? ";" : ",";
 93             }
 94         }else{
 95             fontStr += "url('" + srcs + "') format('" + TYPE[type] + "');";
 96         }
 97         fontStyle.textContent += fontStr + "};";
 98 
 99         //<div style="font-family: PressStart;">.</div>
100         var preloadDiv = cc.newElement("div");
101         var _divStyle =  preloadDiv.style;
102         _divStyle.fontFamily = name;
103         preloadDiv.innerHTML = ".";
104         _divStyle.position = "absolute";
105         _divStyle.left = "-100px";
106         _divStyle.top = "-100px";
107         doc.body.appendChild(preloadDiv);
108     },
109     load : function(realUrl, url, res, cb){
110         var self = this;
111         var type = res.type, name = res.name, srcs = res.srcs;
112         if(typeof res == "string"){
113             type = cc.path.extname(res);
114             name = cc.path.basename(res, type);
115             self._loadFont(name, res, type);
116         }else{
117             self._loadFont(name, srcs);
118         }
119         cb(null, true);
120     }
121 };
122 cc.loader.register(["font", "eot", "ttf", "woff", "svg"], cc._fontLoader);
123 
124 cc._binaryLoader = {
125     load : function(realUrl, url, res, cb){
126         cc.loader.loadBinary(realUrl, cb);
127     }
128 };