art text
/**
* Copyright lizhi ( http://wonderfl.net/user/lizhi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/jmcp
*/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.net.FileReference;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import sliz.miniui.Button;
import sliz.utils.UIUtils;
/**
* ...
* @author lizhi
*/
public class TestText extends Sprite
{
private var fonts:Array = [];
private var tf:TextField;
private var file:FileReference;
private var loader:Loader;
public function TestText()
{
tf = new TextField;
var tfm:TextFormat = new TextFormat;
tfm.size = 10;
tfm.leading = -2;
tf.defaultTextFormat = tfm;
tf.autoSize = TextFieldAutoSize.LEFT;
var text:String = " ";
var min:int = 32;
var max:int = 120;
while (++min < max) {
var c:String = String.fromCharCode(min);
text += c;
}
text += " .'\"*#@%+-)(TiI?><~";
for (var i:int = 0; i < text.length;i++ ) {
c = text.charAt(i);
fonts.push({d:getDensity(c),t:c});
}
fonts.sortOn("d", Array.NUMERIC|Array.DESCENDING);
//[Embed(source = '123.png')]var ic:Class;
//var bmd:BitmapData = (new ic as Bitmap).bitmapData;
//render(bmd);
addChild(tf);
UIUtils.changeStage(stage);
var explorer:Button = new Button("explorer image", 2, 20, this, browse);
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad2);
}
private function render(bmd:BitmapData):void {
tf.width = 1000;
tf.height = 1000;
var x:int = 0;
var y:int = 0;
tf.text = "";
while (y < bmd.height) {
while (x<bmd.width) {
var gray:Number = getGray(bmd.getPixel(x, y)) / 0xff;
var index:int = int((fonts.length - 1) * gray);
tf.appendText(fonts[index].t);
var r:Rectangle = tf.getCharBoundaries(tf.text.length - 1);
x += r.width;
}
tf.appendText("\n");
y = r.bottom
x = 0;
}
}
private function getDensity(t:String):Number {
var ttf:TextField = new TextField;
ttf.autoSize = TextFieldAutoSize.LEFT;
ttf.text = t;
var bmd:BitmapData = new BitmapData(ttf.width, ttf.height, false, 0xffffff);
bmd.draw(ttf);
var c:int = 0;
for (var x:int = 0; x < bmd.width;x++ ) {
for (var y:int = 0; y < bmd.height;y++ ) {
if (bmd.getPixel(x,y)!=0xffffff) {
c++;
}
}
}
return c / ttf.width / ttf.height;
}
private function getR(color:uint):uint {
return color << 8 >>> 24;
}
private function getG(color:uint):uint {
return color << 16 >>> 24;
}
private function getB(color:uint):uint {
return color << 24 >>> 24;
}
private function getGray(color:uint):uint {
return getR(color) * 0.299 + getG(color) * 0.587 + getB(color) * 0.114;
}
private function browse(e:Event):void {
file = new FileReference();
file.browse();
file.addEventListener(Event.SELECT, onSelect);
}
private function onSelect(e:Event):void {
file.load();
file.addEventListener(Event.COMPLETE, onLoad);
}
private function onLoad(e:Event):void {
loader.loadBytes(file.data);
}
private function onLoad2(e:Event):void {
var content:Bitmap = loader.contentLoaderInfo.content as Bitmap;
render(content.bitmapData);
}
}
}