5 Replies Latest reply on Apr 9, 2013 7:20 AM by fabianmoronzirfas

# Getting the Physical Length of a Piece of Text (in mm etc.) in Javascript

I'm trying to write a script in Indesign CS5 (Windows) that can measure the physical, 'rendered' length of two pieces of text of similar length (in mm etc.), calculate the average length of the two, and adjust the tracking/horizontal scale of one or both of the pieces of text so that the physical length of both texts are the same.

I need a script because this task must be done thousands of time in a long document.

I'm struggling to find a way to access the 'rendered' length of a piece of text in Javascript.

I know among the attributes of a text box is whether the text has overrun. So I could copy the text to a special text-box, and use a for loop to reduce the dimentions of the text box, 1mm at a time, until the text box overruns, thus determining the length..

I could also get the length of a rendered string using python with it's built in GUI toolkit Tkinter:

http://stackoverflow.com/questions/2922295/calculating-the-pixel-size-of-a-string-with-pyt hon

And then have an indesign script that takes some data from the python script and sets the tracking/horizontal in Indesign with some fudge factors.

Both ideas seem like real kludges.

Any other suggestions?

Many thanks

David

• ###### 1. Re: Getting the Physical Length of a Piece of Text (in mm etc.) in Javascript

Lets assume that some text is selected and it's a single line:

```var text = app.selection[0];
var start = text.characters[0].horizontalOffset;
var end = text.characters[text.characters.length-1].horizontalOffset;
var length = end - start;
```
• ###### 2. Re: Getting the Physical Length of a Piece of Text (in mm etc.) in Javascript

Another approach (works with multiple lines) is to temporarily convert text to outlines (without deleting the original), read its geometricBounds and finally delete it:

```Main();

function Main() {
var text = app.selection[0];
var arr = text.createOutlines(false);
var outlinedText = arr[0];
var gb = outlinedText.geometricBounds;
alert("Width: " + (gb[3]-gb[1]) + ", Height: " + (gb[2]-gb[0]));
outlinedText.remove();
}
```
• ###### 3. Re: Getting the Physical Length of a Piece of Text (in mm etc.) in Javascript

Ah, that's great. :-D I saw the horizontalOffset function but wasn't sure what it did.

Now trying to figure out how I can determine the length of some text when it's broken over several lines.. the length as if it were on one line..

I guess I could copy the text to a very wide text box and measure it using the method in the first reply.

• ###### 4. Re: Getting the Physical Length of a Piece of Text (in mm etc.) in Javascript

You can simply iterate through each line in text to get the widest line. Something like this:

```Main();

function Main() {
var text = app.selection[0],
minLength, maxLength, start, end;

for (var i = 0; i < text.lines.length; i++) {
start = text.lines[i].characters[0].horizontalOffset,
end = text.lines[i].characters[text.lines[i].characters.length-1].horizontalOffset;

if (minLength == undefined || start < minLength) {
minLength = start;
}

if (maxLength == undefined || end > maxLength) {
maxLength = end;
}
}

var length = maxLength - minLength;
\$.writeln(length);
}

```

But this approach seems to be a little less precise than creating outlines since (I guess) it measures up to the beginning of the last character; discretionary hyphens, line brakes, etc. are not included into the measurement.

• ###### 5. Re: Getting the Physical Length of a Piece of Text (in mm etc.) in Javascript

Hi David,

here are 2 scripts that might help. They are not the solution to your task, but both work with text and scaling.

swissd.jsx creates fixed width text layouts. The provided text gets scaled so all lines have the same width.

https://github.com/natael/swissd

scale_till_overflow.jsx does what you mentioned. It scales text up until it overflows and than scales it down again so it can be displayed.