### Quick Start Example Source: https://github.com/openfl/svg/blob/master/_autodocs/README.md Loads an SVG file using Assets.getText and renders it to the display. ```haxe import format.SVG; import openfl.display.Sprite; import openfl.Assets; class Main extends Sprite { public function new() { super(); // Load SVG var svg = new SVG(Assets.getText("assets/icon.svg")); // Render at position (10, 10) with size (200, 200) svg.render(graphics, 10, 10, 200, 200); } } ``` -------------------------------- ### Gfx Base Class beginFill Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Example of using beginFill. ```haxe gfx.beginFill(0xFF0000, 0.8); // Red at 80% opacity ``` -------------------------------- ### ArcSegment Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/path.md Example of creating an ArcSegment. ```Haxe // Arc from (0,0) to (50,50) with radii 30,30, no rotation, small arc, clockwise var arcSegment = new ArcSegment(0, 0, 30, 30, 0, false, true, 50, 50); path.segments.push(arcSegment); ``` -------------------------------- ### GfxBytes Constructor Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Example of creating a GfxBytes instance and adding drawing commands. ```Haxe import format.gfx.GfxBytes; var gfxBytes = new GfxBytes(); gfxBytes.beginFill(0xFF0000, 1.0); gfxBytes.lineTo(100, 100); var binary = gfxBytes.buffer; ``` -------------------------------- ### Gradient Usage Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Example demonstrating how to create and apply a linear gradient fill. ```haxe import format.gfx.Gradient; import openfl.display.GradientType; var grad = new Gradient(); grad.type = GradientType.LINEAR; grad.colors = [0xFF0000, 0x0000FF]; grad.alphas = [1.0, 1.0]; grad.ratios = [0, 255]; gfx.beginGradientFill(grad); ``` -------------------------------- ### GfxGraphics Class Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Example of creating and using GfxGraphics. ```haxe import openfl.display.Sprite; import format.gfx.GfxGraphics; var sprite = new Sprite(); var gfx = new GfxGraphics(sprite.graphics); gfx.beginFill(0xFF0000, 1.0); gfx.moveTo(0, 0); gfx.lineTo(100, 100); gfx.endFill(); ``` -------------------------------- ### Path Properties Configuration Example Source: https://github.com/openfl/svg/blob/master/_autodocs/configuration.md Example demonstrating how to programmatically configure path properties such as fill, stroke, and opacity. ```Haxe import format.svg.Path; import format.svg.FillType; import openfl.display.CapsStyle; import openfl.display.JointStyle; var path = new Path(); path.fill = FillSolid(0xFF0000); // Red fill path.fill_alpha = 0.8; path.stroke_colour = 0x000000; // Black stroke path.stroke_width = 2.0; path.stroke_caps = CapsStyle.ROUND; path.joint_style = JointStyle.ROUND; ``` -------------------------------- ### PathParser.parse Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/path.md Example of parsing SVG path data. ```Haxe import format.svg.PathParser; var parser = new PathParser(); var pathData = "M10,20 L50,60 Q25,15 50,60 Z"; var segments = parser.parse(pathData, false); for (segment in segments) { trace("Segment type: " + segment.getType()); } ``` -------------------------------- ### CubicSegment.toQuadratics Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/path.md Example of converting a cubic Bezier curve to quadratic curves. ```Haxe var cubicSegment = new CubicSegment(10, 5, 40, 30, 50, 60); var quadSegments = cubicSegment.toQuadratics(0, 0); for (quad in quadSegments) { path.segments.push(quad); } ``` -------------------------------- ### LineStyle Usage Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Example showing how to configure and apply a line style for drawing strokes. ```haxe import format.gfx.LineStyle; import openfl.display.CapsStyle; import openfl.display.JointStyle; var lineStyle = new LineStyle(); lineStyle.thickness = 2.5; lineStyle.color = 0x000000; lineStyle.alpha = 0.8; lineStyle.capsStyle = CapsStyle.ROUND; lineStyle.jointStyle = JointStyle.ROUND; gfx.lineStyle(lineStyle); ``` -------------------------------- ### GfxBytes.fromString Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Example demonstrating how to encode drawing commands to a string and then decode it back to a GfxBytes object. ```Haxe // Encode var gfxBytes = new GfxBytes(); // ... draw commands ... var encoded = gfxBytes.toString(); // Later: decode and use var decoded = GfxBytes.fromString(encoded); decoded.iterate(new GfxGraphics(sprite.graphics)); ``` -------------------------------- ### FillType Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/path.md Example usage of FillType enum. ```Haxe path.fill = FillSolid(0xFF0000); // Red path.fill = FillNone; // No fill path.fill = FillGrad(gradientObject); ``` -------------------------------- ### QuadraticSegment Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/path.md Example of creating and adding a QuadraticSegment to a path's segments. ```Haxe var quadSegment = new QuadraticSegment(25, 15, 50, 60); path.segments.push(quadSegment); ``` -------------------------------- ### GfxBytes.iterate Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Example showing how to replay stored drawing commands onto another Gfx implementation. ```Haxe var stored = new GfxBytes(); // ... store drawing commands ... // Later: render to graphics var replay = new GfxGraphics(sprite.graphics); stored.iterate(replay); ``` -------------------------------- ### Install SVG using haxelib Source: https://github.com/openfl/svg/blob/master/README.md Install the SVG library using the haxelib command-line tool. ```bash haxelib install svg ``` -------------------------------- ### Path Class Properties Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/path.md Example demonstrating how to instantiate and set properties for the Path class. ```Haxe import format.svg.Path; import format.svg.FillType; import openfl.geom.Matrix; var path = new Path(); path.name = "myPath"; path.fill = FillSolid(0xFF0000); // Red fill path.fill_alpha = 1.0; path.stroke_colour = 0x000000; // Black stroke path.stroke_width = 2.0; path.alpha = 0.8; path.segments = []; path.matrix = new Matrix(); ``` -------------------------------- ### DisplayElement Usage Example Source: https://github.com/openfl/svg/blob/master/_autodocs/types.md Example of how to iterate through display elements and handle them using a switch statement. ```Haxe for (element in group.children) { switch(element) { case DisplayPath(path): // Handle path case DisplayGroup(subgroup): // Handle group case DisplayText(text): // Handle text } } ``` -------------------------------- ### Gradient Configuration Example Source: https://github.com/openfl/svg/blob/master/_autodocs/configuration.md Examples showing the configuration of both linear and radial gradients with specified colors, alphas, ratios, and spread methods. ```Haxe import format.svg.Grad; import openfl.display.GradientType; import openfl.display.SpreadMethod; import openfl.display.InterpolationMethod; var linearGrad = new Grad(GradientType.LINEAR); linearGrad.x1 = 0; linearGrad.y1 = 0; linearGrad.x2 = 100; linearGrad.y2 = 0; linearGrad.colors = [0xFF0000, 0x0000FF]; linearGrad.alphas = [1.0, 1.0]; linearGrad.ratios = [0, 255]; linearGrad.spread = SpreadMethod.PAD; var radialGrad = new Grad(GradientType.RADIAL); radialGrad.x1 = 50; radialGrad.y1 = 50; radialGrad.radius = 50; radialGrad.colors = [0xFFFFFF, 0x000000]; radialGrad.alphas = [1.0, 1.0]; radialGrad.ratios = [0, 255]; radialGrad.focus = 0.5; ``` -------------------------------- ### Group Constructor Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/group-text.md Creates a new empty Group instance. ```Haxe import format.svg.Group; var group = new Group(); var childGroup = new Group(); ``` -------------------------------- ### MoveSegment Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/path.md Example of creating and adding a MoveSegment to a path's segments. ```Haxe var moveSegment = new MoveSegment(10, 20); path.segments.push(moveSegment); ``` -------------------------------- ### Install SVG from GitHub for Development Source: https://github.com/openfl/svg/blob/master/README.md Install the SVG library directly from its GitHub repository for development builds. ```bash haxelib git svg https://github.com/openfl/svg ``` -------------------------------- ### DrawSegment Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/path.md Example of creating and adding a DrawSegment to a path's segments. ```Haxe var lineSegment = new DrawSegment(50, 60); path.segments.push(lineSegment); ``` -------------------------------- ### Text Constructor Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/group-text.md Creates a new empty Text instance and sets its properties. ```Haxe import format.svg.Text; import format.svg.FillType; import openfl.geom.Matrix; var text = new Text(); text.text = "Hello SVG"; text.font_family = "Arial"; text.font_size = 24; text.fill = FillSolid(0x000000); // Black text.fill_alpha = 1.0; text.x = 10; text.y = 50; text.matrix = new Matrix(); text.text_align = "start"; ``` -------------------------------- ### Group Usage Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/group-text.md Demonstrates how to create a Group, add children (Path, nested Group, Text), and iterate through them. ```haxe import format.svg.Group; import format.svg.Path; import format.svg.Text; import format.svg.FillType; var group = new Group(); group.name = "root"; // Add a path var path = new Path(); path.fill = FillSolid(0xFF0000); group.children.push(DisplayPath(path)); // Add a nested group var subgroup = new Group(); subgroup.name = "sublayer"; group.children.push(DisplayGroup(subgroup)); // Add text var textElement = new Text(); textElement.text = "Label"; group.children.push(DisplayText(textElement)); // Iterate through children for (child in group.children) { switch(child) { case DisplayPath(p): trace("Found path: " + p.name); case DisplayGroup(g): trace("Found group: " + g.name); case DisplayText(t): trace("Found text: " + t.text); } } ``` -------------------------------- ### Basic SVG Usage Example Source: https://github.com/openfl/svg/blob/master/README.md A simple example demonstrating how to load and render an SVG file in an OpenFL project. ```actionscript package import format.SVG; import openfl.display.Sprite; import openfl.Assets; class Main extends Sprite { public function new () { super (); var svg = new SVG (Assets.getText ("assets/icon.svg")); svg.render (graphics); } } ``` -------------------------------- ### SVGRenderer Constructor Example Source: https://github.com/openfl/svg/blob/master/_autodocs/configuration.md Examples of using the SVGRenderer constructor to render all elements or a specific layer. Includes a check for layer existence. ```Haxe import format.svg.SVGRenderer; // Render all elements var renderer = new SVGRenderer(svg.data); // Render only a specific layer/group var renderer = new SVGRenderer(svg.data, "myLayer"); // Check if layer exists before creating renderer if (svg.data.findGroup("myLayer") != null) { var renderer = new SVGRenderer(svg.data, "myLayer"); } ``` -------------------------------- ### Gfx2Haxe Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Example of using Gfx2Haxe to generate Haxe code from SVG content. ```Haxe import format.svg.SVGRenderer; import format.gfx.Gfx2Haxe; var xml = Xml.parse(svgContent); var haxeGen = new Gfx2Haxe(); SVGRenderer.toHaxe(xml); for (cmd in haxeGen.commands) { trace(cmd); // Output: g.beginFill(16711680,1.0); etc. } ``` -------------------------------- ### SVGData Constructor Example Source: https://github.com/openfl/svg/blob/master/_autodocs/configuration.md Example of using the SVGData constructor for standard parsing and for converting cubic curves. ```Haxe import format.svg.SVGData; // Standard parsing var svgData = new SVGData(xml); // Convert cubic curves to quadratic (useful for Flash, older platforms) var svgData = new SVGData(xml, true); ``` -------------------------------- ### Typical Rendering Sequence Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md A step-by-step example of the typical rendering sequence using the Gfx implementation. ```haxe // 1. Create Gfx implementation var gfx = new GfxGraphics(sprite.graphics); // 2. Set canvas size gfx.size(400, 300); // 3. Begin fill gfx.beginFill(0xFF0000, 1.0); // 4. Draw path (moveTo, lineTo, curveTo) gfx.moveTo(10, 10); gfx.lineTo(100, 100); gfx.curveTo(150, 50, 200, 100); // 5. End fill gfx.endFill(); // 6. Set line style and draw stroked path var lineStyle = new LineStyle(); lineStyle.thickness = 2.0; gfx.lineStyle(lineStyle); gfx.moveTo(10, 10); gfx.lineTo(200, 200); gfx.endLineStyle(); // 7. Signal end of rendering gfx.eof(); ``` -------------------------------- ### FillType Usage Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/group-text.md Demonstrates how to apply and check different fill types for a Path object. ```haxe import format.svg.Path; import format.svg.FillType; var path = new Path(); // Solid fill path.fill = FillSolid(0xFF0000); // Red // No fill path.fill = FillNone; // Gradient fill path.fill = FillGrad(gradientObject); // Check fill type switch(path.fill) { case FillSolid(color): trace("Solid fill: " + color); case FillGrad(grad): trace("Gradient fill with " + grad.colors.length + " stops"); case FillNone: trace("No fill"); } ``` -------------------------------- ### Custom Gfx Implementation Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md An example of a custom Gfx class that overrides methods to trace drawing operations. ```Haxe import format.gfx.Gfx; import format.gfx.LineStyle; import format.svg.Text; class MyCustomGfx extends Gfx { override public function beginFill(color:Int, alpha:Float):Void { trace("Begin fill with color: " + color); } override public function lineTo(inX:Float, inY:Float):Void { trace("Line to: " + inX + ", " + inY); } override public function eof():Void { trace("Rendering complete"); } } ``` -------------------------------- ### GfxExtent Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Example of using GfxExtent to calculate the bounding box of SVG path data. ```haxe import format.svg.SVGRenderer; import format.gfx.GfxExtent; var extent = new GfxExtent(); var renderer = new SVGRenderer(svg.data); renderer.iterate(extent); if (extent.extent != null) { trace("Bounds: " + extent.extent.x + ", " + extent.extent.y); trace("Size: " + extent.extent.width + " x " + extent.extent.height); } ``` -------------------------------- ### loadPath Method Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/svgdata.md Shows an example of how the loadPath method might be called internally to parse path-like SVG elements. ```Haxe // Called internally; user code typically doesn't call this var path = svgData.loadPath(pathElement, matrix, styles, false, false); ``` -------------------------------- ### GfxTextFinder Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Example of using GfxTextFinder to locate text elements within SVG data. ```haxe import format.svg.SVGRenderer; import format.gfx.GfxTextFinder; var finder = new GfxTextFinder(); var renderer = new SVGRenderer(svg.data); renderer.iterate(finder); if (finder.text != null) { trace("Found text: " + finder.text.text); } ``` -------------------------------- ### Binary Format for Performance Source: https://github.com/openfl/svg/blob/master/_autodocs/configuration.md Example of converting SVG XML to binary format for faster rendering. ```haxe import format.svg.SVGRenderer; import format.gfx.GfxBytes; // First time: convert to binary var xml = Xml.parse(svgContent); var gfxBytes = SVGRenderer.toBytes(xml); var binaryData = gfxBytes.buffer.toString(); // Serialize for storage // Later: deserialize and render faster var gfxBytes = GfxBytes.fromString(binaryData); gfxBytes.iterate(new GfxGraphics(graphics)); ``` -------------------------------- ### SVG.render() Common Configurations Source: https://github.com/openfl/svg/blob/master/_autodocs/configuration.md Examples demonstrating common configurations for the SVG.render() method, including natural size rendering, offset rendering, scaled rendering, and rendering specific layers. ```Haxe // Render at natural size with no offset svg.render(graphics); // Render at natural size with offset svg.render(graphics, 10, 10); // Render scaled to 200x200, positioned at (0, 0) svg.render(graphics, 0, 0, 200, 200); // Render specific layer at 100x100 svg.render(graphics, 50, 50, 100, 100, "backgroundLayer"); ``` -------------------------------- ### Complete Error Handling Example Source: https://github.com/openfl/svg/blob/master/_autodocs/errors.md A comprehensive example demonstrating robust error handling for loading, parsing, verifying, and rendering SVG content in OpenFL. It includes checks for file loading errors, empty content, parsing failures, null SVG data, and rendering exceptions. ```Haxe import format.SVG; import openfl.display.Sprite; import openfl.Assets; class SafeSVGLoader extends Sprite { public function loadAndRender(filePath:String):Bool { var content:String; // Step 1: Load SVG content try { content = Assets.getText(filePath); if (content == null || content.length == 0) { trace("Error: Empty SVG file: " + filePath); return false; } } catch (e:String) { trace("Error: Could not load SVG file: " + filePath + " - " + e); return false; } // Step 2: Parse SVG var svg:SVG; try { svg = new SVG(content); } catch (e:String) { trace("Error: SVG parsing failed: " + e); return false; } // Step 3: Verify SVG data if (svg.data == null) { trace("Error: SVG has no data"); return false; } // Step 4: Render SVG try { svg.render(graphics, 0, 0, 200, 200); trace("Success: SVG rendered"); return true; } catch (e:String) { trace("Error: Rendering failed: " + e); return false; } } } ``` -------------------------------- ### Generated Code Format Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Illustrates the format of Haxe code generated by Gfx2Haxe. ```Haxe g.beginFill(16711680,1.0); g.lineStyle(2,0,1.0,false,LineScaleMode.NORMAL,CapsStyle.NONE,JointStyle.MITER,3.0); g.moveTo(10,20); g.lineTo(100,100); g.curveTo(50,50,150,150); g.endLineStyle(); g.endFill(); ``` -------------------------------- ### Constructor Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/svg.md Creates a new SVG instance by parsing XML content. ```haxe import format.SVG; import openfl.Assets; var svgContent = Assets.getText("assets/icon.svg"); var svg = new SVG(svgContent); ``` -------------------------------- ### Grad updateMatrix Method Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/group-text.md Updates the gradient's transformation matrix based on element transformation. ```haxe import format.svg.Grad; import openfl.display.GradientType; import openfl.geom.Matrix; var grad = new Grad(GradientType.LINEAR); grad.x1 = 0; grad.y1 = 0; grad.x2 = 100; grad.y2 = 100; grad.colors = [0xFF0000, 0x0000FF]; grad.alphas = [1.0, 1.0]; grad.ratios = [0, 255]; var elementMatrix = new Matrix(); elementMatrix.translate(10, 10); grad.updateMatrix(elementMatrix); ``` -------------------------------- ### loadText Internal Call Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/svgdata.md Example comment indicating how the loadText method is called internally. ```Haxe // Called internally by loadGroup when parsing elements ``` -------------------------------- ### Render Method Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/svg.md Renders the SVG to OpenFL graphics with various options. ```haxe import format.SVG; import openfl.display.Sprite; import openfl.Assets; class Main extends Sprite { public function new() { super(); var svgContent = Assets.getText("assets/icon.svg"); var svg = new SVG(svgContent); // Render at natural size at position (10, 10) svg.render(graphics, 10, 10); // Render scaled to 200x200 at position (0, 0) // svg.render(graphics, 0, 0, 200, 200); // Render only a specific layer // svg.render(graphics, 10, 10, 200, 200, "myLayer"); } } ``` -------------------------------- ### loadGroup Method Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/svgdata.md Illustrates the internal usage of the loadGroup method, which is called by the constructor. ```Haxe // This is called internally by the constructor // Not typically called directly by user code ``` -------------------------------- ### Adding a New SVG Test Source: https://github.com/openfl/svg/blob/master/test/README.md Example of how to add a new test case in SvgGeneration.hx. ```haxe public function testAppleScalesUpCorrectly() { generateAndCompare("apple.svg"); // 56x56 (SVG size) generateAndCompare("apple.svg", 256, 256); generateAndCompare("apple.svg", 137, 137); } ``` -------------------------------- ### SVGRenderer Constructor Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/svgrenderer.md Demonstrates how to create a new SVGRenderer instance, either for the entire SVG or a specific layer. ```haxe import format.svg.SVGData; import format.svg.SVGRenderer; var svg = new SVG(Assets.getText("assets/icon.svg")); var renderer = new SVGRenderer(svg.data); // Or render only a specific layer // var renderer = new SVGRenderer(svg.data, "myLayer"); ``` -------------------------------- ### Filter Function Examples Source: https://github.com/openfl/svg/blob/master/_autodocs/configuration.md Examples of using filter functions to selectively render SVG elements based on name, groups, or patterns. ```Haxe import openfl.geom.Matrix; var renderer = new SVGRenderer(svg.data); // Render only elements named "background" var filter1:ObjectFilter = function(name, groups) { return name == "background"; }; renderer.render(graphics, null, filter1); // Render all elements except those in hidden layers (starting with ".") var filter2:ObjectFilter = function(name, groups) { if (groups.length > 0 && groups[0].charAt(0) == ".") { return false; } return true; }; renderer.render(graphics, null, filter2); // Render only elements in a specific group path var filter3:ObjectFilter = function(name, groups) { return groups.length > 0 && groups[0] == "visibleElements"; }; renderer.render(graphics, null, filter3); // Render elements matching a pattern var filter4:ObjectFilter = function(name, groups) { return name.indexOf("button") != -1; }; renderer.render(graphics, null, filter4); ``` -------------------------------- ### findGroup Method Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/group-text.md Recursively searches for a child group by name, including nested groups. ```Haxe var group = new Group(); var subgroup = new Group(); subgroup.name = "myLayer"; group.children.push(DisplayGroup(subgroup)); var found = group.findGroup("myLayer"); if (found != null) { trace("Found layer: " + found.name); } ``` -------------------------------- ### Partial Rendering Example Source: https://github.com/openfl/svg/blob/master/_autodocs/errors.md Demonstrates partial rendering of an SVG by using an SVGRenderer to compute the extent of the SVG and then rendering it within those bounds. ```haxe // Try to render only safe elements var renderer = new SVGRenderer(svg.data); var extent = renderer.getExtent(); if (extent != null) { // Render with computed bounds renderer.render(graphics); } ``` -------------------------------- ### SVGRenderer render Method Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/svgrenderer.md Shows how to render SVG data to OpenFL Graphics, with options for transformation matrices, filters, and scaling. ```haxe import openfl.display.Sprite; import openfl.geom.Matrix; var sprite = new Sprite(); var renderer = new SVGRenderer(svg.data); // Render to sprite graphics renderer.render(sprite.graphics); // Render with transformation var matrix = new Matrix(); matrix.translate(10, 10); renderer.render(sprite.graphics, matrix); // Render with filter (only render elements named "background") renderer.render(sprite.graphics, null, function(name, groups) { return name == "background"; }); ``` -------------------------------- ### hasGroup Method Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/group-text.md Checks if a group with the given name exists in this group or any descendants. ```Haxe if (group.hasGroup("background")) { var bgGroup = group.findGroup("background"); } ``` -------------------------------- ### SVGData Constructor Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/svgdata.md Demonstrates how to create a new SVGData instance by parsing an SVG XML string. ```Haxe import format.svg.SVGData; var xml = Xml.parse(''); var svgData = new SVGData(xml); trace("Width: " + svgData.width); trace("Height: " + svgData.height); ``` -------------------------------- ### Properties Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/svg.md Accesses the parsed SVG data structure to get dimensions. ```haxe var svg = new SVG(svgContent); if (svg.data != null) { trace("SVG width: " + svg.data.width); trace("SVG height: " + svg.data.height); } ``` -------------------------------- ### SVGRenderer renderRect Method Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/svgrenderer.md Illustrates rendering SVG with 9-slice scaling to a specified rectangle. ```haxe import openfl.geom.Rectangle; var bounds = new Rectangle(0, 0, 100, 100); var scaleRect = new Rectangle(40, 40, 20, 20); var target = new Rectangle(0, 0, 200, 200); renderer.renderRect(sprite.graphics, null, scaleRect, bounds, target); ``` -------------------------------- ### Catching Unknown URL Reference Errors Source: https://github.com/openfl/svg/blob/master/_autodocs/errors.md Example of how to catch errors when a gradient is referenced but not defined. ```Haxe try { var svg = new SVG(svgContent); } catch (e:String) { if (e.indexOf("Unknown url") != -1) { trace("Missing gradient definition: " + e); } } ``` -------------------------------- ### Layer Handling Example Source: https://github.com/openfl/svg/blob/master/_autodocs/errors.md Demonstrates how to check for the existence of a specific layer (group) within the SVG data and render it, or render all layers if the specified layer is not found. ```haxe var svgData = svg.data; // Check and render specific layer var layerName = "background"; if (svgData.hasGroup(layerName)) { svg.render(graphics, 0, 0, 200, 200, layerName); } else { trace("Warning: Layer '" + layerName + "' not found, rendering all"); svg.render(graphics, 0, 0, 200, 200); } ``` -------------------------------- ### Catching SVG Constructor Errors Source: https://github.com/openfl/svg/blob/master/_autodocs/errors.md Example of how to catch specific SVG constructor errors related to invalid SVG content. ```Haxe try { var svg = new SVG(content); } catch (e:String) { if (e.indexOf("Not an SVG file") != -1) { trace("Invalid SVG content: " + e); } } ``` -------------------------------- ### Preventing SVG Constructor Errors by Manual Parsing Source: https://github.com/openfl/svg/blob/master/_autodocs/errors.md Example of manually parsing XML before creating an SVG instance to prevent constructor errors. ```Haxe try { var xml = Xml.parse(content); var svg = new SVG(content); } catch (e:String) { trace("XML parsing or SVG validation failed: " + e); } ``` -------------------------------- ### Safe Gradient Handling Example Source: https://github.com/openfl/svg/blob/master/_autodocs/errors.md Shows how to safely find a specific group within the SVG data and then iterate through its children to handle different fill types, including gradients and solid colors. ```haxe // Find a specific group var targetGroup = svg.data.findGroup("elements"); if (targetGroup != null) { // Process elements in that group for (element in targetGroup.children) { switch(element) { case DisplayPath(path): switch(path.fill) { case FillGrad(grad): // Handle gradient case FillSolid(color): // Handle solid color case FillNone: // No fill } default: } } } ``` -------------------------------- ### ObjectFilter Example Source: https://github.com/openfl/svg/blob/master/_autodocs/types.md Example usage of an ObjectFilter function to conditionally render SVG elements. ```Haxe var filter:ObjectFilter = function(elementName:String, groupPath:Array):Bool { return elementName != "hidden" && groupPath[0] != ".private"; }; ``` -------------------------------- ### Gfx Base Class beginFill Method Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Begins a solid color fill. ```haxe public function beginFill(color:Int, alpha:Float):Void ``` -------------------------------- ### RenderContext Constructor Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/path.md Creates a new RenderContext. ```Haxe public function new(inMatrix:Matrix, ?inRect:Rectangle, ?inW:Float, ?inH:Float) ``` -------------------------------- ### Gfx Base Class beginGradientFill Method Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Begins a gradient fill. ```haxe public function beginGradientFill(grad:Gradient):Void ``` -------------------------------- ### PathParser Constructor Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/path.md Creates a new PathParser instance. ```Haxe public function new() ``` -------------------------------- ### getMatchingRect Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/svgrenderer.md Gets the bounding rectangle of elements matching a regular expression pattern. ```Haxe public function getMatchingRect(inMatch:EReg):Rectangle ``` ```Haxe var regex = ~/background_.*/; var bgBounds = renderer.getMatchingRect(regex); ``` -------------------------------- ### Get Bounding Box Source: https://github.com/openfl/svg/blob/master/_autodocs/README.md Retrieves the bounding box of the rendered content. ```haxe var bounds = renderer.getExtent(); ``` -------------------------------- ### Grad Constructor Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/group-text.md Creates a new Grad with the specified type. ```haxe public function new(inType:GradientType) ``` -------------------------------- ### Create SVG Instance Source: https://github.com/openfl/svg/blob/master/_autodocs/OVERVIEW.md Creates an SVG instance by passing the SVG content string to the SVG constructor. This process includes parsing XML, creating an SVGData structure, extracting dimensions and viewBox, loading elements, and processing styles and transforms. ```haxe var svg = new SVG(svgContent); ``` -------------------------------- ### Render to Display Source: https://github.com/openfl/svg/blob/master/_autodocs/OVERVIEW.md Shows how to render the parsed SVG content to OpenFL's graphics object, either at its natural size, scaled, or by rendering a specific layer. ```haxe import openfl.display.Sprite; class Main extends Sprite { public function new() { super(); var svg = new SVG(Assets.getText("assets/icon.svg")); // Render at natural size svg.render(graphics); // Or render scaled svg.render(graphics, 10, 10, 200, 200); // Or render specific layer svg.render(graphics, 0, 0, 200, 200, "background"); } } ``` -------------------------------- ### Basic Rendering Source: https://github.com/openfl/svg/blob/master/_autodocs/OVERVIEW.md Load an SVG file from assets and render it to the display graphics. ```Haxe import format.SVG; import openfl.display.Sprite; import openfl.Assets; class Main extends Sprite { public function new() { super(); var svg = new SVG(Assets.getText("assets/icon.svg")); svg.render(graphics, 10, 10, 100, 100); } } ``` -------------------------------- ### Gfx Base Class moveTo Method Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Moves the drawing pointer to a new position without drawing. ```haxe public function moveTo(inX:Float, inY:Float):Void ``` -------------------------------- ### Performance Tip 4: Cubic Conversion Source: https://github.com/openfl/svg/blob/master/_autodocs/README.md Converts cubic Bezier curves to quadratic for platforms lacking cubic support. ```haxe var data = new SVGData(xml, true); // Converts cubics to quadratic ``` -------------------------------- ### Gfx Base Class lineStyle Method Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Sets line/stroke properties for subsequent path drawing. ```haxe public function lineStyle(style:LineStyle):Void ``` -------------------------------- ### SVGRenderer iterate Method Example Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/svgrenderer.md Demonstrates using the iterate method to convert SVG data to Haxe code or binary format. ```haxe import format.gfx.Gfx2Haxe; import format.gfx.GfxBytes; // Convert to Haxe code var gfxHaxe = new Gfx2Haxe(); renderer.iterate(gfxHaxe); var haxeCode = gfxHaxe.commands; // Convert to binary format var gfxBytes = new GfxBytes(); renderer.iterate(gfxBytes); var bytes = gfxBytes.buffer; ``` -------------------------------- ### GfxGraphics Class Constructor Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Creates a new GfxGraphics that renders to the specified Graphics object. ```haxe public function new(inGraphics:Graphics) ``` -------------------------------- ### Catching Unknown Fill String Errors Source: https://github.com/openfl/svg/blob/master/_autodocs/errors.md Example of how to catch errors related to unrecognized fill attribute formats. ```Haxe try { var svg = new SVG(svgContent); } catch (e:String) { if (e.indexOf("Unknown fill string") != -1) { trace("Invalid fill attribute: " + e); } } ``` -------------------------------- ### Cubic to Quadratic Conversion Source: https://github.com/openfl/svg/blob/master/_autodocs/configuration.md Enabling cubic-to-quadratic conversion for improved performance on platforms with limited curve support. ```haxe var svgData = new SVGData(xml, true); // Convert cubics to quadratics ``` -------------------------------- ### Preventing Layer Not Found Error Source: https://github.com/openfl/svg/blob/master/_autodocs/errors.md Example demonstrating how to check for the existence of a layer before creating an SVGRenderer to prevent 'Layer Not Found' errors. ```Haxe if (svg.data.hasGroup("myLayer")) { var renderer = new SVGRenderer(svg.data, "myLayer"); } else { var renderer = new SVGRenderer(svg.data); } ``` -------------------------------- ### OpenFL Version Compatibility Source: https://github.com/openfl/svg/blob/master/_autodocs/configuration.md Conditional compilation for legacy and modern OpenFL APIs. ```haxe #if (openfl_legacy || openfl < "3.6.0") // Legacy OpenFL API #else // Modern OpenFL API #end ``` -------------------------------- ### Gfx Base Class curveTo Method Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Draws a quadratic Bezier curve from the current position. ```haxe public function curveTo(inCX:Float, inCY:Float, inX:Float, inY:Float):Void ``` -------------------------------- ### Catching Layer Not Found Error Source: https://github.com/openfl/svg/blob/master/_autodocs/errors.md Example of how to catch and handle the 'Layer Not Found' error when constructing an SVGRenderer, with a fallback to using the default layer. ```Haxe try { var renderer = new SVGRenderer(svg.data, "nonexistentLayer"); } catch (e:String) { if (e.indexOf("Could not find SVG group") != -1) { trace("Layer not found: " + e); var renderer = new SVGRenderer(svg.data); // Use default } } ``` -------------------------------- ### transX Method Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/path.md Transforms an X coordinate through the context's matrix and scale rectangle. ```Haxe public function transX(inX:Float, inY:Float):Float ``` -------------------------------- ### Catching Malformed Path Data Errors Source: https://github.com/openfl/svg/blob/master/_autodocs/errors.md Example of how to catch errors related to syntax errors in the SVG path data ('d' attribute). ```Haxe try { var svg = new SVG(svgContent); } catch (e:String) { if (e.indexOf("parsing path") != -1) { trace("Invalid path data: " + e); } } ``` -------------------------------- ### SVG Constructor Source: https://github.com/openfl/svg/blob/master/_autodocs/configuration.md The SVG class constructor accepts SVG content as a string. The 'content' parameter is the SVG XML content. If null, an empty SVG is created. ```Haxe public function new(content:String) ``` -------------------------------- ### Gfx Base Class size Method Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Sets the canvas dimensions. ```haxe public function size(inWidth:Float, inHeight:Float):Void ``` -------------------------------- ### Catching Unknown xlink:href Errors Source: https://github.com/openfl/svg/blob/master/_autodocs/errors.md Example of how to catch errors when a gradient references another gradient via xlink:href but it doesn't exist. ```Haxe try { var svg = new SVG(svgContent); } catch (e:String) { if (e.indexOf("xlink") != -1) { trace("Gradient reference error: " + e); } } ``` -------------------------------- ### Performance Tip 1: Binary Format Source: https://github.com/openfl/svg/blob/master/_autodocs/README.md Converts SVG XML to a binary format for efficient reuse. ```haxe var bytes = SVGRenderer.toBytes(xml); var encoded = bytes.toString(); // Store this ``` -------------------------------- ### Cubic to Quadratic Conversion Source: https://github.com/openfl/svg/blob/master/_autodocs/OVERVIEW.md Convert cubic Bezier curves to quadratic curves for platforms that do not natively support them. ```Haxe // For platforms without native cubic support var svgData = new SVGData(xml, true); // Converts cubics ``` -------------------------------- ### Catching Unknown Stop Offset Error Source: https://github.com/openfl/svg/blob/master/_autodocs/errors.md Example of how to catch and handle 'Unknown stop offset' errors during SVG parsing, which occur when a gradient stop has an invalid offset value. ```Haxe try { var svg = new SVG(svgContent); } catch (e:String) { if (e.indexOf("Unknown stop offset") != -1) { trace("Invalid gradient stop: " + e); } } ``` -------------------------------- ### SVG.render() Parameters Source: https://github.com/openfl/svg/blob/master/_autodocs/README.md Signature for the SVG.render() method, showing parameters and their defaults. ```haxe svg.render(graphics:Graphics, x:Float = 0, y:Float = 0, width:Int = -1, height:Int = -1, layer:String = null) ``` -------------------------------- ### Gfx Base Class lineTo Method Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/gfx.md Draws a straight line from the current position to the target. ```haxe public function lineTo(inX:Float, inY:Float):Void ``` -------------------------------- ### CubicSegment.toQuadratics Method Source: https://github.com/openfl/svg/blob/master/_autodocs/api-reference/path.md Converts a cubic Bezier curve to an array of quadratic curves for platforms that don't support cubic curves. ```Haxe public function toQuadratics(tx0:Float, ty0:Float):Array ``` -------------------------------- ### Running SVG Tests Source: https://github.com/openfl/svg/blob/master/test/README.md Commands to run SVG tests on different platforms. ```bash openfl test windows openfl test mac openfl test linux openfl test neko openfl test hl ``` -------------------------------- ### SVG.render() Method Signature Source: https://github.com/openfl/svg/blob/master/_autodocs/configuration.md The main SVG rendering method allows for positioning, scaling, and rendering specific layers. ```Haxe public function render( graphics:Graphics, x:Float = 0, y:Float = 0, width:Int = -1, height:Int = -1, ?inLayer:String = null ):Void ```