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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../include/pdfwindow/PDFWindow.h"
8 #include "../../include/pdfwindow/PWL_Wnd.h"
9 #include "../../include/pdfwindow/PWL_Icon.h"
10 #include "../../include/pdfwindow/PWL_Utils.h"
12 /* ------------------------------- CPWL_Image ----------------------------------
15 CPWL_Image::CPWL_Image() : m_pPDFStream(NULL) {}
17 CPWL_Image::~CPWL_Image() {}
19 CFX_ByteString CPWL_Image::GetImageAppStream() {
20 CFX_ByteTextBuf sAppStream;
22 CFX_ByteString sAlias = GetImageAlias();
23 CPDF_Rect rcPlate = GetClientRect();
25 mt.SetReverse(GetImageMatrix());
27 FX_FLOAT fHScale = 1.0f;
28 FX_FLOAT fVScale = 1.0f;
29 GetScale(fHScale, fVScale);
33 GetImageOffset(fx, fy);
35 if (m_pPDFStream && sAlias.GetLength() > 0) {
37 sAppStream << rcPlate.left << " " << rcPlate.bottom << " "
38 << rcPlate.right - rcPlate.left << " "
39 << rcPlate.top - rcPlate.bottom << " re W n\n";
41 sAppStream << fHScale << " 0 0 " << fVScale << " " << rcPlate.left + fx
42 << " " << rcPlate.bottom + fy << " cm\n";
43 sAppStream << mt.GetA() << " " << mt.GetB() << " " << mt.GetC() << " "
44 << mt.GetD() << " " << mt.GetE() << " " << mt.GetF() << " cm\n";
46 sAppStream << "0 g 0 G 1 w /" << sAlias << " Do\n"
50 return sAppStream.GetByteString();
53 void CPWL_Image::SetPDFStream(CPDF_Stream* pStream) {
54 m_pPDFStream = pStream;
57 CPDF_Stream* CPWL_Image::GetPDFStream() {
61 void CPWL_Image::GetImageSize(FX_FLOAT& fWidth, FX_FLOAT& fHeight) {
66 if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) {
67 CPDF_Rect rect = pDict->GetRect("BBox");
69 fWidth = rect.right - rect.left;
70 fHeight = rect.top - rect.bottom;
75 CPDF_Matrix CPWL_Image::GetImageMatrix() {
77 if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) {
78 return pDict->GetMatrix("Matrix");
85 CFX_ByteString CPWL_Image::GetImageAlias() {
86 if (m_sImageAlias.IsEmpty()) {
88 if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) {
89 return pDict->GetString("Name");
95 return CFX_ByteString();
98 void CPWL_Image::SetImageAlias(const FX_CHAR* sImageAlias) {
99 m_sImageAlias = sImageAlias;
102 void CPWL_Image::GetScale(FX_FLOAT& fHScale, FX_FLOAT& fVScale) {
107 void CPWL_Image::GetImageOffset(FX_FLOAT& x, FX_FLOAT& y) {
112 /* ------------------------------- CPWL_Icon ----------------------------------
115 CPWL_Icon::CPWL_Icon() : m_pIconFit(NULL) {}
117 CPWL_Icon::~CPWL_Icon() {}
119 int32_t CPWL_Icon::GetScaleMethod() {
121 return m_pIconFit->GetScaleMethod();
126 FX_BOOL CPWL_Icon::IsProportionalScale() {
128 return m_pIconFit->IsProportionalScale();
133 void CPWL_Icon::GetIconPosition(FX_FLOAT& fLeft, FX_FLOAT& fBottom) {
135 // m_pIconFit->GetIconPosition(fLeft,fBottom);
139 m_pIconFit->m_pDict ? m_pIconFit->m_pDict->GetArray("A") : NULL;
141 FX_DWORD dwCount = pA->GetCount();
143 fLeft = pA->GetNumber(0);
145 fBottom = pA->GetNumber(1);
153 FX_BOOL CPWL_Icon::GetFittingBounds() {
155 return m_pIconFit->GetFittingBounds();
160 void CPWL_Icon::GetScale(FX_FLOAT& fHScale, FX_FLOAT& fVScale) {
165 FX_FLOAT fImageWidth, fImageHeight;
166 FX_FLOAT fPlateWidth, fPlateHeight;
168 CPDF_Rect rcPlate = GetClientRect();
169 fPlateWidth = rcPlate.right - rcPlate.left;
170 fPlateHeight = rcPlate.top - rcPlate.bottom;
172 GetImageSize(fImageWidth, fImageHeight);
174 int32_t nScaleMethod = GetScaleMethod();
176 switch (nScaleMethod) {
179 fHScale = fPlateWidth / PWL_MAX(fImageWidth, 1.0f);
180 fVScale = fPlateHeight / PWL_MAX(fImageHeight, 1.0f);
183 if (fPlateWidth < fImageWidth)
184 fHScale = fPlateWidth / PWL_MAX(fImageWidth, 1.0f);
185 if (fPlateHeight < fImageHeight)
186 fVScale = fPlateHeight / PWL_MAX(fImageHeight, 1.0f);
189 if (fPlateWidth > fImageWidth)
190 fHScale = fPlateWidth / PWL_MAX(fImageWidth, 1.0f);
191 if (fPlateHeight > fImageHeight)
192 fVScale = fPlateHeight / PWL_MAX(fImageHeight, 1.0f);
199 if (IsProportionalScale()) {
200 fMinScale = PWL_MIN(fHScale, fVScale);
207 void CPWL_Icon::GetImageOffset(FX_FLOAT& x, FX_FLOAT& y) {
208 FX_FLOAT fLeft, fBottom;
210 GetIconPosition(fLeft, fBottom);
214 FX_FLOAT fImageWidth, fImageHeight;
215 GetImageSize(fImageWidth, fImageHeight);
217 FX_FLOAT fHScale, fVScale;
218 GetScale(fHScale, fVScale);
220 FX_FLOAT fImageFactWidth = fImageWidth * fHScale;
221 FX_FLOAT fImageFactHeight = fImageHeight * fVScale;
223 FX_FLOAT fPlateWidth, fPlateHeight;
224 CPDF_Rect rcPlate = GetClientRect();
225 fPlateWidth = rcPlate.right - rcPlate.left;
226 fPlateHeight = rcPlate.top - rcPlate.bottom;
228 x = (fPlateWidth - fImageFactWidth) * fLeft;
229 y = (fPlateHeight - fImageFactHeight) * fBottom;