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