1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "fpdfsdk/pwl/cpwl_icon.h"
8
9 #include <algorithm>
10 #include <sstream>
11 #include <utility>
12
13 #include "core/fpdfdoc/cpdf_icon.h"
14 #include "core/fpdfdoc/cpdf_iconfit.h"
15 #include "fpdfsdk/pwl/cpwl_wnd.h"
16
CPWL_Icon(const CreateParams & cp,std::unique_ptr<CPDF_Icon> pIcon,CPDF_IconFit * pFit)17 CPWL_Icon::CPWL_Icon(const CreateParams& cp,
18 std::unique_ptr<CPDF_Icon> pIcon,
19 CPDF_IconFit* pFit)
20 : CPWL_Wnd(cp, nullptr), m_pIcon(std::move(pIcon)), m_pIconFit(pFit) {
21 ASSERT(m_pIcon);
22 }
23
24 CPWL_Icon::~CPWL_Icon() = default;
25
GetImageSize()26 CFX_SizeF CPWL_Icon::GetImageSize() {
27 return m_pIcon->GetImageSize();
28 }
29
GetImageMatrix()30 CFX_Matrix CPWL_Icon::GetImageMatrix() {
31 return m_pIcon->GetImageMatrix();
32 }
33
GetImageAlias()34 ByteString CPWL_Icon::GetImageAlias() {
35 return m_pIcon->GetImageAlias();
36 }
37
GetIconPosition()38 CFX_PointF CPWL_Icon::GetIconPosition() {
39 if (!m_pIconFit)
40 return CFX_PointF();
41
42 return m_pIconFit->GetIconPosition();
43 }
44
GetScale()45 std::pair<float, float> CPWL_Icon::GetScale() {
46 float fHScale = 1.0f;
47 float fVScale = 1.0f;
48
49 CFX_FloatRect rcPlate = GetClientRect();
50 float fPlateWidth = rcPlate.Width();
51 float fPlateHeight = rcPlate.Height();
52
53 CFX_SizeF image_size = GetImageSize();
54 float fImageWidth = image_size.width;
55 float fImageHeight = image_size.height;
56 int32_t nScaleMethod = m_pIconFit ? m_pIconFit->GetScaleMethod() : 0;
57
58 switch (nScaleMethod) {
59 default:
60 case 0:
61 fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
62 fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
63 break;
64 case 1:
65 if (fPlateWidth < fImageWidth)
66 fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
67 if (fPlateHeight < fImageHeight)
68 fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
69 break;
70 case 2:
71 if (fPlateWidth > fImageWidth)
72 fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
73 if (fPlateHeight > fImageHeight)
74 fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
75 break;
76 case 3:
77 break;
78 }
79
80 float fMinScale;
81 if (m_pIconFit && m_pIconFit->IsProportionalScale()) {
82 fMinScale = std::min(fHScale, fVScale);
83 fHScale = fMinScale;
84 fVScale = fMinScale;
85 }
86 return {fHScale, fVScale};
87 }
88
GetImageOffset()89 std::pair<float, float> CPWL_Icon::GetImageOffset() {
90 CFX_PointF icon_position = GetIconPosition();
91 float fLeft = icon_position.x;
92 float fBottom = icon_position.y;
93
94 CFX_SizeF image_size = GetImageSize();
95 float fImageWidth = image_size.width;
96 float fImageHeight = image_size.height;
97
98 float fHScale, fVScale;
99 std::tie(fHScale, fVScale) = GetScale();
100
101 float fImageFactWidth = fImageWidth * fHScale;
102 float fImageFactHeight = fImageHeight * fVScale;
103
104 CFX_FloatRect rcPlate = GetClientRect();
105 float fPlateWidth = rcPlate.Width();
106 float fPlateHeight = rcPlate.Height();
107
108 return {(fPlateWidth - fImageFactWidth) * fLeft,
109 (fPlateHeight - fImageFactHeight) * fBottom};
110 }
111