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