Skip to content

fontlab.GlyphComponent

GlyphComponent

A component reference inside a composite glyph.

What is GlyphComponent?

A composite glyph is a glyph whose shapes are built entirely from references to other glyphs, rather than containing its own outlines. For example, the "é" (e-acute) glyph in most fonts does not contain its own curves — instead, it contains two component references: one to the "e" base glyph and one to "acutecomb" (the combining acute accent).

Each of those references is a GlyphComponent. It identifies the base glyph (by name or ID), specifies where to place it (through a transform matrix), and optionally says whether to use anchor-to-anchor alignment instead of a fixed transform.

GlyphComponent is the data object describing the reference. On the canvas, each component appears as an flShape with isComponent == True, and shape.component gives you the GlyphComponent data.

Tutorial

from PythonQt.QtGui import QTransform
import fontlab as fl
from fontlab import GlyphComponent

font = fl.CurrentFont()

# ============================================================
# Find all composite glyphs in the font and list their
# component references. A composite is a glyph that has no
# direct contours but contains component shapes.
# ============================================================

print("--- Composite Glyphs ---")
composite_count = 0

for glyph in font.glyphs:
    layer = glyph.layers[0]
    components_in_glyph = []

    for shape in layer.shapes:
        if shape.isComponent:
            comp = shape.component
            if comp:
                # Resolve the component's base glyph name:
                try:
                    base_glyph = font.glyph(comp.name_id)
                    base_name  = base_glyph.name if base_glyph else f"id={comp.name_id}"
                except Exception:
                    base_name = f"id={comp.name_id}"

                components_in_glyph.append(
                    f"'{base_name}' "
                    f"(anchors={comp.use_anchors}  "
                    f"width_scale={comp.width_scale:.2f})"
                )

    if components_in_glyph:
        composite_count += 1
        print(f"  '{glyph.name}': {' + '.join(components_in_glyph)}")

print(f"\nTotal composite glyphs: {composite_count}")

# ============================================================
# Inspect the transform of a specific component.
# For composites that use transforms (not anchors), the
# translation tells you where the component was placed.
# ============================================================

if 'eacute' in [g.name for g in font.glyphs]:
    glyph = font['eacute']
    layer = glyph.layers[0]
    for shape in layer.shapes:
        if shape.isComponent:
            comp = shape.component
            tx   = comp.transform
            dx   = tx.dx()   # horizontal translation
            dy   = tx.dy()   # vertical translation
            print(f"\n'eacute' component transform: dx={dx:.0f}  dy={dy:.0f}")
            print(f"  use_anchors: {comp.use_anchors}")

# ============================================================
# Create a GlyphComponent manually.
# This shows what fields you would set to build a composite
# from scratch in a script.
# ============================================================

# Component referencing "e" by name:
comp_e = GlyphComponent("e")
comp_e.use_anchors = False
comp_e.transform   = QTransform()   # identity (no offset)

# Component referencing "acutecomb" placed 300 units above baseline:
comp_acute = GlyphComponent("acutecomb")
comp_acute.use_anchors = True   # let the anchor system position it
comp_acute.transform   = QTransform().translate(0, 300)

print("\nManual component created:")
print(f"  'e' component: use_anchors={comp_e.use_anchors}")
print(f"  'acutecomb' component: use_anchors={comp_acute.use_anchors}")

Common Scripting Patterns

Count composites and simple glyphs

import fontlab as fl

font = fl.CurrentFont()
composites = 0
simple     = 0

for glyph in font.glyphs:
    layer = glyph.layers[0]
    is_composite = any(s.isComponent for s in layer.shapes)
    if is_composite:
        composites += 1
    elif layer.shapes:
        simple += 1

print(f"Simple: {simple}  Composites: {composites}  "
      f"Total: {composites + simple}")

Find all composites that use a specific base glyph

import fontlab as fl

font    = fl.CurrentFont()
base    = font['a']   # find all composites referencing 'a'
base_id = base.id

for glyph in font.glyphs:
    layer = glyph.layers[0]
    for shape in layer.shapes:
        if shape.isComponent:
            comp = shape.component
            if comp and comp.name_id == base_id:
                print(f"'{glyph.name}' uses '{base.name}' as a component")

Switch all composites from transform-based to anchor-based positioning

import fontlab as fl

font = fl.CurrentFont()
for glyph in font.glyphs:
    layer = glyph.layers[0]
    for shape in layer.shapes:
        if shape.isComponent:
            comp = shape.component
            if comp and not comp.use_anchors:
                comp.use_anchors = True

fl.Update()
print("Switched all components to anchor-based positioning.")

Constructors: GlyphComponent() - default GlyphComponent(GlyphComponent) GlyphComponent(uint id) GlyphComponent(string name) GlyphComponent(uint id, QTransform tx, double w, bool ua)

Native bases: object

Captured native MRO: GlyphComponent, object

Native new allocation evidence (not an init overload): (args, *kwargs) Create and return a new object. See help(type) for accurate signature.

Supplemental documentation: A glyph component described by name_id (uint), transform (QTransform), use_anchors (bool) and width_scale (double). It is created as GlyphComponent(), as a copy GlyphComponent(GlyphComponent), from an id GlyphComponent(uint id), from a name GlyphComponent(string name) or as GlyphComponent(uint id, QTransform tx, double w, bool ua).

Source: FontLab Proteus API documentation

Source: generated/fontlab.py: GlyphComponent captured declaration

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
__init__ function (self, /, *args: Any, **kwargs: Any) -> None Initialize self. (shared captured text)
name_id attribute Any The glyph ID of the base glyph being referenced.
transform attribute Any The 2-D transform applied to the base glyph when it is drawn as part of this composite.
use_anchors attribute Any If True, the component is positioned by matching its attachment anchor to the base glyph's anchor — for example, the _top anchor on the accent snaps to the top anchor on the base letter.
width_scale attribute Any Scale factor applied to the component's advance width when compositing.