mcfonts.glyph.unihex

The unihex glyph format orginates from GNU Unifont's .hex format.

These glyphs store their data as a simple sequence of bits. All unihex glyphs are 16 pixels in height, so this allows rows and columns of the glyph to be deduced and addressed.

This module uses a bitarray to store and pass this data.

Module Contents

class UnihexGlyph(data: str | bitarray.bitarray)

Bases: mcfonts.glyph.Glyph, mcfonts.serde.opentype.ToTrueTypeGlyph, mcfonts.serde.yaff.ToYaffData, mcfonts.serde.unihex.ToUnihexData

The unihex glyph contains its bits (stored as bitarray).

Initialize.

Parameters:
data : str | bitarray.bitarray

String or bitarray. If str, convert to bitarray.

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

Return a deep copy; bits are implicitly copied.

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

Return type:

UnihexGlyph

__repr__() str

Return UnihexGlyph(width x [/ceil]).

Return type:

str

as_bitstring() str
Return type:

str

bake() PIL.Image.Image

Return a image rendering of this glyph.

This does not include any right padding.

Return type:

PIL.Image.Image

get_bits() bitarray.bitarray

Return the bits.

Return type:

bitarray.bitarray

get_metrics() mcfonts.glyph.GlyphMetrics

Return the metrics of this glyph.

Returns:

The metrics.

Return type:

mcfonts.glyph.GlyphMetrics

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

to_truetype_glyph() fontTools.ttLib.tables._g_l_y_f.Glyph

Process and convert this glyph into rectangle shapes.

Returns:

Generator yielding rectangles.

Return type:

fontTools.ttLib.tables._g_l_y_f.Glyph

to_unihex_data() str

Process and convert this glyph into GNU Unihex data.

Returns:

String of the data.

Return type:

str

to_yaff_data() list[str]
Return type:

list[str]

height : ClassVar[int] = 16
valid_widths : ClassVar[list[int]] = [8, 16, 24, 32]
bits_to_rows(bits: bitarray.bitarray) collections.abc.Generator[bitarray.bitarray]

Convert bits into a yield of bitarrays as its rows.

Parameters:
bits : bitarray.bitarray

The bitarray.

Returns:

Generator that yields rows as bitarray.

Return type:

collections.abc.Generator[bitarray.bitarray]

bytes_to_str(b: bytes, /) str
Parameters:
b : bytes

Return type:

str

calculate_metrics(bits: bitarray.bitarray) mcfonts.glyph.GlyphMetrics

Calculate left, right, bottom, and top metrics.

Parameters:
bits : bitarray.bitarray

Return type:

mcfonts.glyph.GlyphMetrics

data_to_bits(data: str) bitarray.bitarray
Parameters:
data : str

Return type:

bitarray.bitarray

data_to_bools(data: str) collections.abc.Generator[bool]
Parameters:
data : str

Return type:

collections.abc.Generator[bool]

get_width(bits: bitarray.bitarray) int

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

Width is equivalent to len(bits) // 4, which can be shortened to len(bits) >> 2. Since this expects a bitarray, the calculation becomes >> 4.

Parameters:
bits : bitarray.bitarray

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

iterate_bits(bits: bitarray.bitarray) collections.abc.Generator[bool]
Parameters:
bits : bitarray.bitarray

Return type:

collections.abc.Generator[bool]

set_width(bits: bitarray.bitarray, width: int) bitarray.bitarray
Parameters:
bits : bitarray.bitarray

width : int

Return type:

bitarray.bitarray

split_whole(unihex_glyph: str) tuple[str, str]

Return the character and data of a whole unihex string.

Parameters:
unihex_glyph : str

The entire .hex glyph.

Returns:

The character and data.

Raises:

ValueError: If codepoint is invalid.

Return type:

tuple[str, str]

trim_bits(bits: bitarray.bitarray, left: int, right: int) bitarray.bitarray
Parameters:
bits : bitarray.bitarray

left : int

right : int

Return type:

bitarray.bitarray

whole_to_bits(unihex_glyph: str) bitarray.bitarray

Return the data portion of a unihex glyph -- the part after :.

>>> whole_to_bits("0041:0000000018242442427E424242420000")
bitarray('0000000018242442427E424242420000')
Parameters:
unihex_glyph : str

A single .hex glyph.

Returns:

The bit string portion.

Return type:

bitarray.bitarray