From cd9df9ea4a5f2373fb4ca70d036d4bcfef626601 Mon Sep 17 00:00:00 2001 From: Tim I Hely Date: Thu, 19 Oct 2023 14:02:31 -0500 Subject: [PATCH 1/2] Adding `appendGlyphs` to `FlxBitmapFont` --- flixel/graphics/frames/FlxBitmapFont.hx | 58 +++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/flixel/graphics/frames/FlxBitmapFont.hx b/flixel/graphics/frames/FlxBitmapFont.hx index 76cb2316c1..4540eddc44 100644 --- a/flixel/graphics/frames/FlxBitmapFont.hx +++ b/flixel/graphics/frames/FlxBitmapFont.hx @@ -79,6 +79,17 @@ class FlxBitmapFont extends FlxFramesCollection */ var frame:FlxFrame; + /** + * Lookup tables for additional glyphs characters. + */ + public var lookupTable:Map = []; + public var revLookupTable:Map = []; + + /** + * Special characters start index. + */ + public var specialStart:Int = 500; + /** * Creates a new bitmap font using specified bitmap data and letter input. */ @@ -635,4 +646,51 @@ class FlxBitmapFont extends FlxFramesCollection font.updateSourceHeight(); return font; } + + /* + * Adds additional glyphs to this font. + * + * @param Frames Additional glyphs frames. + * @param StartingCharacter Starting character code for additional glyphs. + */ + public function appendGlyphs(Frames:FlxFramesCollection, StartingCharacter:Int = 500):Void + { + specialStart = StartingCharacter; + + var offY:Int = parent.height; + + var tmpBmp:BitmapData = new BitmapData(Math.ceil(Math.max(parent.width, Frames.parent.width)), parent.height + Frames.parent.height, true, 0x00000000); + tmpBmp.copyPixels(parent.bitmap, parent.bitmap.rect, point); + tmpBmp.copyPixels(Frames.parent.bitmap, Frames.parent.bitmap.rect, new Point(0, parent.height)); + + parent = FlxGraphic.fromBitmapData(tmpBmp, parent.key + "+" + Frames.parent.key, true); + parent.persist = true; + parent.destroyOnNoUse = false; + + var offset:FlxPoint; + + var currentCode:Int = StartingCharacter; + var charName:String; + + for (frame in Frames.frames) + { + frame.frame.y += offY; + offset = FlxPoint.get(0, Math.min(4, (lineHeight / 2) - (frame.frame.height / 2))); + var w:Float = frame.sourceSize.x; + var h:Float = frame.sourceSize.y; + w += (offset.x > 0) ? offset.x : 0; + h += (offset.y > 0) ? offset.y : 0; + frame.sourceSize.set(w, h); + frame.offset.addPoint(offset); + charName = new UnicodeBuffer().addChar(currentCode).toString(); + lookupTable.set(frame.name, charName); + revLookupTable.set(charName, frame.name); + frame.name = charName; + pushFrame(frame); + charMap.set(currentCode, frame); + charAdvance.set(currentCode, Math.ceil(frame.sourceSize.x)); + currentCode++; + } + } + } From 7e8f8cff18c4e0bcef4d0fca7bf89b012ded1158 Mon Sep 17 00:00:00 2001 From: Tim I Hely Date: Thu, 19 Oct 2023 14:04:16 -0500 Subject: [PATCH 2/2] Update `FlxBitmapText.hx` to make `appendGlyphs` work --- flixel/text/FlxBitmapText.hx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/flixel/text/FlxBitmapText.hx b/flixel/text/FlxBitmapText.hx index 977d028b58..34ae9e3f69 100644 --- a/flixel/text/FlxBitmapText.hx +++ b/flixel/text/FlxBitmapText.hx @@ -570,6 +570,7 @@ class FlxBitmapText extends FlxSprite function set_text(value:String):String { + value = parseExtraGlyphs(value); if (value != text) { text = value; @@ -579,6 +580,19 @@ class FlxBitmapText extends FlxSprite return value; } + function parseExtraGlyphs(?value:String = ""):String + { + var regex:EReg = new EReg("{{([a-zA-Z0-9 ]+)}}", "g"); + + return regex.map(value, (r) -> + { + var unicode:String = font.lookupTable.get(r.matched(1)); + if (unicode == null) + unicode = "!"; + return unicode; + }); + } + function updateText():Void { var tmp:String = (autoUpperCase) ? text.toUpperCase() : text;