Search Box

Google
 

Tuesday, July 24, 2007

Children will be Children, Flash will be Flash

Well the title itself suggests when things prove to be problematic while dealing it the natural way then we need to do some workarounds always. But at the end we find a solution. And here i present a problem that i faced while working one of the vital child of Flash and mine called as the TEXTBOX.

Assuming myself to be a dad in flash programming (not yet a dad in real life b'coz i am not a husband yet. I am still an Eligible bachelor you see...) i had to deal with this TextBox child of mine. Let me tell you his problems, he can either stuff his fonts with him by embedding or either stay using Device Fonts before going out to the world from his development home. But the former way suggests that his bag becomes heavy with each different type of font he will be supporting. Consider supporting Ten generic fonts that will be available in all the operating systems, then the process is to add 4 types of fonts in the library with normal,bold,italic, and bolditalic options seperately.

Elobrating on the process consider embedding Arial font which would result in four font items in the library with following linkage ids :

Arial - normal support with bold and italic option disabled.
ArialBold - Font with only Bold option.
ArialItalic - Font with only Italic support.
ArialBoldItalic - Font with Bold and Italic support.

And the max size for all the above embedded fonts items would be 72.

Now coming to the original count that we needed to support was 10 and hence the total number of embedded fonts we need would be (10 x 4 = 40). Can you imagine the size of your swf ? ok, here still i have the option of using RunTime shared font. After all this is done we can now rotate,scale the textboxes in our application.

Think once again is there a better solution to this ? Now being in the world where Flash7 is close to becoming history and flash 9 prevailing on all the browsers widely we now have options of rendering text in a better way.

One more time i am going to present you all a way to exploit the Bitmap Class for serving this purpose. Coming to the original requirement i just removed all the embedded fonts from the library and selected the Device Fonts as the text rendering method. But here comes the problem that i will not be to rotate and scale the textboxes in my application. Also using Device fonts ensures me no heavy stuffs to be stuffed to my TextBox child. He can be light and compact, the only time when i would face problem would be when rotating my child.

Hmmmm...... what are we going to do now ? Better would be to draw a bitmap object using the textbox, attach it to a movieclip and rotate that instead of transforming the textbox directly. So only while editing the text we need to show the textbox and once the editing is completed we display bitmap that is drawn to the updated version of the textbox with the transformation applied already.

Hope you understood how i dealt with my son and might also help you in a similar scenario.

Thursday, July 05, 2007

Being Greedy for finding WordWrap in Flash.

Hello All,

Again after a longtime here i come to talk about learning a way to find the breaking position in a Dynamic/Input Textbox. Wait did i mean "Finding Wordwrap positions in a Dynamic Textbox " ?. Yes you thought right. I am here to discuss an algorithm to find the positions wherever Wordwrap happens to appear in a Dynamic Textbox in flash. But beware it is very greedy and quite possible that it might eat your processor. Still check out wikipedia on wordwrap and you will find "greedy" type algorithm to be one of the ways to find wordwrap positions in general.

Okay let me not take this post further by posting code which again will not ensure that all of us might understand it.

But before you proceed brush yourself about the TextFormat class and especially the "getTextExtent" method. I hope you would have guessed what i am trying to do. Yeah, Inorder to find the breaking positions within a TextBox this method is sufficient enough to find the answer/solution.

Wait does flash not have a method to do this ? i would say a BIG NO. For that instance not only flash but any application would not have a method to find the wordwrap position. Do you know the reason why ? Since the wordwrapping is handled by the applications UI Algorithm and does not insert nor append any line formatting characters such as "\n" or "\t" etc., at the breaking point so that we can break the text using the split method using these characters.

Instead the solution that worked for me was getting the current TextFormat of the textbox we need to find wordwrap positions using getTextFormat(). Using this textformat we take the string content of the textbox and then using "WHILE" loop i move the character index one by one and store it in a temporary string "tempString" thereby also checking simultaneously the width in pixels which i get by applying MyTextFormat.getTextExtent(tempString) to see if the textWidth returned has not crossed more the width of the textbox for which we are currently checking.

Once if i find the condition of tempString width being more than the currentTextBox Width is successfully satisfied, i remove that part of the tempString from the original string using :

originalString = originalString.substring(tempString.length-1,originalString.length);
someArray.push(tempString.substring(0,tempString.length-1));
// REmember that the length has exeeded so the breaking point is the length -1 ;

//Now clear the tempString

tempString = '';


This process is carried until after all the characters are removed from my original string meaning (originalString == ''").

After this algorithm has finished running i would end up with an array containing strings. From the array length once can find the total number of lines and also the way the lines have been split up in a textbox with any font, size etc.,

Hope you understood the intention and its solution posted in this blog.

Regards,

Ashok Srinivasan.