mcfonts.glyphs

Various glyph formats that can be present in a font.

Each vary in their storage formats and methods of manipulating contained data.

Module Contents

Classes

BitmapGlyph

A bitmap glyph.

Glyph

The base Glyph class. Should never be called or instantiated.

SpaceGlyph

A space glyph.

UnihexGlyph

A unihex glyph.

Attributes

BITMAP_EXPORT_PIXEL_THRESHOLD

SPACE_EXPORT_MAX_ADVANCE

UNIHEX_EXPORT_PIXEL_ON

mcfonts.glyphs.BITMAP_EXPORT_PIXEL_THRESHOLD = 180
class mcfonts.glyphs.BitmapGlyph

Bases: Glyph

A bitmap glyph.

Must contain an image and ascent. Height, ascent, and bearings are optional. Bearings and width are lazily-loaded.

ascent : int

Required ascent.

bearings : tuple[int, int] | None

Optional padding from the edges of the canvas.

height : int | None

Optional height. Lazy default is 8 if None.

image : PIL.Image.Image

Required image.

program : list[str | int] | None

Optional Type 2 charstring program of the glyph.

width : int | None

Optional width. Lazy default is self.image.width.

construct_program(pixel_size)

Create a Type 2 charstring program.

If the glyph is empty or has no white pixels, None will be returned. Otherwise, a list (the program) will be returned.

Warning

These charstrings are not optimized or checked for overlaps. In FontForge, use Element -> Overlap -> Remove Overlap manually afterward.

Parameters:
pixel_size : int

How big each pixel should be.

Returns:

A list of strings of the glyph's program, or None if there was no pixel data in glyph.

Return type:

list[str | int] | None

get_ascent()

The glyph's ascent.

Return type:

int

get_bearings()

From mcfonts.utils.resources.get_image_bearings():

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.

This is lazily-loaded.

Return type:

tuple[int, int]

get_height()

The height of the glyph; this is not necessarily the glyph's image width.

This is lazily-loaded.

Return type:

int

get_image()

The image of the glyph.

Return type:

PIL.Image.Image

get_program()

A Type 2 charstring program. This is used in exporting and can be cached for better performance.

Return type:

list[str | int] | None

get_width()

Width of the glyph's image.

This is lazily-loaded.

Return type:

int

class mcfonts.glyphs.Glyph

Bases: abc.ABC

The base Glyph class. Should never be called or instantiated.

abstract construct_program(pixel_size)

Create a Type 2 charstring program.

If the glyph is empty or has no white pixels, None will be returned. Otherwise, a list (the program) will be returned.

Warning

These charstrings are not optimized or checked for overlaps. In FontForge, use Element -> Overlap -> Remove Overlap manually afterward.

Parameters:
pixel_size : int

How big each pixel should be.

Returns:

A list of strings of the glyph's program, or None if there was no pixel data in glyph.

Return type:

list[str | int] | None

abstract get_ascent()

Ascent of the glyph.

Return type:

int

abstract get_height()

Height of the glyph. This is not always equal to the glyph's image, if applicable.

Return type:

int

abstract get_program()

A Type 2 charstring program. This is used in exporting and can be cached for better performance.

Return type:

list[str | int] | None

abstract get_width()

Width of the glyph.

Return type:

int

mcfonts.glyphs.SPACE_EXPORT_MAX_ADVANCE = 12500
class mcfonts.glyphs.SpaceGlyph

Bases: Glyph

A space glyph.

Contans one required field, the glyph's width.

program : list[str | int] | None

Optional Type 2 charstring program of the glyph.

width : int

The width of the glyph.

construct_program(pixel_size)

Create a Type 2 charstring program.

If the glyph is empty or has no white pixels, None will be returned. Otherwise, a list (the program) will be returned.

Warning

These charstrings are not optimized or checked for overlaps. In FontForge, use Element -> Overlap -> Remove Overlap manually afterward.

Parameters:
pixel_size : float

How big each pixel should be.

Returns:

A list of strings of the glyph's program, or None if there was no pixel data in glyph.

Return type:

list[str | int] | None

get_ascent()

Ascent of the glyph.

Return type:

int

get_height()

Height of the glyph. This is not always equal to the glyph's image, if applicable.

Return type:

int

get_program()

A Type 2 charstring program. This is used in exporting and can be cached for better performance.

Return type:

list[str | int] | None

get_width()

Width of the glyph.

Return type:

int

mcfonts.glyphs.UNIHEX_EXPORT_PIXEL_ON = '1'
class mcfonts.glyphs.UnihexGlyph

Bases: Glyph

A unihex glyph.

Must contain a bit string. Width, image, and bearings are optional. Width, image, and bearings are lazily-loaded.

Unihex glyphs are always 16 pixels tall.

bearings : tuple[int, int] | None

An optional tuple of the glyph's bearings, comparable to a BitmapGlyph.

bit_string : str

The bit string; does not include the codepoint: starting portion.

image : PIL.Image.Image | None

An optional image of the glyph, comparable to a BitmapGlyph. If possible, try not to use this.

program : list[str | int] | None

Optional Type 2 charstring program of the glyph.

size_override : tuple[int, int] | None

An optional tuple of the glyph's size override.

The first value is the starting column, and the second value is the ending column. When exporting, data outside of these columns will be discarded. If this is None, this is lazily-calculated to be (0, self.get_width()).

width : int | None

The width of the glyph. Equal to len(self.bit_string) // 4.

construct_program(pixel_size)

Create a Type 2 charstring program.

If the glyph is empty or has no white pixels, None will be returned. Otherwise, a list (the program) will be returned.

Warning

These charstrings are not optimized or checked for overlaps. In FontForge, use Element -> Overlap -> Remove Overlap manually afterward.

Parameters:
pixel_size : int

How big each pixel should be.

Returns:

A list of strings of the glyph's program, or None if there was no pixel data in glyph.

Return type:

list[str | int] | None

get_ascent()

Ascent of the glyph.

Return type:

int

get_bearings()

See mcfonts.utils.unihex.get_unihex_bearings().

Return type:

tuple[int, int]

get_bit_string()

The bit string is the portion after the codepoint:.

Return type:

str

get_height()

Height of the glyph. This is not always equal to the glyph's image, if applicable.

Return type:

int

get_image()

The image of the glyph.

This is lazily-loaded.

Return type:

PIL.Image.Image

get_program()

A Type 2 charstring program. This is used in exporting and can be cached for better performance.

Return type:

list[str | int] | None

get_size_override()

Size override is a tuple of 2 integers of the starting and ending columns for a glyph.

Return type:

tuple[int, int]

get_width()

Width of the glyph.

Return type:

int


Last update: 2023 November 30