Copy link to clipboard
Copied
Hi
We have written a script which has a specific text area where text will reside.
var text_width_area = 6142;
The rest of the script is as follows
var fontsize = activeDocument.activeLayer.textItem.size;
var textwidth = Number(activeDocument.activeLayer.bounds[2].value) - Number(activeDocument.activeLayer.bounds[0].value);
var difference = Number(textwidth - text_width_area);
var difference_perc = Number(difference / textwidth * 100);
var text_calc = difference_perc / 100 * fontsize;
var text_calc_final = fontsize - text_calc;
if (Number(textwidth > text_width_area)) {
activeDocument.activeLayer.textItem.size = text_calc_final;
activeDocument.activeLayer.textItem.tracking = -50;
}
So we set the fontsize, text width area, then work out the difference between the two.
Then work out the sum and percentage to reduce text size to so that text fits within the area.
It will only resize if the text goes over the text area
For most of the text changes the new text reduction sits in the middle horizontally, but when a large change is needed, the text sits at the bottom.
The text is not in a bound box as we found it was worse in a bound box.
I know of baselineshift, but i am trying to work out a calculation or something to then get the text in the middle vertically.
The width position is always correct, but i am trying to always centre again the text vertically if it shifts down due to font size change.
Any help is appreciated.
Cheers
// it is assumed that app.preferences.rulerUnits != Units.PERCENT
var text_ver_center = (activeDocument.activeLayer.bounds[1] + activeDocument.activeLayer.bounds[3])/2;
activeDocument.activeLayer.textItem.size = text_calc_final;
// your code....
activeDocument.activeLayer.translate(0, text_ver_center - (activeDocument.activeLayer.bounds[1] + activeDocument.activeLayer.bounds[3])/2);
Copy link to clipboard
Copied
// it is assumed that app.preferences.rulerUnits != Units.PERCENT
var text_ver_center = (activeDocument.activeLayer.bounds[1] + activeDocument.activeLayer.bounds[3])/2;
activeDocument.activeLayer.textItem.size = text_calc_final;
// your code....
activeDocument.activeLayer.translate(0, text_ver_center - (activeDocument.activeLayer.bounds[1] + activeDocument.activeLayer.bounds[3])/2);
Copy link to clipboard
Copied
Thank you, worked perfect.