Skip to content

Freehand Context

keyed_extras.freehand

A Cairo context for emulating hand drawn shapes.

This code was adapted/extended from the following sources: - Gaphor/Gaphas, most of the implementation - Apache License 2.0 - https://github.com/gaphor/gaphas/blob/main/LICENSES/Apache-2.0.txt - https://github.com/gaphor/gaphas/blob/main/gaphas/painter/freehand.py - Steve Hanov, the original author - Steve Hanov 2008, Released to the public domain. - http://stevehanov.ca/blog/index.php?id=33 and - http://stevehanov.ca/blog/index.php?id=93

FreeHandContext

Bases: base

A Cairo context that looks hand drawn.

This object proxies an underlying Cairo context for methods not implemented directly.

Parameters:

Name Type Description Default
cr Context

The cairo context to proxy.

required
sloppiness float

In range 0.0 .. 2.0

0.5

line_to

line_to(x, y)

Draw a line to the provided point.

Parameters:

Name Type Description Default
x float

x coordinate

required
y float

y coordinate

required

Returns:

Type Description
None

None

rel_line_to

rel_line_to(dx, dy)

Relative version of line_to.

Parameters:

Name Type Description Default
dx float

Delta x

required
dy float

Delta y

required

Returns:

Type Description
None

None

curve_to

curve_to(x1, y1, x2, y2, x3, y3)

Draw a bezier curve using the provided control points.

Parameters:

Name Type Description Default
x1 float

The x coordinate of the first control point

required
y1 float

The y coordinate of the first control point

required
x2 float

The x coordinate of the second control point

required
y2 float

The y coordinate of the second control point

required
x3 float

the x coordinate of the end of the curve

required
y3 float

the y coordinate of the end of the curve

required

Returns:

Type Description
None

None

rel_curve_to

rel_curve_to(dx1, dy1, dx2, dy2, dx3, dy3)

Relative version of curve_to.

Parameters:

Name Type Description Default
dx1 float

The x offset of the first control point

required
dy1 float

The y offset of the first control point

required
dx2 float

The x offset of the second control point

required
dy2 float

The y offset of the second control point

required
dx3 float

the x offset of the end of the curve

required
dy3 float

the y offset of the end of the curve

required

Returns:

Type Description
None

None

rectangle

rectangle(x, y, width, height)

Draw a rectangle.

Parameters:

Name Type Description Default
x float

The x coordinate of the top left of the rectangle

required
y float

The y coordinate of the top left of the rectangle

required
width float

The width of the rectangle

required
height float

The height of the rectangle

required

Returns:

Type Description
None

None

arc

arc(xc, yc, radius, angle1, angle2)

Draw an arc about a center with the provided radius from angle1 to angle2.

This splits the arc into multiple segments, and then draws curves between them.

Parameters:

Name Type Description Default
xc float

x position of the center of the arc

required
yc float

y position of the center of the arc

required
radius float

the radius of the arc

required
angle1 float

the start angle, in radians

required
angle2 float

the end angle, in radians

required

Returns:

Type Description
None

None