Fix some nits in win32 fxge code.

Change-Id: I32438325532175ea1aee58326ebcc4540b052dd1
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/63470
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxge/win32/cpsoutput.cpp b/core/fxge/win32/cpsoutput.cpp
index 0ae6461..4170db1 100644
--- a/core/fxge/win32/cpsoutput.cpp
+++ b/core/fxge/win32/cpsoutput.cpp
@@ -9,29 +9,30 @@
 #include <algorithm>
 
 #include "core/fxcrt/fx_system.h"
+#include "third_party/base/span.h"
 
 CPSOutput::CPSOutput(HDC hDC, OutputMode mode) : m_hDC(hDC), m_mode(mode) {}
 
-CPSOutput::~CPSOutput() {}
+CPSOutput::~CPSOutput() = default;
 
 bool CPSOutput::WriteBlock(const void* str, size_t len) {
-  int sent_len = 0;
-  while (len > 0) {
-    char buffer[1026];
-    size_t send_len = std::min(len, static_cast<size_t>(1024));
-    *(reinterpret_cast<uint16_t*>(buffer)) = send_len;
-    memcpy(buffer + 2, static_cast<const char*>(str) + sent_len, send_len);
+  pdfium::span<const uint8_t> input(static_cast<const uint8_t*>(str), len);
+  while (!input.empty()) {
+    uint8_t buffer[1026];
+    size_t send_len = std::min<size_t>(input.size(), 1024);
+    *(reinterpret_cast<uint16_t*>(buffer)) = static_cast<uint16_t>(send_len);
+    memcpy(buffer + 2, input.data(), send_len);
 
     switch (m_mode) {
       case OutputMode::kExtEscape:
-        ExtEscape(m_hDC, PASSTHROUGH, send_len + 2, buffer, 0, nullptr);
+        ExtEscape(m_hDC, PASSTHROUGH, send_len + 2,
+                  reinterpret_cast<const char*>(buffer), 0, nullptr);
         break;
       case OutputMode::kGdiComment:
-        GdiComment(m_hDC, send_len + 2, reinterpret_cast<const BYTE*>(buffer));
+        GdiComment(m_hDC, send_len + 2, buffer);
         break;
     }
-    sent_len += send_len;
-    len -= send_len;
+    input = input.subspan(send_len);
   }
   return true;
 }
diff --git a/core/fxge/win32/cpsoutput.h b/core/fxge/win32/cpsoutput.h
index 132ee58..939f4cc 100644
--- a/core/fxge/win32/cpsoutput.h
+++ b/core/fxge/win32/cpsoutput.h
@@ -24,7 +24,7 @@
   bool WriteString(ByteStringView str) override;
 
  private:
-  HDC m_hDC;
+  const HDC m_hDC;
   const OutputMode m_mode;
 };
 
diff --git a/core/fxge/win32/fx_win32_device.cpp b/core/fxge/win32/fx_win32_device.cpp
index bb13a76..441e036 100644
--- a/core/fxge/win32/fx_win32_device.cpp
+++ b/core/fxge/win32/fx_win32_device.cpp
@@ -101,24 +101,24 @@
     PenStyle |= PS_SOLID;
 
   switch (pGraphState->m_LineCap) {
-    case 0:
+    case CFX_GraphStateData::LineCapButt:
       PenStyle |= PS_ENDCAP_FLAT;
       break;
-    case 1:
+    case CFX_GraphStateData::LineCapRound:
       PenStyle |= PS_ENDCAP_ROUND;
       break;
-    case 2:
+    case CFX_GraphStateData::LineCapSquare:
       PenStyle |= PS_ENDCAP_SQUARE;
       break;
   }
   switch (pGraphState->m_LineJoin) {
-    case 0:
+    case CFX_GraphStateData::LineJoinMiter:
       PenStyle |= PS_JOIN_MITER;
       break;
-    case 1:
+    case CFX_GraphStateData::LineJoinRound:
       PenStyle |= PS_JOIN_ROUND;
       break;
-    case 2:
+    case CFX_GraphStateData::LineJoinBevel:
       PenStyle |= PS_JOIN_BEVEL;
       break;
   }
@@ -339,10 +339,11 @@
   void GetJapanesePreference(ByteString& face, int weight, int picth_family);
   ByteString FindFont(const ByteString& name);
 
-  HDC m_hDC;
+  const HDC m_hDC;
   UnownedPtr<CFX_FontMapper> m_pMapper;
   ByteString m_LastFamily;
-  ByteString m_KaiTi, m_FangSong;
+  ByteString m_KaiTi;
+  ByteString m_FangSong;
 };
 
 int CALLBACK FontEnumProc(const LOGFONTA* plf,
@@ -707,7 +708,7 @@
     : m_hDC(hDC), m_DeviceType(device_type) {
   auto* pPlatform =
       static_cast<CWin32Platform*>(CFX_GEModule::Get()->GetPlatform());
-  SetStretchBltMode(hDC, pPlatform->m_bHalfTone ? HALFTONE : COLORONCOLOR);
+  SetStretchBltMode(m_hDC, pPlatform->m_bHalfTone ? HALFTONE : COLORONCOLOR);
   DWORD obj_type = GetObjectType(m_hDC);
   m_bMetafileDCType = obj_type == OBJ_ENHMETADC || obj_type == OBJ_ENHMETAFILE;
   if (obj_type == OBJ_MEMDC) {
diff --git a/core/fxge/win32/fx_win32_dib.cpp b/core/fxge/win32/fx_win32_dib.cpp
index 6c9d394..5f9d42c 100644
--- a/core/fxge/win32/fx_win32_dib.cpp
+++ b/core/fxge/win32/fx_win32_dib.cpp
@@ -76,18 +76,18 @@
 
   memcpy(pBitmap->GetBuffer(), pData, pitch * height);
   if (bBottomUp) {
-    uint8_t* temp_buf = FX_Alloc(uint8_t, pitch);
-    int top = 0, bottom = height - 1;
+    std::vector<uint8_t> temp_buf(pitch);
+    int top = 0;
+    int bottom = height - 1;
     while (top < bottom) {
-      memcpy(temp_buf, pBitmap->GetBuffer() + top * pitch, pitch);
-      memcpy(pBitmap->GetBuffer() + top * pitch,
-             pBitmap->GetBuffer() + bottom * pitch, pitch);
-      memcpy(pBitmap->GetBuffer() + bottom * pitch, temp_buf, pitch);
+      uint8_t* top_ptr = pBitmap->GetBuffer() + top * pitch;
+      uint8_t* bottom_ptr = pBitmap->GetBuffer() + bottom * pitch;
+      memcpy(temp_buf.data(), top_ptr, pitch);
+      memcpy(top_ptr, bottom_ptr, pitch);
+      memcpy(bottom_ptr, temp_buf.data(), pitch);
       top++;
       bottom--;
     }
-    FX_Free(temp_buf);
-    temp_buf = nullptr;
   }
   if (pbmi->bmiHeader.biBitCount == 1) {
     for (int i = 0; i < 2; i++)
diff --git a/core/fxge/win32/fx_win32_print.cpp b/core/fxge/win32/fx_win32_print.cpp
index 2f56790..9ae092a 100644
--- a/core/fxge/win32/fx_win32_print.cpp
+++ b/core/fxge/win32/fx_win32_print.cpp
@@ -21,16 +21,17 @@
 #include "core/fxge/text_char_pos.h"
 #include "core/fxge/win32/cpsoutput.h"
 #include "core/fxge/win32/win32_int.h"
+#include "third_party/base/span.h"
 
 #if defined(PDFIUM_PRINT_TEXT_WITH_GDI)
 namespace {
 
 class ScopedState {
  public:
-  ScopedState(HDC hDC, HFONT hFont) : m_hDC(hDC) {
-    m_iState = SaveDC(m_hDC);
-    m_hFont = SelectObject(m_hDC, hFont);
-  }
+  ScopedState(HDC hDC, HFONT hFont)
+      : m_hDC(hDC),
+        m_iState(SaveDC(m_hDC)),
+        m_hFont(SelectObject(m_hDC, hFont)) {}
 
   ScopedState(const ScopedState&) = delete;
   ScopedState& operator=(const ScopedState&) = delete;
@@ -42,9 +43,9 @@
   }
 
  private:
-  HDC m_hDC;
-  HGDIOBJ m_hFont;
-  int m_iState;
+  const HDC m_hDC;
+  const int m_iState;
+  const HGDIOBJ m_hFont;
 };
 
 }  // namespace
@@ -360,13 +361,12 @@
   m_PSRenderer.Init(pdfium::MakeRetain<CPSOutput>(m_hDC, output_mode), pslevel,
                     m_Width, m_Height, bCmykOutput);
   HRGN hRgn = ::CreateRectRgn(0, 0, 1, 1);
-  int ret = ::GetClipRgn(hDC, hRgn);
-  if (ret == 1) {
-    ret = ::GetRegionData(hRgn, 0, nullptr);
-    if (ret) {
-      RGNDATA* pData = reinterpret_cast<RGNDATA*>(FX_Alloc(uint8_t, ret));
-      ret = ::GetRegionData(hRgn, ret, pData);
-      if (ret) {
+  if (::GetClipRgn(m_hDC, hRgn) == 1) {
+    DWORD dwCount = ::GetRegionData(hRgn, 0, nullptr);
+    if (dwCount) {
+      std::vector<uint8_t> buffer(dwCount);
+      RGNDATA* pData = reinterpret_cast<RGNDATA*>(buffer.data());
+      if (::GetRegionData(hRgn, dwCount, pData)) {
         CFX_PathData path;
         for (uint32_t i = 0; i < pData->rdh.nCount; i++) {
           RECT* pRect =
@@ -378,7 +378,6 @@
         }
         m_PSRenderer.SetClip_PathFill(&path, nullptr, FXFILL_WINDING);
       }
-      FX_Free(pData);
     }
   }
   ::DeleteObject(hRgn);
@@ -450,9 +449,8 @@
                                 FX_ARGB stroke_color,
                                 int fill_mode,
                                 BlendMode blend_type) {
-  if (blend_type != BlendMode::kNormal) {
+  if (blend_type != BlendMode::kNormal)
     return false;
-  }
   return m_PSRenderer.DrawPath(pPathData, pObject2Device, pGraphState,
                                fill_color, stroke_color, fill_mode & 3);
 }
@@ -633,18 +631,18 @@
   // errors below. Value chosen based on the title of https://crbug.com/18383
   const double kScaleFactor = 10;
 
-  WideString wsText;
-  int totalLength = nChars;
-
   // Detect new lines and add clrf characters (since this is Windows only).
   // These characters are removed by SkPDF, but the new line information is
   // preserved in the text location. clrf characters seem to be ignored by
   // label printers that use this driver.
+  WideString wsText;
+  size_t len = nChars;
   float fOffsetY = mtObject2Device.f * kScaleFactor;
   if (m_SetOrigin && FXSYS_roundf(m_OriginY) != FXSYS_roundf(fOffsetY)) {
     wsText += L"\r\n";
-    totalLength += 2;
+    len += 2;
   }
+  wsText.Reserve(len);
   m_OriginY = fOffsetY;
   m_SetOrigin = true;
 
@@ -661,16 +659,15 @@
 
     wsText += charpos.m_Unicode;
   }
-  size_t len = totalLength;
   ByteString text = wsText.ToDefANSI();
-  while (len > 0) {
-    char buffer[1026];
-    size_t send_len = std::min(len, static_cast<size_t>(1024));
-    *(reinterpret_cast<uint16_t*>(buffer)) = send_len;
-    memcpy(buffer + 2, text.c_str(), send_len);
-    ::GdiComment(m_hDC, send_len + 2, reinterpret_cast<const BYTE*>(buffer));
-    len -= send_len;
-    text.Right(len);
+  auto text_span = text.span();
+  while (!text_span.empty()) {
+    uint8_t buffer[1026];
+    size_t send_len = std::min<size_t>(text_span.size(), 1024);
+    *(reinterpret_cast<uint16_t*>(buffer)) = static_cast<uint16_t>(send_len);
+    memcpy(buffer + 2, text_span.data(), send_len);
+    ::GdiComment(m_hDC, send_len + 2, buffer);
+    text_span = text_span.subspan(send_len);
   }
   return true;
 }