August 12, 2008

workaround for 127px font size limit

In ActionScript, text fields formatted via TextFormat, CSS, or HTML have an undocumented maximum font size of 127px. The limitation is based on historical requirements placed on Flash Player by the operating system. The limit also applies to dynamic and input text created in the Flash authoring tool, but not to static text.

Fortunately, the 127 font-size limit can be overcome with an ugly little workaround: set the font size, then scale the text field using scaleX and scaleY. For example, the following code sets a text field's effective font size to 200 by first setting TextFormat.size to 100, then scaling by 2.

var format:TextFormat = new TextFormat();
format.size = 100;
      
var t:TextField = new TextField();
t.autoSize = TextFieldAutoSize.LEFT;
t.text = "Nice and big.";
t.setTextFormat(format);
t.scaleX = 2;
t.scaleY = 2;
addChild(t);

As of Flash Player 10, the 127px font size limit continues to apply to traditional text, but has been removed for text rendered via the new Flash Text Engine (FTE). The following code, courtesy of Flash Player engineer Jeff Mott, sets the font size of a TextElement to 300.

var ef:ElementFormat = new ElementFormat();
ef.fontSize = 300;
var te:TextElement = new TextElement ("Nice and big.", ef);
var tb:TextBlock = new TextBlock (te);
var tl:TextLine = tb.createTextLine();
tl.x = 20;
tl.y = 300;
addChild (tl);

The 127px limit will also be removed for Flash's new Text Layout Components when they are released.

Posted by moock at 01:09 AM

August 01, 2008

ActionScript 3.0 String-concatenation error gotcha

Take a look at this code:

trace(1 + + 2);  // Output: 3

It may seem strange to us, but ActionScript considers it legal. It assumes you mean:

trace(1 + (+2));

which produces the value 3.

Now take a look at this code:

// Oops! One too many + signs...
trace("Hello" +
      + " world");

Once again, ActionScript assumes you mean:

trace("Hello" +
      (+ " world"));

which yields the following somewhat misleading error:

1067: Implicit coercion of a value of type String to an unrelated type Number.	

The error occurs because ActionScript considers '(+ "world")' a datatype-conversion operation, not a String concatenation. The fix is simple: remove one + sign.

trace("Hello"
      + " world");

But why does ActionScript consider '(+ "world")' a datatype conversion? Francis Cheng explains here...

Posted by moock at 04:39 AM