Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Additional glyphs #2946

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions flixel/graphics/frames/FlxBitmapFont.hx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ class FlxBitmapFont extends FlxFramesCollection
*/
var frame:FlxFrame;

/**
* Lookup tables for additional glyphs characters.
*/
public var lookupTable:Map<String, String> = [];
public var revLookupTable:Map<String, String> = [];

/**
* Special characters start index.
*/
public var specialStart:Int = 500;
Comment on lines +85 to +91
Copy link
Member

@Geokureli Geokureli Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think this can be implemented without needing a special start, secondly, an ability to parse tokens in text strings to denote special characters seems helpful on it's own wether that maps to some large unicode index, or not


/**
* Creates a new bitmap font using specified bitmap data and letter input.
*/
Expand Down Expand Up @@ -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);
Comment on lines +662 to +666
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a big part of it, with multiatlases we can simply take frames from one atlas and add them to another without needing to create a new bitmap containing every frame

Also line 666 breals flash CI, by skipping the unique arg

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++;
}
}

}
14 changes: 14 additions & 0 deletions flixel/text/FlxBitmapText.hx
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ class FlxBitmapText extends FlxSprite

function set_text(value:String):String
{
value = parseExtraGlyphs(value);
if (value != text)
{
text = value;
Expand All @@ -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;
Expand Down
Loading