BDF Parser (Python library)

Introduction#

This is a BDF (Glyph Bitmap Distribution; Wikipedia; Spec) format bitmap font file parser library in Python. It has Font, Glyph and Bitmap classes providing more than 30 chainable API methods of parsing BDF fonts, getting their meta information, rendering text in any writing direction, adding special effects and manipulating bitmap images. It works seamlessly with PIL / Pillow and NumPy, and has detailed documentation / tutorials / API reference.

BDF Parser TypeScript (JavaScript) library (documentation; GitHub page; npm page; npm i bdfparser) is a port of BDF Parser Python library (documentation; GitHub page; PyPI page; pip install bdfparser). Both are written by Tom Chen and under the MIT License.

The BDF Parser TypeScript (JavaScript) library has a Live Demo & Editor you can try.

Installation#

pip install bdfparser
(Python 3.4 and later include pip by default. Read this to know how to check whether pip is installed. Read this if you need to install it)

Quick examples#

from bdfparser import Font
font = Font('tests/fonts/unifont-13.0.04.bdf')
print(f"This font's global size is "
f"{font.headers['fbbx']} x {font.headers['fbby']} (pixel), "
f"it contains {len(font)} glyphs.")
# This font's global size is 16 x 16 (pixel), it contains 57086 glyphs.

Print cropped and combined "a" and "c" with shadow effect:

ac = font.glyph("a").draw().crop(6, 8, 1, 2).concat(
font.glyph("c").draw().crop(6, 8, 1, 2)
).shadow()
print(ac)
# .####..####..
# #.&&&##.&&&#.
# .&...##&....&
# .######&.....
# #.&&&##&.....
# #&...##&.....
# #&..###&...#.
# .###.#&####.&
# ..&&&.&.&&&&.

Get an enlarged version (8 times both width and height) of this "ac":

ac_8x8 = ac * 8

And save it as an RGBA (background transparent) image "ac.png", using PIL (Pillow) library (pip install pillow if needed):

from PIL import Image
im_ac = Image.frombytes('RGBA',
(ac_8x8.width(), ac_8x8.height()),
ac_8x8.tobytes('RGBA'))
im_ac.save("ac.png", "PNG")
ac.png (note its background is transparent)
ac.png (note its background is transparent)

Print text "Hello!", from right to left, with glowing effect:

hello = font.draw('Hello!', direction='rl').glow()
print(hello)
# ..........................................................
# ..........................................................
# ..........................................................
# ....................&&......&&............................
# .....&.............&##&....&##&...........&....&..........
# ....&#&.............&#&.....&#&..........&#&..&#&.........
# ....&#&....&&&&.....&#&.....&#&....&&&&..&#&..&#&.........
# ....&#&...&####&....&#&.....&#&...&####&.&#&..&#&.........
# ....&#&..&#&&&&#&...&#&.....&#&..&#&&&&#&&#&&&&#&.........
# ....&#&..&#&..&#&...&#&.....&#&..&#&&&&#&&######&.........
# ....&#&..&#&..&#&...&#&.....&#&..&######&&#&&&&#&.........
# ....&#&..&#&..&#&...&#&.....&#&..&#&&&&&.&#&..&#&.........
# .....&...&#&..&#&...&#&.....&#&..&#&...&.&#&..&#&.........
# ....&#&..&#&&&&#&..&&#&&...&&#&&.&#&&&&#&&#&..&#&.........
# ....&#&...&####&..&#####&.&#####&.&####&.&#&..&#&.........
# .....&.....&&&&....&&&&&...&&&&&...&&&&...&....&..........
# ..........................................................
# ..........................................................

This time, try NumPy and Matplotlib! (pip install if you don't have them)

import numpy
import matplotlib.pyplot as plt
nparr = numpy.array(hello.todata(2))
plt.imshow(nparr, 'Blues')
plt.show()
Plot image "!olleH"
Plot image "!olleH"

Save all glyphs in Unifont as a black-and-white-two-color-only "font_preview.png" (with default width 512px):

font_preview = font.drawall()
im_ac = Image.frombytes('1',
(font_preview.width(), font_preview.height()),
font_preview.tobytes('1'))
im_ac.save("font_preview.png", "PNG")
Parts of the Unifont preview image (click the image to view the long original)
Parts of the Unifont preview image (click the image to view the long original)

Copyright & links#

Written by Tom Chen, under the MIT License, a permissive open-source license.

This library officially supports Python version 3.5 or later.

GitHub repo | PyPI page

The documentation belongs to font.tomchen.org, a website where I put font & typography related stuff. The documentation website's GitHub repo