Determining Width and Height of a Text Layer

Find out why the . goes before the /

Moderator: Paul Tuersley

vidpat
Posts: 86
Joined: October 21st, 2004, 12:36 am
Location: Phoenix, AZ
Contact:

Is it me or is this another item to add to the list of, "Text Layers don't Expose Much Functionality to Scripting":

The width and height of a text layer reported in a script (or expression, for that matter) is the same as the width and height of the comp. If anyone has any ideas as to how to get the actual width and height of a text layer, it would be greatly appreciated.

Thanks,
Peter
Shinjipierre
Posts: 36
Joined: December 4th, 2004, 10:10 am

You should create a plugin for that, in fact ^^

I don't know if t has been coded yet :D
vidpat
Posts: 86
Joined: October 21st, 2004, 12:36 am
Location: Phoenix, AZ
Contact:

For the record, I took Shinjipierre's advice and wrote a plug-in that would return the actual width and height of a Text Layer... almost.

The way Text Layers are implemented, the layer is actually the size of the comp and never moves. The text is then rendered onto that layer by the text engine. This can be observed in other ways such as applying a healthy blur to a Text Layer and then positioning the text near the edge of the comp. Lo and behold, the blur will fade at the edge of the comp even though the text seems to extend beyond.

So, my plug-in successfully reports the width and height of the text so long as it is contained entirely within the comp, which isn't what I was after to begin with. Also, for some odd reason, I had an interaction problem where, when the plug-in was enabled, I was only able to edit the text one character at a time before it would pop out of the editing mode. Strange.

Oh what glorious wonders I suspect are hiding in the unseen depths of the TextDocument, both in scripting and the SDK API.

Peter
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

Any plans to make your plugin available? I can think of a few scripts of mine that could use the data it outputs.
vidpat
Posts: 86
Joined: October 21st, 2004, 12:36 am
Location: Phoenix, AZ
Contact:

For those brave souls like Byron, I have put this version of the plug-in up at http://www.petertorpey.com/files/ae/TextInfo.aex. Windows only. As mentioned, it is buggy and doesn't work in most instances where I had hoped it would be useful. Try it on a non-text AV Layer for some interesting information (i.e. the width and height of the visible portion of a layer).
austin
Posts: 9
Joined: June 21st, 2005, 2:08 pm

any chance of a mac version? or the source, so we can hack one together? This sounds super handy, and poking around in the SDK has been on my to-do list for a while.

The text layer script-ability is one of my bigger disappointments in AE scripting ...

-austin
vidpat
Posts: 86
Joined: October 21st, 2004, 12:36 am
Location: Phoenix, AZ
Contact:

The plug-in is actually deceptively simple and I still find it to be of little practical use. I can provide the source if you (or someone else) would like to build it for a Mac.

I was hoping for improved scripting support for Text Layers in AE7. While there are many other advantages to 7, I think that such support would have motivated me to move on from 6.5.
davestewart
Posts: 114
Joined: March 10th, 2005, 5:50 am
Location: London, UK
Contact:

Absolutely.
austin
Posts: 9
Joined: June 21st, 2005, 2:08 pm

well, i solved my immediate problem with a really hacky function, but if that turns out not to work reliably, the plugin (and a real .width result) may be more helpful.

Basically, I'm just trying to make sure that text fits within a specified width, and if not, scale it down.
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

the only solution we've been able to come up with is to write a script that sets the text in illustrator (although that is not bullet proof either) and brings it in to AE which then properly reports it's height and width
Darkmoon_UK
Posts: 62
Joined: September 5th, 2006, 3:45 am
Location: Chiswick, London, UK
Contact:

If there is a way to create a default mask around the text, through script, then the bounding coordinates could be read from that. Unforuntately the automatic mask creation seems to only be available through the UI, am I wrong?

Reading the width and height properties directly return the size of the containing comp (as has already been said). I have also tried precomping the text layer in the hope that it would crop to the bounding area. It doesn't.

I was trying to use vidpats plugin to determine the size of text so I could draw a bounding box (with small border) around it using an expression. While the reported numbers looked right in the Effects panel, the box scaling was random and jumping between about 3 states when it was referenced through the expression and the timeline was scrubbed. Any ideas?

Austin: Do you mind telling us about how your 'hacky' function worked?

Adobe have done a sterling job at making this important information quite impossible to get at! :oops:
nab
Posts: 203
Joined: November 29th, 2005, 3:00 am
Location: Royan
Contact:

a way to create a default mask around the text, through script
To my knowledge, no way to get this rectangular default box.

I did a quick jump into the SDK guide a few months ago and found the syntax that returns the width of a text, something like

Code: Select all

myTextWidth = in_data->extent_hint.right - in_data->extent_hint.left;
(the in_data pointer represents the layer)

I suppose vidpat has used something similar in his plugin. But this is based on the visible (alpha) part of the layer, so that it becames useless whenever the layer is outside the comp screen.

You may execute another app from ae in which you can determine 'easily' the text length in pixels (Lloyd has mentionned Illustrator, and I wrote a ae script for this that uses Photoshop..). But of course this solution is not always possible and not very 'clean'.

Anecdotical history: last year I was working on a project for a french national tv involving a lot of 'text management'. I have determined the width of approximately hundred characters and symbols one by one (with the rectangular mask tool and a script)....a real pain. Hopefully they use a single font ! I did it one time and include now this small database in all text-related scripts I write for them. :D
vidpat
Posts: 86
Joined: October 21st, 2004, 12:36 am
Location: Phoenix, AZ
Contact:

Yes nab, that is essentially what my plug-in does, hence the limitation about text larger than the comp size or off the edge of the comp.

I had thought about writing a plug-in that would get the masks from the text and then find the max and min verts. That might work, but probably would be unduely computationally expensive.
Darkmoon_UK
Posts: 62
Joined: September 5th, 2006, 3:45 am
Location: Chiswick, London, UK
Contact:

Vidpat; your plugin appears to report the right numbers in the effects panel, but isn't reliably read by an expression. Do you have any idea why the values seen by an expression would 'glitch' in the way I have described? I can do a RAM preview and a box drawn by linking to your plugin will flicker.

Is this what you meant when you said it had limited practical use - and do you think this might be a problem intrinsic to approaching this from a plugin angle?

Being mainly a Java programmer I am not too confident with the whole C++/SDK thing, but stand a chance at working with some existing code. Since I really need this functionality, it would be nice to have a look at your source code so far and see if I can try to stabilise the reported value somehow e.g. reject 'zero' and 'comp size' values and latch on any other value.

Failing this I am joining the pixel counting club with nab!
vidpat
Posts: 86
Joined: October 21st, 2004, 12:36 am
Location: Phoenix, AZ
Contact:

Darkmoon_UK:
I haven't been doing very much work in AE lately, so I haven't looked at it in a very long time and I had just thrown it together on a whim. I suppose I ought to look at it seriously, at some point. I remember some odd behaviors, like not being able to edit the text reliably with my plug-in enabled. I haven't played around to see why you might be getting the problem you described. Off the top of my head, it sounds like it might be an issue with AE caching values or not reevaluating the expression on every frame because it is not expecting my plug-in to change the parameters (since it's usually the other way about). I might not be updating the parameters in the best place, either.

There are several reasons why it is of limited practical use. My original intent at the time was to animate the text using expressions, but the text layers I had were, by necessity, larger than the comp. Thus, it was useless for that.

Your line, "Adobe have done a sterling job at making this important information quite impossible to get at!" says it all. The real problem here is that the text information is not exposed in the SDK. I think Useful Assistants had a way of getting at this data, but I haven't actually used UA myself. If it did, then it is possible to do in a plug-in. All of the pertinent information about text layers is stored in an opaque datatype. It's probably just a struct, but it's undocumented and I didn't spend the time trying to decipher it. It may be the same structure Photoshop uses, so it may be documented in the PS SDK, which I don't have.
Post Reply