Find the font size given the Width and Height of a comp and length of string
Copy link to clipboard
Copied
Im trying to automate the filling of an empty comp with some variable text.
I'll have the information of the comp size, the selected font and the actual text string that will go inside the comp.
I want the font size to adjust to the comp size and length of the text to make it flushed and not overflow or come out short.
Does it make sense?
Thank you in advance
Copy link to clipboard
Copied
There is no way to access the internal parameters of the actual glyphs, in particular the em box relevant for font size and spacing calculations, so you need to have some sort of reference in your formula like e.g. the exact widths of each letter of the typeface you use at a given size in an array or list or such from which you then can parste the string, calculate the lengths at the reference size and adjust the font size that will actually be used, including reformatting the text if necessary to wrap extra words around. Similarly, you could have an iterative algorithm that adjusts your text based on actually visible pixels checked with sampleImage() and sourceRectAtTime() and other functions, but in any case it's going to be quite complicated. The more fancy you want to go, the more code you have to add to check specific things.
Mylenium
Copy link to clipboard
Copied
Something like this should be close (assumes your comp is selected and layer 1 is your text layer):
var myPercent = 90; // fill 90% of comp width
var myComp = app.project.activeItem;
var myTextLayer = myComp.layer(1);
var mySourceText = myTextLayer.property("ADBE Text Properties").property("ADBE Text Document");
var myTextDoc = mySourceText.value;
var myFontSize = myTextDoc.fontSize;
var myTextWidth = myTextLayer.sourceRectAtTime(0,false).width;
var newFontSize = ((myComp.width*myPercent/100)/myTextWidth)*myFontSize;
myTextDoc.fontSize = newFontSize;
mySourceText.setValue(myTextDoc);
Copy link to clipboard
Copied
Thank you very much Dan,
I tried the soulution and definitely is around there.
I couldn’t make it work with the height.
I made a little explanation video of my objective.
I need the text to fill width and height of the comp.
Any idea?
Thank you in advance
Copy link to clipboard
Copied
Yeah, sorry--I didn't read the question close enough. I can't think of any easy way to do it. I thought maybe you could use paragraph text and have the script set the box size to match the comp size, and then increase the font size until until text starts to vanish and then back it off, but I couldn't come up with a reliable algorithm to do that. That would be a good function to have handy--maximize text size to fit in a given rectangle, but I don't have anything like that on hand.

