1 
  2 /**
  3  * @class BitmapText类提供使用位图文本的功能。当前仅支持单行文本。
  4  * @augments Container
  5  * @param {Object} properties 创建对象的属性参数。可包含此类所有可写属性。
  6  * @module hilo/view/BitmapText
  7  * @requires hilo/core/Class
  8  * @requires hilo/core/Hilo
  9  * @requires hilo/view/Container
 10  * @property {Object} glyphs 位图字体的字形集合。格式为:{letter:{image:img, rect:[0,0,100,100]}}。
 11  * @property {Number} letterSpacing 字距,即字符间的间隔。默认值为0。
 12  */
 13 var BitmapText = Class.create(/** @lends BitmapText.prototype */{
 14     Extends: Container,
 15     constructor: function BitmapText(properties){
 16         properties = properties || {};
 17         this.id = this.id || properties.id || Hilo.getUid('BitmapText');
 18         BitmapText.superclass.constructor.call(this, properties);
 19 
 20         this.pointerChildren = false; //disable user events for single letters
 21     },
 22 
 23     glyphs: null,
 24     letterSpacing: 0,
 25     text: {
 26         get: function(){
 27             return this._text || '';
 28         },
 29         set: function(str){
 30             var me = this, str = str.toString(), len = str.length;
 31             if(me._text == str) return;
 32             me._text = str;
 33 
 34             if(!me.children) me.children = [];
 35             me.removeAllChildren();
 36 
 37             var i, charStr, charGlyph, charObj, width = 0, height = 0, left = 0;
 38 
 39             for(i = 0; i < len; i++){
 40                 charStr = str.charAt(i);
 41                 charGlyph = me.glyphs[charStr];
 42                 if(charGlyph){
 43                     left = width + (width > 0 ? me.letterSpacing : 0);
 44                     charObj = new Bitmap({
 45                         image: charGlyph.image,
 46                         rect: charGlyph.rect,
 47                         x: left
 48                     });
 49                     width = left + charGlyph.rect[2];
 50                     height = Math.max(height, charGlyph.rect[3]);
 51                     me.addChild(charObj);
 52                 }
 53             }
 54 
 55             me.width = width;
 56             me.height = height;
 57         }
 58     },
 59 
 60     /**
 61      * 返回能否使用当前指定的字体显示提供的字符串。
 62      * @param {String} str 要检测的字符串。
 63      * @returns {Boolean} 是否能使用指定字体。
 64      */
 65     hasGlyphs: function(str){
 66         var glyphs = this.glyphs;
 67         if(!glyphs) return false;
 68 
 69         var str = str.toString(), len = str.length, i;
 70         for(i = 0; i < len; i++){
 71             if(!glyphs[str.charAt(i)]) return false;
 72         }
 73         return true;
 74     }
 75 
 76 });