Clipping Images in PDF: Now with Text

Clipping Images in PDF: Now with Text

I have a fondness for those “Greetings From…” postcards that show some of the touristic highlights in the text of the name of the place.  So I thought I’d make my own using my own pictures, and use PDF’s ability to use text as a clipping path to generate that effect. Text, but using what font? I found a bunch of likely fonts on my system that I thought might work. And then I tested them:

Images clipped in text

I knew that I wanted a thick font that would allow the images to show through. But the real test was scaling the text using that font to fill the given area, and seeing for myself which fonts worked best. So these were my test fonts:

Using that list, I divided my letter-sized page into two columns with the rows based on the number of fonts I had. Then, I calculated how big the font would have to be to fill that cell with my text:

The actual calculation is basically figuring out the smaller scale factor for stretching the font vertically or horizontally to the size of the fit rectangle.

And while we’re at it, we position horizontal offset so that the text would be centered in the fit rectangle if the vertical limit comes into play.

After that, we calculate the bounding boxes for each letter of the phrase using that font and the matrix we just calculated:

At the same time, we are going to generate the clip for each letter.

Then we are going to take all of those boxes to place images and then we are going to clip them. Add  each of them to a Form XObject, and put all of those Form elements into their own Form:

The images were specified from the command-line. Here we are rotating through each of them so that the same image isn’t used for each letter of text.

Placing the image is slightly different from what I’ve done in the past. Instead of trying to fit all of the image in a given Rect, what we are after is to completely fill the rectangle even if it means that some of the image falls outside of the rectangle. So we take the larger scale factor rather than the smaller.

Lastly, we outline the images we clipped:

Text is typically only filled rather than stroked. But, it is certainly legit to stroke large titular text to make it stand out against a busy background; as will be done in the next image… which I am pulling out now, already pre-prepared Julia Child style:

Tada!

Greetings From Chicago

Feel free to try this with your own pictures.

The full program is here.

Leave a Reply

Your email address will not be published. Required fields are marked *