Skip to content

Commit 13183a7

Browse files
committed
HiDPI support in CCLabelTTF
1 parent b7ed34d commit 13183a7

File tree

2 files changed

+113
-7
lines changed

2 files changed

+113
-7
lines changed

Diff for: cocos2d/core/labelttf/CCLabelTTF.js

+95
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
135135
this._renderCmd._setColorsString();
136136
this._renderCmd._updateTexture();
137137
this._setUpdateTextureDirty();
138+
139+
// Needed for high dpi text.
140+
// In order to render it crisp, we request devicePixelRatio times the
141+
// font size and scale it down 1/devicePixelRatio.
142+
this._scaleX = this._scaleY = 1 / cc.view.getDevicePixelRatio();
138143
return true;
139144
},
140145

@@ -578,6 +583,87 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
578583
return texDef;
579584
},
580585

586+
/*
587+
* BEGIN SCALE METHODS
588+
*
589+
* In order to make the value of scaleX and scaleY consistent across
590+
* screens, we provide patched versions that return the same values as if
591+
* the screen was not HiDPI.
592+
*/
593+
594+
/**
595+
* Returns the scale factor of the node.
596+
* @warning: Assertion will fail when _scaleX != _scaleY.
597+
* @function
598+
* @return {Number} The scale factor
599+
*/
600+
getScale: function () {
601+
if (this._scaleX !== this._scaleY)
602+
cc.log(cc._LogInfos.Node_getScale);
603+
return this._scaleX * cc.view.getDevicePixelRatio();
604+
},
605+
606+
/**
607+
* 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.
608+
* @function
609+
* @param {Number} scale or scaleX value
610+
* @param {Number} [scaleY=]
611+
*/
612+
setScale: function (scale, scaleY) {
613+
this._scaleX = scale / cc.view.getDevicePixelRatio();
614+
this._scaleY = ((scaleY || scaleY === 0) ? scaleY : scale) /
615+
cc.view.getDevicePixelRatio();
616+
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
617+
},
618+
619+
/**
620+
* Returns the scale factor on X axis of this node
621+
* @function
622+
* @return {Number} The scale factor on X axis.
623+
*/
624+
getScaleX: function () {
625+
return this._scaleX * cc.view.getDevicePixelRatio();
626+
},
627+
628+
/**
629+
* <p>
630+
* Changes the scale factor on X axis of this node <br/>
631+
* The default value is 1.0 if you haven't changed it before
632+
* </p>
633+
* @function
634+
* @param {Number} newScaleX The scale factor on X axis.
635+
*/
636+
setScaleX: function (newScaleX) {
637+
this._scaleX = newScaleX / cc.view.getDevicePixelRatio();
638+
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
639+
},
640+
641+
/**
642+
* Returns the scale factor on Y axis of this node
643+
* @function
644+
* @return {Number} The scale factor on Y axis.
645+
*/
646+
getScaleY: function () {
647+
return this._scaleY * cc.view.getDevicePixelRatio();
648+
},
649+
650+
/**
651+
* <p>
652+
* Changes the scale factor on Y axis of this node <br/>
653+
* The Default value is 1.0 if you haven't changed it before.
654+
* </p>
655+
* @function
656+
* @param {Number} newScaleY The scale factor on Y axis.
657+
*/
658+
setScaleY: function (newScaleY) {
659+
this._scaleY = newScaleY / cc.view.getDevicePixelRatio();
660+
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
661+
},
662+
663+
/*
664+
* END SCALE METHODS
665+
*/
666+
581667
/**
582668
* Changes the text content of the label
583669
* @warning Changing the string is as expensive as creating a new cc.LabelTTF. To obtain better performance use cc.LabelAtlas
@@ -832,6 +918,15 @@ document.body ?
832918
document.body.appendChild(cc.LabelTTF.__labelHeightDiv);
833919
}, false);
834920

921+
/**
922+
* Returns the height of text with an specified font family and font size, in
923+
* device independent pixels.
924+
*
925+
* @param {string|cc.FontDefinition} fontName
926+
* @param {number} fontSize
927+
* @returns {number}
928+
* @private
929+
*/
835930
cc.LabelTTF.__getFontHeightByDiv = function (fontName, fontSize) {
836931

837932
if(fontName instanceof cc.FontDefinition){

Diff for: cocos2d/core/labelttf/CCLabelTTFCanvasRenderCmd.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
5757
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontNameOrFontDef);
5858

5959
}else {
60-
this._fontStyleStr = fontStyle + " " + fontWeight + " " + fontSize + "px '" + fontNameOrFontDef + "'";
60+
var deviceFontSize = fontSize * cc.view.getDevicePixelRatio();
61+
this._fontStyleStr = fontStyle + " " + fontWeight + " " + deviceFontSize + "px '" + fontNameOrFontDef + "'";
6162
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontNameOrFontDef, fontSize);
6263
}
6364
};
@@ -123,22 +124,32 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
123124
locStrokeShadowOffsetY += Math.abs(locOffsetSize.y) * 2;
124125
}
125126

127+
var pixelRatio = cc.view.getDevicePixelRatio();
126128
//get offset for stroke and shadow
127129
if (locDimensionsWidth === 0) {
128130
if (this._isMultiLine)
129-
locSize = cc.size(Math.ceil(Math.max.apply(Math, locLineWidth) + locStrokeShadowOffsetX),
130-
Math.ceil((this._fontClientHeight * this._strings.length) + locStrokeShadowOffsetY));
131+
locSize = cc.size(
132+
Math.ceil(Math.max.apply(Math, locLineWidth) + locStrokeShadowOffsetX),
133+
Math.ceil((this._fontClientHeight * pixelRatio * this._strings.length) + locStrokeShadowOffsetY));
131134
else
132-
locSize = cc.size(Math.ceil(this._measure(node._string) + locStrokeShadowOffsetX), Math.ceil(this._fontClientHeight + locStrokeShadowOffsetY));
135+
locSize = cc.size(
136+
Math.ceil(this._measure(node._string) + locStrokeShadowOffsetX),
137+
Math.ceil(this._fontClientHeight * pixelRatio + locStrokeShadowOffsetY));
133138
} else {
134139
if (node._dimensions.height === 0) {
135140
if (this._isMultiLine)
136-
locSize = cc.size(Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX), Math.ceil((node.getLineHeight() * this._strings.length) + locStrokeShadowOffsetY));
141+
locSize = cc.size(
142+
Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX),
143+
Math.ceil((node.getLineHeight() * pixelRatio * this._strings.length) + locStrokeShadowOffsetY));
137144
else
138-
locSize = cc.size(Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX), Math.ceil(node.getLineHeight() + locStrokeShadowOffsetY));
145+
locSize = cc.size(
146+
Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX),
147+
Math.ceil(node.getLineHeight() * pixelRatio + locStrokeShadowOffsetY));
139148
} else {
140149
//dimension is already set, contentSize must be same as dimension
141-
locSize = cc.size(Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX), Math.ceil(node._dimensions.height + locStrokeShadowOffsetY));
150+
locSize = cc.size(
151+
Math.ceil(locDimensionsWidth + locStrokeShadowOffsetX),
152+
Math.ceil(node._dimensions.height * pixelRatio + locStrokeShadowOffsetY));
142153
}
143154
}
144155
if(node._getFontStyle() !== "normal"){ //add width for 'italic' and 'oblique'

0 commit comments

Comments
 (0)