Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Adjust kerning when a number is present in a line of text

  • Adjust kerning when a number is present in a line of text

    Posted by Kevin Dazet on May 27, 2021 at 12:22 am

    I have a text layer that’s auto-populated by a script. Everything works well except that the font I’m using has a large gap between the two numerals in the numbers 10 through 19. I usually kern this manually, but in this case that’s not possible.

    If only the number is present in the line of text, I can simply use a tracking animator with an expression to identify that the source text contains that number and then have it adjust the tracking accordingly.

    However, if other text is present in addition to the number (for example, “June 15”), I would need a way to target just the number. Splitting the text into two layers (one for month and one for date) isn’t practical for me in this case.

    Can an expression scan a line of text for a specified array of numbers and then adjust the kerning or tracking of just those numbers without affecting the rest of the text in the layer?

    Thanks in advance for any insight!

    Kevin Dazet replied 5 years, 3 months ago 3 Members · 4 Replies
  • 4 Replies
  • Andrei Popa

    May 27, 2021 at 6:55 am

    This should do the trick:

    Add an animator based on tracking. Set the tracking you need for those characters. Add an expression selector to this animator and set the “Amount” to this.

    function getIndexes(txt, arr) {
    var indexes = [];
    for (let i = 0; i < arr.length; i++) {
    let idx = txt.indexOf(arr[i]);
    if (idx != -1) {
    for (let j = 1; j <= arr[i].length; j++) {
    indexes.push(idx + j);
    }
    }
    }
    return indexes;
    }
    var cases = ["11", "12", "13", "14"];
    var indexes = getIndexes(text.sourceText, cases);
    indexes.includes(textIndex) ? 100 : 0;

    You need to modify the cases variable to all the combinations you need.

  • Kevin Dazet

    May 27, 2021 at 4:50 pm

    Thanks, Andrei! I got an error on line three of the expression that says “Expected: ;”

    Any idea what’s causing that?

  • Kevin Camp

    May 27, 2021 at 6:51 pm

    In your project settings, go to the expressions tab and make sure the expression engine is set to ‘javascript’

  • Kevin Dazet

    May 27, 2021 at 7:39 pm

    Hallelujah—it works! Thanks to you both! Andrei: you really saved the day.

    I have a feeling I’ll be using this expression a lot in the future.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy