OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 <algorithm> | |
8 | |
9 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | |
10 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" | |
11 #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 "core/fxcrt/include/fx_ext.h" | |
15 #include "core/fxge/include/cfx_renderdevice.h" | |
16 #include "fpdfsdk/include/fsdk_baseannot.h" | |
17 #include "fpdfsdk/include/fsdk_define.h" | |
18 #include "fpdfsdk/include/fsdk_mgr.h" | |
19 | |
20 #ifdef PDF_ENABLE_XFA | |
21 #include "fpdfsdk/fpdfxfa/include/fpdfxfa_doc.h" | |
22 #endif // PDF_ENABLE_XFA | |
23 | |
24 namespace { | |
25 | |
26 const float kMinWidth = 1.0f; | |
27 const float kMinHeight = 1.0f; | |
28 | |
29 int gAfxGetTimeZoneInSeconds(int8_t tzhour, uint8_t tzminute) { | |
30 return (int)tzhour * 3600 + (int)tzminute * (tzhour >= 0 ? 60 : -60); | |
31 } | |
32 | |
33 bool gAfxIsLeapYear(int16_t year) { | |
34 return ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))); | |
35 } | |
36 | |
37 uint16_t gAfxGetYearDays(int16_t year) { | |
38 return (gAfxIsLeapYear(year) ? 366 : 365); | |
39 } | |
40 | |
41 uint8_t gAfxGetMonthDays(int16_t year, uint8_t month) { | |
42 uint8_t mDays; | |
43 switch (month) { | |
44 case 1: | |
45 case 3: | |
46 case 5: | |
47 case 7: | |
48 case 8: | |
49 case 10: | |
50 case 12: | |
51 mDays = 31; | |
52 break; | |
53 | |
54 case 4: | |
55 case 6: | |
56 case 9: | |
57 case 11: | |
58 mDays = 30; | |
59 break; | |
60 | |
61 case 2: | |
62 if (gAfxIsLeapYear(year)) | |
63 mDays = 29; | |
64 else | |
65 mDays = 28; | |
66 break; | |
67 | |
68 default: | |
69 mDays = 0; | |
70 break; | |
71 } | |
72 | |
73 return mDays; | |
74 } | |
75 | |
76 } // namespace | |
77 | |
78 CPDFSDK_DateTime::CPDFSDK_DateTime() { | |
79 ResetDateTime(); | |
80 } | |
81 | |
82 CPDFSDK_DateTime::CPDFSDK_DateTime(const CFX_ByteString& dtStr) { | |
83 ResetDateTime(); | |
84 | |
85 FromPDFDateTimeString(dtStr); | |
86 } | |
87 | |
88 CPDFSDK_DateTime::CPDFSDK_DateTime(const CPDFSDK_DateTime& datetime) { | |
89 operator=(datetime); | |
90 } | |
91 | |
92 CPDFSDK_DateTime::CPDFSDK_DateTime(const FX_SYSTEMTIME& st) { | |
93 operator=(st); | |
94 } | |
95 | |
96 void CPDFSDK_DateTime::ResetDateTime() { | |
97 tzset(); | |
98 | |
99 time_t curTime; | |
100 time(&curTime); | |
101 struct tm* newtime = localtime(&curTime); | |
102 | |
103 dt.year = newtime->tm_year + 1900; | |
104 dt.month = newtime->tm_mon + 1; | |
105 dt.day = newtime->tm_mday; | |
106 dt.hour = newtime->tm_hour; | |
107 dt.minute = newtime->tm_min; | |
108 dt.second = newtime->tm_sec; | |
109 } | |
110 | |
111 CPDFSDK_DateTime& CPDFSDK_DateTime::operator=( | |
112 const CPDFSDK_DateTime& datetime) { | |
113 FXSYS_memcpy(&dt, &datetime.dt, sizeof(FX_DATETIME)); | |
114 return *this; | |
115 } | |
116 | |
117 CPDFSDK_DateTime& CPDFSDK_DateTime::operator=(const FX_SYSTEMTIME& st) { | |
118 tzset(); | |
119 | |
120 dt.year = static_cast<int16_t>(st.wYear); | |
121 dt.month = static_cast<uint8_t>(st.wMonth); | |
122 dt.day = static_cast<uint8_t>(st.wDay); | |
123 dt.hour = static_cast<uint8_t>(st.wHour); | |
124 dt.minute = static_cast<uint8_t>(st.wMinute); | |
125 dt.second = static_cast<uint8_t>(st.wSecond); | |
126 return *this; | |
127 } | |
128 | |
129 bool CPDFSDK_DateTime::operator==(const CPDFSDK_DateTime& datetime) const { | |
130 return (FXSYS_memcmp(&dt, &datetime.dt, sizeof(FX_DATETIME)) == 0); | |
131 } | |
132 | |
133 bool CPDFSDK_DateTime::operator!=(const CPDFSDK_DateTime& datetime) const { | |
134 return !(*this == datetime); | |
135 } | |
136 | |
137 time_t CPDFSDK_DateTime::ToTime_t() const { | |
138 struct tm newtime; | |
139 | |
140 newtime.tm_year = dt.year - 1900; | |
141 newtime.tm_mon = dt.month - 1; | |
142 newtime.tm_mday = dt.day; | |
143 newtime.tm_hour = dt.hour; | |
144 newtime.tm_min = dt.minute; | |
145 newtime.tm_sec = dt.second; | |
146 | |
147 return mktime(&newtime); | |
148 } | |
149 | |
150 CPDFSDK_DateTime& CPDFSDK_DateTime::FromPDFDateTimeString( | |
151 const CFX_ByteString& dtStr) { | |
152 int strLength = dtStr.GetLength(); | |
153 if (strLength <= 0) | |
154 return *this; | |
155 | |
156 int i = 0; | |
157 while (i < strLength && !std::isdigit(dtStr[i])) | |
158 ++i; | |
159 | |
160 if (i >= strLength) | |
161 return *this; | |
162 | |
163 int j = 0; | |
164 int k = 0; | |
165 FX_CHAR ch; | |
166 while (i < strLength && j < 4) { | |
167 ch = dtStr[i]; | |
168 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
169 j++; | |
170 if (!std::isdigit(ch)) | |
171 break; | |
172 i++; | |
173 } | |
174 dt.year = static_cast<int16_t>(k); | |
175 if (i >= strLength || j < 4) | |
176 return *this; | |
177 | |
178 j = 0; | |
179 k = 0; | |
180 while (i < strLength && j < 2) { | |
181 ch = dtStr[i]; | |
182 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
183 j++; | |
184 if (!std::isdigit(ch)) | |
185 break; | |
186 i++; | |
187 } | |
188 dt.month = static_cast<uint8_t>(k); | |
189 if (i >= strLength || j < 2) | |
190 return *this; | |
191 | |
192 j = 0; | |
193 k = 0; | |
194 while (i < strLength && j < 2) { | |
195 ch = dtStr[i]; | |
196 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
197 j++; | |
198 if (!std::isdigit(ch)) | |
199 break; | |
200 i++; | |
201 } | |
202 dt.day = static_cast<uint8_t>(k); | |
203 if (i >= strLength || j < 2) | |
204 return *this; | |
205 | |
206 j = 0; | |
207 k = 0; | |
208 while (i < strLength && j < 2) { | |
209 ch = dtStr[i]; | |
210 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
211 j++; | |
212 if (!std::isdigit(ch)) | |
213 break; | |
214 i++; | |
215 } | |
216 dt.hour = static_cast<uint8_t>(k); | |
217 if (i >= strLength || j < 2) | |
218 return *this; | |
219 | |
220 j = 0; | |
221 k = 0; | |
222 while (i < strLength && j < 2) { | |
223 ch = dtStr[i]; | |
224 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
225 j++; | |
226 if (!std::isdigit(ch)) | |
227 break; | |
228 i++; | |
229 } | |
230 dt.minute = static_cast<uint8_t>(k); | |
231 if (i >= strLength || j < 2) | |
232 return *this; | |
233 | |
234 j = 0; | |
235 k = 0; | |
236 while (i < strLength && j < 2) { | |
237 ch = dtStr[i]; | |
238 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
239 j++; | |
240 if (!std::isdigit(ch)) | |
241 break; | |
242 i++; | |
243 } | |
244 dt.second = static_cast<uint8_t>(k); | |
245 if (i >= strLength || j < 2) | |
246 return *this; | |
247 | |
248 ch = dtStr[i++]; | |
249 if (ch != '-' && ch != '+') | |
250 return *this; | |
251 if (ch == '-') | |
252 dt.tzHour = -1; | |
253 else | |
254 dt.tzHour = 1; | |
255 j = 0; | |
256 k = 0; | |
257 while (i < strLength && j < 2) { | |
258 ch = dtStr[i]; | |
259 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
260 j++; | |
261 if (!std::isdigit(ch)) | |
262 break; | |
263 i++; | |
264 } | |
265 dt.tzHour *= static_cast<int8_t>(k); | |
266 if (i >= strLength || j < 2) | |
267 return *this; | |
268 | |
269 if (dtStr[i++] != '\'') | |
270 return *this; | |
271 j = 0; | |
272 k = 0; | |
273 while (i < strLength && j < 2) { | |
274 ch = dtStr[i]; | |
275 k = k * 10 + FXSYS_toDecimalDigit(ch); | |
276 j++; | |
277 if (!std::isdigit(ch)) | |
278 break; | |
279 i++; | |
280 } | |
281 dt.tzMinute = static_cast<uint8_t>(k); | |
282 return *this; | |
283 } | |
284 | |
285 CFX_ByteString CPDFSDK_DateTime::ToCommonDateTimeString() { | |
286 CFX_ByteString str1; | |
287 str1.Format("%04d-%02u-%02u %02u:%02u:%02u ", dt.year, dt.month, dt.day, | |
288 dt.hour, dt.minute, dt.second); | |
289 if (dt.tzHour < 0) | |
290 str1 += "-"; | |
291 else | |
292 str1 += "+"; | |
293 CFX_ByteString str2; | |
294 str2.Format("%02d:%02u", std::abs(static_cast<int>(dt.tzHour)), dt.tzMinute); | |
295 return str1 + str2; | |
296 } | |
297 | |
298 CFX_ByteString CPDFSDK_DateTime::ToPDFDateTimeString() { | |
299 CFX_ByteString dtStr; | |
300 char tempStr[32]; | |
301 memset(tempStr, 0, sizeof(tempStr)); | |
302 FXSYS_snprintf(tempStr, sizeof(tempStr) - 1, "D:%04d%02u%02u%02u%02u%02u", | |
303 dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second); | |
304 dtStr = CFX_ByteString(tempStr); | |
305 if (dt.tzHour < 0) | |
306 dtStr += CFX_ByteString("-"); | |
307 else | |
308 dtStr += CFX_ByteString("+"); | |
309 memset(tempStr, 0, sizeof(tempStr)); | |
310 FXSYS_snprintf(tempStr, sizeof(tempStr) - 1, "%02d'%02u'", | |
311 std::abs(static_cast<int>(dt.tzHour)), dt.tzMinute); | |
312 dtStr += CFX_ByteString(tempStr); | |
313 return dtStr; | |
314 } | |
315 | |
316 void CPDFSDK_DateTime::ToSystemTime(FX_SYSTEMTIME& st) { | |
317 time_t t = this->ToTime_t(); | |
318 struct tm* pTime = localtime(&t); | |
319 if (pTime) { | |
320 st.wYear = static_cast<uint16_t>(pTime->tm_year) + 1900; | |
321 st.wMonth = static_cast<uint16_t>(pTime->tm_mon) + 1; | |
322 st.wDay = static_cast<uint16_t>(pTime->tm_mday); | |
323 st.wDayOfWeek = static_cast<uint16_t>(pTime->tm_wday); | |
324 st.wHour = static_cast<uint16_t>(pTime->tm_hour); | |
325 st.wMinute = static_cast<uint16_t>(pTime->tm_min); | |
326 st.wSecond = static_cast<uint16_t>(pTime->tm_sec); | |
327 st.wMilliseconds = 0; | |
328 } | |
329 } | |
330 | |
331 CPDFSDK_DateTime CPDFSDK_DateTime::ToGMT() const { | |
332 CPDFSDK_DateTime new_dt = *this; | |
333 new_dt.AddSeconds( | |
334 -gAfxGetTimeZoneInSeconds(new_dt.dt.tzHour, new_dt.dt.tzMinute)); | |
335 new_dt.dt.tzHour = 0; | |
336 new_dt.dt.tzMinute = 0; | |
337 return new_dt; | |
338 } | |
339 | |
340 CPDFSDK_DateTime& CPDFSDK_DateTime::AddDays(short days) { | |
341 if (days == 0) | |
342 return *this; | |
343 | |
344 int16_t y = dt.year; | |
345 uint8_t m = dt.month; | |
346 uint8_t d = dt.day; | |
347 | |
348 int ldays = days; | |
349 if (ldays > 0) { | |
350 int16_t yy = y; | |
351 if ((static_cast<uint16_t>(m) * 100 + d) > 300) | |
352 yy++; | |
353 int ydays = gAfxGetYearDays(yy); | |
354 int mdays; | |
355 while (ldays >= ydays) { | |
356 y++; | |
357 ldays -= ydays; | |
358 yy++; | |
359 mdays = gAfxGetMonthDays(y, m); | |
360 if (d > mdays) { | |
361 m++; | |
362 d -= mdays; | |
363 } | |
364 ydays = gAfxGetYearDays(yy); | |
365 } | |
366 mdays = gAfxGetMonthDays(y, m) - d + 1; | |
367 while (ldays >= mdays) { | |
368 ldays -= mdays; | |
369 m++; | |
370 d = 1; | |
371 mdays = gAfxGetMonthDays(y, m); | |
372 } | |
373 d += ldays; | |
374 } else { | |
375 ldays *= -1; | |
376 int16_t yy = y; | |
377 if ((static_cast<uint16_t>(m) * 100 + d) < 300) | |
378 yy--; | |
379 int ydays = gAfxGetYearDays(yy); | |
380 while (ldays >= ydays) { | |
381 y--; | |
382 ldays -= ydays; | |
383 yy--; | |
384 int mdays = gAfxGetMonthDays(y, m); | |
385 if (d > mdays) { | |
386 m++; | |
387 d -= mdays; | |
388 } | |
389 ydays = gAfxGetYearDays(yy); | |
390 } | |
391 while (ldays >= d) { | |
392 ldays -= d; | |
393 m--; | |
394 d = gAfxGetMonthDays(y, m); | |
395 } | |
396 d -= ldays; | |
397 } | |
398 | |
399 dt.year = y; | |
400 dt.month = m; | |
401 dt.day = d; | |
402 | |
403 return *this; | |
404 } | |
405 | |
406 CPDFSDK_DateTime& CPDFSDK_DateTime::AddSeconds(int seconds) { | |
407 if (seconds == 0) | |
408 return *this; | |
409 | |
410 int n; | |
411 int days; | |
412 | |
413 n = dt.hour * 3600 + dt.minute * 60 + dt.second + seconds; | |
414 if (n < 0) { | |
415 days = (n - 86399) / 86400; | |
416 n -= days * 86400; | |
417 } else { | |
418 days = n / 86400; | |
419 n %= 86400; | |
420 } | |
421 dt.hour = static_cast<uint8_t>(n / 3600); | |
422 dt.hour %= 24; | |
423 n %= 3600; | |
424 dt.minute = static_cast<uint8_t>(n / 60); | |
425 dt.second = static_cast<uint8_t>(n % 60); | |
426 if (days != 0) | |
427 AddDays(days); | |
428 | |
429 return *this; | |
430 } | |
431 | |
432 CPDFSDK_Annot::CPDFSDK_Annot(CPDFSDK_PageView* pPageView) | |
433 : m_pPageView(pPageView), m_bSelected(FALSE), m_nTabOrder(-1) {} | |
434 | |
435 CPDFSDK_Annot::~CPDFSDK_Annot() {} | |
436 | |
437 #ifdef PDF_ENABLE_XFA | |
438 | |
439 FX_BOOL CPDFSDK_Annot::IsXFAField() { | |
440 return FALSE; | |
441 } | |
442 | |
443 CXFA_FFWidget* CPDFSDK_Annot::GetXFAWidget() const { | |
444 return nullptr; | |
445 } | |
446 | |
447 #endif // PDF_ENABLE_XFA | |
448 | |
449 FX_FLOAT CPDFSDK_Annot::GetMinWidth() const { | |
450 return kMinWidth; | |
451 } | |
452 | |
453 FX_FLOAT CPDFSDK_Annot::GetMinHeight() const { | |
454 return kMinHeight; | |
455 } | |
456 | |
457 int CPDFSDK_Annot::GetLayoutOrder() const { | |
458 return 5; | |
459 } | |
460 | |
461 CPDF_Annot* CPDFSDK_Annot::GetPDFAnnot() const { | |
462 return nullptr; | |
463 } | |
464 | |
465 CFX_ByteString CPDFSDK_Annot::GetType() const { | |
466 return ""; | |
467 } | |
468 | |
469 CFX_ByteString CPDFSDK_Annot::GetSubType() const { | |
470 return ""; | |
471 } | |
472 | |
473 void CPDFSDK_Annot::SetRect(const CFX_FloatRect& rect) {} | |
474 | |
475 CFX_FloatRect CPDFSDK_Annot::GetRect() const { | |
476 return CFX_FloatRect(); | |
477 } | |
478 | |
479 void CPDFSDK_Annot::Annot_OnDraw(CFX_RenderDevice* pDevice, | |
480 CFX_Matrix* pUser2Device, | |
481 CPDF_RenderOptions* pOptions) {} | |
482 | |
483 CPDFSDK_BAAnnot::CPDFSDK_BAAnnot(CPDF_Annot* pAnnot, | |
484 CPDFSDK_PageView* pPageView) | |
485 : CPDFSDK_Annot(pPageView), m_pAnnot(pAnnot) {} | |
486 | |
487 CPDFSDK_BAAnnot::~CPDFSDK_BAAnnot() {} | |
488 | |
489 CPDF_Annot* CPDFSDK_BAAnnot::GetPDFAnnot() const { | |
490 return m_pAnnot; | |
491 } | |
492 | |
493 FX_BOOL CPDFSDK_Annot::IsSelected() { | |
494 return m_bSelected; | |
495 } | |
496 | |
497 void CPDFSDK_Annot::SetSelected(FX_BOOL bSelected) { | |
498 m_bSelected = bSelected; | |
499 } | |
500 | |
501 int CPDFSDK_Annot::GetTabOrder() { | |
502 return m_nTabOrder; | |
503 } | |
504 | |
505 void CPDFSDK_Annot::SetTabOrder(int iTabOrder) { | |
506 m_nTabOrder = iTabOrder; | |
507 } | |
508 | |
509 CPDF_Dictionary* CPDFSDK_BAAnnot::GetAnnotDict() const { | |
510 return m_pAnnot->GetAnnotDict(); | |
511 } | |
512 | |
513 void CPDFSDK_BAAnnot::SetRect(const CFX_FloatRect& rect) { | |
514 ASSERT(rect.right - rect.left >= GetMinWidth()); | |
515 ASSERT(rect.top - rect.bottom >= GetMinHeight()); | |
516 | |
517 m_pAnnot->GetAnnotDict()->SetAtRect("Rect", rect); | |
518 } | |
519 | |
520 CFX_FloatRect CPDFSDK_BAAnnot::GetRect() const { | |
521 CFX_FloatRect rect; | |
522 m_pAnnot->GetRect(rect); | |
523 return rect; | |
524 } | |
525 | |
526 CFX_ByteString CPDFSDK_BAAnnot::GetType() const { | |
527 return m_pAnnot->GetSubType(); | |
528 } | |
529 | |
530 CFX_ByteString CPDFSDK_BAAnnot::GetSubType() const { | |
531 return ""; | |
532 } | |
533 | |
534 void CPDFSDK_BAAnnot::DrawAppearance(CFX_RenderDevice* pDevice, | |
535 const CFX_Matrix* pUser2Device, | |
536 CPDF_Annot::AppearanceMode mode, | |
537 const CPDF_RenderOptions* pOptions) { | |
538 m_pAnnot->DrawAppearance(m_pPageView->GetPDFPage(), pDevice, pUser2Device, | |
539 mode, pOptions); | |
540 } | |
541 | |
542 FX_BOOL CPDFSDK_BAAnnot::IsAppearanceValid() { | |
543 return !!m_pAnnot->GetAnnotDict()->GetDictBy("AP"); | |
544 } | |
545 | |
546 FX_BOOL CPDFSDK_BAAnnot::IsAppearanceValid(CPDF_Annot::AppearanceMode mode) { | |
547 CPDF_Dictionary* pAP = m_pAnnot->GetAnnotDict()->GetDictBy("AP"); | |
548 if (!pAP) | |
549 return FALSE; | |
550 | |
551 // Choose the right sub-ap | |
552 const FX_CHAR* ap_entry = "N"; | |
553 if (mode == CPDF_Annot::Down) | |
554 ap_entry = "D"; | |
555 else if (mode == CPDF_Annot::Rollover) | |
556 ap_entry = "R"; | |
557 if (!pAP->KeyExist(ap_entry)) | |
558 ap_entry = "N"; | |
559 | |
560 // Get the AP stream or subdirectory | |
561 CPDF_Object* psub = pAP->GetDirectObjectBy(ap_entry); | |
562 return !!psub; | |
563 } | |
564 | |
565 void CPDFSDK_BAAnnot::DrawBorder(CFX_RenderDevice* pDevice, | |
566 const CFX_Matrix* pUser2Device, | |
567 const CPDF_RenderOptions* pOptions) { | |
568 m_pAnnot->DrawBorder(pDevice, pUser2Device, pOptions); | |
569 } | |
570 | |
571 void CPDFSDK_BAAnnot::ClearCachedAP() { | |
572 m_pAnnot->ClearCachedAP(); | |
573 } | |
574 | |
575 void CPDFSDK_BAAnnot::SetContents(const CFX_WideString& sContents) { | |
576 if (sContents.IsEmpty()) | |
577 m_pAnnot->GetAnnotDict()->RemoveAt("Contents"); | |
578 else | |
579 m_pAnnot->GetAnnotDict()->SetAtString("Contents", | |
580 PDF_EncodeText(sContents)); | |
581 } | |
582 | |
583 CFX_WideString CPDFSDK_BAAnnot::GetContents() const { | |
584 return m_pAnnot->GetAnnotDict()->GetUnicodeTextBy("Contents"); | |
585 } | |
586 | |
587 void CPDFSDK_BAAnnot::SetAnnotName(const CFX_WideString& sName) { | |
588 if (sName.IsEmpty()) | |
589 m_pAnnot->GetAnnotDict()->RemoveAt("NM"); | |
590 else | |
591 m_pAnnot->GetAnnotDict()->SetAtString("NM", PDF_EncodeText(sName)); | |
592 } | |
593 | |
594 CFX_WideString CPDFSDK_BAAnnot::GetAnnotName() const { | |
595 return m_pAnnot->GetAnnotDict()->GetUnicodeTextBy("NM"); | |
596 } | |
597 | |
598 void CPDFSDK_BAAnnot::SetModifiedDate(const FX_SYSTEMTIME& st) { | |
599 CPDFSDK_DateTime dt(st); | |
600 CFX_ByteString str = dt.ToPDFDateTimeString(); | |
601 | |
602 if (str.IsEmpty()) | |
603 m_pAnnot->GetAnnotDict()->RemoveAt("M"); | |
604 else | |
605 m_pAnnot->GetAnnotDict()->SetAtString("M", str); | |
606 } | |
607 | |
608 FX_SYSTEMTIME CPDFSDK_BAAnnot::GetModifiedDate() const { | |
609 FX_SYSTEMTIME systime; | |
610 CFX_ByteString str = m_pAnnot->GetAnnotDict()->GetStringBy("M"); | |
611 | |
612 CPDFSDK_DateTime dt(str); | |
613 dt.ToSystemTime(systime); | |
614 | |
615 return systime; | |
616 } | |
617 | |
618 void CPDFSDK_BAAnnot::SetFlags(uint32_t nFlags) { | |
619 m_pAnnot->GetAnnotDict()->SetAtInteger("F", nFlags); | |
620 } | |
621 | |
622 uint32_t CPDFSDK_BAAnnot::GetFlags() const { | |
623 return m_pAnnot->GetAnnotDict()->GetIntegerBy("F"); | |
624 } | |
625 | |
626 void CPDFSDK_BAAnnot::SetAppState(const CFX_ByteString& str) { | |
627 if (str.IsEmpty()) | |
628 m_pAnnot->GetAnnotDict()->RemoveAt("AS"); | |
629 else | |
630 m_pAnnot->GetAnnotDict()->SetAtString("AS", str); | |
631 } | |
632 | |
633 CFX_ByteString CPDFSDK_BAAnnot::GetAppState() const { | |
634 return m_pAnnot->GetAnnotDict()->GetStringBy("AS"); | |
635 } | |
636 | |
637 void CPDFSDK_BAAnnot::SetStructParent(int key) { | |
638 m_pAnnot->GetAnnotDict()->SetAtInteger("StructParent", key); | |
639 } | |
640 | |
641 int CPDFSDK_BAAnnot::GetStructParent() const { | |
642 return m_pAnnot->GetAnnotDict()->GetIntegerBy("StructParent"); | |
643 } | |
644 | |
645 // border | |
646 void CPDFSDK_BAAnnot::SetBorderWidth(int nWidth) { | |
647 CPDF_Array* pBorder = m_pAnnot->GetAnnotDict()->GetArrayBy("Border"); | |
648 | |
649 if (pBorder) { | |
650 pBorder->SetAt(2, new CPDF_Number(nWidth)); | |
651 } else { | |
652 CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictBy("BS"); | |
653 | |
654 if (!pBSDict) { | |
655 pBSDict = new CPDF_Dictionary; | |
656 m_pAnnot->GetAnnotDict()->SetAt("BS", pBSDict); | |
657 } | |
658 | |
659 pBSDict->SetAtInteger("W", nWidth); | |
660 } | |
661 } | |
662 | |
663 int CPDFSDK_BAAnnot::GetBorderWidth() const { | |
664 if (CPDF_Array* pBorder = m_pAnnot->GetAnnotDict()->GetArrayBy("Border")) { | |
665 return pBorder->GetIntegerAt(2); | |
666 } | |
667 if (CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictBy("BS")) { | |
668 return pBSDict->GetIntegerBy("W", 1); | |
669 } | |
670 return 1; | |
671 } | |
672 | |
673 void CPDFSDK_BAAnnot::SetBorderStyle(BorderStyle nStyle) { | |
674 CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictBy("BS"); | |
675 if (!pBSDict) { | |
676 pBSDict = new CPDF_Dictionary; | |
677 m_pAnnot->GetAnnotDict()->SetAt("BS", pBSDict); | |
678 } | |
679 | |
680 switch (nStyle) { | |
681 case BorderStyle::SOLID: | |
682 pBSDict->SetAtName("S", "S"); | |
683 break; | |
684 case BorderStyle::DASH: | |
685 pBSDict->SetAtName("S", "D"); | |
686 break; | |
687 case BorderStyle::BEVELED: | |
688 pBSDict->SetAtName("S", "B"); | |
689 break; | |
690 case BorderStyle::INSET: | |
691 pBSDict->SetAtName("S", "I"); | |
692 break; | |
693 case BorderStyle::UNDERLINE: | |
694 pBSDict->SetAtName("S", "U"); | |
695 break; | |
696 default: | |
697 break; | |
698 } | |
699 } | |
700 | |
701 BorderStyle CPDFSDK_BAAnnot::GetBorderStyle() const { | |
702 CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictBy("BS"); | |
703 if (pBSDict) { | |
704 CFX_ByteString sBorderStyle = pBSDict->GetStringBy("S", "S"); | |
705 if (sBorderStyle == "S") | |
706 return BorderStyle::SOLID; | |
707 if (sBorderStyle == "D") | |
708 return BorderStyle::DASH; | |
709 if (sBorderStyle == "B") | |
710 return BorderStyle::BEVELED; | |
711 if (sBorderStyle == "I") | |
712 return BorderStyle::INSET; | |
713 if (sBorderStyle == "U") | |
714 return BorderStyle::UNDERLINE; | |
715 } | |
716 | |
717 CPDF_Array* pBorder = m_pAnnot->GetAnnotDict()->GetArrayBy("Border"); | |
718 if (pBorder) { | |
719 if (pBorder->GetCount() >= 4) { | |
720 CPDF_Array* pDP = pBorder->GetArrayAt(3); | |
721 if (pDP && pDP->GetCount() > 0) | |
722 return BorderStyle::DASH; | |
723 } | |
724 } | |
725 | |
726 return BorderStyle::SOLID; | |
727 } | |
728 | |
729 void CPDFSDK_BAAnnot::SetColor(FX_COLORREF color) { | |
730 CPDF_Array* pArray = new CPDF_Array; | |
731 pArray->AddNumber((FX_FLOAT)FXSYS_GetRValue(color) / 255.0f); | |
732 pArray->AddNumber((FX_FLOAT)FXSYS_GetGValue(color) / 255.0f); | |
733 pArray->AddNumber((FX_FLOAT)FXSYS_GetBValue(color) / 255.0f); | |
734 m_pAnnot->GetAnnotDict()->SetAt("C", pArray); | |
735 } | |
736 | |
737 void CPDFSDK_BAAnnot::RemoveColor() { | |
738 m_pAnnot->GetAnnotDict()->RemoveAt("C"); | |
739 } | |
740 | |
741 FX_BOOL CPDFSDK_BAAnnot::GetColor(FX_COLORREF& color) const { | |
742 if (CPDF_Array* pEntry = m_pAnnot->GetAnnotDict()->GetArrayBy("C")) { | |
743 size_t nCount = pEntry->GetCount(); | |
744 if (nCount == 1) { | |
745 FX_FLOAT g = pEntry->GetNumberAt(0) * 255; | |
746 | |
747 color = FXSYS_RGB((int)g, (int)g, (int)g); | |
748 | |
749 return TRUE; | |
750 } else if (nCount == 3) { | |
751 FX_FLOAT r = pEntry->GetNumberAt(0) * 255; | |
752 FX_FLOAT g = pEntry->GetNumberAt(1) * 255; | |
753 FX_FLOAT b = pEntry->GetNumberAt(2) * 255; | |
754 | |
755 color = FXSYS_RGB((int)r, (int)g, (int)b); | |
756 | |
757 return TRUE; | |
758 } else if (nCount == 4) { | |
759 FX_FLOAT c = pEntry->GetNumberAt(0); | |
760 FX_FLOAT m = pEntry->GetNumberAt(1); | |
761 FX_FLOAT y = pEntry->GetNumberAt(2); | |
762 FX_FLOAT k = pEntry->GetNumberAt(3); | |
763 | |
764 FX_FLOAT r = 1.0f - std::min(1.0f, c + k); | |
765 FX_FLOAT g = 1.0f - std::min(1.0f, m + k); | |
766 FX_FLOAT b = 1.0f - std::min(1.0f, y + k); | |
767 | |
768 color = FXSYS_RGB((int)(r * 255), (int)(g * 255), (int)(b * 255)); | |
769 | |
770 return TRUE; | |
771 } | |
772 } | |
773 | |
774 return FALSE; | |
775 } | |
776 | |
777 void CPDFSDK_BAAnnot::WriteAppearance(const CFX_ByteString& sAPType, | |
778 const CFX_FloatRect& rcBBox, | |
779 const CFX_Matrix& matrix, | |
780 const CFX_ByteString& sContents, | |
781 const CFX_ByteString& sAPState) { | |
782 CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictBy("AP"); | |
783 | |
784 if (!pAPDict) { | |
785 pAPDict = new CPDF_Dictionary; | |
786 m_pAnnot->GetAnnotDict()->SetAt("AP", pAPDict); | |
787 } | |
788 | |
789 CPDF_Stream* pStream = nullptr; | |
790 CPDF_Dictionary* pParentDict = nullptr; | |
791 | |
792 if (sAPState.IsEmpty()) { | |
793 pParentDict = pAPDict; | |
794 pStream = pAPDict->GetStreamBy(sAPType); | |
795 } else { | |
796 CPDF_Dictionary* pAPTypeDict = pAPDict->GetDictBy(sAPType); | |
797 if (!pAPTypeDict) { | |
798 pAPTypeDict = new CPDF_Dictionary; | |
799 pAPDict->SetAt(sAPType, pAPTypeDict); | |
800 } | |
801 pParentDict = pAPTypeDict; | |
802 pStream = pAPTypeDict->GetStreamBy(sAPState); | |
803 } | |
804 | |
805 if (!pStream) { | |
806 pStream = new CPDF_Stream(nullptr, 0, nullptr); | |
807 CPDF_Document* pDoc = m_pPageView->GetPDFDocument(); | |
808 int32_t objnum = pDoc->AddIndirectObject(pStream); | |
809 pParentDict->SetAtReference(sAPType, pDoc, objnum); | |
810 } | |
811 | |
812 CPDF_Dictionary* pStreamDict = pStream->GetDict(); | |
813 if (!pStreamDict) { | |
814 pStreamDict = new CPDF_Dictionary; | |
815 pStreamDict->SetAtName("Type", "XObject"); | |
816 pStreamDict->SetAtName("Subtype", "Form"); | |
817 pStreamDict->SetAtInteger("FormType", 1); | |
818 pStream->InitStream(nullptr, 0, pStreamDict); | |
819 } | |
820 | |
821 if (pStreamDict) { | |
822 pStreamDict->SetAtMatrix("Matrix", matrix); | |
823 pStreamDict->SetAtRect("BBox", rcBBox); | |
824 } | |
825 | |
826 pStream->SetData((uint8_t*)sContents.c_str(), sContents.GetLength(), FALSE, | |
827 FALSE); | |
828 } | |
829 | |
830 FX_BOOL CPDFSDK_BAAnnot::IsVisible() const { | |
831 uint32_t nFlags = GetFlags(); | |
832 return !((nFlags & ANNOTFLAG_INVISIBLE) || (nFlags & ANNOTFLAG_HIDDEN) || | |
833 (nFlags & ANNOTFLAG_NOVIEW)); | |
834 } | |
835 | |
836 CPDF_Action CPDFSDK_BAAnnot::GetAction() const { | |
837 return CPDF_Action(m_pAnnot->GetAnnotDict()->GetDictBy("A")); | |
838 } | |
839 | |
840 void CPDFSDK_BAAnnot::SetAction(const CPDF_Action& action) { | |
841 ASSERT(action.GetDict()); | |
842 if (action.GetDict() != m_pAnnot->GetAnnotDict()->GetDictBy("A")) { | |
843 CPDF_Document* pDoc = m_pPageView->GetPDFDocument(); | |
844 CPDF_Dictionary* pDict = action.GetDict(); | |
845 if (pDict && pDict->GetObjNum() == 0) { | |
846 pDoc->AddIndirectObject(pDict); | |
847 } | |
848 m_pAnnot->GetAnnotDict()->SetAtReference("A", pDoc, pDict->GetObjNum()); | |
849 } | |
850 } | |
851 | |
852 void CPDFSDK_BAAnnot::RemoveAction() { | |
853 m_pAnnot->GetAnnotDict()->RemoveAt("A"); | |
854 } | |
855 | |
856 CPDF_AAction CPDFSDK_BAAnnot::GetAAction() const { | |
857 return CPDF_AAction(m_pAnnot->GetAnnotDict()->GetDictBy("AA")); | |
858 } | |
859 | |
860 void CPDFSDK_BAAnnot::SetAAction(const CPDF_AAction& aa) { | |
861 if (aa.GetDict() != m_pAnnot->GetAnnotDict()->GetDictBy("AA")) | |
862 m_pAnnot->GetAnnotDict()->SetAt("AA", aa.GetDict()); | |
863 } | |
864 | |
865 void CPDFSDK_BAAnnot::RemoveAAction() { | |
866 m_pAnnot->GetAnnotDict()->RemoveAt("AA"); | |
867 } | |
868 | |
869 CPDF_Action CPDFSDK_BAAnnot::GetAAction(CPDF_AAction::AActionType eAAT) { | |
870 CPDF_AAction AAction = GetAAction(); | |
871 | |
872 if (AAction.ActionExist(eAAT)) | |
873 return AAction.GetAction(eAAT); | |
874 | |
875 if (eAAT == CPDF_AAction::ButtonUp) | |
876 return GetAction(); | |
877 | |
878 return CPDF_Action(); | |
879 } | |
880 | |
881 void CPDFSDK_BAAnnot::Annot_OnDraw(CFX_RenderDevice* pDevice, | |
882 CFX_Matrix* pUser2Device, | |
883 CPDF_RenderOptions* pOptions) { | |
884 m_pAnnot->GetAPForm(m_pPageView->GetPDFPage(), CPDF_Annot::Normal); | |
885 m_pAnnot->DrawAppearance(m_pPageView->GetPDFPage(), pDevice, pUser2Device, | |
886 CPDF_Annot::Normal, nullptr); | |
887 } | |
888 | |
889 UnderlyingPageType* CPDFSDK_Annot::GetUnderlyingPage() { | |
890 #ifdef PDF_ENABLE_XFA | |
891 return GetPDFXFAPage(); | |
892 #else // PDF_ENABLE_XFA | |
893 return GetPDFPage(); | |
894 #endif // PDF_ENABLE_XFA | |
895 } | |
896 | |
897 CPDF_Page* CPDFSDK_Annot::GetPDFPage() { | |
898 return m_pPageView ? m_pPageView->GetPDFPage() : nullptr; | |
899 } | |
900 | |
901 #ifdef PDF_ENABLE_XFA | |
902 | |
903 CPDFXFA_Page* CPDFSDK_Annot::GetPDFXFAPage() { | |
904 return m_pPageView ? m_pPageView->GetPDFXFAPage() : nullptr; | |
905 } | |
906 | |
907 #endif // PDF_ENABLE_XFA | |
OLD | NEW |