forked from: adobe challenge 2
"Use Flash Player 10 text libraries (FTE).
* You can do a lot of interesting things with text,
* but I think it has even more potential
* in experimental work where
* the APIs are used just as a container format
* for images, movie clips
* and other interactive elements."
* by Justin Everett-Church
*
* This code is an example of FTE.
/**
* Copyright mex_md ( http://wonderfl.net/user/mex_md )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/AeCX
*/
/**
*
* "Use Flash Player 10 text libraries (FTE).
* You can do a lot of interesting things with text,
* but I think it has even more potential
* in experimental work where
* the APIs are used just as a container format
* for images, movie clips
* and other interactive elements."
* by Justin Everett-Church
*
* This code is an example of FTE.
*/
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.engine.*;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.display.SimpleButton;
public class AdobeFTE extends Sprite {
private const TEXT:String = "Use Flash Player 10 text libraries (FTE). You can do a lot of interesting things with text, but I think it has even more potential in experimental work where the APIs are used just as a container format for images, movie clips and other interactive elements.";
private const CHARACTORS:Array =["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"]
private var n:int = 0;
private var textLines:Array;
private var textBlock:TextBlock;
private var element:GroupElement;
private var handwritingTexts:Object;
private var canvas:Canvas;
private var canvases:Array;
private var bmd:BitmapData;
private var charactorString:String;
public function AdobeFTE()
{
canvases = [];
setup();
textLines = [];
var instruction:TextField = new TextField();
instruction.autoSize = "left";
instruction.text = "下のボックスに手書きで文字を書いてからテキストをタイプして下さい"
this.addChild(instruction);
var nextCaharactorBtn:Sprite = new Sprite();
nextCaharactorBtn.graphics.lineStyle(2, 0x666666);
nextCaharactorBtn.graphics.beginFill(0xffffff,0.1);
var label:TextField = new TextField();
label.text = "next";
nextCaharactorBtn.graphics.drawRoundRect(0, 0, 80, 20, 5);
nextCaharactorBtn.graphics.endFill();
this.addChild(label);
this.addChild(nextCaharactorBtn);
nextCaharactorBtn.addEventListener(MouseEvent.CLICK, onNextBtnClick);
nextCaharactorBtn.x =label.x =stage.stageWidth - nextCaharactorBtn.width-32;
nextCaharactorBtn.y = label.y=stage.stageHeight-50;
var prevCaharactorBtn:Sprite = new Sprite();
prevCaharactorBtn.graphics.lineStyle(2, 0x666666);
prevCaharactorBtn.graphics.beginFill(0xffffff,0.1);
var label2:TextField = new TextField();
label2.text = "prev";
prevCaharactorBtn.graphics.drawRoundRect(0, 0, 80, 20, 5);
prevCaharactorBtn.graphics.endFill();
this.addChild(label2);
this.addChild(prevCaharactorBtn);
prevCaharactorBtn.addEventListener(MouseEvent.CLICK, onPrevBtnClick);
prevCaharactorBtn.x = label2.x = 32;
prevCaharactorBtn.y = label2.y=stage.stageHeight-50;
charactorString = CHARACTORS[n];
textCreation();
setCanvas();
}
/*-----------------------------------------//
//onPrevBtnClick
//------------------------------------------*/
private function onPrevBtnClick(evt:MouseEvent):void
{
n--;
if (n < 0) n = CHARACTORS.length - 1;
setCanvas();
}
private function setCanvas():void
{
if (canvas)
{
canvas.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
this.removeChild(canvas);
}
canvas = canvases[n % CHARACTORS.length];
canvas.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
this.addChild(canvas);
if (bmd != null)
{
handwritingTexts[charactorString] = bmd;
}
canvas.canvas.graphics.clear();
bmd = null;
charactorString = CHARACTORS[n % CHARACTORS.length];
canvas.canvas.graphics.clear();
if (handwritingTexts.hasOwnProperty(charactorString))
{
var childbitmap:DisplayObject = canvas.canvas.getChildByName("childBitmap");
if (childbitmap)
{
canvas.canvas.removeChild(childbitmap);
}
var bm:BitmapData = handwritingTexts[charactorString];
var bmp:Bitmap = new Bitmap(bm);
bmp.name = "childBitmap"
canvas.canvas.addChild(bmp)
bmp.x = 50 - bmp.width / 2;
}
canvas.canvas.graphics.lineStyle(4, 0x000000);
}
/*-----------------------------------------//
//onNextBtnClick
//------------------------------------------*/
private function onNextBtnClick(evt:MouseEvent):void
{
n++;
setCanvas();
}
private function textCreation():void
{
handwritingTexts = { };
var l:uint = CHARACTORS.length;
for (var i:uint = 0; i < l; i++ )
{
var c:Canvas = new Canvas(CHARACTORS[i]);
c.x = (this.stage.stageWidth - c.width)/2;
c.y = this.stage.stageHeight - c.height;
canvases.push(c);
}
}
/*-----------------------------------------//
//onMouseDownHandler
//------------------------------------------*/
private function onMouseDownHandler(evt:MouseEvent):void
{
canvas.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
canvas.canvas.graphics.moveTo(canvas.mouseX, canvas.mouseY);
canvas.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}
/*-----------------------------------------//
//mouseUpHandler
//------------------------------------------*/
private function mouseUpHandler(evt:MouseEvent):void
{
canvas.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
canvas.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
}
/*-----------------------------------------//
//mouseMoveHandler
//------------------------------------------*/
private function mouseMoveHandler(evt:MouseEvent):void
{
canvas.canvas.graphics.lineTo(canvas.mouseX, canvas.mouseY);
var bound:Rectangle = canvas.canvas.getBounds(canvas);
bmd = new BitmapData(bound.width, 100, true, 0xffffff);
var mat:Matrix = new Matrix();
mat.translate(-bound.x, 0);
bmd.draw(canvas.canvas,mat, new ColorTransform(), null, new Rectangle(0,0,bound.width, 100),false);
}
private function setup():void
{
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler)
// ContentElement
element = new GroupElement();
// TextBlock
textBlock = new TextBlock();
}
/*-----------------------------------------//
//keyDownHandler
//------------------------------------------*/
private function keyDownHandler(evt:KeyboardEvent):void
{
// FontDescription : フォント設定
var fontDescription:FontDescription = createFontDescription();
// ElementFormat
var elementFormat:ElementFormat = createElementFormat( fontDescription );
// ContentElement
element = createContentElement( String.fromCharCode(evt.charCode), elementFormat );
// TextJustifier
var textJustifier:TextJustifier =createTextJustifier();
// TextBlock
textBlock.content = element;
layout( textBlock );
}
private function layout(targetBlock:TextBlock):void{
deleteAllLines();
var offsetX:int = 32;
var offsetY:int = 32;
var lineHeight:Number = 1;
var textLine:TextLine = targetBlock.createTextLine(null, 400-offsetX );
while (textLine) {
addChild(textLine);
textLines.push(textLine);
textLine.x = offsetX;
offsetY += textLine.height * lineHeight;
textLine.y = offsetY;
textLine = targetBlock.createTextLine( textLine, 400-offsetX );
}
}
private function deleteAllLines():void
{
var l:uint = textLines.length;
for (var i:int = 0; i < l; i++)
{
textLines[i].parent.removeChild(textLines[i]);
}
textLines = [];
}
private function drawLines():void
{
var l:uint = textLines.length;
for (var i:int = 0; i < l; i++)
{
if (textLines[i])
{
this.graphics.lineStyle(1, 0xff0000);
this.graphics.moveTo( 0,TextLine(textLines[i]).y);
this.graphics.lineTo(stage.stageWidth,TextLine(textLines[i]).y);
this.graphics.lineStyle(1, 0x00ff00);
this.graphics.moveTo(0, TextLine(textLines[i]).y+TextLine(textLines[i]).descent);
this.graphics.lineTo(stage.stageWidth,TextLine(textLines[i]).y + TextLine(textLines[i]).descent);
this.graphics.lineStyle(1, 0x0000ff);
this.graphics.moveTo(0, TextLine(textLines[i]).y-TextLine(textLines[i]).ascent);
this.graphics.lineTo(stage.stageWidth,TextLine(textLines[i]).y - TextLine(textLines[i]).ascent);
var atoms:int = TextLine(textLines[i]).atomCount;
for (var j:int = 0; j < atoms; j++)
{
var atomBound:Rectangle = TextLine(textLines[i]).getAtomBounds(j);
this.graphics.lineStyle(1, 0x00ffff);
this.graphics.moveTo(textLines[i].x+atomBound.x,textLines[i].y-textLines[i].ascent);
this.graphics.lineTo(textLines[i].x + atomBound.x,textLines[i].y+textLines[i].descent);
}
}
}
}
// FontDescription : フォント設定
private function createFontDescription():FontDescription {
var fontDescription :FontDescription = new FontDescription();
// CFFHinting.NONE, // CFFHinting.HORIZONTAL_STEM
fontDescription.cffHinting = CFFHinting.HORIZONTAL_STEM;
// FontLookup.DEVICE, FontLookup.EMBEDDED_CFF
fontDescription.fontLookup = FontLookup.DEVICE;
//_ゴシック, 明朝, _等幅, _sans, _serif, _typewriter
fontDescription.fontName = "_ゴシック, _sans";
// FontPosture.NORMAL, FontPosture.ITALIC
fontDescription.fontPosture = FontPosture.NORMAL;
// FontWeight.NORMAL, FontWeight.BOLD
fontDescription.fontWeight = FontWeight.NORMAL;
// true, false
fontDescription.locked = false;
// RenderingMode.NORMAL, RenderingMode.CFF
fontDescription.renderingMode = RenderingMode.CFF;
return fontDescription;
}
// ElementFormat : テキストフォーマット設定
private function createElementFormat( fontDescription:FontDescription ):ElementFormat {
var elementFormat:ElementFormat = new ElementFormat();
// TextBaseline.ROMAN, TextBaseline.ASCENT, TextBaseline.DESCENT, TextBaseline.IDEOGRAPHIC_TOP, TextBaseline.IDEOGRAPHIC_CENTER, TextBaseline.IDEOGRAPHIC_BOTTOM
elementFormat.alignmentBaseline = TextBaseline.ROMAN;
// Number
elementFormat.alpha = 1;
// Number
elementFormat.baselineShift = 0;
// BreakOpportunity.AUTO, BreakOpportunity.ANY, BreakOpportunity.NONE, BreakOpportunity.ALL
elementFormat.breakOpportunity = BreakOpportunity.AUTO;
// uint
elementFormat.color = 0x000000;
// DigitCase.DEFAULT, DigitCase.LINING, DigitCase.OLD_STYLE
elementFormat.digitCase = DigitCase.DEFAULT;
// DigitWidth.DEFAULT, DigitWidth.PROPORTIONAL, DigitWidth.TABULAR
elementFormat.digitWidth = DigitWidth.TABULAR;
// TextBaseline.ROMAN, TextBaseline.ASCENT, TextBaseline.DESCENT, TextBaseline.IDEOGRAPHIC_TOP, TextBaseline.IDEOGRAPHIC_CENTER, TextBaseline.IDEOGRAPHIC_BOTTOM
elementFormat.dominantBaseline = TextBaseline.ROMAN;
// FontDescription
elementFormat.fontDescription = fontDescription;
// Number
elementFormat.fontSize = 50;
// Kerning.ON, Kerning.OFF, Kerning.AUTO
elementFormat.kerning= Kerning.ON;
// LigatureLevel.NONE, LigatureLevel.MINIMUM, LigatureLevel.COMMON, LigatureLevel.UNCOMMON, LigatureLevel.EXOTIC
elementFormat.ligatureLevel = LigatureLevel.NONE;
// String
elementFormat.locale = "ja";
// true, false
elementFormat.locked= false;
// TextRotation.ROTATE_0, TextRotation.ROTATE_90, TextRotation.ROTATE_180, TextRotation.ROTATE_270, TextRotation.AUTO
elementFormat.textRotation = TextRotation.AUTO;
// Number
elementFormat.trackingLeft = 0;
// Number
elementFormat.trackingRight = 0;
// TypographicCase.DEFAULT, TypographicCase.TITLE, TypographicCase.CAPS, TypographicCase.SMALL_CAPS, TypographicCase.UPPERCASE, TypographicCase.LOWERCASE, TypographicCase.CAPS_AND_SMALL_CAPS
elementFormat.typographicCase= TypographicCase.DEFAULT;
return elementFormat;
}
// ContentElement : 表示内容
private function createContentElement( content:*, elementFormat:ElementFormat ):GroupElement {
// ElementFormat
element.elementFormat = elementFormat;
var newElement:ContentElement;
if(handwritingTexts.hasOwnProperty(String(content).toUpperCase()))
{
var graphic:Bitmap = new Bitmap(handwritingTexts[String(content).toUpperCase()]);
graphic.scaleX = graphic.scaleY = 0.5;
newElement = new GraphicElement(graphic);
GraphicElement(newElement).elementWidth = graphic.width;
GraphicElement(newElement).elementHeight = 40;
newElement.elementFormat = elementFormat;
}else
{
newElement = new TextElement(content, elementFormat);
}
var newElemVect:Vector.<ContentElement> = new Vector.<ContentElement>();
newElemVect.push(newElement as ContentElement);
// String
element.replaceElements(element.elementCount,element.elementCount,newElemVect); // read-only
// TextRotation.ROTATE_0, TextRotation.ROTATE_90, TextRotation.ROTATE_180, TextRotation.ROTATE_270, TextRotation.AUTO
element.textRotation = TextRotation.ROTATE_0;
return element;
}
// TextJustifier : 整列設定
private function createTextJustifier():TextJustifier {
// EastAsianJustifier, SpaceJustifier,
var textJustifier:TextJustifier = new EastAsianJustifier("ja");
// var textJustifier:TextJustifier = new SpaceJustifier("ja");
// LineJustification.UNJUSTIFIED, LineJustification.ALL_BUT_LAST, LineJustification.ALL_INCLUDING_LAST
textJustifier.lineJustification = LineJustification.ALL_INCLUDING_LAST;
switch( true ) {
case textJustifier is EastAsianJustifier:
var eastAsianJustifier:EastAsianJustifier = textJustifier as EastAsianJustifier;
// JustificationStyle.PUSH_IN_KINSOKU, JustificationStyle.PUSH_OUT_ONLY, JustificationStyle.PRIORITIZE_LEAST_ADJUSTMENT
eastAsianJustifier.justificationStyle = JustificationStyle.PUSH_IN_KINSOKU;
break;
case textJustifier is SpaceJustifier:
var spaceJustifier:SpaceJustifier= textJustifier as SpaceJustifier;
// true , false
spaceJustifier.letterSpacing = true;
break;
}
return textJustifier;
}
// TextBlock : 段組設定
private function createTextBlock( content:ContentElement, textJustifier:TextJustifier ):TextBlock {
var textBlock:TextBlock = new TextBlock();
// true, false
textBlock.applyNonLinearFontScaling = true;
// FontDescription
textBlock.baselineFontDescription;
// Number
textBlock.baselineFontSize;
// TextBaseline.ROMAN, TextBaseline.ASCENT, TextBaseline.DESCENT, TextBaseline.IDEOGRAPHIC_TOP, TextBaseline.IDEOGRAPHIC_CENTER, TextBaseline.IDEOGRAPHIC_BOTTOM
textBlock.baselineZero = TextBaseline.ROMAN;
// int
textBlock.bidiLevel
// TextElement, GraphicElement, GroupElement
textBlock.content = content;
// TextLine
textBlock.firstInvalidLine; // read-only
// TextLine
textBlock.firstLine; // read-only
// TextLine
textBlock.lastLine; // read-only
// TextRotation.ROTATE_0, TextRotation.ROTATE_90, TextRotation.ROTATE_180, TextRotation.ROTATE_270, TextRotation.AUTO
textBlock.lineRotation = TextRotation.ROTATE_0;
// Vector.<TabStop>
textBlock.tabStops;
// EastAsianJustifier, SpaceJustifier,
textBlock.textJustifier = textJustifier;
// TextLineCreationResult.SUCCESS, TextLineCreationResult.COMPLETE, TextLineCreationResult.INSUFFICIENT_WIDTH
textBlock.textLineCreationResult// read-only
// *
textBlock.userData;
return textBlock;
}
}
}
import flash.text.TextField;
import flash.display.Sprite;
class Canvas extends Sprite
{
private var _canvas:Sprite
public function Canvas(charactor:String):void
{
this.graphics.lineStyle(2, 0x000000);
this.graphics.beginFill(0x000000,0);
this.graphics.drawRect(0,0, 100, 100);
this.graphics.endFill();
_canvas = new Sprite();
_canvas.graphics.lineStyle(4, 0x000000);
var letter:TextField = new TextField();
letter.opaqueBackground = 0x000000;
letter.textColor = 0xffffff;
letter.autoSize = "left";
letter.text = charactor;
this.addChild(letter);
this.addChild(_canvas);
}
public function get canvas():Sprite { return _canvas; }
public function set canvas(value:Sprite):void
{
_canvas = value;
}
}