alptugan
9/18/2018 - 11:04 AM

Haxe-OpenFL-Flixel Font Embedding

// In project XML add the following line
<assets path="assets/fonts" rename="fonts" if="html5" />

// CREATE Font.hx Class
package;
 
import openfl.text.Font;
import openfl.Assets;
 
class Fonts {
    public static var RALEWAY_EXTRALIGHT(default, null):String;
 
    public static function init():Void {
    #if js
        RALEWAY_EXTRALIGHT = Assets.getFont("fonts/raleway-extralight.ttf").fontName;
    #else
        Font.registerFont(RalewayExtraLight);
        RALEWAY_EXTRALIGHT = (new RalewayExtraLight()).fontName;
    #end
    }
}
 
@:font("assets/fonts/raleway-extralight.ttf")
private class RalewayExtraLight extends Font {}


// In Main.hx use it like so
package;
 
import openfl.display.Sprite;
import openfl.Lib;
import openfl.text.TextField;
import openfl.text.TextFormat;
 
class Main extends Sprite {
    public function new() {
        super();
        Fonts.init();
 
        var tf = new TextFormat(Fonts.RALEWAY_EXTRALIGHT, 32);
        var t = new TextField();
        t.embedFonts = true;
        t.defaultTextFormat = tf;
        t.text = Fonts.RALEWAY_EXTRALIGHT;
        t.width = t.textWidth;
 
        this.addChild(t);
    }
}