Algorithm for fitting text into an irregular shape
Dear forum,
I am attempting to write a script for a company that prints hoodies for school leavers. Currently, they just paste the list of names into the number box and then adjust the text manually moving things around, adjusting font size, line spacing, kerning, manually adding in spaces or line breaks until it looks ok, but it is quite difficult and time-consuming, and annoying!

The trickiest part is filling an irregular shape (a text frame) with names.
At the moment, they can be in three variants:
- First name only
- First name and initial
- First name and surname

In fact, I think, there will be needed two algorithms: one for 1 and 2 and another for 3.
The requirements are just to fit the children’s names in the outline of the “23” (for this year) and look neat and correct. All names need to be the same size as each other (no names bigger than others). Initial/surname would need to not be split from the first name by a line break. The shape should be filled with 1 or 2 times the name of each child (1 or 2 times depending on if we need to fill up more space when there aren’t many children, for instance).
The text frame is converted from text with the ‘create outlines’ command and adjusted manually. Now they want “23”, “24”, but in the future, they might need any word, like “Ski Trip” or “PE”.
My customers have looked on InDesign forums and other design forums and googled everything but they couldn’t find any software that will do this automatically for them with good results, or a way to easily do this in InDesign.
They could find wordcloud type software but often that makes lots of repetition and also makes some words bigger than others, which they don’t want as they don’t want one child to appear more ‘important’ than others, they all need to be the same size.
I am experimenting with this trying to find the right approach. Here on my site, I’m writing my thoughts aloud posting code snippets. (They’re constantly changing so I want to update them on the fly).
I’ve got a feeling that I’m reinventing the wheel and similar algorithms have been already developed, so any pointers would be greatly appreciated. I googled for this info, but couldn’t find what I need.


