dist/melonjs.js
function draw(renderer, text, x, y) {
         // allows to provide backward compatibility when
         // adding Bitmap Font to an object container
         if (typeof this.ancestor === "undefined") {
           // update cache
           this.setText(text); // force update bounds

           this.update(0); // save the previous global alpha value

           var _alpha = renderer.globalAlpha();

           renderer.setGlobalAlpha(_alpha * this.getOpacity());
         } else {
           // added directly to an object container
           x = this.pos.x;
           y = this.pos.y;
         }

         var strings = ("" + this._text).split("\n");

         var lX = x;
         var stringHeight = measureTextHeight(this);
         var maxWidth = 0;

         for (var i = 0; i < strings.length; i++) {
           x = lX;
           var string = me.utils.string.trimRight(strings[i]); // adjust x pos based on alignment value

           var stringWidth = measureTextWidth(this, string);

           switch (this.textAlign) {
             case "right":
               x -= stringWidth;
               break;

             case "center":
               x -= stringWidth * 0.5;
               break;

             default:
               break;
           } // adjust y pos based on alignment value


           switch (this.textBaseline) {
             case "middle":
               y -= stringHeight * 0.5;
               break;

             case "ideographic":
             case "alphabetic":
             case "bottom":
               y -= stringHeight;
               break;

             default:
               break;
           } // update initial position if required


           if (this.isDirty === true && typeof this.ancestor === "undefined") {
             if (i === 0) {
               this.pos.y = y;
             }

             if (maxWidth < stringWidth) {
               maxWidth = stringWidth;
               this.pos.x = x;
             }
           } // draw the string


           var lastGlyph = null;

           for (var c = 0, len = string.length; c < len; c++) {
             // calculate the char index
             var ch = string.charCodeAt(c);
             var glyph = this.fontData.glyphs[ch];
             var glyphWidth = glyph.width;
             var glyphHeight = glyph.height;
             var kerning = lastGlyph && lastGlyph.kerning ? lastGlyph.getKerning(ch) : 0; // draw it

             if (glyphWidth !== 0 && glyphHeight !== 0) {
               // some browser throw an exception when drawing a 0 width or height image
               renderer.drawImage(this.fontImage, glyph.x, glyph.y, glyphWidth, glyphHeight, x + glyph.xoffset, y + glyph.yoffset * this.fontScale.y, glyphWidth * this.fontScale.x, glyphHeight * this.fontScale.y);
             } // increment position


             x += (glyph.xadvance + kerning) * this.fontScale.x;
             lastGlyph = glyph;
           } // increment line


           y += stringHeight;
         }

         if (typeof this.ancestor === "undefined") {
           // restore the previous global alpha value
           renderer.setGlobalAlpha(_alpha);
         } // clear the dirty flag


         this.isDirty = false;
       }
function draw(renderer, text, x, y) {
         // allows to provide backward compatibility when
         // adding Bitmap Font to an object container
         if (typeof this.ancestor === "undefined") {
           // update cache
           this.setText(text); // force update bounds

           this.update(0); // save the previous global alpha value

           var _alpha = renderer.globalAlpha();

           renderer.setGlobalAlpha(_alpha * this.getOpacity());
         } else {
           // added directly to an object container
           x = this.pos.x;
           y = this.pos.y;
         }

         var strings = ("" + this._text).split("\n");

         var lX = x;
         var stringHeight = measureTextHeight(this);
         var maxWidth = 0;

         for (var i = 0; i < strings.length; i++) {
           x = lX;
           var string = me.utils.string.trimRight(strings[i]); // adjust x pos based on alignment value

           var stringWidth = measureTextWidth(this, string);

           switch (this.textAlign) {
             case "right":
               x -= stringWidth;
               break;

             case "center":
               x -= stringWidth * 0.5;
               break;

             default:
               break;
           } // adjust y pos based on alignment value


           switch (this.textBaseline) {
             case "middle":
               y -= stringHeight * 0.5;
               break;

             case "ideographic":
             case "alphabetic":
             case "bottom":
               y -= stringHeight;
               break;

             default:
               break;
           } // update initial position if required


           if (this.isDirty === true && typeof this.ancestor === "undefined") {
             if (i === 0) {
               this.pos.y = y;
             }

             if (maxWidth < stringWidth) {
               maxWidth = stringWidth;
               this.pos.x = x;
             }
           } // draw the string


           var lastGlyph = null;

           for (var c = 0, len = string.length; c < len; c++) {
             // calculate the char index
             var ch = string.charCodeAt(c);
             var glyph = this.fontData.glyphs[ch];
             var glyphWidth = glyph.width;
             var glyphHeight = glyph.height;
             var kerning = lastGlyph && lastGlyph.kerning ? lastGlyph.getKerning(ch) : 0; // draw it

             if (glyphWidth !== 0 && glyphHeight !== 0) {
               // some browser throw an exception when drawing a 0 width or height image
               renderer.drawImage(this.fontImage, glyph.x, glyph.y, glyphWidth, glyphHeight, x + glyph.xoffset, y + glyph.yoffset * this.fontScale.y, glyphWidth * this.fontScale.x, glyphHeight * this.fontScale.y);
             } // increment position


             x += (glyph.xadvance + kerning) * this.fontScale.x;
             lastGlyph = glyph;
           } // increment line


           y += stringHeight;
         }

         if (typeof this.ancestor === "undefined") {
           // restore the previous global alpha value
           renderer.setGlobalAlpha(_alpha);
         } // clear the dirty flag


         this.isDirty = false;
       }
function draw ( renderer , text , x , y ) { // allows to provide backward compatibility when // adding Bitmap Font to an object container if ( typeof this . ancestor === "undefined" ) { // update cache this . setText ( text ) ; // force update bounds this . update ( 0 ) ; // save the previous global alpha value var _alpha = renderer . globalAlpha ( ) ; renderer . setGlobalAlpha ( _alpha * this . getOpacity ( ) ) ; } else { // added directly to an object container x = this . pos . x ; y = this . pos . y ; } var strings = ( "" + this . _text ) . split ( "\n" ) ; var lX = x ; var stringHeight = measureTextHeight ( this ) ; var maxWidth = 0 ; for ( var i = 0 ; i < strings . length ; i ++ ) { x = lX ; var string = me . utils . string . trimRight ( strings [ i ] ) ; // adjust x pos based on alignment value var stringWidth = measureTextWidth ( this , string ) ; switch ( this . textAlign ) { case "right" : x -= stringWidth ; break ; case "center" : x -= stringWidth * 0.5 ; break ; default : break ; } // adjust y pos based on alignment value switch ( this . textBaseline ) { case "middle" : y -= stringHeight * 0.5 ; break ; case "ideographic" : case "alphabetic" : case "bottom" : y -= stringHeight ; break ; default : break ; } // update initial position if required if ( this . isDirty === true && typeof this . ancestor === "undefined" ) { if ( i === 0 ) { this . pos . y = y ; } if ( maxWidth < stringWidth ) { maxWidth = stringWidth ; this . pos . x = x ; } } // draw the string var lastGlyph = null ; for ( var c = 0 , len = string . length ; c < len ; c ++ ) { // calculate the char index var ch = string . charCodeAt ( c ) ; var glyph = this . fontData . glyphs [ ch ] ; var glyphWidth = glyph . width ; var glyphHeight = glyph . height ; var kerning = lastGlyph && lastGlyph . kerning ? lastGlyph . getKerning ( ch ) : 0 ; // draw it if ( glyphWidth !== 0 && glyphHeight !== 0 ) { // some browser throw an exception when drawing a 0 width or height image renderer . drawImage ( this . fontImage , glyph . x , glyph . y , glyphWidth , glyphHeight , x + glyph . xoffset , y + glyph . yoffset * this . fontScale . y , glyphWidth * this . fontScale . x , glyphHeight * this . fontScale . y ) ; } // increment position x += ( glyph . xadvance + kerning ) * this . fontScale . x ; lastGlyph = glyph ; } // increment line y += stringHeight ; } if ( typeof this . ancestor === "undefined" ) { // restore the previous global alpha value renderer . setGlobalAlpha ( _alpha ) ; } // clear the dirty flag this . isDirty = false ; }
draw the bitmap font
@name draw
@memberOf me.BitmapText.prototype
@function
@param {me.CanvasRenderer|me.WebGLRenderer} renderer Reference to the destination renderer instance
@param {String} [text]
@param {Number} [x]
@param {Number} [y]
draw the bitmap font