mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
Secure measureText in wechat (offscreen)
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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 = [];
|
||||
|
||||
Reference in New Issue
Block a user