From de5f52adace4bcedd3aeb10989d2ffb675c37bcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alicia=20Boya=20Garc=C3=ADa?= Date: Wed, 9 Nov 2016 17:37:48 +0100 Subject: [PATCH 1/2] Honor device pixel ratio in CCLabelTTF.getContentSize() --- cocos2d/core/labelttf/CCLabelTTF.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/labelttf/CCLabelTTF.js b/cocos2d/core/labelttf/CCLabelTTF.js index 42803f2a2a..7bef98d374 100644 --- a/cocos2d/core/labelttf/CCLabelTTF.js +++ b/cocos2d/core/labelttf/CCLabelTTF.js @@ -806,7 +806,9 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{ getContentSize: function () { if (this._needUpdateTexture) this._renderCmd._updateTTF(); - return cc.size(this._contentSize); + return cc.size( + this._contentSize.width / cc.view.getDevicePixelRatio(), + this._contentSize.height / cc.view.getDevicePixelRatio()); }, _getWidth: function () { From 946367a3cba63a11a0b81e50824f31efcd8a71d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alicia=20Boya=20Garc=C3=ADa?= Date: Wed, 9 Nov 2016 17:40:05 +0100 Subject: [PATCH 2/2] Revert "Fix UIText issue and Label getContentSize value wrong in retina mode" This reverts commit 9b077f81353f82c2bdc45e4cb904e8705a4fb319. --- cocos2d/core/labelttf/CCLabelTTF.js | 6 +- .../labelttf/CCLabelTTFCanvasRenderCmd.js | 55 ++++++++++--------- extensions/ccui/uiwidgets/UIText.js | 2 +- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/cocos2d/core/labelttf/CCLabelTTF.js b/cocos2d/core/labelttf/CCLabelTTF.js index 7bef98d374..fe4b712751 100644 --- a/cocos2d/core/labelttf/CCLabelTTF.js +++ b/cocos2d/core/labelttf/CCLabelTTF.js @@ -612,7 +612,7 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{ setScale: function (scale, scaleY) { this._scaleX = scale / cc.view.getDevicePixelRatio(); this._scaleY = ((scaleY || scaleY === 0) ? scaleY : scale) / - cc.view.getDevicePixelRatio(); + cc.view.getDevicePixelRatio(); this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty); }, @@ -814,12 +814,12 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{ _getWidth: function () { if (this._needUpdateTexture) this._renderCmd._updateTTF(); - return this._contentSize.width; + return cc.Sprite.prototype._getWidth.call(this); }, _getHeight: function () { if (this._needUpdateTexture) this._renderCmd._updateTTF(); - return this._contentSize.height; + return cc.Sprite.prototype._getHeight.call(this); }, setTextureRect: function (rect, rotated, untrimmedSize) { diff --git a/cocos2d/core/labelttf/CCLabelTTFCanvasRenderCmd.js b/cocos2d/core/labelttf/CCLabelTTFCanvasRenderCmd.js index 31109f2f9b..adf6c4aee6 100644 --- a/cocos2d/core/labelttf/CCLabelTTFCanvasRenderCmd.js +++ b/cocos2d/core/labelttf/CCLabelTTFCanvasRenderCmd.js @@ -48,7 +48,6 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/; this._status = []; this._renderingIndex = 0; - this._texRect = cc.rect(); this._canUseDirtyRegion = true; }; var proto = cc.LabelTTF.RenderCmd.prototype; @@ -104,8 +103,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/; proto._updateTTF = function () { var node = this._node; - var pixelRatio = cc.view.getDevicePixelRatio(); - var locDimensionsWidth = node._dimensions.width * pixelRatio, i, strLength; + var locDimensionsWidth = node._dimensions.width, i, strLength; var locLineWidth = this._lineWidths; locLineWidth.length = 0; @@ -137,6 +135,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/; locStrokeShadowOffsetY += Math.abs(locOffsetSize.y) * 2; } + var pixelRatio = cc.view.getDevicePixelRatio(); //get offset for stroke and shadow if (locDimensionsWidth === 0) { if (this._isMultiLine) @@ -167,16 +166,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/; if (node._getFontStyle() !== "normal") { //add width for 'italic' and 'oblique' locSize.width = Math.ceil(locSize.width + node._fontSize * 0.3); } - if (this._strings.length === 0) { - this._texRect.width = 1; - this._texRect.height = locSize.height || 1; - } - else { - this._texRect.width = locSize.width; - this._texRect.height = locSize.height; - } - var nodeW = locSize.width / pixelRatio, nodeH = locSize.height / pixelRatio; - node.setContentSize(nodeW, nodeH); + node.setContentSize(locSize); node._strokeShadowOffsetX = locStrokeShadowOffsetX; node._strokeShadowOffsetY = locStrokeShadowOffsetY; @@ -188,14 +178,13 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/; proto._saveStatus = function () { var node = this._node; - var scale = cc.view.getDevicePixelRatio(); var locStrokeShadowOffsetX = node._strokeShadowOffsetX, locStrokeShadowOffsetY = node._strokeShadowOffsetY; - var locContentSizeHeight = node._contentSize.height * scale - locStrokeShadowOffsetY, locVAlignment = node._vAlignment, + var locContentSizeHeight = node._contentSize.height - locStrokeShadowOffsetY, locVAlignment = node._vAlignment, locHAlignment = node._hAlignment; var dx = locStrokeShadowOffsetX * 0.5, dy = locContentSizeHeight + locStrokeShadowOffsetY * 0.5; var xOffset = 0, yOffset = 0, OffsetYArray = []; - var locContentWidth = node._contentSize.width * scale - locStrokeShadowOffsetX; + var locContentWidth = node._contentSize.width - locStrokeShadowOffsetX; //lineHeight var lineHeight = node.getLineHeight() * scale; @@ -322,6 +311,11 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/; proto.updateStatus = function () { var flags = cc.Node._dirtyFlags, locFlag = this._dirtyFlag; +<<<<<<< HEAD +======= + cc.Node.RenderCmd.prototype.updateStatus.call(this); + +>>>>>>> 45cdfaa... Revert "Fix UIText issue and Label getContentSize value wrong in retina mode" if (locFlag & flags.textDirty) this._updateTexture(); @@ -335,7 +329,13 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/; proto._syncStatus = function (parentCmd) { var flags = cc.Node._dirtyFlags, locFlag = this._dirtyFlag; +<<<<<<< HEAD +======= + + cc.Node.RenderCmd.prototype._syncStatus.call(this, parentCmd); + +>>>>>>> 45cdfaa... Revert "Fix UIText issue and Label getContentSize value wrong in retina mode" if (locFlag & flags.textDirty) this._updateTexture(); @@ -393,7 +393,6 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/; locCanvas.width = 1; locCanvas.height = 1; this._labelContext = locCanvas.getContext("2d"); - this._texRect = cc.rect(); }; cc.LabelTTF.CacheRenderCmd.prototype = Object.create( cc.LabelTTF.RenderCmd.prototype); @@ -405,8 +404,9 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/; proto._updateTexture = function () { this._dirtyFlag = this._dirtyFlag & cc.Node._dirtyFlags.textDirty ^ this._dirtyFlag; var node = this._node; + var locContentSize = node._contentSize; this._updateTTF(); - var width = this._texRect.width, height = this._texRect.height; + var width = locContentSize.width, height = locContentSize.height; var locContext = this._labelContext, locLabelCanvas = this._labelCanvas; @@ -417,10 +417,10 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/; } if (node._string.length === 0) { - locLabelCanvas.width = width; - locLabelCanvas.height = height; + locLabelCanvas.width = 1; + locLabelCanvas.height = locContentSize.height || 1; node._texture && node._texture.handleLoadedTexture(); - node.setTextureRect(this._texRect); + node.setTextureRect(cc.rect(0, 0, 1, locContentSize.height)); return true; } @@ -428,13 +428,13 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/; locContext.font = this._fontStyleStr; var flag = locLabelCanvas.width === width && locLabelCanvas.height === height; - locLabelCanvas.width = this._texRect.width; - locLabelCanvas.height = this._texRect.height; + locLabelCanvas.width = width; + locLabelCanvas.height = height; if (flag) locContext.clearRect(0, 0, width, height); this._saveStatus(); this._drawTTFInCanvas(locContext); node._texture && node._texture.handleLoadedTexture(); - node.setTextureRect(this._texRect); + node.setTextureRect(cc.rect(0, 0, width, height)); return true; }; @@ -482,14 +482,15 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/; proto._updateTexture = function () { this._dirtyFlag = this._dirtyFlag & cc.Node._dirtyFlags.textDirty ^ this._dirtyFlag; var node = this._node; - var scale = cc.view.getDevicePixelRatio(); + var locContentSize = node._contentSize; this._updateTTF(); + var width = locContentSize.width, height = locContentSize.height; if (node._string.length === 0) { - node.setTextureRect(this._texRect); + node.setTextureRect(cc.rect(0, 0, 1, locContentSize.height)); return true; } this._saveStatus(); - node.setTextureRect(this._texRect); + node.setTextureRect(cc.rect(0, 0, width, height)); return true; }; diff --git a/extensions/ccui/uiwidgets/UIText.js b/extensions/ccui/uiwidgets/UIText.js index bfa1198a77..329ccf508e 100644 --- a/extensions/ccui/uiwidgets/UIText.js +++ b/extensions/ccui/uiwidgets/UIText.js @@ -46,7 +46,7 @@ ccui.Text = ccui.Widget.extend(/** @lends ccui.Text# */{ _fontName: "Arial", _fontSize: 16, _onSelectedScaleOffset:0.5, - _labelRenderer: null, + _labelRenderer: "", _textAreaSize: null, _textVerticalAlignment: 0, _textHorizontalAlignment: 0,