mcfonts.providers.legacy_unicode

A legacy unicode provider, handling bitmap glyphs in a 16px x 16px grid.

Module Contents

Classes

LegacyUnicodeProvider

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

Functions

align_unicode_page(sheet)

Align a Unicode page font sheet's characters to the left.

build_template_for_character(character[, template_storage])

Build an image of a template codepoint.

to_advances(glyphsizes[, match_unicode_category])

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

Attributes

TEMPLATE_CHARS

class mcfonts.providers.legacy_unicode.LegacyUnicodeProvider(provider, origin, resources)

Bases: mcfonts.providers.base.Provider

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

It is similar to the bitmap provider in that its glyphs are bitmaps.

It uses a system of templates to create and add 16px x 16px tables of "fallback" characters. Each glyph is 16px x 16px pixels wide, so each page is 256px x 256px; 16 characters on each line, 16 lines, 16 length for each glyph.

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.

Construct a Provider base class.

Parameters:
provider_type

A string of the value in the JSON "type" field.

origin : os.PathLike[str] | str

The absolute path to the provider's JSON file.

chars_covered

A set of the individual characters covered by this provider.

provider : mcfonts.utils.types.TypedProviderLegacyUnicode

resources : dict[str, PIL.Image.Image | bytes]

construct_glyphs()

Yield a tuple of (character, glyph) for every glyph available in the provider's resource.

Some glyphs may be empty or have no content.

Returns:

A yield of (character, Glyph), or None if there are no glyphs.

Return type:

collections.abc.Iterable[tuple[str, mcfonts.glyphs.BitmapGlyph]]

get_contents()

Return the contents of the provider.

Returns:

Dictionary of the provider.

Return type:

mcfonts.utils.types.TypedProviderLegacyUnicode

get_icon()

Return a Unicode character that can be used to represent this provider in shorthand descriptions.

Returns:

A 1-character string.

Return type:

str

get_pack_versions()

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

The first element is guaranteed to be an integer. There will always be a minimum version, even if it's 0. The second element may be an integer, or None. If it is None, there is no maximum version bound.

Returns:

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

Return type:

tuple[int, int]

pretty_print()

Return a short one line description of the provider.

Returns:

A string of the provider's info, normally <icon> <type>: <info,...>.

Return type:

str

print_info()

Print information about this provider.

Return type:

None

validate()

Specific tests for validating a provider.

This function checks values to ensure that they're correct and in an acceptable range.

If applicable, textures are checked to ensure all characters have a defined glyph, and that the dimensions are correct.

Warnings are printed through mcfonts.logger.warning().

Returns:

Nothing; problems are either raised or warned.

Return type:

None

yield_characters()

Yield strings of the individual characters this provider supports.

Returns:

A yield of strings of length 1, or None if no characters are present.

Return type:

collections.abc.Iterable[str]

yield_glyphs()

Yield a tuple of (character, glyph) for every glyph available in the provider's resource.

This is different from construct_glyphs() because it yields glyphs that have already been constructed and stored.

Returns:

A yield of (character, Glyph).

Return type:

collections.abc.Iterable[tuple[str, mcfonts.glyphs.BitmapGlyph]]

yield_glyphs_in_unirange(unirange_notation)

Given unirange_notation, return a dictionary of the requested characters to their glyphs.

Parameters:
unirange_notation : str

A string representing the requested range of chars. See https://pypi.org/project/unirange/.

Returns:

A yield of the requested glyphs that match unirange_notation.

Return type:

collections.abc.Iterable[tuple[str, mcfonts.glyphs.BitmapGlyph | None]]

mcfonts.providers.legacy_unicode.TEMPLATE_CHARS
mcfonts.providers.legacy_unicode.align_unicode_page(sheet)

Align a Unicode page font sheet's characters to the left.

This function is a shortcut for mcfonts.utils.resources.align_font_texture(sheet, (16, 16))().

Parameters:
sheet : PIL.Image.Image

The font sheet, not the individual character.

Returns:

The new font sheet.

Return type:

PIL.Image.Image

mcfonts.providers.legacy_unicode.build_template_for_character(character, template_storage=None)

Build an image of a template codepoint.

Templates are simple white boxes with a codepoint texture inside them. This is intended for use with a "legacy_unicode" provider.

Parameters:
character : str

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

template_storage : dict[int, PIL.Image.Image] | None

Dictionary of integers mapped to the template character PNG images.

  • 0-15 are images of hexadecimal digits.

  • -4 and -6 are "boxes" for 4 and 6 digit-long codepoints, respectively.

Returns:

A PIL.Image.Image of the character template.

Return type:

PIL.Image.Image

mcfonts.providers.legacy_unicode.to_advances(glyphsizes, match_unicode_category=None)

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

Note

This function does not return a new provider.

>>> to_advances(b"\x00\x00\x00...")
{
    "\x00": 0,
    "\x01": 0,
    "\x02": 0,
    "\uBEEF": 15
}
Parameters:
glyphsizes : bytes

The bytes of glyphsizes.bin.

match_unicode_category : list[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.

Returns:

An "advances" dictionary.

Return type:

dict[str, int]


Last update: 2023 November 30