From 66eb9c6db9427240afd543593bc730ea898ef8db Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 21 Sep 2016 16:10:37 +0800 Subject: [PATCH 1/8] make html text parser modular --- cocos2d/core/components/CCRichText.js | 2 ++ cocos2d/core/index.js | 1 - modules.json | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/components/CCRichText.js b/cocos2d/core/components/CCRichText.js index abf4589c23..bf05ab9800 100644 --- a/cocos2d/core/components/CCRichText.js +++ b/cocos2d/core/components/CCRichText.js @@ -22,6 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ +require('../label/CCHtmlTextParser.js'); + var HorizontalAlign = cc.TextAlignment; var VerticalAlign = cc.VerticalTextAlignment; diff --git a/cocos2d/core/index.js b/cocos2d/core/index.js index fdf6acf4c7..084e855a61 100644 --- a/cocos2d/core/index.js +++ b/cocos2d/core/index.js @@ -34,7 +34,6 @@ if (!CC_EDITOR || !Editor.isMainProcess) { require('./CCNode'); require('./CCScene'); - require('./label/CCHtmlTextParser.js'); require('./components'); require('./graphics'); require('./collider'); diff --git a/modules.json b/modules.json index 346f7f7fd9..4b413e89b9 100644 --- a/modules.json +++ b/modules.json @@ -141,6 +141,7 @@ { "name": "RichText", "entries": [ + "./cocos2d/core/label/CCHtmlTextParser.js", "./cocos2d/core/components/CCRichText.js" ] }, From bb8a941e4f22466c577c922f229a80c84458d1bc Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 21 Sep 2016 18:05:41 +0800 Subject: [PATCH 2/8] move richtext common logic to CCTextUtils --- cocos2d/core/components/CCRichText.js | 112 +-------------------- cocos2d/core/label/CCTextUtils.js | 135 ++++++++++++++++++++++++++ gulp/tasks/modular.js | 3 +- 3 files changed, 142 insertions(+), 108 deletions(-) create mode 100644 cocos2d/core/label/CCTextUtils.js diff --git a/cocos2d/core/components/CCRichText.js b/cocos2d/core/components/CCRichText.js index bf05ab9800..ad168a709b 100644 --- a/cocos2d/core/components/CCRichText.js +++ b/cocos2d/core/components/CCRichText.js @@ -23,16 +23,11 @@ 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 @@ -246,92 +241,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) { @@ -405,7 +314,8 @@ var RichText = cc.Class({ } } if(fragmentWidth > this.maxWidth) { - var fragments = this._fragmentText(labelString, fragmentWidth, styleIndex); + var fragments = cc.TextUtils.fragmentText(labelString, fragmentWidth, + this.maxWidth, styleIndex, this._measureText.bind(this)); for(var k = 0; k < fragments.length; ++k) { var splitString = fragments[k]; @@ -555,29 +465,17 @@ 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._isCJK_unicode(character) || cc.TextUtils._isspace_unicode(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._isspace_unicode(character) || cc.TextUtils._isCJK_unicode(character)) { break; } len++; diff --git a/cocos2d/core/label/CCTextUtils.js b/cocos2d/core/label/CCTextUtils.js new file mode 100644 index 0000000000..73222bd5b0 --- /dev/null +++ b/cocos2d/core/label/CCTextUtils.js @@ -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, + + _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); + }, + + fragmentText: function (stringToken, allWidth, maxWidth, styleIndex, 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, styleIndex); + 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, styleIndex); + } + + 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, styleIndex); + } + + 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, styleIndex); + } + if (text.length > 0) { + wrappedWords.push(text); + } + + return wrappedWords; + }, + +}; + +cc.TextUtils = module.exports = TextUtils; diff --git a/gulp/tasks/modular.js b/gulp/tasks/modular.js index f7f511b1bf..f3eb656b11 100644 --- a/gulp/tasks/modular.js +++ b/gulp/tasks/modular.js @@ -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(); }); -}; \ No newline at end of file +}; From 31eeb4e61c1dc59a18bfad4257b4a15495f494b4 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 21 Sep 2016 18:40:19 +0800 Subject: [PATCH 3/8] make fragment text more generic --- cocos2d/core/components/CCRichText.js | 39 ++++++++++++++++----------- cocos2d/core/label/CCTextUtils.js | 10 +++---- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/cocos2d/core/components/CCRichText.js b/cocos2d/core/components/CCRichText.js index ad168a709b..a2406d671c 100644 --- a/cocos2d/core/components/CCRichText.js +++ b/cocos2d/core/components/CCRichText.js @@ -197,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; }, @@ -293,7 +301,7 @@ var RichText = cc.Class({ while (this._lineOffsetX <= this.maxWidth) { 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; @@ -306,7 +314,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; @@ -314,8 +322,7 @@ var RichText = cc.Class({ } } if(fragmentWidth > this.maxWidth) { - var fragments = cc.TextUtils.fragmentText(labelString, fragmentWidth, - this.maxWidth, styleIndex, this._measureText.bind(this)); + var fragments = cc.TextUtils.fragmentText(labelString, fragmentWidth, this.maxWidth, this._measureText(styleIndex)); for(var k = 0; k < fragments.length; ++k) { var splitString = fragments[k]; @@ -429,7 +436,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) { diff --git a/cocos2d/core/label/CCTextUtils.js b/cocos2d/core/label/CCTextUtils.js index 73222bd5b0..2d13a3fabe 100644 --- a/cocos2d/core/label/CCTextUtils.js +++ b/cocos2d/core/label/CCTextUtils.js @@ -44,7 +44,7 @@ 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); }, - fragmentText: function (stringToken, allWidth, maxWidth, styleIndex, measureText) { + fragmentText: function (stringToken, allWidth, maxWidth, measureText) { //check the first character var wrappedWords = []; //fast return if strArr is empty @@ -58,7 +58,7 @@ var TextUtils = { var fuzzyLen = text.length * ( maxWidth / allWidth ) | 0; var tmpText = text.substr(fuzzyLen); - var width = allWidth - measureText(tmpText, styleIndex); + var width = allWidth - measureText(tmpText); var sLine = tmpText; var pushNum = 0; @@ -70,7 +70,7 @@ var TextUtils = { fuzzyLen *= maxWidth / width; fuzzyLen = fuzzyLen | 0; tmpText = text.substr(fuzzyLen); - width = allWidth - measureText(tmpText, styleIndex); + width = allWidth - measureText(tmpText); } checkWhile = 0; @@ -85,7 +85,7 @@ var TextUtils = { fuzzyLen = fuzzyLen + pushNum; tmpText = text.substr(fuzzyLen); - width = allWidth - measureText(tmpText, styleIndex); + width = allWidth - measureText(tmpText); } fuzzyLen -= pushNum; @@ -121,7 +121,7 @@ var TextUtils = { wrappedWords.push(sText); } text = sLine || tmpText; - allWidth = measureText(text, styleIndex); + allWidth = measureText(text); } if (text.length > 0) { wrappedWords.push(text); From 23fbed9cc99e22a42cf99bc159f12809ef4d87ca Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 22 Sep 2016 10:33:07 +0800 Subject: [PATCH 4/8] replace sglabel fragment text --- cocos2d/core/components/CCRichText.js | 18 ++- .../core/label/CCSGLabelCanvasRenderCmd.js | 123 +++--------------- 2 files changed, 30 insertions(+), 111 deletions(-) diff --git a/cocos2d/core/components/CCRichText.js b/cocos2d/core/components/CCRichText.js index a2406d671c..59e76c6cce 100644 --- a/cocos2d/core/components/CCRichText.js +++ b/cocos2d/core/components/CCRichText.js @@ -299,7 +299,9 @@ 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(styleIndex, checkString); @@ -322,7 +324,10 @@ var RichText = cc.Class({ } } if(fragmentWidth > this.maxWidth) { - var fragments = cc.TextUtils.fragmentText(labelString, fragmentWidth, this.maxWidth, this._measureText(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]; @@ -426,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(); @@ -475,14 +481,16 @@ var RichText = cc.Class({ _getFirstWordLen: function(text, startIndex, textLen) { var character = text.charAt(startIndex); - if (cc.TextUtils._isCJK_unicode(character) || cc.TextUtils._isspace_unicode(character)) { + if (cc.TextUtils._isCJK_unicode(character) + || cc.TextUtils._isspace_unicode(character)) { return 1; } var len = 1; for (var index = startIndex + 1; index < textLen; ++index) { character = text.charAt(index); - if (cc.TextUtils._isspace_unicode(character) || cc.TextUtils._isCJK_unicode(character)) { + if (cc.TextUtils._isspace_unicode(character) + || cc.TextUtils._isCJK_unicode(character)) { break; } len++; diff --git a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js index 559a54dfde..4ec428ddbe 100644 --- a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js @@ -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); } } From 20038b8fb9ea23fad861172b6318f63335e36927 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 22 Sep 2016 10:41:45 +0800 Subject: [PATCH 5/8] unify CJK and space unicode in engine --- cocos2d/core/components/CCRichText.js | 8 ++++---- cocos2d/core/label/CCSGLabel.js | 21 ++++++--------------- cocos2d/core/label/CCTextUtils.js | 4 ++-- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/cocos2d/core/components/CCRichText.js b/cocos2d/core/components/CCRichText.js index 59e76c6cce..32beccaf5c 100644 --- a/cocos2d/core/components/CCRichText.js +++ b/cocos2d/core/components/CCRichText.js @@ -481,16 +481,16 @@ var RichText = cc.Class({ _getFirstWordLen: function(text, startIndex, textLen) { var character = text.charAt(startIndex); - if (cc.TextUtils._isCJK_unicode(character) - || cc.TextUtils._isspace_unicode(character)) { + if (cc.TextUtils.isCJK_unicode(character) + || cc.TextUtils.isspace_unicode(character)) { return 1; } var len = 1; for (var index = startIndex + 1; index < textLen; ++index) { character = text.charAt(index); - if (cc.TextUtils._isspace_unicode(character) - || cc.TextUtils._isCJK_unicode(character)) { + if (cc.TextUtils.isspace_unicode(character) + || cc.TextUtils.isCJK_unicode(character)) { break; } len++; diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index c2b5070e15..196ae7014e 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -1042,22 +1042,11 @@ cc.BMFontHelper = { 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.isCJK_unicode(character) + || character === "\n" + || cc.TextUtils.isspace_unicode(character)) { return 1; } @@ -1081,7 +1070,9 @@ cc.BMFontHelper = { } } nextLetterX += letterDef._xAdvance * this._bmfontScale + this._additionalKerning; - if (character === "\n" || this._isspace_unicode(character) || this._isCJK_unicode(character)) { + if (character === "\n" + || cc.TextUtils.isspace_unicode(character) + || cc.TextUtils.isCJK_unicode(character)) { break; } len++; diff --git a/cocos2d/core/label/CCTextUtils.js b/cocos2d/core/label/CCTextUtils.js index 2d13a3fabe..257196545e 100644 --- a/cocos2d/core/label/CCTextUtils.js +++ b/cocos2d/core/label/CCTextUtils.js @@ -31,7 +31,7 @@ var TextUtils = { label_firsrEnglish : /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/, label_wrapinspection : true, - _isCJK_unicode: function(ch) { + 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]+$/; @@ -39,7 +39,7 @@ var TextUtils = { }, //Checking whether the character is a whitespace - _isspace_unicode: function(ch) { + 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); }, From def203cddc298fa1b089e0af13914bd6a5c5927b Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 22 Sep 2016 10:56:28 +0800 Subject: [PATCH 6/8] fix bmfont --- cocos2d/core/label/CCSGLabel.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index 196ae7014e..a1cca2d965 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -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.isspace_unicode(character)) { this._linesWidth.push(letterRight); letterRight = 0; lineIndex++; @@ -1038,7 +1036,7 @@ cc.BMFontHelper = { } }, - _getFirstCharLen: function(text, startIndex, textLen) { + _getFirstCharLen: function() { return 1; }, @@ -1064,7 +1062,9 @@ 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.isspace_unicode(character) + && this._maxLineWidth > 0) { if(len >= 2) { return len - 1; } From b7b3f440e5725037e9a0ff1c13cffe98ac066372 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 22 Sep 2016 11:46:37 +0800 Subject: [PATCH 7/8] add cyrillic language support --- cocos2d/core/label/CCTextUtils.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cocos2d/core/label/CCTextUtils.js b/cocos2d/core/label/CCTextUtils.js index 257196545e..547668ab55 100644 --- a/cocos2d/core/label/CCTextUtils.js +++ b/cocos2d/core/label/CCTextUtils.js @@ -24,11 +24,11 @@ ****************************************************************************/ var TextUtils = { - label_wordRex : /([a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]+|\S)/, + 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_lastWordRex : /([a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûа-яА-ЯЁё]+|\S)$/, + label_lastEnglish : /[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûа-яА-ЯЁё]+$/, + label_firsrEnglish : /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûа-яА-ЯЁё]/, label_wrapinspection : true, isCJK_unicode: function(ch) { From c11e29f2127e6560554cbd011572a2f8f91423ba Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 22 Sep 2016 13:38:54 +0800 Subject: [PATCH 8/8] renaming functions --- cocos2d/core/components/CCRichText.js | 8 ++++---- cocos2d/core/label/CCSGLabel.js | 12 ++++++------ cocos2d/core/label/CCTextUtils.js | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cocos2d/core/components/CCRichText.js b/cocos2d/core/components/CCRichText.js index 32beccaf5c..21d0663b7f 100644 --- a/cocos2d/core/components/CCRichText.js +++ b/cocos2d/core/components/CCRichText.js @@ -481,16 +481,16 @@ var RichText = cc.Class({ _getFirstWordLen: function(text, startIndex, textLen) { var character = text.charAt(startIndex); - if (cc.TextUtils.isCJK_unicode(character) - || cc.TextUtils.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 (cc.TextUtils.isspace_unicode(character) - || cc.TextUtils.isCJK_unicode(character)) { + if (cc.TextUtils.isUnicodeSpace(character) + || cc.TextUtils.isUnicodeCJK(character)) { break; } len++; diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index a1cca2d965..247067a0a7 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -811,7 +811,7 @@ cc.BMFontHelper = { && this._maxLineWidth > 0 && nextTokenX > 0 && letterX + letterDef._width * this._bmfontScale > this._maxLineWidth - && !cc.TextUtils.isspace_unicode(character)) { + && !cc.TextUtils.isUnicodeSpace(character)) { this._linesWidth.push(letterRight); letterRight = 0; lineIndex++; @@ -1042,9 +1042,9 @@ cc.BMFontHelper = { _getFirstWordLen: function(text, startIndex, textLen) { var character = text.charAt(startIndex); - if (cc.TextUtils.isCJK_unicode(character) + if (cc.TextUtils.isUnicodeCJK(character) || character === "\n" - || cc.TextUtils.isspace_unicode(character)) { + || cc.TextUtils.isUnicodeSpace(character)) { return 1; } @@ -1063,7 +1063,7 @@ cc.BMFontHelper = { letterX = nextLetterX + letterDef._offsetX * this._bmfontScale; if(letterX + letterDef._width * this._bmfontScale > this._maxLineWidth - && !cc.TextUtils.isspace_unicode(character) + && !cc.TextUtils.isUnicodeSpace(character) && this._maxLineWidth > 0) { if(len >= 2) { return len - 1; @@ -1071,8 +1071,8 @@ cc.BMFontHelper = { } nextLetterX += letterDef._xAdvance * this._bmfontScale + this._additionalKerning; if (character === "\n" - || cc.TextUtils.isspace_unicode(character) - || cc.TextUtils.isCJK_unicode(character)) { + || cc.TextUtils.isUnicodeSpace(character) + || cc.TextUtils.isUnicodeCJK(character)) { break; } len++; diff --git a/cocos2d/core/label/CCTextUtils.js b/cocos2d/core/label/CCTextUtils.js index 547668ab55..18aa8da906 100644 --- a/cocos2d/core/label/CCTextUtils.js +++ b/cocos2d/core/label/CCTextUtils.js @@ -31,7 +31,7 @@ var TextUtils = { label_firsrEnglish : /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûа-яА-ЯЁё]/, label_wrapinspection : true, - isCJK_unicode: function(ch) { + 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]+$/; @@ -39,7 +39,7 @@ var TextUtils = { }, //Checking whether the character is a whitespace - isspace_unicode: function(ch) { + 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); },