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 | |
dan sinclair | 89e904b | 2016-03-23 19:29:15 -0400 | [diff] [blame] | 7 | #include "fpdfsdk/pdfwindow/PWL_Icon.h" |
Dan Sinclair | aa403d3 | 2016-03-15 14:57:22 -0400 | [diff] [blame] | 8 | |
Dan Sinclair | 5f2d381 | 2017-02-14 15:42:11 -0500 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 11 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 12 | #include "core/fpdfapi/parser/cpdf_stream.h" |
dan sinclair | 89e904b | 2016-03-23 19:29:15 -0400 | [diff] [blame] | 13 | #include "fpdfsdk/pdfwindow/PWL_Utils.h" |
| 14 | #include "fpdfsdk/pdfwindow/PWL_Wnd.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 15 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 16 | CPWL_Image::CPWL_Image() : m_pPDFStream(nullptr) {} |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 17 | |
| 18 | CPWL_Image::~CPWL_Image() {} |
| 19 | |
| 20 | CFX_ByteString CPWL_Image::GetImageAppStream() { |
| 21 | CFX_ByteTextBuf sAppStream; |
| 22 | |
| 23 | CFX_ByteString sAlias = GetImageAlias(); |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 24 | CFX_FloatRect rcPlate = GetClientRect(); |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 25 | CFX_Matrix mt; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 26 | mt.SetReverse(GetImageMatrix()); |
| 27 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 28 | float fHScale = 1.0f; |
| 29 | float fVScale = 1.0f; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 30 | GetScale(fHScale, fVScale); |
| 31 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 32 | float fx = 0.0f; |
| 33 | float fy = 0.0f; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 34 | GetImageOffset(fx, fy); |
| 35 | |
| 36 | if (m_pPDFStream && sAlias.GetLength() > 0) { |
| 37 | sAppStream << "q\n"; |
| 38 | sAppStream << rcPlate.left << " " << rcPlate.bottom << " " |
| 39 | << rcPlate.right - rcPlate.left << " " |
| 40 | << rcPlate.top - rcPlate.bottom << " re W n\n"; |
| 41 | |
| 42 | sAppStream << fHScale << " 0 0 " << fVScale << " " << rcPlate.left + fx |
| 43 | << " " << rcPlate.bottom + fy << " cm\n"; |
Dan Sinclair | 687a79c | 2017-02-09 14:08:53 -0500 | [diff] [blame] | 44 | sAppStream << mt.a << " " << mt.b << " " << mt.c << " " << mt.d << " " |
| 45 | << mt.e << " " << mt.f << " cm\n"; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 46 | |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 47 | sAppStream << "0 g 0 G 1 w /" << sAlias.AsStringC() << " Do\n" |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 48 | << "Q\n"; |
| 49 | } |
| 50 | |
tsepez | 71a452f | 2016-05-13 17:51:27 -0700 | [diff] [blame] | 51 | return sAppStream.MakeString(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 54 | void CPWL_Image::SetPDFStream(CPDF_Stream* pStream) { |
| 55 | m_pPDFStream = pStream; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 58 | CPDF_Stream* CPWL_Image::GetPDFStream() { |
| 59 | return m_pPDFStream; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 62 | void CPWL_Image::GetImageSize(float& fWidth, float& fHeight) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 63 | fWidth = 0.0f; |
| 64 | fHeight = 0.0f; |
| 65 | |
| 66 | if (m_pPDFStream) { |
| 67 | if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 68 | CFX_FloatRect rect = pDict->GetRectFor("BBox"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 69 | |
| 70 | fWidth = rect.right - rect.left; |
| 71 | fHeight = rect.top - rect.bottom; |
| 72 | } |
| 73 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 76 | CFX_Matrix CPWL_Image::GetImageMatrix() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 77 | if (m_pPDFStream) { |
| 78 | if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 79 | return pDict->GetMatrixFor("Matrix"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 83 | return CFX_Matrix(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 86 | CFX_ByteString CPWL_Image::GetImageAlias() { |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 87 | if (!m_sImageAlias.IsEmpty()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 88 | return m_sImageAlias; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 89 | |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 90 | if (m_pPDFStream) { |
| 91 | if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 92 | return pDict->GetStringFor("Name"); |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 96 | return CFX_ByteString(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 99 | void CPWL_Image::SetImageAlias(const char* sImageAlias) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 100 | m_sImageAlias = sImageAlias; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 103 | void CPWL_Image::GetScale(float& fHScale, float& fVScale) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 104 | fHScale = 1.0f; |
| 105 | fVScale = 1.0f; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 108 | void CPWL_Image::GetImageOffset(float& x, float& y) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 109 | x = 0.0f; |
| 110 | y = 0.0f; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 111 | } |
| 112 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 113 | CPWL_Icon::CPWL_Icon() : m_pIconFit(nullptr) {} |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 114 | |
| 115 | CPWL_Icon::~CPWL_Icon() {} |
| 116 | |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 117 | CPDF_IconFit* CPWL_Icon::GetIconFit() { |
| 118 | return m_pIconFit; |
| 119 | } |
| 120 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 121 | int32_t CPWL_Icon::GetScaleMethod() { |
| 122 | if (m_pIconFit) |
| 123 | return m_pIconFit->GetScaleMethod(); |
| 124 | |
| 125 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 126 | } |
| 127 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 128 | bool CPWL_Icon::IsProportionalScale() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 129 | if (m_pIconFit) |
| 130 | return m_pIconFit->IsProportionalScale(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 131 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 132 | return false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 135 | void CPWL_Icon::GetIconPosition(float& fLeft, float& fBottom) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | if (m_pIconFit) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 137 | fLeft = 0.0f; |
| 138 | fBottom = 0.0f; |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 139 | CPDF_Array* pA = m_pIconFit->GetDict() |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 140 | ? m_pIconFit->GetDict()->GetArrayFor("A") |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 141 | : nullptr; |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 142 | if (pA) { |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 143 | size_t dwCount = pA->GetCount(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 144 | if (dwCount > 0) |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 145 | fLeft = pA->GetNumberAt(0); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 146 | if (dwCount > 1) |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 147 | fBottom = pA->GetNumberAt(1); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 148 | } |
| 149 | } else { |
| 150 | fLeft = 0.0f; |
| 151 | fBottom = 0.0f; |
| 152 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 155 | void CPWL_Icon::GetScale(float& fHScale, float& fVScale) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 156 | fHScale = 1.0f; |
| 157 | fVScale = 1.0f; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 158 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 159 | if (m_pPDFStream) { |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 160 | float fImageWidth, fImageHeight; |
| 161 | float fPlateWidth, fPlateHeight; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 162 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 163 | CFX_FloatRect rcPlate = GetClientRect(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 164 | fPlateWidth = rcPlate.right - rcPlate.left; |
| 165 | fPlateHeight = rcPlate.top - rcPlate.bottom; |
| 166 | |
| 167 | GetImageSize(fImageWidth, fImageHeight); |
| 168 | |
| 169 | int32_t nScaleMethod = GetScaleMethod(); |
| 170 | |
| 171 | switch (nScaleMethod) { |
| 172 | default: |
| 173 | case 0: |
Dan Sinclair | 5f2d381 | 2017-02-14 15:42:11 -0500 | [diff] [blame] | 174 | fHScale = fPlateWidth / std::max(fImageWidth, 1.0f); |
| 175 | fVScale = fPlateHeight / std::max(fImageHeight, 1.0f); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 176 | break; |
| 177 | case 1: |
| 178 | if (fPlateWidth < fImageWidth) |
Dan Sinclair | 5f2d381 | 2017-02-14 15:42:11 -0500 | [diff] [blame] | 179 | fHScale = fPlateWidth / std::max(fImageWidth, 1.0f); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 180 | if (fPlateHeight < fImageHeight) |
Dan Sinclair | 5f2d381 | 2017-02-14 15:42:11 -0500 | [diff] [blame] | 181 | fVScale = fPlateHeight / std::max(fImageHeight, 1.0f); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 182 | break; |
| 183 | case 2: |
| 184 | if (fPlateWidth > fImageWidth) |
Dan Sinclair | 5f2d381 | 2017-02-14 15:42:11 -0500 | [diff] [blame] | 185 | fHScale = fPlateWidth / std::max(fImageWidth, 1.0f); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | if (fPlateHeight > fImageHeight) |
Dan Sinclair | 5f2d381 | 2017-02-14 15:42:11 -0500 | [diff] [blame] | 187 | fVScale = fPlateHeight / std::max(fImageHeight, 1.0f); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 188 | break; |
| 189 | case 3: |
| 190 | break; |
| 191 | } |
| 192 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 193 | float fMinScale; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 194 | if (IsProportionalScale()) { |
Dan Sinclair | 5f2d381 | 2017-02-14 15:42:11 -0500 | [diff] [blame] | 195 | fMinScale = std::min(fHScale, fVScale); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | fHScale = fMinScale; |
| 197 | fVScale = fMinScale; |
| 198 | } |
| 199 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 202 | void CPWL_Icon::GetImageOffset(float& x, float& y) { |
| 203 | float fLeft, fBottom; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 204 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 205 | GetIconPosition(fLeft, fBottom); |
| 206 | x = 0.0f; |
| 207 | y = 0.0f; |
| 208 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 209 | float fImageWidth, fImageHeight; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 210 | GetImageSize(fImageWidth, fImageHeight); |
| 211 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 212 | float fHScale, fVScale; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 213 | GetScale(fHScale, fVScale); |
| 214 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 215 | float fImageFactWidth = fImageWidth * fHScale; |
| 216 | float fImageFactHeight = fImageHeight * fVScale; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 217 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 218 | float fPlateWidth, fPlateHeight; |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 219 | CFX_FloatRect rcPlate = GetClientRect(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 220 | fPlateWidth = rcPlate.right - rcPlate.left; |
| 221 | fPlateHeight = rcPlate.top - rcPlate.bottom; |
| 222 | |
| 223 | x = (fPlateWidth - fImageFactWidth) * fLeft; |
| 224 | y = (fPlateHeight - fImageFactHeight) * fBottom; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 225 | } |