public String trimStringToWidth(String string,int newLength,boolean reverse){
  StringBuilder stringbuilder=new StringBuilder();
  int width=0;
  int k=reverse ? string.length() - 1 : 0;
  int l=reverse ? -1 : 1;
  boolean flag1=false;
  boolean flag2=false;
  for (int i1=k; i1 >= 0 && i1 < string.length() && width < newLength; i1+=l) {
    char thisChar=string.charAt(i1);
    int thisWidth=getCharWidth(thisChar);
    if (flag1) {
      flag1=false;
      if (thisChar != 108 && thisChar != 76) {
        if (thisChar == 114 || thisChar == 82) {
          flag2=false;
        }
      }
 else {
        flag2=true;
      }
    }
 else     if (thisWidth < 0) {
      flag1=true;
    }
 else {
      width+=thisWidth;
      if (flag2) {
        ++width;
      }
    }
    if (width > newLength) {
      break;
    }
    if (reverse) {
      stringbuilder.insert(0,thisChar);
    }
 else {
      stringbuilder.append(thisChar);
    }
  }
  return stringbuilder.toString();
}

public String trimStringToWidth ( String string , int newLength , boolean reverse ) { StringBuilder stringbuilder = new StringBuilder ( ) ; int width = 0 ; int k = reverse ? string . length ( ) - 1 : 0 ; int l = reverse ? - 1 : 1 ; boolean flag1 = false ; boolean flag2 = false ; for ( int i1 = k ; i1 >= 0 && i1 < string . length ( ) && width < newLength ; i1 += l ) { char thisChar = string . charAt ( i1 ) ; int thisWidth = getCharWidth ( thisChar ) ; if ( flag1 ) { flag1 = false ; if ( thisChar != 108 && thisChar != 76 ) { if ( thisChar == 114 || thisChar == 82 ) { flag2 = false ; } } else { flag2 = true ; } } else if ( thisWidth < 0 ) { flag1 = true ; } else { width += thisWidth ; if ( flag2 ) { ++ width ; } } if ( width > newLength ) { break ; } if ( reverse ) { stringbuilder . insert ( 0 , thisChar ) ; } else { stringbuilder . append ( thisChar ) ; } } return stringbuilder . toString ( ) ; }
trims a string to a specified width , and will reverse it if par3 is set .
