Shape Functions

class pyonfx.shape.Shape(drawing_cmds)[source]

This class can be used to define a Shape object (by passing its drawing commands) and then apply functions to it in order to accomplish some tasks, like analyzing its bounding box, apply transformations, splitting curves into segments…

Parameters

drawing_cmds (str) – The shape’s drawing commands in ASS format as a string.

has_error()[source]

Utility function that checks if the shape is valid.

Returns

False if no error has been found, else a string with the first error encountered.

map(fun)[source]

Sends every point of a shape through given transformation function to change them.

Tips: Working with outline points can be used to deform the whole shape and make f.e. a wobble effect.

Parameters

fun (function) – A function with two (or optionally three) parameters. It will define how each coordinate will be changed. The first two parameters represent the x and y coordinates of each point. The third optional it represents the type of each point (move, line, bezier…).

Returns

A pointer to the current object.

Examples

original = Shape("m 0 0 l 20 0 20 10 0 10")
print ( original.map(lambda x, y: (x+10, y+5) ) )
>>> m 10 5 l 30 5 30 15 10 15
bounding()[source]

Calculates shape bounding box.

Tips: Using this you can get more precise information about a shape (width, height, position).

Returns

A tuple (x0, y0, x1, y1) containing coordinates of the bounding box.

Examples

print("Left-top: %d %d\nRight-bottom: %d %d" % ( Shape("m 10 5 l 25 5 25 42 10 42").bounding() ) )
>>> Left-top: 10 5
>>> Right-bottom: 25 42
move(x=None, y=None)[source]

Moves shape coordinates in given direction.

If neither x and y are passed, it will automatically center the shape to the origin (0,0).
This function is an high level function, it just uses Shape.map, which is more advanced. Additionally, it is an easy way to center a shape.
Parameters
  • x (int or float) – Displacement along the x-axis.

  • y (int or float) – Displacement along the y-axis.

Returns

A pointer to the current object.

Examples

print( Shape("m 0 0 l 30 0 30 20 0 20").move(-5, 10) )
>>> m -5 10 l 25 10 25 30 -5 30
flatten(tolerance=1.0)[source]

Splits shape’s bezier curves into lines.

This is a low level function. Instead, you should use split() which already calls this function.
Parameters

tolerance (float) – Angle in degree to define a curve as flat (increasing it will boost performance during reproduction, but lower accuracy)

Returns

A pointer to the current object.

Returns

The shape as a string, with bezier curves converted to lines.

split(max_len=16, tolerance=1.0)[source]

Splits shape bezier curves into lines and splits lines into shorter segments with maximum given length.

Tips: You can call this before using :func:`map` to work with more outline points for smoother deforming.

Parameters
  • max_len (int or float) – The max length that you want all the lines to be

  • tolerance (float) – Angle in degree to define a bezier curve as flat (increasing it will boost performance during reproduction, but lower accuracy)

Returns

A pointer to the current object.

Examples

print( Shape("m -100.5 0 l 100 0 b 100 100 -100 100 -100.5 0 c").split() )
>>> m -100.5 0 l -100 0 -90 0 -80 0 -70 0 -60 0 -50 0 -40 0 -30 0 -20 0 -10 0 0 0 10 0 20 0 30 0 40 0 50 0 60 0 70 0 80 0 90 0 100 0 l 99.964 2.325 99.855 4.614 99.676 6.866 99.426 9.082 99.108 11.261 98.723 13.403 98.271 15.509 97.754 17.578 97.173 19.611 96.528 21.606 95.822 23.566 95.056 25.488 94.23 27.374 93.345 29.224 92.403 31.036 91.405 32.812 90.352 34.552 89.246 36.255 88.086 37.921 86.876 39.551 85.614 41.144 84.304 42.7 82.945 44.22 81.54 45.703 80.088 47.15 78.592 48.56 77.053 49.933 75.471 51.27 73.848 52.57 72.184 53.833 70.482 55.06 68.742 56.25 66.965 57.404 65.153 58.521 63.307 59.601 61.427 60.645 59.515 61.652 57.572 62.622 55.599 63.556 53.598 64.453 51.569 65.314 49.514 66.138 47.433 66.925 45.329 67.676 43.201 68.39 41.052 69.067 38.882 69.708 36.692 70.312 34.484 70.88 32.259 71.411 27.762 72.363 23.209 73.169 18.61 73.828 13.975 74.341 9.311 74.707 4.629 74.927 -0.062 75 -4.755 74.927 -9.438 74.707 -14.103 74.341 -18.741 73.828 -23.343 73.169 -27.9 72.363 -32.402 71.411 -34.63 70.88 -36.841 70.312 -39.033 69.708 -41.207 69.067 -43.359 68.39 -45.49 67.676 -47.599 66.925 -49.683 66.138 -51.743 65.314 -53.776 64.453 -55.782 63.556 -57.759 62.622 -59.707 61.652 -61.624 60.645 -63.509 59.601 -65.361 58.521 -67.178 57.404 -68.961 56.25 -70.707 55.06 -72.415 53.833 -74.085 52.57 -75.714 51.27 -77.303 49.933 -78.85 48.56 -80.353 47.15 -81.811 45.703 -83.224 44.22 -84.59 42.7 -85.909 41.144 -87.178 39.551 -88.397 37.921 -89.564 36.255 -90.68 34.552 -91.741 32.812 -92.748 31.036 -93.699 29.224 -94.593 27.374 -95.428 25.488 -96.205 23.566 -96.92 21.606 -97.575 19.611 -98.166 17.578 -98.693 15.509 -99.156 13.403 -99.552 11.261 -99.881 9.082 -100.141 6.866 -100.332 4.614 -100.452 2.325 -100.5 0
static ring(out_r, in_r)[source]

Returns a shape object of a ring with given inner and outer radius, centered around (0,0).

Tips: A ring with increasing inner radius, starting from 0, can look like an outfading point.

Parameters
  • out_r (int or float) – The outer radius for the ring.

  • in_r (int or float) – The inner radius for the ring.

Returns

A shape object representing a ring.

static ellipse(w, h)[source]

Returns a shape object of an ellipse with given width and height, centered around (0,0).

Tips: You could use that to create rounded stribes or arcs in combination with blurring for light effects.

Parameters
  • w (int or float) – The width for the ellipse.

  • h (int or float) – The height for the ellipse.

Returns

A shape object representing an ellipse.

static heart(size, offset=0)[source]

Returns a shape object of a heart object with given size (width&height) and vertical offset of center point, centered around (0,0).

Tips: An offset=size*(2/3) results in a splitted heart.

Parameters
  • size (int or float) – The width&height for the heart.

  • offset (int or float) – The vertical offset of center point.

Returns

A shape object representing an heart.

static star(edges, inner_size, outer_size)[source]

Returns a shape object of a star object with given number of outer edges and sizes, centered around (0,0).

Tips: Different numbers of edges and edge distances allow individual n-angles.

Parameters
  • edges (int) – The number of edges of the star.

  • inner_size (int or float) – The inner edges distance from center.

  • outer_size (int or float) – The outer edges distance from center.

Returns

A shape object as a string representing a star.

static glance(edges, inner_size, outer_size)[source]

Returns a shape object of a glance object with given number of outer edges and sizes, centered around (0,0).

Tips: Glance is similar to Star, but with curves instead of inner edges between the outer edges.

Parameters
  • edges (int) – The number of edges of the star.

  • inner_size (int or float) – The inner edges distance from center.

  • outer_size (int or float) – The control points for bezier curves between edges distance from center.

Returns

A shape object as a string representing a glance.

static rectangle(w=1.0, h=1.0)[source]

Returns a shape object of a rectangle with given width and height, centered around (0,0).

Tips: A rectangle with width=1 and height=1 is a pixel.

Parameters
  • w (int or float) – The width for the rectangle.

  • h (int or float) – The height for the rectangle.

Returns

A shape object representing an rectangle.

static triangle(size)[source]

Returns a shape object of an equilateral triangle with given side length, centered around (0,0).

Parameters

size (int or float) – The side length for the triangle.

Returns

A shape object representing an triangle.