Skip to content

Instantly share code, notes, and snippets.

@trxcllnt
Created May 24, 2011 05:55
Show Gist options
  • Save trxcllnt/988192 to your computer and use it in GitHub Desktop.
Save trxcllnt/988192 to your computer and use it in GitHub Desktop.
Functional and modular text layout algorithm shell.
package
{
import flash.display.*;
import flash.text.engine.*;
import org.tinytlf.layout.*;
import org.tinytlf.layout.properties.LayoutProperties;
import org.tinytlf.util.fte.ContentElementUtil;
[SWF(width="500", height="300")]
public class Main extends Sprite
{
private const child:Sprite = new Sprite();
public function Main()
{
addChild(child);
var g:Graphics = child.graphics;
g.lineStyle(1);
g.drawRect(0, 0, stage.stageWidth - 1, stage.stageHeight - 51);
g.endFill();
child.y = 50;
var lBreakGraphic:GraphicElement = new GraphicElement(new Shape(), 0, 0, new ElementFormat());
lBreakGraphic.userData = 'lineBreak';
var graphic:GraphicElement = new GraphicElement(
new TextGraphic(), 200, 100,
new ElementFormat(null, 12, 0, 1, 'auto', TextBaseline.IDEOGRAPHIC_TOP));
var float:ContentElement = ContentElementUtil.lineBreakBeforeAndAfter(
new GroupElement(new <ContentElement>[graphic, lBreakGraphic]));
float.userData = {float: 'left'};
var element:GroupElement = new GroupElement(
new <ContentElement>[float,
new TextElement('ENIAC (pronounced /ˈɛni.æk/), short for Electronic ' +
'Numerical Integrator And Computer,[1][2] was the first ' +
'general-purpose, electronic computer. It was a Turing-complete, ' +
'digital computer capable of being reprogrammed to solve a full ' +
'range of computing problems.[3]',
new ElementFormat(null, 22))
], new ElementFormat());
var block:TextBlock = new TextBlock(element);
block.userData = new LayoutProperties({width: 500, textIndent: 20});
var lineRenderer:IParagraphLineRenderer = new LineRenderer();
var lineLayout:IParagraphLineLayout = new LineLayout();
var detector:IConstraintDetector = new ConstraintDetector();
var constraintLayout:IConstraintLayout = new ConstraintLayout();
var lineConstraintRenderer:IParagraphLineRenderer = new LineConstraintRenderer();
var lineConstraintLayout:IParagraphLineLayout = new LineConstraintLayout();
var lines:Vector.<TextLine>;
var constraints:Vector.<IConstraint>;
// 1. Break all the lines normally.
// lines = lineRenderer.render(block);
// 2. Layout all the lines normally.
// lines = lineLayout.layout(lines.concat());
// 3. Detect the constraints.
// constraints = detector.detectConstraints(lines.concat());
// 4. Layout the constraints first.
// constraints = constraintLayout.layout(constraints.concat());
// 5. Break the lines around the constraints.
// lines = lineConstraintRenderer.render(block, lines.concat(), constraints.concat());
// 6. Layout the newly broken lines around the laid out constraints.
// lines = lineConstraintLayout.layout(lines.concat(), constraints);
// All 6 steps in a fancy chained call.
lines = lineConstraintLayout.layout(
lineConstraintRenderer.render(block,
lineLayout.layout(lines = lineRenderer.render(block)),
constraints = detector.detectConstraints(lines.concat())),
constraintLayout.layout(constraints.concat()));
// Add lines to the display list
for(var i:int = 0; i < lines.length; ++i)
{
child.addChild(lines[i]);
}
}
}
}
import flash.display.*;
internal class TextGraphic extends Sprite
{
public function TextGraphic()
{
var g:Graphics = graphics;
g.beginFill(0x3333CC, 0.75);
g.drawRect(0, 0, 200, 100);
g.endFill();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment