| OLD | NEW |
| (Empty) |
| 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 "core/include/fpdfapi/cpdf_array.h" | |
| 8 #include "fpdfsdk/include/pdfwindow/PWL_Icon.h" | |
| 9 #include "fpdfsdk/include/pdfwindow/PWL_Utils.h" | |
| 10 #include "fpdfsdk/include/pdfwindow/PWL_Wnd.h" | |
| 11 | |
| 12 CPWL_Image::CPWL_Image() : m_pPDFStream(NULL) {} | |
| 13 | |
| 14 CPWL_Image::~CPWL_Image() {} | |
| 15 | |
| 16 CFX_ByteString CPWL_Image::GetImageAppStream() { | |
| 17 CFX_ByteTextBuf sAppStream; | |
| 18 | |
| 19 CFX_ByteString sAlias = GetImageAlias(); | |
| 20 CFX_FloatRect rcPlate = GetClientRect(); | |
| 21 CFX_Matrix mt; | |
| 22 mt.SetReverse(GetImageMatrix()); | |
| 23 | |
| 24 FX_FLOAT fHScale = 1.0f; | |
| 25 FX_FLOAT fVScale = 1.0f; | |
| 26 GetScale(fHScale, fVScale); | |
| 27 | |
| 28 FX_FLOAT fx = 0.0f; | |
| 29 FX_FLOAT fy = 0.0f; | |
| 30 GetImageOffset(fx, fy); | |
| 31 | |
| 32 if (m_pPDFStream && sAlias.GetLength() > 0) { | |
| 33 sAppStream << "q\n"; | |
| 34 sAppStream << rcPlate.left << " " << rcPlate.bottom << " " | |
| 35 << rcPlate.right - rcPlate.left << " " | |
| 36 << rcPlate.top - rcPlate.bottom << " re W n\n"; | |
| 37 | |
| 38 sAppStream << fHScale << " 0 0 " << fVScale << " " << rcPlate.left + fx | |
| 39 << " " << rcPlate.bottom + fy << " cm\n"; | |
| 40 sAppStream << mt.GetA() << " " << mt.GetB() << " " << mt.GetC() << " " | |
| 41 << mt.GetD() << " " << mt.GetE() << " " << mt.GetF() << " cm\n"; | |
| 42 | |
| 43 sAppStream << "0 g 0 G 1 w /" << sAlias << " Do\n" | |
| 44 << "Q\n"; | |
| 45 } | |
| 46 | |
| 47 return sAppStream.GetByteString(); | |
| 48 } | |
| 49 | |
| 50 void CPWL_Image::SetPDFStream(CPDF_Stream* pStream) { | |
| 51 m_pPDFStream = pStream; | |
| 52 } | |
| 53 | |
| 54 CPDF_Stream* CPWL_Image::GetPDFStream() { | |
| 55 return m_pPDFStream; | |
| 56 } | |
| 57 | |
| 58 void CPWL_Image::GetImageSize(FX_FLOAT& fWidth, FX_FLOAT& fHeight) { | |
| 59 fWidth = 0.0f; | |
| 60 fHeight = 0.0f; | |
| 61 | |
| 62 if (m_pPDFStream) { | |
| 63 if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) { | |
| 64 CFX_FloatRect rect = pDict->GetRectBy("BBox"); | |
| 65 | |
| 66 fWidth = rect.right - rect.left; | |
| 67 fHeight = rect.top - rect.bottom; | |
| 68 } | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 CFX_Matrix CPWL_Image::GetImageMatrix() { | |
| 73 if (m_pPDFStream) { | |
| 74 if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) { | |
| 75 return pDict->GetMatrixBy("Matrix"); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 return CFX_Matrix(); | |
| 80 } | |
| 81 | |
| 82 CFX_ByteString CPWL_Image::GetImageAlias() { | |
| 83 if (!m_sImageAlias.IsEmpty()) | |
| 84 return m_sImageAlias; | |
| 85 | |
| 86 if (m_pPDFStream) { | |
| 87 if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) { | |
| 88 return pDict->GetStringBy("Name"); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 return CFX_ByteString(); | |
| 93 } | |
| 94 | |
| 95 void CPWL_Image::SetImageAlias(const FX_CHAR* sImageAlias) { | |
| 96 m_sImageAlias = sImageAlias; | |
| 97 } | |
| 98 | |
| 99 void CPWL_Image::GetScale(FX_FLOAT& fHScale, FX_FLOAT& fVScale) { | |
| 100 fHScale = 1.0f; | |
| 101 fVScale = 1.0f; | |
| 102 } | |
| 103 | |
| 104 void CPWL_Image::GetImageOffset(FX_FLOAT& x, FX_FLOAT& y) { | |
| 105 x = 0.0f; | |
| 106 y = 0.0f; | |
| 107 } | |
| 108 | |
| 109 CPWL_Icon::CPWL_Icon() : m_pIconFit(NULL) {} | |
| 110 | |
| 111 CPWL_Icon::~CPWL_Icon() {} | |
| 112 | |
| 113 int32_t CPWL_Icon::GetScaleMethod() { | |
| 114 if (m_pIconFit) | |
| 115 return m_pIconFit->GetScaleMethod(); | |
| 116 | |
| 117 return 0; | |
| 118 } | |
| 119 | |
| 120 FX_BOOL CPWL_Icon::IsProportionalScale() { | |
| 121 if (m_pIconFit) | |
| 122 return m_pIconFit->IsProportionalScale(); | |
| 123 | |
| 124 return FALSE; | |
| 125 } | |
| 126 | |
| 127 void CPWL_Icon::GetIconPosition(FX_FLOAT& fLeft, FX_FLOAT& fBottom) { | |
| 128 if (m_pIconFit) { | |
| 129 fLeft = 0.0f; | |
| 130 fBottom = 0.0f; | |
| 131 CPDF_Array* pA = | |
| 132 m_pIconFit->GetDict() ? m_pIconFit->GetDict()->GetArrayBy("A") : NULL; | |
| 133 if (pA) { | |
| 134 FX_DWORD dwCount = pA->GetCount(); | |
| 135 if (dwCount > 0) | |
| 136 fLeft = pA->GetNumberAt(0); | |
| 137 if (dwCount > 1) | |
| 138 fBottom = pA->GetNumberAt(1); | |
| 139 } | |
| 140 } else { | |
| 141 fLeft = 0.0f; | |
| 142 fBottom = 0.0f; | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 FX_BOOL CPWL_Icon::GetFittingBounds() { | |
| 147 if (m_pIconFit) | |
| 148 return m_pIconFit->GetFittingBounds(); | |
| 149 | |
| 150 return FALSE; | |
| 151 } | |
| 152 | |
| 153 void CPWL_Icon::GetScale(FX_FLOAT& fHScale, FX_FLOAT& fVScale) { | |
| 154 fHScale = 1.0f; | |
| 155 fVScale = 1.0f; | |
| 156 | |
| 157 if (m_pPDFStream) { | |
| 158 FX_FLOAT fImageWidth, fImageHeight; | |
| 159 FX_FLOAT fPlateWidth, fPlateHeight; | |
| 160 | |
| 161 CFX_FloatRect rcPlate = GetClientRect(); | |
| 162 fPlateWidth = rcPlate.right - rcPlate.left; | |
| 163 fPlateHeight = rcPlate.top - rcPlate.bottom; | |
| 164 | |
| 165 GetImageSize(fImageWidth, fImageHeight); | |
| 166 | |
| 167 int32_t nScaleMethod = GetScaleMethod(); | |
| 168 | |
| 169 switch (nScaleMethod) { | |
| 170 default: | |
| 171 case 0: | |
| 172 fHScale = fPlateWidth / PWL_MAX(fImageWidth, 1.0f); | |
| 173 fVScale = fPlateHeight / PWL_MAX(fImageHeight, 1.0f); | |
| 174 break; | |
| 175 case 1: | |
| 176 if (fPlateWidth < fImageWidth) | |
| 177 fHScale = fPlateWidth / PWL_MAX(fImageWidth, 1.0f); | |
| 178 if (fPlateHeight < fImageHeight) | |
| 179 fVScale = fPlateHeight / PWL_MAX(fImageHeight, 1.0f); | |
| 180 break; | |
| 181 case 2: | |
| 182 if (fPlateWidth > fImageWidth) | |
| 183 fHScale = fPlateWidth / PWL_MAX(fImageWidth, 1.0f); | |
| 184 if (fPlateHeight > fImageHeight) | |
| 185 fVScale = fPlateHeight / PWL_MAX(fImageHeight, 1.0f); | |
| 186 break; | |
| 187 case 3: | |
| 188 break; | |
| 189 } | |
| 190 | |
| 191 FX_FLOAT fMinScale; | |
| 192 if (IsProportionalScale()) { | |
| 193 fMinScale = PWL_MIN(fHScale, fVScale); | |
| 194 fHScale = fMinScale; | |
| 195 fVScale = fMinScale; | |
| 196 } | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 void CPWL_Icon::GetImageOffset(FX_FLOAT& x, FX_FLOAT& y) { | |
| 201 FX_FLOAT fLeft, fBottom; | |
| 202 | |
| 203 GetIconPosition(fLeft, fBottom); | |
| 204 x = 0.0f; | |
| 205 y = 0.0f; | |
| 206 | |
| 207 FX_FLOAT fImageWidth, fImageHeight; | |
| 208 GetImageSize(fImageWidth, fImageHeight); | |
| 209 | |
| 210 FX_FLOAT fHScale, fVScale; | |
| 211 GetScale(fHScale, fVScale); | |
| 212 | |
| 213 FX_FLOAT fImageFactWidth = fImageWidth * fHScale; | |
| 214 FX_FLOAT fImageFactHeight = fImageHeight * fVScale; | |
| 215 | |
| 216 FX_FLOAT fPlateWidth, fPlateHeight; | |
| 217 CFX_FloatRect rcPlate = GetClientRect(); | |
| 218 fPlateWidth = rcPlate.right - rcPlate.left; | |
| 219 fPlateHeight = rcPlate.top - rcPlate.bottom; | |
| 220 | |
| 221 x = (fPlateWidth - fImageFactWidth) * fLeft; | |
| 222 y = (fPlateHeight - fImageFactHeight) * fBottom; | |
| 223 } | |
| OLD | NEW |