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

6 lines
1.7 KiB
JavaScript

/**
* hilojs 2.0.3 for cmd
* Copyright 2016 alibaba.com
* Licensed under the MIT License
*/
define(function(t,e,i){var r=t("hilo/core/Class"),n=t("hilo/core/Hilo"),h=t("hilo/view/Container"),a=t("hilo/view/Bitmap"),l=r.create({Extends:h,constructor:function(t){t=t||{},this.id=this.id||t.id||n.getUid("BitmapText"),l.superclass.constructor.call(this,t);var e=t.text+"";e&&(this.text="",this.setText(e)),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 n,h,a,l,s=0,o=0,c=0;for(n=0;n<r;n++)h=i.charAt(n),a=e.glyphs[h],a&&(c=s+(s>0?e.letterSpacing:0),e.children[n]?(l=e.children[n],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(n=e.children.length-1;n>=r;n--)e._releaseBitmap(e.children[n]),e.children[n].removeFromParent();return e.width=s,e.height=o,this.setTextAlign(),e}},_createBitmap:function(t){var e;return l._pool.length?(e=l._pool.pop(),e.setImage(t.image,t.rect)):e=new a({image:t.image,rect:t.rect}),e},_releaseBitmap:function(t){l._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 n=t.toString();i=i||n.length,r=r||1;for(var h=e.width/i,a=e.height/r,l={},s=0,o=t.length;s<o;s++){var c=n.charAt(s);l[c]={image:e,rect:[h*(s%i),a*Math.floor(s/i),h,a]}}return l}}});return l});