blob: d8e11c2c02fa99773d550c9ed07a5305b60b5853 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangbde53d22015-11-12 22:21:30 -08007#include "fpdfsdk/include/pdfwindow/PWL_Icon.h"
8#include "fpdfsdk/include/pdfwindow/PWL_Utils.h"
9#include "fpdfsdk/include/pdfwindow/PWL_Wnd.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070010
Nico Weber9d8ec5a2015-08-04 13:00:21 -070011CPWL_Image::CPWL_Image() : m_pPDFStream(NULL) {}
12
13CPWL_Image::~CPWL_Image() {}
14
15CFX_ByteString CPWL_Image::GetImageAppStream() {
16 CFX_ByteTextBuf sAppStream;
17
18 CFX_ByteString sAlias = GetImageAlias();
Tom Sepez281a9ea2016-02-26 14:24:28 -080019 CFX_FloatRect rcPlate = GetClientRect();
Tom Sepez60d909e2015-12-10 15:34:55 -080020 CFX_Matrix mt;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021 mt.SetReverse(GetImageMatrix());
22
23 FX_FLOAT fHScale = 1.0f;
24 FX_FLOAT fVScale = 1.0f;
25 GetScale(fHScale, fVScale);
26
27 FX_FLOAT fx = 0.0f;
28 FX_FLOAT fy = 0.0f;
29 GetImageOffset(fx, fy);
30
31 if (m_pPDFStream && sAlias.GetLength() > 0) {
32 sAppStream << "q\n";
33 sAppStream << rcPlate.left << " " << rcPlate.bottom << " "
34 << rcPlate.right - rcPlate.left << " "
35 << rcPlate.top - rcPlate.bottom << " re W n\n";
36
37 sAppStream << fHScale << " 0 0 " << fVScale << " " << rcPlate.left + fx
38 << " " << rcPlate.bottom + fy << " cm\n";
39 sAppStream << mt.GetA() << " " << mt.GetB() << " " << mt.GetC() << " "
40 << mt.GetD() << " " << mt.GetE() << " " << mt.GetF() << " cm\n";
41
42 sAppStream << "0 g 0 G 1 w /" << sAlias << " Do\n"
43 << "Q\n";
44 }
45
46 return sAppStream.GetByteString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047}
48
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049void CPWL_Image::SetPDFStream(CPDF_Stream* pStream) {
50 m_pPDFStream = pStream;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051}
52
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053CPDF_Stream* CPWL_Image::GetPDFStream() {
54 return m_pPDFStream;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055}
56
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057void CPWL_Image::GetImageSize(FX_FLOAT& fWidth, FX_FLOAT& fHeight) {
58 fWidth = 0.0f;
59 fHeight = 0.0f;
60
61 if (m_pPDFStream) {
62 if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -080063 CFX_FloatRect rect = pDict->GetRectBy("BBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064
65 fWidth = rect.right - rect.left;
66 fHeight = rect.top - rect.bottom;
67 }
68 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070069}
70
Tom Sepez60d909e2015-12-10 15:34:55 -080071CFX_Matrix CPWL_Image::GetImageMatrix() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 if (m_pPDFStream) {
73 if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -080074 return pDict->GetMatrixBy("Matrix");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 }
76 }
77
Tom Sepez60d909e2015-12-10 15:34:55 -080078 return CFX_Matrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079}
80
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081CFX_ByteString CPWL_Image::GetImageAlias() {
Lei Zhangc2fb35f2016-01-05 16:46:58 -080082 if (!m_sImageAlias.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 return m_sImageAlias;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084
Lei Zhangc2fb35f2016-01-05 16:46:58 -080085 if (m_pPDFStream) {
86 if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -080087 return pDict->GetStringBy("Name");
Lei Zhangc2fb35f2016-01-05 16:46:58 -080088 }
89 }
90
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 return CFX_ByteString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092}
93
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094void CPWL_Image::SetImageAlias(const FX_CHAR* sImageAlias) {
95 m_sImageAlias = sImageAlias;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096}
97
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098void CPWL_Image::GetScale(FX_FLOAT& fHScale, FX_FLOAT& fVScale) {
99 fHScale = 1.0f;
100 fVScale = 1.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101}
102
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103void CPWL_Image::GetImageOffset(FX_FLOAT& x, FX_FLOAT& y) {
104 x = 0.0f;
105 y = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106}
107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108CPWL_Icon::CPWL_Icon() : m_pIconFit(NULL) {}
109
110CPWL_Icon::~CPWL_Icon() {}
111
112int32_t CPWL_Icon::GetScaleMethod() {
113 if (m_pIconFit)
114 return m_pIconFit->GetScaleMethod();
115
116 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117}
118
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119FX_BOOL CPWL_Icon::IsProportionalScale() {
120 if (m_pIconFit)
121 return m_pIconFit->IsProportionalScale();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700122
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700124}
125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126void CPWL_Icon::GetIconPosition(FX_FLOAT& fLeft, FX_FLOAT& fBottom) {
127 if (m_pIconFit) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 fLeft = 0.0f;
129 fBottom = 0.0f;
130 CPDF_Array* pA =
Wei Li0fc6b252016-03-01 16:29:41 -0800131 m_pIconFit->GetDict() ? m_pIconFit->GetDict()->GetArrayBy("A") : NULL;
Lei Zhang96660d62015-12-14 18:27:25 -0800132 if (pA) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 FX_DWORD dwCount = pA->GetCount();
134 if (dwCount > 0)
Wei Li9b761132016-01-29 15:44:20 -0800135 fLeft = pA->GetNumberAt(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 if (dwCount > 1)
Wei Li9b761132016-01-29 15:44:20 -0800137 fBottom = pA->GetNumberAt(1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 }
139 } else {
140 fLeft = 0.0f;
141 fBottom = 0.0f;
142 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700143}
144
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145FX_BOOL CPWL_Icon::GetFittingBounds() {
146 if (m_pIconFit)
147 return m_pIconFit->GetFittingBounds();
148
149 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700150}
151
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152void CPWL_Icon::GetScale(FX_FLOAT& fHScale, FX_FLOAT& fVScale) {
153 fHScale = 1.0f;
154 fVScale = 1.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700155
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 if (m_pPDFStream) {
157 FX_FLOAT fImageWidth, fImageHeight;
158 FX_FLOAT fPlateWidth, fPlateHeight;
159
Tom Sepez281a9ea2016-02-26 14:24:28 -0800160 CFX_FloatRect rcPlate = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 fPlateWidth = rcPlate.right - rcPlate.left;
162 fPlateHeight = rcPlate.top - rcPlate.bottom;
163
164 GetImageSize(fImageWidth, fImageHeight);
165
166 int32_t nScaleMethod = GetScaleMethod();
167
168 switch (nScaleMethod) {
169 default:
170 case 0:
171 fHScale = fPlateWidth / PWL_MAX(fImageWidth, 1.0f);
172 fVScale = fPlateHeight / PWL_MAX(fImageHeight, 1.0f);
173 break;
174 case 1:
175 if (fPlateWidth < fImageWidth)
176 fHScale = fPlateWidth / PWL_MAX(fImageWidth, 1.0f);
177 if (fPlateHeight < fImageHeight)
178 fVScale = fPlateHeight / PWL_MAX(fImageHeight, 1.0f);
179 break;
180 case 2:
181 if (fPlateWidth > fImageWidth)
182 fHScale = fPlateWidth / PWL_MAX(fImageWidth, 1.0f);
183 if (fPlateHeight > fImageHeight)
184 fVScale = fPlateHeight / PWL_MAX(fImageHeight, 1.0f);
185 break;
186 case 3:
187 break;
188 }
189
190 FX_FLOAT fMinScale;
191 if (IsProportionalScale()) {
192 fMinScale = PWL_MIN(fHScale, fVScale);
193 fHScale = fMinScale;
194 fVScale = fMinScale;
195 }
196 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700197}
198
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199void CPWL_Icon::GetImageOffset(FX_FLOAT& x, FX_FLOAT& y) {
200 FX_FLOAT fLeft, fBottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 GetIconPosition(fLeft, fBottom);
203 x = 0.0f;
204 y = 0.0f;
205
206 FX_FLOAT fImageWidth, fImageHeight;
207 GetImageSize(fImageWidth, fImageHeight);
208
209 FX_FLOAT fHScale, fVScale;
210 GetScale(fHScale, fVScale);
211
212 FX_FLOAT fImageFactWidth = fImageWidth * fHScale;
213 FX_FLOAT fImageFactHeight = fImageHeight * fVScale;
214
215 FX_FLOAT fPlateWidth, fPlateHeight;
Tom Sepez281a9ea2016-02-26 14:24:28 -0800216 CFX_FloatRect rcPlate = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217 fPlateWidth = rcPlate.right - rcPlate.left;
218 fPlateHeight = rcPlate.top - rcPlate.bottom;
219
220 x = (fPlateWidth - fImageFactWidth) * fLeft;
221 y = (fPlateHeight - fImageFactHeight) * fBottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700222}