Copy link to clipboard
Copied
Hi! I'm trying some ideas with Indesign Scripts. I've been looking for a way to obtain the size of the chars (width and height) inside a textbox, ideally measured in the same unit as the geometricBounds function. So far I've tried with pointSize of the Style, but it doesn't seem to make sense (I got a charsize of 2mm in a textbox of 14mm wide, but the textbox had more than 7 characters in each line). It's very possible that I just don't know how fonts work and I've got the measurements wrong.
Another thing I'm looking for is the leading, measured in mm as well. Am I looking in the wrong places?
Hi @Luis27099811ehw5, for your first quesiton, have a look at my script for this thread. The function getTextOutlineBounds returns a bounding box for the selected glyphs.
- Mark
For your second question: to get the leading, in mm, have a look at this:
(function () {
// this is the default, usually
app.scriptPreferences.measurementUnit = MeasurementUnits.PTS;
const mm = 2.834645;
var doc = app.activeDocument,
text = doc.selection[0];
// sanity-check
if (!text || !text.hasOwnProperty('leading')) {
alert('Please select some text and try again.');
return;
}
// raw value in pts
var leadingPT = text.leading,
...
Copy link to clipboard
Copied
Hi @Luis27099811ehw5, for your first quesiton, have a look at my script for this thread. The function getTextOutlineBounds returns a bounding box for the selected glyphs.
- Mark
Copy link to clipboard
Copied
The easiest way to get width of character / text - if it's in the same line - is to subtract HorizontalOffset of InsertionPoints before and after - without converting to outlines.
Copy link to clipboard
Copied
Yes exactly, but please note that this width also includes the sidebearings (the whitespace on either side of the glyph shape, which can be positive or negative). That's why I wrote my function—to return bounds of the actual glyph path(s).
- Mark
Copy link to clipboard
Copied
For your second question: to get the leading, in mm, have a look at this:
(function () {
// this is the default, usually
app.scriptPreferences.measurementUnit = MeasurementUnits.PTS;
const mm = 2.834645;
var doc = app.activeDocument,
text = doc.selection[0];
// sanity-check
if (!text || !text.hasOwnProperty('leading')) {
alert('Please select some text and try again.');
return;
}
// raw value in pts
var leadingPT = text.leading,
leadingMM;
if (leadingPT === Leading.AUTO) {
// calculate auto leading by multiplying auto leading percentage with point size
leadingMM = Math.floor(((text.autoLeading / 100 * text.pointSize) / mm) * 1000) / 1000
}
else {
// just choose one of the following, depending on your taste:
// (a) calculate millimeters, method 1: manually (rounding is optional depending on needs)
leadingMM = Math.floor((text.leading / mm) * 1000) / 1000;
// (b) calculate millimeters, method 2: the UnitValue object handles it all
leadingMM = UnitValue(text.leading, 'pt').as('mm');
}
alert('Leading in mm: ' + leadingMM);
})();
I prefer this method (I don't change the measurementUnit from points), but you may prefer to do so in your case. You can do this:
(function () {
// explicitly set measurement unit to millimeters
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
var doc = app.activeDocument,
text = doc.selection[0];
// sanity-check
if (!text || !text.hasOwnProperty('leading')) {
alert('Please select some text and try again.');
return;
}
// raw value in MILLIMETERS this time
var leadingMM = text.leading;
if (leadingMM === Leading.AUTO)
// calculate auto leading by multiplying auto leading percentage with point size
leadingMM = Math.floor(text.autoLeading / 100 * text.pointSize * 1000) / 1000;
alert('Leading in mm: ' + leadingMM);
})();
Copy link to clipboard
Copied
Great, both answers. Thanks!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now