SVGPathSegment
Part of wslib
related class : SVGPath
The SVGPathSegment is usually conained within the segments array of SVGPath.
There can be the following types:
SVGPathSegment.propertiesDict.dopostln;
The SVGPathSegments can be absolute or relative. If a segment is absolute, the first letter of it's type will be a capital. Relative means that it's x/y position is added to that of the previous segment.
creation
SVGPathSegment ( type, ... args )
this creates a new object of a specific type, and adds the arguments depending on that
type : the type is a symbol with the name of a type. If the first letter is a capital, the segment is absolute,
otherwise it is relative to the previous segment. The first segment in an SVGPath should always be
absolute. The rest of the arguments depend on the type:
type shortcut extra arguments
moveTo m x, y
lineTo l x, y
curveTo c x1, y1, x2, y2, x, y (x1,y1,x2,y2 are the control points)
arc a rx, ry, xAxisRotation, largeArcFlag, sweepFlag, x, y
vLineTo h y
hLineTo v x
qCurveTo q x1, y1, x, y
sCurveTo s x2, y2, x, y
sQCurveTo t x, y
closePath z
unknown unknown
The shortcuts are the symbols as they are actually used in the SVG file format. Inside
SVGPathElement however I chose to convert them to understandable and SC conform names.
args : the arguments can be Floats, Integers or Points. Points will be split to two adjecent args
SVGPathSegment( \m, 0, 0 ); // a relative 'moveTo' segment to 0@0
SVGPathSegment( \L, 10, 10 ); // an absolute 'lineTo' segment to 10@10
SVGPathSegment( \LineTo, 10, 10 ); // an absolute 'lineTo' segment to 10@10
SVGPathSegment( \LineTo, 10@10 ); // an absolute 'lineTo' segment to 10@10
Note:
- arc segments will draw as straight lines for now when using .plot on SVGPath.
There would need to be a Pen.arcTo command to draw these arcs; Pen.addArc is not sufficient. Another way would be to convert the arcs to curves. Please email me (info@woutersnoei.nl) if you know how to do that.
- Meta_Pen:curveTo and Meta_Pen:quadCurveTo segments were added to the Pen commands for Cocoa curve drawing. SVGPath:plot uses these. They have 2 additional arguments which are required for correct drawing:
start - the start point of the curve
div - the number of steps (default 16). If you increase this number the curve will get smoother
x, y, x1, y1, x2, y2 etc..
all properties for types can be accessed as instance methods. These will produce 'nil' if a specific type doesn't use them. These methods can also set values.
SVGPathSegment( \L, 10, 10 ).x;
SVGPathSegment( \V, 10 ).x;
( SVGPathSegment( \H, 10 ).x = 20 ).x;
type
gets and sets the type, including conversion from shortcuts to full names.
SVGPathSegment( \L, 10, 10 ).type;
SVGPathSegment( \L, 10, 10 ).type = \m;
point
returns a Point containing x and y properties if available. Can also set
SVGPathSegment( \L, 10, 20 ).point;
( SVGPathSegment( \L, 10, 20 ).point = 100@20 );
asSVGPathSegment
convert Arrays and Points to SVGPathSegments
[ \l, 10, 100].asSVGPathSegment;
Point( 10,100 ).asSVGPathSegment( \lineTo );
SVGPathSegment.curveRes
curveRes is a class variable which defines the resolution of curves when drawn in SC Cocoa.
(default 16 steps per curve)
w = SVGPath( [ [ \M, 10, 10], [ \C, 400, 10, 10, 400, 300, 300] ], "test" ).plot;
SVGPathSegment.curveRes = 40; w.refresh; // smoother curve in the window
SVGPathSegment.curveRes = 16; w.refresh; // back to default; faster drawing
asArray
returns a standardized array which could be used as input arguments