Skip to content

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

License

Notifications You must be signed in to change notification settings

pushfoo/Fontknife

Repository files navigation

Fontknife

License PRs Welcome Tests (main) Documentation (main) Tests (stable) Documentation (stable)

Q: How do I convert a font into a sprite sheet?
A: fontknife convert

Goal End Result Command
Rasterize a font's ASCII glyphs Tom Thumb's ASCII Characters fontknife convert supported.font spritesheet.png
Generate filler assets The cool sunglasses and flexed bicep emoji as a png fontknife convert -P 48 -G "💪😎" NotoEmoji-Regular.ttf arm_cool.png

For more detailed explanations, see Practical Examples.

Install Instructions

Users

If you only want to convert fonts, follow these steps:

  1. Have Python 3.8+ installed
  2. Make sure you've created and activated a virtual environemnt (or use pipx)
  3. `pip install
  4. Run fontknife convert --help

If the last command print help text, you're ready to go! Read it for a descirption of supported arguments.

Developers / Contributors

See CONTRIBUTING.md.

Current Features

  • Read multiple font formats (TTF, BDF, PCF, 1-bit sprite sheets)
  • Read arbitrary unicode glyphs from supported fonts, including multi-character emoji
  • 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
  • Assembly code generation (Partial support for Octo)

Please remember:

  1. This is alpha quality software, so there will be bugs and breaking changes
  2. The Future Features section lists known areas for improvements
  3. The project accepts pull requests

Why did you make this?

  1. It helps developers iterate faster.
  2. A refactor of an abandoned projected turned into something else.

Practical Examples

1. Export Specific Glyphs / Make Game Assets

Scenario: You need filler assets for a farming game, ASAP.
Solution:

  1. Download an appropriately licensed emoji font, such as Noto Emoji,

  2. Run the following command

    fontknife convert -P 48 -G "🌽🍇🍎🍏🫐🍓🍒🍐🍅🥕🥔🥒🍑🥑🧅🍈" NotoEmoji-Regular.ttf fruits_and_veggies.png
    
  3. Double check & color the results with your preferred image editor

    Fruit and vegetable emoji exported as a PNG sprite sheet

3. Generate Octo Assembly

Note: This feature is mostly broken legacy code inherited from an earlier project.

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

  1. All glyphs can 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

The data tables should be usable, but the attached drawing routines are not:

  1. Their underlying logic is broken
  2. They predate Octo's idiomatic :stringmode macro

Future features

Features in this section will be added if:

  1. A PR is made addressing it
  2. I have time / interest

The features below are in descending order, from highest to lowest priority:

Feature Questions / Blockers Easiest known approach
Better documentation Sphinx + RTD, in progress
Fix automatic sheet sizing
Export with transparent background color Outline detection Improve cli color parsing, replace #000 default with #0000
Add foreground color control Same as above
Export to folder of files per glyph File naming Use the unicode code point
Support for automatically reading TTF glyph tables Use fonttools to read the TTF table
Fix ugly caching behavior Delete / disable caching entirely
Better scaling flags, including downsampling Recommend imagemagick or image editors
Additional font formats (see Future Formats Time / interest
C header code generation Time / interest
Refactor or remove Octo generation Move it to a downstream project
Better interface for glyph subset selection Good design unclear Write doc on using fontknife in scripts
Color TTF support Pillow has bad support for color TTF Unknown, help welcome!

Future Formats

Potential font formats to support:

  • FNT and other old Windows formats
  • UXF

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages