Skip to content

Simpler CCLabelTTF.getContentSize() fix #3407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions cocos2d/core/labelttf/CCLabelTTF.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},

Expand Down Expand Up @@ -806,18 +806,20 @@ 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 () {
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) {
Expand Down
55 changes: 28 additions & 27 deletions cocos2d/core/labelttf/CCLabelTTFCanvasRenderCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand All @@ -417,24 +417,24 @@ 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;
}

//set size for labelCanvas
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;
};

Expand Down Expand Up @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion extensions/ccui/uiwidgets/UIText.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down