Skip to content

ImageFont

from blanket import ImageFont loads fonts for ImageDraw. Outline fonts use Rust's ab_glyph rasterizer and rustybuzz OpenType shaper; FreeType and Pillow are not runtime dependencies.

from blanket import Image, ImageDraw, ImageFont

font = ImageFont.truetype("example.ttf", size=32)
image = Image.new("RGB", (320, 100), "white")
draw = ImageDraw.Draw(image)
draw.text((160, 50), "Blanket", font=font, anchor="mm", fill="black")
image.save("text.png")

truetype(font, size=10, index=0, encoding="", layout_engine=None) accepts a filesystem path or a binary stream containing TrueType or OpenType data, including CFF outlines. index selects a face in a font collection. Paths are opened directly; system font discovery is not performed. Sizes are pixels per em and may be fractional. font_variant(size=...) reuses the loaded data to create another font; getname() returns family and style names.

load_default(size=None) exposes the same built-in 5 by 7 bitmap fallback used by ImageDraw. It scales in whole multiples of seven, substitutes uppercase glyphs for lowercase, and uses ? for unavailable characters. load(path) reads legacy PILfont metrics and a companion .png, .gif, or binary .pbm atlas with the same basename. Loaded bitmap fonts use Latin-1 glyphs and unshaped left-to-right text. The built-in fallback does not provide Unicode shaping.

Outline fonts support Unicode, script runs, bidirectional text, combining marks, ligatures, kerning, OpenType features (such as ["-kern", "-liga"]), and BCP 47 language tags. direction accepts "ltr", "rtl", or basic top-to-bottom "ttb" layout. Without a direction, Unicode text determines the paragraph direction. Both Layout.BASIC and Layout.RAQM select Blanket's native shaper; the latter does not require the system RAQM library. Glyph availability depends on the font; missing glyphs use its .notdef, with no automatic font fallback.

getlength(text, ...) returns the shaped advance in pixels, including kerning. getbbox(text, ..., anchor=None, stroke_width=0) returns bounds relative to the anchor. getmetrics() returns ascent and positive descent in pixels. Font methods handle single lines; use ImageDraw for newline-separated text. Font sizes, advances, masks, and hinting can differ from Pillow/FreeType.

Horizontal anchors combine l/m/r with a (ascender), t (ink top), m (metrics middle), s (baseline), b (ink bottom), or d (descender). The default is la. Vertical anchors combine l/m/r/s with t/m/b, defaulting to lt. getmask2 returns an 8-bit coverage mask and its anchor offset; getmask returns only the mask. mode="1" thresholds coverage. Strokes expand masks using a rounded-up integer radius. Fractional drawing origins shift outline rasterization through start.

ImageDraw accepts these fonts through font= or draw.font, with existing left/center/right multiline alignment and drawing-mode restrictions. Variable font axis selection, embedded-color glyphs, automatic font fallback, and vertical multiline text are unsupported. Masks are limited to 100 million pixels and strokes to 1024 pixels. MAX_STRING_LENGTH defaults to one million characters; set it to None to disable the text length limit.

Reference

ImageFont

Load bitmap and TrueType/OpenType fonts without FreeType or Pillow.

Outline fonts use ab_glyph coverage masks and rustybuzz OpenType shaping. Glyph availability depends on the selected font; system fallback is not used.

Layout

Bases: IntEnum

Layout engines. Both values use Blanket's native OpenType shaper.

ImageFont

ImageFont(size: float | None = None)

Bitmap font returned by :func:load and :func:load_default.

The built-in fallback retains the fixed-width 5 by 7 glyphs used by ImageDraw. Loaded PIL fonts contain up to 256 Latin-1 glyphs.

getmetrics

getmetrics() -> tuple[int, int]

Return the ascent and positive descent in pixels.

getlength

getlength(text: str | bytes, mode: str = '', direction: str | None = None, features: Sequence[str] | None = None, language: str | None = None) -> float

Return the shaped advance in pixels, including kerning.

direction accepts ltr, rtl, or ttb for outline fonts. OpenType feature strings include kern, -kern, and liga=0. language is a BCP 47 language tag. Bitmap fonts accept only left-to-right layout. The optional mode is accepted for Pillow compatibility.

getbbox

getbbox(text: str | bytes, mode: str = '', direction: str | None = None, features: Sequence[str] | None = None, language: str | None = None, stroke_width: float = 0, anchor: str | None = None) -> tuple[int, int, int, int]

Return anchored bounds, expanded by the rounded-up stroke width.

Horizontal anchors combine l, m, or r with a (ascender), t (ink top), m (metrics middle), s (baseline), b (ink bottom), or d (descender). Vertical anchors use l/m/r/s and t/m/b.

getmask2

getmask2(text: str | bytes, mode: str = 'L', direction: str | None = None, features: Sequence[str] | None = None, language: str | None = None, stroke_width: float = 0, anchor: str | None = None, ink: int = 0, start: tuple[float, float] | None = None, *, stroke_filled: bool = True) -> tuple[BitmapMask, tuple[int, int]]

Return a coverage mask and its offset relative to the text anchor.

mode="1" thresholds coverage; L preserves antialiasing. Strokes expand the filled mask. start supplies fractional pixel offsets for outline fonts. ink and stroke_filled are compatibility arguments.

getmask

getmask(text: str | bytes, mode: str = 'L', direction: str | None = None, features: Sequence[str] | None = None, language: str | None = None, stroke_width: float = 0, anchor: str | None = None) -> BitmapMask

Return the font coverage mask; see :meth:getmask2 for options.

TrueTypeFont

TrueTypeFont(font: str | bytes | PathLike[str] | BinaryIO, size: float = 10, index: int = 0, encoding: str = '', layout_engine: Layout | int | None = None)

Bases: ImageFont

A TrueType/OpenType face rasterized and shaped in Rust.

Fonts can be filesystem paths or binary file objects. Font collections select a face with index. Sizes use pixels per em; rasterization and hinting differ from Pillow/FreeType. Missing glyphs use the font's .notdef.

getname

getname() -> tuple[str, str]

Return the font family and style names when present.

getlength

getlength(text: str | bytes, mode: str = '', direction: str | None = None, features: Sequence[str] | None = None, language: str | None = None) -> float

Return the shaped advance without constructing glyph outlines.

font_variant

font_variant(font: str | bytes | PathLike[str] | BinaryIO | None = None, size: float | None = None, index: int | None = None, encoding: str | None = None, layout_engine: Layout | int | None = None) -> TrueTypeFont

Create another face, reusing the loaded bytes unless font is set.

truetype

truetype(font: str | bytes | PathLike[str] | BinaryIO, size: float = 10, index: int = 0, encoding: str = '', layout_engine: Layout | int | None = None) -> TrueTypeFont

Load a .ttf/.otf file or a collection face from a path or binary stream.

The path is used directly; system font discovery is not performed. Both Layout values select the native shaper and require no system RAQM library.

load_default

load_default(size: float | None = None) -> ImageFont

Return the built-in bitmap fallback, optionally scaled in multiples of seven.

load

load(filename: str | bytes | PathLike[str]) -> ImageFont

Load a legacy PIL bitmap font and its PNG, GIF, or binary PBM atlas.

The atlas shares the font's basename. Glyphs use Latin-1 encoding. Invalid or truncated files raise OSError; the font requires no Pillow installation.