I need Help Improving and optimizing a Script.

I'm fairly new to java and scripting in general and I've been Trying to do a script that would increase the font Size of a text until it fully fills it's box Limits , and this is what I've come up with so far.

 function textResize(v,n){
    var myComp = v;
    var myTextLayer = myComp.layer(n);
    var myText = myTextLayer.sourceText.value;
    var orgText = myText.text;
    var textBoxSurface = myText.boxTextSize[0]*myText.boxTextSize[1];
    var textPixel = myTextLayer.sourceRectAtTime(0,false);
    var textPixelSurface = textPixel.height*textPixel.width;

    myText.text = "W";
    myText.fontSize = 20;
    textPixel = myTextLayer.sourceRectAtTime(0,false);
    textPixelSurface = textPixel.height*textPixel.width;
    while ((  Math.round((textBoxSurface/textPixelSurface))  >=  Math.round(orgText.length)  ) ){
        myText.fontSize = myText.fontSize +1;
        textPixel = myTextLayer.sourceRectAtTime(0,false);
        textPixelSurface = textPixel.height*textPixel.width;

    myText.text = orgText;


it's still inaccurate and even the font size reaches it's limit (1296)!

  • Good question, I'm examining the DOM via the Data Browser in the ESTK, there appear to be no readily-available overset text indicator properties. I see a "baselineLocs" array, which I'm not quite sure what that means yet. If there was a way to detect the height of text by itself and compare to the area-text box height, it would be the way. – Silly-V Apr 26 '17 at 14:49

Thats at spot on suggestion Silly-V. I was looking for a similar solution and tried to use baselineLocs. Basically, an array means, as per documentation

line0.start_x, line0.start_y, line0.end_x,
line0.end_y, line1.start_x, line1.start_y, line1.end_x, line1.end_y ...
lineN-1.start_x, lineN-1.start_y, lineN-1.end_x, lineN-1.end_y

With that said, each 4 array numbers declare a single line.

Accordingly, what I did:

During while cycle, reduced font size by X. Checked for the change in baselinesLoc array size.

According whether there is a change or not, set temp status variable. Which controls the while cycle. As long as baselineLocs increases, cycle spins. If it starts decreasing, it stops.

I am not yet finished with the execution, but this is my initial logic behind this. If there are a lot of text and it is not filled in the box, by introducing new lines (while reducing font size), we make sure all text gets filled. On the other hand, if after some point number of lines start decrease, it means we changed the fontSize too much and it needs to be added back.

Of course, later anchor point and position needs to be adjusted, but thats another topic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.