@@ -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+ */
835930cc . LabelTTF . __getFontHeightByDiv = function ( fontName , fontSize ) {
836931
837932 if ( fontName instanceof cc . FontDefinition ) {
0 commit comments