Mailing List AE-List@media-motion.tv — Message #63942
From: Mathias Möhl <AE-List@media-motion.tv>
Subject: Re: [AE] Expression to break text after a certain number of characters
Date: Wed, 11 Apr 2018 10:08:12 +0200
To: After Effects Mail List <AE-List@media-motion.tv>
You can split the text at linebreaks and then apply dan’s processing to each of the parts separately:

————

txt = thisComp.layer("Source Text").text.sourceText;
n = thisComp.layer("Controls").effect("Text width")("Slider");

function breakToGivenLength(txt,n){
  var outStr = "";
  var newLine = ""
  var splt = txt.split(" ")
  for (var i = 0; i < splt.length; i++){
    if ((newLine + " " + splt[i]).length > n){
      if (outStr != "") outStr += "\r";
      outStr += newLine;
      newLine = splt[i];
    }else{
      if (newLine != "") newLine += " ";
      newLine += splt[i];
    }
  }
  if (newLine != ""){
    if (outStr != "") outStr += "\r";
    outStr += newLine;
  }
  return outStr;
}


lines = txt.split(/|\r|\n|\x03/);
for(var i=0; i< lines.length; i++){
  lines[i]=breakToGivenLength(lines[i],n);
}
lines.join("\n");


————

Note that this still does not deal properly at tabs, for example (it only breaks at simple line breaks but not at tabs).

Cheers,
Mathias



mamoworld.com
simplify your cgi

Dr. Mathias Möhl
Marlow 52 , 18551 Sagard, Germany
Phone +49 (0)38302 - 88 75 61
info@mamoworld.com



On 11 Apr 2018, at 09:33, Jarle Leirpoll <AE-List@media-motion.tv> wrote:

I found this expression from Dan Ebberts, and it works beautifully until the
source text has line breaks.
After the line breaks, the first lines get shorter. It seems to add the
characters from the line above the break to the character count. See this
image:
https://www.dropbox.com/s/a842o913cxhyeua/LineBreakTrouble.png?dl=0

Is there a way to make this code aware of line breaks too, so that it just
keeps the line breaks from the source and starts on a new line with the
correct length?

The goal is that the user should just copy-paste text from a word document
or similar, into a field - and it should just work.

/jarle

txt = thisComp.layer("Source Text").text.sourceText;
n = thisComp.layer("Controls").effect("Text width")("Slider");
outStr = "";
newLine = ""
splt = txt.split(" ")
for (i = 0; i < splt.length; i++){
 if ((newLine + " " + splt[i]).length > n){
   if (outStr != "") outStr += "\r";
   outStr += newLine;
   newLine = splt[i];
 }else{
   if (newLine != "") newLine += " ";
   newLine += splt[i];
 }
}
if (newLine != ""){
 if (outStr != "") outStr += "\r";
 outStr += newLine;
}
outStr;




+---End of message---+
To unsubscribe send any message to <ae-list-off@media-motion.tv>




 
Subscribe (FEED) Subscribe (DIGEST) Subscribe (INDEX) Unsubscribe Mail to ListMaster