diff --git a/src/flash/avm1lib/AS2Globals.js b/src/flash/avm1lib/AS2Globals.js index fcf1902d44..b1e80d1e40 100644 --- a/src/flash/avm1lib/AS2Globals.js +++ b/src/flash/avm1lib/AS2Globals.js @@ -15,18 +15,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/*global AS2Context, Multiname, Stubs */ +/*global AS2Context, Multiname, Stubs, TextFormatDefinition */ var AS2GlobalsDefinition = (function () { var def = { __class__: 'avm1lib.AS2Globals', initialize: function () { + // The AS2 version of TextFormat has an additional method "getTextExtent". + // We install that here so we don't need to have a full AS2 version of + // TextFormat and take care to return that everywhere when in AS2 mode. + flash.text.TextFormat.prototype.asDefinePublicProperty('getTextExtent', { + value: TextFormatDefinition.as2GetTextExtent, + writable: false, + enumerable: false, + configurable: false + }); }, }; - var desc = Object.getOwnPropertyDescriptor; - def.__glue__ = { native: { instance: { @@ -69,4 +76,4 @@ var AS2GlobalsDefinition = (function () { }; return def; -}).call(this); \ No newline at end of file +}).call(this); diff --git a/src/flash/text/TextField.js b/src/flash/text/TextField.js index 22e7de730f..b6115ba03c 100644 --- a/src/flash/text/TextField.js +++ b/src/flash/text/TextField.js @@ -808,6 +808,26 @@ var TextFieldDefinition = (function () { } this._bbox.yMax = value; this._invalidate(); + }, + getLineMetrics: function(lineIndex) { + this.ensureDimensions(); + if (lineIndex < 0 || lineIndex >= this._lines.length) { + throwError('RangeError', Errors.ParamRangeError); + } + var line = this._lines[lineIndex]; + var format = line.largestFormat; + var metrics = format.font._metrics; + var size = format.size; + // Rounding for metrics seems to be screwy. A descent of 3.5 gets + // rounded to 3, but an ascent of 12.8338 gets rounded to 13. + // For now, round up for things slightly above .5. + var ascent = metrics.ascent * size + 0.49999 | 0; + var descent = metrics.descent * size + 0.49999 | 0; + var leading = metrics.leading * size + 0.49999 + line.leading | 0; + // TODO: check if metrics values can be floats for embedded fonts + return new flash.text.TextLineMetrics(line.x + 2, line.width, + line.height, + ascent, descent, leading); } }; @@ -904,24 +924,7 @@ var TextFieldDefinition = (function () { } }, getLineMetrics: function (lineIndex) { // (lineIndex:int) -> TextLineMetrics - this.ensureDimensions(); - if (lineIndex < 0 || lineIndex >= this._lines.length) { - throwError('RangeError', Errors.ParamRangeError); - } - var line = this._lines[lineIndex]; - var format = line.largestFormat; - var metrics = format.font._metrics; - var size = format.size; - // Rounding for metrics seems to be screwy. A descent of 3.5 gets - // rounded to 3, but an ascent of 12.8338 gets rounded to 13. - // For now, round up for things slightly above .5. - var ascent = metrics.ascent * size + 0.49999 | 0; - var descent = metrics.descent * size + 0.49999 | 0; - var leading = metrics.leading * size + 0.49999 + line.leading | 0; - // TODO: check if metrics values can be floats for embedded fonts - return new flash.text.TextLineMetrics(line.x + 2, line.width, - line.height, - ascent, descent, leading); + return this.getLineMetrics(lineIndex); }, scrollV: { get: function scrollV() { diff --git a/src/flash/text/TextFormatClass.js b/src/flash/text/TextFormatClass.js index 692c35b09f..ca6c075dfa 100644 --- a/src/flash/text/TextFormatClass.js +++ b/src/flash/text/TextFormatClass.js @@ -17,6 +17,7 @@ */ var TextFormatDefinition = (function () { + var measureTextField; return { // (font:String = null, size:Object = null, color:Object = null, bold:Object = null, // italic:Object = null, underline:Object = null, url:String = null, target:String = null, @@ -65,6 +66,35 @@ var TextFormatDefinition = (function () { leading: this._leading || 0 }; }, + as2GetTextExtent: function(text, width /* optional */) { + if (!measureTextField) { + measureTextField = new flash.text.TextField(); + measureTextField._multiline = true; + } + if (!isNaN(width) && width > 0) { + measureTextField.width = width + 4; + measureTextField._wordWrap = true; + } else { + measureTextField._wordWrap = false; + } + measureTextField.defaultTextFormat = this; + measureTextField.text = text; + measureTextField.ensureDimensions(); + var result = {}; + var textWidth = measureTextField._textWidth; + var textHeight = measureTextField._textHeight; + result.asSetPublicProperty('width', textWidth); + result.asSetPublicProperty('height', textHeight); + result.asSetPublicProperty('textFieldWidth', textWidth + 4); + result.asSetPublicProperty('textFieldHeight', textHeight + 4); + var metrics = measureTextField.getLineMetrics(0); + result.asSetPublicProperty('ascent', + metrics.asGetPublicProperty('ascent')); + result.asSetPublicProperty('descent', + metrics.asGetPublicProperty('descent')); + return result; + }, + __glue__: { native: { static: {