Skip to main content

Font merge engine — combine two font files into one via cmap-based glyph replacement

Project description

Font Merge Engine

English | 日本語

The core font merge engine behind OFL Font Baker. The code in this directory is MIT-licensed and can be freely used in other projects.

Merges a base font (typically CJK) with a sub font (typically Latin or kana) into a single font file, replacing glyphs and OpenType features via cmap-based mapping.

For details on features and background, see the OFL Font Baker repository.

OFL Fonts Only

This library only accepts fonts licensed under the SIL Open Font License (OFL). If an input font's name table (nameID 13) does not contain an OFL license string, loading fails with an error.

Merged output is automatically tagged with OFL-compliant copyright and license metadata.

Installation

pip install ofl-font-baker

Requires Python 3.9+. fonttools and brotli are installed automatically.

Usage

Provide a JSON config on stdin. The engine supports two modes:

cat config.json | python3 merge_fonts.py

Path Mode

Specify output file paths explicitly via export.path. Only the files whose paths are provided are written.

{
  "baseFont": {
    "path": "/path/to/base.otf",
    "scale": 1.0,
    "baselineOffset": 0,
    "axes": []
  },
  "subFont": {
    "path": "/path/to/sub.ttf",
    "scale": 1.0,
    "baselineOffset": 0,
    "axes": []
  },
  "output": {
    "familyName": "My Font",
    "weight": 400,
    "italic": false,
    "width": 5
  },
  "export": {
    "path": {
      "font": "/out/MyFont-Regular.otf",
      "woff2": "/web/MyFont-Regular.woff2"
    }
  }
}

Package Mode

Specify export.package to create a complete output directory with font files and metadata.

{
  "baseFont": { "path": "/path/to/base.otf", "scale": 1.0, "baselineOffset": 0, "axes": [] },
  "subFont": { "path": "/path/to/sub.ttf", "scale": 1.0, "baselineOffset": 0, "axes": [] },
  "output": { "familyName": "My Font" },
  "export": {
    "package": {
      "dir": "/exports/MyFont-Regular"
    }
  }
}

Output (JSON manifest on stdout):

{
  "outputDir": "/exports/MyFont-Regular",
  "fontPath": "/exports/MyFont-Regular/MyFont-Regular.otf",
  "woff2Path": "/exports/MyFont-Regular/MyFont-Regular.woff2",
  "oflPath": "/exports/MyFont-Regular/OFL.txt",
  "settingsPath": "/exports/MyFont-Regular/Settings.txt",
  "configPath": null,
  "files": [...]
}

Progress is emitted as JSON lines on stderr.

output

Key Default Description
familyName "Font" Output font family name.
postScriptName "" PostScript name (nameID 6). When empty, derived from familyName by stripping chars outside printable ASCII 33–126 and []{}<>()/%, clamped to 63 bytes.
version "1.000" Version string (nameID 5). A Version prefix is prepended automatically if missing.
weight 400 Font weight (100–900).
italic false Whether the output is italic.
width 5 Font width class (1–9).
upm (from base) Target units-per-em. When different from the base font, all metrics and outlines are scaled.
manufacturer "" Manufacturer name (nameID 8).
manufacturerURL "" Manufacturer URL (nameID 11).
copyright "" Additional copyright string appended to source copyrights.
trademark "" Additional trademark string appended to source trademarks.
metricsSource "base" Which font's vertical metrics (OS/2, hhea) to use. "base" keeps the base font metrics and expands only when the sub font is larger. "sub" overwrites with the sub font's metrics.

export.path

All keys are optional. Only files whose paths are specified are written. woff2 requires font.

Key Description
font Font file (OTF/TTF) path.
woff2 WOFF2 file path.
ofl OFL.txt path.
settings Settings.txt path.
config ExportConfig.json path.

export.package

Creates a complete output directory. Always generates font, WOFF2, OFL.txt, and Settings.txt.

Key Default Description
dir Output directory path (required).
overwrite false Allow overwriting an existing directory.
bundleInputFonts false Copy input fonts into a source/ subdirectory and write ExportConfig.json with relative paths. Makes the package self-contained and reproducible.

Tests

python3 -m pytest python/tests/ -v

Test fonts live in python/tests/fonts/.

License

The source code in this directory is licensed under the MIT License. See LICENSE for details.

Test fonts under tests/fonts/ follow each font's own license (OFL, etc.) and are not covered by the MIT notice.

Other parts of the parent repository (OFL Font Baker) are licensed under AGPL-3.0-or-later and are not covered by this MIT notice.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ofl_font_baker-0.3.2.tar.gz (39.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ofl_font_baker-0.3.2-py3-none-any.whl (43.3 kB view details)

Uploaded Python 3

File details

Details for the file ofl_font_baker-0.3.2.tar.gz.

File metadata

  • Download URL: ofl_font_baker-0.3.2.tar.gz
  • Upload date:
  • Size: 39.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.7

File hashes

Hashes for ofl_font_baker-0.3.2.tar.gz
Algorithm Hash digest
SHA256 3ab67e4becc8296b666b4eec2183f570743ac56ccef38b049220eb64090e1f94
MD5 aea1a9794efbf2cb79eb196c07691424
BLAKE2b-256 b8ebc3b00175065a9088634c4350f50643c1e17b2fd74b6af192c4b2f8d10564

See more details on using hashes here.

File details

Details for the file ofl_font_baker-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: ofl_font_baker-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 43.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.7

File hashes

Hashes for ofl_font_baker-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 da2d1022b63a1a196157fe7cb6b9cd800b3dc2523c4d8a25fb9d4394c254fea4
MD5 b8a46f749065a3c7aae3f8c7527331a8
BLAKE2b-256 0e4b7919c679b6f43f72f5399b5142f37d5fdaa98bc044dacd045673457c9b91

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page