Get width of composited text (onto image)

I'm compositing labels onto a chart, and I'd like to place a label right after the previous label

Example, right now, I'm spacing out the labels using a hardcoded distance

but as you can see, it would be nice to draw the 'Resolved' label closer to the 'Open' label, is there a way to get the width of composited text?

Welcome to the murky world of font metrics. :slight_smile:

Where are you doing this? Which font?

You probably aren’t doing this in HTML 5 canvas but if you were there’s a method for discerning the width of text as it would be rendered.

But in most apps you can’t do this. You’d have to find and process font metrics for whichever font you’re using.

However, there is a very naive method: Pretend the font has fixed width characters and count them, multiplying the let’s-pretend width by the number of characters. (Then add a few characters’ width for safety.) Yeuch!

I'm doing this in Keyboard Maestro using "composite text onto image" action. Hence my reasoning for coming here to ask :slight_smile:

FWIW, I know font metrics, CoreText is like a second language to me.

The question is a Keyboard Maestro question.

I am generating those images using Keyboard Maestro's composite text on image action, I was just wondering if there is a way to composite onto, say, a blank canvas, and clip / crop to the bounds of the non-alpha, and measure the result, within keyboard maestro. I know I can do it using ImageMagik or other utilities, but I thought maybe there is a built-in way to do it in KM that I'm missing.

1 Like

Found my answer.

The answer is to 'composite' the text into a temporary canvas (made of a transparent color) and use KM's 'trim' image to crop off the all the non-opaque pixels, then use 'get image width' to get the width

Here it is
Width of label.kmmacros (4.1 KB)


Ingenious. Hopefully you’re not relying on this on an industrial scale - as it can’t be lightning fast.

It's for a report I send out once per day. Side note, I actually adjust the colors because one of the guys on my team is color-blind, and the original colors / legend for the chart doesn't work, so I screenshot the chart, pad the top and add my own legend all using KM. Not sure what KM uses behind the scenes for image manipulation, but it's REALLY fast (FWIW).

Second side note: For any developers out there writing software, PLEASE remember to provide accessible - alternative layouts for folks who are color blind, such as charts with different shape bullets, etc...