Move most logic out of CPWL_Icon

The calculations are better performed by CPDF_IconFit.

Change-Id: Ia5a71ee75fc9e4432ad2e1897c2d8bb71e05b1c2
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/79670
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfdoc/cpdf_iconfit.cpp b/core/fpdfdoc/cpdf_iconfit.cpp
index 36e19da..fbbf508 100644
--- a/core/fpdfdoc/cpdf_iconfit.cpp
+++ b/core/fpdfdoc/cpdf_iconfit.cpp
@@ -74,3 +74,63 @@
   return {dwCount > 0 ? pA->GetNumberAt(0) : 0.0f,
           dwCount > 1 ? pA->GetNumberAt(1) : 0.0f};
 }
+
+std::pair<float, float> CPDF_IconFit::GetScale(
+    const CFX_SizeF& image_size,
+    const CFX_FloatRect& rcPlate) const {
+  float fHScale = 1.0f;
+  float fVScale = 1.0f;
+  float fPlateWidth = rcPlate.Width();
+  float fPlateHeight = rcPlate.Height();
+  float fImageWidth = image_size.width;
+  float fImageHeight = image_size.height;
+  ScaleMethod scale_method = GetScaleMethod();
+  switch (scale_method) {
+    case CPDF_IconFit::ScaleMethod::kAlways:
+      fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
+      fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
+      break;
+    case CPDF_IconFit::ScaleMethod::kBigger:
+      if (fPlateWidth < fImageWidth)
+        fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
+      if (fPlateHeight < fImageHeight)
+        fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
+      break;
+    case CPDF_IconFit::ScaleMethod::kSmaller:
+      if (fPlateWidth > fImageWidth)
+        fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
+      if (fPlateHeight > fImageHeight)
+        fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
+      break;
+    case CPDF_IconFit::ScaleMethod::kNever:
+      break;
+  }
+
+  if (IsProportionalScale()) {
+    float min_scale = std::min(fHScale, fVScale);
+    fHScale = min_scale;
+    fVScale = min_scale;
+  }
+  return {fHScale, fVScale};
+}
+
+std::pair<float, float> CPDF_IconFit::GetImageOffset(
+    const CFX_SizeF& image_size,
+    const CFX_FloatRect& rcPlate) const {
+  CFX_PointF icon_position = GetIconPosition();
+  float fLeft = icon_position.x;
+  float fBottom = icon_position.y;
+  float fImageWidth = image_size.width;
+  float fImageHeight = image_size.height;
+
+  float fHScale, fVScale;
+  std::tie(fHScale, fVScale) = GetScale(image_size, rcPlate);
+
+  float fImageFactWidth = fImageWidth * fHScale;
+  float fImageFactHeight = fImageHeight * fVScale;
+  float fPlateWidth = rcPlate.Width();
+  float fPlateHeight = rcPlate.Height();
+
+  return {(fPlateWidth - fImageFactWidth) * fLeft,
+          (fPlateHeight - fImageFactHeight) * fBottom};
+}
diff --git a/core/fpdfdoc/cpdf_iconfit.h b/core/fpdfdoc/cpdf_iconfit.h
index 8840980..7bd111e 100644
--- a/core/fpdfdoc/cpdf_iconfit.h
+++ b/core/fpdfdoc/cpdf_iconfit.h
@@ -7,6 +7,8 @@
 #ifndef CORE_FPDFDOC_CPDF_ICONFIT_H_
 #define CORE_FPDFDOC_CPDF_ICONFIT_H_
 
+#include <utility>
+
 #include "core/fxcrt/fx_coordinates.h"
 #include "core/fxcrt/fx_system.h"
 #include "core/fxcrt/retain_ptr.h"
@@ -25,9 +27,14 @@
   bool IsProportionalScale() const;
   bool GetFittingBounds() const;
   CFX_PointF GetIconBottomLeftPosition() const;
-  CFX_PointF GetIconPosition() const;
+  std::pair<float, float> GetImageOffset(const CFX_SizeF& image_size,
+                                         const CFX_FloatRect& rcPlate) const;
+  std::pair<float, float> GetScale(const CFX_SizeF& image_size,
+                                   const CFX_FloatRect& rcPlate) const;
 
  private:
+  CFX_PointF GetIconPosition() const;
+
   RetainPtr<const CPDF_Dictionary> const m_pDict;
 };
 
diff --git a/fpdfsdk/cpdfsdk_appstream.cpp b/fpdfsdk/cpdfsdk_appstream.cpp
index e2b865c..13d49e8 100644
--- a/fpdfsdk/cpdfsdk_appstream.cpp
+++ b/fpdfsdk/cpdfsdk_appstream.cpp
@@ -690,7 +690,7 @@
   cp.dwFlags = PWS_VISIBLE;
 
   auto pPDFIcon = std::make_unique<CPDF_Icon>(pIconStream);
-  auto pIcon = std::make_unique<CPWL_Icon>(cp, pPDFIcon.get(), &fit);
+  auto pIcon = std::make_unique<CPWL_Icon>(cp);
   pIcon->Realize();
   if (!pIcon->Move(rcIcon, false, false))
     return ByteString();
@@ -700,15 +700,16 @@
     return ByteString();
 
   CFX_FloatRect rcPlate = pIcon->GetClientRect();
+  CFX_SizeF image_size = pPDFIcon->GetImageSize();
   CFX_Matrix mt = pPDFIcon->GetImageMatrix().GetInverse();
 
   float fHScale;
   float fVScale;
-  std::tie(fHScale, fVScale) = pIcon->GetScale();
+  std::tie(fHScale, fVScale) = fit.GetScale(image_size, rcPlate);
 
   float fx;
   float fy;
-  std::tie(fx, fy) = pIcon->GetImageOffset();
+  std::tie(fx, fy) = fit.GetImageOffset(image_size, rcPlate);
 
   std::ostringstream str;
   {
diff --git a/fpdfsdk/pwl/cpwl_icon.cpp b/fpdfsdk/pwl/cpwl_icon.cpp
index b5a2e89..be9be00 100644
--- a/fpdfsdk/pwl/cpwl_icon.cpp
+++ b/fpdfsdk/pwl/cpwl_icon.cpp
@@ -15,77 +15,6 @@
 #include "fpdfsdk/pwl/cpwl_wnd.h"
 #include "third_party/base/check.h"
 
-CPWL_Icon::CPWL_Icon(const CreateParams& cp,
-                     CPDF_Icon* pIcon,
-                     CPDF_IconFit* pFit)
-    : CPWL_Wnd(cp, nullptr), m_pIcon(pIcon), m_pIconFit(pFit) {
-  DCHECK(m_pIcon);
-  DCHECK(m_pIconFit);
-}
+CPWL_Icon::CPWL_Icon(const CreateParams& cp) : CPWL_Wnd(cp, nullptr) {}
 
 CPWL_Icon::~CPWL_Icon() = default;
-
-std::pair<float, float> CPWL_Icon::GetScale() {
-  float fHScale = 1.0f;
-  float fVScale = 1.0f;
-
-  CFX_FloatRect rcPlate = GetClientRect();
-  float fPlateWidth = rcPlate.Width();
-  float fPlateHeight = rcPlate.Height();
-
-  CFX_SizeF image_size = m_pIcon->GetImageSize();
-  float fImageWidth = image_size.width;
-  float fImageHeight = image_size.height;
-
-  CPDF_IconFit::ScaleMethod scale_method = m_pIconFit->GetScaleMethod();
-  switch (scale_method) {
-    case CPDF_IconFit::ScaleMethod::kAlways:
-      fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
-      fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
-      break;
-    case CPDF_IconFit::ScaleMethod::kBigger:
-      if (fPlateWidth < fImageWidth)
-        fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
-      if (fPlateHeight < fImageHeight)
-        fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
-      break;
-    case CPDF_IconFit::ScaleMethod::kSmaller:
-      if (fPlateWidth > fImageWidth)
-        fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
-      if (fPlateHeight > fImageHeight)
-        fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
-      break;
-    case CPDF_IconFit::ScaleMethod::kNever:
-      break;
-  }
-
-  if (m_pIconFit->IsProportionalScale()) {
-    float min_scale = std::min(fHScale, fVScale);
-    fHScale = min_scale;
-    fVScale = min_scale;
-  }
-  return {fHScale, fVScale};
-}
-
-std::pair<float, float> CPWL_Icon::GetImageOffset() {
-  CFX_PointF icon_position = m_pIconFit->GetIconPosition();
-  float fLeft = icon_position.x;
-  float fBottom = icon_position.y;
-
-  CFX_SizeF image_size = m_pIcon->GetImageSize();
-  float fImageWidth = image_size.width;
-  float fImageHeight = image_size.height;
-
-  float fHScale, fVScale;
-  std::tie(fHScale, fVScale) = GetScale();
-
-  float fImageFactWidth = fImageWidth * fHScale;
-  float fImageFactHeight = fImageHeight * fVScale;
-
-  CFX_FloatRect rcPlate = GetClientRect();
-  float fPlateWidth = rcPlate.Width();
-  float fPlateHeight = rcPlate.Height();
-
-  return {(fPlateWidth - fImageFactWidth) * fLeft,
-          (fPlateHeight - fImageFactHeight) * fBottom};
-}
diff --git a/fpdfsdk/pwl/cpwl_icon.h b/fpdfsdk/pwl/cpwl_icon.h
index dc6e624..cb66c50 100644
--- a/fpdfsdk/pwl/cpwl_icon.h
+++ b/fpdfsdk/pwl/cpwl_icon.h
@@ -7,28 +7,12 @@
 #ifndef FPDFSDK_PWL_CPWL_ICON_H_
 #define FPDFSDK_PWL_CPWL_ICON_H_
 
-#include <utility>
-
-#include "core/fxcrt/unowned_ptr.h"
 #include "fpdfsdk/pwl/cpwl_wnd.h"
 
-class CPDF_Icon;
-class CPDF_IconFit;
-
 class CPWL_Icon final : public CPWL_Wnd {
  public:
-  CPWL_Icon(const CreateParams& cp, CPDF_Icon* pIcon, CPDF_IconFit* pFit);
+  CPWL_Icon(const CreateParams& cp);
   ~CPWL_Icon() override;
-
-  // horizontal scale, vertical scale
-  std::pair<float, float> GetScale();
-
-  // x, y
-  std::pair<float, float> GetImageOffset();
-
- private:
-  UnownedPtr<const CPDF_Icon> const m_pIcon;
-  UnownedPtr<const CPDF_IconFit> const m_pIconFit;
 };
 
 #endif  // FPDFSDK_PWL_CPWL_ICON_H_