JiwaAI
Blog
โ†All posts
engineering
image-generation
content-generation

When AI Text Escapes the Frame โ€” Solving Overlay Overflow in Generated Images

Jiwa AI Teamยท

The Text That Wouldn't Fit

Imagine generating a beautiful product photo with an AI-composed scene โ€” warm lighting, perfect composition, a model enjoying your brand's macaroni. Then the overlay text reads "WEEKEND HANGOUT YAN" with the rest chopped off at the edge. Not exactly the professional Instagram content your brand was hoping for.

This was happening because our text overlay system underestimated how wide bold uppercase characters actually are. The system would calculate that a line of text should fit, wrap it accordingly, and then render it โ€” only for the actual pixels to spill past the image boundary.

Why Bold Uppercase Breaks the Math

Text rendering is deceptively tricky. A lowercase "i" is much narrower than an uppercase "W," and bold weights push every character wider still. Our system estimated character width as roughly 55% of the font size โ€” a reasonable approximation for mixed-case body text, but dangerously optimistic for bold uppercase headlines.

Since every overlay caption gets uppercased for visual impact, every single character was wider than the estimate predicted. Short titles squeaked by. Longer Indonesian phrases like "Weekend Hangout Yang Sempurna" didn't.

The fix required three changes working in concert. First, we increased the character width factor to 65% โ€” a much better fit for bold uppercase sans-serif glyphs. Second, we added automatic font scaling: if the longest word in the caption still can't fit within the text area even after wrapping, the system progressively shrinks the font size until it does, with a minimum floor to keep things readable. Third, we added a safety net at the SVG rendering level that compresses letter spacing to fit within the allocated width, catching any remaining edge cases.

Giving Text More Room to Breathe

We also realized we were being unnecessarily conservative with horizontal space. The default text area used only 65% of the image width, leaving a third of the frame unused. For bottom-left positioned text โ€” the natural Instagram look โ€” there's no reason to reserve that much right margin. We widened the text area to 80% of the image, giving captions significantly more room while still maintaining clean margins.

Small Math, Big Impact

This is the kind of bug that erodes trust invisibly. Every overflowing caption is a post that looks unfinished, a brand that looks unprofessional. The fix touches fewer than twenty lines of code, but it affects every single image the platform generates. Sometimes the highest-impact engineering work is making sure the fundamentals are pixel-perfect.