PcfFont - Python
A library for handling Portable Compiled Format (PCF) Fonts.
Installation
pip install pcffont
Usage
Create
import shutil
import statistics
from examples import build_dir
from pcffont import PcfFontBuilder, PcfGlyph
def main():
outputs_dir = build_dir.joinpath('create')
if outputs_dir.exists():
shutil.rmtree(outputs_dir)
outputs_dir.mkdir(parents=True)
builder = PcfFontBuilder()
builder.config.font_ascent = 14
builder.config.font_descent = 2
builder.config.default_char = 0xFFFE
builder.glyphs.append(PcfGlyph(
name='.notdef',
encodings={0xFFFE},
scalable_width=500,
character_width=8,
dimensions=(8, 16),
offset=(0, -2),
bitmap=[
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
],
))
builder.glyphs.append(PcfGlyph(
name='LATIN_CAPITAL_LETTER_A',
encodings={0x0041},
scalable_width=500,
character_width=8,
dimensions=(8, 16),
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],
],
))
builder.properties.foundry = 'Pixel Font Studio'
builder.properties.family_name = 'My Font'
builder.properties.weight_name = 'Medium'
builder.properties.slant = 'R'
builder.properties.setwidth_name = 'Normal'
builder.properties.add_style_name = 'Sans Serif'
builder.properties.pixel_size = 16
builder.properties.point_size = builder.properties.pixel_size * 10
builder.properties.resolution_x = 75
builder.properties.resolution_y = 75
builder.properties.spacing = 'P'
builder.properties.average_width = round(statistics.fmean(glyph.character_width * 10 for glyph in builder.glyphs))
builder.properties.charset_registry = 'ISO10646'
builder.properties.charset_encoding = '1'
builder.properties.generate_xlfd()
builder.properties.x_height = 7
builder.properties.cap_height = 10
builder.properties.underline_position = -2
builder.properties.underline_thickness = 1
builder.properties.font_version = '1.0.0'
builder.properties.copyright = 'Copyright (c) TakWolf'
builder.save(outputs_dir.joinpath('my-font.pcf'))
if __name__ == '__main__':
main()
Load
import shutil
from examples import assets_dir, build_dir
from pcffont import PcfFont
def main():
outputs_dir = build_dir.joinpath('load')
if outputs_dir.exists():
shutil.rmtree(outputs_dir)
outputs_dir.mkdir(parents=True)
font = PcfFont.load(assets_dir.joinpath('unifont', 'unifont-17.0.05.pcf'))
print(f'name: {font.properties.font}')
print(f'size: {font.properties.pixel_size}')
print(f'ascent: {font.accelerators.font_ascent}')
print(f'descent: {font.accelerators.font_descent}')
print()
for encoding, glyph_index in sorted(font.bdf_encodings.items()):
glyph_name = font.glyph_names[glyph_index]
metric = font.metrics[glyph_index]
bitmap = font.bitmaps[glyph_index]
print(f'char: {chr(encoding)} ({encoding:04X})')
print(f'glyph_name: {glyph_name}')
print(f'advance_width: {metric.character_width}')
print(f'dimensions: {metric.dimensions}')
print(f'offset: {metric.offset}')
for bitmap_row in bitmap:
text = ''.join(' ' if pixel == 0 else '██' for pixel in bitmap_row)
print(f'{text}*')
print()
font.save(outputs_dir.joinpath('unifont-17.0.05.pcf'))
if __name__ == '__main__':
main()
References
- libXfont - X font handling library for server & utilities
- Debian - X Consortium - lib/font/bitmap
- FreeType font driver for PCF fonts
- bdftopcf
- FontForge - The X11 PCF bitmap font file format
- The X Font Library
- X Logical Font Description Conventions - X Consortium Standard
License
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pcffont-0.0.34.tar.gz
(17.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
pcffont-0.0.34-py3-none-any.whl
(26.9 kB
view details)
File details
Details for the file pcffont-0.0.34.tar.gz.
File metadata
- Download URL: pcffont-0.0.34.tar.gz
- Upload date:
- Size: 17.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77de4ddd257b03bbd0a2925381650cef39b2acd3cb474815c41057834667e046
|
|
| MD5 |
5abdb4ae4c7a564b64094bbb8baeb414
|
|
| BLAKE2b-256 |
f43f7d67ed6d68a10bc3bba79737d54561743b375b254f8f12faab586f8da516
|
File details
Details for the file pcffont-0.0.34-py3-none-any.whl.
File metadata
- Download URL: pcffont-0.0.34-py3-none-any.whl
- Upload date:
- Size: 26.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c1c46a4b8ce8f6b6f63e464633c8886299855310b04939fba544f2ecea08ffb
|
|
| MD5 |
d0dbc0cf6bd680960448f8a38e405b32
|
|
| BLAKE2b-256 |
6ecc6a9fdbdffcaa2fd0d5c545f1b531966b5eb4981cab74e0e806792ca4c059
|