
Here's a set of classes that wrap firetype by MaxDidIt. You have to include the firetype .swc in the project.
FiretypeText.as
- Code: Select all
package {
import de.maxdidit.hardware.text.cache.HardwareCharacterCache;
import de.maxdidit.hardware.text.format.TextAlign;
import de.maxdidit.hardware.text.HardwareText;
import de.maxdidit.hardware.text.renderer.SingleGlyphRendererFactory;
import flash.display3D.Context3DBlendFactor;
import flash.geom.Matrix3D;
import flash.geom.Vector3D;
import io.axel.Ax;
import io.axel.AxU;
import io.axel.base.AxEntity;
import io.axel.base.AxPoint;
import io.axel.render.AxColor;
public class FiretypeText extends AxEntity {
public var scroll:AxPoint;
public var zooms:Boolean;
public var scale:AxPoint;
public var origin:AxPoint;
public var screen:AxPoint;
public var align:uint;
protected var _color:AxColor;
protected var matrix:Matrix3D;
public var hwtext:HardwareText;
protected static var cache:HardwareCharacterCache;
public function FiretypeText(x:Number = 0, y:Number = 0, font:FiretypeFont = null, text:String = "", width:Number = 0, size:Number = 10, detail:Number = 100, align:uint = TextAlign.LEFT) {
super(x, y);
if (align != TextAlign.LEFT && width == 0)
throw new Error("Center and Right alignment require specifying the width parameter");
origin = new AxPoint(0, 0);
scale = new AxPoint(1, 1);
scroll = new AxPoint(1, 1);
screen = new AxPoint(x - (Ax.camera.x + Ax.camera.offset.x) * scroll.x, y - (Ax.camera.y + Ax.camera.offset.y) * scroll.y);
zooms = true;
matrix = new Matrix3D();
if(cache == null)
cache = new HardwareCharacterCache(new SingleGlyphRendererFactory(Ax.context));
hwtext = new HardwareText(Ax.context, cache);
hwtext.width = width;
hwtext.scaleX = 0.001 * size;
hwtext.scaleY = -0.001 * size;
if(font != null)
hwtext.standardFormat.font = font.hwfont;
hwtext.standardFormat.textAlign = this.align = align;
hwtext.standardFormat.vertexDistance = detail;
hwtext.text = text;
hwtext.calculateTransformations(Ax.camera.projection, true);
hwtext.update();
this.width = (width == 0 ? hwtext.textWidth : width);
this.height = hwtext.textHeight;
}
public function noScroll():FiretypeText {
scroll.x = scroll.y = 0;
return this;
}
public function centerOrigin():FiretypeText {
origin.x = width / 2;
origin.y = height / 2;
return this;
}
override public function get left():Number {
return x;
}
override public function get top():Number {
return y;
}
override public function get right():Number {
return x + width * scale.x;
}
override public function get bottom():Number {
return y + height * scale.y;
}
public function get color():AxColor {
return _color;
}
public function set color(value:AxColor):void {
_color = value;
hwtext.standardFormat.color = value.hex;
}
public function get text():String {
return hwtext.text;
}
public function set text(value:String):void {
hwtext.text = value;
}
override public function dispose():void {
origin = null;
scroll = null;
scale = null;
hwtext = null;
super.dispose();
}
override public function update():void {
super.update();
hwtext.x = screen.x = x - (Ax.camera.x + Ax.camera.offset.x) * scroll.x;
hwtext.y = screen.y = y - (Ax.camera.y + Ax.camera.offset.y) * scroll.y;
}
override public function draw():void {
matrix.identity();
var alignOffset:int;
if(align == TextAlign.RIGHT) alignOffset = width * scale.x;
else if(align == TextAlign.CENTER) alignOffset = width / 2 * scale.x;
else alignOffset = 0;
var sx:Number = x - offset.x + parentOffset.x;
var sy:Number = y - offset.y + parentOffset.y;
var scalex:Number = scale.x;
var scaley:Number = scale.y;
var cx:Number = Ax.camera.position.x * scroll.x + Ax.camera.effectOffset.x;
var cy:Number = Ax.camera.position.y * scroll.y + Ax.camera.effectOffset.y;
if (scalex != 1 || scaley != 1) {
matrix.appendTranslation(-origin.x, -origin.y, 0);
matrix.appendScale(scalex, scaley, 1);
matrix.appendTranslation(origin.x + Math.round(sx - cx + AxU.EPSILON + alignOffset), origin.y + Math.round(sy - cy + AxU.EPSILON), 0);
} else {
matrix.appendTranslation(Math.round(sx - cx + AxU.EPSILON + alignOffset), Math.round(sy - cy + AxU.EPSILON), 0);
}
matrix.append(zooms ? Ax.camera.projection : Ax.camera.baseProjection);
hwtext.calculateTransformations(matrix, true);
Ax.context.setBlendFactors(Context3DBlendFactor.ONE, Context3DBlendFactor.ZERO); // TODO: Alpha not supported, test against antialiasing
Ax.context.setTextureAt(0, null);
Ax.shader = null; // Unknown shader used
hwtext.cache.render();
}
}
}
FiretypeFont.as
- Code: Select all
package {
import de.maxdidit.hardware.font.HardwareFont;
import de.maxdidit.hardware.font.parser.OpenTypeParser;
import flash.utils.ByteArray;
public class FiretypeFont {
protected static var parser:OpenTypeParser;
public var hwfont:HardwareFont;
public function FiretypeFont(font:Class) {
if(parser == null)
parser = new OpenTypeParser();
hwfont = parser.parseFont(new font() as ByteArray);
}
}
}
Usage:
- Code: Select all
// Embed the font
[Embed(source = "../assets/fonts/Font.ttf", mimeType="application/octet-stream")]
public static const FONT_DATA:Class;
// Initialize the font
var font:FiretypeFont = new FiretypeFont(FONT_DATA);
// Create text
var text:FiretypeText = new FiretypeText(100, 100, font, "Hello world!");