From afcb3354a4e5d614e4cbad0c272ecb4ab2ac7c8c Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Tue, 12 Aug 2014 20:35:55 -0700 Subject: [PATCH] Add Assets.getFont, temporarily for Flash/OpenFL only --- lime/Assets.hx | 91 ++++++++++++++++++++++++--- templates/haxe/DefaultAssetLibrary.hx | 33 +++++++++- 2 files changed, 116 insertions(+), 8 deletions(-) diff --git a/lime/Assets.hx b/lime/Assets.hx index 594a53d3b..e3edac624 100644 --- a/lime/Assets.hx +++ b/lime/Assets.hx @@ -182,6 +182,69 @@ class Assets { } + /** + * Gets an instance of an embedded font + * @usage var fontName = Assets.getFont("font.ttf").fontName; + * @param id The ID or asset path for the font + * @return A new Font object + */ + public static function getFont (id:String, useCache:Bool = true):Dynamic /*Font*/ { + + initialize (); + + #if (tools && !display) + + if (useCache && cache.enabled && cache.font.exists (id)) { + + return cache.font.get (id); + + } + + var libraryName = id.substring (0, id.indexOf (":")); + var symbolName = id.substr (id.indexOf (":") + 1); + var library = getLibrary (libraryName); + + if (library != null) { + + if (library.exists (symbolName, cast AssetType.FONT)) { + + if (library.isLocal (symbolName, cast AssetType.FONT)) { + + var font = library.getFont (symbolName); + + if (useCache && cache.enabled) { + + cache.font.set (id, font); + + } + + return font; + + } else { + + trace ("[Assets] Font asset \"" + id + "\" exists, but only asynchronously"); + + } + + } else { + + trace ("[Assets] There is no Font asset with an ID of \"" + id + "\""); + + } + + } else { + + trace ("[Assets] There is no asset library named \"" + libraryName + "\""); + + } + + #end + + return null; + + } + + /** * Gets an instance of an embedded bitmap * @usage var bitmap = new Bitmap(Assets.getBitmapData("image.jpg")); @@ -965,6 +1028,13 @@ class AssetLibrary { } + public function getFont (id:String):Dynamic /*Font*/ { + + return null; + + } + + public function getImage (id:String):Image { return null; @@ -1046,6 +1116,13 @@ class AssetLibrary { } + public function loadFont (id:String, handler:Dynamic /*Font*/ -> Void):Void { + + handler (getFont (id)); + + } + + public function loadImage (id:String, handler:Image -> Void):Void { handler (getImage (id)); @@ -1098,13 +1175,13 @@ class AssetCache { public var audio:Map; public var enabled:Bool = true; public var image:Map; - public var font:Map; + public var font:Map; public function new () { audio = new Map (); - font = new Map (); + font = new Map (); image = new Map (); } @@ -1115,7 +1192,7 @@ class AssetCache { if (prefix == null) { audio = new Map (); - font = new Map (); + font = new Map (); image = new Map (); } else { @@ -1467,13 +1544,13 @@ class Assets { fields.push ({ kind: FVar(macro :String, fieldValue), name: "resourceName", access: [ APublic, AStatic ], pos: position }); var constructor = macro { - + super(); - + fontName = resourceName; - + }; - + fields.push ({ name: "new", access: [ APublic ], kind: FFun({ args: [], expr: constructor, params: [], ret: null }), pos: Context.currentPos() }); return fields; diff --git a/templates/haxe/DefaultAssetLibrary.hx b/templates/haxe/DefaultAssetLibrary.hx index 4c0fc8971..4cebd0dfa 100644 --- a/templates/haxe/DefaultAssetLibrary.hx +++ b/templates/haxe/DefaultAssetLibrary.hx @@ -224,6 +224,35 @@ class DefaultAssetLibrary extends AssetLibrary { } + public override function getFont (id:String):Dynamic /*Font*/ { + + // TODO: Complete Lime Font API + + #if openfl + #if (flash || js) + + return cast (Type.createInstance (className.get (id), []), openfl.text.Font); + + #else + + if (className.exists (id)) { + + var fontClass = className.get (id); + openfl.text.Font.registerFont (fontClass); + return cast (Type.createInstance (fontClass, []), openfl.text.Font); + + } else { + + return new openfl.text.Font (path.get (id)); + + } + + #end + #end + + } + + public override function getImage (id:String):Image { #if flash @@ -610,8 +639,10 @@ class DefaultAssetLibrary extends AssetLibrary { #elseif html5 -::foreach assets::::if (type == "font")::@:keep class __ASSET__::flatName:: extends lime.graphics.Font { public function new () { super ("::id::"); } }::end:: +#if openfl +::foreach assets::::if (type == "font")::@:keep class __ASSET__::flatName:: extends openfl.text.Font { public function new () { super (); fontName = "::id::"; } } ::end:: ::end:: +#end #elseif (windows || mac || linux)