Revert "Use DataVector in CFX_FontMapper and CFX_FontMgr."

This reverts commit 530f0500f9e89967d01e84d6c6fd70f1015a24ca.

Reason for revert: Regressed performance.

Original change's description:
> Use DataVector in CFX_FontMapper and CFX_FontMgr.
>
> Also removed the now unused CFX_MemoryStream ctor.
>
> Bug: pdfium:1872
> Change-Id: I74b4f0ce6fd8de4816ec7876c056bc82df3020a5
> Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/97691
> Reviewed-by: Tom Sepez <tsepez@chromium.org>
> Commit-Queue: Lei Zhang <thestig@chromium.org>

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: pdfium:1872
Change-Id: I59c88ccf25b68b1a970f493e775a788c118a6f84
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/97853
Bot-Commit: rubber-stamper@appspot.gserviceaccount.com <rubber-stamper@appspot.gserviceaccount.com>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/cfx_memorystream.cpp b/core/fxcrt/cfx_memorystream.cpp
index 9c89bc7..b1f131e 100644
--- a/core/fxcrt/cfx_memorystream.cpp
+++ b/core/fxcrt/cfx_memorystream.cpp
@@ -11,7 +11,12 @@
 
 #include "core/fxcrt/fx_safe_types.h"
 
-CFX_MemoryStream::CFX_MemoryStream() = default;
+CFX_MemoryStream::CFX_MemoryStream() : m_nTotalSize(0), m_nCurSize(0) {}
+
+CFX_MemoryStream::CFX_MemoryStream(
+    std::unique_ptr<uint8_t, FxFreeDeleter> pBuffer,
+    size_t nSize)
+    : m_data(std::move(pBuffer)), m_nTotalSize(nSize), m_nCurSize(nSize) {}
 
 CFX_MemoryStream::~CFX_MemoryStream() = default;
 
diff --git a/core/fxcrt/cfx_memorystream.h b/core/fxcrt/cfx_memorystream.h
index f72a211..ff5ee1d 100644
--- a/core/fxcrt/cfx_memorystream.h
+++ b/core/fxcrt/cfx_memorystream.h
@@ -34,11 +34,13 @@
 
  private:
   CFX_MemoryStream();
+  CFX_MemoryStream(std::unique_ptr<uint8_t, FxFreeDeleter> pBuffer,
+                   size_t nSize);
   ~CFX_MemoryStream() override;
 
   std::unique_ptr<uint8_t, FxFreeDeleter> m_data;
-  size_t m_nTotalSize = 0;
-  size_t m_nCurSize = 0;
+  size_t m_nTotalSize;
+  size_t m_nCurSize;
   size_t m_nCurPos = 0;
 };
 
diff --git a/core/fxge/cfx_fontmapper.cpp b/core/fxge/cfx_fontmapper.cpp
index 82d5ff0..dfd367e 100644
--- a/core/fxge/cfx_fontmapper.cpp
+++ b/core/fxge/cfx_fontmapper.cpp
@@ -793,23 +793,26 @@
 #endif  // BUILDFLAG(IS_WIN)
 
 #ifdef PDF_ENABLE_XFA
-DataVector<uint8_t> CFX_FontMapper::RawBytesForIndex(size_t index) {
+std::unique_ptr<uint8_t, FxFreeDeleter> CFX_FontMapper::RawBytesForIndex(
+    size_t index,
+    size_t* returned_length) {
   CHECK_LT(index, m_FaceArray.size());
 
   void* font_handle = m_pFontInfo->MapFont(0, false, FX_Charset::kDefault, 0,
                                            GetFaceName(index));
   if (!font_handle)
-    return DataVector<uint8_t>();
+    return nullptr;
 
   ScopedFontDeleter scoped_font(m_pFontInfo.get(), font_handle);
   size_t required_size = m_pFontInfo->GetFontData(font_handle, 0, {});
   if (required_size == 0)
-    return DataVector<uint8_t>();
+    return nullptr;
 
-  DataVector<uint8_t> result(required_size);
-  size_t actual_size = m_pFontInfo->GetFontData(font_handle, 0, result);
-  CHECK_EQ(required_size, actual_size);
-  return result;
+  std::unique_ptr<uint8_t, FxFreeDeleter> pBuffer(
+      FX_Alloc(uint8_t, required_size + 1));
+  *returned_length =
+      m_pFontInfo->GetFontData(font_handle, 0, {pBuffer.get(), required_size});
+  return pBuffer;
 }
 #endif  // PDF_ENABLE_XFA
 
@@ -820,10 +823,12 @@
   RetainPtr<CFX_FontMgr::FontDesc> pFontDesc =
       m_pFontMgr->GetCachedTTCFontDesc(ttc_size, checksum);
   if (!pFontDesc) {
-    DataVector<uint8_t> font_data(ttc_size);
-    m_pFontInfo->GetFontData(font_handle, kTableTTCF, font_data);
-    pFontDesc = m_pFontMgr->AddCachedTTCFontDesc(ttc_size, checksum,
-                                                 std::move(font_data));
+    std::unique_ptr<uint8_t, FxFreeDeleter> pFontData(
+        FX_Alloc(uint8_t, ttc_size));
+    m_pFontInfo->GetFontData(font_handle, kTableTTCF,
+                             {pFontData.get(), ttc_size});
+    pFontDesc = m_pFontMgr->AddCachedTTCFontDesc(
+        ttc_size, checksum, std::move(pFontData), ttc_size);
   }
   CHECK(ttc_size >= data_size);
   size_t font_offset = ttc_size - data_size;
@@ -850,10 +855,11 @@
   RetainPtr<CFX_FontMgr::FontDesc> pFontDesc =
       m_pFontMgr->GetCachedFontDesc(subst_name, weight, is_italic);
   if (!pFontDesc) {
-    DataVector<uint8_t> font_data(data_size);
-    m_pFontInfo->GetFontData(font_handle, 0, font_data);
+    std::unique_ptr<uint8_t, FxFreeDeleter> pFontData(
+        FX_Alloc(uint8_t, data_size));
+    m_pFontInfo->GetFontData(font_handle, 0, {pFontData.get(), data_size});
     pFontDesc = m_pFontMgr->AddCachedFontDesc(subst_name, weight, is_italic,
-                                              std::move(font_data));
+                                              std::move(pFontData), data_size);
   }
   RetainPtr<CFX_Face> pFace(pFontDesc->GetFace(0));
   if (pFace)
diff --git a/core/fxge/cfx_fontmapper.h b/core/fxge/cfx_fontmapper.h
index 32b4ca1..e8deaf5 100644
--- a/core/fxge/cfx_fontmapper.h
+++ b/core/fxge/cfx_fontmapper.h
@@ -14,15 +14,12 @@
 #include "build/build_config.h"
 #include "core/fxcrt/bytestring.h"
 #include "core/fxcrt/fx_codepage_forward.h"
+#include "core/fxcrt/fx_memory_wrappers.h"
 #include "core/fxcrt/retain_ptr.h"
 #include "core/fxcrt/unowned_ptr.h"
 #include "core/fxge/cfx_face.h"
 #include "third_party/abseil-cpp/absl/types/optional.h"
 
-#ifdef PDF_ENABLE_XFA
-#include "core/fxcrt/data_vector.h"
-#endif
-
 class CFX_FontMgr;
 class CFX_SubstFont;
 class SystemFontInfoIface;
@@ -88,7 +85,9 @@
 
 #ifdef PDF_ENABLE_XFA
   // `index` must be less than GetFaceSize().
-  DataVector<uint8_t> RawBytesForIndex(size_t index);
+  std::unique_ptr<uint8_t, FxFreeDeleter> RawBytesForIndex(
+      size_t index,
+      size_t* returned_length);
 #endif  // PDF_ENABLE_XFA
 
  private:
diff --git a/core/fxge/cfx_fontmgr.cpp b/core/fxge/cfx_fontmgr.cpp
index c41b09c..eacffc2 100644
--- a/core/fxge/cfx_fontmgr.cpp
+++ b/core/fxge/cfx_fontmgr.cpp
@@ -55,8 +55,9 @@
 
 }  // namespace
 
-CFX_FontMgr::FontDesc::FontDesc(DataVector<uint8_t> data)
-    : m_pFontData(std::move(data)) {}
+CFX_FontMgr::FontDesc::FontDesc(std::unique_ptr<uint8_t, FxFreeDeleter> pData,
+                                size_t size)
+    : m_Size(size), m_pFontData(std::move(pData)) {}
 
 CFX_FontMgr::FontDesc::~FontDesc() = default;
 
@@ -90,8 +91,9 @@
     const ByteString& face_name,
     int weight,
     bool bItalic,
-    DataVector<uint8_t> data) {
-  auto pFontDesc = pdfium::MakeRetain<FontDesc>(std::move(data));
+    std::unique_ptr<uint8_t, FxFreeDeleter> pData,
+    size_t size) {
+  auto pFontDesc = pdfium::MakeRetain<FontDesc>(std::move(pData), size);
   m_FaceMap[{face_name, weight, bItalic}].Reset(pFontDesc.Get());
   return pFontDesc;
 }
@@ -107,8 +109,9 @@
 RetainPtr<CFX_FontMgr::FontDesc> CFX_FontMgr::AddCachedTTCFontDesc(
     size_t ttc_size,
     uint32_t checksum,
-    DataVector<uint8_t> data) {
-  auto pNewDesc = pdfium::MakeRetain<FontDesc>(std::move(data));
+    std::unique_ptr<uint8_t, FxFreeDeleter> pData,
+    size_t size) {
+  auto pNewDesc = pdfium::MakeRetain<FontDesc>(std::move(pData), size);
   m_TTCFaceMap[{ttc_size, checksum}].Reset(pNewDesc.Get());
   return pNewDesc;
 }
diff --git a/core/fxge/cfx_fontmgr.h b/core/fxge/cfx_fontmgr.h
index 1c13807..5bb9620 100644
--- a/core/fxge/cfx_fontmgr.h
+++ b/core/fxge/cfx_fontmgr.h
@@ -15,7 +15,7 @@
 #include <tuple>
 
 #include "core/fxcrt/bytestring.h"
-#include "core/fxcrt/data_vector.h"
+#include "core/fxcrt/fx_memory_wrappers.h"
 #include "core/fxcrt/observed_ptr.h"
 #include "core/fxcrt/retain_ptr.h"
 #include "core/fxge/freetype/fx_freetype.h"
@@ -31,16 +31,17 @@
     CONSTRUCT_VIA_MAKE_RETAIN;
     ~FontDesc() override;
 
-    pdfium::span<const uint8_t> FontData() const {
-      return pdfium::make_span(m_pFontData);
+    pdfium::span<uint8_t> FontData() const {
+      return {m_pFontData.get(), m_Size};
     }
     void SetFace(size_t index, CFX_Face* face);
     CFX_Face* GetFace(size_t index) const;
 
    private:
-    explicit FontDesc(DataVector<uint8_t> data);
+    FontDesc(std::unique_ptr<uint8_t, FxFreeDeleter> pData, size_t size);
 
-    const DataVector<uint8_t> m_pFontData;
+    const size_t m_Size;
+    std::unique_ptr<uint8_t, FxFreeDeleter> const m_pFontData;
     ObservedPtr<CFX_Face> m_TTCFaces[16];
   };
 
@@ -55,15 +56,19 @@
   RetainPtr<FontDesc> GetCachedFontDesc(const ByteString& face_name,
                                         int weight,
                                         bool bItalic);
-  RetainPtr<FontDesc> AddCachedFontDesc(const ByteString& face_name,
-                                        int weight,
-                                        bool bItalic,
-                                        DataVector<uint8_t> data);
+  RetainPtr<FontDesc> AddCachedFontDesc(
+      const ByteString& face_name,
+      int weight,
+      bool bItalic,
+      std::unique_ptr<uint8_t, FxFreeDeleter> pData,
+      size_t size);
 
   RetainPtr<FontDesc> GetCachedTTCFontDesc(size_t ttc_size, uint32_t checksum);
-  RetainPtr<FontDesc> AddCachedTTCFontDesc(size_t ttc_size,
-                                           uint32_t checksum,
-                                           DataVector<uint8_t> data);
+  RetainPtr<FontDesc> AddCachedTTCFontDesc(
+      size_t ttc_size,
+      uint32_t checksum,
+      std::unique_ptr<uint8_t, FxFreeDeleter> pData,
+      size_t size);
 
   RetainPtr<CFX_Face> NewFixedFace(RetainPtr<FontDesc> pDesc,
                                    pdfium::span<const uint8_t> span,
diff --git a/xfa/fgas/font/cfgas_fontmgr.cpp b/xfa/fgas/font/cfgas_fontmgr.cpp
index f7beac7..d9f6833 100644
--- a/xfa/fgas/font/cfgas_fontmgr.cpp
+++ b/xfa/fgas/font/cfgas_fontmgr.cpp
@@ -14,7 +14,7 @@
 #include <utility>
 
 #include "build/build_config.h"
-#include "core/fxcrt/cfx_read_only_vector_stream.h"
+#include "core/fxcrt/cfx_memorystream.h"
 #include "core/fxcrt/data_vector.h"
 #include "core/fxcrt/fx_codepage.h"
 #include "core/fxcrt/fx_extension.h"
@@ -473,11 +473,13 @@
 
 RetainPtr<IFX_SeekableReadStream> CreateFontStream(CFX_FontMapper* pFontMapper,
                                                    size_t index) {
-  DataVector<uint8_t> buffer = pFontMapper->RawBytesForIndex(index);
-  if (buffer.empty())
+  size_t dwFileSize = 0;
+  std::unique_ptr<uint8_t, FxFreeDeleter> pBuffer =
+      pFontMapper->RawBytesForIndex(index, &dwFileSize);
+  if (!pBuffer)
     return nullptr;
 
-  return pdfium::MakeRetain<CFX_ReadOnlyVectorStream>(std::move(buffer));
+  return pdfium::MakeRetain<CFX_MemoryStream>(std::move(pBuffer), dwFileSize);
 }
 
 RetainPtr<IFX_SeekableReadStream> CreateFontStream(