fontParts

repository·main·Indexed 41 days ago

https://github.com/robotools/fontparts

A Python library providing an API for interacting with font parts during the font development process. It features a high-level Public API for designers and a detailed Implementation API for developers.

Tokens
34K
Snippets
92
Records
217
Agent score
52%

What's inside fontParts

  1. Access font features via fontParts.base.Features

    main

    In fontParts, features are represented as text following the Adobe Font Development Kit for OpenType .fea syntax. You can access the features of a font instance through the .features attribute of a Font object.

    Important Considerations:

    • Syntax Validation: fontParts does not validate the syntax of the features text. You must ensure the .fea syntax is correct according to Adobe's specifications.
    • Self-containment: Features must be self-contained; any glyph or mark classes used must be defined within the features file itself.
    • Data Synchronization: The features text may contain data that duplicates or conflicts with data in BaseKerning, BaseGroups, or BaseInfo. Users and application developers are responsible for synchronizing this data.
    font = CurrentFont()
    print(font.features)
  2. Access global font context with fontParts.world

    main

    The fontParts.world module provides a way to access the currently active font, glyph, layer, or other structural elements within a font editing environment. This is useful for writing scripts that operate on whatever the user is currently inspecting or editing without needing to explicitly pass object references through every function call.

    Available functions for accessing the current context include:

    • CurrentFont(): Returns the currently active font.
    • CurrentLayer(): Returns the currently active layer.
    • CurrentGlyph(): Returns the currently active glyph.
    • CurrentContours(): Returns the currently active contours.
    • CurrentSegments(): Returns the currently active segments.
    • CurrentPoints(): Returns the currently active points.
    • CurrentComponents(): Returns the currently active components.
    • CurrentAnchors(): Returns the currently active anchors.
    • CurrentGuidelines(): Returns the currently active guidelines.
  3. Understand the Segment object in fontParts

    main
    In fontParts.base, a Contour object is composed of a list of Segment objects. A Segment represents a path between points and is essentially a list of points equipped with specific attributes and transformation methods. Segments are part of a hierarchy: a Segment belongs to a Contour, which belongs to a Glyph, which belongs to a Layer, which belongs to a Font.
  4. Understand the 'default' layer concept

    main

    In fontParts, when you perform an action on a font or glyph without explicitly specifying a layer, the action is performed on the "default" (or primary) layer.

    For example, if you create a new glyph on a font, the new glyph is automatically added to the font's default layer. If you access a glyph directly, you are accessing its default layer.

    font = CurrentFont()
    # glyph1 references the 'A' glyph's default layer
    glyph1 = font["A"]
    
    # glyph2 is created in the font's default layer
    glyph2 = font.newGlyph("B")
  5. Understand the Component concept in fontParts

    main

    A Component is a reference to another glyph within the same font. This allows glyphs to depend on one another; any changes made to the base glyph will automatically reflect in the component.

    Key behaviors:

    • Parentage: A component's parent is typically a glyph.
    • Decomposition: Components can be 'decomposed', which replaces the component reference with the actual outlines of the base glyph. Once decomposed, the link is broken, and changes to the base glyph will no longer affect the decomposed glyph.
  6. What are Anchors in fontParts

    main

    Anchors are single points within a glyph that are not part of a contour. They serve as reference positions for tasks such as assembling components. In most font editors, anchors have a distinct visual appearance and are editable. You can access them via the anchors attribute of a glyph object.

    glyph = CurrentGlyph()
    for anchor in glyph.anchors:
        print(anchor)
  7. Understand the BaseGlyph object

    main

    The BaseGlyph object in fontParts.base represents a single glyph, including its parts, contours, and associated metadata.

    Key characteristics:

    • Container Behavior: A BaseGlyph can be treated as a list of Contour objects.
    • Parentage: When a glyph is retrieved from a Font object, that font becomes its parent. It also belongs to a specific layer.
    • Composition: Glyphs can contain contours, components, anchors, guidelines, and images.
  8. Manipulate Glyph layers

    main

    Layer interaction in glyphs is similar to layer interaction in fonts. When you request a layer from a glyph, you receive a glyph layer. A glyph layer is itself a BaseGlyph object, allowing you to perform any glyph operation on it.

    Methods

    • newLayer(name): Creates a new glyph layer.
    • getLayer(name): Retrieves an existing layer.
    • removeLayer(name): Removes a layer.
    • layers: Access the collection of layers.
    # Create a new background layer and manipulate it
    bgdGlyph = glyph.newLayer('background')
    bgdGlyph.appendGlyph(glyph)
    bgdGlyph.appendGuideline((10, 10), 45)
  9. Structure of Documentation Strings (Docstrings)

    main

    Documentation is primarily contained within source code using docstrings. The project follows a specific hierarchical structure for classes, methods, and properties:

    Classes

    Provide a brief explanation of the object and notes on when to instantiate it. This text is prepended to the __init__ string in compiled docs.

    Public Methods

    1. A brief description highlighting significant args.
    2. An interactive example using >>>.
    3. Detailed paragraph explaining arguments, options (using tables), default values, and possible errors.
    4. Lists for step-by-step behavior.

    Private/Internal Methods

    Describe the implementation relationship to the public method. Note if arguments are normalized (e.g., via normalizers.normalizeValue) and specify required return types and error handling (e.g., raising FontPartsError).

    Dynamic Properties

    Use a brief description and an interactive example.

    class BaseThing(BaseOtherThing):
        """
        This is a very brief explanation of the object.
        """
    
        def aMethod(arg, kwarg="blah"):
            """
            A very brief description calling out majorly significant ``args``.
    
                >>> blah.public()
                """output"""
    
            Detailed paragraph about ``arg`` and ``kwarg``...
            """
    
        aProperty = dynamicProperty(
            "base_aProperty",
            """
            A very brief description.
    
                >>> print(font.aProperty)
                """output"""
            """
        )