Skip to content

Instantly share code, notes, and snippets.

@Beeblerox
Created January 18, 2013 19:49
Show Gist options
  • Save Beeblerox/4567854 to your computer and use it in GitHub Desktop.
Save Beeblerox/4567854 to your computer and use it in GitHub Desktop.
package ;
import org.flixel.FlxG;
import org.flixel.plugin.pxText.PxBitmapFont;
import nme.display.BitmapData;
import nme.geom.Rectangle;
class BitmapFont extends PxBitmapFont
{
private var glyphWidth:Int;
private var glyphHeight:Int;
public function new ()
{
super ();
glyphWidth = 0;
glyphHeight = 0;
}
public function loadOnlineMonospace(pBitmapData:BitmapData, pLetters:String):BitmapFont
{
reset();
_glyphString = pLetters;
#if (flash || js)
// fill array with nulls
for (i in 0...(256))
{
_glyphs.push(null);
}
#end
if (pBitmapData != null)
{
_tileRects = [];
var result:BitmapData = loadOnlineMonospace_(pBitmapData, _tileRects);
_bitmapDataKey = FlxG.getUniqueBitmapKey("font");
_pixels = FlxG.addBitmap(result, false, false, _bitmapDataKey, glyphWidth, glyphHeight);
var currRect:Rectangle;
#if (flash || js)
updateGlyphData();
#end
}
return this;
}
public function loadOnlineMonospace_ (pBitmapData : BitmapData,
pRects : Array <Rectangle>) : BitmapData
{
var w : Int = pBitmapData.width;
var h : Int = pBitmapData.height;
var count = Std.int(w / h);
glyphWidth = Std.int(w / count);
glyphHeight = h;
for (i in 0...count)
{
#if (flash || js)
pRects.push (new Rectangle (i * glyphWidth, 0, glyphWidth, glyphHeight));
#else
pRects.push (new Rectangle (i * (glyphWidth + 1), 0, glyphWidth, glyphHeight));
#end
}
var bd : BitmapData = new BitmapData (w,
h,
true, 0xffffffff);
bd.copyPixels (pBitmapData, pBitmapData.rect, PxBitmapFont.ZERO_POINT);
return bd;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment