Skip to content

Commit

Permalink
Implement AS2's TextFormat#getTextExtent
Browse files Browse the repository at this point in the history
Another fix for the ad from mozilla#471
  • Loading branch information
tschneidereit committed Oct 31, 2013
1 parent fcec5e9 commit 23c12e4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
15 changes: 11 additions & 4 deletions src/flash/avm1lib/AS2Globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -69,4 +76,4 @@ var AS2GlobalsDefinition = (function () {
};

return def;
}).call(this);
}).call(this);
39 changes: 21 additions & 18 deletions src/flash/text/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down Expand Up @@ -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() {
Expand Down
30 changes: 30 additions & 0 deletions src/flash/text/TextFormatClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 23c12e4

Please sign in to comment.