Skip to content

Text

TextClipMaker

Bases: BaseClipMaker[BaseTextAsset]

A clip maker for text assets.

The process of text clip creation involves:

  1. Font and Text Preparation:

    • Loads system fonts using the specified font family
    • Wraps text to fit within video width
    • Calculates text dimensions
  2. Shadow Creation (if enabled):

    • Generates shadow layer with specified angle and distance
    • Applies blur effect and opacity
    • Handles shadow color and positioning
  3. Text Rendering:

    • Creates text layer with specified font and color
    • Applies stroke/outline effects
    • Handles text alignment and line height
    • Supports RGBA colors with transparency
  4. Image Composition:

    • Combines shadow and text layers if shadow enabled
    • Crops image to text boundaries
    • Creates temporary PNG file for MoviePy
  5. Clip Creation:

    • Converts rendered image to MoviePy clip
    • Sets position based on text parameters
    • Applies specified duration

Examples:

# Create a basic text clip
maker = TextClipMaker(duration=5.0, video_resolution=(1920, 1080))
clip = maker.make_clip(text_asset)

# Create clip with shadow
text_asset.params.has_shadow = True
clip = maker.make_clip(text_asset)  # Will add shadow effect

# Create clip with custom position
text_asset.params.position = AbsolutePosition(x=100, y=50)
clip = maker.make_clip(text_asset)  # Will position at x=100, y=50

# Create clip with custom font size
text_asset.params.font_size = 48
clip = maker.make_clip(text_asset)  # Will render text with font size 48

SystemFont dataclass

SystemFont(path: str)

System font representation.

path instance-attribute

path: str

The path to the font file in the system.

name property

name: str

Get the font name.

slug property

slug: str

Get the slugified font name.

matches

matches(name: str) -> bool

Check if the font name matches the given name.

Source code in src/mosaico/clip_makers/text.py
def matches(self, name: str) -> bool:
    """
    Check if the font name matches the given name.
    """
    return self.name == name or self.slug == _slugify_font_name(name)

load

load(size: float) -> FreeTypeFont

Load the font.

Source code in src/mosaico/clip_makers/text.py
def load(self, size: float) -> ImageFont.FreeTypeFont:
    """
    Load the font.
    """
    return ImageFont.truetype(self.path, size)