Skip to content

Support for multiline text and text wrapping in Caption filter #229

@artyomsv

Description

@artyomsv

Description

Currently, the Caption filter in Thumbnailator renders text as a single horizontal line. When providing a string containing newline characters (\n) or a string that exceeds the width of the base image, the text is either rendered on one line or cut off at the image boundaries.

I would like to request support for multiline text rendering and/or automatic text wrapping within the Caption filter.

Research Conducted

Before opening this request, I investigated the current library capabilities:

  • Searched existing GitHub issues and discussions for "multiline" and "text wrap" but found no native implementation or existing roadmap for this feature.
  • Experimented with passing \n characters to the Caption constructor, which resulted in the characters being ignored or rendered as a single-line string.

Proposed Behavior

  • Manual Line Breaks: The Caption filter should recognize \n characters and increment the vertical position for subsequent text based on the FontMetrics of the current font.
  • Automatic Wrapping (Optional but preferred): A way to specify a maximum width (perhaps as a percentage of the image width) where text will automatically wrap to a new line.

Example Use Case

This is particularly useful for generating dynamic image overlays such as:

  1. Long quotes or product descriptions using non-standard .ttf fonts.
  2. Address blocks where data is naturally split across multiple lines.
  3. Meme-style captions or informational overlays where the text length is unpredictable.

Current Workaround

The current workaround involves manually creating a BufferedImage via standard Graphics2D and LineBreakMeasurer, then passing that image into the .watermark() method. This breaks the fluent API chain and adds significant boilerplate code.

Are there any plans to implement this in the Caption class?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions