Contour

Description

A Contour is a single path of any number of points. A Glyph usually consists of a couple of contours, and this is the object that represents each one. The Contour object offers access to the outline matter in various ways. The parent of Contour is usually Glyph.

Overview

Copy

BaseContour.copy

Copy the current object into a new object of the same type.

Parents

BaseContour.glyph

Get or set the contour's parent glyph object.

BaseContour.layer

Get the contour's parent layer object.

BaseContour.font

Get the contour's parent font object.

Identification

BaseContour.identifier

Get the object's unique identifier.

BaseContour.index

Get or set the index of the contour.

Winding Direction

BaseContour.clockwise

Specify or determine whether the contour's winding direction is clockwise.

BaseContour.reverse

Reverse the direction of the contour.

Queries

BaseContour.bounds

Get the bounds of the contour.

BaseContour.pointInside

Check if point is within the filled area of the contour.

Pens and Drawing

BaseContour.draw

Draw the contour's outline data to the given pen.

BaseContour.drawPoints

Draw the contour's outline data to the given point pen.

Segments

BaseContour.segments

Get the contour's segments.

BaseContour.__len__

Return the number of segments in the contour.

BaseContour.__iter__

Return an iterator over the segments in the contour.

BaseContour.__getitem__

Get the segment at the specified index.

BaseContour.appendSegment

Append the given segment to the contour.

BaseContour.insertSegment

Insert the given segment into the contour.

BaseContour.removeSegment

Remove the given segment from the contour.

BaseContour.setStartSegment

Set the first segment in the contour.

BaseContour.autoStartSegment

Automatically calculate and set the contour's first segment.

bPoints

BaseContour.bPoints

Get a list of all bPoints in the contour.

BaseContour.appendBPoint

Append the given bPoint to the contour.

BaseContour.insertBPoint

Insert the given bPoint into the contour.

Points

BaseContour.points

Get a list of all points in the contour.

BaseContour.appendPoint

Append the given point to the contour.

BaseContour.insertPoint

Insert the given point into the contour.

BaseContour.removePoint

Remove the given point from the contour.

Transformations

BaseContour.transformBy

Transform the object according to the given matrix.

BaseContour.moveBy

Move the object according to the given coordinates.

BaseContour.scaleBy

Scale the object according to the given values.

BaseContour.rotateBy

Rotate the object by the specified value.

BaseContour.skewBy

Skew the object by the given value.

Normalization

BaseContour.round

Round all point coordinates in the contour to the nearest integer.

Environment

BaseContour.naked

Return the environment's native object wrapped by the current object.

BaseContour.changed

Tell the environment that something has changed in the object.

Reference

class fontParts.base.BaseContour(*args: Any, **kwargs: Any)[source]

Represent the basis for a contour object.

Variables:
  • segmentClass – A class representing contour segments. This will usually be a BaseSegment subclass.

  • bPointClass – A class representing contour bPoints. This will usually be a BaseBPoint subclass.

Copy

BaseContour.copy() BaseObjectType

Copy the current object into a new object of the same type.

The returned object will not have a parent object.

Returns:

A new BaseObject subclass instance with the same attributes.

Parents

BaseContour.glyph: dynamicProperty

Get or set the contour’s parent glyph object.

The value must be a BaseGlyph instance or None.

Returns:

The BaseGlyph instance containing the contour or None.

Raises:

AssertionError – If attempting to set the glyph when it has already been set.

Example:

>>> glyph = contour.glyph
BaseContour.layer: dynamicProperty

Get the contour’s parent layer object.

This property is read-only.

Returns:

The BaseLayer instance containing the contour or None.

Example:

>>> layer = contour.layer
BaseContour.font: dynamicProperty

Get the contour’s parent font object.

This property is read-only.

Returns:

The BaseFont instance containing the contour or None.

Example:

>>> font = contour.font

Identification

BaseContour.identifier

Get the object’s unique identifier.

This attribute is read-only. Use IdentifierMixin.getIdentifier to request an identifier if it does not exist.

Returns:

The unique identifier assigned to the object as a str, or None indicating the object has no identifier.

Example

>>> object.identifier
'ILHGJlygfds'
BaseContour.index: dynamicProperty

Get or set the index of the contour.

The value must be an int.

Returns:

An int representing the contour’s index within an ordered list of the parent glyph’s contours, or None if the contour does not belong to a glyph.

Raises:

FontPartsError – If the contour does not belong to a glyph.

Example:

>>> contour.index
1
>>> contour.index = 0

Winding Direction

BaseContour.clockwise: dynamicProperty

Specify or determine whether the contour’s winding direction is clockwise.

The value must be a bool indicating the contour’s winding direction.

Returns:

True if the contour’s winding direction is clockwise, otherwise False.

BaseContour.reverse() None[source]

Reverse the direction of the contour.

Example:

>>> contour.clockwise
False
>>> contour.reverse()
>>> contour.clockwise
True

Queries

BaseContour.bounds: dynamicProperty

Get the bounds of the contour.

This property is read-only.

Returns:

A tuple of four int or float values in the form (x minimum, y minimum, x maximum, y maximum) representing the bounds of the contour, or None if the contour is open.

Example:

>>> contour.bounds
(10, 30, 765, 643)
BaseContour.pointInside(point: list[int | float] | tuple[int | float, int | float]) bool[source]

Check if point is within the filled area of the contour.

Parameters:

point – The point to check as a Coordinate.

Returns:

True if point is inside the filled area of the contour, False otherwise.

Example:

>>> contour.pointInside((40, 65))
True

Pens and Drawing

BaseContour.draw(pen: AbstractPen) None[source]

Draw the contour’s outline data to the given pen.

Parameters:

pen – The fontTools.pens.basePen.AbstractPen to which the outline data should be drawn.

Example:

>>> contour.draw(pen)
BaseContour.drawPoints(pen: AbstractPointPen) None[source]

Draw the contour’s outline data to the given point pen.

Parameters:

pen – The fontTools.pens.basePen.AbstractPointPen to which the outline data should be drawn.

Example:

>>> contour.drawPoints(pointPen)

Segments

BaseContour.segments: dynamicProperty

Get the contour’s segments.

This property is read-only.

Returns:

A tuple of BaseSegment instances.

Example:

>>> contour.segments
(<BaseSegment curve index='0' at 4573388368>, ...)
BaseContour.__len__() int[source]

Return the number of segments in the contour.

Returns:

An int representing the number of BaseSegment instances belonging to the contour.

BaseContour.__iter__() Iterator[BaseSegment][source]

Return an iterator over the segments in the contour.

Returns:

An iterator over the BaseSegment instances belonging to the contour.

BaseContour.__getitem__(index: int) BaseSegment[source]

Get the segment at the specified index.

Parameters:

index – The zero-based index of the point to retrieve as an int.

Returns:

The BaseSegment instance located at the specified index.

Raises:

IndexError – If the specified index is out of range.

BaseContour.appendSegment(type: str | None = None, points: PointCollectionType | None = None, smooth: bool = False, segment: BaseSegment | None = None) None[source]

Append the given segment to the contour.

If type or points are specified, those values will be used instead of the values in the given segment object. The specified smooth state will be applied if segment=None.

Parameters:
BaseContour.insertSegment(index: int, type: str | None = None, points: PointCollectionType | None = None, smooth: bool = False, segment: BaseSegment | None = None) None[source]

Insert the given segment into the contour.

If type or points are specified, those values will be used instead of the values in the given segment object. The specified smooth state will be applied if segment=None.

Parameters:
BaseContour.removeSegment(segment: BaseSegment | int, preserveCurve: bool = False) None[source]

Remove the given segment from the contour.

If preserveCurve=True, an attempt will be made to preserve the overall shape of the curve after the segment is removed, provided the environment supports such functionality.

Parameters:
  • segment – The segment to remove as a BaseSegment instance, or an int representing the segment’s index.

  • preserveCurve – A bool indicating whether to preserve the curve’s shape after the segment is removed. Defaults to False.

Raises:

ValueError – If the segment index is out of range or if the specified segment is not part of the contour.

Example:

>>> contour.removeSegment(mySegment)
>>> contour.removeSegment(2, preserveCurve=True)
BaseContour.setStartSegment(segment: BaseSegment | int) None[source]

Set the first segment in the contour.

Parameters:

segment – The segment to set as the first instance in the contour as a BaseSegment instance, or an int representing the segment’s index.

Raises:
  • FontPartsError – If the contour is open.

  • ValueError – If the segment index is out of range or if the specified segment is not part of the contour.

Example:

>>> contour.setStartSegment(mySegment)
>>> contour.setStartSegment(2)
BaseContour.autoStartSegment() None[source]

Automatically calculate and set the contour’s first segment.

The behavior of this may vary across environments.

Example:

>>> contour.autoStartSegment()

bPoints

BaseContour.bPoints: dynamicProperty

Get a list of all bPoints in the contour.

This property is read-only.

Returns:

A tuple of :class`BaseBPoints`.

BaseContour.appendBPoint(type: str | None = None, anchor: CoordinateLike | None = None, bcpIn: CoordinateLike | None = None, bcpOut: CoordinateLike | None = None, bPoint: BaseBPoint | None = None) None[source]

Append the given bPoint to the contour.

If type, anchor, bcpIn or bcpOut are specified, those values will be used instead of the values in the given segment object.

Parameters:
BaseContour.insertBPoint(index: int, type: str | None = None, anchor: CoordinateLike | None = None, bcpIn: CoordinateLike | None = None, bcpOut: CoordinateLike | None = None, bPoint: BaseBPoint | None = None) None[source]

Insert the given bPoint into the contour.

If type, anchor, bcpIn or bcpOut are specified, those values will be used instead of the values in the given segment object.

Parameters:

Points

BaseContour.points: dynamicProperty

Get a list of all points in the contour.

This property is read-only.

Returns:

A tuple of :class`BasePoints`.

BaseContour.appendPoint(position: CoordinateLike | None = None, type: str = 'line', smooth: bool = False, name: str | None = None, identifier: str | None = None, point: BasePoint | None = None) None[source]

Append the given point to the contour.

If position, type or name are specified, those values will be used instead of the values in the given segment object. The specified smooth state will be applied if point=None.

Parameters:
BaseContour.insertPoint(index: int, position: CoordinateLike | None = None, type: str = 'line', smooth: bool = False, name: str | None = None, identifier: str | None = None, point: BasePoint | None = None) None[source]

Insert the given point into the contour.

If position, type or name are specified, those values will be used instead of the values in the given segment object. The specified smooth state will be applied if point=None.

Parameters:
BaseContour.removePoint(point: BasePoint | int, preserveCurve: bool = False) None[source]

Remove the given point from the contour.

If preserveCurve=True, an attempt will be made to preserve the overall shape of the curve after the segment is removed, provided the environment supports such functionality.

Parameters:
  • point – The point to remove as a BasePoint instance, or an int representing the point’s index.

  • preserveCurve – A bool indicating whether to preserve the curve’s shape after the point is removed. Defaults to False.

Raises:

ValueError – If the point index is out of range or if the specified point is not part of the contour.

Example:

>>> contour.removePoint(myPoint)
>>> contour.removePoint(2, preserveCurve=True)

Transformations

BaseContour.transformBy(matrix: list[int | float] | tuple[float, float, float, float, float, float], origin: list[int | float] | tuple[int | float, int | float] | None = None) None

Transform the object according to the given matrix.

Parameters:
  • matrix – The Transformation Matrix to apply.

  • origin – The optional point at which the transformation should originate as a:ref:type-coordinate. Defaults to None, representing an origin of (0, 0).

Example:

>>> obj.transformBy((0.5, 0, 0, 2.0, 10, 0))
>>> obj.transformBy((0.5, 0, 0, 2.0, 10, 0), origin=(500, 500))
BaseContour.moveBy(value: list[int | float] | tuple[int | float, int | float]) None

Move the object according to the given coordinates.

Parameters:

value – The x and y values to move the object by as a Coordinate.

Example:

>>> obj.moveBy((10, 0))
BaseContour.scaleBy(value: int | float | list[int | float] | tuple[float, float], origin: list[int | float] | tuple[int | float, int | float] | None = None) None

Scale the object according to the given values.

Parameters:
  • value – The value to scale the glyph by as a single int or float, or a tuple or list of two int or float values representing the values (x, y).

  • origin – The optional point at which the scale should originate as a Coordinate. Defaults to None, representing an origin of (0, 0).

Example:

>>> obj.scaleBy(2.0)
>>> obj.scaleBy((0.5, 2.0), origin=(500, 500))
BaseContour.rotateBy(value: int | float, origin: list[int | float] | tuple[int | float, int | float] | None = None) None

Rotate the object by the specified value.

Parameters:
  • value – The angle at which to rotate the object as an int or a float.

  • origin – The optional point at which the rotation should originate as a Coordinate. Defaults to None, representing an origin of (0, 0).

Example:

>>> obj.rotateBy(45)
>>> obj.rotateBy(45, origin=(500, 500))
BaseContour.skewBy(value: int | float | list[int | float] | tuple[float, float], origin: list[int | float] | tuple[int | float, int | float] | None = None) None

Skew the object by the given value.

Parameters:
  • value – The value by which to skew the object as either a single int or float corresponding to the x direction, or a tuple of two int or float values corresponding to the x and y directions.

  • origin – The optional point at which the rotation should originate as a Coordinate. Defaults to None, representing an origin of (0, 0).

Example:

>>> obj.skewBy(11)
>>> obj.skewBy((25, 10), origin=(500, 500))

Normalization

BaseContour.round() None[source]

Round all point coordinates in the contour to the nearest integer.

Example:

>>> contour.round()

Environment

BaseContour.naked() Any

Return the environment’s native object wrapped by the current object.

Raises:

NotImplementedError – If the method has not been overridden by a subclass.

Example:

>>> loweLevelObj = obj.naked()
BaseContour.changed(*args: Any, **kwargs: Any) None

Tell the environment that something has changed in the object.

The behavior of this method will vary from environment to environment.

Parameters:
  • *args – Any positional arguments.

  • **kwargs – Any keyword arguments.

Example:

>>> obj.changed()