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¶
Copy data from the current layer into a new layer. |
|
Copy data from another layer instance. |
Parents¶
Get or set the layer's parent font object. |
Attributes¶
Get or set the name of the layer. |
|
Get or set the color of the layer. |
Sub-Objects¶
Get the layer's lib object. |
|
Get the layer's temporary lib object. |
Glyphs¶
Return the number of glyphs in the layer. |
|
Get the names of all glyphs in the layer. |
|
Iterate through the glyphs in the layer. |
|
Check if the layer contains the specified glyph. |
|
Get the specified glyph from the layer. |
|
Create a new glyph in the layer. |
|
Insert a specified glyph into the layer. |
|
Remove the specified glyph from the layer. |
Interpolation¶
Evaluate interpolation compatibility with another layer. |
|
Interpolate all possible data in the layer. |
Mapping¶
Get the layer's character mapping. |
|
Get a reversed map of the layer's component references. |
Selection¶
Get or set the selected glyphs in the layer. |
|
Get or set the selected glyph names in the layer. |
Normalization¶
Round all approriate layer data to integers. |
|
Use heuristics to set Unicode values in all font glyphs. |
Environment¶
Return the environment's native object wrapped by the current object. |
|
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
BaseLayerinstance with the same attributes.
Example:
>>> copiedLayer = layer.copy()
- BaseLayer.copyData(source: BaseLayer) None[source]¶
Copy data from another layer instance.
- Refer to
BaseLayer.copyfor 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)
- Refer to
Parents¶
Attributes¶
- BaseLayer.name: dynamicProperty¶
Get or set the name of the layer.
The value must be a
str.- Returns:
A
strdefining the name of the current layer orNoneif 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
tupleofintorfloatnumbers representing a Color, orNoneto indicate that the layer does not have an assigned color.- Returns:
A
tuplecontainingintorfloatvalues representing the color, orNoneif 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
BaseLibclass.
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
BaseLibclass, used for storing data that should not be persisted. It is similar toBaseLayer.lib, except that its contents will not be saved when calling theBaseLayer.savemethod.- Returns:
A temporary instance of the
BaseLibclass.
Example:
>>> layer.tempLib["org.robofab.hello"] "world"
Glyphs¶
- BaseLayer.keys() tuple[str, ...]¶
Get the names of all glyphs in the layer.
This method returns an unordered
tupleof glyph names representing all theBaseGlyphinstances in the active layer. If called from aBaseFontinstance, it returns the glyphs from the default layer. If called from aBaseLayerinstance, it returns the glyphs from the current layer.- Returns:
A
tupleof glyph names representing the glyphs in the current or defaultBaseLayerinstance.
Example:
>>> layer.keys() ["B", "C", "A"]
- BaseLayer.__iter__() Iterator[BaseGlyph]¶
Iterate through the glyphs in the layer.
- Returns:
An iterator over
BaseGlyphinstances.
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
BaseFontinstance, it checks the default layer. When called from aBaseLayerinstance, it checks the current layer.- Parameters:
name – The name of the glyph to check for.
- Returns:
Note
has_keyis 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
BaseGlyphinstance 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
Falseand rename isFalse, the existing glyph is returned unchanged.If clear is
Falseand rename isTrue, 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
.1or.2to the original glyph name.When called from a
BaseFontinstance, the glyph is created in the default layer. When called from aBaseLayerinstance, the glyph is created in the current layer.- Parameters:
- Returns:
The newly created or existing
BaseGlyphinstance.
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
BaseGlyphinstance containing the data from glyph. The data inserted from glyph is the same data as documented inBaseGlyph.copy.- Parameters:
- Returns:
The newly inserted
BaseGlyphinstance.
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
BaseFontinstance, it removes the glyph from the default layer. When called from aBaseLayerinstance, 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
BaseLayerinstance to check compatibility with.- Returns:
A
tuplewhere the first element is aboolindicating compatibility, and the second element is afontParts.base.compatibility.LayerCompatibilityReporterinstance.
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
intorfloator atupleof twointorfloatvalues representing the factors(x, y).minLayer – The
BaseLayerinstance corresponding to the 0.0 position in the interpolation.maxLayer – The
BaseLayerinstance corresponding to the 1.0 position in the interpolation.round – A
boolindicating whether the result should be rounded to integers. Defaults toTrue.suppressError – A
boolindicating whether to ignore incompatible data or raise an error when such incompatibilities are found. Defaults toTrue.
- 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
dictmapping 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
dictmapping 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
dictmapping the name of each component base glyph in the font to atuplecontaining the composite glyph names that include the comoponent. All glyphs are loaded.- Returns:
A
dictof 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
listortupleofBaseGlyphinstances.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
listortupleof names representingBaseGlyphinstances.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.roundmethod on all glyphs in the layer.Example:
>>> layer.round()
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()