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

6 lines
2.3 KiB
JavaScript

/**
* hilojs 2.0.3 for cmd
* Copyright 2016 alibaba.com
* Licensed under the MIT License
*/
define(function(t,e,i){var n=t("hilo/core/Class"),o=t("hilo/core/Hilo"),h=t("hilo/view/View"),r=t("hilo/view/CacheMixin"),l=n.create({Extends:h,Mixes:r,constructor:function(t){t=t||{},this.id=this.id||t.id||o.getUid("Text"),l.superclass.constructor.call(this,t),t.font||(this.font="12px arial"),this._fontHeight=l.measureFontHeight(this.font)},text:"",color:"#000",textAlign:null,textVAlign:null,outline:!1,lineSpacing:0,maxWidth:200,font:null,textWidth:0,textHeight:0,setFont:function(t){var e=this;return e.font!==t&&(e.font=t,e._fontHeight=l.measureFontHeight(t)),e},render:function(t,e){var i=this;if("canvas"===t.renderType)this.drawable?t.draw(i):i._draw(t.context);else if("dom"===t.renderType){var n=i.drawable,o=n.domElement,h=o.style;h.font=i.font,h.textAlign=i.textAlign,h.color=i.color,h.width=i.width+"px",h.height=i.height+"px",h.lineHeight=i._fontHeight+i.lineSpacing+"px",o.innerHTML=i.text,t.draw(this)}else i.cache(),t.draw(i)},_draw:function(t){var e=this,i=e.text.toString();if(i){t.font=e.font,t.textAlign=e.textAlign,t.textBaseline="top";var n,o,h,r,l,a=i.split(/\r\n|\r|\n|<br(?:[ \/])*>/),s=0,c=0,d=e._fontHeight+e.lineSpacing,x=[];for(n=0,r=a.length;n<r;n++)if(o=a[n],h=t.measureText(o).width,h<=e.maxWidth)x.push({text:o,y:c}),s<h&&(s=h),c+=d;else{var g,f,u,w="",m=0;for(f=0,l=o.length;f<l;f++)u=o[f],g=t.measureText(w+u).width,g>e.maxWidth?(x.push({text:w,y:c}),s<m&&(s=m),c+=d,w=u):(m=g,w+=u),f==l-1&&(x.push({text:w,y:c}),w!==u&&s<g&&(s=g),c+=d)}e.textWidth=s,e.textHeight=c,e.width||(e.width=s),e.height||(e.height=c);var p=0;switch(e.textVAlign){case"middle":p=e.height-e.textHeight>>1;break;case"bottom":p=e.height-e.textHeight}var v=e.background;for(v&&(t.fillStyle=v,t.fillRect(0,0,e.width,e.height)),e.outline?t.strokeStyle=e.color:t.fillStyle=e.color,n=0;n<x.length;n++)o=x[n],e._drawTextLine(t,o.text,p+o.y)}},_drawTextLine:function(t,e,i){var n=this,o=0,h=n.width;switch(n.textAlign){case"center":o=h>>1;break;case"right":case"end":o=h}n.outline?t.strokeText(e,o,i):t.fillText(e,o,i)},Statics:{measureFontHeight:function(t){var e,i=document.documentElement,n=o.createElement("div",{style:{font:t,position:"absolute"},innerHTML:"M"});return i.appendChild(n),e=n.offsetHeight,i.removeChild(n),e}}});return l});