mcfonts.provider

Contains providers -- mappings of character --> glyph.

Note

Reference providers have no class and they are not represented. Reference providers are immediately unpacked in mcfonts.factory functions. It is not possible to create a reference provider.

Submodules

Package Contents

class Glyph

Bases: abc.ABC

The Glyph class represents any visible character glyph.

Glyphs are immutable. To change them, create another with different fields and overwrite the previous glyph.

__bool__() bool

Return True if glyph is not empty.

Return type:

bool

abstractmethod bake() PIL.Image.Image

Return a image rendering of this glyph.

This does not include any right padding.

Return type:

PIL.Image.Image

abstractmethod get_metrics() GlyphMetrics

Return the metrics of this glyph.

Returns:

The metrics.

Return type:

GlyphMetrics

is_empty() bool

Return if glyph is entirely empty.

Returns:

True if height and width bearings are height and width, respectively.

Return type:

bool

abstractmethod show() str

Return a pretty visual representation of this glyph.

It is best to pass this into print().

Returns:

String of glyph representation.

Return type:

str

class HasPriority

Bases: Protocol

Priority indicates a natural ordering to providers.

Providers with a higher priority can overwrite glyphs from lower priorities. A higher number means a higher priority.

Priority is a property of the class, not instances. All instances share the same priority.

classmethod get_priority() int

Return the priority of this type of provider.

Returns:

Priority.

Return type:

int

classmethod set_priority(priority: int) None

Set the priority of this type of provider.

Parameters:
priority : int

New priority.

Return type:

None

DEFAULT_PRIORITY : ClassVar[int] = -1
class Provider[G: mcfonts.glyph.Glyph]

Bases: abc.ABC, collections.abc.MutableMapping[str, G], collections.abc.Container[str]

The Provider base class. All providers subclass this.

At the most basic, providers contain mappings of character --> <G>; specific providers define other properties and provider-specific behavior.

All providers have a ProviderFilter field.

Warning

For most providers, constructor fields do not create glyphs (generally).

Although this may seem counter-intuitive and lengthy, this has the benefit of being able to easily change individual glyphs and fields without needing to "recompile" the entire provider.

It also disconnects providers from I/O (reading files, interpreting them, forming them into glyphs).

Other providers may mutate fields when glyphs are changed - but this is no guarantee. Not all fields will react to changes in the provider's glyphs or other state. For example, fields of a path to an external resource will not update when new glyphs added. Thus, field paths may point to "stale resources" that do not reflect the actual state of the provider currently.

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

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:

Self

delete_filter() None

Delete filter.

Return type:

None

delete_glyphs(glyph: G) None

Delete all characters mapped to an equivalent instance of glyph.

Parameters:
glyph : G

Return type:

None

get_filter() ProviderFilter | None

Return the filter applied to this provider, or None if not set.

Returns:

ProviderFilter, or None if not set.

Return type:

ProviderFilter | None

classmethod get_glyph_type() type[G]
Abstractmethod:

Return type:

type[G]

Return the type of Glyph this provider uses.

Returns:

Type of Glyph.

Return type:

type[G]

classmethod get_pack_versions() tuple[int | None, int | None]
Abstractmethod:

Return type:

tuple[int | None, 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, int | None]

replace_glyphs(original: G, replacement: G) None

Replace all instances of a glyph with another. Replacement glyph is copied.

Parameters:
original : G

The glyph to be replaced.

replacement : G

The new glyph to replace with.

Raises:

TypeError -- If glyph is of incompatible type.

Return type:

None

set_filter(provider_filter: ProviderFilter | None) None

Set filter.

Parameters:
provider_filter : ProviderFilter | None

New filter, or None to unset.

Return type:

None

swap_glyphs(a: str, b: str) None

Swap the glyphs for characters a and b.

If glyphs for a xor b do not exist, then the present character will be deleted and the non-present character set.

If glyphs for a and b do not exist, nothing will happen.

Parameters:
a : str

First character.

b : str

Second character.

Return type:

None

class ProviderFilter

Options in Language --> Font Settings that must be in the given state for the provider to be used.

If None, that option is ignored.

__copy__() ProviderFilter

Return a (deep) copy of this filter.

Returns:

A filter copy that is equal but not identical.

Return type:

ProviderFilter

classmethod from_dict(contents: collections.abc.Mapping[str, Any]) ProviderFilter

Create an instance of ProviderFilter from this dictionary.

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

A dictionary with the individual filter option keys.

Returns:

Instance of ProviderFilter. If a key does not exist, its value is None.

Return type:

ProviderFilter

is_default() bool

Whether this filter is equal to the default state of settings (all disabled).

Returns:

If filter is in a default state.

Return type:

bool

is_enabled_for(game_state: ProviderFilter) bool

Return if this filter should allow its provider to show, when given the state of the game options.

Parameters:
game_state : ProviderFilter

Game options as filter.

Returns:

If filter allows provider to show.

Return type:

bool

is_none() bool

Whether this filter is equal to the default state of settings.

Returns:

If filter is in a None state.

Return type:

bool

merge(other: ProviderFilter) ProviderFilter

Produce a new provider filter merged with another.

Options in other override self, unless they are None.

Parameters:
other : ProviderFilter

Filter that will override.

Returns:

New filter.

Return type:

ProviderFilter

to_dict() dict[str, bool]

Convert to JSON-compatible dictionary.

Filters of None are {}.

Returns:

JSON-compatible dictionary version of this filter.

Return type:

dict[str, bool]

jp : bool | None = None
uniform : bool | None = None
dict_autoset_filter(provider: Provider[Any], base: collections.abc.MutableMapping[str, Any]) None

Modify the provider mapping to add applied filter from the provider.

If the provider's filter is equal to the default, it is not added.

Parameters:
provider : Provider[Any]

Provider.

base : collections.abc.MutableMapping[str, Any]

Dictionary, modified in-place.

Return type:

None

transfer_filters[G1: mcfonts.glyph.Glyph, G2: mcfonts.glyph.Glyph](provider_a: Provider[G1], provider_b: Provider[G2]) None

Copy the filter in provider_a over to provider_b.

This function mutates provider_b in-place and returns None. It does not mutate provider_a.

Parameters:
provider_a : Provider[G1]

Source provider.

provider_b : Provider[G2]

Destination provider.

Return type:

None

transfer_glyphs[P: Provider[Any]](provider_from: P, provider_to: P) None

Deep copy all glyphs in provider_from over to provider_to.

This function mutates provider_to in-place and returns None. It does not mutate provider_from.

Both providers must be of the same type.

Parameters:
provider_from : P

Source provider.

provider_to : P

Destination provider. This is mutated.

Return type:

None