Cleanup m_RTFLine code in RTFBreak.

This CL cleans up the m_RTFLine and m_iReady to be easier to work with.

Change-Id: Ic8a2938d01672a4bc3f587fd144a5b63a0cee300
Reviewed-on: https://pdfium-review.googlesource.com/2935
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Nicolás Peña <npm@chromium.org>
Commit-Queue: dsinclair <dsinclair@chromium.org>
diff --git a/xfa/fgas/layout/fgas_rtfbreak.cpp b/xfa/fgas/layout/fgas_rtfbreak.cpp
index 8f5895f..774d78f 100644
--- a/xfa/fgas/layout/fgas_rtfbreak.cpp
+++ b/xfa/fgas/layout/fgas_rtfbreak.cpp
@@ -32,15 +32,13 @@
       m_pUserData(nullptr),
       m_eCharType(FX_CHARTYPE_Unknown),
       m_dwIdentity(0),
-      m_RTFLine1(),
-      m_RTFLine2(),
       m_pCurLine(nullptr),
-      m_iReady(0),
-      m_iTolerance(0) {
-  m_pCurLine = &m_RTFLine1;
+      m_iTolerance(0),
+      m_iReady(-1) {
+  m_pCurLine = &m_RTFLine[0];
 
   SetBreakStatus();
-  m_bPagination = (m_dwLayoutStyles & FX_RTFLAYOUTSTYLE_Pagination) != 0;
+  m_bPagination = !!(m_dwLayoutStyles & FX_RTFLAYOUTSTYLE_Pagination);
 }
 
 CFX_RTFBreak::~CFX_RTFBreak() {
@@ -147,7 +145,7 @@
 }
 
 void CFX_RTFBreak::SetBreakStatus() {
-  m_dwIdentity++;
+  ++m_dwIdentity;
   int32_t iCount = m_pCurLine->CountChars();
   if (iCount < 1)
     return;
@@ -175,19 +173,6 @@
   return nullptr;
 }
 
-const CFX_RTFLine* CFX_RTFBreak::GetRTFLine() const {
-  if (m_iReady == 1)
-    return &m_RTFLine1;
-  if (m_iReady == 2)
-    return &m_RTFLine2;
-  return nullptr;
-}
-
-const CFX_RTFPieceArray* CFX_RTFBreak::GetRTFPieces() const {
-  const CFX_RTFLine* pRTFLine = GetRTFLine();
-  return pRTFLine ? &pRTFLine->m_LinePieces : nullptr;
-}
-
 inline FX_CHARTYPE CFX_RTFBreak::GetUnifiedCharType(
     FX_CHARTYPE chartype) const {
   return chartype >= FX_CHARTYPE_ArabicAlef ? FX_CHARTYPE_Arabic : chartype;
@@ -395,7 +380,7 @@
 CFX_RTFBreakType CFX_RTFBreak::EndBreak(CFX_RTFBreakType dwStatus) {
   ASSERT(dwStatus != CFX_RTFBreakType::None);
 
-  m_dwIdentity++;
+  ++m_dwIdentity;
   const CFX_RTFPieceArray* pCurPieces = &m_pCurLine->m_LinePieces;
   int32_t iCount = pCurPieces->GetSize();
   if (iCount > 0) {
@@ -407,9 +392,8 @@
     return dwStatus;
   }
 
-  const CFX_RTFLine* pLastLine = GetRTFLine();
-  if (pLastLine) {
-    pCurPieces = &pLastLine->m_LinePieces;
+  if (HasRTFLine()) {
+    pCurPieces = &m_RTFLine[m_iReady].m_LinePieces;
     iCount = pCurPieces->GetSize();
     if (iCount-- > 0) {
       CFX_RTFPiece* pLastPiece = pCurPieces->GetPtrAt(iCount);
@@ -431,9 +415,8 @@
   if (dwStatus == CFX_RTFBreakType::Piece)
     return dwStatus;
 
-  m_iReady = m_pCurLine == &m_RTFLine1 ? 1 : 2;
-  CFX_RTFLine* pNextLine =
-      m_pCurLine == &m_RTFLine1 ? &m_RTFLine2 : &m_RTFLine1;
+  m_iReady = m_pCurLine == &m_RTFLine[0] ? 0 : 1;
+  CFX_RTFLine* pNextLine = &m_RTFLine[1 - m_iReady];
   bool bAllChars = m_iAlignment == CFX_RTFLineAlignment::Justified ||
                    m_iAlignment == CFX_RTFLineAlignment::Distributed;
 
@@ -478,8 +461,6 @@
   }
 
   const CFX_RTFChar* pCurChars = m_pCurLine->m_LineChars.data();
-  const CFX_RTFChar* pTC;
-  CFX_RTFPieceArray* pCurPieces = &m_pCurLine->m_LinePieces;
   CFX_RTFPiece tp;
   tp.m_pChars = &m_pCurLine->m_LineChars;
   bool bNew = true;
@@ -487,7 +468,7 @@
   int32_t iLast = m_pCurLine->CountChars() - 1;
   int32_t j = 0;
   for (int32_t i = 0; i <= iLast;) {
-    pTC = pCurChars + i;
+    const CFX_RTFChar* pTC = pCurChars + i;
     if (bNew) {
       tp.m_iStartChar = i;
       tp.m_iStartPos += tp.m_iWidth;
@@ -511,13 +492,13 @@
         tp.m_dwStatus = pTC->m_dwStatus;
         tp.m_iWidth += pTC->m_iCharWidth;
         tp.m_iChars += 1;
-        i++;
+        ++i;
       }
-      pCurPieces->Add(tp);
+      m_pCurLine->m_LinePieces.Add(tp);
       bNew = true;
     } else {
       tp.m_iWidth += pTC->m_iCharWidth;
-      i++;
+      ++i;
     }
   }
   return true;
@@ -525,16 +506,12 @@
 
 void CFX_RTFBreak::EndBreak_BidiLine(std::deque<FX_TPO>* tpos,
                                      CFX_RTFBreakType dwStatus) {
-  FX_TPO tpo;
-  CFX_RTFPiece tp;
   CFX_RTFChar* pTC;
-  int32_t i;
-  int32_t j;
   std::vector<CFX_RTFChar>& chars = m_pCurLine->m_LineChars;
   int32_t iCount = m_pCurLine->CountChars();
   if (!m_bPagination && m_pCurLine->m_iArabicChars > 0) {
     int32_t iBidiNum = 0;
-    for (i = 0; i < iCount; i++) {
+    for (int32_t i = 0; i < iCount; ++i) {
       pTC = &chars[i];
       pTC->m_iBidiPos = i;
       if (pTC->GetCharType() != FX_CHARTYPE_Control)
@@ -544,7 +521,7 @@
     }
     FX_BidiLine(chars, iBidiNum + 1, 0);
   } else {
-    for (i = 0; i < iCount; i++) {
+    for (int32_t i = 0; i < iCount; ++i) {
       pTC = &chars[i];
       pTC->m_iBidiLevel = 0;
       pTC->m_iBidiPos = 0;
@@ -552,15 +529,17 @@
     }
   }
 
+  CFX_RTFPiece tp;
   tp.m_dwStatus = CFX_RTFBreakType::Piece;
   tp.m_iStartPos = m_pCurLine->m_iStart;
   tp.m_pChars = &chars;
-  CFX_RTFPieceArray* pCurPieces = &m_pCurLine->m_LinePieces;
+
   int32_t iBidiLevel = -1;
   int32_t iCharWidth;
+  FX_TPO tpo;
   uint32_t dwIdentity = static_cast<uint32_t>(-1);
-  i = 0;
-  j = 0;
+  int32_t i = 0;
+  int32_t j = 0;
   while (i < iCount) {
     pTC = &chars[i];
     if (iBidiLevel < 0) {
@@ -577,11 +556,11 @@
       tp.m_dwIdentity = dwIdentity;
       tp.m_pUserData = pTC->m_pUserData;
       tp.m_dwStatus = CFX_RTFBreakType::Piece;
-      i++;
+      ++i;
     } else if (iBidiLevel != pTC->m_iBidiLevel ||
                pTC->m_dwIdentity != dwIdentity) {
       tp.m_iChars = i - tp.m_iStartChar;
-      pCurPieces->Add(tp);
+      m_pCurLine->m_LinePieces.Add(tp);
       tp.m_iStartPos += tp.m_iWidth;
       tp.m_iStartChar = i;
       tpo.index = j++;
@@ -592,14 +571,14 @@
       iCharWidth = pTC->m_iCharWidth;
       if (iCharWidth > 0)
         tp.m_iWidth += iCharWidth;
-      i++;
+      ++i;
     }
   }
 
   if (i > tp.m_iStartChar) {
     tp.m_dwStatus = dwStatus;
     tp.m_iChars = i - tp.m_iStartChar;
-    pCurPieces->Add(tp);
+    m_pCurLine->m_LinePieces.Add(tp);
     tpo.index = j;
     tpo.pos = tp.m_iBidiPos;
     tpos->push_back(tpo);
@@ -608,7 +587,7 @@
   std::sort(tpos->begin(), tpos->end());
   int32_t iStartPos = m_pCurLine->m_iStart;
   for (const auto& it : *tpos) {
-    CFX_RTFPiece& ttp = pCurPieces->GetAt(it.index);
+    CFX_RTFPiece& ttp = m_pCurLine->m_LinePieces.GetAt(it.index);
     ttp.m_iStartPos = iStartPos;
     iStartPos += ttp.m_iWidth;
   }
@@ -617,35 +596,28 @@
 void CFX_RTFBreak::EndBreak_Alignment(const std::deque<FX_TPO>& tpos,
                                       bool bAllChars,
                                       CFX_RTFBreakType dwStatus) {
-  CFX_RTFPieceArray* pCurPieces = &m_pCurLine->m_LinePieces;
   int32_t iNetWidth = m_pCurLine->m_iWidth;
   int32_t iGapChars = 0;
-  int32_t iCharWidth;
-  int32_t iCount = pCurPieces->GetSize();
+  int32_t iCount = m_pCurLine->m_LinePieces.GetSize();
   bool bFind = false;
-  uint32_t dwCharType;
-  int32_t i;
-  int32_t j;
-  FX_TPO tpo;
-  for (i = iCount - 1; i > -1; i--) {
-    tpo = tpos[i];
-    CFX_RTFPiece& ttp = pCurPieces->GetAt(tpo.index);
+  for (int32_t i = iCount - 1; i > -1; --i) {
+    CFX_RTFPiece& ttp = m_pCurLine->m_LinePieces.GetAt(tpos[i].index);
     if (!bFind)
       iNetWidth = ttp.GetEndPos();
 
     bool bArabic = FX_IsOdd(ttp.m_iBidiLevel);
-    j = bArabic ? 0 : ttp.m_iChars - 1;
+    int32_t j = bArabic ? 0 : ttp.m_iChars - 1;
     while (j > -1 && j < ttp.m_iChars) {
       const CFX_RTFChar& tc = ttp.GetChar(j);
       if (tc.m_nBreakType == FX_LBT_DIRECT_BRK)
-        iGapChars++;
+        ++iGapChars;
 
       if (!bFind || !bAllChars) {
-        dwCharType = tc.GetCharType();
+        uint32_t dwCharType = tc.GetCharType();
         if (dwCharType == FX_CHARTYPE_Space ||
             dwCharType == FX_CHARTYPE_Control) {
           if (!bFind) {
-            iCharWidth = tc.m_iCharWidth;
+            int32_t iCharWidth = tc.m_iCharWidth;
             if (bAllChars && iCharWidth > 0)
               iNetWidth -= iCharWidth;
           }
@@ -666,15 +638,14 @@
                         (m_iAlignment == CFX_RTFLineAlignment::Justified &&
                          dwStatus != CFX_RTFBreakType::Paragraph))) {
     int32_t iStart = -1;
-    for (i = 0; i < iCount; i++) {
-      tpo = tpos[i];
-      CFX_RTFPiece& ttp = pCurPieces->GetAt(tpo.index);
+    for (int32_t i = 0; i < iCount; ++i) {
+      CFX_RTFPiece& ttp = m_pCurLine->m_LinePieces.GetAt(tpos[i].index);
       if (iStart < 0)
         iStart = ttp.m_iStartPos;
       else
         ttp.m_iStartPos = iStart;
 
-      for (j = 0; j < ttp.m_iChars; j++) {
+      for (int32_t j = 0; j < ttp.m_iChars; ++j) {
         CFX_RTFChar& tc = ttp.GetChar(j);
         if (tc.m_nBreakType != FX_LBT_DIRECT_BRK || tc.m_iCharWidth < 0)
           continue;
@@ -683,7 +654,7 @@
         tc.m_iCharWidth += k;
         ttp.m_iWidth += k;
         iOffset -= k;
-        iGapChars--;
+        --iGapChars;
         if (iGapChars < 1)
           break;
       }
@@ -694,8 +665,8 @@
     if (m_iAlignment == CFX_RTFLineAlignment::Center)
       iOffset /= 2;
     if (iOffset > 0) {
-      for (i = 0; i < iCount; i++) {
-        CFX_RTFPiece& ttp = pCurPieces->GetAt(i);
+      for (int32_t i = 0; i < iCount; ++i) {
+        CFX_RTFPiece& ttp = m_pCurLine->m_LinePieces.GetAt(i);
         ttp.m_iStartPos += iOffset;
       }
     }
@@ -777,7 +748,7 @@
         iEndPos -= iCharWidth;
     }
     nNext = nCodeProp & 0x003F;
-    iLength--;
+    --iLength;
   }
   if (bOnlyBrk)
     return 0;
@@ -811,11 +782,10 @@
   if (iCharPos < 0)
     iCharPos = 0;
 
-  iCharPos++;
+  ++iCharPos;
   if (iCharPos >= iCount) {
     pNextLine->RemoveAll(true);
-    CFX_Char* pTC = &curChars[iCharPos - 1];
-    pTC->m_nBreakType = FX_LBT_UNKNOWN;
+    curChars[iCharPos - 1].m_nBreakType = FX_LBT_UNKNOWN;
     return;
   }
 
@@ -827,7 +797,7 @@
   pCurLine->m_iWidth = iEndPos;
   curChars[iCharPos - 1].m_nBreakType = FX_LBT_UNKNOWN;
 
-  for (size_t i = 0; i < pNextLine->m_LineChars.size(); i++) {
+  for (size_t i = 0; i < pNextLine->m_LineChars.size(); ++i) {
     if (pNextLine->m_LineChars[i].GetCharType() >= FX_CHARTYPE_ArabicAlef) {
       pCurLine->m_iArabicChars--;
       pNextLine->m_iArabicChars++;
@@ -837,30 +807,30 @@
 }
 
 int32_t CFX_RTFBreak::CountBreakPieces() const {
-  const CFX_RTFPieceArray* pRTFPieces = GetRTFPieces();
-  return pRTFPieces ? pRTFPieces->GetSize() : 0;
+  return HasRTFLine() ? m_RTFLine[m_iReady].m_LinePieces.GetSize() : 0;
 }
 
 const CFX_RTFPiece* CFX_RTFBreak::GetBreakPiece(int32_t index) const {
-  const CFX_RTFPieceArray* pRTFPieces = GetRTFPieces();
-  if (!pRTFPieces)
+  if (!HasRTFLine())
     return nullptr;
+
+  const CFX_RTFPieceArray* pRTFPieces = &m_RTFLine[m_iReady].m_LinePieces;
   if (index < 0 || index >= pRTFPieces->GetSize())
     return nullptr;
   return pRTFPieces->GetPtrAt(index);
 }
 
 void CFX_RTFBreak::ClearBreakPieces() {
-  const CFX_RTFLine* pRTFLine = GetRTFLine();
-  if (pRTFLine)
-    const_cast<CFX_RTFLine*>(pRTFLine)->RemoveAll(true);
-  m_iReady = 0;
+  if (HasRTFLine())
+    m_RTFLine[m_iReady].RemoveAll(true);
+
+  m_iReady = -1;
 }
 
 void CFX_RTFBreak::Reset() {
   m_eCharType = FX_CHARTYPE_Unknown;
-  m_RTFLine1.RemoveAll(true);
-  m_RTFLine2.RemoveAll(true);
+  m_RTFLine[0].RemoveAll(true);
+  m_RTFLine[1].RemoveAll(true);
 }
 
 int32_t CFX_RTFBreak::GetDisplayPos(const FX_RTFTEXTOBJ* pText,
@@ -882,48 +852,36 @@
   FX_FLOAT fFontHeight = fFontSize;
   FX_FLOAT fAscent = fFontHeight * static_cast<FX_FLOAT>(iAscent) /
                      static_cast<FX_FLOAT>(iMaxHeight);
-  FX_WCHAR wch;
   FX_WCHAR wPrev = 0xFEFF;
   FX_WCHAR wNext;
-  FX_WCHAR wForm;
-  int32_t iWidth;
-  int32_t iCharWidth;
-  int32_t iCharHeight;
   FX_FLOAT fX = rtText.left;
-  FX_FLOAT fY = rtText.top;
-  FX_FLOAT fCharWidth;
-  FX_FLOAT fCharHeight;
   int32_t iHorScale = pText->iHorizontalScale;
   int32_t iVerScale = pText->iVerticalScale;
-  bool bEmptyChar;
-  uint32_t dwProps;
-  uint32_t dwCharType;
-
   if (bRTLPiece)
     fX = rtText.right();
 
-  fY += fAscent;
+  FX_FLOAT fY = rtText.top + fAscent;
   int32_t iCount = 0;
-  for (int32_t i = 0; i < pText->iLength; i++) {
-    wch = pText->pStr[i];
-    iWidth = pText->pWidths[i];
-    dwProps = FX_GetUnicodeProperties(wch);
-    dwCharType = (dwProps & FX_CHARTYPEBITSMASK);
+  for (int32_t i = 0; i < pText->iLength; ++i) {
+    FX_WCHAR wch = pText->pStr[i];
+    int32_t iWidth = pText->pWidths[i];
+    uint32_t dwProps = FX_GetUnicodeProperties(wch);
+    uint32_t dwCharType = (dwProps & FX_CHARTYPEBITSMASK);
     if (iWidth == 0) {
       if (dwCharType == FX_CHARTYPE_ArabicAlef)
         wPrev = 0xFEFF;
       continue;
     }
 
-    iCharWidth = FXSYS_abs(iWidth);
-    bEmptyChar =
+    int32_t iCharWidth = FXSYS_abs(iWidth);
+    bool bEmptyChar =
         (dwCharType >= FX_CHARTYPE_Tab && dwCharType <= FX_CHARTYPE_Control);
     if (!bEmptyChar)
-      iCount++;
+      ++iCount;
 
     if (pCharPos) {
       iCharWidth /= iFontSize;
-      wForm = wch;
+      FX_WCHAR wForm = wch;
       if (dwCharType >= FX_CHARTYPE_ArabicAlef) {
         if (i + 1 < pText->iLength) {
           wNext = pText->pStr[i + 1];
@@ -951,10 +909,8 @@
 #endif
         pCharPos->m_FontCharWidth = iCharWidth;
       }
-      iCharHeight = 1000;
 
-      fCharWidth = fFontSize * iCharWidth / 1000.0f;
-      fCharHeight = fFontSize * iCharHeight / 1000.0f;
+      FX_FLOAT fCharWidth = fFontSize * iCharWidth / 1000.0f;
       if (bRTLPiece && dwCharType != FX_CHARTYPE_Combination)
         fX -= fCharWidth;
 
@@ -982,7 +938,7 @@
           pCharPos->m_AdjustMatrix[3] =
               pCharPos->m_AdjustMatrix[3] * iVerScale / 100.0f;
         }
-        pCharPos++;
+        ++pCharPos;
       }
     }
     if (iWidth > 0)
diff --git a/xfa/fgas/layout/fgas_rtfbreak.h b/xfa/fgas/layout/fgas_rtfbreak.h
index 8907871..835430e 100644
--- a/xfa/fgas/layout/fgas_rtfbreak.h
+++ b/xfa/fgas/layout/fgas_rtfbreak.h
@@ -179,8 +179,7 @@
   void FontChanged();
   void SetBreakStatus();
   CFX_RTFChar* GetLastChar(int32_t index) const;
-  const CFX_RTFLine* GetRTFLine() const;
-  const CFX_RTFPieceArray* GetRTFPieces() const;
+  bool HasRTFLine() const { return m_iReady >= 0; }
   FX_CHARTYPE GetUnifiedCharType(FX_CHARTYPE chartype) const;
   int32_t GetLastPositionedTab() const;
   bool GetPositionedTab(int32_t* iTabPos) const;
@@ -219,11 +218,10 @@
   CFX_RetainPtr<CFX_Retainable> m_pUserData;
   FX_CHARTYPE m_eCharType;
   uint32_t m_dwIdentity;
-  CFX_RTFLine m_RTFLine1;
-  CFX_RTFLine m_RTFLine2;
+  CFX_RTFLine m_RTFLine[2];
   CFX_RTFLine* m_pCurLine;
-  int32_t m_iReady;
   int32_t m_iTolerance;
+  int8_t m_iReady;
 };
 
 #endif  // XFA_FGAS_LAYOUT_FGAS_RTFBREAK_H_