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