public static boolean fixLayout(StaticLayout layout) {
    int lineStart = layout.getLineStart(0);
    for (int i = 0, lineCount = layout.getLineCount(); i < lineCount; ++i) {
      int lineEnd = layout.getLineEnd(i);
      if (lineEnd < lineStart) {
        // Bug, need to swap lineStart and lineEnd
        try {
          Field mLinesField = StaticLayout.class.getDeclaredField("mLines");
          mLinesField.setAccessible(true);

          Field mColumnsField = StaticLayout.class.getDeclaredField("mColumns");
          mColumnsField.setAccessible(true);

          int[] mLines = (int[]) mLinesField.get(layout);
          int mColumns = mColumnsField.getInt(layout);

          // swap lineStart and lineEnd by swapping all the following data:
          // mLines[mColumns * i.. mColumns * i+1] <-> mLines[mColumns * (i+1)..mColumns * (i+2)]
          for (int j = 0; j < mColumns; ++j) {
            swap(mLines, mColumns * i + j, mColumns * i + j + mColumns);
          }
        } catch (Exception e) {
          // something is wrong, bail out
          break;
        }

        // start over
        return false;
      }

      lineStart = lineEnd;
    }

    return true;
  }
Attempts to fix a StaticLayout with wrong layout information that can result in
StringIndexOutOfBoundsException during layout.draw().

@param layout The {@link StaticLayout} to fix
@return Whether the layout was fixed or not
library/src/main/java/com/facebook/fbui/textlayoutbuilder/StaticLayoutHelper.java