Skip to main content

A library for parsing Bits'N'Picas native save format files ('.kbits' and '.kbitx')

Project description

KbitFont.Python

Python PyPI

KbitFont is a library for parsing Bits'N'Picas native save format files (.kbits and .kbitx).

Installation

pip install kbitfont

Usage

Create

import shutil

from examples import build_dir
from kbitfont import KbitFont, KbitGlyph


def main():
    outputs_dir = build_dir.joinpath('create')
    if outputs_dir.exists():
        shutil.rmtree(outputs_dir)
    outputs_dir.mkdir(parents=True)

    font = KbitFont()
    font.props.em_ascent = 14
    font.props.em_descent = 2
    font.props.line_ascent = 14
    font.props.line_descent = 2
    font.props.x_height = 7
    font.props.cap_height = 10

    font.names.version = '1.0.0'
    font.names.family = 'My Font'
    font.names.style = 'Regular'
    font.names.manufacturer = 'Pixel Font Studio'
    font.names.designer = 'TakWolf'
    font.names.description = 'A pixel font'
    font.names.copyright = 'Copyright (c) TakWolf'
    font.names.license_description = 'This Font Software is licensed under the SIL Open Font License, Version 1.1'
    font.names.vendor_url = 'https://github.com/TakWolf/kbitfont-python'
    font.names.designer_url = 'https://takwolf.com'
    font.names.license_url = 'https://openfontlicense.org'

    font.characters[65] = KbitGlyph(
        x=0,
        y=14,
        advance=8,
        bitmap=[
            [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
            [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
            [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
            [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
            [0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00],
            [0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00],
            [0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00],
            [0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00],
            [0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00],
            [0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00],
            [0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00],
            [0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00],
            [0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00],
            [0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00],
            [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
            [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
        ],
    )

    font.named_glyphs['.notdef'] = KbitGlyph(
        x=0,
        y=14,
        advance=8,
        bitmap=[
            [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
            [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
            [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
            [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
            [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
            [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
            [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
            [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
            [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
            [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
            [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
            [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
            [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
            [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
            [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
            [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
        ],
    )

    font.save_kbits(outputs_dir.joinpath('my-font.kbits'))
    font.save_kbitx(outputs_dir.joinpath('my-font.kbitx'))


if __name__ == '__main__':
    main()

Load Kbits

import shutil

from examples import assets_dir, build_dir
from kbitfont import KbitFont


def main():
    outputs_dir = build_dir.joinpath('load_kbits')
    if outputs_dir.exists():
        shutil.rmtree(outputs_dir)
    outputs_dir.mkdir(parents=True)

    font = KbitFont.load_kbits(assets_dir.joinpath('macintosh', 'Athens.kbits'))
    print(f'name: {font.names.family}')
    print(f'size: {font.props.em_height}')
    print(f'ascent: {font.props.line_ascent}')
    print(f'descent: {font.props.line_descent}')
    print()
    for code_point, glyph in sorted(font.characters.items()):
        print(f'char: {chr(code_point)} ({code_point:04X})')
        print(f'xy: {(glyph.x, glyph.y)}')
        print(f'dimensions: {glyph.dimensions}')
        print(f'advance: {glyph.advance}')
        for bitmap_row in glyph.bitmap:
            text = ''.join('  ' if color <= 127 else '██' for color in bitmap_row)
            print(f'{text}*')
        print()
    font.save_kbits(outputs_dir.joinpath('Athens.kbits'))


if __name__ == '__main__':
    main()

Load Kbitx

import shutil

from examples import assets_dir, build_dir
from kbitfont import KbitFont


def main():
    outputs_dir = build_dir.joinpath('load_kbitx')
    if outputs_dir.exists():
        shutil.rmtree(outputs_dir)
    outputs_dir.mkdir(parents=True)

    font = KbitFont.load_kbitx(assets_dir.joinpath('macintosh', 'Athens.kbitx'))
    print(f'name: {font.names.family}')
    print(f'size: {font.props.em_height}')
    print(f'ascent: {font.props.line_ascent}')
    print(f'descent: {font.props.line_descent}')
    print()
    for code_point, glyph in sorted(font.characters.items()):
        print(f'char: {chr(code_point)} ({code_point:04X})')
        print(f'xy: {(glyph.x, glyph.y)}')
        print(f'dimensions: {glyph.dimensions}')
        print(f'advance: {glyph.advance}')
        for bitmap_row in glyph.bitmap:
            text = ''.join('  ' if color <= 127 else '██' for color in bitmap_row)
            print(f'{text}*')
        print()
    font.save_kbitx(outputs_dir.joinpath('Athens.kbitx'))


if __name__ == '__main__':
    main()

Specifications

Font Struct

Kbits

Kbitx

Dependencies

License

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

kbitfont-0.0.3.tar.gz (52.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kbitfont-0.0.3-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file kbitfont-0.0.3.tar.gz.

File metadata

  • Download URL: kbitfont-0.0.3.tar.gz
  • Upload date:
  • Size: 52.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for kbitfont-0.0.3.tar.gz
Algorithm Hash digest
SHA256 d15fa4379aabb16dbb5cce5107cafdbdf8bad5728cbffed4c1d5935fdb6ee4ad
MD5 1555343e6d27249c3ab46778d74f8b2d
BLAKE2b-256 64cf0f2a951e98301821511244e0e53a7ac397e76b369653cd60aaaa34cec086

See more details on using hashes here.

Provenance

The following attestation bundles were made for kbitfont-0.0.3.tar.gz:

Publisher: publish.yml on TakWolf/kbitfont-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kbitfont-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: kbitfont-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for kbitfont-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7790e3068b241cf1be2ed8a4f49f7e322500b5d4f61c85969db4fdc7029bbab3
MD5 b1c30cf6b813a96a0c468138d47f7b3a
BLAKE2b-256 67933f61bbb3b82789d30dafa9edcc1bf7f9d6d5cb2d4c61fb699f153fab7315

See more details on using hashes here.

Provenance

The following attestation bundles were made for kbitfont-0.0.3-py3-none-any.whl:

Publisher: publish.yml on TakWolf/kbitfont-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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