Skip to main content

A Python package for font manipulation and conversion

Project description

Font Generator

A Python package for font manipulation and conversion. Convert TTF/OTF fonts to SVG format, convert SVG files back to fonts, and add handwritten effects to fonts.

Features

  • TTF to SVG Conversion: Extract individual glyphs from TrueType/OpenType fonts as SVG files
  • SVG to TTF Conversion: Combine SVG files back into a TTF font
  • Handwritten Effect: Add a handwritten, jittery effect to fonts using FontForge
  • CLI Interface: Easy-to-use command-line interface for all operations
  • Python API: Use as a library in your Python projects

Installation

From PyPI

pip install font-generator

From Source

git clone https://github.com/GenXLabs-org/font-generator.git
cd font-generator
pip install -e .

Optional: Handwritten Feature

The handwritten font generation feature requires FontForge to be installed on your system:

macOS:

brew install fontforge

Ubuntu/Debian:

sudo apt-get install fontforge

Windows: Download from FontForge website

Then install the Python bindings:

pip install fontforge

Quick Start

Command Line Usage

Convert TTF to SVG

font-generator ttf-to-svg input.ttf output_dir/

This will extract all glyphs from the font and save them as individual SVG files in the output directory, along with a _font_metadata.json file containing font metrics.

Convert SVG to TTF

font-generator svg-to-ttf svg_dir/ base_font.ttf output.ttf --width 800

This will combine SVG files from the input directory into a new TTF font, using the base font as a template.

Add Handwritten Effect

font-generator handwritten input.ttf output.ttf --jitter 35 --smoothing 15

Options:

  • --jitter: Amount of random jitter to apply (default: 35)
  • --smoothing: Smoothing amount for curves (default: 15)

Python API Usage

from font_generator import ttf_to_svg, svg_to_ttf, make_handwritten

# Convert TTF to SVG
ttf_to_svg('input.ttf', 'output_dir/')

# Convert SVG to TTF
svg_to_ttf('svg_dir/', 'base_font.ttf', 'output.ttf')

# Add handwritten effect
make_handwritten('input.ttf', 'output.ttf', jitter_amount=35, smoothing=15)

Detailed Usage

TTF to SVG Conversion

The ttf-to-svg command extracts all glyphs from a font file:

font-generator ttf-to-svg path/to/font.ttf path/to/output/

Output:

  • Individual SVG files for each glyph (named with Unicode values)
  • _font_metadata.json file containing:
    • Font metadata (name, family, version)
    • Font metrics (units per em, ascent, descent)
    • Glyph metrics (width, left bearing)
    • OpenType feature information (GSUB/GPOS)

SVG to TTF Conversion

The svg-to-ttf command combines SVG files into a font:

font-generator svg-to-ttf svg_folder/ base_font.ttf output.ttf --width 800

Parameters:

  • svg_folder/: Directory containing SVG files
  • base_font.ttf: Template font file
  • output.ttf: Output font file path
  • --width: Default glyph width (default: 800)

Note: SVG files should be named to match glyph names (e.g., A.svg, uni0041_A.svg). The command uses a default Unicode map for A-Z, but you can customize this in the Python API.

Handwritten Effect

The handwritten command adds a jittery, handwritten effect:

font-generator handwritten input.ttf output.ttf --jitter 50 --smoothing 20

How it works:

  1. Adds random jitter to each point in the glyph outlines
  2. Converts curves to polygons (on-curve points)
  3. Smooths the result back into curves

Parameters:

  • --jitter: Range of random movement for points (higher = more jittery)
  • --smoothing: Amount of smoothing applied (higher = smoother curves)

Requirements

  • Python >=3.7
  • fonttools >= 4.0.0
  • svgpathtools >= 1.4.0
  • fontforge (optional, for handwritten feature)

Project Structure

font-generator/
├── font_generator/
│   ├── __init__.py
│   ├── cli.py              # CLI interface
│   ├── handwritten.py      # Handwritten effect module
│   ├── ttf_to_svg.py       # TTF to SVG conversion
│   └── svg_to_ttf.py       # SVG to TTF conversion
├── setup.py
├── pyproject.toml
├── requirements.txt
└── README.md

Examples

Example 1: Extract Glyphs from a Font

font-generator ttf-to-svg MyFont.ttf extracted_glyphs/

This creates individual SVG files for each character in the font.

Example 2: Create Custom Font from SVGs

# First, extract glyphs
font-generator ttf-to-svg BaseFont.ttf svgs/

# Edit SVG files as needed...

# Then rebuild the font
font-generator svg-to-ttf svgs/ BaseFont.ttf CustomFont.ttf

Example 3: Make a Font Look Handwritten

font-generator handwritten CleanFont.ttf HandwrittenFont.ttf --jitter 40

Troubleshooting

FontForge Import Error

If you get an error about FontForge not being found:

  1. Install FontForge on your system (see installation instructions above)
  2. Install Python bindings: pip install fontforge

Invalid Font File Error

Make sure you're using a valid TTF or OTF file. The tool validates font files by checking their headers.

SVG Conversion Issues

  • Ensure SVG files are properly formatted
  • Check that glyph names match the Unicode map
  • Verify the base font file is valid

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE file for details.

Author

Chandra Bahadur Khadka

Support

For issues, questions, or contributions, please visit the GitHub repository.

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

font_generator-0.1.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

font_generator-0.1.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file font_generator-0.1.0.tar.gz.

File metadata

  • Download URL: font_generator-0.1.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for font_generator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1066b72eb0af1472db5c444b889b4f34fbbef7c09edcd16b236e17a09e2f247a
MD5 e841131c9e10c146e5e3db9bd0d69c28
BLAKE2b-256 0ce905dc8a662f02e1589f25d3a64743aad7d5fad2874d7395e630dba80d49a2

See more details on using hashes here.

File details

Details for the file font_generator-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: font_generator-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for font_generator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95d2493436b91420e7b25901572214939e098c146e2dbc34e6424a796afbe771
MD5 9cd8b3072a70df75150de15640fe6ace
BLAKE2b-256 384960bba89f223ba0abdb644aef55623de77411500960e818dbf897185a7025

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