jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 | |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 7 | #include "fpdfsdk/cpdfsdk_baannot.h" |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 8 | |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [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_document.h" |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 13 | #include "core/fpdfapi/parser/cpdf_name.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 14 | #include "core/fpdfapi/parser/cpdf_number.h" |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 15 | #include "core/fpdfapi/parser/cpdf_reference.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 16 | #include "core/fpdfapi/parser/cpdf_stream.h" |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 17 | #include "core/fpdfapi/parser/cpdf_string.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 18 | #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 19 | #include "fpdfsdk/cpdfsdk_datetime.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 20 | #include "fpdfsdk/cpdfsdk_pageview.h" |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 21 | |
| 22 | CPDFSDK_BAAnnot::CPDFSDK_BAAnnot(CPDF_Annot* pAnnot, |
| 23 | CPDFSDK_PageView* pPageView) |
| 24 | : CPDFSDK_Annot(pPageView), m_pAnnot(pAnnot) {} |
| 25 | |
| 26 | CPDFSDK_BAAnnot::~CPDFSDK_BAAnnot() {} |
| 27 | |
| 28 | CPDF_Annot* CPDFSDK_BAAnnot::GetPDFAnnot() const { |
| 29 | return m_pAnnot; |
| 30 | } |
| 31 | |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 32 | CPDF_Annot* CPDFSDK_BAAnnot::GetPDFPopupAnnot() const { |
| 33 | return m_pAnnot->GetPopupAnnot(); |
| 34 | } |
| 35 | |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 36 | CPDF_Dictionary* CPDFSDK_BAAnnot::GetAnnotDict() const { |
| 37 | return m_pAnnot->GetAnnotDict(); |
| 38 | } |
| 39 | |
| 40 | void CPDFSDK_BAAnnot::SetRect(const CFX_FloatRect& rect) { |
| 41 | ASSERT(rect.right - rect.left >= GetMinWidth()); |
| 42 | ASSERT(rect.top - rect.bottom >= GetMinHeight()); |
| 43 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 44 | m_pAnnot->GetAnnotDict()->SetRectFor("Rect", rect); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | CFX_FloatRect CPDFSDK_BAAnnot::GetRect() const { |
jaepark | a1d2111 | 2016-08-25 13:33:34 -0700 | [diff] [blame] | 48 | return m_pAnnot->GetRect(); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 49 | } |
| 50 | |
jaepark | 956553e | 2016-08-31 06:49:27 -0700 | [diff] [blame] | 51 | CPDF_Annot::Subtype CPDFSDK_BAAnnot::GetAnnotSubtype() const { |
jaepark | 9ed9137 | 2016-08-26 16:16:10 -0700 | [diff] [blame] | 52 | return m_pAnnot->GetSubtype(); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void CPDFSDK_BAAnnot::DrawAppearance(CFX_RenderDevice* pDevice, |
| 56 | const CFX_Matrix* pUser2Device, |
| 57 | CPDF_Annot::AppearanceMode mode, |
| 58 | const CPDF_RenderOptions* pOptions) { |
| 59 | m_pAnnot->DrawAppearance(m_pPageView->GetPDFPage(), pDevice, pUser2Device, |
| 60 | mode, pOptions); |
| 61 | } |
| 62 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 63 | bool CPDFSDK_BAAnnot::IsAppearanceValid() { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 64 | return !!m_pAnnot->GetAnnotDict()->GetDictFor("AP"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 65 | } |
| 66 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 67 | bool CPDFSDK_BAAnnot::IsAppearanceValid(CPDF_Annot::AppearanceMode mode) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 68 | CPDF_Dictionary* pAP = m_pAnnot->GetAnnotDict()->GetDictFor("AP"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 69 | if (!pAP) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 70 | return false; |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 71 | |
| 72 | // Choose the right sub-ap |
| 73 | const FX_CHAR* ap_entry = "N"; |
| 74 | if (mode == CPDF_Annot::Down) |
| 75 | ap_entry = "D"; |
| 76 | else if (mode == CPDF_Annot::Rollover) |
| 77 | ap_entry = "R"; |
| 78 | if (!pAP->KeyExist(ap_entry)) |
| 79 | ap_entry = "N"; |
| 80 | |
| 81 | // Get the AP stream or subdirectory |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 82 | CPDF_Object* psub = pAP->GetDirectObjectFor(ap_entry); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 83 | return !!psub; |
| 84 | } |
| 85 | |
| 86 | void CPDFSDK_BAAnnot::DrawBorder(CFX_RenderDevice* pDevice, |
| 87 | const CFX_Matrix* pUser2Device, |
| 88 | const CPDF_RenderOptions* pOptions) { |
| 89 | m_pAnnot->DrawBorder(pDevice, pUser2Device, pOptions); |
| 90 | } |
| 91 | |
| 92 | void CPDFSDK_BAAnnot::ClearCachedAP() { |
| 93 | m_pAnnot->ClearCachedAP(); |
| 94 | } |
| 95 | |
| 96 | void CPDFSDK_BAAnnot::SetContents(const CFX_WideString& sContents) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 97 | if (sContents.IsEmpty()) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 98 | m_pAnnot->GetAnnotDict()->RemoveFor("Contents"); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 99 | } else { |
| 100 | m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_String>( |
| 101 | "Contents", PDF_EncodeText(sContents), false); |
| 102 | } |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | CFX_WideString CPDFSDK_BAAnnot::GetContents() const { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 106 | return m_pAnnot->GetAnnotDict()->GetUnicodeTextFor("Contents"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void CPDFSDK_BAAnnot::SetAnnotName(const CFX_WideString& sName) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 110 | if (sName.IsEmpty()) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 111 | m_pAnnot->GetAnnotDict()->RemoveFor("NM"); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 112 | } else { |
| 113 | m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_String>( |
| 114 | "NM", PDF_EncodeText(sName), false); |
| 115 | } |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | CFX_WideString CPDFSDK_BAAnnot::GetAnnotName() const { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 119 | return m_pAnnot->GetAnnotDict()->GetUnicodeTextFor("NM"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void CPDFSDK_BAAnnot::SetModifiedDate(const FX_SYSTEMTIME& st) { |
| 123 | CPDFSDK_DateTime dt(st); |
| 124 | CFX_ByteString str = dt.ToPDFDateTimeString(); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 125 | if (str.IsEmpty()) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 126 | m_pAnnot->GetAnnotDict()->RemoveFor("M"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 127 | else |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 128 | m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_String>("M", str, false); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | FX_SYSTEMTIME CPDFSDK_BAAnnot::GetModifiedDate() const { |
| 132 | FX_SYSTEMTIME systime; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 133 | CFX_ByteString str = m_pAnnot->GetAnnotDict()->GetStringFor("M"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 134 | CPDFSDK_DateTime dt(str); |
| 135 | dt.ToSystemTime(systime); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 136 | return systime; |
| 137 | } |
| 138 | |
| 139 | void CPDFSDK_BAAnnot::SetFlags(uint32_t nFlags) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 140 | m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Number>("F", |
| 141 | static_cast<int>(nFlags)); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | uint32_t CPDFSDK_BAAnnot::GetFlags() const { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 145 | return m_pAnnot->GetAnnotDict()->GetIntegerFor("F"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void CPDFSDK_BAAnnot::SetAppState(const CFX_ByteString& str) { |
| 149 | if (str.IsEmpty()) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 150 | m_pAnnot->GetAnnotDict()->RemoveFor("AS"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 151 | else |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 152 | m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_String>("AS", str, false); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | CFX_ByteString CPDFSDK_BAAnnot::GetAppState() const { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 156 | return m_pAnnot->GetAnnotDict()->GetStringFor("AS"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void CPDFSDK_BAAnnot::SetStructParent(int key) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 160 | m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Number>("StructParent", key); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | int CPDFSDK_BAAnnot::GetStructParent() const { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 164 | return m_pAnnot->GetAnnotDict()->GetIntegerFor("StructParent"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // border |
| 168 | void CPDFSDK_BAAnnot::SetBorderWidth(int nWidth) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 169 | CPDF_Array* pBorder = m_pAnnot->GetAnnotDict()->GetArrayFor("Border"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 170 | if (pBorder) { |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 171 | pBorder->SetNewAt<CPDF_Number>(2, nWidth); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 172 | } else { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 173 | CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictFor("BS"); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 174 | if (!pBSDict) |
| 175 | pBSDict = m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Dictionary>("BS"); |
| 176 | |
| 177 | pBSDict->SetNewFor<CPDF_Number>("W", nWidth); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | int CPDFSDK_BAAnnot::GetBorderWidth() const { |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 182 | if (CPDF_Array* pBorder = m_pAnnot->GetAnnotDict()->GetArrayFor("Border")) |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 183 | return pBorder->GetIntegerAt(2); |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 184 | |
| 185 | if (CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictFor("BS")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 186 | return pBSDict->GetIntegerFor("W", 1); |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 187 | |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | void CPDFSDK_BAAnnot::SetBorderStyle(BorderStyle nStyle) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 192 | CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictFor("BS"); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 193 | if (!pBSDict) |
| 194 | pBSDict = m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Dictionary>("BS"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 195 | |
| 196 | switch (nStyle) { |
| 197 | case BorderStyle::SOLID: |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 198 | pBSDict->SetNewFor<CPDF_Name>("S", "S"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 199 | break; |
| 200 | case BorderStyle::DASH: |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 201 | pBSDict->SetNewFor<CPDF_Name>("S", "D"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 202 | break; |
| 203 | case BorderStyle::BEVELED: |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 204 | pBSDict->SetNewFor<CPDF_Name>("S", "B"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 205 | break; |
| 206 | case BorderStyle::INSET: |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 207 | pBSDict->SetNewFor<CPDF_Name>("S", "I"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 208 | break; |
| 209 | case BorderStyle::UNDERLINE: |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 210 | pBSDict->SetNewFor<CPDF_Name>("S", "U"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 211 | break; |
| 212 | default: |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | BorderStyle CPDFSDK_BAAnnot::GetBorderStyle() const { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 218 | CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictFor("BS"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 219 | if (pBSDict) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 220 | CFX_ByteString sBorderStyle = pBSDict->GetStringFor("S", "S"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 221 | if (sBorderStyle == "S") |
| 222 | return BorderStyle::SOLID; |
| 223 | if (sBorderStyle == "D") |
| 224 | return BorderStyle::DASH; |
| 225 | if (sBorderStyle == "B") |
| 226 | return BorderStyle::BEVELED; |
| 227 | if (sBorderStyle == "I") |
| 228 | return BorderStyle::INSET; |
| 229 | if (sBorderStyle == "U") |
| 230 | return BorderStyle::UNDERLINE; |
| 231 | } |
| 232 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 233 | CPDF_Array* pBorder = m_pAnnot->GetAnnotDict()->GetArrayFor("Border"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 234 | if (pBorder) { |
| 235 | if (pBorder->GetCount() >= 4) { |
| 236 | CPDF_Array* pDP = pBorder->GetArrayAt(3); |
| 237 | if (pDP && pDP->GetCount() > 0) |
| 238 | return BorderStyle::DASH; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return BorderStyle::SOLID; |
| 243 | } |
| 244 | |
| 245 | void CPDFSDK_BAAnnot::SetColor(FX_COLORREF color) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 246 | CPDF_Array* pArray = m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Array>("C"); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 247 | pArray->AddNew<CPDF_Number>(static_cast<FX_FLOAT>(FXSYS_GetRValue(color)) / |
| 248 | 255.0f); |
| 249 | pArray->AddNew<CPDF_Number>(static_cast<FX_FLOAT>(FXSYS_GetGValue(color)) / |
| 250 | 255.0f); |
| 251 | pArray->AddNew<CPDF_Number>(static_cast<FX_FLOAT>(FXSYS_GetBValue(color)) / |
| 252 | 255.0f); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void CPDFSDK_BAAnnot::RemoveColor() { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 256 | m_pAnnot->GetAnnotDict()->RemoveFor("C"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 257 | } |
| 258 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 259 | bool CPDFSDK_BAAnnot::GetColor(FX_COLORREF& color) const { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 260 | if (CPDF_Array* pEntry = m_pAnnot->GetAnnotDict()->GetArrayFor("C")) { |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 261 | size_t nCount = pEntry->GetCount(); |
| 262 | if (nCount == 1) { |
| 263 | FX_FLOAT g = pEntry->GetNumberAt(0) * 255; |
| 264 | |
| 265 | color = FXSYS_RGB((int)g, (int)g, (int)g); |
| 266 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 267 | return true; |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 268 | } else if (nCount == 3) { |
| 269 | FX_FLOAT r = pEntry->GetNumberAt(0) * 255; |
| 270 | FX_FLOAT g = pEntry->GetNumberAt(1) * 255; |
| 271 | FX_FLOAT b = pEntry->GetNumberAt(2) * 255; |
| 272 | |
| 273 | color = FXSYS_RGB((int)r, (int)g, (int)b); |
| 274 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 275 | return true; |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 276 | } else if (nCount == 4) { |
| 277 | FX_FLOAT c = pEntry->GetNumberAt(0); |
| 278 | FX_FLOAT m = pEntry->GetNumberAt(1); |
| 279 | FX_FLOAT y = pEntry->GetNumberAt(2); |
| 280 | FX_FLOAT k = pEntry->GetNumberAt(3); |
| 281 | |
| 282 | FX_FLOAT r = 1.0f - std::min(1.0f, c + k); |
| 283 | FX_FLOAT g = 1.0f - std::min(1.0f, m + k); |
| 284 | FX_FLOAT b = 1.0f - std::min(1.0f, y + k); |
| 285 | |
| 286 | color = FXSYS_RGB((int)(r * 255), (int)(g * 255), (int)(b * 255)); |
| 287 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 288 | return true; |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 292 | return false; |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void CPDFSDK_BAAnnot::WriteAppearance(const CFX_ByteString& sAPType, |
| 296 | const CFX_FloatRect& rcBBox, |
| 297 | const CFX_Matrix& matrix, |
| 298 | const CFX_ByteString& sContents, |
| 299 | const CFX_ByteString& sAPState) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 300 | CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictFor("AP"); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 301 | if (!pAPDict) |
| 302 | pAPDict = m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Dictionary>("AP"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 303 | |
| 304 | CPDF_Stream* pStream = nullptr; |
| 305 | CPDF_Dictionary* pParentDict = nullptr; |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 306 | if (sAPState.IsEmpty()) { |
| 307 | pParentDict = pAPDict; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 308 | pStream = pAPDict->GetStreamFor(sAPType); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 309 | } else { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 310 | CPDF_Dictionary* pAPTypeDict = pAPDict->GetDictFor(sAPType); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 311 | if (!pAPTypeDict) |
| 312 | pAPTypeDict = pAPDict->SetNewFor<CPDF_Dictionary>(sAPType); |
| 313 | |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 314 | pParentDict = pAPTypeDict; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 315 | pStream = pAPTypeDict->GetStreamFor(sAPState); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | if (!pStream) { |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 319 | CPDF_Document* pDoc = m_pPageView->GetPDFDocument(); |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 320 | pStream = pDoc->NewIndirect<CPDF_Stream>(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 321 | pParentDict->SetNewFor<CPDF_Reference>(sAPType, pDoc, pStream->GetObjNum()); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | CPDF_Dictionary* pStreamDict = pStream->GetDict(); |
| 325 | if (!pStreamDict) { |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 326 | pStreamDict = |
| 327 | new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool()); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 328 | pStreamDict->SetNewFor<CPDF_Name>("Type", "XObject"); |
| 329 | pStreamDict->SetNewFor<CPDF_Name>("Subtype", "Form"); |
| 330 | pStreamDict->SetNewFor<CPDF_Number>("FormType", 1); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 331 | pStream->InitStream(nullptr, 0, pStreamDict); |
| 332 | } |
| 333 | |
| 334 | if (pStreamDict) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 335 | pStreamDict->SetMatrixFor("Matrix", matrix); |
| 336 | pStreamDict->SetRectFor("BBox", rcBBox); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 337 | } |
| 338 | |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 339 | pStream->SetData((uint8_t*)sContents.c_str(), sContents.GetLength()); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 340 | } |
| 341 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 342 | bool CPDFSDK_BAAnnot::IsVisible() const { |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 343 | uint32_t nFlags = GetFlags(); |
| 344 | return !((nFlags & ANNOTFLAG_INVISIBLE) || (nFlags & ANNOTFLAG_HIDDEN) || |
| 345 | (nFlags & ANNOTFLAG_NOVIEW)); |
| 346 | } |
| 347 | |
| 348 | CPDF_Action CPDFSDK_BAAnnot::GetAction() const { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 349 | return CPDF_Action(m_pAnnot->GetAnnotDict()->GetDictFor("A")); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void CPDFSDK_BAAnnot::SetAction(const CPDF_Action& action) { |
tsepez | bb577af | 2016-09-21 19:10:19 -0700 | [diff] [blame] | 353 | CPDF_Dictionary* pDict = action.GetDict(); |
| 354 | if (pDict != m_pAnnot->GetAnnotDict()->GetDictFor("A")) { |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 355 | CPDF_Document* pDoc = m_pPageView->GetPDFDocument(); |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 356 | if (pDict->IsInline()) |
| 357 | pDict = pDoc->AddIndirectObject(pDict->Clone())->AsDictionary(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 358 | m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Reference>("A", pDoc, |
| 359 | pDict->GetObjNum()); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
| 363 | void CPDFSDK_BAAnnot::RemoveAction() { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 364 | m_pAnnot->GetAnnotDict()->RemoveFor("A"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | CPDF_AAction CPDFSDK_BAAnnot::GetAAction() const { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 368 | return CPDF_AAction(m_pAnnot->GetAnnotDict()->GetDictFor("AA")); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void CPDFSDK_BAAnnot::SetAAction(const CPDF_AAction& aa) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 372 | if (aa.GetDict() != m_pAnnot->GetAnnotDict()->GetDictFor("AA")) |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame^] | 373 | m_pAnnot->GetAnnotDict()->SetFor("AA", pdfium::WrapUnique(aa.GetDict())); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | void CPDFSDK_BAAnnot::RemoveAAction() { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 377 | m_pAnnot->GetAnnotDict()->RemoveFor("AA"); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | CPDF_Action CPDFSDK_BAAnnot::GetAAction(CPDF_AAction::AActionType eAAT) { |
| 381 | CPDF_AAction AAction = GetAAction(); |
jaepark | 2736276 | 2016-08-11 13:10:39 -0700 | [diff] [blame] | 382 | if (AAction.ActionExist(eAAT)) |
| 383 | return AAction.GetAction(eAAT); |
| 384 | |
| 385 | if (eAAT == CPDF_AAction::ButtonUp) |
| 386 | return GetAction(); |
| 387 | |
| 388 | return CPDF_Action(); |
| 389 | } |
| 390 | |
| 391 | void CPDFSDK_BAAnnot::Annot_OnDraw(CFX_RenderDevice* pDevice, |
| 392 | CFX_Matrix* pUser2Device, |
| 393 | CPDF_RenderOptions* pOptions) { |
| 394 | m_pAnnot->GetAPForm(m_pPageView->GetPDFPage(), CPDF_Annot::Normal); |
| 395 | m_pAnnot->DrawAppearance(m_pPageView->GetPDFPage(), pDevice, pUser2Device, |
| 396 | CPDF_Annot::Normal, nullptr); |
| 397 | } |
jaepark | 35512aa | 2016-08-29 17:15:08 -0700 | [diff] [blame] | 398 | |
| 399 | void CPDFSDK_BAAnnot::SetOpenState(bool bOpenState) { |
| 400 | if (CPDF_Annot* pAnnot = m_pAnnot->GetPopupAnnot()) |
| 401 | pAnnot->SetOpenState(bOpenState); |
| 402 | } |