Created
April 25, 2012 03:36
-
-
Save Blecki/2486041 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.BitmapData; | |
import flash.geom.ColorTransform; | |
import flash.geom.Matrix; | |
import flash.geom.Point; | |
import flash.geom.Rectangle; | |
import net.flashpunk.graphics.Spritemap; | |
import net.flashpunk.FP; | |
public class BitmapFont | |
{ | |
private var font:BitmapData = null; | |
private var gWidth:int = 0; | |
private var gHeight:int = 0; | |
private var kWidth:int = 0; | |
private var color:ColorTransform = new ColorTransform(); | |
public function BitmapFont(source:*, glyphWidth:int, glyphHeight:int, kerningWidth:int) | |
{ | |
font = FP.getBitmap(source); | |
gWidth = glyphWidth; | |
gHeight = glyphHeight; | |
kWidth = kerningWidth; | |
} | |
public static function WrapString(text:String, maxWidth:int, kerningWidth:int):String | |
{ | |
var result:String = ""; | |
var word:String = ""; | |
var thisLine:int = 0; | |
for (var i:int = 0; i < text.length; ++i) | |
{ | |
var char:String = text.charAt(i); | |
if (char == '\n') | |
{ | |
thisLine = 0; | |
result = result + word + "\n"; | |
word = ""; | |
} | |
else if (char == ' ') | |
{ | |
result = result + word + " "; | |
thisLine += 1; | |
word = ""; | |
} | |
else | |
{ | |
thisLine += 1; | |
if (thisLine * kerningWidth > maxWidth) | |
{ | |
thisLine = 0; | |
result = result + "\n"; | |
} | |
word = word + char; | |
} | |
} | |
if (word.length > 0) result = result + word; | |
return result; | |
} | |
public static function MakeTextImage(text:String, color:uint, maxWidth:int, font:BitmapFont):BitmapData | |
{ | |
text = WrapString(text, maxWidth, font.kWidth); | |
var lineCount:int = 1; | |
var longestLine:int = 0; | |
var thisLine:int = 0; | |
for (var i:int = 0; i < text.length; ++i) | |
{ | |
if (text.charAt(i) == '\n') | |
{ | |
lineCount += 1; | |
thisLine = 0; | |
} | |
else | |
{ | |
thisLine += 1; | |
if (thisLine > longestLine) longestLine = thisLine; | |
} | |
} | |
var target:BitmapData = new BitmapData(font.kWidth * longestLine, font.gHeight * lineCount, true, 0x00000000); | |
font.color.redMultiplier = Number(color >> 16 & 0xFF) / 255; | |
font.color.greenMultiplier = Number(color >> 8 & 0xFF) / 255; | |
font.color.blueMultiplier = Number(color & 0xFF) / 255; | |
RenderText(target, text, font); | |
return target; | |
} | |
private static var _tPoint1:Point = new Point(0, 0); | |
private static function mtPoint(x:Number, y:Number):Point { _tPoint1.x = x; _tPoint1.y = y; return _tPoint1; } | |
private static var _tRect:Rectangle = new Rectangle(0, 0, 0, 0); | |
private static function mtRect(x:Number, y:Number, w:Number, h:Number):Rectangle | |
{ | |
_tRect.x = x; _tRect.y = y; _tRect.width = w; _tRect.height = h; | |
return _tRect; | |
} | |
private static var _tMatrix:Matrix = new Matrix(); | |
public static function RenderText(target:BitmapData, text:String, font:BitmapFont):void | |
{ | |
var offset:int = 0x20; | |
var x:int = 0; | |
var y:int = 0; | |
var kx:int = (font.gWidth - font.kWidth) / 2; | |
var col:int = font.font.width / font.gWidth; | |
for (var i:int = 0; i < text.length; ++i) | |
{ | |
var code:int = text.charCodeAt(i); | |
if (code == '\n'.charCodeAt(0)) | |
{ | |
x = 0; | |
y += font.gHeight; | |
} | |
else if (code == ' '.charCodeAt(0)) | |
{ | |
x += font.kWidth; | |
} | |
else if (code < 0x80) | |
{ | |
code -= offset; | |
var fx:int = (code % col) * font.gWidth; | |
var fy:int = uint(code / col) * font.gHeight; | |
_tMatrix.identity(); | |
_tMatrix.translate(x - fx - kx, y - fy); | |
target.draw(font.font, _tMatrix, font.color, null, mtRect(x - kx, y, font.gWidth, font.gHeight)); | |
x += font.kWidth; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment