mcfonts.utils.unihex

Utilities for dealing with .hex glyphs.

This is often used in conjunction with mcfonts.providers.unihex.UnihexProvider.

Module Contents

Functions

bit_string_to_bytes(bit_string)

Return bit_string but with each set of "bytes" as real bytes.

bit_string_to_image(bit_string)

Given bit_string, return a Image that is a 1:1 representation of the glyph.

bit_string_to_rows(bit_string[, reverse])

Convert bit_string into a yield of strings, with each character in the string being either "1" or "0".

get_bit_string(hex_glyph)

Return the "bit string" portion of a .hex glyph. This is the part after the :.

get_codepoint(hex_glyph)

Return the character that a .hex glyph is associated with.

get_unihex_bearings(bit_string)

Return the two integers of the bearings of an .hex glyph's bit string.

get_width(bit_string)

Return how wide a .hex glyph's bit string is.

mcfonts.utils.unihex.bit_string_to_bytes(bit_string)

Return bit_string but with each set of "bytes" as real bytes.

For example, "00EA" returns "\x00\xEA". This is useful for iterating over the pixels of a bit string, or converting to an image.

>>> bit_string_to_bytes("0000000018242442427E424242420000")
b'\x00\x00\x00\x00\x18$$BB~BBBB\x00\x00'
Parameters:
bit_string : str

The bit string.

Returns:

Bytes of the bit string, with each two-character portion translated to their byte representation.

Return type:

bytes

mcfonts.utils.unihex.bit_string_to_image(bit_string)

Given bit_string, return a Image that is a 1:1 representation of the glyph.

Parameters:
bit_string : str

A bit string.

Returns:

An Image of the bit string.

Return type:

PIL.Image.Image

mcfonts.utils.unihex.bit_string_to_rows(bit_string, reverse=True)

Convert bit_string into a yield of strings, with each character in the string being either "1" or "0".

If reverse (the default), the rows will be flipped vertically.

>>> list(bit_string_to_rows("0000000018242442427E424242420000"))
[
    '00000000',
    '00000000',
    '01000010',
    '01000010',
    '01000010',
    '01000010',
    '01111110',
    '01000010',
    '01000010',
    '00100100',
    '00100100',
    '00011000',
    '00000000',
    '00000000',
    '00000000',
    '00000000'
]
Parameters:
bit_string : str

The bit string.

reverse : bool

Whether to flip the image along the Y-axis. This is to match the normal behavior of iterating over the pixels of a PIL.Image.Image, from the bottom-left to the top-right, row first.

Returns:

Return type:

collections.abc.Iterable[str]

mcfonts.utils.unihex.get_bit_string(hex_glyph)

Return the "bit string" portion of a .hex glyph. This is the part after the :.

>>> get_bit_string("0041:0000000018242442427E424242420000")
'0000000018242442427E424242420000'
Parameters:
hex_glyph : str

A single .hex glyph.

Returns:

The bit string portion.

Return type:

str

mcfonts.utils.unihex.get_codepoint(hex_glyph)

Return the character that a .hex glyph is associated with.

Parameters:
hex_glyph : str

The entire .hex glyph.

Returns:

The character portion of a .hex glyph.

Return type:

str

mcfonts.utils.unihex.get_unihex_bearings(bit_string)

Return the two integers of the bearings of an .hex glyph's bit string.

Bearings are a "padding" from the edge of the canvas to image pixels. Left bearing is the distance from the left edge of the canvas to the most-left pixel data. Right bearing is the distance from the right edge of the canvas to the most-right pixel data.

If return is (0, 0), there's no pixel data, the glyph is all spaces.

This function is similar to mcfonts.utils.image.get_image_bearings().

>>> get_unihex_bearings("0000000018242442427E424242420000")
(1, 7)
Parameters:
bit_string : str

The bit string of a .hex glyph.

Returns:

Left bearing and right bearing. Returns (0, 0) if there's no pixel data.

Return type:

tuple[int, int]

mcfonts.utils.unihex.get_width(bit_string)

Return how wide a .hex glyph's bit string is.

Width is equivalent to len(bit_string) // 4, which can be shortened to len(bit_string) >> 2.

Parameters:
bit_string : str

The bit string portion of a .hex glyph.

Returns:

The width of the bit string. A power of 2 above or equal to 8.

Return type:

int


Last update: 2023 November 30