Hilo/build/amd/hilo/view/BitmapText.min.js
2022-06-29 10:45:02 +08:00

6 lines
1.7 KiB
JavaScript

/**
* hilojs 2.0.3 for amd
* Copyright 2016 alibaba.com
* Licensed under the MIT License
*/
define("hilo/view/BitmapText",["hilo/core/Class","hilo/core/Hilo","hilo/view/Container","hilo/view/Bitmap"],function(t,e,i,r){var h=t.create({Extends:i,constructor:function(t){t=t||{},this.id=this.id||t.id||e.getUid("BitmapText"),h.superclass.constructor.call(this,t);var i=t.text+"";i&&(this.text="",this.setText(i)),this.pointerChildren=!1},glyphs:null,letterSpacing:0,text:"",textAlign:"left",setText:function(t){var e=this,i=t.toString(),r=i.length;if(e.text!=i){e.text=i;var h,n,a,l,s=0,o=0,c=0;for(h=0;h<r;h++)n=i.charAt(h),a=e.glyphs[n],a&&(c=s+(s>0?e.letterSpacing:0),e.children[h]?(l=e.children[h],l.setImage(a.image,a.rect)):(l=e._createBitmap(a),e.addChild(l)),l.x=c,s=c+a.rect[2],o=Math.max(o,a.rect[3]));for(h=e.children.length-1;h>=r;h--)e._releaseBitmap(e.children[h]),e.children[h].removeFromParent();return e.width=s,e.height=o,this.setTextAlign(),e}},_createBitmap:function(t){var e;return h._pool.length?(e=h._pool.pop(),e.setImage(t.image,t.rect)):e=new r({image:t.image,rect:t.rect}),e},_releaseBitmap:function(t){h._pool.push(t)},setTextAlign:function(t){switch(this.textAlign=t||this.textAlign,this.textAlign){case"center":this.pivotX=.5*this.width;break;case"right":this.pivotX=this.width;break;case"left":default:this.pivotX=0}return this},hasGlyphs:function(t){var e=this.glyphs;if(!e)return!1;t=t.toString();var i,r=t.length;for(i=0;i<r;i++)if(!e[t.charAt(i)])return!1;return!0},Statics:{_pool:[],createGlyphs:function(t,e,i,r){var h=t.toString();i=i||h.length,r=r||1;for(var n=e.width/i,a=e.height/r,l={},s=0,o=t.length;s<o;s++){var c=h.charAt(s);l[c]={image:e,rect:[n*(s%i),a*Math.floor(s/i),n,a]}}return l}}});return h});