PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/demos/src/taskDemos/SVGPolygonParserDemo.as

http://libdamago.googlecode.com/
ActionScript | 559 lines | 328 code | 85 blank | 146 comment | 40 complexity | a3a019ce186b0457b5c2f4a57b2e79da MD5 | raw file
Possible License(s): Apache-2.0
  1. package taskDemos {
  2. import com.lorentz.SVG.SVGColor;
  3. import com.lorentz.SVG.SVGParser;
  4. import com.lorentz.SVG.StringUtil;
  5. import flash.display.CapsStyle;
  6. import flash.display.GradientType;
  7. import flash.display.JointStyle;
  8. import flash.display.Sprite;
  9. import flash.geom.Matrix;
  10. import org.svgweb.SVGViewerFlash;
  11. public class SVGPolygonParserDemo extends Sprite
  12. {
  13. [Embed(source="../../assets/demo02.pbelevel", mimeType='application/octet-stream')]
  14. public static const DATA :Class;
  15. protected var currentFontSize:Number;
  16. protected var currentViewBox:Object;
  17. protected var svg_object:Object;
  18. protected const WIDTH:String = "width";
  19. protected const HEIGHT:String = "height";
  20. protected const WIDTH_HEIGHT:String = "width_height";
  21. public function SVGPolygonParserDemo ()
  22. {
  23. var svgParser :SVGParser = new SVGParser(svg);
  24. var parsed :Object = svgParser.parse();
  25. visit(parsed);
  26. }
  27. protected function visit (elt :Object) :Sprite
  28. {
  29. var obj :Sprite;
  30. // inheritStyles(elt);
  31. //Save current fontSize and viewBoxSize, and set the new one
  32. // var oldFontSize :Number = currentFontSize;
  33. // var oldViewBox :* = currentViewBox;
  34. // if (elt.finalStyle["font-size"] != null) {
  35. // currentFontSize = getUserUnit(elt.finalStyle["font-size"], HEIGHT);
  36. // }
  37. if (elt.viewBox != null) {
  38. currentViewBox = elt.viewBox;
  39. }
  40. //
  41. trace("elt.type=" + elt.type);
  42. switch (elt.type) {
  43. case 'svg':
  44. obj = visitSvg(elt);
  45. break;
  46. case 'rect':
  47. obj = visitRect(elt);
  48. break;
  49. case 'path':
  50. obj = visitPath(elt);
  51. break;
  52. case 'polygon':
  53. obj = visitPolygon(elt);
  54. break;
  55. case 'polyline':
  56. obj = visitPolyline(elt);
  57. break;
  58. case 'line':
  59. obj = visitLine(elt);
  60. break;
  61. case 'circle':
  62. trace("not implemented");
  63. // obj = visitCircle(elt);
  64. break;
  65. case 'ellipse':
  66. trace("not implemented");
  67. // obj = visitEllipse(elt);
  68. break;
  69. case 'g':
  70. // obj =
  71. visitG(elt);
  72. break;
  73. case 'text':
  74. trace("not implemented");
  75. // obj = visitText(elt);
  76. break;
  77. default:
  78. throw new Error("Unknown tag type " + elt.localName());
  79. }
  80. if (obj != null) {
  81. if (elt.transform)
  82. obj.transform.matrix = elt.transform;
  83. // if (elt.finalStyle["display"] == "none" || elt.finalStyle["visibility"] == "hidden") {
  84. // obj.visible = false;
  85. // }
  86. //Testing
  87. // if (elt.clipPath != null) {
  88. // var id :String = StringUtil.rtrim(String(elt.clipPath).split("(")[1], ")");
  89. // id = StringUtil.ltrim(id, "#");
  90. //
  91. // var mask :* = visitClipPath(svg_object.defs[id]);
  92. //
  93. // var newGroup :Sprite = new Sprite();
  94. // newGroup.addChild(obj);
  95. // newGroup.addChild(mask);
  96. // obj.mask = mask;
  97. //
  98. // obj = newGroup;
  99. // }
  100. //Restore the old fontSize and viewBoxSize
  101. // currentFontSize = oldFontSize;
  102. // currentViewBox = oldViewBox;
  103. //
  104. }
  105. return obj;
  106. }
  107. private function visitG(elt:Object):void {
  108. // var s:Sprite = new Sprite();
  109. // s.name = elt.id != null ? elt.id : "g";
  110. // if( elt.x != null )
  111. // s.x = getUserUnit(elt.x, WIDTH);
  112. // if( elt.y != null )
  113. // s.y = getUserUnit(elt.y, HEIGHT);
  114. // if(elt.transform)
  115. // s.transform.matrix = elt.transform;
  116. for each(var childElt:Object in elt.children) {
  117. // s.addChild(
  118. visit(childElt);//);
  119. }
  120. // return s;
  121. }
  122. protected function visitLine (elt :Object) :Sprite
  123. {
  124. var s :Sprite = new Sprite();
  125. s.name = elt.id != null ? elt.id : "line";
  126. var x1 :Number = getUserUnit(elt.x1, WIDTH);
  127. var y1 :Number = getUserUnit(elt.y1, HEIGHT);
  128. var x2 :Number = getUserUnit(elt.x2, WIDTH);
  129. var y2 :Number = getUserUnit(elt.y2, HEIGHT);
  130. lineStyle(s, elt);
  131. s.graphics.moveTo(x1, y1);
  132. s.graphics.lineTo(x2, y2);
  133. s.graphics.lineStyle();
  134. return s;
  135. }
  136. private function lineStyle(s:Sprite, elt:Object):void {
  137. return;
  138. // var color:uint = 0x000000;//SVGColor.parseToInt(elt.finalStyle.stroke);
  139. // var noStroke:Boolean = true;//elt.finalStyle.stroke==null || elt.finalStyle.stroke == '' || elt.finalStyle.stroke=="none";
  140. //
  141. // var stroke_opacity:Number = Number(elt.finalStyle["opacity"]?elt.finalStyle["opacity"]: (elt.finalStyle["stroke-opacity"]? elt.finalStyle["stroke-opacity"] : 1));
  142. //
  143. // var w:Number = 1;
  144. // if(elt.finalStyle["stroke-width"])
  145. // w = getUserUnit(elt.finalStyle["stroke-width"], WIDTH_HEIGHT);
  146. //
  147. // var stroke_linecap:String = CapsStyle.NONE;
  148. //
  149. // if(elt.finalStyle["stroke-linecap"]){
  150. // var linecap:String = StringUtil.trim(elt.finalStyle["stroke-linecap"]).toLowerCase();
  151. // if(linecap=="round")
  152. // stroke_linecap = CapsStyle.ROUND;
  153. // else if(linecap=="square")
  154. // stroke_linecap = CapsStyle.SQUARE;
  155. // }
  156. //
  157. // var stroke_linejoin:String = JointStyle.MITER;
  158. //
  159. // if(elt.finalStyle["stroke-linejoin"]){
  160. // var linejoin:String = StringUtil.trim(elt.finalStyle["stroke-linejoin"]).toLowerCase();
  161. // if(linejoin=="round")
  162. // stroke_linejoin = JointStyle.ROUND;
  163. // else if(linejoin=="bevel")
  164. // stroke_linejoin = JointStyle.BEVEL;
  165. // }
  166. //
  167. // if(!noStroke && elt.finalStyle.stroke.indexOf("url")>-1){
  168. // var id:String = StringUtil.rtrim(String(elt.finalStyle.stroke).split("(")[1], ")");
  169. // id = StringUtil.ltrim(id, "#");
  170. //
  171. // var grad:Object = svg_object.gradients[id];
  172. //
  173. // if(grad!=null){
  174. // switch(grad.type){
  175. // case GradientType.LINEAR: {
  176. // calculateLinearGradient(grad);
  177. //
  178. // s.graphics.lineGradientStyle(grad.type, grad.colors, grad.alphas, grad.ratios, grad.mat, grad.spreadMethod, "rgb");
  179. // break;
  180. // }
  181. // case GradientType.RADIAL: {
  182. // calculateRadialGradient(grad);
  183. //
  184. // if(grad.r==0)
  185. // s.graphics.lineStyle(w, grad.colors[grad.colors.length-1], grad.alphas[grad.alphas.length-1], true, "normal", stroke_linecap, stroke_linejoin);
  186. // else
  187. // s.graphics.lineGradientStyle(grad.type, grad.colors, grad.alphas, grad.ratios, grad.mat, grad.spreadMethod, "rgb", grad.focalRatio);
  188. //
  189. // break;
  190. // }
  191. // }
  192. // }
  193. // return;
  194. // } else if(noStroke)
  195. // s.graphics.lineStyle();
  196. // else
  197. // s.graphics.lineStyle(w, color, stroke_opacity, true, "normal", stroke_linecap, stroke_linejoin);
  198. }
  199. private function calculateLinearGradient(grad:Object):void {
  200. var x1:Number = getUserUnit(grad.x1, WIDTH);
  201. var y1:Number = getUserUnit(grad.y1, HEIGHT);
  202. var x2:Number = getUserUnit(grad.x2, WIDTH);
  203. var y2:Number = getUserUnit(grad.y2, HEIGHT);
  204. grad.mat = flashLinearGradientMatrix(x1, y1, x2, y2);
  205. }
  206. private function flashLinearGradientMatrix( x1:Number, y1:Number, x2:Number, y2:Number ):Matrix {
  207. var w:Number = x2-x1;
  208. var h:Number = y2-y1;
  209. var a:Number = Math.atan2(h,w);
  210. var vl:Number = Math.sqrt( Math.pow(w,2) + Math.pow(h,2) );
  211. var matr:Matrix = new flash.geom.Matrix();
  212. matr.createGradientBox( 1, 1, 0, 0., 0. );
  213. matr.rotate( a );
  214. matr.scale( vl, vl );
  215. matr.translate( x1, y1 );
  216. return matr;
  217. }
  218. private function calculateRadialGradient(grad:Object):void {
  219. var cx:Number = getUserUnit(grad.cx, WIDTH);
  220. var cy:Number = getUserUnit(grad.cy, HEIGHT);
  221. var r:Number = getUserUnit(grad.r, WIDTH);
  222. var fx:Number = getUserUnit(grad.fx, WIDTH);
  223. var fy:Number = getUserUnit(grad.fy, HEIGHT);
  224. grad.mat = flashRadialGradientMatrix(cx, cy, r, fx, fy);
  225. var f:* = { x:fx-cx, y:fy-cy };
  226. grad.focalRatio = Math.sqrt( (f.x*f.x)+(f.y*f.y) )/r;
  227. }
  228. private function flashRadialGradientMatrix( cx:Number, cy:Number, r:Number, fx:Number, fy:Number ):Matrix {
  229. var d:Number = r*2;
  230. var mat:Matrix = new flash.geom.Matrix();
  231. mat.createGradientBox( d, d, 0, 0., 0. );
  232. var a:Number = Math.atan2(fy-cy,fx-cx);
  233. mat.translate( -cx, -cy );
  234. mat.rotate( -a );
  235. mat.translate( cx, cy );
  236. mat.translate( cx-r, cy-r );
  237. return mat;
  238. }
  239. public function getUserUnit(s:String, viewBoxReference:String):Number {
  240. var value:Number;
  241. if(s.indexOf("pt")!=-1){
  242. value = Number(StringUtil.remove(s, "pt"));
  243. return value*1.25;
  244. } else if(s.indexOf("pc")!=-1){
  245. value = Number(StringUtil.remove(s, "pc"));
  246. return value*15;
  247. } else if(s.indexOf("mm")!=-1){
  248. value = Number(StringUtil.remove(s, "mm"));
  249. return value*3.543307;
  250. } else if(s.indexOf("cm")!=-1){
  251. value = Number(StringUtil.remove(s, "cm"));
  252. return value*35.43307;
  253. } else if(s.indexOf("in")!=-1){
  254. value = Number(StringUtil.remove(s, "in"));
  255. return value*90;
  256. } else if(s.indexOf("px")!=-1){
  257. value = Number(StringUtil.remove(s, "px"));
  258. return value;
  259. //Relative
  260. } else if(s.indexOf("em")!=-1){
  261. value = Number(StringUtil.remove(s, "em"));
  262. return value*currentFontSize;
  263. //Percentage
  264. } else if(s.indexOf("%")!=-1){
  265. value = Number(StringUtil.remove(s, "%"));
  266. switch(viewBoxReference){
  267. case WIDTH : return value/100 * currentViewBox.width;
  268. break;
  269. case HEIGHT : return value/100 * currentViewBox.height;
  270. break;
  271. default : return value/100 * Math.sqrt(Math.pow(currentViewBox.width,2)+Math.pow(currentViewBox.height,2))/Math.sqrt(2)
  272. break;
  273. }
  274. } else {
  275. return Number(s);
  276. }
  277. }
  278. protected function visitPath (elt :Object) :Sprite
  279. {
  280. var s :Sprite = new Sprite();
  281. s.name = elt.id != null ? elt.id : "path";
  282. // var winding :String =
  283. // elt.finalStyle["fill-rule"] == null ? "nonzero" : elt.finalStyle["fill-rule"];
  284. // var renderer :PathRenderer = new PathRenderer(elt.d);
  285. //
  286. // beginFill(s, elt);
  287. // lineStyle(s, elt);
  288. // renderer.render(s, winding);
  289. // s.graphics.endFill();
  290. return s;
  291. }
  292. protected function visitPolygon (elt :Object) :Sprite
  293. {
  294. trace("visitPolygon");
  295. return visitPolywhatever(elt, true);
  296. }
  297. protected function visitPolyline (elt :Object) :Sprite
  298. {
  299. trace("visitPolyline");
  300. return visitPolywhatever(elt, false);
  301. }
  302. protected function visitPolywhatever (elt :Object, isPolygon :Boolean) :Sprite
  303. {
  304. var s :Sprite = new Sprite();
  305. if (elt.id != null)
  306. s.name = elt.id;
  307. else
  308. s.name = isPolygon ? "polygon" : "polyline";
  309. var args :Array = elt.points;
  310. // if (isPolygon) {
  311. // beginFill(s, elt);
  312. // }
  313. lineStyle(s, elt);
  314. if (args.length > 2) {
  315. s.graphics.moveTo(Number(args[0]), Number(args[1]));
  316. var index :int = 2;
  317. while (index < args.length) {
  318. s.graphics.lineTo(Number(args[index]), Number(args[index + 1]));
  319. index += 2;
  320. }
  321. if (isPolygon) {
  322. s.graphics.lineTo(Number(args[0]), Number(args[1]));
  323. s.graphics.endFill();
  324. }
  325. }
  326. s.graphics.lineStyle();
  327. return s;
  328. }
  329. protected function visitRect (elt :Object) :Sprite
  330. {
  331. var s :Sprite = new Sprite();
  332. s.name = elt.id != null ? elt.id : "rectangle";
  333. var x :Number = getUserUnit(elt.x, WIDTH);
  334. var y :Number = getUserUnit(elt.y, HEIGHT);
  335. var width :Number = getUserUnit(elt.width, WIDTH);
  336. var height :Number = getUserUnit(elt.height, HEIGHT);
  337. // beginFill(s, elt);
  338. lineStyle(s, elt);
  339. if (elt.isRound) {
  340. var rx :Number = getUserUnit(elt.rx, WIDTH);
  341. var ry :Number = getUserUnit(elt.ry, HEIGHT);
  342. s.graphics.drawRoundRect(x, y, width, height, rx, ry);
  343. } else {
  344. s.graphics.drawRect(x, y, width, height);
  345. }
  346. s.graphics.endFill();
  347. return s;
  348. }
  349. protected function visitSvg (elt :Object) :Sprite
  350. {
  351. // the view box
  352. var viewBox :Sprite = new Sprite();
  353. viewBox.name = "viewBox";
  354. //viewBox.graphics.drawRect(0,0,elt.viewBox.width, elt.viewBox.height);
  355. var activeArea :Sprite = new Sprite();
  356. activeArea.name = "activeArea";
  357. viewBox.addChild(activeArea);
  358. // iterate through the children of the svg node
  359. for each (var childElt :Object in elt.children) {
  360. // activeArea.addChild(
  361. visit(childElt);
  362. }
  363. /*
  364. // find the minimum point in the active area.
  365. var min:Point = new Point(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
  366. var r:Rectangle;
  367. var i:int = 0;
  368. var c:DisplayObject;
  369. for (i = 0; i < activeArea.numChildren; i++) {
  370. c = activeArea.getChildAt(i);
  371. r = c.getBounds(activeArea);
  372. min.x = Math.min(min.x, r.x);
  373. min.y = Math.min(min.y, r.y);
  374. }
  375. // move the transform into the activeArea layer
  376. activeArea.x = min.x;
  377. activeArea.y = min.y;
  378. for (i = 0; i < activeArea.numChildren; i++) {
  379. c = activeArea.getChildAt(i);
  380. c.x -= min.x;
  381. c.y -= min.y;
  382. }
  383. */
  384. //Testing
  385. if (elt.width != null && elt.height != null) {
  386. var activeAreaWidth :int = elt.viewBox.width || activeArea.width;
  387. var activeAreaHeight :int = elt.viewBox.height || activeArea.height;
  388. activeArea.scaleX = getUserUnit(elt.width, WIDTH) / activeAreaWidth;
  389. activeArea.scaleY = getUserUnit(elt.height, HEIGHT) / activeAreaHeight;
  390. activeArea.scaleX = Math.min(activeArea.scaleX, activeArea.scaleY);
  391. activeArea.scaleY = Math.min(activeArea.scaleX, activeArea.scaleY);
  392. }
  393. //
  394. return viewBox;
  395. }
  396. private var svgImage :SVGViewerFlash;
  397. protected static const svg :XML = <svg
  398. xmlns:dc="http://purl.org/dc/elements/1.1/"
  399. xmlns:cc="http://creativecommons.org/ns#"
  400. xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  401. xmlns:svg="http://www.w3.org/2000/svg"
  402. xmlns="http://www.w3.org/2000/svg"
  403. xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
  404. xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
  405. width="744.09448819"
  406. height="1052.3622047"
  407. id="svg2"
  408. version="1.1"
  409. inkscape:version="0.47 r22583"
  410. sodipodi:docname="New document 1">
  411. <defs
  412. id="defs4">
  413. <inkscape:perspective
  414. sodipodi:type="inkscape:persp3d"
  415. inkscape:vp_x="0 : 526.18109 : 1"
  416. inkscape:vp_y="0 : 1000 : 0"
  417. inkscape:vp_z="744.09448 : 526.18109 : 1"
  418. inkscape:persp3d-origin="372.04724 : 350.78739 : 1"
  419. id="perspective10" />
  420. </defs>
  421. <sodipodi:namedview
  422. id="base"
  423. pagecolor="#ffffff"
  424. bordercolor="#666666"
  425. borderopacity="1.0"
  426. inkscape:pageopacity="0.0"
  427. inkscape:pageshadow="2"
  428. inkscape:zoom="0.49497475"
  429. inkscape:cx="397.38541"
  430. inkscape:cy="479.83179"
  431. inkscape:document-units="px"
  432. inkscape:current-layer="layer1"
  433. showgrid="false"
  434. inkscape:snap-midpoints="false"
  435. inkscape:snap-bbox="true"
  436. inkscape:window-width="1312"
  437. inkscape:window-height="1046"
  438. inkscape:window-x="0"
  439. inkscape:window-y="0"
  440. inkscape:window-maximized="0" />
  441. <metadata
  442. id="metadata7">
  443. <rdf:RDF>
  444. <cc:Work
  445. rdf:about="">
  446. <dc:format>image/svg+xml</dc:format>
  447. <dc:type
  448. rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
  449. <dc:title></dc:title>
  450. </cc:Work>
  451. </rdf:RDF>
  452. </metadata>
  453. <g
  454. inkscape:label="Layer 1"
  455. inkscape:groupmode="layer"
  456. id="layer1">
  457. <rect
  458. style="fill:#0000ff;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
  459. id="rect2816"
  460. width="155.87529"
  461. height="291.91876"
  462. x="172.26543"
  463. y="108.40568" />
  464. <path
  465. style="fill:#0000ff;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
  466. d="m 267.24437,775.56595 -92.46844,13.93909 0,-308.57142 105.77375,-10.10153"
  467. id="rect2820"
  468. sodipodi:nodetypes="cccc" />
  469. </g>
  470. </svg>;
  471. }
  472. }