I am writing a script that takes a multiline paragraph and scales down the pointSize to fit all the text on one lines that matches the width of the text frame.
Here's how i calculate it.
- I add up the length of each line to get the total width.
- calculate the scaling percentage by diving the text frame width with the total width.
- multiple current point size with pecentage.
Pretty simple stuff.
The issue i am facing is that indesign makes the last character as the last insertion point of each line. IT IGNORES THE SPACE AFTER IT. Which shoul technically be the last inseriton point. So since that space doesnt get calculated, it throw off my entire scaling.
Am i doing something wrong. Any help would be appreciated.
my code:
var frameWidth = app.selection[0].geometricBounds[3] - app.selection[0].geometricBounds[1];
var numOfLines = app.selection[0].lines.length;
var totalWidth = 0; for (var j = numOfLines - 1; j>-1; j--) { if(j == numOfLines-1) { totalWidth = totalWidth + (app.selection[0].lines[j].insertionPoints[-1].horizontalOffset - app.selection[0].lines[j].insertionPoints[0].horizontalOffset); }else{ totalWidth = totalWidth + (app.selection[0].lines[j].characters[-1].horizontalOffset - app.selection[0].lines[j].characters[0].horizontalOffset); }}
var percentage = frameWidth / totalWidth;
app.selection[0].paragraphs[0].pointSize = app.selection[0].paragraphs[0].pointSize * percentage + "pt";