fontmake

repository·main·Indexed 21 days ago

https://github.com/googlefonts/fontmake

A tool for compiling font source files, such as .glyphs, .ufo, and .designspace, into binary font formats including .otf and .ttf. It supports the creation of both static instances and variable fonts (TrueType and CFF2), providing options for interpolation, contour handling, and OpenType layout customization.

Tokens
5.8K
Snippets
16
Records
28
Agent score
65%

What's inside fontmake

  1. Customize feature generation with feature writers

    main

    Feature writers control how kerning and anchor rules from design sources are converted into explicit OpenType features.

    Configuration Methods:

    • UFO lib: Add entries to the com.github.googlei18n.ufo2ft.featureWriters key.
    • Command Line: Use the --feature-writer flag. Use --feature-writer "None" to disable all automatic feature generation.

    Common Options: All feature writers support a mode parameter:

    • skip (default): Skips writing the feature if it already exists in the source. Note: If the source contains the magic string # Automatic Code, fontmake will insert generated code at that location even in skip mode.
    • append: Appends the generated rules to the existing feature code.
    <!-- Example: Configuring KernFeatureWriter to append rules in a UFO lib.plist -->
    <key>com.github.googlei18n.ufo2ft.featureWriters</key>
    <array>
      <dict>
        <key>class</key>
        <string>KernFeatureWriter</string>
        <key>options</key>
        <dict>
          <key>mode</key>
          <string>append</string>
        </dict>
      </dict>
    </array>
  2. Understand the fontmake compilation process

    main

    To effectively use fontmake options, it is helpful to understand the sequence of operations performed during font compilation:

    1. Source Conversion: Converts Glyphs files to .designspace and .ufo files.
    2. Pre-processing: Runs filters such as converting cubics to quadratics (for TTF), decomposing mixed glyphs, or removing overlaps.
    3. Feature Generation: Generates explicit feature files (converting anchors and kerning into Adobe feature file syntax).
    4. Outline Creation: Generates the actual outlines.
    5. Table Building: Builds OpenType tables.
    6. Post-processing: Runs filters like renaming glyphs to production names.
  3. Debug feature writer operations

    main

    If you need to inspect the intermediate files generated by the feature writers, use the --debug-feature-file <file> flag. This causes fontmake to write the generated feature file to a known filename, allowing you to inspect its contents to ensure they are as expected.

    # Example: debugging feature file generation
    fontmake --debug-feature-file path/to/feature_file.fea input_file
  4. Apply outline filters via command line or UFO lib

    main

    fontmake uses a two-pass filtering pipeline: a "preprocessing" pass on UFO files (e.g., converting cubics to quadratics, removing overlaps) and a "postprocessing" pass on output binary files.

    You can add custom filters in two ways:

    1. Via UFO/Designspace files: Add an entry to the com.github.googlei18n.ufo2ft.filters key in the lib dictionary. For filters outside the ufo2ft library, you must provide a namespace.
    2. Via Command Line: Use the --filter flag with the syntax: --filter "python.package::ClassName(argument,argument)". To run a filter as a preprocessing step, add pre=True as a pseudo-argument.

    Any Python class inheriting from ufo2ft.filters.BaseFilter can be used.

    # Example: Use ufostroker to apply a stroke of width 50 as a preprocessing step
    fontmake --filter 'ufostroker::StrokeFilter(Width=50,pre=True)'
    
    # Example: Fix potential rasterizing/hinting errors by decomposing transformed components
    fontmake --filter DecomposeTransformedComponentsFilter
  5. Debug Glyphs file conversion

    main

    When using Glyphs files as a source, fontmake first uses glyphsLib to convert them into masters and a .designspace file, which are placed in the master_ufo directory.

    To troubleshoot:

    1. Inspect the master_ufo directory: Verify that the generated UFO files and the .designspace file are correct.
    2. Check axis ranges: In the .designspace file, pay close attention to the axis ranges and the positions of masters and instances.
    3. Skip conversion: If the files in master_ufo are correct, you can skip the Glyphs conversion step in subsequent runs by running fontmake directly on the .designspace file using the -m flag.
    # Example: running fontmake on the designspace file to skip Glyphs conversion
    fontmake -m path/to/your_font.designspace
  6. Validate UFO files and get debug tracebacks

    main

    Use these flags to increase the visibility of the compilation process:

    • Validate UFOs: If you are starting with UFO files, use the --validate-ufo flag to check that they are valid and correct before compilation proceeds.
    • Get full tracebacks: If fontmake returns an error, use the --verbose DEBUG flag to see the full Python traceback. This is highly recommended before filing any issues, as the traceback often identifies the specific library causing the failure.

    Note: Always pass the verbose flag before filing an issue.

    # Example: running with debug verbosity to see full tracebacks
    fontmake --verbose DEBUG input_file
    
    # Example: validating UFO files
    fontmake --validate-ufo input_folder
  7. Install fontmake for development

    main

    To develop on fontmake, clone the repository and install it locally. It is recommended to use a virtual environment to avoid dependency conflicts.

    To test changes without re-installing, use the -e (editable) flag.

    # Standard local installation
    git clone https://github.com/googlefonts/fontmake
    cd fontmake
    pip install .
    
    # Editable installation for development
    pip install -e .
  8. Install fontmake

    main

    fontmake requires Python 3.10 or later. You can install it via pip from PyPI.

    To update fontmake and its dependencies to the newest available release, use the -U or --upgrade flag.

    pip3 install fontmake
    
    # To upgrade
    pip3 install -U fontmake
  9. Basic usage of fontmake

    main

    Use the fontmake executable to compile fonts from sources like .glyphs, .ufo, or .designspace into binary formats like .otf or .ttf. You can create both static instances and variable fonts.

    To compile a variable font from a Glyphs source file, use the -o variable flag.

    fontmake MyFont.glyphs -o variable
  10. Identify the source of errors in fontmake

    main

    Because fontmake acts as an orchestrator for several specialized Python libraries, errors often originate in the underlying libraries rather than fontmake itself. Use this guide to identify which library is likely responsible for your error:

    Symptom/StepLikely Responsible Library
    Converting Glyphs files to .designspace and .ufoglyphsLib
    Converting cubics to quadraticscu2qu
    Decomposing mixed glyphsufo2ft.filters.decomposeComponents
    Removing overlapsbooleanOperations
    Compiling multiple files into a variable fontfontTools.varLib
    General UFO processing errorsufo2ft
  11. Reference: ufo2ft default and available filters

    main

    The following filters are part of the ufo2ft library. Some run automatically during the standard fontmake pipeline, while others must be added manually.

    Automatic Pipeline Filters:

    • ExplodeColorLayerGlyphs: Creates glyphs from color layers for COLR fonts.
    • DecomposeComponents: Used for OTF outlines or TTF glyphs with both components and outlines.
    • FlattenComponents: Flattens nested components when the -f flag is used.
    • RemoveOverlaps: Removes glyph overlaps.
    • CubicToQuadratic: Converts cubics to quadratics for TTF binaries.

    Manually Addable Filters:

    • DecomposeTransformedComponentsFilter: Decomposes components with non-identity transformation matrices (translation/scaling) to prevent rasterizing/hinting errors.
    • PropagateAnchors: Creates anchors for composite glyphs based on their components.
    • SortContours: Sorts contours by bounding box size (should be placed between DecomposeComponents and RemoveOverlaps).
    • Transformations: Scales, translates, or transforms outlines on export (requires kwargs and include list in the UFO lib).
  12. Other important command line options

    main

    Use these flags to control the compilation process:

    • -i (Interpolate instances): By default, fontmake generates per-master binaries. If your source (like a Glyphs file) defines specific instances/exports, use -i to interpolate and generate them. They are placed in instance_ttf/ or instance_otf/.
    • --output-dir <some_directory>: Redirects all output to the specified directory instead of the default per-format subdirectories.
    • --output-path <filename>: Valid only if the output is a single binary file; writes the output directly to the specified filename.
    • -f (Flatten components): Decomposes nested components into a single level. This is recommended for compatibility with rendering environments that struggle with nested components.