mcfonts.provider.legacy_unicode

A legacy unicode provider, handling bitmap glyphs in a 16*16 pixel grid.

Module Contents

class GlyphSizes(contents: bytes | bytearray | None = None)

Bases: collections.abc.MutableMapping[str, tuple[int, int]]

Container for the legacy unicode "glyph_sizes.bin" format.

Initialize.

Parameters:
contents : bytes | bytearray | None

Contents of the sizes file. If None, sizes is entirely empty.

__bytes__() bytes

Return bytes of this bytearray.

Return type:

bytes

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

Return a deepcopy of this instance.

Returns:

A sizes copy that is equal but not identical.

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

Return type:

GlyphSizes

__delitem__(key: str) None

Set key bearings to (0, 0).

Parameters:
key : str

Return type:

None

__getitem__(item: str) tuple[int, int]

Get decoded size of a character.

Parameters:
item : str

The single character.

Returns:

Tuple of (left bearing, right bearing).

Raises:

KeyError -- If character is above U+FFFF.

Return type:

tuple[int, int]

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

collections.abc.Generator[str]

__len__() int

Length is number of glyphs.

Return type:

int

__setitem__(key: str, value: tuple[int, int])

Set decoded size of a character.

Parameters:
key : str

The single character.

value : tuple[int, int]

Tuple of (left bearing, right bearing).

Raises:

KeyError -- If either size is not within 0 to 15, or if character is above U+FFFF.

Return type:

None

get_bytes() bytearray
Returns:

65536 bytes of contents.

Return type:

bytearray

set_bytes(value: bytes | bytearray) None

Set contents of this glyph sizes.

Parameters:
value : bytes | bytearray

The new bytes.

Raises:

ValueError -- If length of value is not 65536.

Return type:

None

to_space_advances(match_unicode_category: collections.abc.Sequence[str] | None = None) dict[str, int]

Translate a glyphsizes.bin file into an "advances" mapping, which goes inside a "space" provider.

Note

This function does not return a new provider.

>>> GlyphSizes(b"\x00\x00\x00...").to_space_advances()
{
    "\x00": 0,
    "\x01": 0,
    "\x02": 0,
    "\uBEEF": 15
}
Parameters:
match_unicode_category : collections.abc.Sequence[str] | None

Only translate Unicode characters with these categories. By default, this is [Mc, Zp, Zs, Zl]. This should cover most whitespace and marking characters. To work on all character, set this to [], not None.

Returns:

An "advances" dictionary.

Return type:

dict[str, int]

trim(character: str, image: PIL.Image.Image) PIL.Image.Image

Trim the left and right columns of image to comply with the sizes for character.

Parameters:
character : str

The character to find a sizes for.

image : PIL.Image.Image

The image to trim.

Returns:

Nothing or new image (copied).

Return type:

PIL.Image.Image

default_size : ClassVar[bytes] = b'\x0f'
class LegacyUnicodeProvider(*, template: str, sizes: str)

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

The legacy unicode provider is a "fallback" provider intended to be used as a last-resort.

It uses a system of templates to create and add 16*16 pixel tables of "fallback" characters. Each glyph is 16*16 wide, so each page is 256*256 -- 16 characters on each line, 16 lines, 16 length for each glyph. Its glyphs are bitmaps.

GlyphSizes controls the size of each glyph, but only when getting it. No trimming is performed when setting.

The GlyphSizes has a higher priority over glyph widths. For example, setting a character to a glyph while that character's "glyph size" is (1, 8) will cause the glyph's image to be automatically trimmed to have width bearings of (1, 8).

Warning

This provider is deprecated and should not be used when possible. Use the "unihex" provider instead.

Important

Characters above the BMP (U+FFFF) are not handled or supported.

Initialize.

Parameters:
template : str

Template string -- must include "%s".

sizes : str

Sizes path.

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

Return type:

bool

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

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:

LegacyUnicodeProvider

__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 LegacyUnicodeProvider().

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_glyph_sizes() GlyphSizes

Get a deep copy of the internal glyph sizes.

Returns:

A GlyphSizes instance.

Return type:

GlyphSizes

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_in_codepage(codepage: int) collections.abc.Generator[tuple[str, mcfonts.glyph.bitmap.BitmapGlyph]]

Get all characters and glyphs in a codepage.

Parameters:
codepage : int

The codepage number.

Returns:

Generator yielding tuples of (character, glyph) for the codepage.

Return type:

collections.abc.Generator[tuple[str, mcfonts.glyph.bitmap.BitmapGlyph]]

classmethod get_pack_versions() tuple[int, int]

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, int]

classmethod get_priority() int

Return the priority of this type of provider.

Returns:

Priority.

Return type:

int

get_sizes() str

Get the sizes path.

Returns:

Path to the glyph sizes file.

Return type:

str

get_template() str

Get the template field.

Returns:

Value of template field.

Return type:

str

static get_valid_codepages() collections.abc.Generator[int]

Get a generator of all valid codepage numbers.

Returns:

Generator yielding valid codepage numbers from 0x00 to 0xFF, excluding invalid codepages.

Return type:

collections.abc.Generator[int]

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

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:

LegacyUnicodeProvider

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_glyph_sizes(value: GlyphSizes | None) None

Set the glyph sizes.

Parameters:
value : GlyphSizes | None

New GlyphSizes instance, or None to use blank sizes.

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

set_sizes(value: str) None

Set the sizes path.

Parameters:
value : str

Path to the glyph sizes file.

Return type:

None

set_template(value: str) None

Set the template field.

Parameters:
value : str

New value.

Raises:

ValueError -- If value does not contain "%s".

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

invalid_codepages : ClassVar[set[int]]
priority : ClassVar[int] = 0
auto_trim_glyph_sizes(provider: LegacyUnicodeProvider) None

For all characters, re-assign its spot in glyph_sizes to match its current metrics.

Parameters:
provider : LegacyUnicodeProvider

The provider.

Return type:

None

build_template_for_character(character: str) PIL.Image.Image

Build an image of a template character.

Templates are simple white boxes with a codepoint texture inside them. To customize the template texture, change the images within TEMPLATES.

Parameters:
character : str

A single character. This character's codepoint is what goes inside the box.

Returns:

A PIL.Image.Image of the character template, in RGBA mode.

Return type:

PIL.Image.Image

create_page(provider: LegacyUnicodeProvider, codepage: int = 0) PIL.Image.Image

Create an image of a specific codepage for some LegacyUnicodeProvider.

Parameters:
provider : LegacyUnicodeProvider

The legacy unicode provider.

codepage : int

Codepage integer, default is 00.

Returns:

An image of a 16x16 grid of glyphs.

Raises:

ValueError -- If the specified codepage contains no glyphs.

Return type:

PIL.Image.Image

get_codepage(character: str) int

Return the codepage a character is in.

>>> get_codepage("╪")
25
Parameters:
character : str

The single character.

Returns:

Codepage integer.

Return type:

int

iterate_grid(image: PIL.Image.Image, row_column_count: tuple[int, int]) collections.abc.Generator[PIL.Image.Image]

Yield images of the sections of a divided resource.

Given a resource and number of characters in each row and column of that resource, yield every PIL.Image.Image in that resource.

If no data exists in a cell, yield None.

Parameters:
image : PIL.Image.Image

The PIL.Image.Image.

row_column_count : tuple[int, int]

A tuple of the number of characters in each row and column of image.

Returns:

A yield of PIL.Image.Image.

Return type:

collections.abc.Generator[PIL.Image.Image]

PF
TEMPLATES : Final[dict[int, PIL.Image.Image]]