Skip to content

Commit

Permalink
support for non-utf8 encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Beeblerox committed Feb 8, 2015
1 parent b974aa9 commit a3b3017
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 165 deletions.
65 changes: 57 additions & 8 deletions Haxe/lib/bitmapFont/BitmapFont.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package bitmapFont;

import haxe.Utf8;
import haxe.xml.Fast;
import openfl.display.Tilesheet;
import openfl.display.BitmapData;
Expand All @@ -18,6 +19,14 @@ import openfl.display.Tilesheet;
*/
class BitmapFont
{
public static inline var spaceCode:Int = 32;
public static inline var tabCode:Int = 9;
public static inline var newLineCode:Int = 10;

public static inline var defaultFontKey:String = "defaultFontKey";

private static inline var defaultFontData:String = " 36000000000000000000!26101010001000\"46101010100000000000000000#66010100111110010100111110010100000000$56001000111011000001101110000100%66100100000100001000010000010010000000&66011000100000011010100100011010000000'26101000000000(36010100100100010000)36100010010010100000*46000010100100101000000000+46000001001110010000000000,36000000000000010100-46000000001110000000000000.26000000001000/66000010000100001000010000100000000000056011001001010010100100110000000156011000010000100001000010000000256111000001001100100001111000000356111000001001100000101110000000456100101001010010011100001000000556111101000011100000101110000000656011001000011100100100110000000756111000001000010001100001000000856011001001001100100100110000000956011001001010010011100001000000:26001000100000;26001000101000<46001001001000010000100000=46000011100000111000000000>46100001000010010010000000?56111000001001100000000100000000@66011100100010101110101010011100000000A56011001001010010111101001000000B56111001001011100100101110000000C56011001001010000100100110000000D56111001001010010100101110000000E56111101000011000100001111000000F56111101000010000110001000000000G56011001000010110100100111000000H56100101001011110100101001000000I26101010101000J56000100001000010100100110000000K56100101001010010111001001000000L46100010001000100011100000M66100010100010110110101010100010000000N56100101001011010101101001000000O56011001001010010100100110000000P56111001001010010111001000000000Q56011001001010010100100110000010R56111001001010010111001001000000S56011101000001100000101110000000T46111001000100010001000000U56100101001010010100100110000000V56100101001010010101000100000000W66100010100010101010110110100010000000X56100101001001100100101001000000Y56100101001010010011100001001100Z56111100001001100100001111000000[36110100100100110000}46110001000010010011000000]36110010010010110000^46010010100000000000000000_46000000000000000011110000'26101000000000a56000000111010010100100111000000b56100001110010010100101110000000c46000001101000100001100000d56000100111010010100100111000000e56000000110010110110000110000000f46011010001000110010000000g5700000011001001010010011100001001100h56100001110010010100101001000000i26100010101000j37010000010010010010100k56100001001010010111001001000000l26101010101000m66000000111100101010101010101010000000n56000001110010010100101001000000o56000000110010010100100110000000p5700000111001001010010111001000010000q5700000011101001010010011100001000010r46000010101100100010000000s56000000111011000001101110000000t46100011001000100001100000u56000001001010010100100111000000v56000001001010010101000100000000w66000000101010101010101010011110000000x56000001001010010011001001000000y5700000100101001010010011100001001100z56000001111000100010001111000000{46011001001000010001100000|26101010101000}46110001000010010011000000~56010101010000000000000000000000\\46111010101010101011100000";

private static var fonts:Map<String, BitmapFont> = new Map<String, BitmapFont>();

/**
Expand Down Expand Up @@ -74,9 +83,50 @@ class BitmapFont
/**
* Retrieves default BitmapFont.
*/
public static function getDefault():BitmapFont
public static function getDefaultFont():BitmapFont
{
return DefaultBitmapFont.getDefaultFont();
var font:BitmapFont = BitmapFont.get(defaultFontKey);

if (font != null)
{
return font;
}

var letters:String = "";
var bd:BitmapData = new BitmapData(700, 9, true, 0xFF888888);

var letterPos:Int = 0;
var i:Int = 0;

while (i < defaultFontData.length)
{
letters += defaultFontData.substr(i, 1);

var gw:Int = Std.parseInt(defaultFontData.substr(++i, 1));
var gh:Int = Std.parseInt(defaultFontData.substr(++i, 1));

for (py in 0...gh)
{
for (px in 0...gw)
{
i++;

if (defaultFontData.substr(i, 1) == "1")
{
bd.setPixel32(1 + letterPos * 7 + px, 1 + py, 0xFFFFFFFF);
}
else
{
bd.setPixel32(1 + letterPos * 7 + px, 1 + py, 0x00000000);
}
}
}

i++;
letterPos++;
}

return BitmapFont.fromXNA(defaultFontKey, bd, letters);
}

/**
Expand Down Expand Up @@ -237,7 +287,7 @@ class BitmapFont
default: glyph;
}

charCode = glyph.charCodeAt(0);
charCode = Utf8.charCodeAt(glyph, 0);
}

font.addGlyphFrame(charCode, frame, xOffset, yOffset, xAdvance);
Expand Down Expand Up @@ -285,8 +335,7 @@ class BitmapFont
var cx:Int;
var letterIdx:Int = 0;
var charCode:Int;
var spaceCharCode:Int = " ".charCodeAt(0);
var numLetters:Int = letters.length;
var numLetters:Int = Utf8.length(letters);
var rect:Rectangle;
var xAdvance:Int;

Expand All @@ -310,15 +359,15 @@ class BitmapFont
var gw:Int = gx - cx;
var gh:Int = gy - cy;

charCode = letters.charCodeAt(letterIdx);
charCode = Utf8.charCodeAt(letters, letterIdx);

rect = new Rectangle(cx, cy, gw, gh);

xAdvance = gw;

font.addGlyphFrame(charCode, rect, 0, 0, xAdvance);

if (charCode == spaceCharCode)
if (charCode == spaceCode)
{
font.spaceWidth = xAdvance;
}
Expand Down Expand Up @@ -452,7 +501,7 @@ class BitmapFont
for (i in 0...(numCols))
{
charRect = new Rectangle(startX + i * spacedWidth, startY + j * spacedHeight, charWidth, charHeight);
font.addGlyphFrame(letters.charCodeAt(letterIndex), charRect, 0, 0, xAdvance);
font.addGlyphFrame(Utf8.charCodeAt(letters, letterIndex), charRect, 0, 0, xAdvance);

letterIndex++;

Expand Down
Loading

0 comments on commit a3b3017

Please sign in to comment.