Fix Textfields JSFL

Today Seb was having some issues when dealing with embedded fonts – read his post. He came up with some ActionScript ways to get around it. Well another way would be to use JSFL. A couple of my team members at Fuel Industries wrote the following JSFL script. It goes through your library and searches out any dynamic textfields. Once it finds one it makes sure it’s on a whole pixel, turns the auto kern off, embeds “UpperCase, LowerCase, Numerals, Punctuation” characters, and makes it not selectable. But you could make it do pretty much anything you can through JSFL.

fl.outputPanel.clear();
scanLibrary(fl.getDocumentDOM().library);
 
 
 
function getIsWholeInt(n)
{
	var s = String(n);
	if(s.indexOf(".") != -1)
	{
		return false;
	}
	else
	{
		return true;
	}
}
 
function round(n)
{
	var s = String(n);
	var a = s.split(".");
	var num = parseInt(a[0]);
	var dec = parseInt(a[1].substr(0, 1));
	if(dec >= 5)
	{
		num++;
	}
	return num;
}
 
/**
 * Scans the supplied flash library for linked classes and makes sure the textfields are set up properly
 * @param 	library		A flash library to scan.
 */
function scanLibrary(library)
{
    var items = library.items;
    var item;
	var replaceCount = 0;
 
    for( var i = 0; i < items.length; i++ )
    {
        item = items[i];
		if (item.itemType == 'movie clip') 
		{
			var timeline = item.timeline;
			var h = timeline.layerCount;
 
			library.selectItem(item);
			library.editItem();
 
			while(h--)
			{
				var k = timeline.layers[h].frameCount;
 
				while(k--)
				{
					var j = timeline.layers[h].frames[k].elements.length;
 
					while(j--)
					{
						var elems = timeline.layers[h].frames[k].elements;
						var p = elems.length;
 
						while(p--)
						{
							if(elems[p].elementType == "text" && (elems[p].textType == "dynamic" || elems[p].textType == "input"))
							{
								//Change the Static Text Fields to Dynamic
								if(elems[p].textType == "static") {
									elems[p].textType = "dynamic";
								}
 
								//Handle all Dynamic TextFields
								if(elems[p].textType == "dynamic") {
									//Remove the ability to be selectable
									elems[p].selectable = false;
								}
 
								//Handle all Dynamic or Input Textfields
 
								//Remove the auto kern attribute
								elems[p].setTextAttr('autoKern', false);
								//Embed fonts UpperCase, LowerCase, Numerals, Punctuation
								elems[p].embedRanges = "1|2|3|4";
 
								//Pop onto whole pixel
								var matX = elems[p].matrix;
								var x = matX.tx;
								if(!getIsWholeInt(x))
								{
									matX.tx = round(x);
									elems[p].matrix = matX;
								}
 
								var matY = elems[p].matrix;
								var y = matY.ty;
								if(!getIsWholeInt(y))
								{
									matY.ty = round(y);
									elems[p].matrix = matY;
								}
 
							}
						}
 
					}
 
				}
			}
		}
    }
}
Continue reading » · Rating: · Written on: 08-24-09 · 4 Comments »

AS3 FP10 FileReference.save bug

While playing with FileReference.save today in Flash Player 10 I found a REALLY REALLY BIG BUG. It seems that if you go to overwrite a file it appends it’s contents instead of replacing.

1. Copy and paste the code below into a blank fla in CS4.
2. Compile the swf and save a file.
3. Close the swf
4. repeat step 2
5. Open the test.txt file.

I am on OSX and haven’t tested on a PC.

var str:String = "12345";
var ba:ByteArray = new ByteArray();
ba.writeUTFBytes( str );
 
var fr:FileReference = new FileReference();
fr.save(ba, "test.txt");

I did a quick search on the Adobe bug base and found some related issues from 2008 but they where for downloading files from the net.

Continue reading » · Rating: · Written on: 08-04-09 · 4 Comments »