Page 1 of 1

### An expression for determining the size of a text layer

Posted: February 10th, 2008, 10:31 pm
Here is a (slow) expression that determines the width and the height of a text layer using the new sampleImage(). It is assumed that the text is horizontal and entirely visible. The expression can probably be optimized (this is a job for you Dan ), but here is the general idea:
• 1. initialize the left, right, up and down variables of the bounding box we're looking for
2. sample the pixels of the text layer
3. if the current pixel has an alpha greater than 0, update the variables
4. output width=right-left, height=down-up
This can be written like that (in another text layer):

Code: Select all

``````L = thisComp.layer("MyTextLayer");
w = L.width; h = L.height;
lmin = w; rmax = 0;
umin = h; dmax = 0;

for (i = 0; i < h; i++)
{
for (j = 0; j < w; j++)
{
p = L.sampleImage([j,i], [0.5,0.5]);
if (p > 0)
{
if (i < umin) umin = i;
if (i > dmax) dmax = i;
if (j < lmin) lmin = j;
if (j > rmax) rmax = j;
}
}
}
textW = rmax - lmin;
textH = dmax - umin;

"Size of text:\r" + textW + "x" + textH;
``````

### Re: An expression for determining the size of a text layer

Posted: February 11th, 2008, 7:27 pm
Please guys don't let me alone in the playground Instead of sampling every pixels, we can directly sample rows or columns by changing the radius parameter in sampleImage(). The number of operations decreases from w*h to w+h which is a huge gain (considering that sampling a larger area doesn't increase that much the computation time).

So here is an optimized version of the previous expression:

Code: Select all

``````function ExistPixelInCol(layer, colIndex)
{
return layer.sampleImage([colIndex,thisComp.height/2], [.5,thisComp.height/2]) > 0 ? true : false;
}

function ExistPixelInRow(layer, rowIndex)
{
return layer.sampleImage([thisComp.width/2,rowIndex], [thisComp.width/2,.5]) > 0 ? true : false;
}

L = thisComp.layer("MyTextLayer");
w = L.width; h = L.height;
lmin = L.width; rmax = 0;
umin = L.height; dmax = 0;

for (i = 0; i < h; i++)
{
b = ExistPixelInRow(L,i);
if (b && i < umin)
umin = i;
if (b && i > dmax)
dmax = i;
}
for (j = 0; j < w; j++)
{
b = ExistPixelInCol(L,j);
if (b && j < lmin)
lmin = j;
if (b && j > rmax)
rmax = j;
}
textW = rmax - lmin;
textH = dmax - umin;

"Size of text:\r" + textW + "x" + textH;
``````
If you change the Vertical or Horizontal scale in the Character panel, you'll see the expression updates in 'real time', which was definitely not the case with the previous expression.

### Re: An expression for determining the size of a text layer

Posted: February 12th, 2008, 10:04 pm
This one is more restrictive as it assumes the text is left aligned and doesn't handle 'fancy text' (experts will understand what the expression considers as fancy text), but it should be a little faster.
The idea is to start the search with a bounding box of zero area from the current position of the text layer. We then expand the box to the right/top until no more pixels are detected in the column/row.

Code: Select all

``````function getTextPosition(layer)
{
return layer.toWorld(layer.anchorPoint) - layer.anchorPoint;
}

function ExistPixelInCol(layer, colIndex)
{
return layer.sampleImage([colIndex,thisComp.height/2], [.5,thisComp.height/2]) > 0 ? true : false;
}

function ExistPixelInRow(layer, rowIndex)
{
return layer.sampleImage([thisComp.width/2,rowIndex], [thisComp.width/2,.5]) > 0 ? true : false;
}

L = thisComp.layer("MyText");
P = getTextPosition(L);
lmin = P; rmax = lmin;
tmin = P; bmax = tmin;

do { tmin--; } while (ExistPixelInRow(L,tmin));

for (j = P+1; j < L.width; j++)
if (ExistPixelInCol(L,j))
rmax = j;

textW = rmax - lmin;
textH = bmax - tmin;

textW + "," + textH;
``````
I don't claim these expressions are extremely useful but it's for sure an interesting training ### Re: An expression for determining the size of a text layer

Posted: February 13th, 2008, 1:56 pm
Wow! Nicely done! This will defintiely come in handy. I just wrote a script the other day just to use sourceRectAtTime. Thanks!

### Re: An expression for determining the size of a text layer

Posted: February 13th, 2008, 8:57 pm
Yeah, nice job. It works great for text and shape layers. For solids, you have to use the toWorld() transform to get it to work right. I tried to optimize the code a little, but it really isn't much better than what you have:

Code: Select all

``````L = thisComp.layer("test text");;
top = bottom = left = right = 0;

gotOne = false;
for (i = 0; i < height; i++){
if (L.sampleImage([width/2, i], [width/2, 0.5], true) > 0){
bottom = i;
if (! gotOne) {
top = i;
gotOne = true;
}
}
}

gotOne = false;
for (i = 0; i < width; i++){
if (L.sampleImage([i, height/2], [0.5, height/2], true) > 0){
right = i;
if (! gotOne) {
left = i;
gotOne = true;
}
}
}

"UL = [" + left + "," + top + "]   LR = [" + right + "," + bottom + "]";
``````
Dan

### Re: An expression for determining the size of a text layer

Posted: February 14th, 2008, 8:33 pm
Nice job too, Dan.
For the sake of writing the best code we can, we could add two variables for "width/2" and "height/2" so that the expression doesn't have to recalculate them at each iteration of the loops.
I had a new idea for this expression that I'd like to experiment soon...I don't expose it yet because I'm not sure I will succeed in writing it ### Re: An expression for determining the size of a text layer

Posted: February 16th, 2008, 1:39 pm
Okay, I got my monster!

My starting point is the last expression posted by Dan. What is slow in this expression (and in previous expressions as well) is the fact that we sample every row and column even if this row or column is contained in a larger area that does not contain non-zero alpha pixels. In other words, say your text is written at the bottom of the comp, so that the upper half of the layer has no pixels. The expression will still sample each row of this area. This looks like too much work. So if we can determine that we won't find anything in that zone, there is no need to sample there.

This observation is the base idea of my new expression which is a sort of Divide and Conquer approach. We recursively split the layer into smaller parts and when an area is empty, we don't sample further in that area. The process is done vertically and horizontally. For instance, at the beginning, the expression determines whether there are some pixels in the upper part of the layer or not, if it finds some, then it analyzes the upper quarter, and so on until the area is around 1 pixel wide or empty.

I set up a little project to test these two techniques and the divide and conquer expression seems to outperforms the previous one in terms of computation time, while providing a bounding box of good quality. When I rendered the two comps in the project, the "new" comp was 375% faster than the "old" comp.

In both methods, I noticed that when the text is very small compared to the size of the comp, when we sample a row or a column that contains very few pixels, sampleImage() round the result to zero, so we may miss some pixel.

The expression isn't easily readable but if someone wants to improve it, here it is:

Code: Select all

``````function getParams(area)
{
var pointX = (area + area) / 2;
var pointY = (area + area) / 2;
var radiusX = (area - area) / 2;
var radiusY = (area - area) / 2;
}
function isAreaEmpty(area)
{
var params = getParams(area);
if (params < 0.5 || params < 0.5)
return true;
return L.sampleImage(params, params, true) > 0 ? false : true;
}
function sampleRow(area)
{
var params = getParams(area);
if (L.sampleImage(params, params, true) > 0)
{
if (params < top)
top = params;
if (params > bottom)
bottom = params;
}
}
function sampleCol(area)
{
var params = getParams(area);
if (L.sampleImage(params, params, true) > 0)
{
if (params < left)
left = params;
if (params > right)
right = params;
}
}
function sampleAreaHorizontally(area)
{
if (isAreaEmpty(area))
return;

if (area - area < 1.5)
sampleRow(area);

var UpperArea = [ area, [area,(area+area)/2] ];
var LowerArea = [ [area,(area+area)/2], area ];
sampleAreaHorizontally(UpperArea);
sampleAreaHorizontally(LowerArea);
}
function sampleAreaVertically(area)
{
if (isAreaEmpty(area))
return;

if (area - area < 1.5)
sampleCol(area);

var LeftArea  = [ area, [(area+area)/2, area] ];
var RightArea = [ [(area+area)/2, area], area ];
sampleAreaVertically(LeftArea);
sampleAreaVertically(RightArea);
}
var L = thisComp.layer("Text");
var top = L.height, bottom = 0, left = L.width, right = 0;
var halfW = L.width / 2;
var halfH = L.height / 2;
var area = [[0,0], [L.width,L.height]];
sampleAreaHorizontally(area);
sampleAreaVertically(area);
"" + left + "," + top + "," + right + "," + bottom;
``````
I know this looks like overkill but we are in the "Discuss the code" section ### Re: An expression for determining the size of a text layer

Posted: March 15th, 2016, 11:25 pm
Wow so this is what I was not knowing about all this time. â€œtextW = rmax - lmin;" and "textH = dmax - umin;â€ these are the commands that I did not use until now. No wonder I used to get idiotic results all this time. Thanks a ton for helping out.

::::::::::::::::::::::::::::::
Redrocker
sell citi thank you points
::::::::::::::::::::::::::::::