mcfonts.provider.bitmap

A bitmap provider, handling PNG glyphs.

Module Contents

class BitmapProvider(*, ascent: int, chars: collections.abc.Sequence[str], file: str, height: int | None)

Bases: mcfonts.provider.Provider[mcfonts.glyph.bitmap.BitmapGlyph], mcfonts.provider.HasPriority, mcfonts.serde.minecraft.MinecraftLoader[BitmapProvider], mcfonts.serde.minecraft.MinecraftSaver, mcfonts.serde.opentype.OpenTypeSaver, mcfonts.serde.unihex.UnihexSaver, mcfonts.serde.yaff.YaffSaver, mcfonts.render.Renderable

The bitmap provider defines simple bitmap glyphs from a texture.

The texture is evenly divided according to the number of entries and the length of the items of the "chars" field.

When adding new glyphs for characters, that character must exist in the chars list. If it does not exist, a ValueError will be raised. Characters must be added to the chars list beforehand; this is not done automatically.

Initialize.

Parameters:
ascent : int

Vertical shift.

chars : collections.abc.Sequence[str]

Character list.

file : str

Resource location to PNG.

height : int | None

Scale of glyphs.

__contains__(item: object) bool
Parameters:
item : object

Return type:

bool

__deepcopy__(memodict: collections.abc.Mapping[Any, Any] | None = None) BitmapProvider

Return a deep copy of this provider.

In order:

  1. Initialization, field copy

  2. Filter copy

  3. Glyph copy

Returns:

A provider copy that is equal but not identical.

Parameters:
memodict : collections.abc.Mapping[Any, Any] | None

Return type:

BitmapProvider

__delitem__(key: str) None
Parameters:
key : str

Return type:

None

__getitem__(key: str) mcfonts.glyph.bitmap.BitmapGlyph
Parameters:
key : str

Return type:

mcfonts.glyph.bitmap.BitmapGlyph

__iter__() collections.abc.Generator[str]
Return type:

collections.abc.Generator[str]

__len__() int

Return the number of glyphs.

Return type:

int

__repr__() str

Return BitmapProvider(x character, height x, ascent x, file x).

Return type:

str

__setitem__(key: str, value: mcfonts.glyph.bitmap.BitmapGlyph)
Parameters:
key : str

value : mcfonts.glyph.bitmap.BitmapGlyph

Return type:

None

as_dict() dict[str, Any]
Return type:

dict[str, Any]

get_ascent() int

Get the ascent field.

The ascent is always less than the height.

Returns:

Value of ascent field.

Return type:

int

get_chars() list[str]

Get the chars field.

Returns:

Value of chars field.

Return type:

list[str]

get_file() str

Get the file field.

Returns:

Value of file field.

Return type:

str

classmethod get_glyph_type() type[mcfonts.glyph.bitmap.BitmapGlyph]

Return the type of Glyph this provider uses.

Returns:

Type of Glyph.

Return type:

type[mcfonts.glyph.bitmap.BitmapGlyph]

get_height() int

Get the height field.

Returns:

Value of height field.

Return type:

int

classmethod get_pack_versions() tuple[int, None]

Return a tuple of the minimum and maximum pack versions for which this provider is used.

The tuple contains 2 optional integers - the start and end pack versions, both inclusive. None items indicate no bound:

  • (None, 4) means from any up to 4.

  • (4, None) means from 4 up to any.

Returns:

A 2-element tuple of the minimum and maximum valid pack versions.

Return type:

tuple[int, None]

classmethod get_priority() int

Return the priority of this type of provider.

Returns:

Priority.

Return type:

int

classmethod minecraft_load(root: mcfonts.path.PackRoot, resource_location: str, contents: collections.abc.Mapping[str, Any]) BitmapProvider

Return an instance of a provider, given the contents of the provider and where it is.

Parameters:
root : mcfonts.path.PackRoot

Resource pack root.

resource_location : str

Resource location of font JSON.

contents : collections.abc.Mapping[str, Any]

Font JSON.

Returns:

A provider.

Return type:

BitmapProvider

minecraft_save(root: mcfonts.path.PackRoot, resource_location: str) None

Save a provider back to a location.

If the target file already exists, this provider will be appended to it.

Warning

This will overwrite files.

Parameters:
root : mcfonts.path.PackRoot

Resource pack root.

resource_location : str

Resource location of font JSON.

Return type:

None

opentype_save(tables: mcfonts.serde.opentype.OpenTypeTables) None

Save provider into an OpenType font by modifying its tables.

Parameters:
tables : mcfonts.serde.opentype.OpenTypeTables

Table storage.

Return type:

None

render(character: str, metrics: mcfonts.render.RenderMetrics) mcfonts.render.RenderResult | None

Draw a character contained in this provider.

Parameters:
character : str

The character to render.

metrics : mcfonts.render.RenderMetrics

Current rendering metrics.

Returns:

RenderResult, or None if character cannot be rendered by this provider.

Return type:

mcfonts.render.RenderResult | None

set_ascent(value: int) None

Set the ascent field.

Parameters:
value : int

New value.

Raises:

ValueError -- If value is not less than height.

Return type:

None

set_chars(value: collections.abc.Sequence[str]) None

Set the chars field.

Parameters:
value : collections.abc.Sequence[str]

New chars (a list of strings).

Return type:

None

set_file(value: str) None

Set the file field.

Parameters:
value : str

New value.

Return type:

None

set_height(value: int | None) None

Set the height field.

Parameters:
value : int | None

New height, or None to unset.

Return type:

None

classmethod set_priority(priority: int) None

Set the priority of this type of provider.

Parameters:
priority : int

New priority.

Return type:

None

unihex_save(entries: collections.abc.MutableSequence[str]) None

Save provider into entries.

Parameters:
entries : collections.abc.MutableSequence[str]

List of the current lines in the file.

Return type:

None

yaff_save(data: mcfonts.serde.yaff.YaffData) None

Save provider into data.

Parameters:
data : mcfonts.serde.yaff.YaffData

Current whole YAFF font data.

Return type:

None

default_height : ClassVar[int] = 8
invalid_codepoints : ClassVar[set[int]]
priority : ClassVar[int] = 3
image_from_provider(provider: BitmapProvider) PIL.Image.Image

Return an Image that reflects the current state of provider.

Assumes the first glyph's dimensions are equal for all other glyphs.

This function does not use the provider's file field: the image is built from its glyphs.

Parameters:
provider : BitmapProvider

Return type:

PIL.Image.Image

image_to_sequential_chars(image: PIL.Image.Image, character: str, cell_bounds: tuple[int, int] = (8, 8)) collections.abc.Generator[str]

Given a path to a image and a starting character, return an appropriate chars list.

Return a provider that has a charlist that correctly encompasses all chars covered by the resource. Glyphs in the resource that are empty are skipped.

Parameters:
image : PIL.Image.Image

The image to grab glyph textures from.

character : str

A single character of the starting point. The codepoint of this will increase by 1 on every glyph.

cell_bounds : tuple[int, int]

The dimensions of an individual glyph in image. Glyph dimensions must be the same throughout the whole of image.

Returns:

A valid chars list.

Return type:

collections.abc.Generator[str]

validate_chars(chars: collections.abc.Sequence[str]) None

Validate a "chars" field value.

Parameters:
chars : collections.abc.Sequence[str]

Character list.

Raises:

ValueError -- If an issue is found.

Return type:

None