Created
August 13, 2010 15:17
-
-
Save claus/523033 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
// Draws the first glyph of the first embedded font in a SWF | |
// Visualizes on and off curve points | |
import com.codeazur.as3swf.SWF; | |
import com.codeazur.as3swf.tags.TagDefineFont3; | |
import com.codeazur.as3swf.exporters.AS3GraphicsDataShapeExporter; | |
import flash.display.IGraphicsData; | |
import flash.display.GraphicsPath; | |
import flash.display.GraphicsPathCommand; | |
import flash.geom.Point; | |
import flash.geom.Rectangle; | |
import flash.display.Shape; | |
import flash.display.Graphics; | |
var sOutline:Shape = new Shape(); | |
addChild(sOutline); | |
var sDots:Shape = new Shape(); | |
addChild(sDots); | |
var swf:SWF = new SWF(loaderInfo.bytes); | |
// Assume the font has character id 1. Change this if needed | |
var font:TagDefineFont3 = swf.getTagByCharacterId(1) as TagDefineFont3; | |
var exporter:AS3GraphicsDataShapeExporter = new AS3GraphicsDataShapeExporter(swf); | |
font.export(exporter, 0); | |
for(var i:uint = 0; i < exporter.graphicsData.length; i++) { | |
var gdo:IGraphicsData = exporter.graphicsData[i]; | |
if(gdo is GraphicsPath) { | |
var gp:GraphicsPath = gdo as GraphicsPath; | |
processGraphicsPath(gp); | |
} | |
} | |
function processGraphicsPath(gp:GraphicsPath):void { | |
var c:Vector.<int> = gp.commands; | |
var d:Vector.<Number> = gp.data; | |
var p:Point = new Point(0, 0); | |
var coords:Vector.<Number>; | |
var bounds:Rectangle = getCharacterBounds(d); | |
var dx:Number = bounds.x - 20; | |
var dy:Number = bounds.y - 20; | |
sOutline.graphics.lineStyle(1, 0x000000); | |
for(var i:uint = 0; i < c.length; i++) { | |
coords = null; | |
switch(c[i]) { | |
case GraphicsPathCommand.MOVE_TO: | |
coords = d.splice(0, 2); | |
//trace("moveTo",coords); | |
sOutline.graphics.moveTo(coords[0] - dx, coords[1] - dy); | |
drawDot(sDots.graphics, coords[0] - dx, coords[1] - dy, 2); | |
break; | |
case GraphicsPathCommand.LINE_TO: | |
coords = d.splice(0, 2); | |
if(coords[0] != p.x || coords[1] != p.y) { | |
//trace("lineTo",coords); | |
sOutline.graphics.lineTo(coords[0] - dx, coords[1] - dy); | |
drawDot(sDots.graphics, coords[0] - dx, coords[1] - dy); | |
} | |
break; | |
case GraphicsPathCommand.CURVE_TO: | |
coords = d.splice(0, 4); | |
if(coords[0] != p.x || coords[1] != p.y) { | |
//trace("curveTo",coords); | |
sOutline.graphics.curveTo(coords[0] - dx, coords[1] - dy, coords[2] - dx, coords[3] - dy); | |
drawDot(sDots.graphics, coords[0] - dx, coords[1] - dy, 1); | |
drawDot(sDots.graphics, coords[2] - dx, coords[3] - dy); | |
} | |
break; | |
} | |
if(coords) { | |
p.y = coords.pop(); | |
p.x = coords.pop(); | |
} | |
} | |
} | |
function drawDot(g:Graphics, px:Number, py:Number, type:uint = 0):void { | |
var col:uint; | |
switch(type) { | |
case 0: col = 0x000000; break; | |
case 1: col = 0x0000ff; break; | |
case 2: col = 0xff0000; break; | |
} | |
g.lineStyle(1, col); | |
g.drawRect(px - 3, py - 3, 6, 6); | |
} | |
function getCharacterBounds(points:Vector.<Number>):Rectangle { | |
var tl:Point = new Point(Number.MAX_VALUE, Number.MAX_VALUE); | |
var br:Point = new Point(-Number.MAX_VALUE, -Number.MAX_VALUE); | |
var num:uint = points.length; | |
for(var i:uint = 0; i < num; i += 2) { | |
var px:Number = points[i]; | |
var py:Number = points[i + 1]; | |
if(tl.x > px) tl.x = px; | |
if(tl.y > py) tl.y = py; | |
if(br.x < px) br.x = px; | |
if(br.y < py) br.y = py; | |
} | |
return new Rectangle(tl.x, tl.y, br.x - tl.x, br.y - tl.y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment