Improve handling of fonts (fixes openfl/openfl#537)
This commit is contained in:
@@ -25,18 +25,24 @@ class Font {
|
||||
public var height (get, null):Int;
|
||||
public var name (default, null):String;
|
||||
public var numGlyphs (get, null):Int;
|
||||
public var src:Dynamic;
|
||||
public var underlinePosition (get, null):Int;
|
||||
public var underlineThickness (get, null):Int;
|
||||
public var unitsPerEM (get, null):Int;
|
||||
|
||||
@:noCompletion private var __fontPath:String;
|
||||
@:noCompletion private var __handle:Dynamic;
|
||||
|
||||
|
||||
public function new (name:String = null) {
|
||||
|
||||
this.name = name;
|
||||
|
||||
if (__fontPath != null) {
|
||||
|
||||
__fromFile (__fontPath);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -44,8 +50,8 @@ class Font {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
|
||||
if (__handle == null) throw "Uninitialized font handle.";
|
||||
return lime_font_outline_decompose (__handle, 1024 * 20);
|
||||
if (src == null) throw "Uninitialized font handle.";
|
||||
return lime_font_outline_decompose (src, 1024 * 20);
|
||||
|
||||
#else
|
||||
|
||||
@@ -77,7 +83,7 @@ class Font {
|
||||
public function getGlyph (character:String):Glyph {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
return lime_font_get_glyph_index (__handle, character);
|
||||
return lime_font_get_glyph_index (src, character);
|
||||
#else
|
||||
return -1;
|
||||
#end
|
||||
@@ -88,7 +94,7 @@ class Font {
|
||||
public function getGlyphs (characters:String = #if (display && haxe_ver < "3.2") "" #else "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^`'\"/\\&*()[]{}<>|:;_-+=?,. " #end):Array<Glyph> {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
return lime_font_get_glyph_indices (__handle, characters);
|
||||
return lime_font_get_glyph_indices (src, characters);
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
@@ -99,7 +105,7 @@ class Font {
|
||||
public function getGlyphMetrics (glyph:Glyph):GlyphMetrics {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
var value = lime_font_get_glyph_metrics (__handle, glyph);
|
||||
var value = lime_font_get_glyph_metrics (src, glyph);
|
||||
var metrics = new GlyphMetrics ();
|
||||
|
||||
metrics.advance = new Vector2 (value.horizontalAdvance, value.verticalAdvance);
|
||||
@@ -119,12 +125,12 @@ class Font {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
|
||||
lime_font_set_size (__handle, fontSize);
|
||||
lime_font_set_size (src, fontSize);
|
||||
|
||||
var bytes = new ByteArray ();
|
||||
bytes.bigEndian = false;
|
||||
|
||||
if (lime_font_render_glyph (__handle, glyph, bytes)) {
|
||||
if (lime_font_render_glyph (src, glyph, bytes)) {
|
||||
|
||||
bytes.position = 0;
|
||||
|
||||
@@ -173,12 +179,12 @@ class Font {
|
||||
|
||||
}
|
||||
|
||||
lime_font_set_size (__handle, fontSize);
|
||||
lime_font_set_size (src, fontSize);
|
||||
|
||||
var bytes = new ByteArray ();
|
||||
bytes.bigEndian = false;
|
||||
|
||||
if (lime_font_render_glyphs (__handle, glyphList, bytes)) {
|
||||
if (lime_font_render_glyphs (src, glyphList, bytes)) {
|
||||
|
||||
bytes.position = 0;
|
||||
|
||||
@@ -320,11 +326,11 @@ class Font {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
|
||||
__handle = lime_font_load (bytes);
|
||||
src = lime_font_load (bytes);
|
||||
|
||||
if (__handle != null) {
|
||||
if (src != null && name == null) {
|
||||
|
||||
name = lime_font_get_family_name (__handle);
|
||||
name = lime_font_get_family_name (src);
|
||||
|
||||
}
|
||||
|
||||
@@ -339,11 +345,11 @@ class Font {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
|
||||
__handle = lime_font_load (__fontPath);
|
||||
src = lime_font_load (__fontPath);
|
||||
|
||||
if (__handle != null) {
|
||||
if (src != null && name == null) {
|
||||
|
||||
name = lime_font_get_family_name (__handle);
|
||||
name = lime_font_get_family_name (src);
|
||||
|
||||
}
|
||||
|
||||
@@ -362,7 +368,7 @@ class Font {
|
||||
private function get_ascender ():Int {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
return lime_font_get_ascender (__handle);
|
||||
return lime_font_get_ascender (src);
|
||||
#else
|
||||
return 0;
|
||||
#end
|
||||
@@ -373,7 +379,7 @@ class Font {
|
||||
private function get_descender ():Int {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
return lime_font_get_descender (__handle);
|
||||
return lime_font_get_descender (src);
|
||||
#else
|
||||
return 0;
|
||||
#end
|
||||
@@ -384,7 +390,7 @@ class Font {
|
||||
private function get_height ():Int {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
return lime_font_get_height (__handle);
|
||||
return lime_font_get_height (src);
|
||||
#else
|
||||
return 0;
|
||||
#end
|
||||
@@ -395,7 +401,7 @@ class Font {
|
||||
private function get_numGlyphs ():Int {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
return lime_font_get_num_glyphs (__handle);
|
||||
return lime_font_get_num_glyphs (src);
|
||||
#else
|
||||
return 0;
|
||||
#end
|
||||
@@ -406,7 +412,7 @@ class Font {
|
||||
private function get_underlinePosition ():Int {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
return lime_font_get_underline_position (__handle);
|
||||
return lime_font_get_underline_position (src);
|
||||
#else
|
||||
return 0;
|
||||
#end
|
||||
@@ -417,7 +423,7 @@ class Font {
|
||||
private function get_underlineThickness ():Int {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
return lime_font_get_underline_thickness (__handle);
|
||||
return lime_font_get_underline_thickness (src);
|
||||
#else
|
||||
return 0;
|
||||
#end
|
||||
@@ -428,7 +434,7 @@ class Font {
|
||||
private function get_unitsPerEM ():Int {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
return lime_font_get_units_per_em (__handle);
|
||||
return lime_font_get_units_per_em (src);
|
||||
#else
|
||||
return 0;
|
||||
#end
|
||||
|
||||
@@ -51,7 +51,7 @@ class TextLayout {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
|
||||
if (__handle != null && text != null && text != "" && font != null && font.__handle != null) {
|
||||
if (__handle != null && text != null && text != "" && font != null && font.src != null) {
|
||||
|
||||
if (__buffer == null) {
|
||||
|
||||
@@ -60,7 +60,7 @@ class TextLayout {
|
||||
|
||||
}
|
||||
|
||||
lime_text_layout_position (__handle, font.__handle, size, text, __buffer);
|
||||
lime_text_layout_position (__handle, font.src, size, text, __buffer);
|
||||
|
||||
if (__buffer.length > 4) {
|
||||
|
||||
|
||||
@@ -68,18 +68,21 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
#if openfl
|
||||
::if (assets != null)::
|
||||
::foreach assets::::if (type == "font")::openfl.text.Font.registerFont (__ASSET__::flatName::);::end::
|
||||
::foreach assets::::if (type == "font")::openfl.text.Font.registerFont (__ASSET__OPENFL__::flatName::);::end::
|
||||
::end::::end::
|
||||
#end
|
||||
|
||||
#if (windows || mac || linux)
|
||||
|
||||
var useManifest = false;
|
||||
::if (assets != null)::::foreach assets::::if (embed)::
|
||||
::if (assets != null)::::foreach assets::::if (type == "font")::
|
||||
className.set ("::id::", __ASSET__::flatName::);
|
||||
type.set ("::id::", AssetType.$$upper(::type::));
|
||||
::else::::if (embed)::
|
||||
className.set ("::id::", __ASSET__::flatName::);
|
||||
type.set ("::id::", AssetType.$$upper(::type::));
|
||||
::else::useManifest = true;
|
||||
::end::::end::::end::
|
||||
::end::::end::::end::::end::
|
||||
|
||||
if (useManifest) {
|
||||
|
||||
@@ -255,23 +258,30 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
public override function getFont (id:String):Font {
|
||||
|
||||
#if (flash || html5)
|
||||
#if flash
|
||||
|
||||
var src = Type.createInstance (className.get (id), []);
|
||||
var font = new Font (src.fontName);
|
||||
font.src = src;
|
||||
|
||||
return font;
|
||||
|
||||
#elseif html5
|
||||
|
||||
return cast (Type.createInstance (className.get (id), []), 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 {
|
||||
if (className.exists (id)) {
|
||||
|
||||
var fontClass = className.get (id);
|
||||
return cast (Type.createInstance (fontClass, []), Font);
|
||||
|
||||
} else {
|
||||
|
||||
return Font.fromFile (path.get (id));
|
||||
|
||||
//}
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
@@ -664,17 +674,14 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
#elseif html5
|
||||
|
||||
#if openfl
|
||||
::foreach assets::::if (type == "font")::@:keep #if display private #end class __ASSET__::flatName:: extends openfl.text.Font { public function new () { super (); fontName = "::fontName::"; } } ::end::
|
||||
::foreach assets::::if (type == "font")::@:keep #if display private #end class __ASSET__::flatName:: extends lime.text.Font { public function new () { super (); name = "::fontName::"; } } ::end::
|
||||
::end::
|
||||
#end
|
||||
|
||||
#else
|
||||
|
||||
#if openfl
|
||||
::if (assets != null)::::foreach assets::::if (type == "font")::@:keep class __ASSET__::flatName:: extends openfl.text.Font { public function new () { super (); __fontPath = "::targetPath::"; fontName = "::fontName::"; }}
|
||||
::end::::end::::end::
|
||||
#end
|
||||
::if (assets != null)::::foreach assets::::if (!embed)::::if (type == "font")::@:keep class __ASSET__::flatName:: extends lime.text.Font { public function new () { __fontPath = "::targetPath::"; name = "::fontName::"; super (); }}
|
||||
#if openfl @:keep class __ASSET__OPENFL__::flatName:: extends openfl.text.Font { public function new () { __fontPath = "::targetPath::"; name = "::fontName::"; super (); }} #end
|
||||
::end::::end::::end::::end::
|
||||
|
||||
#if (windows || mac || linux)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user