BezierPath

BezierPath(path=None, glyphSet=None)

Return a BezierPath object. This is a reusable object, if you want to draw the same over and over again.

# create a bezier path
path = BezierPath()

# move to a point
path.moveTo((100, 100))
# line to a point
path.lineTo((100, 200))
path.lineTo((200, 200))
# close the path
path.closePath()

# loop over a range of 10
for i in range(10):
    # set a random color with alpha value of .3
    fill(random(), random(), random(), .3)
    # in each loop draw the path
    drawPath(path)
    # translate the canvas
    translate(50, 50)

path.text("Hello world", font="Helvetica", fontSize=30, offset=(210, 210))

print("All Points:")
print(path.points)

print("On Curve Points:")
print(path.onCurvePoints)

print("Off Curve Points:")
print(path.offCurvePoints)

# print out all points from all segments in all contours
for contour in path.contours:
    for segment in contour:
        for x, y in segment:
            print((x, y))
    print(["contour is closed", "contour is open"][contour.open])

# translate the path
path.translate(0, -100)
# draw the path again
drawPath(path)
# translate the path
path.translate(-300, 0)
path.scale(2)
# draw the path again
drawPath(path)
class BezierPath(path=None, glyphSet=None)

Bases: Mock, drawBot.context.baseContext.SVGContextPropertyMixin, drawBot.context.baseContext.ContextPropertyMixin

A bezier path object, if you want to draw the same over and over again.

contourClass

alias of BezierContour

moveTo(point)

Move to a point x, y.

lineTo(point)

Line to a point x, y.

curveTo(*points)

Draw a cubic bezier with an arbitrary number of control points.

The last point specified is on-curve, all others are off-curve (control) points.

qCurveTo(*points)

Draw a whole string of quadratic curve segments.

The last point specified is on-curve, all others are off-curve (control) points.

closePath()

Close the path.

beginPath(identifier=None)

Begin using the path as a so called point pen and start a new subpath.

addPoint(point, segmentType=None, smooth=False, name=None, identifier=None, **kwargs)

Use the path as a point pen and add a point to the current subpath. beginPath must have been called prior to adding points with addPoint calls.

endPath()

End the current subpath. Calling this method has two distinct meanings depending on the context:

When the bezier path is used as a segment pen (using moveTo, lineTo, etc.), the current subpath will be finished as an open contour.

When the bezier path is used as a point pen (using beginPath, addPoint and endPath), the path will process all the points added with addPoint, finishing the current subpath.

addComponent(glyphName, transformation)

Add a sub glyph. The ‘transformation’ argument must be a 6-tuple containing an affine transformation, or a Transform object from the fontTools.misc.transform module. More precisely: it should be a sequence containing 6 numbers.

A glyphSet is required during initialization of the BezierPath object.

drawToPen(pen)

Draw the bezier path into a pen

drawToPointPen(pointPen)

Draw the bezier path into a point pen.

arc(center, radius, startAngle, endAngle, clockwise)

Arc with center and a given radius, from startAngle to endAngle, going clockwise if clockwise is True and counter clockwise if clockwise is False.

arcTo(point1, point2, radius)

Arc defined by a circle inscribed inside the angle specified by three points: the current point, point1, and point2. The arc is drawn between the two points of the circle that are tangent to the two legs of the angle.

rect(x, y, w, h)

Add a rectangle at possition x, y with a size of w, h

oval(x, y, w, h)

Add a oval at possition x, y with a size of w, h

line(point1, point2)

Add a line between two given points.

polygon(*points, **kwargs)

Draws a polygon with n-amount of points. Optionally a close argument can be provided to open or close the path. As default a polygon is a closed path.

text(txt, offset=None, font='LucidaGrande', fontSize=10, align=None, fontNumber=0)

Draws a txt with a font and fontSize at an offset in the bezier path. If a font path is given the font will be installed and used directly.

Optionally an alignment can be set. Possible align values are: “left”, “center” and “right”.

The default alignment is left.

Optionally txt can be a FormattedString.

textBox(txt, box, font='LucidaGrande', fontSize=10, align=None, hyphenation=None, fontNumber=0)

Draws a txt with a font and fontSize in a box in the bezier path. If a font path is given the font will be installed and used directly.

Optionally an alignment can be set. Possible align values are: “left”, “center” and “right”.

The default alignment is left.

Optionally hyphenation can be provided.

Optionally txt can be a FormattedString. Optionally box can be a BezierPath.

traceImage(path, threshold=0.2, blur=None, invert=False, turd=2, tolerance=0.2, offset=None)

Convert a given image to a vector outline.

Optionally some tracing options can be provide:

  • threshold: the threshold used to bitmap an image
  • blur: the image can be blurred
  • invert: invert to the image
  • turd: the size of small turd that can be ignored
  • tolerance: the precision tolerance of the vector outline
  • offset: add the traced vector outline with an offset to the BezierPath
getNSBezierPath()

Return the nsBezierPath.

setNSBezierPath(path)

Set a nsBezierPath.

pointInside(xy)

Check if a point x, y is inside a path.

bounds()

Return the bounding box of the path in the form (x minimum, y minimum, x maximum, y maximum)` or, in the case of empty path None.

controlPointBounds()

Return the bounding box of the path including the offcurve points in the form (x minimum, y minimum, x maximum, y maximum)` or, in the case of empty path None.

optimizePath()
copy()

Copy the bezier path.

reverse()

Reverse the path direction

appendPath(otherPath)

Append a path.

translate(x=0, y=0)

Translate the path with a given offset.

rotate(angle, center=(0, 0))

Rotate the path around the center point (which is the origin by default) with a given angle in degrees.

scale(x=1, y=None, center=(0, 0))

Scale the path with a given x (horizontal scale) and y (vertical scale).

If only 1 argument is provided a proportional scale is applied.

The center of scaling can optionally be set via the center keyword argument. By default this is the origin.

skew(angle1, angle2=0, center=(0, 0))

Skew the path with given angle1 and angle2.

If only one argument is provided a proportional skew is applied.

The center of skewing can optionally be set via the center keyword argument. By default this is the origin.

transform(transformMatrix, center=(0, 0))

Transform a path with a transform matrix (xy, xx, yy, yx, x, y).

union(other)

Return the union between two bezier paths.

removeOverlap()

Remove all overlaps in a bezier path.

difference(other)

Return the difference between two bezier paths.

intersection(other)

Return the intersection between two bezier paths.

xor(other)

Return the xor between two bezier paths.

intersectionPoints(other=None)

Return a list of intersection points as x, y tuples.

Optionaly provide an other path object to find intersection points.

expandStroke(width, lineCap='round', lineJoin='round', miterLimit=10)

Returns a new bezier path with an expanded stroke around the original path, with a given width. Note: the new path will not contain the original path.

The following optional arguments are available with respect to line caps and joins: * lineCap: Possible values are “butt”, “square” or “round” * lineJoin: Possible values are “bevel”, “miter” or “round” * miterLimit: The miter limit to use for “miter” lineJoin option

points

Return an immutable list of all points in the BezierPath as point coordinate (x, y) tuples.

onCurvePoints

Return an immutable list of all on curve points in the BezierPath as point coordinate (x, y) tuples.

offCurvePoints

Return an immutable list of all off curve points in the BezierPath as point coordinate (x, y) tuples.

contours

Return an immutable list of contours with all point coordinates sorted in segments. A contour object has an open attribute.

svgClass

The svg class, as a string.

svgID

The svg id, as a string.

The svg link, as a string.