mcfonts.serde.opentype

See https://learn.microsoft.com/en-us/typography/opentype/spec/.

Important

Not all OpenType features/tables are supported.

Module Contents

class Direction(*args, **kwds)

Bases: enum.Enum

Enumeration of four cardinal directions with associated movement deltas.

check_backward(point: Point, f: WithinFunc) tuple[Point, Direction] | None

Check if moving backward (180 degree turn) is possible.

Parameters:
point : Point

Current position.

f : WithinFunc

Predicate for inside region.

Returns:

New position and opposite direction if possible, otherwise None.

Return type:

tuple[Point, Direction] | None

check_forward(point: Point, f: WithinFunc) tuple[Point, Direction] | None

Check if moving forward is possible.

Parameters:
point : Point

Current position.

f : WithinFunc

Predicate for inside region.

Returns:

New position and same direction if possible, otherwise None.

Return type:

tuple[Point, Direction] | None

check_left(point: Point, f: WithinFunc, cut_corners: bool) tuple[Point, Direction] | None

Check if a left turn (relative to current direction) is possible.

Parameters:
point : Point

Current position (x, y).

f : WithinFunc

Predicate indicating if a point is inside the region.

cut_corners : bool

If True, allow diagonal moves when the direct side is blocked.

Returns:

New position and direction if turn possible, otherwise None.

Return type:

tuple[Point, Direction] | None

check_right(point: Point, f: WithinFunc) tuple[Point, Direction] | None

Check if a right turn is possible.

Parameters:
point : Point

Current position.

f : WithinFunc

Predicate for inside region.

Returns:

New position and direction if turn possible, otherwise None.

Return type:

tuple[Point, Direction] | None

EAST = (1, 0)
NORTH
SOUTH = (0, 1)
WEST
class MetadataOptions
to_name() Name

Generate a OpenType NAME table from metadata.

Returns:

NAME table.

Return type:

Name

author : str | None = None
include_credits : bool | None = None
license : str | None = None
name : str | None = None
class OpenTypeSaver

Bases: Protocol

Protocol for saving a provider to the OpenType format.

opentype_save(tables: OpenTypeTables) None

Save provider into an OpenType font by modifying its tables.

Parameters:
tables : OpenTypeTables

Table storage.

Return type:

None

class OpenTypeTables

A limited collection of OpenType tables.

apply_to_builder(builder: fontTools.fontBuilder.FontBuilder) None

Apply these tables to a FontBuilder.

Parameters:
builder : fontTools.fontBuilder.FontBuilder

Return type:

None

prefill(metadata: MetadataOptions) None
Parameters:
metadata : MetadataOptions

Return type:

None

cmap : Cmap
glyf : Glyf
glyph_order : GlyphOrder = []
head : Head
hhea : Hhea
hmtx : Hmtx
name : Name
os_2 : Os_2
post : Post
class ToTrueTypeGlyph

Bases: Protocol

Protocol for turning a glyph into a OpenType GLYF glyph.

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

contours_to_glyph(contours: collections.abc.Iterable[Contour]) fontTools.ttLib.tables._g_l_y_f.Glyph
Parameters:
contours : collections.abc.Iterable[Contour]

Return type:

fontTools.ttLib.tables._g_l_y_f.Glyph

find_all_contours(f: WithinFunc, xmin: int, xmax: int, ymin: int, ymax: int) collections.abc.Generator[Contour]

Find all closed contours in a binary region defined by predicate f.

Scans the bounding box pixel by pixel. Interior regions (holes) are detected as non-border-touching background components and traced in reverse direction (CCW); exterior regions (solids) are CW.

Parameters:
f : WithinFunc

Predicate returning True for foreground pixels.

xmin : int

Inclusive minimum x.

xmax : int

Exclusive maximum x.

ymin : int

Inclusive minimum y.

ymax : int

Exclusive maximum y.

Returns:

Generator yielding contours.

Raises:

ValueError -- If bounds are negative.

Return type:

collections.abc.Generator[Contour]

flood_fill4(filled: list[bytearray], point: Point, f: WithinFunc, xmin: int, xmax: int, ymin: int, ymax: int) bool

Perform 4-connected flood fill optimized with span filling.

Marks a region and detects if it touches the image border.

Parameters:
filled : list[bytearray]

2D bytearray grid for marking visited/work pixels.

point : Point

Seed point inside the region.

f : WithinFunc

Predicate defining the region (True if fillable).

xmin : int

Minimum x bound (inclusive).

xmax : int

Maximum x bound (exclusive).

ymin : int

Minimum y bound (inclusive).

ymax : int

Maximum y bound (exclusive).

Returns:

True if the filled region touches the border.

Return type:

bool

flood_fill8(filled: list[bytearray], point: Point, f: WithinFunc, xmin: int, xmax: int, ymin: int, ymax: int) None

Perform simple 8-connected flood fill.

Parameters:
filled : list[bytearray]

2D bytearray grid for marking.

point : Point

Seed point.

f : WithinFunc

Region predicate.

xmin : int

Inclusive min x.

xmax : int

Exclusive max x.

ymin : int

Inclusive min y.

ymax : int

Exclusive max y.

Return type:

None

get_glyph_name(character: str) tuple[int, str]

Return tuple of codepoint and suitable glyph name (currently u<codepoint:04X> as in uA46F).

Parameters:
character : str

A single character.

Returns:

Suitable glyph name.

Return type:

tuple[int, str]

save_font(font: mcfonts.font.MinecraftFont, metadata: MetadataOptions | None = None) fontTools.ttLib.TTFont
Parameters:
font : mcfonts.font.MinecraftFont

metadata : MetadataOptions | None

Return type:

fontTools.ttLib.TTFont

set_notdef(tables: OpenTypeTables) None
Parameters:
tables : OpenTypeTables

Return type:

None

to_truetype_glyph(glyph: mcfonts.glyph.Glyph, func: WithinFunc) fontTools.ttLib.tables._g_l_y_f.Glyph

Generate a glyph, given that glyph and a callable determining how to navigate it.

If the glyph is empty, an empty TTGlyph is returned.

Parameters:
glyph : mcfonts.glyph.Glyph

The glyph to work on.

func : WithinFunc

Callable that returns whether a position has data.

Return type:

fontTools.ttLib.tables._g_l_y_f.Glyph

trace(start: Point, f: WithinFunc, direction: Direction = Direction.EAST, cut_corners: bool = True) Contour

Trace a single closed contour starting from a given point.

The algorithm follows a boundary by preferring left turns, then forward, then right turns. It first normalizes the starting point to the top-left most position on the boundary.

Parameters:
start : Point

Starting point on the boundary.

f : WithinFunc

Predicate that returns True for points on the boundary side used.

direction : Direction

Initial tracing direction.

cut_corners : bool

Allow diagonal moves in left/right turns.

Returns:

List of points forming the closed contour (first and last point identical only implicitly).

Return type:

Contour

transform_glyph(glyph: fontTools.ttLib.tables._g_l_y_f.Glyph, transform: fontTools.misc.transform.Transform) fontTools.ttLib.tables._g_l_y_f.Glyph
Parameters:
glyph : fontTools.ttLib.tables._g_l_y_f.Glyph

transform : fontTools.misc.transform.Transform

Return type:

fontTools.ttLib.tables._g_l_y_f.Glyph

ACHVENDID_DEFAULT : str = 'pMCF'
type Cmap = dict[int, GlyphName]
type Contour = list[Point]
EM_SIZE : int = 1024
GRID_UNVISITED : Final[int] = 0
GRID_VISITED : Final[int] = 2
GRID_WORK : Final[int] = 1
type Glyf = dict[GlyphName, TTGlyph]
type GlyphName = str
type GlyphOrder = list[GlyphName]
type Head = dict[str, int]
type Hhea = dict[str, int]
type Hmtx = dict[GlyphName, tuple[int, int]]
NOTDEF_GLYPH : Final[fontTools.ttLib.tables._g_l_y_f.Glyph]
NOTDEF_NAME : str = '.notdef'
NOTDEF_SCALE : int = 128
type Name = dict[int | str, str]
type Os_2 = dict[str, int | str | dict[str, int]]
type Point = tuple[int, int]
type Post = dict[str, int | float]
type WithinFunc = collections.abc.Callable[[Point], bool]