A graphical text class.

October 13, 2008 at 9:10 am Leave a comment

This is a class that didn’t quite make the cut into something I’m working on – so I’ve put it on my blog.  I’m sure someone else will enjoy it.   It’s a graphical writing class that utilises the graphics api to draw simple letter shapes like this…

For example, to produce the above result, call it with the following parameters…

new Write(this,16,128,’abcdefghijklmnopqrstuvwxyz’,3);

new Write(this,16,150,’abcdefghijklmnopqrstuvwxyz’);

 

Enjoy.

package asfiles {

	import flash.display.Sprite;

	public class Write extends Sprite {

		protected static const alphabet:Array=[

			[[0,4, 0,2, 2,0, 4,2, 4,4],[0,2, 4,2]],
			[[0,0, 3,0, 4,1, 4,2, 0,2, 3,2, 4,3, 4,4, 0,4, 0,0]],
			[[4,0, 1,0, 0,1, 0,3, 1,4, 4,4]],
			[[0,0, 3,0, 4,1, 4,3, 3,4, 0,4, 0,0]],
			[[4,0, 0,0, 0,4, 4,4],[0,2, 3,2]],
			[[4,0, 0,0, 0,4],[0,2, 3,2]],
			[[4,0, 1,0, 0,1, 0,3, 1,4, 4,4, 4,2, 2,2]],
			[[0,0, 0,4],[4,0, 4,4],[0,2, 4,2]],
			[[2,0, 2,4],[1,0, 3,0],[1,4, 3,4]],
			[[3,0, 3,3, 2,4, 1,4, 0,3],[2,0, 4,0]],
			[[0,0, 0,4],[4,0, 0,2, 4,4]],
			[[0,0, 0,4, 4,4]],
			[[0,4, 0,0, 2,2, 4,0, 4,4]],
			[[0,4, 0,0, 4,4, 4,0]],
			[[1,0, 3,0, 4,1, 4,3, 3,4, 1,4, 0,3, 0,1, 1,0]],
			[[0,4, 0,0, 4,0, 4,2, 0,2]],
			[[1,0, 3,0, 4,1, 4,3, 3,4, 1,4, 0,3, 0,1, 1,0],[3,3, 5,5]],
			[[0,4, 0,0, 4,0, 4,2, 0,2],[2,2, 4,4]],
			[[4,0, 1,0, 0,1, 0,2, 4,2, 4,3, 3,4, 0,4]],
			[[0,0, 4,0],[2,0, 2,4]],
			[[0,0, 0,3, 1,4, 3,4, 4,3, 4,0]],
			[[0,0, 0,2, 2,4, 4,2, 4,0]],
			[[0,0, 0,4, 2,2, 4,4, 4,0]],
			[[0,0, 4,4],[0,4, 4,0]],
			[[0,0, 2,2, 4,0],[2,2, 2,4]],
			[[0,0, 4,0, 0,4, 4,4],[1,2, 3,2]]

		]

		private static const kscale:Number=1.4;
		private static const kcolour:uint=0x999999;
		private const lettergap:int=6;

		public function Write(screen:Sprite,xx:int,yy:int,txt:String,scale:Number=kscale,colour:uint=kcolour) {
			screen.addChild(this);x=xx;y=yy;
			graphics.clear();
			graphics.lineStyle(1,colour);
			write(txt,scale);
		}

		private function write(txt:String,scale:Number):void {
			var pos:int=0;
			var letter:Array;
			txt=txt.toLowerCase();
			for (var i:int=0;i<txt.length;i++) if (txt.substr(i,1)!=' ') {
				letter=alphabet[txt.charCodeAt(i)-'a'.charCodeAt(0)];
				for (var j:int=0;j<letter.length;j++) {
					graphics.moveTo(pos+letter[j][0]*scale,letter[j][1]*scale);
					for (var k:int=2;k<letter[j].length;k+=2) {
						graphics.lineTo(pos+letter[j][k]*scale,letter[j][k+1]*scale);
						}
					}
				pos+=lettergap*scale;
				} else pos+=lettergap*scale;
		}

	}
}
Advertisements

Entry filed under: Adobe AIR.

Will AIR developers get sued? “Blasphemy! Burn the Flex-hating heretic!”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


  RSS feed          View Daniel Freeman's LinkedIn profileView my profile

Add to Technorati Favorites

October 2008
M T W T F S S
« Sep   Nov »
 12345
6789101112
13141516171819
20212223242526
2728293031  

%d bloggers like this: