Text Engine 縦書きサンプル
...
@author ...
package
{
[SWF(width="465", height="465", backgroundColor="0xffffff", frameRate="0")]
import flash.display.Shape;
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.text.engine.EastAsianJustifier;
import flash.text.engine.ElementFormat;
import flash.text.engine.FontDescription;
import flash.text.engine.TextBlock;
import flash.text.engine.TextBaseline;
import flash.text.engine.LineJustification;
import flash.text.engine.TextElement;
import flash.text.engine.TextLine;
import flash.text.engine.TextRotation;
import flash.system.Capabilities;
/**
* ...
* @author ...
*/
public class Tategaki extends Sprite
{
private var grid:Shape;
private var screen:Sprite;
public function Tategaki()
{
grid = new Shape();
grid.graphics.lineStyle(1, 0xA0EBF3);
GraphicsUtils.drawGrid(grid.graphics, new Rectangle(0, 0, 451, 451), 50);
GraphicsUtils.drawGrid(grid.graphics, new Rectangle(0, 0, 451, 451), 25, Vector.<Number>([2.5,2.5]));
addChild(grid);
addChild(screen = new Sprite());
grid.x = screen.x = 5;
grid.y = screen.y = 5;
var japanese:String = "夕暮れは\n雲のはたてに物ぞ思ふ\nあまつそらなる\n人を恋ふとて";
var textBlock:TextBlock = new TextBlock();
var font:FontDescription = new FontDescription();
if (Capabilities.os.search("Mac OS") > -1) {
font.fontName = "小塚明朝";
} else {
font.fontName = "HGP行書体";
}
var format:ElementFormat = new ElementFormat(font);
format.fontSize = 50;
format.locale = "ja";
format.color = 0x222222;
textBlock.baselineZero = TextBaseline.IDEOGRAPHIC_CENTER;
textBlock.textJustifier = new EastAsianJustifier("ja", LineJustification.ALL_INCLUDING_LAST);
textBlock.lineRotation = TextRotation.ROTATE_90;
var linePosition:Number = this.stage.stageWidth - 40;
textBlock.content = new TextElement(japanese, format);
var previousLine:TextLine = null;
var textLine:TextLine;
screen.graphics.lineStyle(1, 0xff0000);
while (true) {
textLine = textBlock.createTextLine(previousLine, 450);
if (textLine == null)
break;
textLine.y = 0;
textLine.x = linePosition;
screen.graphics.moveTo(textLine.x, textLine.y);
screen.graphics.lineTo(textLine.x, textLine.height);
linePosition -= 50;
screen.addChild(textLine);
previousLine = textLine;
}
}
}
}
import flash.display.Graphics;
import flash.geom.Point;
import flash.geom.Rectangle;
class GraphicsUtils {
public function GraphicsUtils() {
;
}
public static function drawGrid(g:Graphics, rect:Rectangle, size:Number, pattern:Vector.<Number> = null):void {
var i:Number;
var limitW:Number;
var limitH:Number;
limitW = rect.x + rect.width;
limitH = rect.y + rect.height;
if (pattern == null) {
i = rect.y;
while (i < limitH) {
g.moveTo(rect.x, i);
g.lineTo(limitW, i);
i += size;
}
i = rect.x;
while ( i < limitW) {
g.moveTo(i, rect.y);
g.lineTo(i, limitH);
i += size;
}
}else {
i = rect.y;
while (i < limitH) {
drawDotLine(g, rect.x, i, limitW, i, pattern);
i += size;
}
i = rect.x;
while (i < limitW) {
drawDotLine(g, i, rect.y, i, limitH, pattern);
i += size;
}
}
}
public static function drawDotLine(g:Graphics, fromX:Number, fromY:Number, toX:Number, toY:Number, pattern:Vector.<Number>):void {
var i:int;
var n:Number;
var v:Point = new Point(toX - fromX, toY - fromY);
var tmp:Point;
var l:Number = v.length;
var sl:SimpleList = new SimpleList();
var s:Boolean = true;
v.normalize(1);
for (i = 0; i < pattern.length; i++) {
n = pattern[i];
tmp = v.clone();
tmp.normalize(n);
sl.append({v:tmp, length:n});
}
var pos:Point = new Point(fromX, fromY);
var obj:Object;
n = 0;
g.moveTo(pos.x, pos.y);
while (true) {
obj = sl.next();
n += obj.length;
if (n > l) {
pos.x = toX;
pos.y = toY;
s = step(g, pos, s);
break;
}else {
pos.offset(obj.v.x, obj.v.y);
s = step(g, pos, s);
}
}
}
private static function step(g:Graphics, p:Point, s:Boolean):Boolean {
if (s) {
g.lineTo(p.x, p.y);
}else {
g.moveTo(p.x, p.y);
}
return !s;
}
}
class SimpleList {
private var currentElement:ListElement;
private var appendFunc:Function;
public function SimpleList(data:* = null) {
if (data != null) {
_initialize(data);
}else{
appendFunc = _initialize;
}
}
public function append(data:*):void {
appendFunc.call(null, data);
}
public function _initialize(data:*):void {
currentElement = new ListElement(data);
currentElement.next = currentElement;
currentElement.prev = currentElement;
appendFunc = _append;
}
public function _append(data:*):void {
var elem:ListElement = new ListElement(data);
var tmp:ListElement;
tmp = currentElement.next;
currentElement.next = elem;
elem.prev = currentElement;
elem.next = tmp;
tmp.prev = elem;
currentElement = elem;
}
public function current():*{
return currentElement.data;
}
public function next():* {
currentElement = currentElement.next;
return currentElement.data;
}
public function prev():*{
currentElement = currentElement.prev;
return currentElement.data;
}
}
class ListElement {
public var next:ListElement;
public var prev:ListElement;
public var data:*;
public function ListElement(data:*) {
this.data = data;
}
}