merge html5 CCLabelTTF / CCLabelTTFCanvasRenderCmd for #fireball/issues/3022

This commit is contained in:
knoxHuang
2016-06-28 10:34:42 +08:00
parent 45d0d2d777
commit 178c89e4bf
2 changed files with 136 additions and 25 deletions
+99 -3
View File
@@ -135,6 +135,12 @@ cc.LabelTTF = _ccsg.Sprite.extend(/** @lends cc.LabelTTF# */{
this._renderCmd._setColorsString();
this._renderCmd._updateTexture();
this._setUpdateTextureDirty();
// Needed for high dpi text.
// In order to render it crisp, we request devicePixelRatio times the
// font size and scale it down 1/devicePixelRatio.
this._scaleX = this._scaleY = 1 / cc.view.getDevicePixelRatio();
return true;
},
@@ -572,6 +578,87 @@ cc.LabelTTF = _ccsg.Sprite.extend(/** @lends cc.LabelTTF# */{
return texDef;
},
/*
* BEGIN SCALE METHODS
*
* In order to make the value of scaleX and scaleY consistent across
* screens, we provide patched versions that return the same values as if
* the screen was not HiDPI.
*/
/**
* Returns the scale factor of the node.
* @warning: Assertion will fail when _scaleX != _scaleY.
* @function
* @return {Number} The scale factor
*/
getScale: function () {
if (this._scaleX !== this._scaleY)
cc.log(cc._LogInfos.Node_getScale);
return this._scaleX * cc.view.getDevicePixelRatio();
},
/**
* Sets the scale factor of the node. 1.0 is the default scale factor.
* This function can modify the X and Y scale at the same time.
* @function
* @param {Number} scale or scaleX value
* @param {Number} [scaleY=]
*/
setScale: function (scale, scaleY) {
this._scaleX = scale / cc.view.getDevicePixelRatio();
this._scaleY = ((scaleY || scaleY === 0) ? scaleY : scale) / cc.view.getDevicePixelRatio();
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/**
* Returns the scale factor on X axis of this node
* @function
* @return {Number} The scale factor on X axis.
*/
getScaleX: function () {
return this._scaleX * cc.view.getDevicePixelRatio();
},
/**
* <p>
* Changes the scale factor on X axis of this node <br/>
* The default value is 1.0 if you haven't changed it before
* </p>
* @function
* @param {Number} newScaleX The scale factor on X axis.
*/
setScaleX: function (newScaleX) {
this._scaleX = newScaleX / cc.view.getDevicePixelRatio();
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/**
* Returns the scale factor on Y axis of this node
* @function
* @return {Number} The scale factor on Y axis.
*/
getScaleY: function () {
return this._scaleY * cc.view.getDevicePixelRatio();
},
/**
* <p>
* Changes the scale factor on Y axis of this node <br/>
* The Default value is 1.0 if you haven't changed it before.
* </p>
* @function
* @param {Number} newScaleY The scale factor on Y axis.
*/
setScaleY: function (newScaleY) {
this._scaleY = newScaleY / cc.view.getDevicePixelRatio();
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/*
* END SCALE METHODS
*/
/**
* Changes the text content of the label
* @warning Changing the string is as expensive as creating a new cc.LabelTTF. To obtain better performance use cc.LabelAtlas
@@ -714,18 +801,18 @@ cc.LabelTTF = _ccsg.Sprite.extend(/** @lends cc.LabelTTF# */{
getContentSize: function () {
if (this._needUpdateTexture)
this._renderCmd._updateTTF();
return _ccsg.Sprite.prototype.getContentSize.call(this);
return cc.size(this._contentSize);
},
_getWidth: function () {
if (this._needUpdateTexture)
this._renderCmd._updateTTF();
return _ccsg.Sprite.prototype._getWidth.call(this);
return this._contentSize.width;
},
_getHeight: function () {
if (this._needUpdateTexture)
this._renderCmd._updateTTF();
return _ccsg.Sprite.prototype._getHeight.call(this);
return this._contentSize.height;
},
setTextureRect: function (rect, rotated, untrimmedSize) {
@@ -825,6 +912,15 @@ document.body ?
document.body.appendChild(cc.LabelTTF.__labelHeightDiv);
}, false);
/**
* Returns the height of text with an specified font family and font size, in
* device independent pixels.
*
* @param {string|cc.FontDefinition} fontName
* @param {number} fontSize
* @returns {number}
* @private
*/
cc.LabelTTF.__getFontHeightByDiv = function (fontName, fontSize) {
if(fontName instanceof cc.FontDefinition){
@@ -47,6 +47,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
this._isMultiLine = false;
this._status = [];
this._renderingIndex = 0;
this._texRect = cc.rect();
};
var proto = cc.LabelTTF.RenderCmd.prototype;
proto.constructor = cc.LabelTTF.RenderCmd;
@@ -57,7 +58,8 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontNameOrFontDef);
}else {
this._fontStyleStr = fontStyle + " " + fontWeight + " " + fontSize + "px '" + fontNameOrFontDef + "'";
var deviceFontSize = fontSize * cc.view.getDevicePixelRatio();
this._fontStyleStr = fontStyle + " " + fontWeight + " " + deviceFontSize + "px '" + fontNameOrFontDef + "'";
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontNameOrFontDef, fontSize);
}
};
@@ -91,7 +93,8 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
proto._updateTTF = function () {
var node = this._node;
var locDimensionsWidth = node._dimensions.width, i, strLength;
var pixelRatio = cc.view.getDevicePixelRatio();
var locDimensionsWidth = node._dimensions.width * pixelRatio, i, strLength;
var locLineWidth = this._lineWidths;
locLineWidth.length = 0;
@@ -127,24 +130,37 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
if (locDimensionsWidth === 0) {
if (this._isMultiLine)
locSize = cc.size(Math.ceil(Math.max.apply(Math, locLineWidth) + locStrokeShadowOffsetX),
Math.ceil((this._fontClientHeight * this._strings.length) + locStrokeShadowOffsetY));
Math.ceil((this._fontClientHeight * pixelRatio * this._strings.length) + locStrokeShadowOffsetY));
else
locSize = cc.size(Math.ceil(this._measure(node._string) + locStrokeShadowOffsetX), Math.ceil(this._fontClientHeight + locStrokeShadowOffsetY));
locSize = cc.size(Math.ceil(this._measure(node._string) + locStrokeShadowOffsetX),
Math.ceil(this._fontClientHeight * pixelRatio + locStrokeShadowOffsetY));
} else {
if (node._dimensions.height === 0) {
if (this._isMultiLine)
locSize = cc.size(Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX), Math.ceil((node.getLineHeight() * this._strings.length) + locStrokeShadowOffsetY));
locSize = cc.size(Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX),
Math.ceil((node.getLineHeight() * pixelRatio * this._strings.length) + locStrokeShadowOffsetY));
else
locSize = cc.size(Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX), Math.ceil(node.getLineHeight() + locStrokeShadowOffsetY));
locSize = cc.size(Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX),
Math.ceil(node.getLineHeight() * pixelRatio + locStrokeShadowOffsetY));
} else {
//dimension is already set, contentSize must be same as dimension
locSize = cc.size(Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX), Math.ceil(node._dimensions.height + locStrokeShadowOffsetY));
locSize = cc.size(Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX),
Math.ceil(node._dimensions.height * pixelRatio + locStrokeShadowOffsetY));
}
}
if(node._getFontStyle() !== "normal"){ //add width for 'italic' and 'oblique'
locSize.width = Math.ceil(locSize.width + node._fontSize * 0.3);
}
node.setContentSize(locSize);
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._strokeShadowOffsetX = locStrokeShadowOffsetX;
node._strokeShadowOffsetY = locStrokeShadowOffsetY;
@@ -156,13 +172,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 - locStrokeShadowOffsetY, locVAlignment = node._vAlignment,
locHAlignment = node._hAlignment;
var locContentSizeHeight = node._contentSize.height * scale - locStrokeShadowOffsetY, locVAlignment = node._vAlignment;
var dx = locStrokeShadowOffsetX * 0.5,
dy = locContentSizeHeight + locStrokeShadowOffsetY * 0.5;
var xOffset = 0, yOffset = 0, OffsetYArray = [];
var locContentWidth = node._contentSize.width - locStrokeShadowOffsetX;
var locContentWidth = node._contentSize.width * scale - locStrokeShadowOffsetX;
//lineHeight
var lineHeight = node.getLineHeight();
@@ -386,6 +402,7 @@ 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);
@@ -397,9 +414,8 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
proto._updateTexture = function () {
this._dirtyFlag = this._dirtyFlag & _ccsg.Node._dirtyFlags.textDirty ^ this._dirtyFlag;
var node = this._node;
var locContentSize = node._contentSize;
this._updateTTF();
var width = locContentSize.width, height = locContentSize.height;
var width = this._texRect.width, height = this._texRect.height;
var locContext = this._labelContext, locLabelCanvas = this._labelCanvas;
@@ -410,10 +426,10 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
}
if (node._string.length === 0) {
locLabelCanvas.width = 1;
locLabelCanvas.width = width;
locLabelCanvas.height = locContentSize.height || 1;
node._texture && node._texture.handleLoadedTexture();
node.setTextureRect(cc.rect(0, 0, 1, locContentSize.height));
node.setTextureRect(this._texRect);
return true;
}
@@ -421,13 +437,13 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
locContext.font = this._fontStyleStr;
var flag = locLabelCanvas.width === width && locLabelCanvas.height === height;
locLabelCanvas.width = width;
locLabelCanvas.height = height;
locLabelCanvas.width = this._texRect.width;
locLabelCanvas.height = this._texRect.height;
if (flag) locContext.clearRect(0, 0, width, height);
this._saveStatus();
this._drawTTFInCanvas(locContext);
node._texture && node._texture.handleLoadedTexture();
node.setTextureRect(cc.rect(0, 0, width, height));
node.setTextureRect(this._texRect);
return true;
};
@@ -475,15 +491,14 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
proto._updateTexture = function () {
this._dirtyFlag = this._dirtyFlag & _ccsg.Node._dirtyFlags.textDirty ^ this._dirtyFlag;
var node = this._node;
var locContentSize = node._contentSize;
var scale = cc.view.getDevicePixelRatio();
this._updateTTF();
var width = locContentSize.width, height = locContentSize.height;
if (node._string.length === 0) {
node.setTextureRect(cc.rect(0, 0, 1, locContentSize.height));
node.setTextureRect(this._texRect);
return true;
}
this._saveStatus();
node.setTextureRect(cc.rect(0, 0, width, height));
node.setTextureRect(this._texRect);
return true;
};