Paths

Basic Concept
The SQUARE, CIRCLE, and TRIANGLE primitive shapes are implemented as lists of path operations, which are drawn on a canvas. The new path feature allows new primitive shapes paths to be defined. A path contains a list of path operations and path commands. Paths are defined to support the path drawing features found of SVG files and the OpenVG specification.

The simplest path is a path sequence followed by a path command. The path sequence specifies where to draw and the path command specifies whether to fill the path sequence or stroke it. A path sequence begins with a MOVETO to set where the drawing part of the path starts, some number of path operations (LINETO, CURVETO, ARCTO) that specify the path for drawing, and an optional CLOSEPOLY that causes the path sequence to return to the its beginning (i.e., the MOVETO). Here is a simple path: There are a couple of ways that a path can be more complex:
 * There can be multiple path commands that can cause the path sequence to be stroked or filled many times.
 * The FILL or STROKE commands can have color adjustments that cause the same path sequence to be drawn with different colors.
 * The FILL or STROKE commands can have geometric adjustments that cause the same path sequence to be drawn in different places (or rotated, or scaled, or flipped, or skewed).
 * There can be multiple path sequences that are all drawn by the path command (or path commands if you have more than one). Just follow the path sequence with another path sequence (a MOVETO and some more drawing operations).
 * The path operations and path commands can be put in loops (see below).
 * After the path command (or path commands) there can be more path sequences and path commands.
 * The MOVETO at the beginning of the path sequence is actually optional if:
 * It is at the beginning of the path and the MOVETO is to (0, 0)
 * If the MOVETO follows a CLOSEPOLY, FILL or STROKE and the position of the MOVETO is the same as the position of the last drawing operation in the preceding path sequence.
 * If you leave out the path command at the end of the path (for the last set of path sequences) then the path sequence(s) will be filled using the non-zero filling rule.

Path Operations
The supported path operations are:
 * MOVETO{x x y y} - Moves the path to the point (x, y) without drawing, begins a new path sequence
 * LINETO{x x y y} - Draws to the point (x, y)
 * ARCTO{x x y y rx x_radius ry y_radius r ellipse_angle param parameters} - draws an elliptical arc segment to the point (x, y), the ellipse has a radius (rx, ry) and is rotated by the ellipse angle (in degrees)
 * ARCTO{x x y y r radius param parameters} - draws a circular arc segment with the specified radius to the point (x, y)
 * CURVETO{x x y y x1 control_x_1 y1 control_y_1} - draw a quadratic bezier curve to point (x, y) with a control point at (x1, y1)
 * CURVETO{x x y y x1 control_x_1 y1 control_y_1 x2 control_x_2 y2 control_y_2} - draw a cubic bezier curve to point (x, y) with a starting control point at (x1, y1) and an ending control point at (x2, y2)
 * CURVETO{x x y y} - draw a smooth quadratic bezier curve to point (x, y) with a control point that is the mirror of the previous bezier curve &dagger;
 * CURVETO{x x y y x2 control_x_2 y2 control_y_2} - draw a smooth cubic bezier curve to point (x, y) with a starting control point that is the mirror of the ending control point of the previous bezier curve and an ending control point at (x2, y2) &dagger;

It is not necessary to list all of the path operation parameters. If a path operation parameter is omitted then a default value will be used. The default position is (0,0) for end points and control points. If the x or y part of a position or control point is omitted then 0 will be used. However, for cubic bezier curves either x2 or y2 must be specified for Context Free to know that it is cubic and not quadratic. For non-smooth cubic and quadratic bezier curves either x1 or y1 must be specified for Context Free to know that the non-smooth variant is desired. For arcs, the default ellipse radius is (1,1) and the default angle is 0.

&dagger; The smooth forms of the quadratic and cubic curve operations infer the unspecified control point by looking at the preceding curve operation. If the preceding operation is not a curve operation (CURVETO, CURVEREL, ARCTO, or ARCREL) then a smooth curve operation is not permitted.

Relative Path Operations
Each of the absolute path operations above has a relative form in which the position of the previous path operation is added to the current position (and to any control points):
 * MOVEREL{x x y y} - Moves the path to the relative point (x, y) without drawing
 * LINEREL{x x y y} - Draws to the relative point (x, y)
 * ARCREL{x x y y rx x_radius ry y_radius r ellipse_angle param parameters} - draws an elliptical arc segment to the relative point (x, y), the ellipse has a radius (rx, ry) and is rotated by the ellipse angle (in degrees)
 * ARCREL{x x y y r radius param parameters} - draws a circular arc segment with the specified radius to the relative point (x, y)
 * CURVEREL{x x y y x1 control_x_1 y1 control_y_1} - draw a quadratic bezier curve to relative point (x, y) with a relative control point at (x1, y1)
 * CURVEREL{x x y y x1 control_x_1 y1 control_y_1 x2 control_x_2 y2 control_y_2} - draw a cubic bezier curve to relative point (x, y) with a starting relative control point at (x1, y1) and an ending relative control point at (x2, y2)
 * CURVEREL{x x y y} - draw a smooth quadratic bezier curve to relative point (x, y) with a control point that is the mirror of the previous bezier curve
 * CURVEREL{x x y y x2 control_x_2 y2 control_y_2} - draw a smooth cubic bezier curve to relative point (x, y) with a starting control point that is the mirror of the ending control point of the previous bezier curve and an ending relative control point at (x2, y2)

Path Ending Operations
Path sequences can be explicitly ended and closed: A path sequence is implicitly ended without closure by starting a new path sequence with MOVETO/MOVEREL, by following it with one or more path commands, or by ending the path.
 * CLOSEPOLY{param parameters} - ends the current polygon and draws a line from the ending position back to the beginning position, unless the beginning and end points coincide exactly. If the path sequence is stroked then the connected beginning and end are drawn with a line joint, rather than with two line end caps.

Aligned Path Closures
A design might have a series of drawing operations that should result in the last point being the same as the first point. But when the path is closed a small line segment might be seen at the join between the beginning and end points. This is due to floating point math errors causing the end point to be slightly off from the beginning point. There is an alternate form of CLOSEPOLY that modifies the last drawing point so that the end point exactly matches the beginning point: Context Free will scan backward until it finds a MOVETO or MOVEREL and sets the ending drawing operation to the same point as the MOVETO/MOVEREL.
 * CLOSEPOLY{p align} - ends the current polygon and sets the ending point to exactly match the beginning point.

Path Commands
After a path sequence there can be one or more path commands. Path commands instruct Context Free to draw (stroke or fill) all of the path sequences between the path command and the previous group of path commands.
 * STROKE {shape adjustments width stroke_width param parameters} - Stroke the preceding path sequences with a pen of the specified width. Stroke width is relative to the size of the shape. If the stroke size is not specified then the default of 0.1 is used. If color or shape modifications are specified then the path sequences are modified when they are drawn.
 * FILL {shape adjustments param parameters} - Fill the preceding path sequences. If color or shape modifications are specified then the path sequences are modified when they are drawn. If the path sequences are intersecting or self-intersecting then a filling rule determines whether a given piece is filled or not. The default filling rule is non-zero, but even-odd filling can also be specified (see below).

Non-Zero filling http://www.w3.org/TR/SVG11/images/painting/fillrule-nonzero.png

Even-Odd filling http://www.w3.org/TR/SVG11/images/painting/fillrule-evenodd.png

Note that the shape adjustments in path commands can either be basic or ordered (see basic vs. ordered).

If a path is completed (by a closing curly brace, '}') with no path command following the last set of path sequences then an implicit fill command is appended to the path.

Parameters
Many path operations and commands have parameters that modify their action. These parameters have the form of the keyword 'p' or 'param' followed by a string. The string can be without quotes if it has no white-space characters. If there are white-space characters then the string must be enclosed in quotes.
 * ARCTO/ARCREL cw parameter - indicates that the clock-wise arc is drawn
 * ARCTO/ARCREL large parameter - indicates that the large arc is drawn
 * CLOSEPOLY align parameter - indicates that the closing path operation should be modified to exactly match the beginning path position
 * FILL evenodd parameter - indicates that the even-odd filling rule is used
 * STROKE miterjoin parameter - indicates that join between path sequence segments (and between the beginning and end of closed paths) should have miter joins.
 * STROKE roundjoin parameter - indicates that join between path sequence segments (and between the beginning and end of closed paths) should have round joins.
 * STROKE beveljoin parameter - indicates that join between path sequence segments (and between the beginning and end of closed paths) should have bevel joins.
 * STROKE buttcap parameter - indicates that the end points of unclosed path sequences should have butt caps.
 * STROKE roundcap parameter - indicates that the end points of unclosed path sequences should have round caps.
 * STROKE squarecap parameter - indicates that the end points of unclosed path sequences should have square caps.

Join types http://www.w3.org/TR/SVG11/images/painting/linejoin.png

Cap types http://www.w3.org/TR/SVG11/images/painting/linecap.png


 * STROKE iso parameter - indicates that stroke width is not transformed if the stroke is transformed, short for isowidth.

Top is default, bottom is isowidth

Arc Parameters
The basic arc drawing operation specifies a start point, an end point, and an ellipse. The ellipse is positioned such that the start point and end points touch the ellipse and the arc is drawn from the start to the finish. However, in general there are two possible ellipse positions for any pair of starting and ending points, and two different arcs on each ellipse that can be drawn. The cw and large parameters indicate which of the four possible arcs are drawn.



Two of the four arcs are large, i.e., more than 180°. Specifying the large parameter indicates that one of these arcs should be drawn. Otherwise one of the arcs that are less than 180° will be drawn. Two of the four arcs draw from start to end clockwise around the ellipse and two draw counter-clockwise. Specifying the cw parameter indicates that a clockwise arc should be drawn. Otherwise a counter-clockwise arc will be drawn.

Setting the radius of the arc to be negative has the effect of inverting the arc drawing direction. A counter-clockwise arc will be drawn clockwise if the radius is negative. A clockwise arc will be drawn counter-clockwise if the radius is negative.

Bezier Control Points
The control points for bezier curve segments control the slope of the curve at the ends. For cubic bezier curves each end has its own control point and the slope at each end is indicated by the slope of the line from the end to the control point. For quadratic bezier curves both ends share a single control point and the slope at each end is indicated by the slope of the line from each end to the shared control point.



For smooth cubic bezier curves the starting control point is the mirror of the ending control point on the previous bezier curve or arc curve. For smooth quadratic bezier curves the single control point is the mirror of the ending control point on the previous bezier curve or arc curve. The preceding curve does not need to be of the same order (quadratic or cubic) as the smooth curve. The preceding curve can even be an ARCTO. Context Free will figure out what control point will match the slope and curvature between the smooth curve and the curve the precedes it.

Loops in Paths
Paths support the same extended loop constructs as rules, with a slight difference. Simple loops look pretty much the same as rule simple loops: But for complex loops there is the restriction that loops must either be all path operations or all path commands, no mixing is allowed. Z changes are not allowed in the loop transform for path operation or command loops and color changes are not allowed in the loop transform for path operation loops.