Skip to main content
dublove
Legend
Nov 25, 2026 5:44 AM UTC
Question

How to calculate the number of lines occupied by the "space before and space after" in the current selection area?

Hello everyone.

As shown in the figure below, suppose I have selected the text in the largest text box on the right.
Assuming the value of “space before”  is SpBValue, the value of “space after”  is  SpAValue, and the leading is “myLeading”
According to the following logic, how to calculate how many lines are occupied by the “space before and space after” in the selected area?

if(SpBValue<=myLeading||SpAValue<=myLeading){
//Consider occupying a row
}
if((SpBValue>1.2*myLeading&&SpBValue<2*myLeading)||(SpAValue>1.2*myLeading&&SpAValue<2*myLeading)){
//Consider occupying 2 rows
}

There are still 3 lines occupied, and my logic needs to be reorganized (i.e. if it is greater than 1.2, it is considered to occupy one line)

It should be noted that:
When a line with a “space before”  at the top of the text box, the “space before” does not occupy a line.
Similarly, line with a  “space after” do not occupy lines when it at the bottom of the text box.

Thank you very much.

 

    6 replies

    rob day
    Community Expert
    Community Expert
    Nov 25, 2026 1:01 PM UTC

    When a line with a “space before”  at the top of the text box, the “space before” does not occupy a line.

     

    Space Above has no affect on a text frame’s first paragraph because the frame’s Text Frame Options > Baseline Options > First Baseline setting handles the position of the first paragraph in the text frame. The Space After setting is applied to the first paragraph:

     

     

     

    The paragraphs that follow have the Space Above amount added to the Leading. Here the Leading is 14 pt and the 10 pt Space Above amount is added

     

     

    Translate
    dublove
    dubloveAuthor
    Legend
    Nov 25, 2026 4:58 PM UTC

    ​@rob day  ​@Eugene Tyson 

    Now,I would rather know how to determine if the first line of a paragraph is at the top of the text box
    For example, ps [i] in my code below.

    item = d.selection[0];
    var ps = item.paragraphs;
    var bf = [];
    for (var i = 0; i < ps.length; i++) {
    if (ps[i].spaceBefore > 0) {
    bf.push(ps[i]);
    }

     

    Translate
    rob day
    Community Expert
    Community Expert
    Nov 25, 2026 5:58 PM UTC

     

    You get the text frame’s first line baseline position from Text Frame Options (TextFramePreferences), not the paragraph.

     

    https://www.indesignjs.de/indesignapi/indesign/TextFramePreference.html

     

     

     

    textFramePreferences.insetSpacing[0]

     

     

    textFramePreferences.firstBaselineOffset

     

     

     

    Translate
    dublove
    dubloveAuthor
    Legend
    Nov 25, 2026 10:32 AM UTC

    I've figured something out.

                item = d.selection[0];
    var ps = item.paragraphs;
    var bf = [];
    var af = [];
    for (var i = 0; i < ps.length; i++) {
    if (ps[i].spaceBefore > 0) {
    bf.push(ps[i]);
    }
    if (ps[i].spaceAfter > 0) {
    af.push(ps[i]);
    }
    }
    var abf = af.length + bf.length;
    Translate
    Community Expert
    Nov 25, 2026 1:17 PM UTC

    I think you're nearly there, but you're counting the occurrences of Space Before and Space After rather than how many lines of space they actually occupy.

    For example, if you have 14 pt leading and 7 pt Space After followed by 7 pt Space Before, you're counting that as two lines when it would actually occupy the equivalent of one line.

    Perhaps another way to approach this would be to measure the distance between the actual baselines of consecutive lines, subtract the leading and work out how much additional space is being occupied.

    This should also naturally take care of the Space Before at the top of the text frame and Space After at the bottom, as there's no subsequent or preceding baseline to measure against within the frame.

    Something along these lines might work.

    (function () {

    if (!app.documents.length || app.selection.length !== 1) {
    alert("Please select some text.");
    return;
    }

    var sel = app.selection[0];

    if (!sel.lines || sel.lines.length < 2) {
    alert("Please select at least two lines of text.");
    return;
    }

    var oldUnits = app.scriptPreferences.measurementUnit;

    try {

    app.scriptPreferences.measurementUnit =
    MeasurementUnits.POINTS;

    var lines = sel.lines.everyItem().getElements();
    var myLeading = Number(lines[0].leading);
    var totalSpace = 0;

    if (lines[0].leading == Leading.AUTO ||
    !isFinite(myLeading) || myLeading <= 0) {
    alert("Please use fixed leading.");
    return;
    }

    for (var i = 1; i < lines.length; i++) {

    if (lines[i].leading == Leading.AUTO ||
    Number(lines[i].leading) !== myLeading) {
    alert("Please use consistent fixed leading.");
    return;
    }
    }

    for (var i = 1; i < lines.length; i++) {

    var prev = lines[i - 1];
    var curr = lines[i];

    // Ignore transitions between text frames
    if (prev.parentTextFrames[0].id !==
    curr.parentTextFrames[0].id) {
    continue;
    }

    // Ignore lines within the same paragraph
    if (prev.paragraphs[0].insertionPoints[0].index ===
    curr.paragraphs[0].insertionPoints[0].index) {
    continue;
    }

    // Measure the distance between baselines
    var distance = curr.baseline - prev.baseline;

    if (distance <= 0) {
    continue;
    }

    // Subtract normal leading
    var extraSpace = distance - myLeading;

    if (extraSpace > 0.001) {
    totalSpace += extraSpace;
    }
    }

    var extraLines = totalSpace / myLeading;
    var wholeLines = Math.ceil(extraLines - 0.000001);

    alert(
    "Actual text lines: " + lines.length +
    "\nAdditional spacing: " + totalSpace.toFixed(2) + " pt" +
    "\nEquivalent extra lines: " + extraLines.toFixed(2) +
    "\nExtra whole lines: " + wholeLines +
    "\nTotal occupied lines: " + (lines.length + wholeLines)
    );

    } finally {
    app.scriptPreferences.measurementUnit = oldUnits;
    }

    })();


     

    Translate