Cleanup CPWL_Icon AppStream generation

This CL moves the AppSteam generation code for CPWL_Icon to a separate
method. The CPWL_Image code is also folded directly into CPWL_Icon as it
is the only subclass.

Change-Id: I7936bac4af76e34d0f73d48ca00f5713c5f20095
Reviewed-on: https://pdfium-review.googlesource.com/8314
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Commit-Queue: dsinclair <dsinclair@chromium.org>
diff --git a/fpdfsdk/pdfwindow/cpwl_appstream.cpp b/fpdfsdk/pdfwindow/cpwl_appstream.cpp
index cf291e3..f7b5736 100644
--- a/fpdfsdk/pdfwindow/cpwl_appstream.cpp
+++ b/fpdfsdk/pdfwindow/cpwl_appstream.cpp
@@ -593,6 +593,53 @@
   return CFX_ByteString(sAppStream);
 }
 
+CFX_ByteString GenerateIconAppStream(CPDF_IconFit& fit,
+                                     CPDF_Stream* pIconStream,
+                                     const CFX_FloatRect& rcIcon) {
+  if (rcIcon.IsEmpty() || !pIconStream)
+    return CFX_ByteString();
+
+  CPWL_Icon icon;
+  PWL_CREATEPARAM cp;
+  cp.dwFlags = PWS_VISIBLE;
+  icon.Create(cp);
+  icon.SetIconFit(&fit);
+  icon.SetPDFStream(pIconStream);
+  icon.Move(rcIcon, false, false);
+
+  CFX_ByteString sAlias = icon.GetImageAlias();
+  if (sAlias.GetLength() <= 0)
+    return CFX_ByteString();
+
+  CFX_FloatRect rcPlate = icon.GetClientRect();
+  CFX_Matrix mt = icon.GetImageMatrix().GetInverse();
+
+  float fHScale;
+  float fVScale;
+  std::tie(fHScale, fVScale) = icon.GetScale();
+
+  float fx;
+  float fy;
+  std::tie(fx, fy) = icon.GetImageOffset();
+
+  std::ostringstream str;
+  str << "q\n";
+  str << rcPlate.left << " " << rcPlate.bottom << " "
+      << rcPlate.right - rcPlate.left << " " << rcPlate.top - rcPlate.bottom
+      << " re W n\n";
+
+  str << fHScale << " 0 0 " << fVScale << " " << rcPlate.left + fx << " "
+      << rcPlate.bottom + fy << " cm\n";
+  str << mt.a << " " << mt.b << " " << mt.c << " " << mt.d << " " << mt.e << " "
+      << mt.f << " cm\n";
+
+  str << "0 g 0 G 1 w /" << sAlias << " Do\n"
+      << "Q\n";
+  icon.Destroy();
+
+  return CFX_ByteString(str);
+}
+
 CFX_ByteString GetPushButtonAppStream(const CFX_FloatRect& rcBBox,
                                       IPVT_FontMap* pFontMap,
                                       CPDF_Stream* pIconStream,
@@ -756,54 +803,14 @@
   }
 
   std::ostringstream sTemp;
-
-  if (!rcIcon.IsEmpty()) {
-    CPWL_Icon icon;
-    PWL_CREATEPARAM cp;
-    cp.dwFlags = PWS_VISIBLE;
-    icon.Create(cp);
-    icon.SetIconFit(&IconFit);
-    icon.SetPDFStream(pIconStream);
-    icon.Move(rcIcon, false, false);
-
-    CFX_ByteString sAlias = icon.GetImageAlias();
-    if (sAlias.GetLength() > 0) {
-      CFX_FloatRect rcPlate = icon.GetClientRect();
-      CFX_Matrix mt = icon.GetImageMatrix().GetInverse();
-
-      float fHScale = 1.0f;
-      float fVScale = 1.0f;
-      icon.GetScale(fHScale, fVScale);
-
-      float fx = 0.0f;
-      float fy = 0.0f;
-      icon.GetImageOffset(fx, fy);
-
-      if (icon.GetPDFStream()) {
-        sTemp << "q\n";
-        sTemp << rcPlate.left << " " << rcPlate.bottom << " "
-              << rcPlate.right - rcPlate.left << " "
-              << rcPlate.top - rcPlate.bottom << " re W n\n";
-
-        sTemp << fHScale << " 0 0 " << fVScale << " " << rcPlate.left + fx
-              << " " << rcPlate.bottom + fy << " cm\n";
-        sTemp << mt.a << " " << mt.b << " " << mt.c << " " << mt.d << " "
-              << mt.e << " " << mt.f << " cm\n";
-
-        sTemp << "0 g 0 G 1 w /" << sAlias << " Do\n"
-              << "Q\n";
-      }
-    }
-    icon.Destroy();
-  }
+  sTemp << GenerateIconAppStream(IconFit, pIconStream, rcIcon);
 
   if (!rcLabel.IsEmpty()) {
     pEdit->SetPlateRect(rcLabel);
     CFX_ByteString sEdit =
         GetEditAppStream(pEdit.get(), CFX_PointF(0.0f, 0.0f), true, 0);
-    if (sEdit.GetLength() > 0) {
+    if (sEdit.GetLength() > 0)
       sTemp << "BT\n" << GetColorAppStream(crText, true) << sEdit << "ET\n";
-    }
   }
 
   if (sTemp.tellp() <= 0)
diff --git a/fpdfsdk/pdfwindow/cpwl_icon.cpp b/fpdfsdk/pdfwindow/cpwl_icon.cpp
index d2f2e5f..4de3e70 100644
--- a/fpdfsdk/pdfwindow/cpwl_icon.cpp
+++ b/fpdfsdk/pdfwindow/cpwl_icon.cpp
@@ -13,179 +13,119 @@
 #include "core/fpdfapi/parser/cpdf_stream.h"
 #include "fpdfsdk/pdfwindow/cpwl_wnd.h"
 
-CPWL_Image::CPWL_Image() : m_pPDFStream(nullptr) {}
-
-CPWL_Image::~CPWL_Image() {}
-
-void CPWL_Image::SetPDFStream(CPDF_Stream* pStream) {
-  m_pPDFStream = pStream;
-}
-
-CPDF_Stream* CPWL_Image::GetPDFStream() const {
-  return m_pPDFStream.Get();
-}
-
-void CPWL_Image::GetImageSize(float& fWidth, float& fHeight) {
-  fWidth = 0.0f;
-  fHeight = 0.0f;
-
-  if (m_pPDFStream) {
-    if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) {
-      CFX_FloatRect rect = pDict->GetRectFor("BBox");
-
-      fWidth = rect.right - rect.left;
-      fHeight = rect.top - rect.bottom;
-    }
-  }
-}
-
-CFX_Matrix CPWL_Image::GetImageMatrix() {
-  if (m_pPDFStream) {
-    if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) {
-      return pDict->GetMatrixFor("Matrix");
-    }
-  }
-
-  return CFX_Matrix();
-}
-
-CFX_ByteString CPWL_Image::GetImageAlias() {
-  if (!m_sImageAlias.IsEmpty())
-    return m_sImageAlias;
-
-  if (m_pPDFStream) {
-    if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) {
-      return pDict->GetStringFor("Name");
-    }
-  }
-
-  return CFX_ByteString();
-}
-
-void CPWL_Image::SetImageAlias(const char* sImageAlias) {
-  m_sImageAlias = sImageAlias;
-}
-
-void CPWL_Image::GetScale(float& fHScale, float& fVScale) {
-  fHScale = 1.0f;
-  fVScale = 1.0f;
-}
-
-void CPWL_Image::GetImageOffset(float& x, float& y) {
-  x = 0.0f;
-  y = 0.0f;
-}
-
-CPWL_Icon::CPWL_Icon() : m_pIconFit(nullptr) {}
+CPWL_Icon::CPWL_Icon() : m_pPDFStream(nullptr), m_pIconFit(nullptr) {}
 
 CPWL_Icon::~CPWL_Icon() {}
 
-CPDF_IconFit* CPWL_Icon::GetIconFit() const {
-  return m_pIconFit.Get();
+std::pair<float, float> CPWL_Icon::GetImageSize() {
+  if (!m_pPDFStream)
+    return {0.0f, 0.0f};
+
+  CPDF_Dictionary* pDict = m_pPDFStream->GetDict();
+  if (!pDict)
+    return {0.0f, 0.0f};
+
+  CFX_FloatRect rect = pDict->GetRectFor("BBox");
+  return {rect.right - rect.left, rect.top - rect.bottom};
 }
 
-int32_t CPWL_Icon::GetScaleMethod() {
-  if (m_pIconFit)
-    return m_pIconFit->GetScaleMethod();
-
-  return 0;
+CFX_Matrix CPWL_Icon::GetImageMatrix() {
+  if (!m_pPDFStream)
+    return CFX_Matrix();
+  if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict())
+    return pDict->GetMatrixFor("Matrix");
+  return CFX_Matrix();
 }
 
-bool CPWL_Icon::IsProportionalScale() {
-  if (m_pIconFit)
-    return m_pIconFit->IsProportionalScale();
-
-  return false;
+CFX_ByteString CPWL_Icon::GetImageAlias() {
+  if (!m_pPDFStream)
+    return CFX_ByteString();
+  if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict())
+    return pDict->GetStringFor("Name");
+  return CFX_ByteString();
 }
 
-void CPWL_Icon::GetIconPosition(float& fLeft, float& fBottom) {
-  if (m_pIconFit) {
-    fLeft = 0.0f;
-    fBottom = 0.0f;
-    CPDF_Array* pA = m_pIconFit->GetDict()
-                         ? m_pIconFit->GetDict()->GetArrayFor("A")
-                         : nullptr;
-    if (pA) {
-      size_t dwCount = pA->GetCount();
-      if (dwCount > 0)
-        fLeft = pA->GetNumberAt(0);
-      if (dwCount > 1)
-        fBottom = pA->GetNumberAt(1);
-    }
-  } else {
-    fLeft = 0.0f;
-    fBottom = 0.0f;
-  }
+std::pair<float, float> CPWL_Icon::GetIconPosition() {
+  if (!m_pIconFit)
+    return {0.0f, 0.0f};
+
+  CPDF_Array* pA =
+      m_pIconFit->GetDict() ? m_pIconFit->GetDict()->GetArrayFor("A") : nullptr;
+  if (!pA)
+    return {0.0f, 0.0f};
+
+  size_t dwCount = pA->GetCount();
+  return {dwCount > 0 ? pA->GetNumberAt(0) : 0.0f,
+          dwCount > 1 ? pA->GetNumberAt(1) : 0.0f};
 }
 
-void CPWL_Icon::GetScale(float& fHScale, float& fVScale) {
-  fHScale = 1.0f;
-  fVScale = 1.0f;
+std::pair<float, float> CPWL_Icon::GetScale() {
+  float fHScale = 1.0f;
+  float fVScale = 1.0f;
 
-  if (m_pPDFStream) {
-    float fImageWidth, fImageHeight;
-    float fPlateWidth, fPlateHeight;
+  if (!m_pPDFStream)
+    return {fHScale, fVScale};
 
-    CFX_FloatRect rcPlate = GetClientRect();
-    fPlateWidth = rcPlate.right - rcPlate.left;
-    fPlateHeight = rcPlate.top - rcPlate.bottom;
+  CFX_FloatRect rcPlate = GetClientRect();
+  float fPlateWidth = rcPlate.right - rcPlate.left;
+  float fPlateHeight = rcPlate.top - rcPlate.bottom;
 
-    GetImageSize(fImageWidth, fImageHeight);
+  float fImageWidth;
+  float fImageHeight;
+  std::tie(fImageWidth, fImageHeight) = GetImageSize();
 
-    int32_t nScaleMethod = GetScaleMethod();
+  int32_t nScaleMethod = m_pIconFit ? m_pIconFit->GetScaleMethod() : 0;
 
-    switch (nScaleMethod) {
-      default:
-      case 0:
+  switch (nScaleMethod) {
+    default:
+    case 0:
+      fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
+      fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
+      break;
+    case 1:
+      if (fPlateWidth < fImageWidth)
         fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
+      if (fPlateHeight < fImageHeight)
         fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
-        break;
-      case 1:
-        if (fPlateWidth < fImageWidth)
-          fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
-        if (fPlateHeight < fImageHeight)
-          fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
-        break;
-      case 2:
-        if (fPlateWidth > fImageWidth)
-          fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
-        if (fPlateHeight > fImageHeight)
-          fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
-        break;
-      case 3:
-        break;
-    }
-
-    float fMinScale;
-    if (IsProportionalScale()) {
-      fMinScale = std::min(fHScale, fVScale);
-      fHScale = fMinScale;
-      fVScale = fMinScale;
-    }
+      break;
+    case 2:
+      if (fPlateWidth > fImageWidth)
+        fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
+      if (fPlateHeight > fImageHeight)
+        fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
+      break;
+    case 3:
+      break;
   }
+
+  float fMinScale;
+  if (m_pIconFit && m_pIconFit->IsProportionalScale()) {
+    fMinScale = std::min(fHScale, fVScale);
+    fHScale = fMinScale;
+    fVScale = fMinScale;
+  }
+  return {fHScale, fVScale};
 }
 
-void CPWL_Icon::GetImageOffset(float& x, float& y) {
-  float fLeft, fBottom;
+std::pair<float, float> CPWL_Icon::GetImageOffset() {
+  float fLeft;
+  float fBottom;
+  std::tie(fLeft, fBottom) = GetIconPosition();
 
-  GetIconPosition(fLeft, fBottom);
-  x = 0.0f;
-  y = 0.0f;
-
-  float fImageWidth, fImageHeight;
-  GetImageSize(fImageWidth, fImageHeight);
+  float fImageWidth;
+  float fImageHeight;
+  std::tie(fImageWidth, fImageHeight) = GetImageSize();
 
   float fHScale, fVScale;
-  GetScale(fHScale, fVScale);
+  std::tie(fHScale, fVScale) = GetScale();
 
   float fImageFactWidth = fImageWidth * fHScale;
   float fImageFactHeight = fImageHeight * fVScale;
 
-  float fPlateWidth, fPlateHeight;
   CFX_FloatRect rcPlate = GetClientRect();
-  fPlateWidth = rcPlate.right - rcPlate.left;
-  fPlateHeight = rcPlate.top - rcPlate.bottom;
+  float fPlateWidth = rcPlate.right - rcPlate.left;
+  float fPlateHeight = rcPlate.top - rcPlate.bottom;
 
-  x = (fPlateWidth - fImageFactWidth) * fLeft;
-  y = (fPlateHeight - fImageFactHeight) * fBottom;
+  return {(fPlateWidth - fImageFactWidth) * fLeft,
+          (fPlateHeight - fImageFactHeight) * fBottom};
 }
diff --git a/fpdfsdk/pdfwindow/cpwl_icon.h b/fpdfsdk/pdfwindow/cpwl_icon.h
index 0b82a4b..e97c540 100644
--- a/fpdfsdk/pdfwindow/cpwl_icon.h
+++ b/fpdfsdk/pdfwindow/cpwl_icon.h
@@ -7,48 +7,37 @@
 #ifndef FPDFSDK_PDFWINDOW_CPWL_ICON_H_
 #define FPDFSDK_PDFWINDOW_CPWL_ICON_H_
 
+#include <utility>
+
 #include "core/fxcrt/cfx_unowned_ptr.h"
 #include "core/fxcrt/fx_string.h"
 #include "fpdfsdk/pdfwindow/cpwl_wnd.h"
 
-class CPWL_Image : public CPWL_Wnd {
- public:
-  CPWL_Image();
-  ~CPWL_Image() override;
-
-  virtual void GetScale(float& fHScale, float& fVScale);
-  virtual void GetImageOffset(float& x, float& y);
-
-  CPDF_Stream* GetPDFStream() const;
-  void SetPDFStream(CPDF_Stream* pStream);
-  void GetImageSize(float& fWidth, float& fHeight);
-  CFX_Matrix GetImageMatrix();
-  CFX_ByteString GetImageAlias();
-  void SetImageAlias(const char* sImageAlias);
-
- protected:
-  CFX_UnownedPtr<CPDF_Stream> m_pPDFStream;
-  CFX_ByteString m_sImageAlias;
-};
-
-class CPWL_Icon : public CPWL_Image {
+class CPWL_Icon : public CPWL_Wnd {
  public:
   CPWL_Icon();
   ~CPWL_Icon() override;
 
-  CPDF_IconFit* GetIconFit() const;
-
-  // CPWL_Image
-  void GetScale(float& fHScale, float& fVScale) override;
-  void GetImageOffset(float& x, float& y) override;
-
-  int32_t GetScaleMethod();
-  bool IsProportionalScale();
-  void GetIconPosition(float& fLeft, float& fBottom);
-
   void SetIconFit(CPDF_IconFit* pIconFit) { m_pIconFit = pIconFit; }
+  void SetPDFStream(CPDF_Stream* pStream) { m_pPDFStream = pStream; }
+
+  // horizontal scale, vertical scale
+  std::pair<float, float> GetScale();
+
+  // x, y
+  std::pair<float, float> GetImageOffset();
+
+  CFX_Matrix GetImageMatrix();
+  CFX_ByteString GetImageAlias();
 
  private:
+  // left, bottom
+  std::pair<float, float> GetIconPosition();
+
+  // width, height
+  std::pair<float, float> GetImageSize();
+
+  CFX_UnownedPtr<CPDF_Stream> m_pPDFStream;
   CFX_UnownedPtr<CPDF_IconFit> m_pIconFit;
 };