John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 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. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 7 | #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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 10 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 11 | CPWL_Image::CPWL_Image() : m_pPDFStream(NULL) {} |
| 12 | |
| 13 | CPWL_Image::~CPWL_Image() {} |
| 14 | |
| 15 | CFX_ByteString CPWL_Image::GetImageAppStream() { |
| 16 | CFX_ByteTextBuf sAppStream; |
| 17 | |
| 18 | CFX_ByteString sAlias = GetImageAlias(); |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 19 | CFX_FloatRect rcPlate = GetClientRect(); |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 20 | CFX_Matrix mt; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 21 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 49 | void CPWL_Image::SetPDFStream(CPDF_Stream* pStream) { |
| 50 | m_pPDFStream = pStream; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 53 | CPDF_Stream* CPWL_Image::GetPDFStream() { |
| 54 | return m_pPDFStream; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 57 | void 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 Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 63 | CFX_FloatRect rect = pDict->GetRectBy("BBox"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 64 | |
| 65 | fWidth = rect.right - rect.left; |
| 66 | fHeight = rect.top - rect.bottom; |
| 67 | } |
| 68 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 71 | CFX_Matrix CPWL_Image::GetImageMatrix() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 72 | if (m_pPDFStream) { |
| 73 | if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 74 | return pDict->GetMatrixBy("Matrix"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 78 | return CFX_Matrix(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 81 | CFX_ByteString CPWL_Image::GetImageAlias() { |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 82 | if (!m_sImageAlias.IsEmpty()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 83 | return m_sImageAlias; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 84 | |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 85 | if (m_pPDFStream) { |
| 86 | if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 87 | return pDict->GetStringBy("Name"); |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 91 | return CFX_ByteString(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 94 | void CPWL_Image::SetImageAlias(const FX_CHAR* sImageAlias) { |
| 95 | m_sImageAlias = sImageAlias; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 98 | void CPWL_Image::GetScale(FX_FLOAT& fHScale, FX_FLOAT& fVScale) { |
| 99 | fHScale = 1.0f; |
| 100 | fVScale = 1.0f; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 103 | void CPWL_Image::GetImageOffset(FX_FLOAT& x, FX_FLOAT& y) { |
| 104 | x = 0.0f; |
| 105 | y = 0.0f; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 108 | CPWL_Icon::CPWL_Icon() : m_pIconFit(NULL) {} |
| 109 | |
| 110 | CPWL_Icon::~CPWL_Icon() {} |
| 111 | |
| 112 | int32_t CPWL_Icon::GetScaleMethod() { |
| 113 | if (m_pIconFit) |
| 114 | return m_pIconFit->GetScaleMethod(); |
| 115 | |
| 116 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 119 | FX_BOOL CPWL_Icon::IsProportionalScale() { |
| 120 | if (m_pIconFit) |
| 121 | return m_pIconFit->IsProportionalScale(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 122 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 123 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 126 | void CPWL_Icon::GetIconPosition(FX_FLOAT& fLeft, FX_FLOAT& fBottom) { |
| 127 | if (m_pIconFit) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 128 | fLeft = 0.0f; |
| 129 | fBottom = 0.0f; |
| 130 | CPDF_Array* pA = |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame^] | 131 | m_pIconFit->GetDict() ? m_pIconFit->GetDict()->GetArrayBy("A") : NULL; |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 132 | if (pA) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 133 | FX_DWORD dwCount = pA->GetCount(); |
| 134 | if (dwCount > 0) |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 135 | fLeft = pA->GetNumberAt(0); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | if (dwCount > 1) |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 137 | fBottom = pA->GetNumberAt(1); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 138 | } |
| 139 | } else { |
| 140 | fLeft = 0.0f; |
| 141 | fBottom = 0.0f; |
| 142 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 145 | FX_BOOL CPWL_Icon::GetFittingBounds() { |
| 146 | if (m_pIconFit) |
| 147 | return m_pIconFit->GetFittingBounds(); |
| 148 | |
| 149 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 152 | void CPWL_Icon::GetScale(FX_FLOAT& fHScale, FX_FLOAT& fVScale) { |
| 153 | fHScale = 1.0f; |
| 154 | fVScale = 1.0f; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 155 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 156 | if (m_pPDFStream) { |
| 157 | FX_FLOAT fImageWidth, fImageHeight; |
| 158 | FX_FLOAT fPlateWidth, fPlateHeight; |
| 159 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 160 | CFX_FloatRect rcPlate = GetClientRect(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 161 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 199 | void CPWL_Icon::GetImageOffset(FX_FLOAT& x, FX_FLOAT& y) { |
| 200 | FX_FLOAT fLeft, fBottom; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 201 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 202 | 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 Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 216 | CFX_FloatRect rcPlate = GetClientRect(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 217 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 222 | } |