mcfonts.compacting

Functions for compacting glyphs.

Compacting is the process of taking every glyph provided by a Minecraft font provider and storing it in as little space as possible in a bitmap image.

This means:

  1. Finding the largest effective dimensions out of all the characters,

  2. Expanding the dimensions of all other character textures to fit those new dimensions,

  3. Creating a new texture that will fit all characters in those dimensions,

  4. Sequencing them one-after-another while ignoring characters with a blank texture,

  5. Creating a new charlist that corresponds with the compacted texture.

This contains functions for compacting any provider(s) into individual textures (one for each provider), and for compacting a list of providers into a single, cohesive texture.

Module Contents

Functions

compact_glyphs(glyphs[, chars_in_row, cell_size, ...])

Compact a list of glyphs so that they take up a minimum amount of space in an image.

compact_images(images[, chars_in_row, cell_size, ...])

Similar to compact_glyphs(), except that it works on plain Image instead.

mcfonts.compacting.compact_glyphs(glyphs, chars_in_row=0, cell_size=(0, 0), square_cells=True)

Compact a list of glyphs so that they take up a minimum amount of space in an image.

Given a list of glyphs, return an image wherein all glyphs are compacted into a single sheet, with the number of glyphs in one row matching chars_in_row (excluding exceptions).

Parameters:
glyphs : list[mcfonts.glyphs.BitmapGlyph | mcfonts.glyphs.UnihexGlyph]

A list of lists of [glyphs (PIL.Image.Image), their height, and their ascent]. Baseline can be calculated from this: \(height - ascent\).

chars_in_row : int

How many characters to fit inside each row of the resulting sheet. Only positive values will set fixed rows. If this is negative or 0, this will be set so that the resulting sheet is square. By default, this is 0 (a square).

cell_size : tuple[int, int]

What size to make each glyph cell. If this is (0, 0), this will be set to the largest dimensions of every glyph in glyphs.

square_cells : bool

If True, each glyph's width will equal its height. This is based on whichever number is largest. If False, each glyph's width will be unrelated to its height.

Returns:

A tuple containing a modified resource with the least amount of padding between glyphs, and a tuple of the size of each glyph cell.

Return type:

tuple[PIL.Image.Image, tuple[int, int]]

mcfonts.compacting.compact_images(images, chars_in_row=0, cell_size=(0, 0), square_cells=True, include_empty_images=True)

Similar to compact_glyphs(), except that it works on plain Image instead.

Parameters:
images : list[PIL.Image.Image | None]

chars_in_row : int

cell_size : tuple[int, int]

square_cells : bool

include_empty_images : bool

Return type:

tuple[PIL.Image.Image, tuple[int, int]]


Last update: 2023 November 30