Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
11

Measure text/chars and leading

Community Beginner ,
Nov 06, 2023 Nov 06, 2023

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?

TOPICS
Scripting , Type
441
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Nov 06, 2023 Nov 06, 2023

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

Translate
Community Expert , Nov 06, 2023 Nov 06, 2023

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,
...
Translate
Community Expert ,
Nov 06, 2023 Nov 06, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 06, 2023 Nov 06, 2023

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. 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2023 Nov 06, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2023 Nov 06, 2023

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);

})();

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 07, 2023 Nov 07, 2023
LATEST

Great, both answers. Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines