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
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[3] > 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;