Skip to main content

A library for manipulating '.bdf' format fonts.

Project description

BdfFont

Python PyPI

BdfFont is a library for manipulating .bdf format 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.0.01.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}')
    for glyph in font.get_glyphs():
        print(f'glyph: {glyph.name} - {glyph.code_point:04X}')
    font.save(os.path.join(outputs_dir, 'unifont-15.0.01.bdf'), optimize_bitmap=True)


if __name__ == '__main__':
    main()

Create

import os
import shutil

from bdffont import BdfFont, BdfGlyph, xlfd
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(
        point_size=16,
        resolution_xy=(75, 75),
        bounding_box_size=(16, 16),
        bounding_box_offset=(0, -2),
    )

    font.add_glyph(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.add_style_name = xlfd.AddStyleName.SANS_SERIF
    font.properties.pixel_size = 16
    font.properties.point_size = 160
    font.properties.spacing = xlfd.Spacing.PROPORTIONAL
    font.properties.average_width = round(sum([glyph.device_width_x * 10 for glyph in font.code_point_to_glyph.values()]) / font.get_glyphs_count())
    font.setup_missing_xlfd_properties()

    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.generate_xlfd_font_name()

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


if __name__ == '__main__':
    main()

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.17.tar.gz (1.9 MB view details)

Uploaded Source

Built Distribution

bdffont-0.0.17-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for bdffont-0.0.17.tar.gz
Algorithm Hash digest
SHA256 2413e8e6d9b09d74733e965b06b716db0102fd735df0cfa68b9f02448a4e54bd
MD5 9e0c60c40d86501e9d4577f34b2d6fc3
BLAKE2b-256 fec7293dd14d4655c03495adc0cc60b427bed97dc2d8d2ac1bc476600500a647

See more details on using hashes here.

Provenance

File details

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

File metadata

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

File hashes

Hashes for bdffont-0.0.17-py3-none-any.whl
Algorithm Hash digest
SHA256 7c8e446982a5b6ed80230db9c5d2aeb93e54572f7b397d00b75ac37c0cfca518
MD5 bf8d514e359125d2ab4e89bb70bd1586
BLAKE2b-256 b1b990827f59ce928645cfcb940ea1f3e274c8e7465b7b684a999623dcfe4972

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