Use unsigned for char width

Bug: 806612
Change-Id: I22bd9046dd37a1b596762c46a6b29a323d6e9fa1
Reviewed-on: https://pdfium-review.googlesource.com/24410
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Nicolás Peña Moreno <npm@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_cidfont.cpp b/core/fpdfapi/font/cpdf_cidfont.cpp
index 21f8add..f17af41 100644
--- a/core/fpdfapi/font/cpdf_cidfont.cpp
+++ b/core/fpdfapi/font/cpdf_cidfont.cpp
@@ -514,7 +514,7 @@
   return rect;
 }
 
-int CPDF_CIDFont::GetCharWidthF(uint32_t charcode) {
+uint32_t CPDF_CIDFont::GetCharWidthF(uint32_t charcode) {
   if (charcode < 0x80 && m_bAnsiWidthsFixed)
     return (charcode >= 32 && charcode < 127) ? 500 : 0;
 
@@ -524,7 +524,7 @@
   for (size_t i = 0; i < size; i += 3) {
     const uint32_t* pEntry = pList + i;
     if (IsMetricForCID(pEntry, cid))
-      return static_cast<int>(pEntry[2]);
+      return pEntry[2];
   }
   return m_DefaultWidth;
 }
diff --git a/core/fpdfapi/font/cpdf_cidfont.h b/core/fpdfapi/font/cpdf_cidfont.h
index f6ff83d..68fce2c 100644
--- a/core/fpdfapi/font/cpdf_cidfont.h
+++ b/core/fpdfapi/font/cpdf_cidfont.h
@@ -44,7 +44,7 @@
   const CPDF_CIDFont* AsCIDFont() const override;
   CPDF_CIDFont* AsCIDFont() override;
   int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) override;
-  int GetCharWidthF(uint32_t charcode) override;
+  uint32_t GetCharWidthF(uint32_t charcode) override;
   FX_RECT GetCharBBox(uint32_t charcode) override;
   uint32_t GetNextChar(const char* pString,
                        int nStrLen,
diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp
index 8d35739..013cdde 100644
--- a/core/fpdfapi/font/cpdf_font.cpp
+++ b/core/fpdfapi/font/cpdf_font.cpp
@@ -285,9 +285,9 @@
   m_pToUnicodeMap->Load(pStream);
 }
 
-int CPDF_Font::GetStringWidth(const char* pString, int size) {
+uint32_t CPDF_Font::GetStringWidth(const char* pString, int size) {
   int offset = 0;
-  int width = 0;
+  uint32_t width = 0;
   while (offset < size) {
     uint32_t charcode = GetNextChar(pString, size, offset);
     width += GetCharWidthF(charcode);
diff --git a/core/fpdfapi/font/cpdf_font.h b/core/fpdfapi/font/cpdf_font.h
index 0468bcb..db99efd 100644
--- a/core/fpdfapi/font/cpdf_font.h
+++ b/core/fpdfapi/font/cpdf_font.h
@@ -75,11 +75,11 @@
   void GetFontBBox(FX_RECT& rect) const { rect = m_FontBBox; }
   int GetTypeAscent() const { return m_Ascent; }
   int GetTypeDescent() const { return m_Descent; }
-  int GetStringWidth(const char* pString, int size);
+  uint32_t GetStringWidth(const char* pString, int size);
   uint32_t FallbackFontFromCharcode(uint32_t charcode);
   int FallbackGlyphFromCharcode(int fallbackFont, uint32_t charcode);
 
-  virtual int GetCharWidthF(uint32_t charcode) = 0;
+  virtual uint32_t GetCharWidthF(uint32_t charcode) = 0;
   virtual FX_RECT GetCharBBox(uint32_t charcode) = 0;
 
   CPDF_Document* GetDocument() const { return m_pDocument.Get(); }
diff --git a/core/fpdfapi/font/cpdf_simplefont.cpp b/core/fpdfapi/font/cpdf_simplefont.cpp
index 92965b0..89f6fe1 100644
--- a/core/fpdfapi/font/cpdf_simplefont.cpp
+++ b/core/fpdfapi/font/cpdf_simplefont.cpp
@@ -80,7 +80,7 @@
   }
 }
 
-int CPDF_SimpleFont::GetCharWidthF(uint32_t charcode) {
+uint32_t CPDF_SimpleFont::GetCharWidthF(uint32_t charcode) {
   if (charcode > 0xff)
     charcode = 0;
 
diff --git a/core/fpdfapi/font/cpdf_simplefont.h b/core/fpdfapi/font/cpdf_simplefont.h
index 5291211..9cb730d 100644
--- a/core/fpdfapi/font/cpdf_simplefont.h
+++ b/core/fpdfapi/font/cpdf_simplefont.h
@@ -20,7 +20,7 @@
   ~CPDF_SimpleFont() override;
 
   // CPDF_Font
-  int GetCharWidthF(uint32_t charcode) override;
+  uint32_t GetCharWidthF(uint32_t charcode) override;
   FX_RECT GetCharBBox(uint32_t charcode) override;
   int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) override;
   bool IsUnicodeCompatible() const override;
diff --git a/core/fpdfapi/font/cpdf_type3char.h b/core/fpdfapi/font/cpdf_type3char.h
index c9c5555..28baffe 100644
--- a/core/fpdfapi/font/cpdf_type3char.h
+++ b/core/fpdfapi/font/cpdf_type3char.h
@@ -38,7 +38,7 @@
   CPDF_Form* form() { return m_pForm.get(); }
 
   bool colored() const { return m_bColored; }
-  int width() const { return m_Width; }
+  uint32_t width() const { return m_Width; }
   const CFX_Matrix& matrix() const { return m_ImageMatrix; }
   const FX_RECT& bbox() const { return m_BBox; }
 
@@ -46,7 +46,7 @@
   std::unique_ptr<CPDF_Form> m_pForm;
   RetainPtr<CFX_DIBitmap> m_pBitmap;
   bool m_bColored = false;
-  int m_Width = 0;
+  uint32_t m_Width = 0;
   CFX_Matrix m_ImageMatrix;
   FX_RECT m_BBox;
 };
diff --git a/core/fpdfapi/font/cpdf_type3font.cpp b/core/fpdfapi/font/cpdf_type3font.cpp
index b3eaef0..824d6f4 100644
--- a/core/fpdfapi/font/cpdf_type3font.cpp
+++ b/core/fpdfapi/font/cpdf_type3font.cpp
@@ -130,7 +130,7 @@
   return pCachedChar;
 }
 
-int CPDF_Type3Font::GetCharWidthF(uint32_t charcode) {
+uint32_t CPDF_Type3Font::GetCharWidthF(uint32_t charcode) {
   if (charcode >= FX_ArraySize(m_CharWidthL))
     charcode = 0;
 
diff --git a/core/fpdfapi/font/cpdf_type3font.h b/core/fpdfapi/font/cpdf_type3font.h
index 3f2e018..67400d5 100644
--- a/core/fpdfapi/font/cpdf_type3font.h
+++ b/core/fpdfapi/font/cpdf_type3font.h
@@ -26,7 +26,7 @@
   bool IsType3Font() const override;
   const CPDF_Type3Font* AsType3Font() const override;
   CPDF_Type3Font* AsType3Font() override;
-  int GetCharWidthF(uint32_t charcode) override;
+  uint32_t GetCharWidthF(uint32_t charcode) override;
   FX_RECT GetCharBBox(uint32_t charcode) override;
 
   void SetPageResources(CPDF_Dictionary* pResources) {
@@ -47,7 +47,7 @@
   // CPDF_SimpleFont:
   void LoadGlyphMap() override;
 
-  int m_CharWidthL[256];
+  uint32_t m_CharWidthL[256];
   UnownedPtr<CPDF_Dictionary> m_pCharProcs;
   UnownedPtr<CPDF_Dictionary> m_pPageResources;
   UnownedPtr<CPDF_Dictionary> m_pFontResources;
diff --git a/core/fpdfapi/render/cpdf_charposlist.cpp b/core/fpdfapi/render/cpdf_charposlist.cpp
index c0c700c..ddb215c 100644
--- a/core/fpdfapi/render/cpdf_charposlist.cpp
+++ b/core/fpdfapi/render/cpdf_charposlist.cpp
@@ -69,8 +69,8 @@
     float scalingFactor = 1.0f;
     if (!pFont->IsEmbedded() && pFont->HasFontWidths() && !bVertWriting &&
         !pCurrentFont->GetSubstFont()->m_bFlagMM) {
-      int pdfGlyphWidth = pFont->GetCharWidthF(CharCode);
-      int ftGlyphWidth =
+      uint32_t pdfGlyphWidth = pFont->GetCharWidthF(CharCode);
+      uint32_t ftGlyphWidth =
           pCurrentFont ? pCurrentFont->GetGlyphWidth(charpos.m_GlyphIndex) : 0;
       if (ftGlyphWidth && pdfGlyphWidth > ftGlyphWidth + 1) {
         // Move the initial x position by half of the excess (transformed to
@@ -80,7 +80,6 @@
       } else if (pdfGlyphWidth && ftGlyphWidth &&
                  pdfGlyphWidth < ftGlyphWidth) {
         scalingFactor = static_cast<float>(pdfGlyphWidth) / ftGlyphWidth;
-        ASSERT(scalingFactor >= 0.0f);
         charpos.m_AdjustMatrix[0] = scalingFactor;
         charpos.m_AdjustMatrix[1] = 0.0f;
         charpos.m_AdjustMatrix[2] = 0.0f;
diff --git a/core/fpdfdoc/cpdf_variabletext.cpp b/core/fpdfdoc/cpdf_variabletext.cpp
index 74ea239..38c631d8 100644
--- a/core/fpdfdoc/cpdf_variabletext.cpp
+++ b/core/fpdfdoc/cpdf_variabletext.cpp
@@ -38,8 +38,8 @@
 
 CPDF_VariableText::Provider::~Provider() {}
 
-int32_t CPDF_VariableText::Provider::GetCharWidth(int32_t nFontIndex,
-                                                  uint16_t word) {
+uint32_t CPDF_VariableText::Provider::GetCharWidth(int32_t nFontIndex,
+                                                   uint16_t word) {
   if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex)) {
     uint32_t charcode = pPDFFont->CharCodeFromUnicode(word);
     if (charcode != CPDF_Font::kInvalidCharCode)
@@ -917,9 +917,9 @@
   return rcRet;
 }
 
-int32_t CPDF_VariableText::GetCharWidth(int32_t nFontIndex,
-                                        uint16_t Word,
-                                        uint16_t SubWord) {
+uint32_t CPDF_VariableText::GetCharWidth(int32_t nFontIndex,
+                                         uint16_t Word,
+                                         uint16_t SubWord) {
   if (!m_pVTProvider)
     return 0;
   uint16_t word = SubWord ? SubWord : Word;
diff --git a/core/fpdfdoc/cpdf_variabletext.h b/core/fpdfdoc/cpdf_variabletext.h
index a939dcb..786ad10 100644
--- a/core/fpdfdoc/cpdf_variabletext.h
+++ b/core/fpdfdoc/cpdf_variabletext.h
@@ -53,7 +53,7 @@
     explicit Provider(IPVT_FontMap* pFontMap);
     virtual ~Provider();
 
-    virtual int32_t GetCharWidth(int32_t nFontIndex, uint16_t word);
+    virtual uint32_t GetCharWidth(int32_t nFontIndex, uint16_t word);
     virtual int32_t GetTypeAscent(int32_t nFontIndex);
     virtual int32_t GetTypeDescent(int32_t nFontIndex);
     virtual int32_t GetWordFontIndex(uint16_t word,
@@ -165,7 +165,7 @@
   float GetLineIndent();
 
  private:
-  int32_t GetCharWidth(int32_t nFontIndex, uint16_t Word, uint16_t SubWord);
+  uint32_t GetCharWidth(int32_t nFontIndex, uint16_t Word, uint16_t SubWord);
   int32_t GetTypeAscent(int32_t nFontIndex);
   int32_t GetTypeDescent(int32_t nFontIndex);
   int32_t GetWordFontIndex(uint16_t word, int32_t charset, int32_t nFontIndex);
diff --git a/core/fpdftext/cpdf_textpage.cpp b/core/fpdftext/cpdf_textpage.cpp
index 72097b4..1621426 100644
--- a/core/fpdftext/cpdf_textpage.cpp
+++ b/core/fpdftext/cpdf_textpage.cpp
@@ -595,18 +595,22 @@
   }
 }
 
-int CPDF_TextPage::GetCharWidth(uint32_t charCode, CPDF_Font* pFont) const {
+uint32_t CPDF_TextPage::GetCharWidth(uint32_t charCode,
+                                     CPDF_Font* pFont) const {
   if (charCode == CPDF_Font::kInvalidCharCode)
     return 0;
 
-  if (int w = pFont->GetCharWidthF(charCode))
+  uint32_t w = pFont->GetCharWidthF(charCode);
+  if (w > 0)
     return w;
 
   ByteString str;
   pFont->AppendChar(&str, charCode);
-  if (int w = pFont->GetStringWidth(str.c_str(), 1))
+  w = pFont->GetStringWidth(str.c_str(), 1);
+  if (w > 0)
     return w;
 
+  ASSERT(pFont->GetCharBBox(charCode).Width() >= 0);
   return pFont->GetCharBBox(charCode).Width();
 }
 
@@ -1044,7 +1048,6 @@
       spacing -= matrix.TransformDistance(fabs(charSpace));
     spacing -= baseSpace;
     if (spacing && i > 0) {
-      int last_width = 0;
       float fontsize_h = pTextObj->m_TextState.GetFontSizeH();
       uint32_t space_charcode = pFont->CharCodeFromUnicode(' ');
       float threshold = 0;
@@ -1055,10 +1058,7 @@
       else
         threshold /= 2;
       if (threshold == 0) {
-        threshold = fontsize_h;
-        int this_width = abs(GetCharWidth(item.m_CharCode, pFont));
-        threshold =
-            this_width > last_width ? (float)this_width : (float)last_width;
+        threshold = static_cast<float>(GetCharWidth(item.m_CharCode, pFont));
         threshold = NormalizeThreshold(threshold);
         threshold = fontsize_h * threshold / 1000;
       }
@@ -1275,10 +1275,11 @@
   }
 
   float last_pos = PrevItem.m_Origin.x;
-  int nLastWidth = GetCharWidth(PrevItem.m_CharCode, m_pPreTextObj->GetFont());
+  uint32_t nLastWidth =
+      GetCharWidth(PrevItem.m_CharCode, m_pPreTextObj->GetFont());
   float last_width = nLastWidth * m_pPreTextObj->GetFontSize() / 1000;
   last_width = fabs(last_width);
-  int nThisWidth = GetCharWidth(item.m_CharCode, pObj->GetFont());
+  uint32_t nThisWidth = GetCharWidth(item.m_CharCode, pObj->GetFont());
   float this_width = nThisWidth * pObj->GetFontSize() / 1000;
   this_width = fabs(this_width);
   float threshold = last_width > this_width ? last_width / 4 : this_width / 4;
diff --git a/core/fpdftext/cpdf_textpage.h b/core/fpdftext/cpdf_textpage.h
index 51d0660..c87ab00 100644
--- a/core/fpdftext/cpdf_textpage.h
+++ b/core/fpdftext/cpdf_textpage.h
@@ -147,7 +147,7 @@
                              const CPDF_PageObjectList* pObjList,
                              CPDF_PageObjectList::const_iterator ObjPos);
   bool IsSameTextObject(CPDF_TextObject* pTextObj1, CPDF_TextObject* pTextObj2);
-  int GetCharWidth(uint32_t charCode, CPDF_Font* pFont) const;
+  uint32_t GetCharWidth(uint32_t charCode, CPDF_Font* pFont) const;
   void CloseTempLine();
   FPDFText_MarkedContent PreMarkedContent(PDFTEXT_Obj pObj);
   void ProcessMarkedContent(PDFTEXT_Obj pObj);
diff --git a/core/fxge/apple/fx_apple_platform.cpp b/core/fxge/apple/fx_apple_platform.cpp
index 3c142c6..1801814 100644
--- a/core/fxge/apple/fx_apple_platform.cpp
+++ b/core/fxge/apple/fx_apple_platform.cpp
@@ -169,7 +169,7 @@
     const CFX_Font* pFont,
     uint32_t glyph_index,
     const CFX_Matrix* pMatrix,
-    int dest_width,
+    uint32_t dest_width,
     int anti_alias) {
   return nullptr;
 }
diff --git a/core/fxge/cfx_facecache.cpp b/core/fxge/cfx_facecache.cpp
index 4f373ff..ea72905 100644
--- a/core/fxge/cfx_facecache.cpp
+++ b/core/fxge/cfx_facecache.cpp
@@ -73,7 +73,7 @@
     uint32_t glyph_index,
     bool bFontStyle,
     const CFX_Matrix* pMatrix,
-    int dest_width,
+    uint32_t dest_width,
     int anti_alias) {
   if (!m_Face)
     return nullptr;
@@ -193,7 +193,7 @@
 
 const CFX_PathData* CFX_FaceCache::LoadGlyphPath(const CFX_Font* pFont,
                                                  uint32_t glyph_index,
-                                                 int dest_width) {
+                                                 uint32_t dest_width) {
   if (!m_Face || glyph_index == kInvalidGlyphIndex)
     return nullptr;
 
@@ -216,7 +216,7 @@
                                                       uint32_t glyph_index,
                                                       bool bFontStyle,
                                                       const CFX_Matrix* pMatrix,
-                                                      int dest_width,
+                                                      uint32_t dest_width,
                                                       int anti_alias,
                                                       int& text_flags) {
   if (glyph_index == kInvalidGlyphIndex)
@@ -339,7 +339,7 @@
     const ByteString& FaceGlyphsKey,
     uint32_t glyph_index,
     bool bFontStyle,
-    int dest_width,
+    uint32_t dest_width,
     int anti_alias) {
   SizeGlyphCache* pSizeCache;
   auto it = m_SizeMap.find(FaceGlyphsKey);
diff --git a/core/fxge/cfx_facecache.h b/core/fxge/cfx_facecache.h
index a39da88..4ff4c41 100644
--- a/core/fxge/cfx_facecache.h
+++ b/core/fxge/cfx_facecache.h
@@ -30,12 +30,12 @@
                                          uint32_t glyph_index,
                                          bool bFontStyle,
                                          const CFX_Matrix* pMatrix,
-                                         int dest_width,
+                                         uint32_t dest_width,
                                          int anti_alias,
                                          int& text_flags);
   const CFX_PathData* LoadGlyphPath(const CFX_Font* pFont,
                                     uint32_t glyph_index,
-                                    int dest_width);
+                                    uint32_t dest_width);
 
 #if defined _SKIA_SUPPORT_ || _SKIA_SUPPORT_PATHS_
   CFX_TypeFace* GetDeviceCache(const CFX_Font* pFont);
@@ -44,26 +44,26 @@
  private:
   using SizeGlyphCache = std::map<uint32_t, std::unique_ptr<CFX_GlyphBitmap>>;
   // <glyph_index, width, weight, angle, vertical>
-  using PathMapKey = std::tuple<uint32_t, int, int, int, bool>;
+  using PathMapKey = std::tuple<uint32_t, uint32_t, int, int, bool>;
 
   std::unique_ptr<CFX_GlyphBitmap> RenderGlyph(const CFX_Font* pFont,
                                                uint32_t glyph_index,
                                                bool bFontStyle,
                                                const CFX_Matrix* pMatrix,
-                                               int dest_width,
+                                               uint32_t dest_width,
                                                int anti_alias);
   std::unique_ptr<CFX_GlyphBitmap> RenderGlyph_Nativetext(
       const CFX_Font* pFont,
       uint32_t glyph_index,
       const CFX_Matrix* pMatrix,
-      int dest_width,
+      uint32_t dest_width,
       int anti_alias);
   CFX_GlyphBitmap* LookUpGlyphBitmap(const CFX_Font* pFont,
                                      const CFX_Matrix* pMatrix,
                                      const ByteString& FaceGlyphsKey,
                                      uint32_t glyph_index,
                                      bool bFontStyle,
-                                     int dest_width,
+                                     uint32_t dest_width,
                                      int anti_alias);
   void InitPlatform();
   void DestroyPlatform();
diff --git a/core/fxge/cfx_font.cpp b/core/fxge/cfx_font.cpp
index 6d969a3..ece3f96 100644
--- a/core/fxge/cfx_font.cpp
+++ b/core/fxge/cfx_font.cpp
@@ -295,7 +295,7 @@
 }
 #endif  // PDF_ENABLE_XFA
 
-int CFX_Font::GetGlyphWidth(uint32_t glyph_index) {
+uint32_t CFX_Font::GetGlyphWidth(uint32_t glyph_index) {
   if (!m_Face)
     return 0;
   if (m_pSubstFont && m_pSubstFont->m_bFlagMM)
@@ -307,7 +307,7 @@
     return 0;
 
   int horiAdvance = FXFT_Get_Glyph_HoriAdvance(m_Face);
-  if (horiAdvance < kThousandthMinInt || horiAdvance > kThousandthMaxInt)
+  if (horiAdvance < 0 || horiAdvance > kThousandthMaxInt)
     return 0;
 
   return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), horiAdvance);
@@ -501,7 +501,7 @@
 }
 
 void CFX_Font::AdjustMMParams(int glyph_index,
-                              int dest_width,
+                              uint32_t dest_width,
                               int weight) const {
   FXFT_MM_Var pMasters = nullptr;
   FXFT_Get_MM_Var(m_Face, &pMasters);
@@ -544,7 +544,7 @@
 }
 
 CFX_PathData* CFX_Font::LoadGlyphPathImpl(uint32_t glyph_index,
-                                          int dest_width) const {
+                                          uint32_t dest_width) const {
   if (!m_Face)
     return nullptr;
   FXFT_Set_Pixel_Sizes(m_Face, 0, 64);
@@ -613,7 +613,7 @@
 const CFX_GlyphBitmap* CFX_Font::LoadGlyphBitmap(uint32_t glyph_index,
                                                  bool bFontStyle,
                                                  const CFX_Matrix* pMatrix,
-                                                 int dest_width,
+                                                 uint32_t dest_width,
                                                  int anti_alias,
                                                  int& text_flags) const {
   return GetFaceCache()->LoadGlyphBitmap(this, glyph_index, bFontStyle, pMatrix,
@@ -621,7 +621,7 @@
 }
 
 const CFX_PathData* CFX_Font::LoadGlyphPath(uint32_t glyph_index,
-                                            int dest_width) const {
+                                            uint32_t dest_width) const {
   return GetFaceCache()->LoadGlyphPath(this, glyph_index, dest_width);
 }
 
diff --git a/core/fxge/cfx_font.h b/core/fxge/cfx_font.h
index c8c4cf7..3739cad 100644
--- a/core/fxge/cfx_font.h
+++ b/core/fxge/cfx_font.h
@@ -52,16 +52,17 @@
   const CFX_GlyphBitmap* LoadGlyphBitmap(uint32_t glyph_index,
                                          bool bFontStyle,
                                          const CFX_Matrix* pMatrix,
-                                         int dest_width,
+                                         uint32_t dest_width,
                                          int anti_alias,
                                          int& text_flags) const;
-  const CFX_PathData* LoadGlyphPath(uint32_t glyph_index, int dest_width) const;
+  const CFX_PathData* LoadGlyphPath(uint32_t glyph_index,
+                                    uint32_t dest_width) const;
 
 #if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
   CFX_TypeFace* GetDeviceCache() const;
 #endif
 
-  int GetGlyphWidth(uint32_t glyph_index);
+  uint32_t GetGlyphWidth(uint32_t glyph_index);
   int GetAscent() const;
   int GetDescent() const;
   bool GetGlyphBBox(uint32_t glyph_index, FX_RECT& bbox);
@@ -83,9 +84,10 @@
 #endif
   uint8_t* GetFontData() const { return m_pFontData; }
   uint32_t GetSize() const { return m_dwSize; }
-  void AdjustMMParams(int glyph_index, int width, int weight) const;
+  void AdjustMMParams(int glyph_index, uint32_t width, int weight) const;
 
-  CFX_PathData* LoadGlyphPathImpl(uint32_t glyph_index, int dest_width) const;
+  CFX_PathData* LoadGlyphPathImpl(uint32_t glyph_index,
+                                  uint32_t dest_width) const;
 
   static const size_t kAngleSkewArraySize = 30;
   static const char s_AngleSkew[kAngleSkewArraySize];
diff --git a/core/fxge/cfx_renderdevice.h b/core/fxge/cfx_renderdevice.h
index d8cb9a6..d3ebed4 100644
--- a/core/fxge/cfx_renderdevice.h
+++ b/core/fxge/cfx_renderdevice.h
@@ -76,7 +76,7 @@
   CFX_PointF m_Origin;
   uint32_t m_Unicode;
   uint32_t m_GlyphIndex;
-  int32_t m_FontCharWidth;
+  uint32_t m_FontCharWidth;
 #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
   uint32_t m_ExtGID;
 #endif
diff --git a/core/fxge/skia/fx_skia_device_unittest.cpp b/core/fxge/skia/fx_skia_device_unittest.cpp
index 7cb28cf..66db939 100644
--- a/core/fxge/skia/fx_skia_device_unittest.cpp
+++ b/core/fxge/skia/fx_skia_device_unittest.cpp
@@ -38,7 +38,7 @@
   FXTEXT_CHARPOS charPos[1];
   charPos[0].m_Origin = CFX_PointF(0, 1);
   charPos[0].m_GlyphIndex = 1;
-  charPos[0].m_FontCharWidth = 4;
+  charPos[0].m_FontCharWidth = 4u;
 
   CFX_Font font;
   float fontSize = 1;
diff --git a/fpdfsdk/fpdfedit_embeddertest.cpp b/fpdfsdk/fpdfedit_embeddertest.cpp
index ee2fc7e..070c51e 100644
--- a/fpdfsdk/fpdfedit_embeddertest.cpp
+++ b/fpdfsdk/fpdfedit_embeddertest.cpp
@@ -105,7 +105,7 @@
         int cnt = static_cast<int>(arr->GetCount());
         size_t inner_idx = 0;
         for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) {
-          int width = arr->GetNumberAt(inner_idx++);
+          uint32_t width = arr->GetNumberAt(inner_idx++);
           EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid "
                                                                << cur_cid;
         }
@@ -116,7 +116,7 @@
       ASSERT_TRUE(next->IsNumber());
       int last_cid = next->AsNumber()->GetInteger();
       ASSERT_FALSE(++idx == widths_array->GetCount());
-      int width = widths_array->GetNumberAt(idx);
+      uint32_t width = widths_array->GetNumberAt(idx);
       for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) {
         EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid "
                                                              << cur_cid;
diff --git a/fpdfsdk/fpdfedittext.cpp b/fpdfsdk/fpdfedittext.cpp
index 22c6266..7bbb6a8 100644
--- a/fpdfsdk/fpdfedittext.cpp
+++ b/fpdfsdk/fpdfedittext.cpp
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#include <algorithm>
+#include <limits>
 #include <map>
 #include <memory>
 #include <utility>
@@ -259,7 +261,10 @@
   fontDict->SetNewFor<CPDF_Number>("FirstChar", static_cast<int>(currentChar));
   CPDF_Array* widthsArray = pDoc->NewIndirect<CPDF_Array>();
   while (true) {
-    widthsArray->AddNew<CPDF_Number>(pFont->GetGlyphWidth(glyphIndex));
+    uint32_t width =
+        std::min(pFont->GetGlyphWidth(glyphIndex),
+                 static_cast<uint32_t>(std::numeric_limits<int>::max()));
+    widthsArray->AddNew<CPDF_Number>(static_cast<int>(width));
     uint32_t nextChar =
         FXFT_Get_Next_Char(pFont->GetFace(), currentChar, &glyphIndex);
     // Simple fonts have 1-byte charcodes only.
diff --git a/fpdfsdk/pwl/cpwl_edit_impl.cpp b/fpdfsdk/pwl/cpwl_edit_impl.cpp
index 1881ba2..91496f0 100644
--- a/fpdfsdk/pwl/cpwl_edit_impl.cpp
+++ b/fpdfsdk/pwl/cpwl_edit_impl.cpp
@@ -125,8 +125,8 @@
   return m_pFontMap;
 }
 
-int32_t CPWL_EditImpl_Provider::GetCharWidth(int32_t nFontIndex,
-                                             uint16_t word) {
+uint32_t CPWL_EditImpl_Provider::GetCharWidth(int32_t nFontIndex,
+                                              uint16_t word) {
   if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex)) {
     uint32_t charcode = word;
 
diff --git a/fpdfsdk/pwl/cpwl_edit_impl.h b/fpdfsdk/pwl/cpwl_edit_impl.h
index 38477db..7f4d3e1 100644
--- a/fpdfsdk/pwl/cpwl_edit_impl.h
+++ b/fpdfsdk/pwl/cpwl_edit_impl.h
@@ -423,7 +423,7 @@
   IPVT_FontMap* GetFontMap() const;
 
   // CPDF_VariableText::Provider:
-  int32_t GetCharWidth(int32_t nFontIndex, uint16_t word) override;
+  uint32_t GetCharWidth(int32_t nFontIndex, uint16_t word) override;
   int32_t GetTypeAscent(int32_t nFontIndex) override;
   int32_t GetTypeDescent(int32_t nFontIndex) override;
   int32_t GetWordFontIndex(uint16_t word,
diff --git a/fxbarcode/oned/BC_OneDimWriter.cpp b/fxbarcode/oned/BC_OneDimWriter.cpp
index 0fa23bb..040257b 100644
--- a/fxbarcode/oned/BC_OneDimWriter.cpp
+++ b/fxbarcode/oned/BC_OneDimWriter.cpp
@@ -142,9 +142,9 @@
   float charWidth = 0;
   for (size_t j = 0; j < length; j++) {
     pCharCode[j] = encoding->CharCodeFromUnicode(text[j]);
-    int32_t glyp_code = encoding->GlyphFromCharCode(pCharCode[j]);
-    int32_t glyp_value = cFont->GetGlyphWidth(glyp_code);
-    float temp = (float)((glyp_value)*fontSize / 1000.0);
+    int32_t glyph_code = encoding->GlyphFromCharCode(pCharCode[j]);
+    uint32_t glyph_value = cFont->GetGlyphWidth(glyph_code);
+    float temp = glyph_value * fontSize / 1000.0;
     charWidth += temp;
   }
   charsLen = charWidth;
diff --git a/xfa/fgas/font/cfgas_pdffontmgr.cpp b/xfa/fgas/font/cfgas_pdffontmgr.cpp
index 4d34ac8..e2fb905 100644
--- a/xfa/fgas/font/cfgas_pdffontmgr.cpp
+++ b/xfa/fgas/font/cfgas_pdffontmgr.cpp
@@ -194,7 +194,10 @@
     return false;
 
   CPDF_Font* pPDFFont = it->second;
-  *pWidth = pPDFFont->GetCharWidthF(pPDFFont->CharCodeFromUnicode(wUnicode));
+  // TODO(npm): CFGAS_GEFont::GetCharWidth currently uses -1 as a special value,
+  // so |pWidth| cannot be changed to unsigned until this behavior is changed.
+  *pWidth = static_cast<int32_t>(
+      pPDFFont->GetCharWidthF(pPDFFont->CharCodeFromUnicode(wUnicode)));
   return true;
 }
 
diff --git a/xfa/fgas/layout/cfx_rtfbreak.cpp b/xfa/fgas/layout/cfx_rtfbreak.cpp
index 3ef0ef2..9497e5f 100644
--- a/xfa/fgas/layout/cfx_rtfbreak.cpp
+++ b/xfa/fgas/layout/cfx_rtfbreak.cpp
@@ -702,7 +702,7 @@
       continue;
     }
 
-    int32_t iCharWidth = abs(iWidth);
+    uint32_t iCharWidth = abs(iWidth);
     bool bEmptyChar =
         (dwCharType >= FX_CHARTYPE_Tab && dwCharType <= FX_CHARTYPE_Control);
     if (!bEmptyChar)
diff --git a/xfa/fgas/layout/cfx_txtbreak.cpp b/xfa/fgas/layout/cfx_txtbreak.cpp
index b028c9b..b019992 100644
--- a/xfa/fgas/layout/cfx_txtbreak.cpp
+++ b/xfa/fgas/layout/cfx_txtbreak.cpp
@@ -831,6 +831,8 @@
 #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
         pCharPos->m_ExtGID = pCharPos->m_GlyphIndex;
 #endif
+        // TODO(npm): change widths in this method to unsigned to avoid implicit
+        // cast in the following line.
         pCharPos->m_FontCharWidth = iCharWidth;
       }