Rendering text in AS3 using FTE (Flash Text Engine)

Since I think devlopers’ attention will focus on mobile game developement during these days, let’s see a feature that will let you use fonts in a way that will successfully adapt across devices, as introduced in the Authoring mobile Flash content for multiple screen sizes guide.

FTE (Flash Text Engine)

The FTE provides low-level support for sophisticated control of text metrics, formatting, and bidirectional text. It was primarily designed as a foundation for developers to create text-handling components, but in this simple example I’ll use it like a normal text field to see if I can improve its readability.

This example is intended to be ran on Flash Player 10

package {
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.engine.*;
	public class fte extends Sprite {
		public function fte():void {
			// FTE TEXT
			var font_description:FontDescription = new FontDescription();
			font_description.fontName="Verdana";
			font_description.fontWeight=FontWeight.BOLD;
			font_description.fontPosture=FontPosture.ITALIC;
			font_description.renderingMode=RenderingMode.CFF;
			font_description.locked=true;
			var element_format:ElementFormat=new ElementFormat(font_description);
			element_format.fontSize=40;
			element_format.kerning=Kerning.ON;
			element_format.color=0x000000;
			element_format.alpha=1;
			var text_element:TextElement=new TextElement("Hello Flash World",element_format);
			var text_block:TextBlock = new TextBlock();
			text_block.content=text_element;
			var text_line:TextLine=text_block.createTextLine(null,500);
			text_line.x=20;
			text_line.y=40;
			addChild(text_line);
			// NORMAL TEXT
			var text_field:TextField=new TextField();
			var text_format:TextFormat = new TextFormat();
			text_format.color=0x000000;
			text_format.size=40;
			text_format.bold=true;
			text_format.font="Verdana";
			text_format.italic=true;
			text_field.x=18;
			text_field.y=30;
			text_field.width=460;
			text_field.height=60;
			text_field.text="Hello Flash World";
			text_field.setTextFormat(text_format);
			addChild(text_field);
		}
	}
}

Line 5: importing the required library

Lines 9-14: using FontDescription class to represent the font. As you can see in the official docs, locking the font (line 14) and setting the rendering mode to CFF (line 13) shoud make the text more legible, especially at small sizes

Lines 15-19: using ElementFormat class to set formatting information. As described in the docs the only interesting feature I did not use was the baseline alignment. Turning off the kerning (line 17) should improve readability

Line 20: using TextElement class to declare the string of formatted text I am about to display. No fancy features here according to the docs

Lines 21-22: TextBlock class is the renderer itself, but it’s more about formatting and management than rendering, according to the docs

Lines 23-25: using TextLine class to display the content. According to the docs, it just has a lot of read only properties.

Lines 28-40: doing the same thing with old text fields.

And this is the result:

The upper line is made using FTE, the lower one with normal text fields.

I have to say I don’t see noticeable improvements, anyway it will introduce you into FTE world.

Download the source code.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers