Avoid overloading CPDF_TextPage::ProcessTextObject()

CPDF_TextPage has two ProcessTextObject() methods that do different
things. Take the one not related to the other "Process" methods and
rename it to ProcessTransformedTextObjects() to give it a distinct name.
Then change it to process all `text_objects_` elements directly, instead
of making its caller do that.

Change-Id: Ide6e4905207a34ae24c3d62379fb5ec0947c4d37
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/153831
Reviewed-by: Andy Phan <andyphan@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdftext/cpdf_textpage.cpp b/core/fpdftext/cpdf_textpage.cpp
index 7df95e5..207c8ed 100644
--- a/core/fpdftext/cpdf_textpage.cpp
+++ b/core/fpdftext/cpdf_textpage.cpp
@@ -757,10 +757,7 @@
       ProcessFormObject(page_obj->AsForm(), CFX_Matrix());
     }
   }
-  for (const auto& obj : text_objects_) {
-    ProcessTextObject(obj);
-  }
-
+  ProcessTransformedTextObjects();
   text_objects_.clear();
   CloseTempLine();
 }
@@ -884,11 +881,10 @@
     return;
   }
 
-  size_t count = text_objects_.size();
   TransformedTextObject new_obj;
   new_obj.text_obj_ = text_obj;
   new_obj.form_matrix_ = form_matrix;
-  if (count == 0) {
+  if (text_objects_.empty()) {
     text_objects_.push_back(new_obj);
     return;
   }
@@ -896,7 +892,8 @@
     return;
   }
 
-  TransformedTextObject prev_obj = text_objects_[count - 1];
+  // Explicitly copy in case `text_objects_` gets cleared.
+  TransformedTextObject prev_obj = text_objects_.back();
   size_t nItem = prev_obj.text_obj_->CountItems();
   if (nItem == 0) {
     return;
@@ -924,15 +921,13 @@
   CFX_PointF this_pos =
       display_matrix_.Transform(form_matrix.Transform(text_obj->GetPos()));
   if (fabs(this_pos.y - prev_pos.y) > threshold * 2) {
-    for (size_t i = 0; i < count; ++i) {
-      ProcessTextObject(text_objects_[i]);
-    }
+    ProcessTransformedTextObjects();
     text_objects_.clear();
     text_objects_.push_back(new_obj);
     return;
   }
 
-  for (size_t i = count; i > 0; --i) {
+  for (size_t i = text_objects_.size(); i > 0; --i) {
     TransformedTextObject prev_text_obj = text_objects_[i - 1];
     CFX_PointF new_prev_pos =
         display_matrix_.Transform(prev_text_obj.form_matrix_.Transform(
@@ -1074,52 +1069,54 @@
   }
 }
 
-void CPDF_TextPage::ProcessTextObject(const TransformedTextObject& obj) {
-  CPDF_TextObject* const text_obj = obj.text_obj_;
-  if (fabs(text_obj->GetRect().Width()) < kSizeEpsilon) {
-    return;
-  }
+void CPDF_TextPage::ProcessTransformedTextObjects() {
+  for (const TransformedTextObject& obj : text_objects_) {
+    CPDF_TextObject* const text_obj = obj.text_obj_;
+    if (fabs(text_obj->GetRect().Width()) < kSizeEpsilon) {
+      continue;
+    }
 
-  const CFX_Matrix form_matrix = obj.form_matrix_;
-  const MarkedContentState ePreMKC = PreMarkedContent(text_obj);
-  if (ePreMKC == MarkedContentState::kDone) {
-    prev_text_obj_ = text_obj;
-    prev_matrix_ = form_matrix;
-    return;
-  }
+    const CFX_Matrix& form_matrix = obj.form_matrix_;
+    const MarkedContentState ePreMKC = PreMarkedContent(text_obj);
+    if (ePreMKC == MarkedContentState::kDone) {
+      prev_text_obj_ = text_obj;
+      prev_matrix_ = form_matrix;
+      continue;
+    }
 
-  if (prev_text_obj_) {
-    GenerateCharacter type = ProcessInsertObject(text_obj, form_matrix);
-    if (type == GenerateCharacter::kLineBreak) {
-      curline_rect_ = text_obj->GetRect();
+    if (prev_text_obj_) {
+      GenerateCharacter type = ProcessInsertObject(text_obj, form_matrix);
+      if (type == GenerateCharacter::kLineBreak) {
+        curline_rect_ = text_obj->GetRect();
+      } else {
+        curline_rect_.Union(text_obj->GetRect());
+      }
+
+      if (!ProcessGenerateCharacter(type, text_obj, form_matrix)) {
+        continue;
+      }
     } else {
-      curline_rect_.Union(text_obj->GetRect());
+      curline_rect_ = text_obj->GetRect();
     }
 
-    if (!ProcessGenerateCharacter(type, text_obj, form_matrix)) {
-      return;
+    if (ePreMKC == MarkedContentState::kDelay) {
+      ProcessMarkedContent(obj);
+      prev_text_obj_ = text_obj;
+      prev_matrix_ = form_matrix;
+      continue;
     }
-  } else {
-    curline_rect_ = text_obj->GetRect();
-  }
 
-  if (ePreMKC == MarkedContentState::kDelay) {
-    ProcessMarkedContent(obj);
     prev_text_obj_ = text_obj;
     prev_matrix_ = form_matrix;
-    return;
-  }
 
-  prev_text_obj_ = text_obj;
-  prev_matrix_ = form_matrix;
+    const CFX_Matrix matrix = text_obj->GetTextMatrix() * form_matrix;
+    // Save these before ProcessTextObjectItems() modifies the containers.
+    const size_t orig_char_list_index = temp_char_list_.size();
+    const size_t orig_buf_index = temp_text_buf_.GetLength();
 
-  const CFX_Matrix matrix = text_obj->GetTextMatrix() * form_matrix;
-  // Save these before ProcessTextObjectItems() modifies the containers.
-  const size_t orig_char_list_index = temp_char_list_.size();
-  const size_t orig_buf_index = temp_text_buf_.GetLength();
-
-  if (ProcessTextObjectItems(text_obj, form_matrix, matrix)) {
-    ReverseTempTextBufs(orig_char_list_index, orig_buf_index);
+    if (ProcessTextObjectItems(text_obj, form_matrix, matrix)) {
+      ReverseTempTextBufs(orig_char_list_index, orig_buf_index);
+    }
   }
 }
 
diff --git a/core/fpdftext/cpdf_textpage.h b/core/fpdftext/cpdf_textpage.h
index 5059a40..e1127d4 100644
--- a/core/fpdftext/cpdf_textpage.h
+++ b/core/fpdftext/cpdf_textpage.h
@@ -141,13 +141,15 @@
   void ProcessObject();
   void ProcessFormObject(CPDF_FormObject* form_obj,
                          const CFX_Matrix& form_matrix);
-  void ProcessTextObject(const TransformedTextObject& obj);
   void ProcessTextObject(CPDF_TextObject* text_obj,
                          const CFX_Matrix& form_matrix,
                          const CPDF_PageObjectHolder* obj_list,
                          CPDF_PageObjectHolder::const_iterator obj_iter);
   GenerateCharacter ProcessInsertObject(const CPDF_TextObject* text_obj,
                                         const CFX_Matrix& form_matrix);
+
+  void ProcessTransformedTextObjects();
+
   // Returns whether to continue or not.
   bool ProcessGenerateCharacter(GenerateCharacter type,
                                 const CPDF_TextObject* text_object,