* @function LevelRenderer.Tool.Areal.prototype.getTextWidth * @description 测算多行文本宽度
(text, textFont)
| 1007 | * @description 测算多行文本宽度 |
| 1008 | */ |
| 1009 | getTextWidth(text, textFont) { |
| 1010 | var key = text + ':' + textFont; |
| 1011 | if (this._textWidthCache[key]) { |
| 1012 | return this._textWidthCache[key]; |
| 1013 | } |
| 1014 | this._ctx = this._ctx || this.util.getContext(); |
| 1015 | this._ctx.save(); |
| 1016 | |
| 1017 | if (textFont) { |
| 1018 | this._ctx.font = textFont; |
| 1019 | } |
| 1020 | |
| 1021 | text = (text + '').split('\n'); |
| 1022 | var width = 0; |
| 1023 | for (var i = 0, l = text.length; i < l; i++) { |
| 1024 | width = Math.max( |
| 1025 | this._ctx.measureText(text[i]).width, |
| 1026 | width |
| 1027 | ); |
| 1028 | } |
| 1029 | this._ctx.restore(); |
| 1030 | |
| 1031 | this._textWidthCache[key] = width; |
| 1032 | if (++this._textWidthCacheCounter > this.TEXT_CACHE_MAX) { |
| 1033 | // 内存释放 |
| 1034 | this._textWidthCacheCounter = 0; |
| 1035 | this._textWidthCache = {}; |
| 1036 | } |
| 1037 | |
| 1038 | return width; |
| 1039 | } |
| 1040 | |
| 1041 | /** |
| 1042 | * @function LevelRenderer.Tool.Areal.prototype.getTextHeight |
no test coverage detected