Scaling text to text box expression

Hi -
I've been searching around for an expression that will scale text to fit within a paragraph text box. Basically, I've created a text box that will feed in text from outside of AE and I need the text to scale to fit within a designated space.

The attached project is the best I've managed to do thus far. It reflows the text (breaking between words) so that the text area becomes approximately the same shape as the rectangle and then scales the resulting text area to fit into the rectangle. As long as the type is all the same formatting, it works pretty good.

There are two slider controls on the text layer:

1. "margin" for how much margin there is between the edge of the text box and the text.

2. "leading ratio" which needs to be adjusted if you change the typeface, leading or other character formats. It is used to tell the reflow expression what the average height to width ratio for one letter is, including the leading (line spacing). If you get too much empty space at the right or at the bottom, adjust this setting.

Easy way to see how this works: Resize the rectangle from the bottom right corner. (Note: If you make the text box very thin and tall, the end of the text will be missing. To fix that, make the original text box taller with the type tool.)

Hey Kalle -
One more thing if you have a moment. I've now tried to create an expression that will pull text from outside of After Effects and space it evenly on a path. It only needs to be one line but the biggest problem is the variation in the length of the text that will be brought in dynamically. For instance, one line of text is country names so one could be "Chile" and another is "The United States of America". If you look at the example in the dropbox link below, you'll see what I'm talking about (where it says "Switzerland"). Similar to what you did before, the expression would need to scale the text and adjust the tracking between each letter. On the flip side, when a short name like "Chile" is brought in, the text needs to be centered and letter spacing close together so it's not too spread apart.

If you wouldn't mind giving me your thoughts on this, I would appreciate it.

Apparently I need to take some programming classes. This stuff is insane beneficial. I gave it the old college try but am not left brained enough to get this to work. If you feel like looking at the file, it's in the Dropbox below.

What I'm getting currently is that when the text is too long, the characters start piling up on each other. If there is a way to keep the tracking the same whether big or small, that would be sweet. Basically, the text will need to scale down after it reaches the max limit on the path.

Expressions are extremely useful for repetitive tasks. Scripting even more so.

Okay, looking at this again, I'm thinking the Linear expression would give you more control.

If you still want the text to have some tracking depending on number of characters, you can do this for tracking:linear(text.sourceText.length, 5, 20, 25, -3)

This means that for any input values (text.sourceText.length) from 5 to 20 the output values (tracking) go from 25 to -3. So, if you have 5 letters of less, tracking is 25, if you have 20 letters or more, tracking is -3, and in-between values are linearly interpolated.

Similarly, you can use this for layer scaling. You'll have to make sure that the path can hold the longest text by having ample extra length at each end, since it will be scaled down. And put the anchor point in the right place as the layer scales around it.

s=linear(text.sourceText.length, 5, 20, 100, 50);
[s,s]

(See if you can figure out what happens there based on the first example. Note that scale is a two-dimensional property, that is why you need a two-dimensional array [s,s].)

Awesome! Everything works great outside of a few things. The text doesn't see the bounds of the path and so when I type too many characters, it drops down to a second line. The anchor point also changes when there are multiple lines of text but that won't be an issue if it is restricted to one line. I've been trying to look up expression help but not seeing much out there or not searching the right terms.

hey Guys,
This script was a live saver in the past but tried to use this script in a recent project. I noticed that once a stroke is added to the text the point size & leading are not the same. I know this because I duplicated the "Text" layer and did this...the two layers react differently to text being added. I just want to know why does this happen and how could it be fixed? Thanks!