Skip to main content

A library for manipulating Glyph Bitmap Distribution Format (BDF) Fonts.

Project description

BdfFont

Python PyPI

BdfFont is a library for manipulating Glyph Bitmap Distribution Format (BDF) Fonts.

Installation

pip install bdffont

Usage

Load

import os
import shutil

from bdffont import BdfFont
from examples import assets_dir, build_dir


def main():
    outputs_dir = os.path.join(build_dir, 'load')
    if os.path.exists(outputs_dir):
        shutil.rmtree(outputs_dir)
    os.makedirs(outputs_dir)

    font = BdfFont.load(os.path.join(assets_dir, 'unifont', 'unifont-15.1.05.bdf'))
    print(f'name: {font.name}')
    print(f'size: {font.point_size}')
    print(f'ascent: {font.properties.font_ascent}')
    print(f'descent: {font.properties.font_descent}')
    print()
    for glyph in font.glyphs:
        print(f'char: {chr(glyph.code_point)} ({glyph.code_point:04X})')
        print(f'glyph_name: {glyph.name}')
        print(f'advance_width: {glyph.device_width_x}')
        print(f'offset: {glyph.bounding_box_offset}')
        for bitmap_row in glyph.bitmap:
            text = ''.join(map(str, bitmap_row)).replace('0', '  ').replace('1', '██')
            print(f'{text}*')
        print()
    font.save(os.path.join(outputs_dir, 'unifont-15.0.01.bdf'))


if __name__ == '__main__':
    main()

Create

import os
import shutil

from bdffont import BdfFont, BdfGlyph
from examples import build_dir


def main():
    outputs_dir = os.path.join(build_dir, 'create')
    if os.path.exists(outputs_dir):
        shutil.rmtree(outputs_dir)
    os.makedirs(outputs_dir)

    font = BdfFont()

    font.point_size = 16
    font.resolution_xy = 75, 75
    font.bounding_box_size = 16, 16
    font.bounding_box_offset = 0, -2

    font.glyphs.append(BdfGlyph(
        name='A',
        code_point=ord('A'),
        scalable_width=(500, 0),
        device_width=(8, 0),
        bounding_box_size=(8, 16),
        bounding_box_offset=(0, -2),
        bitmap=[
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 1, 1, 0, 0, 0],
            [0, 0, 1, 0, 0, 1, 0, 0],
            [0, 0, 1, 0, 0, 1, 0, 0],
            [0, 1, 0, 0, 0, 0, 1, 0],
            [0, 1, 0, 0, 0, 0, 1, 0],
            [0, 1, 1, 1, 1, 1, 1, 0],
            [0, 1, 0, 0, 0, 0, 1, 0],
            [0, 1, 0, 0, 0, 0, 1, 0],
            [0, 1, 0, 0, 0, 0, 1, 0],
            [0, 1, 0, 0, 0, 0, 1, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
        ],
    ))

    font.properties.foundry = 'Pixel Font Studio'
    font.properties.family_name = 'Demo Pixel'
    font.properties.weight_name = 'Medium'
    font.properties.slant = 'R'
    font.properties.setwidth_name = 'Normal'
    font.properties.add_style_name = 'Sans Serif'
    font.properties.pixel_size = font.point_size
    font.properties.point_size = font.point_size * 10
    font.properties.resolution_x = font.resolution_x
    font.properties.resolution_y = font.resolution_y
    font.properties.spacing = 'P'
    font.properties.average_width = round(sum([glyph.device_width_x * 10 for glyph in font.glyphs]) / len(font.glyphs))
    font.properties.charset_registry = 'ISO10646'
    font.properties.charset_encoding = '1'
    font.generate_name_as_xlfd()

    font.properties.default_char = -1
    font.properties.font_ascent = 14
    font.properties.font_descent = 2
    font.properties.x_height = 5
    font.properties.cap_height = 7

    font.properties.font_version = '1.0.0'
    font.properties.copyright = 'Copyright (c) TakWolf'

    font.save(os.path.join(outputs_dir, 'my-font.bdf'))


if __name__ == '__main__':
    main()

Test Fonts

References

License

Under the MIT license.

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

bdffont-0.0.21.tar.gz (1.9 MB view details)

Uploaded Source

Built Distribution

bdffont-0.0.21-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file bdffont-0.0.21.tar.gz.

File metadata

  • Download URL: bdffont-0.0.21.tar.gz
  • Upload date:
  • Size: 1.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for bdffont-0.0.21.tar.gz
Algorithm Hash digest
SHA256 4d377bf2b321ad6f817e9bde85e91cae475602ed43f82762f0654d09aa92a8bb
MD5 7e6bb5005ab2dea3779cc89fc10afb0c
BLAKE2b-256 cd9208fe1a8d97cda196c6fc6d3ac3da9590696bb059390dfea885b16ab61203

See more details on using hashes here.

Provenance

File details

Details for the file bdffont-0.0.21-py3-none-any.whl.

File metadata

  • Download URL: bdffont-0.0.21-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for bdffont-0.0.21-py3-none-any.whl
Algorithm Hash digest
SHA256 622e4d375e752a56c29977af9e4c54218ab16d4f47ec45a6de5a0e0294860935
MD5 b34f6bfcddd292cb8adab15208fdfef3
BLAKE2b-256 6ebf5339534b12e680710148333773bdcfaf4802d1985680bae18fc390ad773b

See more details on using hashes here.

Provenance

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