From f2c6a48976cee36e5bd40ab11d362ed581e4d6d4 Mon Sep 17 00:00:00 2001 From: WenhaiLin Date: Tue, 20 Jan 2015 11:46:43 +0800 Subject: [PATCH 1/2] Fixed lose the precision of label's dimensions --- cocos/2d/CCLabel.cpp | 10 +++++----- cocos/2d/CCLabel.h | 22 +++++++++++----------- cocos/platform/mac/CCDevice-mac.mm | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index 27329913b0..e9bae0ca6a 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -245,10 +245,10 @@ Label::Label(FontAtlas *atlas /* = nullptr */, TextHAlignment hAlignment /* = Te , _commonLineHeight(0.0f) , _lineBreakWithoutSpaces(false) , _horizontalKernings(nullptr) -, _maxLineWidth(0) +, _maxLineWidth(0.0f) , _labelDimensions(Size::ZERO) -, _labelWidth(0) -, _labelHeight(0) +, _labelWidth(0.0f) +, _labelHeight(0.0f) , _hAlignment(hAlignment) , _vAlignment(vAlignment) , _currNumLines(-1) @@ -496,7 +496,7 @@ void Label::setAlignment(TextHAlignment hAlignment,TextVAlignment vAlignment) } } -void Label::setMaxLineWidth(unsigned int maxLineWidth) +void Label::setMaxLineWidth(float maxLineWidth) { if (_labelWidth == 0 && _maxLineWidth != maxLineWidth) { @@ -505,7 +505,7 @@ void Label::setMaxLineWidth(unsigned int maxLineWidth) } } -void Label::setDimensions(unsigned int width, unsigned int height) +void Label::setDimensions(float width, float height) { if (height != _labelHeight || width != _labelWidth) { diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index 02844fa5f4..e6d7c4715b 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -183,25 +183,25 @@ public: * The label's max line width be used for force line breaks if the set value not equal zero. * The label's width and max line width has not always to be equal. */ - void setMaxLineWidth(unsigned int maxLineWidth); - unsigned int getMaxLineWidth() { return _maxLineWidth;} + void setMaxLineWidth(float maxLineWidth); + float getMaxLineWidth() { return _maxLineWidth; } /** Sets the untransformed size of the label. * The label's width be used for text align if the set value not equal zero. * The label's max line width will be equal to the same value. */ - void setWidth(unsigned int width) { setDimensions(width,_labelHeight);} - unsigned int getWidth() const { return _labelWidth; } + void setWidth(float width) { setDimensions(width,_labelHeight);} + float getWidth() const { return _labelWidth; } /** Sets the untransformed size of the label. * The label's height be used for text align if the set value not equal zero. * The text will display of incomplete when the size of label not enough to support display all text. */ - void setHeight(unsigned int height){ setDimensions(_labelWidth,height);} - unsigned int getHeight() const { return _labelHeight;} + void setHeight(float height){ setDimensions(_labelWidth, height); } + float getHeight() const { return _labelHeight; } /** Sets the untransformed size of the label in a more efficient way. */ - void setDimensions(unsigned int width,unsigned int height); + void setDimensions(float width, float height); const Size& getDimensions() const{ return _labelDimensions;} /** update content immediately.*/ @@ -365,10 +365,10 @@ protected: bool _lineBreakWithoutSpaces; int * _horizontalKernings; - unsigned int _maxLineWidth; - Size _labelDimensions; - unsigned int _labelWidth; - unsigned int _labelHeight; + float _maxLineWidth; + Size _labelDimensions; + float _labelWidth; + float _labelHeight; TextHAlignment _hAlignment; TextVAlignment _vAlignment; diff --git a/cocos/platform/mac/CCDevice-mac.mm b/cocos/platform/mac/CCDevice-mac.mm index f1c01c0c3c..fa067c566a 100644 --- a/cocos/platform/mac/CCDevice-mac.mm +++ b/cocos/platform/mac/CCDevice-mac.mm @@ -132,9 +132,9 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch lastBreakLocation = i + insertCount; } textSize = [lineBreak sizeWithAttributes:tokenAttributesDict]; - if(info->height > 0 && textSize.height > info->height) + if(info->height > 0 && (int)textSize.height > info->height) break; - if (textSize.width > info->width) { + if ((int)textSize.width > info->width) { if(lastBreakLocation > 0) { [lineBreak insertString:@"\r" atIndex:lastBreakLocation]; lastBreakLocation = 0; From e5fc236ec40a1d83d6d22f8bed2bbf2453c58b1f Mon Sep 17 00:00:00 2001 From: WenhaiLin Date: Tue, 20 Jan 2015 12:01:51 +0800 Subject: [PATCH 2/2] Add test case for issue#10089 --- .../Classes/LabelTest/LabelTestNew.cpp | 30 ++++++++++++++++++- .../Classes/LabelTest/LabelTestNew.h | 11 +++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/cpp-tests/Classes/LabelTest/LabelTestNew.cpp b/tests/cpp-tests/Classes/LabelTest/LabelTestNew.cpp index 8617d9e93b..b2f808627c 100644 --- a/tests/cpp-tests/Classes/LabelTest/LabelTestNew.cpp +++ b/tests/cpp-tests/Classes/LabelTest/LabelTestNew.cpp @@ -84,7 +84,8 @@ static std::function createFunctions[] = CL(LabelIssue8492Test), CL(LabelMultilineWithOutline), CL(LabelIssue9255Test), - CL(LabelSmallDimensionsTest) + CL(LabelSmallDimensionsTest), + CL(LabelIssue10089Test) }; #define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0])) @@ -1895,3 +1896,30 @@ std::string LabelSmallDimensionsTest::subtitle() const { return "Program should not dead loop"; } + +LabelIssue10089Test::LabelIssue10089Test() +{ + auto center = VisibleRect::center(); + + auto labelA = Label::createWithSystemFont("create label with system font", "fonts/arial.ttf", 24); + auto size = labelA->getContentSize(); + labelA->setDimensions(size.width, size.height); + labelA->setPosition(center.x, center.y + 50); + addChild(labelA); + + auto labelB = Label::createWithTTF("create label with TTF", "fonts/arial.ttf", 24); + size = labelB->getContentSize(); + labelB->setDimensions(size.width, size.height); + labelB->setPosition(center.x, center.y - 50); + addChild(labelB); +} + +std::string LabelIssue10089Test::title() const +{ + return "Test for Issue #10089"; +} + +std::string LabelIssue10089Test::subtitle() const +{ + return "Should be able to see two single-line text"; +} diff --git a/tests/cpp-tests/Classes/LabelTest/LabelTestNew.h b/tests/cpp-tests/Classes/LabelTest/LabelTestNew.h index 22dece7eed..b7eb66e039 100644 --- a/tests/cpp-tests/Classes/LabelTest/LabelTestNew.h +++ b/tests/cpp-tests/Classes/LabelTest/LabelTestNew.h @@ -555,5 +555,16 @@ public: virtual std::string subtitle() const override; }; +class LabelIssue10089Test : public AtlasDemoNew +{ +public: + CREATE_FUNC(LabelIssue10089Test); + + LabelIssue10089Test(); + + virtual std::string title() const override; + virtual std::string subtitle() const override; +}; + #endif