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