From ae8057b933650816b7ef4b6384542a259e82e6fb Mon Sep 17 00:00:00 2001 From: Pisces000221 <1786762946@qq.com> Date: Sat, 8 Feb 2014 14:07:18 +0800 Subject: [PATCH 01/10] Added ischinese_unicode function --- cocos/2d/ccUTF8.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cocos/2d/ccUTF8.h b/cocos/2d/ccUTF8.h index 352a4db41a..93f5ad06b4 100644 --- a/cocos/2d/ccUTF8.h +++ b/cocos/2d/ccUTF8.h @@ -41,6 +41,16 @@ CC_DLL void cc_utf8_trim_ws(std::vector* str); * */ CC_DLL bool isspace_unicode(unsigned short ch); +/** + * Whether the character is a Chinese character. + * + * @param ch the unicode character + * @returns whether the character is a Chinese character. + * + * @see http://www.searchtb.com/2012/04/chinese_encode.html + * */ +CC_DLL bool ischinese_unicode(unsigned short ch); + /** * Returns the length of the string in characters. * From 7aebdd89a5804786c9d10876282e8f4e428e8b46 Mon Sep 17 00:00:00 2001 From: Pisces000221 <1786762946@qq.com> Date: Sat, 8 Feb 2014 14:15:42 +0800 Subject: [PATCH 02/10] Added ischinese_unicode function --- cocos/2d/ccUTF8.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cocos/2d/ccUTF8.cpp b/cocos/2d/ccUTF8.cpp index 7ebf8a0e31..ba19f75a2c 100644 --- a/cocos/2d/ccUTF8.cpp +++ b/cocos/2d/ccUTF8.cpp @@ -171,6 +171,11 @@ bool isspace_unicode(unsigned short ch) || ch == 0x205F || ch == 0x3000; } +bool ischinese_unicode(unsigned short ch) +{ + return ch >= 0x4E00 && ch <= 0x9FBF; +} + void cc_utf8_trim_ws(std::vector* str) { int len = static_cast(str->size()); From b5e9a4a00a0bc8ba4daf21ef9a239dae7fb5be26 Mon Sep 17 00:00:00 2001 From: Pisces000221 <1786762946@qq.com> Date: Sat, 8 Feb 2014 14:52:10 +0800 Subject: [PATCH 03/10] Rename ischinese_unicode as iscjk_unicode --- cocos/2d/ccUTF8.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cocos/2d/ccUTF8.cpp b/cocos/2d/ccUTF8.cpp index ba19f75a2c..33543ba8b1 100644 --- a/cocos/2d/ccUTF8.cpp +++ b/cocos/2d/ccUTF8.cpp @@ -171,9 +171,16 @@ bool isspace_unicode(unsigned short ch) || ch == 0x205F || ch == 0x3000; } -bool ischinese_unicode(unsigned short ch) +bool iscjk_unicode(unsigned short ch) { - return ch >= 0x4E00 && ch <= 0x9FBF; + return (ch >= 0x4E00 && ch <= 0x9FBF) // CJK Unified Ideographs + || (ch >= 0x2E80 && ch <= 0x2FDF) // CJK Radicals Supplement & Kangxi Radicals + || (ch >= 0x2FF0 && ch <= 0x30FF) // Ideographic Description Characters, CJK Symbols and Punctuation & Japanese + || (ch >= 0x3100 && ch <= 0x31BF) // Korean + || (ch >= 0xAC00 && ch <= 0xD7AF) // Hangul Syllables + || (ch >= 0xF900 && ch <= 0xFAFF) // CJK Compatibility Ideographs + || (ch >= 0xFE30 && ch <= 0xFE4F) // CJK Compatibility Forms + || (ch >= 0x31C0 && ch <= 0x4DFF); // Other exiensions } void cc_utf8_trim_ws(std::vector* str) From 7484286f25f69b0ffa6946696925c10b11b3c53a Mon Sep 17 00:00:00 2001 From: Pisces000221 <1786762946@qq.com> Date: Sat, 8 Feb 2014 14:52:59 +0800 Subject: [PATCH 04/10] Rename ischinese_unicode as iscjk_unicode --- cocos/2d/ccUTF8.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cocos/2d/ccUTF8.h b/cocos/2d/ccUTF8.h index 93f5ad06b4..e3e16da3c7 100644 --- a/cocos/2d/ccUTF8.h +++ b/cocos/2d/ccUTF8.h @@ -42,14 +42,15 @@ CC_DLL void cc_utf8_trim_ws(std::vector* str); CC_DLL bool isspace_unicode(unsigned short ch); /** - * Whether the character is a Chinese character. + * Whether the character is a Chinese/Japanese/Korean character. * * @param ch the unicode character * @returns whether the character is a Chinese character. * * @see http://www.searchtb.com/2012/04/chinese_encode.html + * @see http://tieba.baidu.com/p/748765987 * */ -CC_DLL bool ischinese_unicode(unsigned short ch); +CC_DLL bool iscjk_unicode(unsigned short ch); /** * Returns the length of the string in characters. From da2a58ccc99178c0bb0aa468b56e3b86a7767546 Mon Sep 17 00:00:00 2001 From: Pisces000221 <1786762946@qq.com> Date: Sat, 8 Feb 2014 15:05:36 +0800 Subject: [PATCH 05/10] Improved formatting when using both CJK and ASCII --- cocos/2d/CCLabelTextFormatter.cpp | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/cocos/2d/CCLabelTextFormatter.cpp b/cocos/2d/CCLabelTextFormatter.cpp index 0ed58fbce2..249b00f730 100644 --- a/cocos/2d/CCLabelTextFormatter.cpp +++ b/cocos/2d/CCLabelTextFormatter.cpp @@ -107,26 +107,39 @@ bool LabelTextFormatter::multilineText(Label *theLabel) startOfLine = startOfWord; isStartOfLine = true; } - - // Whitespace. - if (isspace_unicode(character)) + + // 1) Whitespace. + // 2) This character is non-CJK, but the last character is CJK + if (isspace_unicode(character) || + !last_word.empty() && iscjk_unicode(last_word.back()) && !iscjk_unicode(character)) { - last_word.push_back(character); + // if current character is white space, put it into the current word + if (isspace_unicode(character)) last_word.push_back(character); multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end()); last_word.clear(); isStartOfWord = false; startOfWord = -1; + // put the CJK character in the last word + // and put the non-CJK(ASCII) character in the current word + if (!isspace_unicode(character)) last_word.push_back(character); continue; } + + // CJK characters. + if (iscjk_unicode(character)) + { + multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end()); + last_word.clear(); + isStartOfWord = false; + startOfWord = -1; + } float posRight = (info->position.x + info->contentSize.width) * scalsX; // Out of bounds. if (posRight - startOfLine > lineWidth) { - if (!breakLineWithoutSpace) + if (!breakLineWithoutSpace && !iscjk_unicode(character)) { - last_word.push_back(character); - int found = cc_utf8_find_last_not_char(multiline_string, ' '); if (found != -1) cc_utf8_trim_ws(&multiline_string); @@ -145,6 +158,7 @@ bool LabelTextFormatter::multilineText(Label *theLabel) cc_utf8_trim_ws(&last_word); last_word.push_back('\n'); + multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end()); last_word.clear(); isStartOfWord = false; @@ -349,4 +363,4 @@ bool LabelTextFormatter::createStringSprites(Label *theLabel) return true; } -NS_CC_END \ No newline at end of file +NS_CC_END From c9e4944321ec429c6b2834d323c238208ed12de4 Mon Sep 17 00:00:00 2001 From: Pisces000221 <1786762946@qq.com> Date: Sat, 8 Feb 2014 15:18:18 +0800 Subject: [PATCH 06/10] Fix one character disappears when breaking line --- cocos/2d/CCLabelTextFormatter.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cocos/2d/CCLabelTextFormatter.cpp b/cocos/2d/CCLabelTextFormatter.cpp index 249b00f730..308e0957ec 100644 --- a/cocos/2d/CCLabelTextFormatter.cpp +++ b/cocos/2d/CCLabelTextFormatter.cpp @@ -140,6 +140,8 @@ bool LabelTextFormatter::multilineText(Label *theLabel) { if (!breakLineWithoutSpace && !iscjk_unicode(character)) { + last_word.push_back(character); + int found = cc_utf8_find_last_not_char(multiline_string, ' '); if (found != -1) cc_utf8_trim_ws(&multiline_string); From 1170943cf89d5dfcd32f3c64dd08b6d909a0515f Mon Sep 17 00:00:00 2001 From: Pisces000221 <1786762946@qq.com> Date: Sat, 8 Feb 2014 17:14:38 +0800 Subject: [PATCH 07/10] Add LabelTTFCJKWrappingTest --- tests/test-cpp/Classes/LabelTest/LabelTestNew.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test-cpp/Classes/LabelTest/LabelTestNew.h b/tests/test-cpp/Classes/LabelTest/LabelTestNew.h index 3d5e99f368..226de1773c 100644 --- a/tests/test-cpp/Classes/LabelTest/LabelTestNew.h +++ b/tests/test-cpp/Classes/LabelTest/LabelTestNew.h @@ -287,6 +287,19 @@ private: }; +class LabelTTFCJKWrappingTest : public AtlasDemoNew +{ +public: + CREATE_FUNC(LabelTTFCJKWrappingTest); + + LabelTTFCJKWrappingTest(); + virtual std::string title() const override; + virtual std::string subtitle() const override; + +private: +}; + + class LabelTTFFontsTestNew : public AtlasDemoNew { public: From 49163bb774a88c6d4e610d7bffe8f457a43cb618 Mon Sep 17 00:00:00 2001 From: Pisces000221 <1786762946@qq.com> Date: Sat, 8 Feb 2014 17:15:57 +0800 Subject: [PATCH 08/10] Add LabelTTFCJKWrappingTest --- .../Classes/LabelTest/LabelTestNew.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/test-cpp/Classes/LabelTest/LabelTestNew.cpp b/tests/test-cpp/Classes/LabelTest/LabelTestNew.cpp index e0ee9e3a5a..544b76d46a 100644 --- a/tests/test-cpp/Classes/LabelTest/LabelTestNew.cpp +++ b/tests/test-cpp/Classes/LabelTest/LabelTestNew.cpp @@ -63,6 +63,7 @@ static std::function createFunctions[] = CL(LabelTTFColor), CL(LabelTTFFontsTestNew), CL(LabelTTFDynamicAlignment), + CL(LabelTTFCJKWrappingTest), CL(LabelTTFUnicodeNew), CL(LabelBMFontTestNew), CL(LabelTTFDistanceField), @@ -1071,6 +1072,56 @@ std::string LabelTTFDynamicAlignment::subtitle() const return "Uses the new Label with TTF. Testing alignment"; } +// +// NewLabelTTF Chinese/Japanese/Korean wrapping test +// +LabelTTFCJKWrappingTest::LabelTTFCJKWrappingTest() +{ + auto size = Director::getInstance()->getWinSize(); + + auto drawNode = DrawNode::create(); + drawNode->setAnchorPoint(Point(0, 0)); + this->addChild(drawNode); + drawNode->drawSegment( + Point(size.width * 0.1, size.height * 0.8), + Point(size.width * 0.1, 0), 1, Color4F(1, 0, 0, 1)); + drawNode->drawSegment( + Point(size.width * 0.85, size.height * 0.8), + Point(size.width * 0.85, 0), 1, Color4F(1, 0, 0, 1)); + + TTFConfig ttfConfig("fonts/wt021.ttf", 50, GlyphCollection::DYNAMIC); + auto label1 = Label::createWithTTF(ttfConfig, + "你好,Cocos2d-x v3的New Label。", TextHAlignment::LEFT, size.width * 0.75); + label1->setColor(Color3B(128, 255, 255)); + label1->setPosition(Point(size.width * 0.1, size.height * 0.6)); + label1->setAnchorPoint(Point(0, 0.5)); + this->addChild(label1); + + auto label2 = Label::createWithTTF(ttfConfig, + "早上好,Cocos2d-x v3的New Label。", TextHAlignment::LEFT, size.width * 0.75); + label2->setColor(Color3B(255, 128, 255)); + label2->setPosition(Point(size.width * 0.1, size.height * 0.4)); + label2->setAnchorPoint(Point(0, 0.5)); + this->addChild(label2); + auto label3 = Label::createWithTTF(ttfConfig, + "美好的一天啊美好的一天啊美好的一天啊", TextHAlignment::LEFT, size.width * 0.75); + label3->setColor(Color3B(255, 255, 128)); + label3->setPosition(Point(size.width * 0.1, size.height * 0.2)); + label3->setAnchorPoint(Point(0, 0.5)); + this->addChild(label3); +} + +std::string LabelTTFCJKWrappingTest::title() const +{ + return "New Label + .TTF"; +} + +std::string LabelTTFCJKWrappingTest::subtitle() const +{ + return "New Label with CJK + ASCII characters\n" + "Characters should stay in the correct position"; +} + // // NewLabelTTF unicode test // From cef84f05af17e08039574b3700c6529465495d06 Mon Sep 17 00:00:00 2001 From: Pisces000221 <1786762946@qq.com> Date: Sat, 8 Feb 2014 17:16:59 +0800 Subject: [PATCH 09/10] Add LabelTTFCJKWrappingTest --- .../luaScript/LabelTestNew/LabelTestNew.lua | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tests/test-lua/Resources/luaScript/LabelTestNew/LabelTestNew.lua b/tests/test-lua/Resources/luaScript/LabelTestNew/LabelTestNew.lua index 0bcdadd979..f269aabd91 100644 --- a/tests/test-lua/Resources/luaScript/LabelTestNew/LabelTestNew.lua +++ b/tests/test-lua/Resources/luaScript/LabelTestNew/LabelTestNew.lua @@ -1008,6 +1008,64 @@ function LabelTTFDynamicAlignment.setAlignmentRight(pSender) end +-------------------------------------------------------- +----- LabelCJKASCIITest +-------------------------------------------------------- +local LabelTTFCJKWrappingTest = {} +function LabelTTFCJKWrappingTest.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("New Label + .TTF") + Helper.subtitleLabel:setString( + "New Label with CJK + ASCII characters\n" + .. "Characters should stay in the correct position") + + local size = cc.Director:getInstance():getVisibleSize() + local ttfConfig = {} + ttfConfig.fontFilePath = "fonts/wt021.ttf" + ttfConfig.fontSize = 50 + ttfConfig.glyphs = cc.GLYPHCOLLECTION_DYNAMIC + ttfConfig.customGlyphs = nil + ttfConfig.distanceFieldEnabled = true + + local drawNode = cc.DrawNode:create() + drawNode:setAnchorPoint(cc.p(0, 0)) + layer:addChild(drawNode) + drawNode:drawSegment( + cc.p(size.width * 0.1, size.height * 0.8), + cc.p(size.width * 0.1, 0), 1, cc.c4f(1, 0, 0, 1)) + drawNode:drawSegment( + cc.p(size.width * 0.85, size.height * 0.8), + cc.p(size.width * 0.85, 0), 1, cc.c4f(1, 0, 0, 1)) + + local label1 = cc.Label:createWithTTF( + ttfConfig, "你好,Cocos2d-x v3的New Label。", + cc.TEXT_ALIGNMENT_LEFT, size.width * 0.75) + label1:setColor(cc.c3b(128, 255, 255)) + label1:setPosition(cc.p(size.width * 0.1, size.height * 0.6)) + label1:setAnchorPoint(cc.p(0, 0.5)) + layer:addChild(label1) + + local label2 = cc.Label:createWithTTF( + ttfConfig, "早上好,Cocos2d-x v3的New Label。", + cc.TEXT_ALIGNMENT_LEFT, size.width * 0.75) + label2:setColor(cc.c3b(255, 128, 255)) + label2:setPosition(cc.p(size.width * 0.1, size.height * 0.4)) + label2:setAnchorPoint(cc.p(0, 0.5)) + layer:addChild(label2) + + local label3 = cc.Label:createWithTTF( + ttfConfig, "美好的一天啊美好的一天啊美好的一天啊", + cc.TEXT_ALIGNMENT_LEFT, size.width * 0.75) + label3:setColor(cc.c3b(255, 255, 128)) + label3:setPosition(cc.p(size.width * 0.1, size.height * 0.2)) + label3:setAnchorPoint(cc.p(0, 0.5)) + layer:addChild(label3) + + return layer +end + + -------------------------------------------------------- ----- LabelTTFFontsTestNew -------------------------------------------------------- @@ -1262,6 +1320,7 @@ function LabelTestNew() LabelFNTBounds.create, LabelTTFLongLineWrapping.create, LabelTTFDynamicAlignment.create, + LabelTTFCJKWrappingTest.create, LabelTTFFontsTestNew.create, LabelBMFontTestNew.create, LabelTTFDistanceField.create, From 03be45937617c1e418db1986602948ebe31cb4d4 Mon Sep 17 00:00:00 2001 From: Pisces000221 <1786762946@qq.com> Date: Sat, 8 Feb 2014 17:17:35 +0800 Subject: [PATCH 10/10] Update LabelTestNew.lua --- .../test-lua/Resources/luaScript/LabelTestNew/LabelTestNew.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-lua/Resources/luaScript/LabelTestNew/LabelTestNew.lua b/tests/test-lua/Resources/luaScript/LabelTestNew/LabelTestNew.lua index f269aabd91..72011b9237 100644 --- a/tests/test-lua/Resources/luaScript/LabelTestNew/LabelTestNew.lua +++ b/tests/test-lua/Resources/luaScript/LabelTestNew/LabelTestNew.lua @@ -1009,7 +1009,7 @@ end -------------------------------------------------------- ------ LabelCJKASCIITest +----- LabelTTFCJKWrappingTest -------------------------------------------------------- local LabelTTFCJKWrappingTest = {} function LabelTTFCJKWrappingTest.create()