4
I’m working on a game using the framework Haxeflixel 2D and I’m having some difficulties using bitmap fonts to render accented characters. But to make it easier I’m going to divide the question into two parts, basically what I did and what’s the problem.
Preparation and Use of the Chosen Source
I used the online tool Littera.com to create the following image for the bitmap source from True Type font "Gloria" by Peter Wiegel:
The Littera tool also generates the angelcode format (extension. fnt), which is basically an XML mapping the character codes to the subregions in the texture image from where the visual representation of the character should be obtained for rendering.
Here is an example of the Angelcode generated for the previous image:
<font>
<info face="Gloria" size="72" bold="0" italic="0" charset="" unicode="" stretchH="100" smooth="1" aa="1" padding="2,2,2,2" spacing="0,0" outline="0"/>
<common lineHeight="72" base="62" scaleW="558" scaleH="511" pages="1" packed="0"/>
<pages>
<page id="0" file="Gloria.png"/>
</pages>
<chars count="137">
<char id="97" x="2" y="2" width="27" height="31" xoffset="1" yoffset="33" xadvance="26" page="0" chnl="15"/>
<char id="98" x="2" y="35" width="26" height="45" xoffset="1" yoffset="19" xadvance="26" page="0" chnl="15"/>
<char id="99" x="2" y="82" width="22" height="32" xoffset="2" yoffset="32" xadvance="21" page="0" chnl="15"/>
. . .
Once having these two files (the image with the texture and the xml with Angelcode), the use in Haxelflixel is quite simple. Basically I loaded the font using the class Pxbitmapfont and I used it to create text in a specific text rendering platform called Flxbitmaptextfield. Below I reproduce the parts where the creation of the text takes place:
. . .
/**
* Inicialização da classe.
*/
override public function create():Void
{
super.create();
var oFont:PxBitmapFont = getFont();
m_oLabel = new FlxBitmapTextField(oFont);
m_oLabel.useTextColor = false;
m_oLabel.fontScale = 1;
m_oLabel.alignment = PxTextAlign.CENTER;
m_oLabel.antialiasing = true;
m_oLabel.text = "Créditos";
m_oLabel.x = 400;
m_oLabel.y = 300;
m_oLabel.offset.x = oFont.getTextWidth("Créditos") / 2;
m_oLabel.offset.y = oFont.getFontHeight() / 2;
add(m_oLabel);
}
/**
* Obtém a fonte para renderização de texto. A fonte é carregada apenas uma
* vez, e então mantida em cache para novos usos futuros.
* @return Instância de PxBitmapFont com a font carregada.
*/
private function getFont():PxBitmapFont
{
var oFont:PxBitmapFont = null;
oFont = PxBitmapFont.fetch("Gloria");
if (oFont == null)
{
var sData = Assets.getText("assets/fonts/Gloria.fnt");
var oData = Xml.parse(sData);
oFont = (new PxBitmapFont()).loadAngelCode(Assets.getBitmapData("assets/fonts/Gloria.png"), oData);
PxBitmapFont.store("Gloria", oFont);
}
return oFont;
}
. . .
The full sample project can be downloaded of this zip in 4shared.
Problems with Accented Characters
The problem is that accented characters are not being correctly rendered on all operating systems (in the example project, the letter is lowercase - Unicode Charset 0xE9 - is not being correctly displayed). In fact, I tested compiling the project for Flash, Windows and Android, and only when exporting to Flash the text is correctly rendered (in Android the error is the same as in Windows):
I imagine it’s some problem with the charset of the application when compiled directly to the operating system, but I do not know how to change. I’ve even tried to add "1" to the file’s "Unicode" option. fnt (as specified in the Angelcode documentation referenced above), but it still doesn’t work. By the way, I have already put logs to print the charcode of the characters "is" in both Flash and Windows, and they are printed correctly (233 in decimal, or E9 in hexadecimal).