Layer

Note

This section needs to contain the following:

  • description of what this is

  • sub-object with basic usage

  • glyph interaction with basic usage

Overview

Copy

BaseLayer.copy

Copy data from the current layer into a new layer.

BaseLayer.copyData

Copy data from another layer instance.

Parents

BaseLayer.font

Get or set the layer's parent font object.

Attributes

BaseLayer.name

Get or set the name of the layer.

BaseLayer.color

Get or set the color of the layer.

Sub-Objects

BaseLayer.lib

Get the layer's lib object.

BaseLayer.tempLib

Get the layer's temporary lib object.

Glyphs

BaseLayer.__len__

Return the number of glyphs in the layer.

BaseLayer.keys

Get the names of all glyphs in the layer.

BaseLayer.__iter__

Iterate through the glyphs in the layer.

BaseLayer.__contains__

Check if the layer contains the specified glyph.

BaseLayer.__getitem__

Get the specified glyph from the layer.

BaseLayer.newGlyph

Create a new glyph in the layer.

BaseLayer.insertGlyph

Insert a specified glyph into the layer.

BaseLayer.removeGlyph

Remove the specified glyph from the layer.

Interpolation

BaseLayer.isCompatible

Evaluate interpolation compatibility with another layer.

BaseLayer.interpolate

Interpolate all possible data in the layer.

Mapping

BaseLayer.getCharacterMapping

Get the layer's character mapping.

BaseLayer.getReverseComponentMapping

Get a reversed map of the layer's component references.

Selection

BaseLayer.selectedGlyphs

Get or set the selected glyphs in the layer.

BaseLayer.selectedGlyphNames

Get or set the selected glyph names in the layer.

Normalization

BaseLayer.round

Round all approriate layer data to integers.

BaseLayer.autoUnicodes

Use heuristics to set Unicode values in all font glyphs.

Environment

BaseLayer.naked

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

BaseLayer.changed

Tell the environment that something has changed in the object.

Reference

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

Represent the basis for a layer object.

This object will almost always be created by retrieving it from a font object. It can exist at either the font or glyph level. See layers.

Copy

BaseLayer.copy() BaseLayer[source]

Copy data from the current layer into a new layer.

This will copy:

Returns:

A new BaseLayer instance with the same attributes.

Example:

>>> copiedLayer = layer.copy()
BaseLayer.copyData(source: BaseLayer) None[source]

Copy data from another layer instance.

Refer to BaseLayer.copy for a list of values that will

be copied.

Parameters:

source – The source :class`BaseLayer` instance from which to copy data.

Example:

>>> sourceFont = MyFont('path/to/source.ufo')
>>> font.copyData(sourceFont)

Parents

BaseLayer.font: dynamicProperty

Get or set the layer’s parent font object.

The value must be a BaseFont instance or None.

Returns:

The BaseFont instance containing the layer or None.

Raises:

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

Example:

>>> font = layer.font

Attributes

BaseLayer.name: dynamicProperty

Get or set the name of the layer.

The value must be a str.

Returns:

A str defining the name of the current layer or None if the layer is the default layer.

Raises:

ValueError – If attempting to set the name to one that already exists in the font.

Example:

>>> layer.name
"foreground"
>>> layer.name = "top"
BaseLayer.color: dynamicProperty

Get or set the color of the layer.

The value must be a tuple of int or float numbers representing a Color, or None to indicate that the layer does not have an assigned color.

Returns:

A tuple containing int or float values representing the color, or None if no color is assigned.

Example:

>>> layer.color
None
>>> layer.color = (1, 0, 0, 0.5)

Sub-Objects

BaseLayer.lib: dynamicProperty

Get the layer’s lib object.

This property is read-only.

Returns:

An instance of the BaseLib class.

Example:

>>> layer.lib["org.robofab.hello"]
"world"
BaseLayer.tempLib: dynamicProperty

Get the layer’s temporary lib object.

This property is read-only.

This property provides access to a temporary instance of the BaseLib class, used for storing data that should not be persisted. It is similar to BaseLayer.lib, except that its contents will not be saved when calling the BaseLayer.save method.

Returns:

A temporary instance of the BaseLib class.

Example:

>>> layer.tempLib["org.robofab.hello"]
"world"

Glyphs

BaseLayer.__len__() int

Return the number of glyphs in the layer.

Returns:

The number of BaseGlyph instances in the layer as an int.

Example:

>>> len(layer)
256
BaseLayer.keys() tuple[str, ...]

Get the names of all glyphs in the layer.

This method returns an unordered tuple of glyph names representing all the BaseGlyph instances in the active layer. If called from a BaseFont instance, it returns the glyphs from the default layer. If called from a BaseLayer instance, it returns the glyphs from the current layer.

Returns:

A tuple of glyph names representing the glyphs in the current or default BaseLayer instance.

Example:

>>> layer.keys()
["B", "C", "A"]
BaseLayer.__iter__() Iterator[BaseGlyph]

Iterate through the glyphs in the layer.

Returns:

An iterator over BaseGlyph instances.

Example:

>>> for glyph in layer:
...     glyph.name
"A"
"B"
"C"
BaseLayer.__contains__(name: str) bool

Check if the layer contains the specified glyph.

This method checks whether a glyph with the given name exists in the layer. When called from a BaseFont instance, it checks the default layer. When called from a BaseLayer instance, it checks the current layer.

Parameters:

name – The name of the glyph to check for.

Returns:

True if the glyph exists in the layer, False otherwise.

Note

has_key is provided as an alias for this method for backward compatibility but may be deprecated in the future. It is advisable to use __contains__ instead.

Example:

>>> "A" in layer
True
BaseLayer.__getitem__(name: str) BaseGlyph

Get the specified glyph from the layer.

Parameters:

name – The name representing the glyph to retrieve.

Returns:

a BaseGlyph instance with the specified name.

Raises:

KeyError – If no glyph with the given name exists in the layer.

Example:

>>> glyph = layer["A"]
BaseLayer.newGlyph(name: str, clear: bool = True, rename: bool = False) BaseGlyph

Create a new glyph in the layer.

This method creates a new glyph with the given name in the layer.

If no glyph with the specified name exists, a new glyph is created and returned.

If a glyph with the specified name already exists:

  • If clear is True, the existing glyph is removed before creating and returning a new glyph with the same name.

  • If clear is False and rename is False, the existing glyph is returned unchanged.

  • If clear is False and rename is True, the existing glyph is renamed to a unique replacement name and a new glyph with the original name is created and returned.

Replacement names are generated by appending a numeric suffix such as .1 or .2 to the original glyph name.

When called from a BaseFont instance, the glyph is created in the default layer. When called from a BaseLayer instance, the glyph is created in the current layer.

Parameters:
  • name – The name of the glyph to create.

  • clear – Whether to remove an existing glyph with the same name before creating a new glyph. Defaults to True.

  • rename – Whether to rename an existing glyph to a unique replacement name when clear is False. Defaults to False.

Returns:

The newly created or existing BaseGlyph instance.

Example:

>>> glyph = layer.newGlyph("A")
BaseLayer.insertGlyph(glyph: BaseGlyph, name: str | None = None) BaseGlyph

Insert a specified glyph into the layer.

This method will not insert a glyph directly, but rather create a new BaseGlyph instance containing the data from glyph. The data inserted from glyph is the same data as documented in BaseGlyph.copy.

Parameters:
  • glyph – The BaseGlyph instance to insert.

  • name – The name to assign to the new layer after insertion. If value is None, the origninal name will be used. Defaults to None.

Returns:

The newly inserted BaseGlyph instance.

Example:

>>> glyph = font.insertGlyph(otherGlyph, name="glyph2")
BaseLayer.removeGlyph(name: str) None

Remove the specified glyph from the layer.

This method removes the glyph with the given name from the layer. When called from a BaseFont instance, it removes the glyph from the default layer. When called from a BaseLayer instance, it removes the glyph from the current layer.

Parameters:

name – The name of the glyph to remove.

Example:

>>> layer.removeGlyph("A")

Interpolation

BaseLayer.isCompatible(other: BaseLayer, cls=None) tuple[bool, LayerCompatibilityReporter][source]

Evaluate interpolation compatibility with another layer.

Parameters:

other – The other BaseLayer instance to check compatibility with.

Returns:

A tuple where the first element is a bool indicating compatibility, and the second element is a fontParts.base.compatibility.LayerCompatibilityReporter instance.

Example:

>>> compatible, report = self.isCompatible(otherLayer)
>>> compatible
False
>>> report
A
-
[Fatal] The glyphs do not contain the same number of contours.
BaseLayer.interpolate(factor: int | float | list[int | float] | tuple[float, float], minLayer: BaseLayer, maxLayer: BaseLayer, round: bool = True, suppressError: bool = True) None[source]

Interpolate all possible data in the layer.

The interpolation occurs on a 0 to 1.0 range between minLayer and maxLayer, using the specified factor.

Parameters:
  • factor – The interpolation value as a single int or float or a tuple of two int or float values representing the factors (x, y).

  • minLayer – The BaseLayer instance corresponding to the 0.0 position in the interpolation.

  • maxLayer – The BaseLayer instance corresponding to the 1.0 position in the interpolation.

  • round – A bool indicating whether the result should be rounded to integers. Defaults to True.

  • suppressError – A bool indicating whether to ignore incompatible data or raise an error when such incompatibilities are found. Defaults to True.

Raises:

TypeError – If minLayer or maxLayer are not instances of BaseLayer.

Example:

>>> layer.interpolate(0.5, otherLayer1, otherLayer2)
>>> layer.interpolate((0.5, 2.0), otherLayer1, otherLayer2, round=False)

Mapping

BaseLayer.getCharacterMapping() dict[int, tuple[str, ...]][source]

Get the layer’s character mapping.

This method creates a dict mapping Unicode values to tuples of glyph names. Each Unicode value corresponds to one or more glyphs, and the glyph names represent these glyphs in the mapping.

Note

One glyph can have multiple unicode values, and a unicode value can have multiple glyphs pointing to it.

Returns:

A dict mapping Unicode values to tuples of glyph names.

BaseLayer.getReverseComponentMapping() dict[str, tuple[str, ...]][source]

Get a reversed map of the layer’s component references.

This method creates a dict mapping the name of each component base glyph in the font to a tuple containing the composite glyph names that include the comoponent. All glyphs are loaded.

Returns:

A dict of component glyph names mapped to tuples of composite glyph names.

Example:

>>> mapping = layer.getReverseComponentMapping()
>>> mapping
{'A': ('Aacute', 'Aring'), 'acute': ('Aacute',),
'ring': ('Aring',), ...}

Selection

BaseLayer.selectedGlyphs()

Get or set the selected glyphs in the layer.

The value must be a list or tuple of BaseGlyph instances.

Returns:

An unordered tuple of currently selected BaseGlyph instances.

Getting selected glyph objects:

>>> for glyph in layer.selectedGlyphs:
...     glyph.markColor = (1, 0, 0, 0.5)

Setting selected glyph objects:

>>> layer.selectedGlyphs = someGlyphs
BaseLayer.selectedGlyphNames()

Get or set the selected glyph names in the layer.

The value must be a list or tuple of names representing BaseGlyph instances.

Returns:

An unordered tuple of glyph names representing the currently selected BaseGlyph instances.

Getting selected glyph names:

>>> for name in layer.selectedGlyphNames:
...     print(name)

Setting selected glyph names:

>>> layer.selectedGlyphNames = ["A", "B", "C"]

Normalization

BaseLayer.round() None[source]

Round all approriate layer data to integers.

This is the equivalent of calling the BaseGlyph.round method on all glyphs in the layer.

Example:

>>> layer.round()
BaseLayer.autoUnicodes() None[source]

Use heuristics to set Unicode values in all font glyphs.

Environments will define their own heuristics for automatically determining values.

Example:

>>> layer.autoUnicodes()

Environment

BaseLayer.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()
BaseLayer.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()