pcffont


Namepcffont JSON
Version 0.0.6 PyPI version JSON
download
home_pageNone
SummaryA library for manipulating Portable Compiled Format (PCF) Fonts.
upload_time2024-04-17 07:36:41
maintainerTakWolf
docs_urlNone
authorTakWolf
requires_python>=3.11
licenseMIT License
keywords font pcf
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PcfFont

[![Python](https://img.shields.io/badge/python-3.11-brightgreen)](https://www.python.org)
[![PyPI](https://img.shields.io/pypi/v/pcffont)](https://pypi.org/project/pcffont/)

PcfFont is a library for manipulating [Portable Compiled Format (PCF) Fonts](https://en.wikipedia.org/wiki/Portable_Compiled_Format).

## Installation

```shell
pip install pcffont
```

## Usage

### Load

```python
import os
import shutil

from examples import assets_dir, build_dir
from pcffont import PcfFont


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 = PcfFont.load(os.path.join(assets_dir, 'unifont', 'unifont-15.1.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 code_point, 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(code_point)} ({code_point:04X})')
        print(f'glyph_name: {glyph_name}')
        print(f'advance_width: {metric.character_width}')
        print(f'offset: ({metric.left_side_bearing}, {-metric.descent})')
        for bitmap_row in bitmap:
            text = ''.join(map(str, bitmap_row)).replace('0', '  ').replace('1', '██')
            print(f'{text}*')
        print()
    font.save(os.path.join(outputs_dir, 'unifont-15.1.05.pcf'))


if __name__ == '__main__':
    main()
```

### Create

```python
import os
import shutil

from examples import build_dir
from pcffont import PcfFont, PcfTableFormat, PcfMetric, PcfProperties, PcfAccelerators, PcfMetrics, PcfBitmaps, PcfBdfEncodings, PcfScalableWidths, PcfGlyphNames


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 = PcfFont()

    encodings = PcfBdfEncodings()
    glyph_names = PcfGlyphNames()
    metrics = PcfMetrics()
    scalable_widths = PcfScalableWidths()
    bitmaps = PcfBitmaps()

    encodings[ord('A')] = len(glyph_names)
    glyph_names.append('A')
    metrics.append(PcfMetric(
        left_side_bearing=0,
        right_side_bearing=8,
        character_width=8,
        ascent=14,
        descent=2,
    ))
    scalable_widths.append(500)
    bitmaps.append([
        [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.bdf_encodings = encodings
    font.glyph_names = glyph_names
    font.metrics = metrics
    font.ink_metrics = metrics
    font.scalable_widths = scalable_widths
    font.bitmaps = bitmaps

    accelerators = PcfAccelerators(PcfTableFormat.build(has_ink_bounds=True))
    accelerators.no_overlap = True
    accelerators.ink_inside = True
    accelerators.ink_metrics = True
    accelerators.font_ascent = 14
    accelerators.font_descent = 2
    accelerators.min_bounds = metrics[0]
    accelerators.max_bounds = metrics[0]
    accelerators.ink_min_bounds = metrics[0]
    accelerators.ink_max_bounds = metrics[0]
    font.accelerators = accelerators
    font.bdf_accelerators = accelerators

    properties = PcfProperties()
    properties.foundry = 'Pixel Font Studio'
    properties.family_name = 'Demo Pixel'
    properties.weight_name = 'Medium'
    properties.slant = 'R'
    properties.setwidth_name = 'Normal'
    properties.add_style_name = 'Sans Serif'
    properties.pixel_size = 16
    properties.point_size = properties.pixel_size * 10
    properties.resolution_x = 75
    properties.resolution_y = 75
    properties.spacing = 'P'
    properties.average_width = round(sum([metric.character_width * 10 for metric in font.metrics]) / len(font.metrics))
    properties.charset_registry = 'ISO10646'
    properties.charset_encoding = '1'
    properties.generate_xlfd()
    properties.x_height = 5
    properties.cap_height = 7
    properties.font_version = '1.0.0'
    properties.copyright = 'Copyright (c) TakWolf'
    font.properties = properties

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


if __name__ == '__main__':
    main()
```

## Test Fonts

- [GNU Unifont Glyphs](https://unifoundry.com/unifont/index.html)
- [bitmap-fonts](https://github.com/masaeedu/bitmap-fonts)

## References

- [FreeType font driver for PCF fonts](https://github.com/freetype/freetype/tree/master/src/pcf)
- [FontForge - The X11 PCF bitmap font file format](https://fontforge.org/docs/techref/pcf-format.html)
- [pcf2bdf](https://github.com/ganaware/pcf2bdf)
- [bdftopcf](https://gitlab.freedesktop.org/xorg/util/bdftopcf)
- [X Logical Font Description Conventions - X Consortium Standard](https://www.x.org/releases/current/doc/xorg-docs/xlfd/xlfd.html)

## License

Under the [MIT license](LICENSE).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pcffont",
    "maintainer": "TakWolf",
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "font, pcf",
    "author": "TakWolf",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/a3/bf/77177c9c402abe5b436afd456890bdbf60e5a7e7b75a552ba97f32d7989b/pcffont-0.0.6.tar.gz",
    "platform": null,
    "description": "# PcfFont\n\n[![Python](https://img.shields.io/badge/python-3.11-brightgreen)](https://www.python.org)\n[![PyPI](https://img.shields.io/pypi/v/pcffont)](https://pypi.org/project/pcffont/)\n\nPcfFont is a library for manipulating [Portable Compiled Format (PCF) Fonts](https://en.wikipedia.org/wiki/Portable_Compiled_Format).\n\n## Installation\n\n```shell\npip install pcffont\n```\n\n## Usage\n\n### Load\n\n```python\nimport os\nimport shutil\n\nfrom examples import assets_dir, build_dir\nfrom pcffont import PcfFont\n\n\ndef main():\n    outputs_dir = os.path.join(build_dir, 'load')\n    if os.path.exists(outputs_dir):\n        shutil.rmtree(outputs_dir)\n    os.makedirs(outputs_dir)\n\n    font = PcfFont.load(os.path.join(assets_dir, 'unifont', 'unifont-15.1.05.pcf'))\n    print(f'name: {font.properties.font}')\n    print(f'size: {font.properties.pixel_size}')\n    print(f'ascent: {font.accelerators.font_ascent}')\n    print(f'descent: {font.accelerators.font_descent}')\n    print()\n    for code_point, glyph_index in sorted(font.bdf_encodings.items()):\n        glyph_name = font.glyph_names[glyph_index]\n        metric = font.metrics[glyph_index]\n        bitmap = font.bitmaps[glyph_index]\n        print(f'char: {chr(code_point)} ({code_point:04X})')\n        print(f'glyph_name: {glyph_name}')\n        print(f'advance_width: {metric.character_width}')\n        print(f'offset: ({metric.left_side_bearing}, {-metric.descent})')\n        for bitmap_row in bitmap:\n            text = ''.join(map(str, bitmap_row)).replace('0', '  ').replace('1', '\u2588\u2588')\n            print(f'{text}*')\n        print()\n    font.save(os.path.join(outputs_dir, 'unifont-15.1.05.pcf'))\n\n\nif __name__ == '__main__':\n    main()\n```\n\n### Create\n\n```python\nimport os\nimport shutil\n\nfrom examples import build_dir\nfrom pcffont import PcfFont, PcfTableFormat, PcfMetric, PcfProperties, PcfAccelerators, PcfMetrics, PcfBitmaps, PcfBdfEncodings, PcfScalableWidths, PcfGlyphNames\n\n\ndef main():\n    outputs_dir = os.path.join(build_dir, 'create')\n    if os.path.exists(outputs_dir):\n        shutil.rmtree(outputs_dir)\n    os.makedirs(outputs_dir)\n\n    font = PcfFont()\n\n    encodings = PcfBdfEncodings()\n    glyph_names = PcfGlyphNames()\n    metrics = PcfMetrics()\n    scalable_widths = PcfScalableWidths()\n    bitmaps = PcfBitmaps()\n\n    encodings[ord('A')] = len(glyph_names)\n    glyph_names.append('A')\n    metrics.append(PcfMetric(\n        left_side_bearing=0,\n        right_side_bearing=8,\n        character_width=8,\n        ascent=14,\n        descent=2,\n    ))\n    scalable_widths.append(500)\n    bitmaps.append([\n        [0, 0, 0, 0, 0, 0, 0, 0],\n        [0, 0, 0, 0, 0, 0, 0, 0],\n        [0, 0, 0, 0, 0, 0, 0, 0],\n        [0, 0, 0, 0, 0, 0, 0, 0],\n        [0, 0, 0, 1, 1, 0, 0, 0],\n        [0, 0, 1, 0, 0, 1, 0, 0],\n        [0, 0, 1, 0, 0, 1, 0, 0],\n        [0, 1, 0, 0, 0, 0, 1, 0],\n        [0, 1, 0, 0, 0, 0, 1, 0],\n        [0, 1, 1, 1, 1, 1, 1, 0],\n        [0, 1, 0, 0, 0, 0, 1, 0],\n        [0, 1, 0, 0, 0, 0, 1, 0],\n        [0, 1, 0, 0, 0, 0, 1, 0],\n        [0, 1, 0, 0, 0, 0, 1, 0],\n        [0, 0, 0, 0, 0, 0, 0, 0],\n        [0, 0, 0, 0, 0, 0, 0, 0],\n    ])\n\n    font.bdf_encodings = encodings\n    font.glyph_names = glyph_names\n    font.metrics = metrics\n    font.ink_metrics = metrics\n    font.scalable_widths = scalable_widths\n    font.bitmaps = bitmaps\n\n    accelerators = PcfAccelerators(PcfTableFormat.build(has_ink_bounds=True))\n    accelerators.no_overlap = True\n    accelerators.ink_inside = True\n    accelerators.ink_metrics = True\n    accelerators.font_ascent = 14\n    accelerators.font_descent = 2\n    accelerators.min_bounds = metrics[0]\n    accelerators.max_bounds = metrics[0]\n    accelerators.ink_min_bounds = metrics[0]\n    accelerators.ink_max_bounds = metrics[0]\n    font.accelerators = accelerators\n    font.bdf_accelerators = accelerators\n\n    properties = PcfProperties()\n    properties.foundry = 'Pixel Font Studio'\n    properties.family_name = 'Demo Pixel'\n    properties.weight_name = 'Medium'\n    properties.slant = 'R'\n    properties.setwidth_name = 'Normal'\n    properties.add_style_name = 'Sans Serif'\n    properties.pixel_size = 16\n    properties.point_size = properties.pixel_size * 10\n    properties.resolution_x = 75\n    properties.resolution_y = 75\n    properties.spacing = 'P'\n    properties.average_width = round(sum([metric.character_width * 10 for metric in font.metrics]) / len(font.metrics))\n    properties.charset_registry = 'ISO10646'\n    properties.charset_encoding = '1'\n    properties.generate_xlfd()\n    properties.x_height = 5\n    properties.cap_height = 7\n    properties.font_version = '1.0.0'\n    properties.copyright = 'Copyright (c) TakWolf'\n    font.properties = properties\n\n    font.save(os.path.join(outputs_dir, 'my-font.pcf'))\n\n\nif __name__ == '__main__':\n    main()\n```\n\n## Test Fonts\n\n- [GNU Unifont Glyphs](https://unifoundry.com/unifont/index.html)\n- [bitmap-fonts](https://github.com/masaeedu/bitmap-fonts)\n\n## References\n\n- [FreeType font driver for PCF fonts](https://github.com/freetype/freetype/tree/master/src/pcf)\n- [FontForge - The X11 PCF bitmap font file format](https://fontforge.org/docs/techref/pcf-format.html)\n- [pcf2bdf](https://github.com/ganaware/pcf2bdf)\n- [bdftopcf](https://gitlab.freedesktop.org/xorg/util/bdftopcf)\n- [X Logical Font Description Conventions - X Consortium Standard](https://www.x.org/releases/current/doc/xorg-docs/xlfd/xlfd.html)\n\n## License\n\nUnder the [MIT license](LICENSE).\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "A library for manipulating Portable Compiled Format (PCF) Fonts.",
    "version": "0.0.6",
    "project_urls": {
        "homepage": "https://github.com/TakWolf/pcffont",
        "issues": "https://github.com/TakWolf/pcffont/issues",
        "source": "https://github.com/TakWolf/pcffont"
    },
    "split_keywords": [
        "font",
        " pcf"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ae9c3f33dc8f610394326bdb357937ccb82b9b145c5e00bb620ea05157465142",
                "md5": "fa7d10ad1df9179e28225d74f8af9452",
                "sha256": "722d83de4b93d058b17c537b55fddb2bf1263ce0035af1ac58d1bad2671e4dfc"
            },
            "downloads": -1,
            "filename": "pcffont-0.0.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fa7d10ad1df9179e28225d74f8af9452",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 18362,
            "upload_time": "2024-04-17T07:36:38",
            "upload_time_iso_8601": "2024-04-17T07:36:38.967070Z",
            "url": "https://files.pythonhosted.org/packages/ae/9c/3f33dc8f610394326bdb357937ccb82b9b145c5e00bb620ea05157465142/pcffont-0.0.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a3bf77177c9c402abe5b436afd456890bdbf60e5a7e7b75a552ba97f32d7989b",
                "md5": "91d203f23d53c22788177e02da69258d",
                "sha256": "c22342fe531e328a3724532adaa8b1d3774c421bac05af92767ac8eb2b4daec8"
            },
            "downloads": -1,
            "filename": "pcffont-0.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "91d203f23d53c22788177e02da69258d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 1384449,
            "upload_time": "2024-04-17T07:36:41",
            "upload_time_iso_8601": "2024-04-17T07:36:41.181094Z",
            "url": "https://files.pythonhosted.org/packages/a3/bf/77177c9c402abe5b436afd456890bdbf60e5a7e7b75a552ba97f32d7989b/pcffont-0.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-17 07:36:41",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "TakWolf",
    "github_project": "pcffont",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "pcffont"
}
        
Elapsed time: 0.32949s