mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
Merge pull request #1139 from zilongshanren/addRichTextImageSupport
Refactoring and support cyrillic language wrap
This commit is contained in:
@@ -22,15 +22,12 @@
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
require('../label/CCHtmlTextParser.js');
|
||||
require('../label/CCTextUtils.js');
|
||||
|
||||
var HorizontalAlign = cc.TextAlignment;
|
||||
var VerticalAlign = cc.VerticalTextAlignment;
|
||||
|
||||
var label_wrapinspection = true;
|
||||
var label_wordRex = /([a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]+|\S)/;
|
||||
var label_symbolRex = /^[!,.:;}\]%\?>、‘“》?。,!]/;
|
||||
var label_lastWordRex = /([a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]+|\S)$/;
|
||||
var label_lastEnglish = /[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]+$/;
|
||||
var label_firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
|
||||
|
||||
// Returns a function, that, as long as it continues to be invoked, will not
|
||||
// be triggered. The function will be called after it stops being called for
|
||||
@@ -200,19 +197,27 @@ var RichText = cc.Class({
|
||||
this._updateRichText();
|
||||
},
|
||||
|
||||
_measureText: function (string, styleIndex) {
|
||||
var label;
|
||||
if(this._labelSegmentsCache.length === 0) {
|
||||
label = new _ccsg.Label(string);
|
||||
this._labelSegmentsCache.push(label);
|
||||
_measureText: function (styleIndex, string) {
|
||||
var self = this;
|
||||
var func = function (string) {
|
||||
var label;
|
||||
if(self._labelSegmentsCache.length === 0) {
|
||||
label = new _ccsg.Label(string);
|
||||
self._labelSegmentsCache.push(label);
|
||||
} else {
|
||||
label = self._labelSegmentsCache[0];
|
||||
label.setString(string);
|
||||
}
|
||||
label._styleIndex = styleIndex;
|
||||
self._applyTextAttribute(label);
|
||||
var labelSize = label.getContentSize();
|
||||
return labelSize.width;
|
||||
};
|
||||
if(string) {
|
||||
return func(string);
|
||||
} else {
|
||||
label = this._labelSegmentsCache[0];
|
||||
label.setString(string);
|
||||
return func;
|
||||
}
|
||||
label._styleIndex = styleIndex;
|
||||
this._applyTextAttribute(label);
|
||||
var labelSize = label.getContentSize();
|
||||
return labelSize.width;
|
||||
},
|
||||
|
||||
|
||||
@@ -244,92 +249,6 @@ var RichText = cc.Class({
|
||||
this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this);
|
||||
},
|
||||
|
||||
_fragmentText: function (stringToken, allWidth, styleIndex) {
|
||||
//check the first character
|
||||
var wrappedWords = [];
|
||||
//fast return if strArr is empty
|
||||
if(stringToken.length === 0 || this.maxWidth < 0) {
|
||||
wrappedWords.push('');
|
||||
return wrappedWords;
|
||||
}
|
||||
|
||||
var text = stringToken;
|
||||
while (allWidth > this.maxWidth && text.length > 1) {
|
||||
|
||||
var fuzzyLen = text.length * ( this.maxWidth / allWidth ) | 0;
|
||||
var tmpText = text.substr(fuzzyLen);
|
||||
var width = allWidth - this._measureText(tmpText, styleIndex);
|
||||
var sLine = tmpText;
|
||||
var pushNum = 0;
|
||||
|
||||
var checkWhile = 0;
|
||||
var checkCount = 10;
|
||||
|
||||
//Exceeded the size
|
||||
while (width > this.maxWidth && checkWhile++ < checkCount) {
|
||||
fuzzyLen *= this.maxWidth / width;
|
||||
fuzzyLen = fuzzyLen | 0;
|
||||
tmpText = text.substr(fuzzyLen);
|
||||
width = allWidth - this._measureText(tmpText, styleIndex);
|
||||
}
|
||||
|
||||
checkWhile = 0;
|
||||
|
||||
//Find the truncation point
|
||||
while (width < this.maxWidth && checkWhile++ < checkCount) {
|
||||
if (tmpText) {
|
||||
var exec = label_wordRex.exec(tmpText);
|
||||
pushNum = exec ? exec[0].length : 1;
|
||||
sLine = tmpText;
|
||||
}
|
||||
|
||||
fuzzyLen = fuzzyLen + pushNum;
|
||||
tmpText = text.substr(fuzzyLen);
|
||||
width = allWidth - this._measureText(tmpText, styleIndex);
|
||||
}
|
||||
|
||||
fuzzyLen -= pushNum;
|
||||
if (fuzzyLen === 0) {
|
||||
fuzzyLen = 1;
|
||||
sLine = sLine.substr(1);
|
||||
}
|
||||
|
||||
var sText = text.substr(0, fuzzyLen), result;
|
||||
|
||||
//symbol in the first
|
||||
if (label_wrapinspection) {
|
||||
if (label_symbolRex.test(sLine || tmpText)) {
|
||||
result = label_lastWordRex.exec(sText);
|
||||
fuzzyLen -= result ? result[0].length : 0;
|
||||
if (fuzzyLen === 0) fuzzyLen = 1;
|
||||
|
||||
sLine = text.substr(fuzzyLen);
|
||||
sText = text.substr(0, fuzzyLen);
|
||||
}
|
||||
}
|
||||
|
||||
//To judge whether a English words are truncated
|
||||
if (label_firsrEnglish.test(sLine)) {
|
||||
result = label_lastEnglish.exec(sText);
|
||||
if (result && sText !== result[0]) {
|
||||
fuzzyLen -= result[0].length;
|
||||
sLine = text.substr(fuzzyLen);
|
||||
sText = text.substr(0, fuzzyLen);
|
||||
}
|
||||
}
|
||||
if (sText.trim().length > 0) {
|
||||
wrappedWords.push(sText);
|
||||
}
|
||||
text = sLine || tmpText;
|
||||
allWidth = this._measureText(text, styleIndex);
|
||||
}
|
||||
if (text.length > 0) {
|
||||
wrappedWords.push(text);
|
||||
}
|
||||
|
||||
return wrappedWords;
|
||||
},
|
||||
|
||||
_resetState: function () {
|
||||
var sgNode = this._sgNode;
|
||||
if(sgNode) {
|
||||
@@ -380,9 +299,11 @@ var RichText = cc.Class({
|
||||
//concat previous line
|
||||
var checkStartIndex = 0;
|
||||
while (this._lineOffsetX <= this.maxWidth) {
|
||||
var checkEndIndex = this._getFirstWordLen(labelString, checkStartIndex, labelString.length);
|
||||
var checkEndIndex = this._getFirstWordLen(labelString,
|
||||
checkStartIndex,
|
||||
labelString.length);
|
||||
var checkString = labelString.substr(checkStartIndex, checkEndIndex);
|
||||
var checkStringWidth = this._measureText(checkString, styleIndex);
|
||||
var checkStringWidth = this._measureText(styleIndex, checkString);
|
||||
|
||||
if(this._lineOffsetX + checkStringWidth <= this.maxWidth) {
|
||||
this._lineOffsetX += checkStringWidth;
|
||||
@@ -395,7 +316,7 @@ var RichText = cc.Class({
|
||||
this._addLabelSegment(remainingString, styleIndex);
|
||||
|
||||
labelString = labelString.substr(checkStartIndex, labelString.length);
|
||||
fragmentWidth = this._measureText(labelString, styleIndex);
|
||||
fragmentWidth = this._measureText(styleIndex, labelString);
|
||||
}
|
||||
this._updateLineInfo();
|
||||
break;
|
||||
@@ -403,7 +324,10 @@ var RichText = cc.Class({
|
||||
}
|
||||
}
|
||||
if(fragmentWidth > this.maxWidth) {
|
||||
var fragments = this._fragmentText(labelString, fragmentWidth, styleIndex);
|
||||
var fragments = cc.TextUtils.fragmentText(labelString,
|
||||
fragmentWidth,
|
||||
this.maxWidth,
|
||||
this._measureText(styleIndex));
|
||||
for(var k = 0; k < fragments.length; ++k) {
|
||||
var splitString = fragments[k];
|
||||
|
||||
@@ -507,7 +431,8 @@ var RichText = cc.Class({
|
||||
var labelString = multilineTexts[j];
|
||||
if(labelString === "") {
|
||||
//for continues \n
|
||||
if(this._isLastComponentCR(text) && j == multilineTexts.length - 1) {
|
||||
if(this._isLastComponentCR(text)
|
||||
&& j == multilineTexts.length - 1) {
|
||||
continue;
|
||||
}
|
||||
this._updateLineInfo();
|
||||
@@ -517,7 +442,7 @@ var RichText = cc.Class({
|
||||
lastEmptyLine = false;
|
||||
|
||||
if(this.maxWidth > 0) {
|
||||
var labelWidth = this._measureText(labelString, i);
|
||||
var labelWidth = this._measureText(i, labelString);
|
||||
this._updateRichTextWithMaxWidth(labelString, labelWidth, i);
|
||||
|
||||
if(multilineTexts.length > 1 && j < multilineTexts.length - 1) {
|
||||
@@ -553,29 +478,19 @@ var RichText = cc.Class({
|
||||
this._layoutDirty = false;
|
||||
},
|
||||
|
||||
_isCJK_unicode: function(ch) {
|
||||
var __CHINESE_REG = /^[\u4E00-\u9FFF\u3400-\u4DFF]+$/;
|
||||
var __JAPANESE_REG = /[\u3000-\u303F]|[\u3040-\u309F]|[\u30A0-\u30FF]|[\uFF00-\uFFEF]|[\u4E00-\u9FAF]|[\u2605-\u2606]|[\u2190-\u2195]|\u203B/g;
|
||||
var __KOREAN_REG = /^[\u1100-\u11FF]|[\u3130-\u318F]|[\uA960-\uA97F]|[\uAC00-\uD7AF]|[\uD7B0-\uD7FF]+$/;
|
||||
return __CHINESE_REG.test(ch) || __JAPANESE_REG.test(ch) || __KOREAN_REG.test(ch);
|
||||
},
|
||||
|
||||
//Checking whether the character is a whitespace
|
||||
_isspace_unicode: function(ch) {
|
||||
ch = ch.charCodeAt(0);
|
||||
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);
|
||||
},
|
||||
|
||||
_getFirstWordLen: function(text, startIndex, textLen) {
|
||||
var character = text.charAt(startIndex);
|
||||
if (this._isCJK_unicode(character) || this._isspace_unicode(character)) {
|
||||
if (cc.TextUtils.isUnicodeCJK(character)
|
||||
|| cc.TextUtils.isUnicodeSpace(character)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
var len = 1;
|
||||
for (var index = startIndex + 1; index < textLen; ++index) {
|
||||
character = text.charAt(index);
|
||||
if (this._isspace_unicode(character) || this._isCJK_unicode(character)) {
|
||||
if (cc.TextUtils.isUnicodeSpace(character)
|
||||
|| cc.TextUtils.isUnicodeCJK(character)) {
|
||||
break;
|
||||
}
|
||||
len++;
|
||||
|
||||
@@ -34,7 +34,6 @@ if (!CC_EDITOR || !Editor.isMainProcess) {
|
||||
require('./CCNode');
|
||||
require('./CCScene');
|
||||
|
||||
require('./label/CCHtmlTextParser.js');
|
||||
require('./components');
|
||||
require('./graphics');
|
||||
require('./collider');
|
||||
|
||||
@@ -536,7 +536,8 @@ _ccsg.Label = _ccsg.Node.extend({
|
||||
if (CC_EDITOR) {
|
||||
this._updateLabel();
|
||||
} else {
|
||||
this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.textDirty|_ccsg.Node._dirtyFlags.contentDirty);
|
||||
this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.textDirty
|
||||
|_ccsg.Node._dirtyFlags.contentDirty);
|
||||
}
|
||||
},
|
||||
_createRenderCmd: function() {
|
||||
@@ -627,7 +628,6 @@ cc.BMFontHelper = {
|
||||
var ret = true;
|
||||
|
||||
this._spriteBatchNode.removeAllChildren();
|
||||
var letterClamp = false;
|
||||
for (var ctr = 0; ctr < this._string.length; ++ctr) {
|
||||
if (this._lettersInfo[ctr]._valid) {
|
||||
var letterDef = this._fontAtlas._letterDefinitions[this._lettersInfo[ctr]._char];
|
||||
@@ -662,7 +662,6 @@ cc.BMFontHelper = {
|
||||
this._reusedRect.width = 0;
|
||||
} else if (this._overFlow === _ccsg.Label.Overflow.SHRINK) {
|
||||
if (this._contentSize.width > letterDef._width) {
|
||||
letterClamp = true;
|
||||
ret = false;
|
||||
break;
|
||||
} else {
|
||||
@@ -694,7 +693,6 @@ cc.BMFontHelper = {
|
||||
|
||||
this._updateLetterSpriteScale(fontChar);
|
||||
|
||||
// this._spriteBatchNode.insertQuadFromSprite(this._reusedLetter, index);
|
||||
this._spriteBatchNode.addChild(fontChar);
|
||||
|
||||
}
|
||||
@@ -813,7 +811,7 @@ cc.BMFontHelper = {
|
||||
&& this._maxLineWidth > 0
|
||||
&& nextTokenX > 0
|
||||
&& letterX + letterDef._width * this._bmfontScale > this._maxLineWidth
|
||||
&& !this._isspace_unicode(character)) {
|
||||
&& !cc.TextUtils.isUnicodeSpace(character)) {
|
||||
this._linesWidth.push(letterRight);
|
||||
letterRight = 0;
|
||||
lineIndex++;
|
||||
@@ -1038,26 +1036,15 @@ cc.BMFontHelper = {
|
||||
}
|
||||
},
|
||||
|
||||
_getFirstCharLen: function(text, startIndex, textLen) {
|
||||
_getFirstCharLen: function() {
|
||||
return 1;
|
||||
},
|
||||
|
||||
_isCJK_unicode: function(ch) {
|
||||
var __CHINESE_REG = /^[\u4E00-\u9FFF\u3400-\u4DFF]+$/;
|
||||
var __JAPANESE_REG = /[\u3000-\u303F]|[\u3040-\u309F]|[\u30A0-\u30FF]|[\uFF00-\uFFEF]|[\u4E00-\u9FAF]|[\u2605-\u2606]|[\u2190-\u2195]|\u203B/g;
|
||||
var __KOREAN_REG = /^[\u1100-\u11FF]|[\u3130-\u318F]|[\uA960-\uA97F]|[\uAC00-\uD7AF]|[\uD7B0-\uD7FF]+$/;
|
||||
return __CHINESE_REG.test(ch) || __JAPANESE_REG.test(ch) || __KOREAN_REG.test(ch);
|
||||
},
|
||||
|
||||
//Checking whether the character is a whitespace
|
||||
_isspace_unicode: function(ch) {
|
||||
ch = ch.charCodeAt(0);
|
||||
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);
|
||||
},
|
||||
|
||||
_getFirstWordLen: function(text, startIndex, textLen) {
|
||||
var character = text.charAt(startIndex);
|
||||
if (this._isCJK_unicode(character) || character === "\n" || this._isspace_unicode(character)) {
|
||||
if (cc.TextUtils.isUnicodeCJK(character)
|
||||
|| character === "\n"
|
||||
|| cc.TextUtils.isUnicodeSpace(character)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1075,13 +1062,17 @@ cc.BMFontHelper = {
|
||||
}
|
||||
letterX = nextLetterX + letterDef._offsetX * this._bmfontScale;
|
||||
|
||||
if(letterX + letterDef._width * this._bmfontScale > this._maxLineWidth && !this._isspace_unicode(character) && this._maxLineWidth > 0) {
|
||||
if(letterX + letterDef._width * this._bmfontScale > this._maxLineWidth
|
||||
&& !cc.TextUtils.isUnicodeSpace(character)
|
||||
&& this._maxLineWidth > 0) {
|
||||
if(len >= 2) {
|
||||
return len - 1;
|
||||
}
|
||||
}
|
||||
nextLetterX += letterDef._xAdvance * this._bmfontScale + this._additionalKerning;
|
||||
if (character === "\n" || this._isspace_unicode(character) || this._isCJK_unicode(character)) {
|
||||
if (character === "\n"
|
||||
|| cc.TextUtils.isUnicodeSpace(character)
|
||||
|| cc.TextUtils.isUnicodeCJK(character)) {
|
||||
break;
|
||||
}
|
||||
len++;
|
||||
|
||||
@@ -103,106 +103,6 @@
|
||||
return lineHeight;
|
||||
};
|
||||
|
||||
var label_wrapinspection = true;
|
||||
|
||||
//Support: English French German
|
||||
//Other as Oriental Language
|
||||
var label_wordRex = /([a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]+|\S)/;
|
||||
var label_symbolRex = /^[!,.:;}\]%\?>、‘“》?。,!]/;
|
||||
var label_lastWordRex = /([a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]+|\S)$/;
|
||||
var label_lastEnglish = /[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]+$/;
|
||||
var label_firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
|
||||
|
||||
//Note: Here the maxWidth is the label's content width.
|
||||
proto._fragmentText = function (strArr, maxWidth, ctx) {
|
||||
//check the first character
|
||||
maxWidth -= 2 * this._getMargin();
|
||||
var wrappedWords = [];
|
||||
//fast return if strArr is empty
|
||||
if(strArr.length === 0 || maxWidth < 0) {
|
||||
wrappedWords.push('');
|
||||
return wrappedWords;
|
||||
}
|
||||
|
||||
var text = strArr;
|
||||
var allWidth = ctx.measureText(text).width;
|
||||
while (allWidth > maxWidth && text.length > 1) {
|
||||
|
||||
var fuzzyLen = text.length * ( maxWidth / allWidth ) | 0;
|
||||
var tmpText = text.substr(fuzzyLen);
|
||||
var width = allWidth - ctx.measureText(tmpText).width;
|
||||
var sLine = tmpText;
|
||||
var pushNum = 0;
|
||||
|
||||
//Increased while cycle maximum ceiling. default 100 time
|
||||
var checkWhile = 0;
|
||||
var checkCount = 10;
|
||||
|
||||
//Exceeded the size
|
||||
while (width > maxWidth && checkWhile++ < checkCount) {
|
||||
fuzzyLen *= maxWidth / width;
|
||||
fuzzyLen = fuzzyLen | 0;
|
||||
tmpText = text.substr(fuzzyLen);
|
||||
width = allWidth - ctx.measureText(tmpText).width;
|
||||
}
|
||||
|
||||
checkWhile = 0;
|
||||
|
||||
//Find the truncation point
|
||||
while (width < maxWidth && checkWhile++ < checkCount) {
|
||||
if (tmpText) {
|
||||
var exec = label_wordRex.exec(tmpText);
|
||||
pushNum = exec ? exec[0].length : 1;
|
||||
sLine = tmpText;
|
||||
}
|
||||
|
||||
fuzzyLen = fuzzyLen + pushNum;
|
||||
tmpText = text.substr(fuzzyLen);
|
||||
width = allWidth - ctx.measureText(tmpText).width;
|
||||
}
|
||||
|
||||
fuzzyLen -= pushNum;
|
||||
if (fuzzyLen === 0) {
|
||||
fuzzyLen = 1;
|
||||
sLine = sLine.substr(1);
|
||||
}
|
||||
|
||||
var sText = text.substr(0, fuzzyLen), result;
|
||||
|
||||
//symbol in the first
|
||||
if (label_wrapinspection) {
|
||||
if (label_symbolRex.test(sLine || tmpText)) {
|
||||
result = label_lastWordRex.exec(sText);
|
||||
fuzzyLen -= result ? result[0].length : 0;
|
||||
if (fuzzyLen === 0) fuzzyLen = 1;
|
||||
|
||||
sLine = text.substr(fuzzyLen);
|
||||
sText = text.substr(0, fuzzyLen);
|
||||
}
|
||||
}
|
||||
|
||||
//To judge whether a English words are truncated
|
||||
if (label_firsrEnglish.test(sLine)) {
|
||||
result = label_lastEnglish.exec(sText);
|
||||
if (result && sText !== result[0]) {
|
||||
fuzzyLen -= result[0].length;
|
||||
sLine = text.substr(fuzzyLen);
|
||||
sText = text.substr(0, fuzzyLen);
|
||||
}
|
||||
}
|
||||
if (sText.trim().length > 0) {
|
||||
wrappedWords.push(sText);
|
||||
}
|
||||
text = sLine || tmpText;
|
||||
allWidth = ctx.measureText(text).width;
|
||||
}
|
||||
if (text.length > 0) {
|
||||
wrappedWords.push(text);
|
||||
}
|
||||
|
||||
return wrappedWords;
|
||||
};
|
||||
|
||||
proto._constructFontDesc = function () {
|
||||
var node = this._node;
|
||||
var fontDesc = node._fontSize.toString() + 'px ';
|
||||
@@ -215,6 +115,12 @@
|
||||
return fontDesc;
|
||||
};
|
||||
|
||||
proto._measureText = function (ctx) {
|
||||
return function(string) {
|
||||
return ctx.measureText(string).width;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
proto._calculateLabelFont = function() {
|
||||
var node = this._node;
|
||||
@@ -267,9 +173,11 @@
|
||||
totalHeight = 0;
|
||||
for (i = 0; i < paragraphedStrings.length; ++i) {
|
||||
var j = 0;
|
||||
textFragment = this._fragmentText(paragraphedStrings[i],
|
||||
canvasWidthNoMargin,
|
||||
this._labelContext);
|
||||
var allWidth = this._labelContext.measureText(paragraphedStrings[i]).width;
|
||||
textFragment = cc.TextUtils.fragmentText(paragraphedStrings[i],
|
||||
allWidth,
|
||||
canvasWidthNoMargin,
|
||||
this._measureText(this._labelContext));
|
||||
while(j < textFragment.length) {
|
||||
var measureWidth = this._labelContext.measureText(textFragment[j]).width;
|
||||
maxLength = measureWidth;
|
||||
@@ -342,10 +250,13 @@
|
||||
var i;
|
||||
if (node._isWrapText) {
|
||||
this._splitedStrings = [];
|
||||
var canvasWidthNoMargin = this._canvasSize.width - 2 * this._getMargin();
|
||||
for (i = 0; i < paragraphedStrings.length; ++i) {
|
||||
var textFragment = this._fragmentText(paragraphedStrings[i],
|
||||
this._canvasSize.width,
|
||||
this._labelContext);
|
||||
var allWidth = this._labelContext.measureText(paragraphedStrings[i]).width;
|
||||
var textFragment = cc.TextUtils.fragmentText(paragraphedStrings[i],
|
||||
allWidth,
|
||||
canvasWidthNoMargin,
|
||||
this._measureText(this._labelContext));
|
||||
this._splitedStrings = this._splitedStrings.concat(textFragment);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Chukong Aipu reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var TextUtils = {
|
||||
label_wordRex : /([a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûа-яА-ЯЁё]+|\S)/,
|
||||
label_symbolRex : /^[!,.:;}\]%\?>、‘“》?。,!]/,
|
||||
label_lastWordRex : /([a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûа-яА-ЯЁё]+|\S)$/,
|
||||
label_lastEnglish : /[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûа-яА-ЯЁё]+$/,
|
||||
label_firsrEnglish : /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûа-яА-ЯЁё]/,
|
||||
label_wrapinspection : true,
|
||||
|
||||
isUnicodeCJK: function(ch) {
|
||||
var __CHINESE_REG = /^[\u4E00-\u9FFF\u3400-\u4DFF]+$/;
|
||||
var __JAPANESE_REG = /[\u3000-\u303F]|[\u3040-\u309F]|[\u30A0-\u30FF]|[\uFF00-\uFFEF]|[\u4E00-\u9FAF]|[\u2605-\u2606]|[\u2190-\u2195]|\u203B/g;
|
||||
var __KOREAN_REG = /^[\u1100-\u11FF]|[\u3130-\u318F]|[\uA960-\uA97F]|[\uAC00-\uD7AF]|[\uD7B0-\uD7FF]+$/;
|
||||
return __CHINESE_REG.test(ch) || __JAPANESE_REG.test(ch) || __KOREAN_REG.test(ch);
|
||||
},
|
||||
|
||||
//Checking whether the character is a whitespace
|
||||
isUnicodeSpace: function(ch) {
|
||||
ch = ch.charCodeAt(0);
|
||||
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);
|
||||
},
|
||||
|
||||
fragmentText: function (stringToken, allWidth, maxWidth, measureText) {
|
||||
//check the first character
|
||||
var wrappedWords = [];
|
||||
//fast return if strArr is empty
|
||||
if(stringToken.length === 0 || maxWidth < 0) {
|
||||
wrappedWords.push('');
|
||||
return wrappedWords;
|
||||
}
|
||||
|
||||
var text = stringToken;
|
||||
while (allWidth > maxWidth && text.length > 1) {
|
||||
|
||||
var fuzzyLen = text.length * ( maxWidth / allWidth ) | 0;
|
||||
var tmpText = text.substr(fuzzyLen);
|
||||
var width = allWidth - measureText(tmpText);
|
||||
var sLine = tmpText;
|
||||
var pushNum = 0;
|
||||
|
||||
var checkWhile = 0;
|
||||
var checkCount = 10;
|
||||
|
||||
//Exceeded the size
|
||||
while (width > maxWidth && checkWhile++ < checkCount) {
|
||||
fuzzyLen *= maxWidth / width;
|
||||
fuzzyLen = fuzzyLen | 0;
|
||||
tmpText = text.substr(fuzzyLen);
|
||||
width = allWidth - measureText(tmpText);
|
||||
}
|
||||
|
||||
checkWhile = 0;
|
||||
|
||||
//Find the truncation point
|
||||
while (width < maxWidth && checkWhile++ < checkCount) {
|
||||
if (tmpText) {
|
||||
var exec = this.label_wordRex.exec(tmpText);
|
||||
pushNum = exec ? exec[0].length : 1;
|
||||
sLine = tmpText;
|
||||
}
|
||||
|
||||
fuzzyLen = fuzzyLen + pushNum;
|
||||
tmpText = text.substr(fuzzyLen);
|
||||
width = allWidth - measureText(tmpText);
|
||||
}
|
||||
|
||||
fuzzyLen -= pushNum;
|
||||
if (fuzzyLen === 0) {
|
||||
fuzzyLen = 1;
|
||||
sLine = sLine.substr(1);
|
||||
}
|
||||
|
||||
var sText = text.substr(0, fuzzyLen), result;
|
||||
|
||||
//symbol in the first
|
||||
if (this.label_wrapinspection) {
|
||||
if (this.label_symbolRex.test(sLine || tmpText)) {
|
||||
result = this.label_lastWordRex.exec(sText);
|
||||
fuzzyLen -= result ? result[0].length : 0;
|
||||
if (fuzzyLen === 0) fuzzyLen = 1;
|
||||
|
||||
sLine = text.substr(fuzzyLen);
|
||||
sText = text.substr(0, fuzzyLen);
|
||||
}
|
||||
}
|
||||
|
||||
//To judge whether a English words are truncated
|
||||
if (this.label_firsrEnglish.test(sLine)) {
|
||||
result = this.label_lastEnglish.exec(sText);
|
||||
if (result && sText !== result[0]) {
|
||||
fuzzyLen -= result[0].length;
|
||||
sLine = text.substr(fuzzyLen);
|
||||
sText = text.substr(0, fuzzyLen);
|
||||
}
|
||||
}
|
||||
if (sText.trim().length > 0) {
|
||||
wrappedWords.push(sText);
|
||||
}
|
||||
text = sLine || tmpText;
|
||||
allWidth = measureText(text);
|
||||
}
|
||||
if (text.length > 0) {
|
||||
wrappedWords.push(text);
|
||||
}
|
||||
|
||||
return wrappedWords;
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
cc.TextUtils = module.exports = TextUtils;
|
||||
@@ -70,6 +70,7 @@ var modules = {
|
||||
],
|
||||
|
||||
'Label': [
|
||||
'./cocos2d/core/label/CCTextUtils.js',
|
||||
'./cocos2d/core/label/CCSGLabel.js',
|
||||
'./cocos2d/core/label/CCSGLabelCanvasRenderCmd.js',
|
||||
'./cocos2d/core/label/CCSGLabelWebGLRenderCmd.js'
|
||||
@@ -207,4 +208,4 @@ exports.buildFileWithoutModular = function ( outDir, done ) {
|
||||
}, function () {
|
||||
done();
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
@@ -141,6 +141,7 @@
|
||||
{
|
||||
"name": "RichText",
|
||||
"entries": [
|
||||
"./cocos2d/core/label/CCHtmlTextParser.js",
|
||||
"./cocos2d/core/components/CCRichText.js"
|
||||
]
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user