kiva.abstract_graphics_context module

class kiva.abstract_graphics_context.AbstractGraphicsContext[source]

Bases: object

Abstract Base Class for Kiva Graphics Contexts

abstract add_path(compiled_path)[source]

Add the current path to a compiled path

abstract arc(x, y, radius, start_angle, end_angle, cw=False)[source]

Draw a circular arc of the given radius, centered at (x, y)

The angular span is from start_angle to end_angle, where angles are measured counter-clockwise from the positive X axis.

If “cw” is True, then the arc is swept from the end_angle back to the start_angle (it does not change the sense in which the angles are measured, but may affect rendering based on winding number calculations).

abstract arc_to(x1, y1, x2, y2, radius)[source]

Draw a circular arc from current point to tangent line

The arc is tangent to the line from the current point to (x1, y1), and it is also tangent to the line from (x1, y1) to (x2, y2). (x1, y1) is the imaginary intersection point of the two lines tangent to the arc at the current point and at (x2, y2).

If the tangent point on the line from the current point to (x1, y1) is not equal to the current point, a line is drawn to it. Depending on the supplied radius, the tangent point on the line from (x1, y1) to (x2, y2) may or may not be (x2, y2). In either case, the arc is drawn to the point of tangency, which is also the new current point.

Consider the common case of rounding a rectangle’s upper left corner. Let “r” be the radius of rounding. Let the current point be (x_left + r, y_top). Then (x2, y2) would be (x_left, y_top - radius), and (x1, y1) would be (x_left, y_top).

abstract begin_page()[source]

Start rendering in a new page

abstract begin_path()[source]

Start a new path

abstract clear_rect(rect)[source]

Set rectangle to background colour

Note

This may not be available in some backends, such as PDF or PostScript.

abstract clip()[source]

Set the clipping region to the current path

abstract clip_to_rect(rect)[source]

Set the clipping region to the specified rectangle

abstract clip_to_rects(rect_array)[source]

Set the clipping region to the collection of rectangles

abstract close_path()[source]

Finish a subpath, connecting back to the start

abstract concat_ctm(matrix)[source]

Concatenate an arbitrary affine matrix to the current transformation matrix.

abstract curve_to(x1, y1, x2, y2, end_x, end_y)[source]

Draw a cubic bezier curve

The curve starts from the current point and ends at (end_x, end_y), with control points (x1, y1) and (x2, y2).

abstract draw_image(image, rect=None)[source]

Render an image into a rectangle

Parameters
  • image – An image. Can be a numpy array, a PIL Image instance, or another GraphicsContext instance.

  • rect – A tuple (x, y, w, h). If not specified then the bounds of the the graphics context are used as the rectangle.

abstract draw_path(draw_mode=5)[source]

Draw the current path with the specified mode

Parameters

draw_mode – One of FILL, EOF_FILL, STROKE, FILL_STROKE, or EOF_FILL_STROKE. Each is defined in kiva.api.

abstract draw_rect(rect, draw_mode=5)[source]

Draw a rectangle with the specified mode

Parameters
  • rect – A tuple (x, y, w, h)

  • draw_mode – One of FILL, EOF_FILL, STROKE, FILL_STROKE, or EOF_FILL_STROKE. Each is defined in kiva.api.

abstract end_page()[source]

Finish rendering in a page

abstract eof_fill_path()[source]

Fill the current path with fill settings from the current state

This fills using the even-odd rule filling algorithm

abstract even_odd_clip()[source]

Modify clipping region with current path using even-odd rule

abstract fill_path()[source]

Fill the current path with fill settings from the current state

This fills using the nonzero rule filling algorithm

abstract flush()[source]

Render all pending draw operations immediately

This only makes sense in GUI window contexts (eg. Quartz or QPainter).

abstract get_alpha(alpha)[source]

Return the alpha used when drawing

abstract get_antialias()[source]

Set whether or not to antialias when drawing

abstract get_character_spacing()[source]

Get the current spacing between characters when drawing text

abstract get_ctm()[source]

Get the current transformation matrix

abstract get_empty_path()[source]

Get an empty CompiledPath instance

abstract get_fill_color()[source]

Get the color used to fill the region bounded by a path or when drawing text.

abstract get_font()[source]

Get the current font

abstract get_full_text_extent(string)[source]

Get the text extent as a tuple (w, h, x, y)

Note

This method is deprecated: you should use get_text_extent() instead. This order is provided for backwards-compatibility with existing Enable code.

abstract get_image_interpolation()[source]

Get the type of interpolation to use when scaling images

abstract get_stroke_color()[source]

Get the current color used when stroking a path

abstract get_text_extent(text)[source]

Return a rectangle which encloses the specified text

The rectangle (x, y, w, h) is relative to an origin which is at the baseline of the text and at the left of the first character rendered. In other words, x is the leading and y the descent.

abstract get_text_matrix()[source]

Get the transformation matrix to use when drawing text

abstract get_text_position()[source]

Get the current point where text will be drawn

abstract height()[source]

Returns the height of the graphics context.

abstract line_set(starts, ends)[source]

Adds a set of disjoint lines as a new subpath.

Parameters
  • starts – an Nx2 array of x,y pairs

  • ends – an Nx2 array of x,y pairs

starts and ends arrays should have the same length. The current point is moved to the last point in ends.

abstract line_to(x, y)[source]

Add a line from the current point to (x, y) to the path

abstract linear_gradient(x1, y1, x2, y2, stops, spread_method, units='userSpaceOnUse')[source]

Modify the fill color to be a linear gradient

Parameters
  • x1 – The X starting point of the gradient.

  • y1 – The Y starting point of the gradient.

  • x2 – The X ending point of the gradient.

  • y3 – The Y ending point of the gradient.

  • stops – An array/sequence of color stops ((offset, r, g, b, a), (offset2, r2, g2, b2, a2), …) where offset is some number between 0 and 1 inclusive and the entries are sorted from lowest offset to highest.

  • spread_method – One of the following strings: “pad”, “reflect”, “repeat”.

  • units – One of the following strings: “userSpaceOnUse”, “objectBoundingBox”.

abstract lines(points)[source]

Adds a series of lines as a new subpath.

Parameters

points – an Nx2 sequence of (x, y) pairs

The current point is moved to the last point in points.

abstract move_to(x, y)[source]

Move the current point on the path without drawing

abstract quad_curve_to(cp_x, cp_y, end_x, end_y)[source]

Draw a quadratic bezier curve

The curve starts the current point and ends at (end_x, end_y), with control point (cp_x, cp_y)

abstract radial_gradient(cx, cy, r, fx, fy, stops, spread_method, units='userSpaceOnUse')[source]

Modify the fill color to be a radial gradient

Parameters
  • cx – The X center point of the gradient.

  • cy – The Y center point of the gradient.

  • r – The radius of the gradient

  • fx – The X ending point of the gradient.

  • fy – The Y ending point of the gradient.

  • stops – An array/sequence of color stops ((offset, r, g, b, a), (offset2, r2, g2, b2, a2), …) where offset is some number between 0 and 1 inclusive and the entries are sorted from lowest offset to highest.

  • spread_method – One of the following strings: “pad”, “reflect”, “repeat”.

  • units – One of the following strings: “userSpaceOnUse”, “objectBoundingBox”.

abstract rect(x, y, w, h)[source]

Add a rectangle as a new sub-path

Parameters
  • x – The left X coordinate of the rectangle

  • y – The bottom Y coordinate of the rectangle

  • w – The width of the rectangle

  • h – The height of the rectangle

abstract rects(rect_array)[source]

Add a sequence of rectangles as separate sub-paths.

Parameters

rect_array – An Nx4 array of (x, y, w, h) quadruples

abstract restore_state()[source]

Pop the previous graphics state from the stack

abstract rotate_ctm(angle)[source]

Concatenate a rotation to the current transformation matrix.

Parameters

angle – An angle in radians.

abstract save(filename, file_format=None, pil_options=None)[source]

Save the graphics context to a file

Data is always saved in RGB or RGBA format, and converted to that format if not already in it.

If the file_format argument is None, then the file format is inferred from the filename extension, and so is not usually needed.

The pil_options argument is a dictionary of format-specific options that can be passed directly to PIL’s image file writers. For example, this can be used to control the compression level of JPEG or PNG output. Unrecognized options are silently ignored.

abstract save_state()[source]

Push the current graphics state onto the stack

abstract scale_ctm(x_scale, y_scale)[source]

Concatenate a scaling to the current transformation matrix

abstract select_font(name, size=12, style='regular', encoding=None)[source]

Set the font based on the provided parameters

Parameters
  • name – The name of a font. E.g.: “Times New Roman”

  • size – The font size in points.

  • style – One of “regular”, “bold”, “italic”, “bold italic”

  • encoding

    A 4 letter encoding name. Common ones are:

    • ”unic” – unicode

    • ”armn” – apple roman

    • ”symb” – symbol

    Not all fonts support all encodings. If none is specified, fonts that have unicode encodings default to unicode. Symbol is the second choice. If neither are available, the encoding defaults to the first one returned in the FreeType charmap list for the font face.

abstract set_alpha(alpha)[source]

Set the alpha to use when drawing

abstract set_antialias(antialias)[source]

Set whether or not to antialias when drawing

abstract set_character_spacing(spacing)[source]

Set the spacing between characters when drawing text

Parameters

spacing (float) – units of space extra space to add between text coordinates. It is specified in text coordinate system.

abstract set_ctm(matrix)[source]

Set the current transformation matrix

abstract set_fill_color(color)[source]

Set the color used to fill the region bounded by a path or when drawing text.

Parameters

color – A three or four component tuple describing a color (R, G, B[, A]). Each color component should be in the range [0.0, 1.0]

abstract set_flatness(flatness)[source]

Set the error tolerance when drawing curved paths

abstract set_font(font)[source]

Set the font with a kiva.fonttools.font.Font object

abstract set_font_size(size)[source]

Set the size of the current font

abstract set_image_interpolation(interpolation)[source]

Set the type of interpolation to use when scaling images.

Parameters

interpolation – One of “nearest”, “bilinear”, “bicubic”, “spline16”, “spline36”, “sinc64”, “sinc144”, “sinc256”, “blackman64”, “blackman100”, or “blackman256”.

abstract set_line_cap(line_cap)[source]

Set the style of cap to use a path ends

Parameters

line_cap – One of CAP_BUTT, CAP_ROUND, or CAP_SQUARE. Each is defined in kiva.api.

abstract set_line_dash(line_dash, phase=0)[source]

Set the dash style to use when stroking a path

Parameters
  • line_dash – An even-lengthed tuple of floats that represents the width of each dash and gap in the dash pattern.

  • phase (float) – Specifies how many units into the dash pattern to start.

abstract set_line_join(line_join)[source]

Set the style of join to use a path corners

Parameters

line_join – Options are JOIN_ROUND, JOIN_BEVEL, or JOIN_MITER. Each is defined in kiva.api.

abstract set_line_width(width)[source]

Set the width of the pen used to stroke a path

abstract set_miter_limit(miter_limit)[source]

Set the limit at which mitered joins are flattened.

Only applicable when the line join type is set to JOIN_MITER.

abstract set_stroke_color(color)[source]

Set the color used when stroking a path

Parameters

color – A three or four component tuple describing a color (R, G, B[, A]). Each color component should be in the range [0.0, 1.0]

abstract set_text_drawing_mode(draw_mode)[source]

Set the drawing mode to use with text

Parameters

draw_mode – Allowed values are TEXT_FILL, TEXT_STROKE, TEXT_FILL_STROKE, TEXT_INVISIBLE, TEXT_FILL_CLIP, TEXT_STROKE_CLIP, TEXT_FILL_STROKE_CLIP, or TEXT_CLIP. Each is defined in kiva.api.

abstract set_text_matrix(text_matrix)[source]

Set the transformation matrix to use when drawing text

abstract set_text_position(x, y)[source]

Set the current point for drawing text

This point is on the baseline of the text

abstract show_text(text, point=None)[source]

Draw the specified string at the current point

abstract show_text_at_point(x, y)[source]

Draw text at the absolute position specified by the point

abstract stroke_path()[source]

Stroke the current path with pen settings from current state

abstract synchronize()[source]

A deferred version of flush()

Also only relevant in window contexts.

abstract translate_ctm(x, y)[source]

Concatenate a translation to the current transformation matrix

abstract width()[source]

Returns the width of the graphics context.

class kiva.abstract_graphics_context.EnhancedAbstractGraphicsContext[source]

Bases: kiva.abstract_graphics_context.AbstractGraphicsContext

ABC for graphics contexts which provide additional methods

abstract draw_marker_at_points(point_array, size, marker=1)[source]

Draw a marker at a collection of points

Parameters
  • point_array – An Nx2 array of x,y points

  • size – The size of the marker in points.

  • marker – One of NO_MARKER, SQUARE_MARKER, DIAMOND_MARKER, CIRCLE_MARKER, CROSSED_CIRCLE_MARKER, CROSS_MARKER, TRIANGLE_MARKER, INVERTED_TRIANGLE_MARKER, PLUS_MARKER, DOT_MARKER, or PIXEL_MARKER. Each is defined in kiva.api.

Returns

result – True if markers were drawn, False otherwise.

Return type

bool

abstract draw_path_at_points(point_array, compiled_path, draw_mode=5)[source]

Draw a compiled path at a collection of points

The starting point of the paths are specified by the points, and the drawing mode is specified by the third argument.

Parameters
  • point_array – An Nx2 array of x,y points

  • compiled_path – A CompiledPath instance.

  • draw_mode – One of FILL, EOF_FILL, STROKE, FILL_STROKE, or EOF_FILL_STROKE. Each is defined in kiva.api.