private static int computeWrapPosition(String line, int width) {
    if (line.length() < width) {
      return line.length();
    }

    int preferredBreakPoint = -1;
    int backupBreakPoint = -1;
    int tagBraceBalance = 0;

    for (int i = 0; i < line.length() && (i <= width || backupBreakPoint < 0); i++) {
      char c = line.charAt(i);
      if (tagBraceBalance > 0) {
        if (c == '{') {
          tagBraceBalance++;
        }
        else if (c == '}') {
          tagBraceBalance--;
        }
      }
      else if (c == '@' && i > 0 && line.charAt(i - 1) == '{') {
        // We're now inside of an inline tag. Start keeping track of the balance
        // of opening and closing braces to determine when we've left the tag.
        tagBraceBalance++;
      }
      else if (c == ' ') {
        preferredBreakPoint = i;
      }

      // We'll use this position in a pitch if we can't break somewhere not inside a tag.
      if (c == ' ') {
        backupBreakPoint = i;
      }
    }

    if (preferredBreakPoint > 0) {
      return preferredBreakPoint;
    }
    if (backupBreakPoint > 0) {
      return backupBreakPoint;
    }
    return line.length();
  }
private static int computeWrapPosition(String line, int width)