mcfonts.glyphs#

TODO doc - this - add basic doc for all getters

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#

AnyGlyph

Any glyph, either Bitmap, Space, or Unihex.

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: int, options: dict | None = None) list[str | int] | None#

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 -- How big each pixel should be.

  • options --

    A specialized dictionary that should be derived from an instance of mcfonts.providers.options.OptionsProvider.

    Should be in the form of {field*: value}. For example, an Options dictionary of {"width": {"a": 4}, "spacing": {"a": 2}} should transform into {"width": 4, "spacing": 2}; use mcfonts.providers.options.OptionsProvider.get_options_for_char() for this.

    This is deprecated since v0.6 and will be removed in v0.7.

Returns:

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

export_to_character(font_xml: lxml.etree._Element, character: str, allocated_characters: set[str] | None = None, char_options: dict | None = None) None#

Export this glyph to a character in a font XML.

The font XML is mutated in-place and nothing is returned.

Parameters:
  • font_xml -- The font XML as a lxml.etree._Element.

  • character -- A string of a single character for what Unicode character to export and assign the glyph to.

  • allocated_characters -- A set of all the characters the font_xml already has an allocation for.

  • char_options --

    An optional dictionary of character options. Get this from mcfonts.providers.options.OptionsProvider.get_options_for_char().

    This is deprecated since v0.6 and will be removed in v0.7.

Raises:

mcfonts.exceptions.CharacterPaddingError -- If character is a padding character.

get_ascent() int#

The glyph's ascent.

get_bearings() tuple[int, int]#

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.

get_height() int#

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

This is lazily-loaded.

get_image() PIL.Image.Image#

The image of the glyph.

get_program(char_options: dict | None = None) list[str, int] | None#

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

Parameters:

char_options --

A specialized dictionary that should be derived from an instance of mcfonts.providers.options.OptionsProvider.

This is deprecated since v0.6 and will be removed in v0.7.

get_width() int#

Width of the glyph's image.

This is lazily-loaded.

class mcfonts.glyphs.Glyph#

Bases: abc.ABC

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

abstract construct_program(pixel_size: int, options: dict | None = None) list[str | int] | None#

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 -- How big each pixel should be.

  • options --

    A specialized dictionary that should be derived from an instance of mcfonts.providers.options.OptionsProvider.

    Should be in the form of {field*: value}. For example, an Options dictionary of {"width": {"a": 4}, "spacing": {"a": 2}} should transform into {"width": 4, "spacing": 2}; use mcfonts.providers.options.OptionsProvider.get_options_for_char() for this.

    This is deprecated since v0.6 and will be removed in v0.7.

Returns:

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

abstract export_to_character(font_xml: lxml.etree._Element, character: str, allocated_characters: set[str], char_options: dict | None = None) None#

Export this glyph to a character in a font XML.

The font XML is mutated in-place and nothing is returned.

Parameters:
  • font_xml -- The font XML as a lxml.etree._Element.

  • character -- A string of a single character for what Unicode character to export and assign the glyph to.

  • allocated_characters -- A set of all the characters the font_xml already has an allocation for.

  • char_options --

    An optional dictionary of character options. Get this from mcfonts.providers.options.OptionsProvider.get_options_for_char().

    This is deprecated since v0.6 and will be removed in v0.7.

Raises:

mcfonts.exceptions.CharacterPaddingError -- If character is a padding character.

abstract get_ascent() int#

Ascent of the glyph.

abstract get_height() int#

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

abstract get_program(char_options: dict | None = None) list[str, int] | None#

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

Parameters:

char_options --

A specialized dictionary that should be derived from an instance of mcfonts.providers.options.OptionsProvider.

This is deprecated since v0.6 and will be removed in v0.7.

abstract get_width() int#

Width of the glyph.

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: float, options: dict | None = None) list[str | int] | None#

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 -- How big each pixel should be.

  • options --

    A specialized dictionary that should be derived from an instance of mcfonts.providers.options.OptionsProvider.

    Should be in the form of {field*: value}. For example, an Options dictionary of {"width": {"a": 4}, "spacing": {"a": 2}} should transform into {"width": 4, "spacing": 2}; use mcfonts.providers.options.OptionsProvider.get_options_for_char() for this.

    This is deprecated since v0.6 and will be removed in v0.7.

Returns:

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

export_to_character(font_xml: lxml.etree._Element, character: str, allocated_characters: set[str], char_options: dict | None = None) None#

Export this glyph to a character in a font XML.

The font XML is mutated in-place and nothing is returned.

Parameters:
  • font_xml -- The font XML as a lxml.etree._Element.

  • character -- A string of a single character for what Unicode character to export and assign the glyph to.

  • allocated_characters -- A set of all the characters the font_xml already has an allocation for.

  • char_options --

    An optional dictionary of character options. Get this from mcfonts.providers.options.OptionsProvider.get_options_for_char().

    This is deprecated since v0.6 and will be removed in v0.7.

Raises:

mcfonts.exceptions.CharacterPaddingError -- If character is a padding character.

get_ascent() int#

Ascent of the glyph.

get_height() int#

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

get_program(char_options: dict | None = None) list[str, int] | None#

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

Parameters:

char_options --

A specialized dictionary that should be derived from an instance of mcfonts.providers.options.OptionsProvider.

This is deprecated since v0.6 and will be removed in v0.7.

get_width() int#

Width of the glyph.

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.

TODO more stuff here about what it is, what it does, etc.

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: int, options: dict | None = None) list[str | int] | None#

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 -- How big each pixel should be.

  • options --

    A specialized dictionary that should be derived from an instance of mcfonts.providers.options.OptionsProvider.

    Should be in the form of {field*: value}. For example, an Options dictionary of {"width": {"a": 4}, "spacing": {"a": 2}} should transform into {"width": 4, "spacing": 2}; use mcfonts.providers.options.OptionsProvider.get_options_for_char() for this.

    This is deprecated since v0.6 and will be removed in v0.7.

Returns:

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

export_to_character(font_xml: lxml.etree._Element, character: str, allocated_characters: set[str], char_options: dict | None = None) None#

Export this glyph to a character in a font XML.

The font XML is mutated in-place and nothing is returned.

Parameters:
  • font_xml -- The font XML as a lxml.etree._Element.

  • character -- A string of a single character for what Unicode character to export and assign the glyph to.

  • allocated_characters -- A set of all the characters the font_xml already has an allocation for.

  • char_options --

    An optional dictionary of character options. Get this from mcfonts.providers.options.OptionsProvider.get_options_for_char().

    This is deprecated since v0.6 and will be removed in v0.7.

Raises:

mcfonts.exceptions.CharacterPaddingError -- If character is a padding character.

get_ascent() int#

Ascent of the glyph.

get_bearings() tuple[int, int]#

See mcfonts.utils.unihex.get_unihex_bearings().

get_bit_string() str#

The bit string is the portion after the codepoint:.

get_height() int#

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

get_image() PIL.Image.Image#

The image of the glyph.

This is lazily-loaded.

get_program(char_options: dict | None = None) list[str, int] | None#

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

Parameters:

char_options --

A specialized dictionary that should be derived from an instance of mcfonts.providers.options.OptionsProvider.

This is deprecated since v0.6 and will be removed in v0.7.

get_size_override() tuple[int, int]#

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

get_width() int#

Width of the glyph.

mcfonts.glyphs.AnyGlyph#

Any glyph, either Bitmap, Space, or Unihex.

However, the method names will obviously vary. Check instances first. All of these inherit from the metaclass Glyph.