Glyph

Description

The Glyph object represents a glyph, its parts and associated data.

Glyph can be used as a list of Contour objects.

When a Glyph is obtained from a Font object, the font is the parent object of the glyph.

Overview

Copy

BaseGlyph.copy Copy the glyph into a new glyph that does not belong to a glyph.

Parents

BaseGlyph.layer The glyph’s parent layer.
BaseGlyph.font The glyph’s parent font.

Identification

BaseGlyph.name The glyph’s name.
BaseGlyph.unicodes The glyph’s unicode values in order from most to least important.
BaseGlyph.unicode The glyph’s primary unicode value.

Metrics

BaseGlyph.width The glyph’s width.
BaseGlyph.leftMargin The glyph’s left margin.
BaseGlyph.rightMargin The glyph’s right margin.
BaseGlyph.height The glyph’s height.
BaseGlyph.bottomMargin The glyph’s bottom margin.
BaseGlyph.topMargin The glyph’s top margin.

Queries

BaseGlyph.bounds The bounds of the glyph: (xMin, yMin, xMax, yMax) or None.
BaseGlyph.pointInside Determine if point is in the black or white of the glyph.

Pens and Drawing

BaseGlyph.getPen Return a Pen object for modifying the glyph.
BaseGlyph.getPointPen Return a PointPen object for modifying the glyph.
BaseGlyph.draw Draw the glyph with the given Pen.
BaseGlyph.drawPoints Draw the glyph with the given PointPen.

Layers

BaseGlyph.layers Immutable list of the glyph’s layers.
BaseGlyph.getLayer Get the glyph layer with name in this glyph.
BaseGlyph.newLayer Make a new layer with name in this glyph.
BaseGlyph.removeLayer Remove the layer from the glyph (not the font).

Global

BaseGlyph.clear Clear the glyph.
BaseGlyph.appendGlyph Append copies of the contours, components, anchors and guidelines from other.

Contours

BaseGlyph.contours An immutable list of contours in the glyph.
BaseGlyph.__len__ The number of contours in the glyph.
BaseGlyph.__iter__ Iterate through the contours in the glyph.
BaseGlyph.__getitem__ Get the contour located at index from the glyph.
BaseGlyph.appendContour A copy of the given contour to the glyph.
BaseGlyph.removeContour Remove the contour from the glyph.
BaseGlyph.clearContours Clear all contours.
BaseGlyph.removeOverlap Perform a remove overlap operation on the contours.

Components

BaseGlyph.components An immutable list of components in the glyph.
BaseGlyph.appendComponent Append a new component to the glyph.
BaseGlyph.removeComponent Remove component from the glyph.
BaseGlyph.clearComponents Clear all components.
BaseGlyph.decompose Decompose all components.

Anchors

BaseGlyph.anchors An immutable list of anchors in the glyph.
BaseGlyph.appendAnchor Append a new anchor to the glyph.
BaseGlyph.removeAnchor Remove anchor from the glyph.
BaseGlyph.clearAnchors Clear all anchors.

Guidelines

BaseGlyph.guidelines An immutable list of font-level guidelines.
BaseGlyph.appendGuideline Append a new guideline to the glyph.
BaseGlyph.removeGuideline Remove guideline from the glyph.
BaseGlyph.clearGuidelines Clear all guidelines.

Image

BaseGlyph.image The image for the glyph.
BaseGlyph.addImage Set the image in the glyph.
BaseGlyph.clearImage Remove the image from the glyph.

Note

BaseGlyph.note A note for the glyph as a string or None.
BaseGlyph.markColor The mark color for the glyph.

Sub-Objects

BaseGlyph.lib The lib for the glyph.

Transformations

BaseGlyph.transformBy Transform the object.
BaseGlyph.moveBy Move the object.
BaseGlyph.scaleBy Scale the object.
BaseGlyph.rotateBy Rotate the object.
BaseGlyph.skewBy Skew the object.

Interpolation

BaseGlyph.isCompatible Evaluate interpolation compatibility with other.
BaseGlyph.interpolate Interpolate all possible data in the glyph.

Normalization

BaseGlyph.round Round coordinates.
BaseGlyph.autoUnicodes Use heuristics to set the Unicode values in the glyph.

Environment

BaseGlyph.naked Return the environment’s native object that has been wrapped by this object.
BaseGlyph.changed Tell the environment that something has changed in the object.

Reference

class fontParts.base.BaseGlyph(*args, **kwargs)

Glyph object.

Copy

BaseGlyph.copy()

Copy the glyph into a new glyph that does not belong to a glyph.

>>> copiedGlyph = glyph.copy()

This will copy:

  • name
  • unicodes
  • width
  • height
  • note
  • markColor
  • lib
  • contours
  • components
  • anchors
  • guidelines
  • image

Parents

BaseGlyph.layer

The glyph’s parent layer.

>>> layer = glyph.layer
BaseGlyph.font

The glyph’s parent font.

>>> font = glyph.font

Identification

BaseGlyph.name

The glyph’s name.

>>> glyph.name
"A"
>>> glyph.name = "A.alt"
BaseGlyph.unicodes

The glyph’s unicode values in order from most to least important.

>>> glyph.unicodes
[65]
>>> glyph.unicodes = [65, 0x42]
>>> glyph.unicodes = []

The values in the returned list will be integers. When setting you may send int or hex values.

BaseGlyph.unicode

The glyph’s primary unicode value.

>>> glyph.unicode
65
>>> glyph.unicode = None

The returned value will be an integer or None. When setting you may send int or hex values or None.

Metrics

BaseGlyph.width

The glyph’s width.

>>> glyph.width
500
>>> glyph.width = 200
BaseGlyph.leftMargin

The glyph’s left margin.

>>> glyph.leftMargin
35
>>> glyph.leftMargin = 45
BaseGlyph.rightMargin

The glyph’s right margin.

>>> glyph.rightMargin
35
>>> glyph.rightMargin = 45
BaseGlyph.height

The glyph’s height.

>>> glyph.height
500
>>> glyph.height = 200
BaseGlyph.bottomMargin

The glyph’s bottom margin.

>>> glyph.bottomMargin
35
>>> glyph.bottomMargin = 45
BaseGlyph.topMargin

The glyph’s top margin.

>>> glyph.topMargin
35
>>> glyph.topMargin = 45

Queries

BaseGlyph.bounds

The bounds of the glyph: (xMin, yMin, xMax, yMax) or None.

>>> glyph.bounds
(10, 30, 765, 643)
BaseGlyph.pointInside(point)

Determine if point is in the black or white of the glyph.

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

point must be an (x, y) tuple.

Pens and Drawing

BaseGlyph.getPen()

Return a Pen object for modifying the glyph.

>>> pen = glyph.getPen()
BaseGlyph.getPointPen()

Return a PointPen object for modifying the glyph.

>>> pointPen = glyph.getPointPen()
BaseGlyph.draw(pen, contours=True, components=True)

Draw the glyph with the given Pen.

>>> glyph.draw(pen)
>>> glyph.draw(pen, contours=True, components=False)
BaseGlyph.drawPoints(pen, contours=True, components=True)

Draw the glyph with the given PointPen.

>>> glyph.drawPoints(pointPen)
>>> glyph.drawPoints(pointPen, contours=True, components=False)

Layers

Layer interaction in glyphs is very similar to the layer interaction in fonts. When you ask a glyph for a layer, you get a glyph layer in return. A glyph layer lets you do anything that you can do to a glyph. In fact a glyph layer is really just a glyph.

>>> bgdGlyph = glyph.newLayer('background')
>>> bgdGlyph.appendGlyph(glyph)
>>> bgdGlyph.appendGuideline((10, 10), 45)
BaseGlyph.layers

Immutable list of the glyph’s layers.

>>> for glyphLayer in glyph.layers:
...     len(glyphLayer)
3
2
BaseGlyph.getLayer(name, **kwargs)

Get the glyph layer with name in this glyph.

>>> glyphLayer = glyph.getLayer("foreground")
BaseGlyph.newLayer(name, **kwargs)

Make a new layer with name in this glyph.

>>> glyphLayer = glyph.newLayer("background")

This is the equivalent of using the newGlyph method on a named layer. If the glyph already exists in the layer it will be cleared. Return the new glyph layer.

BaseGlyph.removeLayer(layer, **kwargs)

Remove the layer from the glyph (not the font).

>>> glyph.removeLayer("background")

Layer can be a glyph layer or a layer name.

Global

BaseGlyph.clear(contours=True, components=True, anchors=True, guidelines=True, image=True)

Clear the glyph.

>>> glyph.clear()

This clears:

  • contours
  • components
  • anchors
  • guidelines
  • image

It’s possible to selectively turn off the clearing of portions of the glyph with the arguments.

BaseGlyph.appendGlyph(other, offset=None)

Append copies of the contours, components, anchors and guidelines from other.

>>> glyph.appendGlyph(otherGlyph)
>>> glyph.appendGlyph(otherGlyph, (100, 0))

offset indicates the offset that should be applied to the appended data. The default is (0, 0).

Contours

BaseGlyph.contours

An immutable list of contours in the glyph.

>>> for contour in glyph.contours:
...     contour.bounds
(10, 15, 57, 36)
(875, 35, 926, 647)
BaseGlyph.__len__()

The number of contours in the glyph.

>>> len(glyph)
2
BaseGlyph.__iter__()

Iterate through the contours in the glyph.

>>> for contour in glyph:
...     contour.bounds
(10, 15, 57, 36)
(875, 35, 926, 647)
BaseGlyph.__getitem__(index)

Get the contour located at index from the glyph.

>>> contour = glyph[0]
BaseGlyph.appendContour(contour, offset=None)

A copy of the given contour to the glyph.

>>> contour = glyph.appendContour(contour)
>>> contour = glyph.appendContour(contour, (100, 0))

offset indicates the distance that the contour should be offset when added to the glyph. The default is (0, 0).

BaseGlyph.removeContour(contour)

Remove the contour from the glyph.

>>> glyph.removeContour(contour)
>>> glyph.removeContour(0)

Contour may be a contour object or a contour index.

BaseGlyph.clearContours()

Clear all contours.

>>> glyph.clearContours()
BaseGlyph.removeOverlap()

Perform a remove overlap operation on the contours.

>>> glyph.removeOverlap()

Components

BaseGlyph.components

An immutable list of components in the glyph.

>>> for component in glyph.components:
...     component.baseGlyph
"A"
"acute"
BaseGlyph.appendComponent(baseGlyph, offset=None, scale=None)

Append a new component to the glyph.

>>> component = glyph.appendComponent("A")
>>> component = glyph.appendComponent("acute", offset=(20, 200))

baseGlyph indicates the glyph that the component will reference. offset indictaes the offset that should be defined in the component. The default is (0, 0). scale indicates the scale that should be defined in the component. The default is (1.0, 1.0).

BaseGlyph.removeComponent(component)

Remove component from the glyph.

>>> glyph.removeComponent(component)
>>> glyph.removeComponent(1)

component can be a component object or an integer representing the component index.

BaseGlyph.clearComponents()

Clear all components.

>>> glyph.clearComponents()
BaseGlyph.decompose()

Decompose all components.

>>> glyph.decompose()

Anchors

BaseGlyph.anchors

An immutable list of anchors in the glyph.

>>> for anchor in glyph.anchors:
...     anchor.name
"top"
BaseGlyph.appendAnchor(name, position, color=None)

Append a new anchor to the glyph.

>>> anchor = glyph.appendAnchor("top", (50, 500))
>>> anchor = glyph.appendAnchor("top", (50, 500), (1, 0, 0, 0.5))

name indicates the name that should be assigned to the anchor. position is an (x, y) tuple defining the position for the anchor. color is None or a color tuple.

BaseGlyph.removeAnchor(anchor)

Remove anchor from the glyph.

>>> glyph.removeAnchor(anchor)
>>> glyph.removeAnchor(2)

anchor can be a anchor object or an integer representing the anchor index.

BaseGlyph.clearAnchors()

Clear all anchors.

>>> glyph.clearAnchors()

Guidelines

BaseGlyph.guidelines

An immutable list of font-level guidelines.

>>> for guideline in glyph.guidelines:
...     guideline.angle
0
45
90
BaseGlyph.appendGuideline(position, angle, name=None, color=None)

Append a new guideline to the glyph.

>>> guideline = glyph.appendGuideline((50, 0), 90)
>>> guideline = glyph.appendGuideline((0, 540), 0, name="overshoot", color=(0, 0, 0, 0.2))

position (x, y) indicates the position of the guideline. angle indicates the angle of the guideline. name indicates the name for the guideline. color indicates the color for the guideline.

BaseGlyph.removeGuideline(guideline)

Remove guideline from the glyph.

>>> glyph.removeGuideline(guideline)
>>> glyph.removeGuideline(2)

guideline can be a guideline object or an integer representing the guideline index.

BaseGlyph.clearGuidelines()

Clear all guidelines.

>>> glyph.clearGuidelines()

Image

BaseGlyph.image

The image for the glyph.

BaseGlyph.addImage(path=None, data=None, scale=None, position=None, color=None)

Set the image in the glyph.

>>> image = glyph.addImage(path="/path/to/my/image.png", color=(1, 0, 0, 0.5))

path is a path to an image file. data is the raw image data. scale (x, y) is the scale of the image (optional). position (x, y) is the position of the image (optional). color is the color of the image (optional).

The image data format is not defined. That will be environment specific and is handled in the Image object.

BaseGlyph.clearImage(**kwargs)

Remove the image from the glyph.

>>> glyph.clearImage()

Note

BaseGlyph.note

A note for the glyph as a string or None.

>>> glyph.note
None
>>> glyph.note = "P.B. said this looks 'awesome.'"
BaseGlyph.markColor

The mark color for the glyph.

>>> glyph.markColor
None
>>> glyph.markColor = (1, 0, 0, 0.5)

Sub-Objects

BaseGlyph.lib

The lib for the glyph.

>>> glyph.lib["org.robofab.hello"]
"world"

Transformations

BaseGlyph.transformBy(matrix, origin=None)

Transform the object.

>>> obj.transformBy((0.5, 0, 0, 2.0, 10, 0))
>>> obj.transformBy((0.5, 0, 0, 2.0, 10, 0), origin=(500, 500))

matrix must be a Transformation Matrix. origin defines the point at with the transformation should originate. It must be a Coordinate or None. The default is (0, 0).

BaseGlyph.moveBy(value)

Move the object.

>>> obj.transformBy((10, 0))

value must be an iterable containing two Integer/Float values defining the x and y values to move the object by.

BaseGlyph.scaleBy(value, origin=None)

Scale the object.

>>> obj.transformBy(2.0)
>>> obj.transformBy((0.5, 2.0), origin=(500, 500))

value must be an iterable containing two Integer/Float values defining the x and y values to scale the object by. origin defines the point at with the scale should originate. It must be a Coordinate or None. The default is (0, 0).

BaseGlyph.rotateBy(value, origin=None)

Rotate the object.

>>> obj.transformBy(45)
>>> obj.transformBy(45, origin=(500, 500))

value must be a Integer/Float values defining the angle to rotate the object by. origin defines the point at with the rotation should originate. It must be a Coordinate or None. The default is (0, 0).

BaseGlyph.skewBy(value, origin=None)

Skew the object.

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

value must be rone of the following:

  • single Integer/Float indicating the value to skew the x direction by.
  • iterable cointaining type Integer/Float defining the values to skew the x and y directions by.

origin defines the point at with the skew should originate. It must be a Coordinate or None. The default is (0, 0).

Interpolation

BaseGlyph.isCompatible(other)

Evaluate interpolation compatibility with other.

>>> compat, report = self.isCompatible(otherFont)
>>> compat
False
>>> report
[Fatal] The glyphs do not contain the same number of contours.

Returns a boolean indicating if the glyph is compatible for interpolation with other and a string of compatibility notes.

BaseGlyph.interpolate(factor, minGlyph, maxGlyph, round=True, suppressError=True)

Interpolate all possible data in the glyph.

>>> glyph.interpolate(0.5, otherGlyph1, otherGlyph2)
>>> glyph.interpolate((0.5, 2.0), otherGlyph1, otherGlyph2, round=False)

The interpolation occurs on a 0 to 1.0 range where minGlyph is located at 0 and maxGlyph is located at 1.0.

factor is the interpolation value. It may be less than 0 and greater than 1.0. It may be a number (integer, float) or a tuple of two numbers. If it is a tuple, the first number indicates the x factor and the second number indicates the y factor.

round indicates if the result should be rounded to integers.

suppressError indicates if incompatible data should be ignored or if an error should be raised when such incompatibilities are found.

Normalization

BaseGlyph.round()

Round coordinates.

>>> glyph.round()

This applies to the following:

  • width
  • height
  • contours
  • components
  • anchors
  • guidelines
BaseGlyph.autoUnicodes()

Use heuristics to set the Unicode values in the glyph.

>>> glyph.autoUnicodes()

Environments will define their own heuristics for automatically determining values.

Environment

BaseGlyph.naked()

Return the environment’s native object that has been wrapped by this object.

>>> loweLevelObj = obj.naked()
BaseGlyph.changed(*args, **kwargs)

Tell the environment that something has changed in the object. The behavior of this method will vary from environment to environment.

>>> obj.changed()