1 /**
  2  * The Render Engine
  3  * ContextText
  4  *
  5  * @fileoverview A native context font renderer.  Uses the context's font rendering
  6  *               mechanism to generate textual output.
  7  *
  8  * @author: Brett Fattori (brettf@renderengine.com)
  9  * @author: $Author: bfattori $
 10  * @version: $Revision: 1555 $
 11  *
 12  * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com)
 13  *
 14  * Permission is hereby granted, free of charge, to any person obtaining a copy
 15  * of this software and associated documentation files (the "Software"), to deal
 16  * in the Software without restriction, including without limitation the rights
 17  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 18  * copies of the Software, and to permit persons to whom the Software is
 19  * furnished to do so, subject to the following conditions:
 20  *
 21  * The above copyright notice and this permission notice shall be included in
 22  * all copies or substantial portions of the Software.
 23  *
 24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 29  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 30  * THE SOFTWARE.
 31  *
 32  */
 33 
 34 // The class this file defines and its required classes
 35 R.Engine.define({
 36     "class":"R.text.ContextText",
 37     "requires":[
 38         "R.text.AbstractTextRenderer",
 39         "R.math.Point2D",
 40         "R.rendercontexts.RenderContext2D",
 41         "R.text.TextRenderer"
 42     ]
 43 });
 44 
 45 /**
 46  * @class A text renderer which draws text using the context's native
 47  *        text rendering mechansim.  This may not work on all platforms, in
 48  *        all browsers.  If not, see {@link R.text.BitmapText} as an alternative.
 49  *
 50  * @constructor
 51  * @extends R.text.AbstractTextRenderer
 52  */
 53 R.text.ContextText = function () {
 54     return R.text.AbstractTextRenderer.extend(/** @scope R.text.ContextText.prototype */{
 55 
 56         /** @private */
 57         constructor:function () {
 58             this.base();
 59             this.tInit();
 60         },
 61 
 62         /**
 63          * Initialize some basics
 64          * @private
 65          */
 66         tInit:function () {
 67             this.setTextAlignment(R.rendercontexts.RenderContext2D.FONT_ALIGN_LEFT);
 68             this.setTextWeight(R.rendercontexts.RenderContext2D.FONT_WEIGHT_NORMAL);
 69             this.setTextFont("sans-serif");
 70             this.setTextStyle(R.rendercontexts.RenderContext2D.FONT_STYLE_NORMAL);
 71         },
 72 
 73         /**
 74          * Release the text renderer back into the pool for reuse
 75          */
 76         release:function () {
 77             this.base();
 78             this.tInit();
 79         },
 80 
 81         /**
 82          * Return <tt>true</tt> if the text renderer is native to the context.
 83          * @return {Boolean}
 84          */
 85         isNative:function () {
 86             return true;
 87         },
 88 
 89         setText:function (text) {
 90             this.base(text);
 91             this.calculateBoundingBox();
 92         },
 93 
 94         /**
 95          * Calculate the bounding box for the text and set it on the host object.
 96          * @private
 97          */
 98         calculateBoundingBox:function () {
 99             if (this.getGameObject().getRenderContext()) {
100                 var ctx = this.getGameObject().getRenderContext();
101                 ctx.pushTransform();
102                 ctx.setFontStyle(this.getTextStyle());
103                 ctx.setFontAlign(this.getTextAlignment());
104                 ctx.setFontWeight(this.getTextWeight());
105                 ctx.setFont(this.getTextFont());
106                 ctx.setFontSize(Math.floor(this.getSize() * R.text.TextRenderer.BASE_TEXT_PIXELSIZE) || R.text.TextRenderer.BASE_TEXT_PIXELSIZE);
107 
108                 this.getGameObject().setBoundingBox(ctx.getTextMetrics(this.getText()));
109                 ctx.popTransform();
110             }
111         },
112 
113         /**
114          * @private
115          */
116         execute:function (renderContext, time, dt) {
117 
118             if (this.getText().length == 0) {
119                 return;
120             }
121 
122             renderContext.setFontStyle(this.getTextStyle());
123             renderContext.setFontAlign(this.getTextAlignment());
124             renderContext.setFontWeight(this.getTextWeight());
125             renderContext.setFont(this.getTextFont());
126             renderContext.setFontSize(Math.floor(this.getSize() * R.text.TextRenderer.BASE_TEXT_PIXELSIZE) || R.text.TextRenderer.BASE_TEXT_PIXELSIZE);
127 
128             renderContext.setFillStyle(this.getColor());
129             renderContext.drawText(R.math.Point2D.ZERO, this.getText(), this.getGameObject());
130         }
131 
132     }, /** @scope R.text.ContextText.prototype */ {
133         /**
134          * Get the class name of this object
135          * @return {String} The string "R.text.ContextText"
136          */
137         getClassName:function () {
138             return "R.text.ContextText";
139         }
140     });
141 };
142