Skip to main content
Participant
April 6, 2021
Question

Dégradé approche texte

  • April 6, 2021
  • 2 replies
  • 374 views

Bonjour,

 

Je cherche une solution pour réaliser des textes sur une affiche avec une approche du texte en dégradé, sans se casser la tête... (voir image jointe)

 

Jusqu'à présent, j'effectuais la manip en y insérant des espaces, mais ça prend un peu de temps.. Il y aurait-il une fonction qui permettrait de réaliser ceci?

 

Merci pour votre aide!

 

Meilleures salutations,

Simon

This topic has been closed for replies.

2 replies

m1b
Community Expert
Community Expert
April 9, 2021

Here's my version, using tracking. It can do left- and right- justified text. It's a bit messy and have only tested on basic text frames but hopefully gives you something else to try, as well as femkeblanco's script. Note: after applying the tracking, you can copy the text out and into another shape text frame to achieve the curve in your example.

 

// these are just examples that modify the tracking you can make your own or change these
var exampleAdjustment1 = function (n) { return n + 30 };
var exampleAdjustment2 = function (n) { return (n + 10) * 1.1 };


// call the spaceOut function on the selected item (must be a TextFrame)
spaceOut(selection[0], exampleAdjustment1);



function spaceOut(txtFrame, adjustment, baseTrackAmount) {
    if (!txtFrame) return false;
    if (txtFrame.typename != 'TextFrame') throw 'spaceOut requires TextFrame item';
    txtFrame.textRanges[0].paragraphAttributes.hyphenate = false;
    if (baseTrackAmount == undefined) baseTrackAmount = 0;

    // make an array of lines, containing an array of characters
    var _lines = [];
    for (var i = txtFrame.story.lines.length - 1; i >= 0; i--) {
        // first add line break after the line so it doesn't wrap
        txtFrame.story.lines[i].contents += '\n';

        _lines[i] = [];
        for (var j = 0; j < txtFrame.story.lines[i].characters.length; j++) {
            _lines[i][j] = txtFrame.story.lines[i].characters[j];
        }
    }

    // get font size, used for calculating text box width expansion
    var fontSize = _lines[0][0].characterAttributes.size;

    // get justification
    var isJustifiedLeft = txtFrame.paragraphs[0].paragraphAttributes.justification == Justification.LEFT;

    var start, end, step;
    var longestLineAddedSpace = 0;
    // loop over lines
    for (var i = 0; i < _lines.length; i++) {
        var trackAmount = baseTrackAmount, total = 0;

        // these determine the looping order
        if (isJustifiedLeft) {
            start = 0, end = _lines[i].length, step = 1;
        } else {
            // right justified, start at last character, going backwards
            start = _lines[i].length, end = 0, step = -1;
        }

        // loop over characters
        for (var j = start; j != end; j += step) {
            var ch = _lines[i][j];
            if (!ch) continue; // empty line
            if (ch.length > 1) ch = ch.characters[ch.length - 1]; // had to do this due to unexpected value of j=0, but don't know why!

            // apply tracking
            ch.tracking = trackAmount;

            // keep record of running total for this line
            total += trackAmount;

            // update track amount according to function
            trackAmount = adjustment(trackAmount);
        }
        longestLineAddedSpace = Math.max(longestLineAddedSpace, total)
    }

    // adjust width of text box to accommodate added space
    try {
        addedSpaceInPts = (longestLineAddedSpace / 1000) * fontSize;
        txtFrame.textPath.width += addedSpaceInPts;
        if (!isJustifiedLeft) txtFrame.textPath.left -= addedSpaceInPts;
    } catch (error) {
        $.writeln(error);
    }
}
femkeblanco
Legend
April 9, 2021

Very nice.

femkeblanco
Legend
April 6, 2021

This is a basic idea

var string1 = selection[0].contents.replace(/ /g, "          ");
selection[0].contents = string1;
var paragraphz = selection[0].paragraphs;
for (var i = 0; i < paragraphz.length; i++) {
    var wordz = paragraphz[i].words;
    for (var j = 0; j < wordz.length; j++) {
        var characterz = wordz[j].characters;
        for (var k = 0, l = characterz.length - 1; l > 0; k += 1 + l, l--) {
            for (var n = 0; n < l; n++) {
                characterz.add(" ", characterz[k]);
            }
        }
    }
}