Cut the Drama. Flash is Here to Stay.

I really wish that Steve Jobs would quit misinforming the public. It amazes me that Apple and Adobe’s childish feud is even news worthy. I’ve had a number of people tell me that I should start looking for a new profession because HTML5 is the future and Flash is dead (link). All of them iPhone users and each of them were just tech savvy enough not to know what the hell they were talking about. Here is ten minutes worth of research to show Steve Jobs’ opinion on the matter is a bit more like a leap of blind faith.

What does this graph tell us?
Well, even through a recession, ActionScript job growth has held strong. Objective C on the other hand looks very unstable.

And this one?
Nobody seems to be able to make up their mind about how much to pay Objective C programmers. Is it worth more than HTML or not? Would you work in a development language that had the potential for a 50% pay cut if you lost your job?


http://www.hiten.net/wilberwind/wordpress/?p=826

How about this chart?
I would like someone to point out the web development language that died. Anyone? Oh wait… none of them have ever died! When somebody comes out with something better, the competition does the same. Not to mention that it took AJAX nearly ten years to catch on. As much as some people hate PHP, it is still around. As much as some people hate Microsoft, .NET is still kicking. As much as Steve Jobs hates Adobe, Flash is here to stay.

Do I hate HTML5 and think it will fail? No. I hope it succeeds. The reason I code ActionScript is because it is widely supported and cutting edge. HTML4 wasn’t enough for me. When HTML5 has a 3D engine, physics support, frameworks, blogs of examples, full browser support, and a great IDE, I’ll strongly consider it. Until then, please don’t tell me I should look for a new profession.

*Update 02/02/2010 @ 7am in response to a comment below*

Analysis:
Perl seems to be holding strong. I wish the data went back further, was there ever a significant decline? They still have a pretty big market for jobs with a steady incline. Coldfusion jobs have not gone up but have also not really gone down over the past 5 years. That’s pretty good job security for ‘dead’ languages.

Looks like Silverlight has seen nothing but positive growth over the past few years. Was it two years ago Microsoft and .NET developers where predicting the death of Flash due to Silverlight? Looks like there is enough market for both. If the web wants interactive maybe there is room for Flash, HTML5, and Silverlight to all grow over the next 5 years. Based on the data at hand, that would be my prediction.

Fix Disappearing Fonts Caused by SWC’s

Have you ever had that feeling that something is very wrong in your project? Maybe that some paranormal force is working against you? Well, when embedded text starts disappearing after adding a SWC to a project, that’s the feeling I used to get.

Problem:
Let me start by saying SWC’s are great. I’ve been using them more and more to include third party API’s, utilities and assets. There is one problem, embedding a font into an asset in a SWC will override the internally defined fonts in a project. Why is that bad? Well lets say you have a static text field in your SWC using Georgia and a TextField instance in your Flex project using Georgia. No text will show up in your Flex TextField. Check out this example (notice it starts invisible):

SWC Text Embed Conflict:

Example of Conflicting Fonts

Get Adobe Flash player

(switch between MyGeorgia and Georgia fonts to see the disappearing text)

Full source code can be found here.

Solution:
Define your embed tags in ActionScript with a different fontFamily and fontName. So, if your font is Georgia than name it EmbedGeorgia. This will allow your TextField to reference the correct class definition. In my experience the SWC will always win if there is a conflict.

[Embed(source="/assets/fonts/georgia.ttf", advancedAntiAliasing="true", fontFamily="MyGeorgia", 
	fontName="MyGeorgia", fontWeight="normal", mimeType="application/x-font" )]
private var MyGeorgiaFont:Class;
public static const MY_GEORGIA:String = "MyGeorgia";

rather than…

// This will conflict with Georgia references in a SWC!!!
[Embed(source="/assets/fonts/georgia.ttf", advancedAntiAliasing="true", fontFamily="Georgia", 
	fontName="Georgia", fontWeight="normal", mimeType="application/x-font" )]
private var GeorgiaFont:Class;
public static const GEORGIA:String = "Georgia";

It’s that easy?
Yup.

What if you have two SWC’s with Georgia embedded text?
Will they conflict with each other? Based on the example above the answer is no. Awesome!

What if my fonts are stored in a SWF file?
Than duplicate the above code but reference your SWF instead of the font in the assets folder.

Code that results in invisible text:

// This will conflict with Georgia references in a SWC!!!
[Embed(source="/assets/fonts/georgia.ttf", advancedAntiAliasing="true", fontFamily="Georgia", 
	fontName="Georgia", fontWeight="normal", mimeType="application/x-font" )]
private var GeorgiaFont:Class;
public static const GEORGIA:String = "Georgia";

private var georgiaFormat:TextFormat;
private var georgiaText:TextField;

public function ConflictingFonts()
{
	// Component from SWC which uses Georgia in a static text field
	var cc:ColorComponent = new ColorComponent();
	cc.colorPicker.addEventListener(ColorPickerEvent.CHANGE, selectColor, false, 0, true);
	cc.y = 30;
	this.addChild(cc);
	
	// Create the local TextFormat
	georgiaFormat = new TextFormat(GEORGIA, 18);
	
	// Create the local TextField (the text won't show up!)
	georgiaText = new TextField();
	georgiaText.embedFonts = true;
	georgiaText.width = 300;
	georgiaText.height = 20;
	georgiaText.defaultTextFormat = georgiaFormat;
	georgiaText.setTextFormat(georgiaFormat);
	georgiaText.text = "Hello World";
	this.addChild(georgiaText);
}

Swap out the Embed tag, update the TextFormat and you will be good to go.

Rant:

On the way to this solution I came up with a number of hacks that appeared to work only to break at the worst time. These hacks included storing a second reference of the font in the SWC (assuming you have access to the source), enumerating through the font list at runtime (which only fixes the problem in debug), and breaking apart the text into bitmaps (you don’t want to know how long that takes). Rather than stop using SWC’s I started to create more and more of them pushing towards an acceptable solution. This is what I ended up on. Hopefully it will save everyone some time and a little sanity 😉

Don’t Forget to Update Your Adobe Products

Is Flash CS4 crashing when you try to open an FLA? Are you pulling out your hair wondering why Flex 3 doesn’t auto complete BitmapData? Adobe Updater may be the answer.

Flash CS4 10.0.2

One of my co-workers called me over to troubleshoot when a folder full of FLAs were crashing Flash CS4 every time he tried to open them. He had already tried a fresh install of the software and had all of the necessary fonts. So why did it crash? Well it turns out that Flash CS4 had a bug where it would crash when embedded fonts are used.

“FLA crash on open when fonts loaded.” –Glavan on Flash (detailed list of what was fixed)

After running Adobe Updater everything worked great!

Flex Builder 3.0.2

How about Flex Builder? I recently installed Windows 7 with a fresh copy of Flex Builder 3 Professional. Unfortunately I pulled the install from one of my external hard drives and it was version 3.0.1 rather than 3.0.2. A major bug with version 3.0.1 using SDK 3.3 and higher is that BitmapData does not auto complete. After trying multiple versions of the SDK I finally found the bug report on Adobe Bugs citing that the problem was fixed in version 3.0.2.

After running Adobe Updater everything worked great!

See the common theme here? I tend to disable most of the automatic software updates since they are usually very annoying. After my experiences over the past week I will be checking Adobe Updater much more frequently! Always update after a fresh install.

Base64 Encoding BitmapData

Turn your BitmapData into a string and back! Great for storing images in XML, Databases, or transmitting them over a network. This post will cover Bitmap Encoding, PNG Encoding and JPG Encoding along with the pros and cons for each.

Bitmap Encoding – (link)

// Encode
var encoded:String = BitmapEncoder.encodeBase64(bitmap.bitmapData);
	
// Decode
var bd:BitmapData = BitmapEncoder.decodeBase64(encoded);

Advantages:
Keep transparency
Fastest Method
Does not require a Loader

Disadvantages:
Largest Filesize
Will only work with images up to a certain size


PNG Encoding – (link)

// Encode  	
var ba:ByteArray = PNGEncoder.encode(bitmap.bitmapData);
var encoded:String = Base64.encodeByteArray(ba);

// Decode  	
var decoded:ByteArray = Base64.decodeToByteArray(encoded);
var loader:Loader = new Loader();
loader.loadBytes(decoded);

Advantages:
Keep transparency
Smaller filesize than Bitmap Encoding

Disadvantages:
Slower than Bitmap Encoding
Larger than JPG Encoding
Requires Loader

JPG Encoding – (link)

// Encode
var jpgEncoder:JPGEncoder = new JPGEncoder();
var ba:ByteArray = jpgEncoder.encode(bitmap.bitmapData);
var encoded:String = Base64.encodeByteArray(ba);

// Decode
var decoded:ByteArray = Base64.decodeToByteArray(encoded);
var loader:Loader = new Loader();
loader.loadBytes(decoded);

Advantages:
Smallest Filesize
Variable Quality

Disadvantages:
Slower than Bitmap Encoding
No Transparency
Requires Loader