• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The PDFium Authors
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 "core/fpdfdoc/cpdf_iconfit.h"
8 
9 #include <algorithm>
10 #include <utility>
11 
12 #include "core/fpdfapi/parser/cpdf_array.h"
13 #include "core/fpdfapi/parser/cpdf_dictionary.h"
14 #include "core/fxcrt/fx_string.h"
15 
16 namespace {
17 
18 constexpr float kDefaultPosition = 0.5f;
19 
20 }  // namespace
21 
CPDF_IconFit(RetainPtr<const CPDF_Dictionary> pDict)22 CPDF_IconFit::CPDF_IconFit(RetainPtr<const CPDF_Dictionary> pDict)
23     : m_pDict(std::move(pDict)) {}
24 
25 CPDF_IconFit::CPDF_IconFit(const CPDF_IconFit& that) = default;
26 
27 CPDF_IconFit::~CPDF_IconFit() = default;
28 
GetScaleMethod() const29 CPDF_IconFit::ScaleMethod CPDF_IconFit::GetScaleMethod() const {
30   if (!m_pDict)
31     return ScaleMethod::kAlways;
32 
33   ByteString csSW = m_pDict->GetByteStringFor("SW", "A");
34   if (csSW == "B")
35     return ScaleMethod::kBigger;
36   if (csSW == "S")
37     return ScaleMethod::kSmaller;
38   if (csSW == "N")
39     return ScaleMethod::kNever;
40   return ScaleMethod::kAlways;
41 }
42 
IsProportionalScale() const43 bool CPDF_IconFit::IsProportionalScale() const {
44   return !m_pDict || m_pDict->GetByteStringFor("S", "P") != "A";
45 }
46 
GetIconBottomLeftPosition() const47 CFX_PointF CPDF_IconFit::GetIconBottomLeftPosition() const {
48   float fLeft = kDefaultPosition;
49   float fBottom = kDefaultPosition;
50   if (!m_pDict)
51     return {fLeft, fBottom};
52 
53   RetainPtr<const CPDF_Array> pA = m_pDict->GetArrayFor("A");
54   if (!pA)
55     return {fLeft, fBottom};
56 
57   size_t dwCount = pA->size();
58   if (dwCount > 0)
59     fLeft = pA->GetFloatAt(0);
60   if (dwCount > 1)
61     fBottom = pA->GetFloatAt(1);
62   return {fLeft, fBottom};
63 }
64 
GetFittingBounds() const65 bool CPDF_IconFit::GetFittingBounds() const {
66   return m_pDict && m_pDict->GetBooleanFor("FB", false);
67 }
68 
GetIconPosition() const69 CFX_PointF CPDF_IconFit::GetIconPosition() const {
70   if (!m_pDict)
71     return CFX_PointF();
72 
73   RetainPtr<const CPDF_Array> pA = m_pDict->GetArrayFor("A");
74   if (!pA)
75     return CFX_PointF();
76 
77   size_t dwCount = pA->size();
78   return {dwCount > 0 ? pA->GetFloatAt(0) : 0.0f,
79           dwCount > 1 ? pA->GetFloatAt(1) : 0.0f};
80 }
81 
GetScale(const CFX_SizeF & image_size,const CFX_FloatRect & rcPlate) const82 CFX_VectorF CPDF_IconFit::GetScale(const CFX_SizeF& image_size,
83                                    const CFX_FloatRect& rcPlate) const {
84   float fHScale = 1.0f;
85   float fVScale = 1.0f;
86   const float fPlateWidth = rcPlate.Width();
87   const float fPlateHeight = rcPlate.Height();
88   const float fImageWidth = image_size.width;
89   const float fImageHeight = image_size.height;
90   switch (GetScaleMethod()) {
91     case CPDF_IconFit::ScaleMethod::kAlways:
92       fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
93       fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
94       break;
95     case CPDF_IconFit::ScaleMethod::kBigger:
96       if (fPlateWidth < fImageWidth)
97         fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
98       if (fPlateHeight < fImageHeight)
99         fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
100       break;
101     case CPDF_IconFit::ScaleMethod::kSmaller:
102       if (fPlateWidth > fImageWidth)
103         fHScale = fPlateWidth / std::max(fImageWidth, 1.0f);
104       if (fPlateHeight > fImageHeight)
105         fVScale = fPlateHeight / std::max(fImageHeight, 1.0f);
106       break;
107     case CPDF_IconFit::ScaleMethod::kNever:
108       break;
109   }
110 
111   if (IsProportionalScale()) {
112     float min_scale = std::min(fHScale, fVScale);
113     fHScale = min_scale;
114     fVScale = min_scale;
115   }
116   return {fHScale, fVScale};
117 }
118 
GetImageOffset(const CFX_SizeF & image_size,const CFX_VectorF & scale,const CFX_FloatRect & rcPlate) const119 CFX_VectorF CPDF_IconFit::GetImageOffset(const CFX_SizeF& image_size,
120                                          const CFX_VectorF& scale,
121                                          const CFX_FloatRect& rcPlate) const {
122   const CFX_PointF icon_position = GetIconPosition();
123   const float fImageFactWidth = image_size.width * scale.x;
124   const float fImageFactHeight = image_size.height * scale.y;
125   return {(rcPlate.Width() - fImageFactWidth) * icon_position.x,
126           (rcPlate.Height() - fImageFactHeight) * icon_position.y};
127 }
128