Use ByteStringView / pdfium::span in CPDF font as appropriate.

Change-Id: I92c7ba605bf95a9023ad046b8dddebe0a0592802
Reviewed-on: https://pdfium-review.googlesource.com/29992
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_cidfont.cpp b/core/fpdfapi/font/cpdf_cidfont.cpp
index 7de4d7c..4d3ffdf 100644
--- a/core/fpdfapi/font/cpdf_cidfont.cpp
+++ b/core/fpdfapi/font/cpdf_cidfont.cpp
@@ -751,18 +751,17 @@
   return pdata[0] * 256 + pdata[1];
 }
 
-uint32_t CPDF_CIDFont::GetNextChar(const char* pString,
-                                   int nStrLen,
-                                   int& offset) const {
-  return m_pCMap->GetNextChar(pString, nStrLen, offset);
+uint32_t CPDF_CIDFont::GetNextChar(const ByteStringView& pString,
+                                   size_t& offset) const {
+  return m_pCMap->GetNextChar(pString, offset);
 }
 
 int CPDF_CIDFont::GetCharSize(uint32_t charcode) const {
   return m_pCMap->GetCharSize(charcode);
 }
 
-int CPDF_CIDFont::CountChar(const char* pString, int size) const {
-  return m_pCMap->CountChar(pString, size);
+size_t CPDF_CIDFont::CountChar(const ByteStringView& pString) const {
+  return m_pCMap->CountChar(pString);
 }
 
 int CPDF_CIDFont::AppendChar(char* str, uint32_t charcode) const {
diff --git a/core/fpdfapi/font/cpdf_cidfont.h b/core/fpdfapi/font/cpdf_cidfont.h
index 0bf5f7a..fe2e2fe 100644
--- a/core/fpdfapi/font/cpdf_cidfont.h
+++ b/core/fpdfapi/font/cpdf_cidfont.h
@@ -46,10 +46,9 @@
   int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) override;
   uint32_t GetCharWidthF(uint32_t charcode) override;
   FX_RECT GetCharBBox(uint32_t charcode) override;
-  uint32_t GetNextChar(const char* pString,
-                       int nStrLen,
-                       int& offset) const override;
-  int CountChar(const char* pString, int size) const override;
+  uint32_t GetNextChar(const ByteStringView& pString,
+                       size_t& offset) const override;
+  size_t CountChar(const ByteStringView& pString) const override;
   int AppendChar(char* str, uint32_t charcode) const override;
   bool IsVertWriting() const override;
   bool IsUnicodeCompatible() const override;
diff --git a/core/fpdfapi/font/cpdf_cmap.cpp b/core/fpdfapi/font/cpdf_cmap.cpp
index 81ad633..8e46a75 100644
--- a/core/fpdfapi/font/cpdf_cmap.cpp
+++ b/core/fpdfapi/font/cpdf_cmap.cpp
@@ -337,10 +337,9 @@
   return it->m_StartCID + charcode - it->m_StartCode;
 }
 
-uint32_t CPDF_CMap::GetNextChar(const char* pString,
-                                int nStrLen,
-                                int& offset) const {
-  auto* pBytes = reinterpret_cast<const uint8_t*>(pString);
+uint32_t CPDF_CMap::GetNextChar(const ByteStringView& pString,
+                                size_t& offset) const {
+  auto pBytes = pString.span();
   switch (m_CodingScheme) {
     case OneByte: {
       return pBytes[offset++];
@@ -370,7 +369,7 @@
             charcode = (charcode << 8) + codes[i];
           return charcode;
         }
-        if (char_size == 4 || offset == nStrLen)
+        if (char_size == 4 || offset == pBytes.size())
           return 0;
         codes[char_size++] = pBytes[offset++];
       }
@@ -402,33 +401,32 @@
   return 1;
 }
 
-int CPDF_CMap::CountChar(const char* pString, int size) const {
+size_t CPDF_CMap::CountChar(const ByteStringView& pString) const {
   switch (m_CodingScheme) {
     case OneByte:
-      return size;
+      return pString.GetLength();
     case TwoBytes:
-      return (size + 1) / 2;
+      return (pString.GetLength() + 1) / 2;
     case MixedTwoBytes: {
-      int count = 0;
-      for (int i = 0; i < size; i++) {
+      size_t count = 0;
+      for (size_t i = 0; i < pString.GetLength(); i++) {
         count++;
-        if (m_MixedTwoByteLeadingBytes[reinterpret_cast<const uint8_t*>(
-                pString)[i]]) {
+        if (m_MixedTwoByteLeadingBytes[pString[i]])
           i++;
-        }
       }
       return count;
     }
     case MixedFourBytes: {
-      int count = 0, offset = 0;
-      while (offset < size) {
-        GetNextChar(pString, size, offset);
+      size_t count = 0;
+      size_t offset = 0;
+      while (offset < pString.GetLength()) {
+        GetNextChar(pString, offset);
         count++;
       }
       return count;
     }
   }
-  return size;
+  return pString.GetLength();
 }
 
 int CPDF_CMap::AppendChar(char* str, uint32_t charcode) const {
diff --git a/core/fpdfapi/font/cpdf_cmap.h b/core/fpdfapi/font/cpdf_cmap.h
index c6fdcae..96ccf02 100644
--- a/core/fpdfapi/font/cpdf_cmap.h
+++ b/core/fpdfapi/font/cpdf_cmap.h
@@ -62,8 +62,8 @@
   uint16_t CIDFromCharCode(uint32_t charcode) const;
 
   int GetCharSize(uint32_t charcode) const;
-  uint32_t GetNextChar(const char* pString, int nStrLen, int& offset) const;
-  int CountChar(const char* pString, int size) const;
+  uint32_t GetNextChar(const ByteStringView& pString, size_t& offset) const;
+  size_t CountChar(const ByteStringView& pString) const;
   int AppendChar(char* str, uint32_t charcode) const;
 
   void SetVertical(bool vert) { m_bVertical = vert; }
diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp
index 013cdde..f636e93 100644
--- a/core/fpdfapi/font/cpdf_font.cpp
+++ b/core/fpdfapi/font/cpdf_font.cpp
@@ -122,8 +122,8 @@
   return false;
 }
 
-int CPDF_Font::CountChar(const char* pString, int size) const {
-  return size;
+size_t CPDF_Font::CountChar(const ByteStringView& pString) const {
+  return pString.GetLength();
 }
 
 int CPDF_Font::GlyphFromCharCodeExt(uint32_t charcode) {
@@ -278,20 +278,18 @@
 void CPDF_Font::LoadUnicodeMap() const {
   m_bToUnicodeLoaded = true;
   CPDF_Stream* pStream = m_pFontDict->GetStreamFor("ToUnicode");
-  if (!pStream) {
+  if (!pStream)
     return;
-  }
+
   m_pToUnicodeMap = pdfium::MakeUnique<CPDF_ToUnicodeMap>();
   m_pToUnicodeMap->Load(pStream);
 }
 
-uint32_t CPDF_Font::GetStringWidth(const char* pString, int size) {
-  int offset = 0;
+uint32_t CPDF_Font::GetStringWidth(const ByteStringView& pString) {
+  size_t offset = 0;
   uint32_t width = 0;
-  while (offset < size) {
-    uint32_t charcode = GetNextChar(pString, size, offset);
-    width += GetCharWidthF(charcode);
-  }
+  while (offset < pString.GetLength())
+    width += GetCharWidthF(GetNextChar(pString, offset));
   return width;
 }
 
@@ -346,13 +344,13 @@
   return pFont->Load() ? std::move(pFont) : nullptr;
 }
 
-uint32_t CPDF_Font::GetNextChar(const char* pString,
-                                int nStrLen,
-                                int& offset) const {
-  if (offset < 0 || nStrLen < 1) {
+uint32_t CPDF_Font::GetNextChar(const ByteStringView& pString,
+                                size_t& offset) const {
+  if (pString.IsEmpty())
     return 0;
-  }
-  uint8_t ch = offset < nStrLen ? pString[offset++] : pString[nStrLen - 1];
+
+  uint8_t ch = offset < pString.GetLength() ? pString[offset++]
+                                            : pString[pString.GetLength() - 1];
   return static_cast<uint32_t>(ch);
 }
 
diff --git a/core/fpdfapi/font/cpdf_font.h b/core/fpdfapi/font/cpdf_font.h
index db99efd..588fb66 100644
--- a/core/fpdfapi/font/cpdf_font.h
+++ b/core/fpdfapi/font/cpdf_font.h
@@ -53,10 +53,9 @@
 
   virtual bool IsVertWriting() const;
   virtual bool IsUnicodeCompatible() const;
-  virtual uint32_t GetNextChar(const char* pString,
-                               int nStrLen,
-                               int& offset) const;
-  virtual int CountChar(const char* pString, int size) const;
+  virtual uint32_t GetNextChar(const ByteStringView& pString,
+                               size_t& offset) const;
+  virtual size_t CountChar(const ByteStringView& pString) const;
   virtual int AppendChar(char* buf, uint32_t charcode) const;
   virtual int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) = 0;
   virtual int GlyphFromCharCodeExt(uint32_t charcode);
@@ -75,7 +74,7 @@
   void GetFontBBox(FX_RECT& rect) const { rect = m_FontBBox; }
   int GetTypeAscent() const { return m_Ascent; }
   int GetTypeDescent() const { return m_Descent; }
-  uint32_t GetStringWidth(const char* pString, int size);
+  uint32_t GetStringWidth(const ByteStringView& pString);
   uint32_t FallbackFontFromCharcode(uint32_t charcode);
   int FallbackGlyphFromCharcode(int fallbackFont, uint32_t charcode);
 
diff --git a/core/fpdfapi/page/cpdf_textobject.cpp b/core/fpdfapi/page/cpdf_textobject.cpp
index 402bf2e..36a4722 100644
--- a/core/fpdfapi/page/cpdf_textobject.cpp
+++ b/core/fpdfapi/page/cpdf_textobject.cpp
@@ -152,18 +152,17 @@
   CPDF_Font* pFont = m_TextState.GetFont();
   int nChars = 0;
   for (int i = 0; i < nsegs; ++i)
-    nChars += pFont->CountChar(pStrs[i].c_str(), pStrs[i].GetLength());
+    nChars += pFont->CountChar(pStrs[i].AsStringView());
   nChars += nsegs - 1;
   m_CharCodes.resize(nChars);
   m_CharPos.resize(nChars - 1);
-  int index = 0;
+  size_t index = 0;
   for (int i = 0; i < nsegs; ++i) {
-    const char* segment = pStrs[i].c_str();
-    int len = pStrs[i].GetLength();
-    int offset = 0;
-    while (offset < len) {
-      ASSERT(static_cast<size_t>(index) < m_CharCodes.size());
-      m_CharCodes[index++] = pFont->GetNextChar(segment, len, offset);
+    ByteStringView segment = pStrs[i].AsStringView();
+    size_t offset = 0;
+    while (offset < segment.GetLength()) {
+      ASSERT(index < m_CharCodes.size());
+      m_CharCodes[index++] = pFont->GetNextChar(segment, offset);
     }
     if (i != nsegs - 1) {
       m_CharPos[index - 1] = pKerning[i];
diff --git a/core/fpdfapi/render/cpdf_textrenderer.cpp b/core/fpdfapi/render/cpdf_textrenderer.cpp
index 711dbfa..7aeddf0 100644
--- a/core/fpdfapi/render/cpdf_textrenderer.cpp
+++ b/core/fpdfapi/render/cpdf_textrenderer.cpp
@@ -83,18 +83,18 @@
   if (pFont->IsType3Font())
     return;
 
-  int nChars = pFont->CountChar(str.c_str(), str.GetLength());
+  int nChars = pFont->CountChar(str.AsStringView());
   if (nChars <= 0)
     return;
 
-  int offset = 0;
+  size_t offset = 0;
   std::vector<uint32_t> codes;
   std::vector<float> positions;
   codes.resize(nChars);
   positions.resize(nChars - 1);
   float cur_pos = 0;
   for (int i = 0; i < nChars; i++) {
-    codes[i] = pFont->GetNextChar(str.c_str(), str.GetLength(), offset);
+    codes[i] = pFont->GetNextChar(str.AsStringView(), offset);
     if (i)
       positions[i - 1] = cur_pos;
     cur_pos += pFont->GetCharWidthF(codes[i]) * font_size / 1000;
diff --git a/core/fpdftext/cpdf_textpage.cpp b/core/fpdftext/cpdf_textpage.cpp
index e2d1f5f..91c9249 100644
--- a/core/fpdftext/cpdf_textpage.cpp
+++ b/core/fpdftext/cpdf_textpage.cpp
@@ -610,7 +610,7 @@
 
   ByteString str;
   pFont->AppendChar(&str, charCode);
-  w = pFont->GetStringWidth(str.c_str(), 1);
+  w = pFont->GetStringWidth(str.AsStringView());
   if (w > 0)
     return w;