Clean up CXFA_TextLayout.

- Encapsulate member variables.
- Use early returns / continues.
- Add a constant.

Change-Id: I3c5031c99329ccaba3ce8cbc90e1bfc97efe944a
Reviewed-on: https://pdfium-review.googlesource.com/c/49490
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/xfa/fxfa/cxfa_fftext.cpp b/xfa/fxfa/cxfa_fftext.cpp
index 3012ab9..cca6cc0 100644
--- a/xfa/fxfa/cxfa_fftext.cpp
+++ b/xfa/fxfa/cxfa_fftext.cpp
@@ -66,7 +66,7 @@
 
 bool CXFA_FFText::IsLoaded() {
   CXFA_TextLayout* pTextLayout = m_pNode->GetTextLayout();
-  return pTextLayout && !pTextLayout->m_bHasBlock;
+  return pTextLayout && !pTextLayout->HasBlock();
 }
 
 bool CXFA_FFText::PerformLayout() {
@@ -74,10 +74,10 @@
   CXFA_TextLayout* pTextLayout = m_pNode->GetTextLayout();
   if (!pTextLayout)
     return false;
-  if (!pTextLayout->m_bHasBlock)
+  if (!pTextLayout->HasBlock())
     return true;
 
-  pTextLayout->m_Blocks.clear();
+  pTextLayout->ClearBlocks();
   CXFA_ContentLayoutItem* pItem = this;
   if (!pItem->GetPrev() && !pItem->GetNext())
     return true;
@@ -95,7 +95,7 @@
     pTextLayout->ItemBlocks(rtText, pItem->GetIndex());
     pItem = pItem->GetNext();
   }
-  pTextLayout->m_bHasBlock = false;
+  pTextLayout->ResetHasBlock();
   return true;
 }
 
diff --git a/xfa/fxfa/cxfa_textlayout.cpp b/xfa/fxfa/cxfa_textlayout.cpp
index 0ba64a5..1b0d9c7 100644
--- a/xfa/fxfa/cxfa_textlayout.cpp
+++ b/xfa/fxfa/cxfa_textlayout.cpp
@@ -36,6 +36,8 @@
 
 namespace {
 
+constexpr float kHeightTolerance = 0.001f;
+
 void ProcessText(WideString* pText) {
   int32_t iLen = pText->GetLength();
   if (iLen == 0)
@@ -79,9 +81,6 @@
 }
 
 void CXFA_TextLayout::GetTextDataNode() {
-  if (!m_pTextProvider)
-    return;
-
   CXFA_Node* pNode = m_pTextProvider->GetTextNode(&m_bRichText);
   if (pNode && m_bRichText)
     m_textParser.Reset();
@@ -358,36 +357,44 @@
   }
 
   int32_t iCount = pdfium::CollectionSize<int32_t>(m_pLoader->lineHeights);
-  int32_t i = 0;
-  for (i = iLineIndex; i < iCount; i++) {
-    float fLineHeight = m_pLoader->lineHeights[i];
-    if (i == iLineIndex && fLineHeight - fContentAreaHeight > 0.001)
-      return 0;
+  if (iLineIndex < 0 || iLineIndex >= iCount)
+    return fCalcHeight;
 
-    if (fLinePos + fLineHeight - fContentAreaHeight > 0.001) {
-      if (iBlockCount >= (iBlockIndex + 1) * 2) {
-        m_Blocks[iBlockIndex * 2] = iLineIndex;
-        m_Blocks[iBlockIndex * 2 + 1] = i - iLineIndex;
-      } else {
-        m_Blocks.push_back(iLineIndex);
-        m_Blocks.push_back(i - iLineIndex);
-      }
-      if (i == iLineIndex) {
-        if (fCalcHeight <= fLinePos) {
-          if (pdfium::CollectionSize<int32_t>(m_pLoader->blocksHeight) >
-                  iBlockIndex * 2 &&
-              (m_pLoader->blocksHeight[iBlockIndex * 2] == iBlockIndex)) {
-            m_pLoader->blocksHeight[iBlockIndex * 2 + 1] = fCalcHeight;
-          } else {
-            m_pLoader->blocksHeight.push_back(iBlockIndex);
-            m_pLoader->blocksHeight.push_back(fCalcHeight);
-          }
-        }
-        return fCalcHeight;
-      }
-      return fLinePos;
+  if (m_pLoader->lineHeights[iLineIndex] - fContentAreaHeight >
+      kHeightTolerance) {
+    return 0;
+  }
+
+  for (int32_t i = iLineIndex; i < iCount; ++i) {
+    float fLineHeight = m_pLoader->lineHeights[i];
+    if (fLinePos + fLineHeight - fContentAreaHeight <= kHeightTolerance) {
+      fLinePos += fLineHeight;
+      continue;
     }
-    fLinePos += fLineHeight;
+
+    if (iBlockCount >= (iBlockIndex + 1) * 2) {
+      m_Blocks[iBlockIndex * 2] = iLineIndex;
+      m_Blocks[iBlockIndex * 2 + 1] = i - iLineIndex;
+    } else {
+      m_Blocks.push_back(iLineIndex);
+      m_Blocks.push_back(i - iLineIndex);
+    }
+
+    if (i != iLineIndex)
+      return fLinePos;
+
+    if (fCalcHeight > fLinePos)
+      return fCalcHeight;
+
+    if (pdfium::CollectionSize<int32_t>(m_pLoader->blocksHeight) >
+            iBlockIndex * 2 &&
+        (m_pLoader->blocksHeight[iBlockIndex * 2] == iBlockIndex)) {
+      m_pLoader->blocksHeight[iBlockIndex * 2 + 1] = fCalcHeight;
+    } else {
+      m_pLoader->blocksHeight.push_back(iBlockIndex);
+      m_pLoader->blocksHeight.push_back(fCalcHeight);
+    }
+    return fCalcHeight;
   }
   return fCalcHeight;
 }
@@ -552,7 +559,7 @@
   int32_t i = 0;
   for (i = iLineIndex; i < iCountHeight; i++) {
     float fLineHeight = m_pLoader->lineHeights[i];
-    if (fLinePos + fLineHeight - rtText.height > 0.001) {
+    if (fLinePos + fLineHeight - rtText.height > kHeightTolerance) {
       m_Blocks.push_back(iLineIndex);
       m_Blocks.push_back(i - iLineIndex);
       bEndItem = false;
diff --git a/xfa/fxfa/cxfa_textlayout.h b/xfa/fxfa/cxfa_textlayout.h
index 2deac8a..f6ca196 100644
--- a/xfa/fxfa/cxfa_textlayout.h
+++ b/xfa/fxfa/cxfa_textlayout.h
@@ -52,12 +52,14 @@
                   int32_t iBlock);
   bool IsLoaded() const { return !m_pieceLines.empty(); }
   void Unload();
+
   const std::vector<std::unique_ptr<CXFA_PieceLine>>* GetPieceLines() const {
     return &m_pieceLines;
   }
 
-  bool m_bHasBlock = false;
-  std::vector<int32_t> m_Blocks;
+  bool HasBlock() const { return m_bHasBlock; }
+  void ClearBlocks() { m_Blocks.clear(); }
+  void ResetHasBlock() { m_bHasBlock = false; }
 
  private:
   void GetTextDataNode();
@@ -110,10 +112,12 @@
   bool Layout(int32_t iBlock);
   int32_t CountBlocks() const;
 
+  bool m_bHasBlock = false;
   bool m_bRichText = false;
   bool m_bBlockContinue = true;
   int32_t m_iLines = 0;
   float m_fMaxWidth = 0;
+  std::vector<int32_t> m_Blocks;
   UnownedPtr<CXFA_FFDoc> const m_pDoc;
   CXFA_TextProvider* const m_pTextProvider;  // Raw, owned by tree node.
   CXFA_Node* m_pTextDataNode = nullptr;      // Raw, owned by tree node.