Clean up CFX_RTFBreak::GetDisplayPos() and its caller.

- Remove useless parameter and dead code.
- Return size_t.
- Add CFX_TextPiece and make CXFA_TextPiece a subclass.
- Remove FX_RTFTEXTOBJ and just use CFX_TextPiece.

Change-Id: I14ff986600991a8814c6e1274c345d33aee67407
Reviewed-on: https://pdfium-review.googlesource.com/c/50133
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/xfa/fgas/layout/BUILD.gn b/xfa/fgas/layout/BUILD.gn
index ba0b40c..6c170d7 100644
--- a/xfa/fgas/layout/BUILD.gn
+++ b/xfa/fgas/layout/BUILD.gn
@@ -22,6 +22,8 @@
     "cfx_linkuserdata.h",
     "cfx_rtfbreak.cpp",
     "cfx_rtfbreak.h",
+    "cfx_textpiece.cpp",
+    "cfx_textpiece.h",
     "cfx_textuserdata.cpp",
     "cfx_textuserdata.h",
     "cfx_txtbreak.cpp",
diff --git a/xfa/fgas/layout/cfx_rtfbreak.cpp b/xfa/fgas/layout/cfx_rtfbreak.cpp
index 776a2fe..5a86528 100644
--- a/xfa/fgas/layout/cfx_rtfbreak.cpp
+++ b/xfa/fgas/layout/cfx_rtfbreak.cpp
@@ -13,6 +13,7 @@
 #include "third_party/base/stl_util.h"
 #include "xfa/fgas/font/cfgas_gefont.h"
 #include "xfa/fgas/layout/cfx_char.h"
+#include "xfa/fgas/layout/cfx_textpiece.h"
 #include "xfa/fgas/layout/cfx_textuserdata.h"
 #include "xfa/fgas/layout/fx_arabic.h"
 #include "xfa/fgas/layout/fx_linebreak.h"
@@ -716,19 +717,15 @@
   }
 }
 
-int32_t CFX_RTFBreak::GetDisplayPos(const FX_RTFTEXTOBJ* pText,
-                                    TextCharPos* pCharPos,
-                                    bool bCharCode) const {
-  if (!pText || pText->iLength < 1)
-    return 0;
+size_t CFX_RTFBreak::GetDisplayPos(const CFX_TextPiece* pPiece,
+                                   TextCharPos* pCharPos) const {
+  ASSERT(pPiece->iChars > 0);
+  ASSERT(pPiece->pFont);
 
-  ASSERT(pText->pFont);
-  ASSERT(pText->pRect);
-
-  RetainPtr<CFGAS_GEFont> pFont = pText->pFont;
-  CFX_RectF rtText(*pText->pRect);
-  bool bRTLPiece = FX_IsOdd(pText->iBidiLevel);
-  float fFontSize = pText->fFontSize;
+  RetainPtr<CFGAS_GEFont> pFont = pPiece->pFont;
+  CFX_RectF rtText(pPiece->rtPiece);
+  bool bRTLPiece = FX_IsOdd(pPiece->iBidiLevel);
+  float fFontSize = pPiece->fFontSize;
   int32_t iFontSize = FXSYS_round(fFontSize * 20.0f);
   if (iFontSize == 0)
     return 0;
@@ -742,16 +739,16 @@
   wchar_t wPrev = 0xFEFF;
   wchar_t wNext;
   float fX = rtText.left;
-  int32_t iHorScale = pText->iHorizontalScale;
-  int32_t iVerScale = pText->iVerticalScale;
+  int32_t iHorScale = pPiece->iHorScale;
+  int32_t iVerScale = pPiece->iVerScale;
   if (bRTLPiece)
     fX = rtText.right();
 
   float fY = rtText.top + fAscent;
-  int32_t iCount = 0;
-  for (int32_t i = 0; i < pText->iLength; ++i) {
-    wchar_t wch = pText->pStr[i];
-    int32_t iWidth = pText->pWidths[i];
+  size_t szCount = 0;
+  for (int32_t i = 0; i < pPiece->iChars; ++i) {
+    wchar_t wch = pPiece->szText[i];
+    int32_t iWidth = pPiece->Widths[i];
     FX_CHARTYPE dwCharType = FX_GetCharType(wch);
     if (iWidth == 0) {
       if (dwCharType == FX_CHARTYPE::kArabicAlef)
@@ -763,16 +760,16 @@
     bool bEmptyChar = (dwCharType >= FX_CHARTYPE::kTab &&
                        dwCharType <= FX_CHARTYPE::kControl);
     if (!bEmptyChar)
-      ++iCount;
+      ++szCount;
 
     if (pCharPos) {
       iCharWidth /= iFontSize;
       wchar_t wForm = wch;
       if (dwCharType >= FX_CHARTYPE::kArabicAlef) {
-        if (i + 1 < pText->iLength) {
-          wNext = pText->pStr[i + 1];
-          if (pText->pWidths[i + 1] < 0 && i + 2 < pText->iLength)
-            wNext = pText->pStr[i + 2];
+        if (i + 1 < pPiece->iChars) {
+          wNext = pPiece->szText[i + 1];
+          if (pPiece->Widths[i + 1] < 0 && i + 2 < pPiece->iChars)
+            wNext = pPiece->szText[i + 2];
         } else {
           wNext = 0xFEFF;
         }
@@ -782,13 +779,9 @@
       }
 
       if (!bEmptyChar) {
-        if (bCharCode) {
-          pCharPos->m_GlyphIndex = wch;
-        } else {
-          pCharPos->m_GlyphIndex = pFont->GetGlyphIndex(wForm);
-          if (pCharPos->m_GlyphIndex == 0xFFFF)
-            pCharPos->m_GlyphIndex = pFont->GetGlyphIndex(wch);
-        }
+        pCharPos->m_GlyphIndex = pFont->GetGlyphIndex(wForm);
+        if (pCharPos->m_GlyphIndex == 0xFFFF)
+          pCharPos->m_GlyphIndex = pFont->GetGlyphIndex(wch);
 #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
         pCharPos->m_ExtGID = pCharPos->m_GlyphIndex;
 #endif
@@ -829,17 +822,5 @@
     if (iWidth > 0)
       wPrev = wch;
   }
-  return iCount;
+  return szCount;
 }
-
-FX_RTFTEXTOBJ::FX_RTFTEXTOBJ()
-    : pFont(nullptr),
-      pRect(nullptr),
-      wLineBreakChar(L'\n'),
-      fFontSize(12.0f),
-      iLength(0),
-      iBidiLevel(0),
-      iHorizontalScale(100),
-      iVerticalScale(100) {}
-
-FX_RTFTEXTOBJ::~FX_RTFTEXTOBJ() {}
diff --git a/xfa/fgas/layout/cfx_rtfbreak.h b/xfa/fgas/layout/cfx_rtfbreak.h
index 2f09d99..ef24204 100644
--- a/xfa/fgas/layout/cfx_rtfbreak.h
+++ b/xfa/fgas/layout/cfx_rtfbreak.h
@@ -17,6 +17,7 @@
 
 class CFGAS_GEFont;
 class CFX_TextUserData;
+class CFX_TextPiece;
 class TextCharPos;
 
 enum class CFX_RTFLineAlignment {
@@ -27,22 +28,6 @@
   Distributed
 };
 
-struct FX_RTFTEXTOBJ {
-  FX_RTFTEXTOBJ();
-  ~FX_RTFTEXTOBJ();
-
-  WideString pStr;
-  std::vector<int32_t> pWidths;
-  RetainPtr<CFGAS_GEFont> pFont;
-  const CFX_RectF* pRect;
-  wchar_t wLineBreakChar;
-  float fFontSize;
-  int32_t iLength;
-  int32_t iBidiLevel;
-  int32_t iHorizontalScale;
-  int32_t iVerticalScale;
-};
-
 class CFX_RTFBreak final : public CFX_Break {
  public:
   explicit CFX_RTFBreak(uint32_t dwLayoutStyles);
@@ -57,9 +42,8 @@
 
   CFX_BreakType EndBreak(CFX_BreakType dwStatus);
 
-  int32_t GetDisplayPos(const FX_RTFTEXTOBJ* pText,
-                        TextCharPos* pCharPos,
-                        bool bCharCode) const;
+  size_t GetDisplayPos(const CFX_TextPiece* pPiece,
+                       TextCharPos* pCharPos) const;
 
   CFX_BreakType AppendChar(wchar_t wch);
 
diff --git a/xfa/fgas/layout/cfx_textpiece.cpp b/xfa/fgas/layout/cfx_textpiece.cpp
new file mode 100644
index 0000000..06f6cd6
--- /dev/null
+++ b/xfa/fgas/layout/cfx_textpiece.cpp
@@ -0,0 +1,13 @@
+// Copyright 2019 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "xfa/fgas/layout/cfx_textpiece.h"
+
+#include "xfa/fgas/font/cfgas_gefont.h"
+
+CFX_TextPiece::CFX_TextPiece() = default;
+
+CFX_TextPiece::~CFX_TextPiece() = default;
diff --git a/xfa/fgas/layout/cfx_textpiece.h b/xfa/fgas/layout/cfx_textpiece.h
new file mode 100644
index 0000000..409d200
--- /dev/null
+++ b/xfa/fgas/layout/cfx_textpiece.h
@@ -0,0 +1,34 @@
+// Copyright 2019 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef XFA_FGAS_LAYOUT_CFX_TEXTPIECE_H_
+#define XFA_FGAS_LAYOUT_CFX_TEXTPIECE_H_
+
+#include <vector>
+
+#include "core/fxcrt/fx_coordinates.h"
+#include "core/fxcrt/fx_string.h"
+#include "core/fxcrt/retain_ptr.h"
+
+class CFGAS_GEFont;
+
+class CFX_TextPiece {
+ public:
+  CFX_TextPiece();
+  ~CFX_TextPiece();
+
+  WideString szText;
+  std::vector<int32_t> Widths;
+  int32_t iChars;
+  int32_t iHorScale;
+  int32_t iVerScale;
+  int32_t iBidiLevel;
+  float fFontSize;
+  CFX_RectF rtPiece;
+  RetainPtr<CFGAS_GEFont> pFont;
+};
+
+#endif  // XFA_FGAS_LAYOUT_CFX_TEXTPIECE_H_
diff --git a/xfa/fxfa/cxfa_textlayout.cpp b/xfa/fxfa/cxfa_textlayout.cpp
index 2d6bce9..4a942bc 100644
--- a/xfa/fxfa/cxfa_textlayout.cpp
+++ b/xfa/fxfa/cxfa_textlayout.cpp
@@ -1258,30 +1258,7 @@
 
 size_t CXFA_TextLayout::GetDisplayPos(const CXFA_TextPiece* pPiece,
                                       TextCharPos* pCharPos) {
-  if (!pPiece)
+  if (!pPiece || pPiece->iChars < 1)
     return 0;
-
-  FX_RTFTEXTOBJ tr;
-  if (!ToRun(pPiece, &tr))
-    return 0;
-
-  return m_pBreak->GetDisplayPos(&tr, pCharPos, false);
-}
-
-bool CXFA_TextLayout::ToRun(const CXFA_TextPiece* pPiece, FX_RTFTEXTOBJ* tr) {
-  int32_t iLength = pPiece->iChars;
-  if (iLength < 1)
-    return false;
-
-  tr->pStr = pPiece->szText;
-  tr->pFont = pPiece->pFont;
-  tr->pRect = &pPiece->rtPiece;
-  tr->pWidths = pPiece->Widths;
-  tr->iLength = iLength;
-  tr->fFontSize = pPiece->fFontSize;
-  tr->iBidiLevel = pPiece->iBidiLevel;
-  tr->wLineBreakChar = L'\n';
-  tr->iVerticalScale = pPiece->iVerScale;
-  tr->iHorizontalScale = pPiece->iHorScale;
-  return true;
+  return m_pBreak->GetDisplayPos(pPiece, pCharPos);
 }
diff --git a/xfa/fxfa/cxfa_textlayout.h b/xfa/fxfa/cxfa_textlayout.h
index b28be42..847c98e 100644
--- a/xfa/fxfa/cxfa_textlayout.h
+++ b/xfa/fxfa/cxfa_textlayout.h
@@ -112,7 +112,6 @@
                   TextCharPos* pCharPos,
                   const CFX_Matrix& tmDoc2Device);
   size_t GetDisplayPos(const CXFA_TextPiece* pPiece, TextCharPos* pCharPos);
-  bool ToRun(const CXFA_TextPiece* pPiece, FX_RTFTEXTOBJ* tr);
   void DoTabstops(CFX_CSSComputedStyle* pStyle, CXFA_PieceLine* pPieceLine);
   bool LayoutInternal(size_t szBlockIndex);
   size_t CountBlocks() const;
diff --git a/xfa/fxfa/cxfa_textpiece.cpp b/xfa/fxfa/cxfa_textpiece.cpp
index 166bb51..363361a 100644
--- a/xfa/fxfa/cxfa_textpiece.cpp
+++ b/xfa/fxfa/cxfa_textpiece.cpp
@@ -6,9 +6,8 @@
 
 #include "xfa/fxfa/cxfa_textpiece.h"
 
-#include "xfa/fgas/font/cfgas_gefont.h"
 #include "xfa/fgas/layout/cfx_linkuserdata.h"
 
-CXFA_TextPiece::CXFA_TextPiece() {}
+CXFA_TextPiece::CXFA_TextPiece() = default;
 
-CXFA_TextPiece::~CXFA_TextPiece() {}
+CXFA_TextPiece::~CXFA_TextPiece() = default;
diff --git a/xfa/fxfa/cxfa_textpiece.h b/xfa/fxfa/cxfa_textpiece.h
index d09d066..c9d524d 100644
--- a/xfa/fxfa/cxfa_textpiece.h
+++ b/xfa/fxfa/cxfa_textpiece.h
@@ -7,34 +7,22 @@
 #ifndef XFA_FXFA_CXFA_TEXTPIECE_H_
 #define XFA_FXFA_CXFA_TEXTPIECE_H_
 
-#include <vector>
-
-#include "core/fxcrt/fx_coordinates.h"
-#include "core/fxcrt/fx_string.h"
+#include "core/fxcrt/retain_ptr.h"
 #include "core/fxge/fx_dib.h"
+#include "xfa/fgas/layout/cfx_textpiece.h"
 #include "xfa/fxfa/fxfa_basic.h"
 
-class CFGAS_GEFont;
 class CFX_LinkUserData;
 
-class CXFA_TextPiece {
+class CXFA_TextPiece : public CFX_TextPiece {
  public:
   CXFA_TextPiece();
   ~CXFA_TextPiece();
 
-  WideString szText;
-  std::vector<int32_t> Widths;
-  int32_t iChars;
-  int32_t iHorScale;
-  int32_t iVerScale;
-  int32_t iBidiLevel;
   int32_t iUnderline;
-  XFA_AttributeValue iPeriod;
   int32_t iLineThrough;
+  XFA_AttributeValue iPeriod;
   FX_ARGB dwColor;
-  float fFontSize;
-  CFX_RectF rtPiece;
-  RetainPtr<CFGAS_GEFont> pFont;
   RetainPtr<CFX_LinkUserData> pLinkData;
 };