Skip to main content

Rasterize only the glyphs you need.

Project description

Fontknife

Rasterize only the glyphs you need. Cut out everything else.

Warning: This is alpha-quality software. It is full of bugs and may change drastically!

Current features:

  • Read multiple font formats (TTF, BDF, PCF, 1-bit Sprite sheets)
  • Export 1-bit sprite sheets to multiple image formats (PNG, BMP, JPG)
  • Guess common formats from file extensions
  • Simpler than GUI font editors
  • Can be used as a library in custom Python scripts
  • Code generation (Partial support for Octo)

See the end of this file for planned features.

How do I install it?

Requirements

  1. Python 3.7+
  2. Enough disk space to install pillow and its dependencies
  3. The ability to create virtual environments or use pipx

Installation

Create a virtual environment and run the following:

pip install fontknife

You can use pipx instead if you'd like.

If you want to use fontknife as a dependency in your projects, be sure to pin a specific version. Breaking changes are almost guaranteed in the future.

What does it do?

1. Convert Fonts to Sprite Sheets

Tom thumb is a public domain pixel font with a common problem: it's shipped in a format most GUI and game frameworks do not support.

This tool fixes that instantly:

fontknife convert tom-thumb.bdf tom_thumb.png

A PNG spritesheet of tom thumb.bdf

Use your preferred image editor to upscale & adjust the resulting sprite sheet.

Note: Reading the glyph table for TTF fonts is not yet supported. Unless otherwise specified, they will be assumed to provide English-focused glyphs. See the next example for information on how to select specific glyphs.

2. Export Specific Glyphs / Make Game Assets

Imagine you're making a farming game. You need filler assets ASAP.

Use this tool with a Noto Emoji Font variant to rapidly generate filler assets:

fontknife convert -P 48 -G "🌽🍇🍎🍏🫐🍓🍒🍐🍅🥕🥔🥒🍑🥑🧅🍈" NotoEmoji-Regular.ttf fruits_and_veggies.png

Fruit and vegetable emoji exported as a PNG sprite sheet

If you need more color, the outlines created by the command above should be easy to improve upon with a paint bucket tool in your preferred image editor.

Explanation of Flags

Flag Long version Meaning
-P --src-font-size-points A point size to render a TTF at. Ignored for non-TTF fonts.
-G --src-glyph-sequence The glyphs to use and the order to render them in.

Note: Although multi-codepoint emoji ( ⛈️ , country and region flags, etc) are not yet supported, adding support is a high priority due to their usefulness for asset generation and LCD hardware projects.

3. Export Fonts to Octo Code

Warning: This feature is legacy code inherited from an earlier project! Do not rely on the drawing routines it outputs, only the tables!

You can generate a width table and sprite data table as valid Octo source your font meets the following requirements:

  1. All glyphs render as 8px x 8px or smaller
  2. There are 256 or fewer glyphs in the font

For example, run the following to generate tables of widths and sprite data for Tom thumb as Octo source:

fontknife emit-code tom-thumb.bdf tom-thumb.8o

You should ignore the prefixed drawing routines in the output. Although this command's implementation was refactored to increase readability, the underlying logic is still from before Octo's :stringmode macro was added. Updating this is on the todo list. See the end of this file for more information.

Why did you make this?

tl;dr It helps developers iterate faster

If you want a longer history:

  1. I made a PR for a project
  2. After checking back over a year later, I forked the project to start an incremental rewrite
  3. Bitmap fonts were repeatedly brought up as a potential future feature for two projects I contribute to, pyglet and arcade
  4. I started reading up on bitmap fonts to learn more
  5. It became painfully clear that finding and importing assets would become even more of a friction point if that feature was added
  6. Format conversion proved to be more useful and interesting to solve than Octo support

The project began shifting focus and gaining inertia at roughly item 4. I've been using the project as an opportunity to applying new techniques and Python features I've learned about when appropriate.

When will you add X?

When I have time or if I merge a PR.

Features I'd like to add, roughly in order of descending priority:

  • Export with a transparent background color
  • Upscaling flags to apply during rasterization and after sprite sheet creation
  • Image folder export format, 1 glyph per file
  • Support for automatically reading TTF glyph tables
  • Multi-codepoint emoji (examples: ⛈️ , 🌶️, country/region flags)
  • Fix ugly caching behavior
  • Regex-like expressions for glyph selection
  • C header code generation
  • Refactor Octo generation
  • Additional font formats, both old (FNT, etc) and new (UXF, etc).
  • Additional color support
  • Color TTF support

Download files

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

Source Distribution

fontknife-0.1.2.tar.gz (52.9 kB view hashes)

Uploaded Source

Built Distribution

fontknife-0.1.2-py3-none-any.whl (60.7 kB view hashes)

Uploaded Python 3

Supported by

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