From c42f77ab255c6c61274b1ee2be1c8b8bc2d29602 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 17 Jul 2018 16:33:34 +0800 Subject: [PATCH] Secure measureText in wechat (offscreen) --- cocos2d/core/renderer/utils/label/ttf.js | 14 +++++++------- cocos2d/core/utils/text-utils.js | 9 +++++++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cocos2d/core/renderer/utils/label/ttf.js b/cocos2d/core/renderer/utils/label/ttf.js index 9a1afbb642..70ced076d2 100644 --- a/cocos2d/core/renderer/utils/label/ttf.js +++ b/cocos2d/core/renderer/utils/label/ttf.js @@ -317,7 +317,7 @@ module.exports = { let canvasSizeX = 0; let canvasSizeY = 0; for (let i = 0; i < paragraphedStrings.length; ++i) { - let paraLength = _context.measureText(paragraphedStrings[i]).width; + let paraLength = TextUtils.safeMeasureText(_context, paragraphedStrings[i]); canvasSizeX = canvasSizeX > paraLength ? canvasSizeX : paraLength; } canvasSizeY = _splitedStrings.length * this._getLineHeight(); @@ -369,7 +369,7 @@ module.exports = { _splitedStrings = []; let canvasWidthNoMargin = _canvasSize.width - 2 * _margin; for (let i = 0; i < paragraphedStrings.length; ++i) { - let allWidth = _context.measureText(paragraphedStrings[i]).width; + let allWidth = TextUtils.safeMeasureText(_context, paragraphedStrings[i]); let textFragment = TextUtils.fragmentText(paragraphedStrings[i], allWidth, canvasWidthNoMargin, @@ -408,8 +408,8 @@ module.exports = { let paragraphLength = []; for (let i = 0; i < paragraphedStrings.length; ++i) { - let textMetric = ctx.measureText(paragraphedStrings[i]); - paragraphLength.push(textMetric.width); + let width = TextUtils.safeMeasureText(ctx, paragraphedStrings[i]); + paragraphLength.push(width); } return paragraphLength; @@ -417,7 +417,7 @@ module.exports = { _measureText (ctx) { return function (string) { - return ctx.measureText(string).width; + return TextUtils.measureText(ctx, string); }; }, @@ -468,13 +468,13 @@ module.exports = { totalHeight = 0; for (i = 0; i < paragraphedStrings.length; ++i) { let j = 0; - let allWidth = _context.measureText(paragraphedStrings[i]).width; + let allWidth = TextUtils.safeMeasureText(_context, paragraphedStrings[i]); textFragment = TextUtils.fragmentText(paragraphedStrings[i], allWidth, canvasWidthNoMargin, this._measureText(_context)); while (j < textFragment.length) { - let measureWidth = _context.measureText(textFragment[j]).width; + let measureWidth = TextUtils.safeMeasureText(_context, textFragment[j]); maxLength = measureWidth; totalHeight += this._getLineHeight(); ++j; diff --git a/cocos2d/core/utils/text-utils.js b/cocos2d/core/utils/text-utils.js index 9b3147afae..09554d27cb 100644 --- a/cocos2d/core/utils/text-utils.js +++ b/cocos2d/core/utils/text-utils.js @@ -90,7 +90,7 @@ var CustomFontLoader = { } var oldWidth = this._fontWidthCache[k]; this._canvasContext.font = '40px ' + k; - var newWidth = this._canvasContext.measureText(this._testString).width; + var newWidth = TextUtils.safeMeasureText(this._canvasContext, this._testString); if(oldWidth !== newWidth) { fontDescriptor.onLoaded(); } else { @@ -169,7 +169,7 @@ var CustomFontLoader = { var fontDesc = '40px ' + fontFamilyName; this._canvasContext.font = fontDesc; - var width = this._canvasContext.measureText(this._testString).width; + var width = TextUtils.safeMeasureText(this._canvasContext, this._testString); this._fontWidthCache[fontFamilyName] = width; var self = this; @@ -227,6 +227,11 @@ var TextUtils = { return ((ch >= 9 && ch <= 13) || ch === 32 || ch === 133 || ch === 160 || ch === 5760 || (ch >= 8192 && ch <= 8202) || ch === 8232 || ch === 8233 || ch === 8239 || ch === 8287 || ch === 12288); }, + safeMeasureText = function (ctx, string) { + var metric = ctx.measureText(string); + return metric && metric.width || 0; + }, + fragmentText: function (stringToken, allWidth, maxWidth, measureText) { //check the first character var wrappedWords = [];