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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../include/fsdk_define.h"
8 #include "../include/fsdk_mgr.h"
9 #include "../include/fsdk_baseannot.h"
12 //---------------------------------------------------------------------------
14 //---------------------------------------------------------------------------
15 int _gAfxGetTimeZoneInSeconds(FX_CHAR tzhour, FX_BYTE tzminute)
17 return (int)tzhour * 3600 + (int)tzminute * (tzhour >= 0 ? 60 : -60);
20 FX_BOOL _gAfxIsLeapYear(FX_SHORT year)
22 return ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)));
25 FX_WORD _gAfxGetYearDays(FX_SHORT year)
27 return (_gAfxIsLeapYear(year) == TRUE ? 366 : 365);
30 FX_BYTE _gAfxGetMonthDays(FX_SHORT year, FX_BYTE month)
53 if (_gAfxIsLeapYear(year) == TRUE)
67 CPDFSDK_DateTime::CPDFSDK_DateTime()
72 CPDFSDK_DateTime::CPDFSDK_DateTime(const CFX_ByteString& dtStr)
76 FromPDFDateTimeString(dtStr);
79 CPDFSDK_DateTime::CPDFSDK_DateTime(const CPDFSDK_DateTime& datetime)
81 operator = (datetime);
84 CPDFSDK_DateTime::CPDFSDK_DateTime(const FX_SYSTEMTIME& st)
90 void CPDFSDK_DateTime::ResetDateTime()
97 //newtime = gmtime(&curTime);
98 newtime = localtime(&curTime);
100 dt.year = newtime->tm_year + 1900;
101 dt.month = newtime->tm_mon + 1;
102 dt.day = newtime->tm_mday;
103 dt.hour = newtime->tm_hour;
104 dt.minute = newtime->tm_min;
105 dt.second = newtime->tm_sec;
106 // dt.tzHour = _timezone / 3600 * -1;
107 // dt.tzMinute = (abs(_timezone) % 3600) / 60;
110 CPDFSDK_DateTime& CPDFSDK_DateTime::operator = (const CPDFSDK_DateTime& datetime)
112 FXSYS_memcpy(&dt, &datetime.dt, sizeof(FX_DATETIME));
116 CPDFSDK_DateTime& CPDFSDK_DateTime::operator = (const FX_SYSTEMTIME& st)
120 dt.year = (FX_SHORT)st.wYear;
121 dt.month = (FX_BYTE)st.wMonth;
122 dt.day = (FX_BYTE)st.wDay;
123 dt.hour = (FX_BYTE)st.wHour;
124 dt.minute = (FX_BYTE)st.wMinute;
125 dt.second = (FX_BYTE)st.wSecond;
126 // dt.tzHour = _timezone / 3600 * -1;
127 // dt.tzMinute = (abs(_timezone) % 3600) / 60;
131 FX_BOOL CPDFSDK_DateTime::operator == (CPDFSDK_DateTime& datetime)
133 return (FXSYS_memcmp(&dt, &datetime.dt, sizeof(FX_DATETIME)) == 0);
136 FX_BOOL CPDFSDK_DateTime::operator != (CPDFSDK_DateTime& datetime)
138 return (FXSYS_memcmp(&dt, &datetime.dt, sizeof(FX_DATETIME)) != 0);
141 FX_BOOL CPDFSDK_DateTime::operator > (CPDFSDK_DateTime& datetime)
143 CPDFSDK_DateTime dt1 = ToGMT();
144 CPDFSDK_DateTime dt2 = datetime.ToGMT();
145 int d1 = (((int)dt1.dt.year) << 16) | (((int)dt1.dt.month) << 8) | (int)dt1.dt.day;
146 int d2 = (((int)dt1.dt.hour) << 16) | (((int)dt1.dt.minute) << 8) | (int)dt1.dt.second;
147 int d3 = (((int)dt2.dt.year) << 16) | (((int)dt2.dt.month) << 8) | (int)dt2.dt.day;
148 int d4 = (((int)dt2.dt.hour) << 16) | (((int)dt2.dt.minute) << 8) | (int)dt2.dt.second;
150 if (d1 > d3) return TRUE;
151 if (d2 > d4) return TRUE;
155 FX_BOOL CPDFSDK_DateTime::operator >= (CPDFSDK_DateTime& datetime)
157 CPDFSDK_DateTime dt1 = ToGMT();
158 CPDFSDK_DateTime dt2 = datetime.ToGMT();
159 int d1 = (((int)dt1.dt.year) << 16) | (((int)dt1.dt.month) << 8) | (int)dt1.dt.day;
160 int d2 = (((int)dt1.dt.hour) << 16) | (((int)dt1.dt.minute) << 8) | (int)dt1.dt.second;
161 int d3 = (((int)dt2.dt.year) << 16) | (((int)dt2.dt.month) << 8) | (int)dt2.dt.day;
162 int d4 = (((int)dt2.dt.hour) << 16) | (((int)dt2.dt.minute) << 8) | (int)dt2.dt.second;
164 if (d1 >= d3) return TRUE;
165 if (d2 >= d4) return TRUE;
169 FX_BOOL CPDFSDK_DateTime::operator < (CPDFSDK_DateTime& datetime)
171 CPDFSDK_DateTime dt1 = ToGMT();
172 CPDFSDK_DateTime dt2 = datetime.ToGMT();
173 int d1 = (((int)dt1.dt.year) << 16) | (((int)dt1.dt.month) << 8) | (int)dt1.dt.day;
174 int d2 = (((int)dt1.dt.hour) << 16) | (((int)dt1.dt.minute) << 8) | (int)dt1.dt.second;
175 int d3 = (((int)dt2.dt.year) << 16) | (((int)dt2.dt.month) << 8) | (int)dt2.dt.day;
176 int d4 = (((int)dt2.dt.hour) << 16) | (((int)dt2.dt.minute) << 8) | (int)dt2.dt.second;
178 if (d1 < d3) return TRUE;
179 if (d2 < d4) return TRUE;
183 FX_BOOL CPDFSDK_DateTime::operator <= (CPDFSDK_DateTime& datetime)
185 CPDFSDK_DateTime dt1 = ToGMT();
186 CPDFSDK_DateTime dt2 = datetime.ToGMT();
187 int d1 = (((int)dt1.dt.year) << 16) | (((int)dt1.dt.month) << 8) | (int)dt1.dt.day;
188 int d2 = (((int)dt1.dt.hour) << 16) | (((int)dt1.dt.minute) << 8) | (int)dt1.dt.second;
189 int d3 = (((int)dt2.dt.year) << 16) | (((int)dt2.dt.month) << 8) | (int)dt2.dt.day;
190 int d4 = (((int)dt2.dt.hour) << 16) | (((int)dt2.dt.minute) << 8) | (int)dt2.dt.second;
192 if (d1 <= d3) return TRUE;
193 if (d2 <= d4) return TRUE;
197 CPDFSDK_DateTime::operator time_t()
201 newtime.tm_year = dt.year - 1900;
202 newtime.tm_mon = dt.month - 1;
203 newtime.tm_mday = dt.day;
204 newtime.tm_hour = dt.hour;
205 newtime.tm_min = dt.minute;
206 newtime.tm_sec = dt.second;
208 return mktime(&newtime);
211 CPDFSDK_DateTime& CPDFSDK_DateTime::FromPDFDateTimeString(const CFX_ByteString& dtStr)
213 int strLength = dtStr.GetLength();
219 while (i < strLength)
222 if (ch >= '0' && ch <= '9') break;
225 if (i >= strLength) return *this;
229 while (i < strLength && j < 4)
232 k = k * 10 + ch - '0';
234 if (ch < '0' || ch > '9') break;
237 dt.year = (FX_SHORT)k;
238 if (i >= strLength || j < 4) return *this;
242 while (i < strLength && j < 2)
245 k = k * 10 + ch - '0';
247 if (ch < '0' || ch > '9') break;
250 dt.month = (FX_BYTE)k;
251 if (i >= strLength || j < 2) return *this;
255 while (i < strLength && j < 2)
258 k = k * 10 + ch - '0';
260 if (ch < '0' || ch > '9') break;
264 if (i >= strLength || j < 2) return *this;
268 while (i < strLength && j < 2)
271 k = k * 10 + ch - '0';
273 if (ch < '0' || ch > '9') break;
276 dt.hour = (FX_BYTE)k;
277 if (i >= strLength || j < 2) return *this;
281 while (i < strLength && j < 2)
284 k = k * 10 + ch - '0';
286 if (ch < '0' || ch > '9') break;
289 dt.minute = (FX_BYTE)k;
290 if (i >= strLength || j < 2) return *this;
294 while (i < strLength && j < 2)
297 k = k * 10 + ch - '0';
299 if (ch < '0' || ch > '9') break;
302 dt.second = (FX_BYTE)k;
303 if (i >= strLength || j < 2) return *this;
306 if (ch != '-' && ch != '+') return *this;
313 while (i < strLength && j < 2)
316 k = k * 10 + ch - '0';
318 if (ch < '0' || ch > '9') break;
321 dt.tzHour *= (FX_CHAR)k;
322 if (i >= strLength || j < 2) return *this;
325 if (ch != '\'') return *this;
328 while (i < strLength && j < 2)
331 k = k * 10 + ch - '0';
333 if (ch < '0' || ch > '9') break;
336 dt.tzMinute = (FX_BYTE)k;
337 if (i >= strLength || j < 2) return *this;
343 CFX_ByteString CPDFSDK_DateTime::ToCommonDateTimeString()
346 str1.Format("%04d-%02d-%02d %02d:%02d:%02d ", dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second);
352 str2.Format("%02d:%02d", abs(dt.tzHour), dt.tzMinute);
356 CFX_ByteString CPDFSDK_DateTime::ToPDFDateTimeString()
358 CFX_ByteString dtStr;
360 memset(tempStr, 0, sizeof(tempStr));
361 FXSYS_snprintf(tempStr, sizeof(tempStr) - 1, "D:%04d%02d%02d%02d%02d%02d",
362 dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second);
363 dtStr = CFX_ByteString(tempStr);
365 dtStr += CFX_ByteString("-");
367 dtStr += CFX_ByteString("+");
368 memset(tempStr, 0, sizeof(tempStr));
369 FXSYS_snprintf(tempStr, sizeof(tempStr) - 1, "%02d'%02d'", abs(dt.tzHour), dt.tzMinute);
370 dtStr += CFX_ByteString(tempStr);
374 void CPDFSDK_DateTime::ToSystemTime(FX_SYSTEMTIME& st)
376 CPDFSDK_DateTime dt = *this;
377 time_t t = (time_t)dt;
378 struct tm* pTime = localtime(&t);
380 st.wYear = (FX_WORD)pTime->tm_year + 1900;
381 st.wMonth = (FX_WORD)pTime->tm_mon + 1;
382 st.wDay = (FX_WORD)pTime->tm_mday;
383 st.wDayOfWeek = (FX_WORD)pTime->tm_wday;
384 st.wHour = (FX_WORD)pTime->tm_hour;
385 st.wMinute = (FX_WORD)pTime->tm_min;
386 st.wSecond = (FX_WORD)pTime->tm_sec;
387 st.wMilliseconds = 0;
391 CPDFSDK_DateTime CPDFSDK_DateTime::ToGMT()
393 CPDFSDK_DateTime dt = *this;
394 dt.AddSeconds(-_gAfxGetTimeZoneInSeconds(dt.dt.tzHour, dt.dt.tzMinute));
400 CPDFSDK_DateTime& CPDFSDK_DateTime::AddDays(short days)
402 if (days == 0) return *this;
404 FX_SHORT y = dt.year, yy;
405 FX_BYTE m = dt.month;
407 int mdays, ydays, ldays;
413 if (((FX_WORD)m * 100 + d) > 300) yy ++;
414 ydays = _gAfxGetYearDays(yy);
415 while (ldays >= ydays)
420 mdays = _gAfxGetMonthDays(y, m);
426 ydays = _gAfxGetYearDays(yy);
428 mdays = _gAfxGetMonthDays(y, m) - d + 1;
429 while (ldays >= mdays)
434 mdays = _gAfxGetMonthDays(y, m);
442 if (((FX_WORD)m * 100 + d) < 300) yy --;
443 ydays = _gAfxGetYearDays(yy);
444 while (ldays >= ydays)
449 mdays = _gAfxGetMonthDays(y, m);
455 ydays = _gAfxGetYearDays(yy);
461 mdays = _gAfxGetMonthDays(y, m);
474 CPDFSDK_DateTime& CPDFSDK_DateTime::AddSeconds(int seconds)
476 if (seconds == 0) return *this;
481 n = dt.hour * 3600 + dt.minute * 60 + dt.second + seconds;
484 days = (n - 86399) / 86400;
492 dt.hour = (FX_BYTE)(n / 3600);
495 dt.minute = (FX_BYTE)(n / 60);
496 dt.second = (FX_BYTE)(n % 60);
497 if (days != 0) AddDays(days);
503 //---------------------------------------------------------------------------
505 //---------------------------------------------------------------------------
506 CPDFSDK_Annot::CPDFSDK_Annot(CPDF_Annot* pAnnot, CPDFSDK_PageView* pPageView) :
508 m_pPageView(pPageView),
514 CPDFSDK_Annot::~CPDFSDK_Annot()
520 CPDF_Annot* CPDFSDK_Annot::GetPDFAnnot()
525 FX_DWORD CPDFSDK_Annot::GetFlags()
527 ASSERT(m_pAnnot != NULL);
529 return m_pAnnot->GetFlags();
532 void CPDFSDK_Annot::SetPage(CPDFSDK_PageView* pPageView)
534 m_pPageView = pPageView;
537 CPDFSDK_PageView* CPDFSDK_Annot::GetPageView()
542 FX_BOOL CPDFSDK_Annot::IsSelected()
547 void CPDFSDK_Annot::SetSelected(FX_BOOL bSelected)
549 m_bSelected = bSelected;
553 int CPDFSDK_Annot::GetTabOrder()
558 void CPDFSDK_Annot::SetTabOrder(int iTabOrder)
560 m_nTabOrder = iTabOrder;
563 CPDF_Dictionary* CPDFSDK_Annot::GetAnnotDict() const
565 ASSERT(m_pAnnot != NULL);
567 return m_pAnnot->m_pAnnotDict;
570 void CPDFSDK_Annot::SetRect(const CPDF_Rect& rect)
572 ASSERT(m_pAnnot != NULL);
573 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
574 ASSERT(rect.right - rect.left >= GetMinWidth());
575 ASSERT(rect.top - rect.bottom >= GetMinHeight());
577 m_pAnnot->m_pAnnotDict->SetAtRect("Rect", rect);
580 CPDF_Rect CPDFSDK_Annot::GetRect() const
582 ASSERT(m_pAnnot != NULL);
585 m_pAnnot->GetRect(rect);
590 CFX_ByteString CPDFSDK_Annot::GetType() const
592 ASSERT(m_pAnnot != NULL);
594 return m_pAnnot->GetSubType();
597 CFX_ByteString CPDFSDK_Annot::GetSubType() const
602 void CPDFSDK_Annot::DrawAppearance(CFX_RenderDevice* pDevice, const CPDF_Matrix* pUser2Device,
603 CPDF_Annot::AppearanceMode mode, const CPDF_RenderOptions* pOptions)
605 ASSERT(m_pPageView != NULL);
606 ASSERT(m_pAnnot != NULL);
608 m_pAnnot->DrawAppearance(m_pPageView->GetPDFPage(), pDevice, pUser2Device, mode, pOptions);
611 FX_BOOL CPDFSDK_Annot::IsAppearanceValid()
613 ASSERT(m_pAnnot != NULL);
614 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
616 return m_pAnnot->m_pAnnotDict->GetDict("AP") != NULL;
619 FX_BOOL CPDFSDK_Annot::IsAppearanceValid(CPDF_Annot::AppearanceMode mode)
621 ASSERT(m_pAnnot != NULL);
622 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
624 CPDF_Dictionary* pAP = m_pAnnot->m_pAnnotDict->GetDict("AP");
625 if (pAP == NULL) return FALSE;
627 // Choose the right sub-ap
628 const FX_CHAR* ap_entry = "N";
629 if (mode == CPDF_Annot::Down)
631 else if (mode == CPDF_Annot::Rollover)
633 if (!pAP->KeyExist(ap_entry))
636 // Get the AP stream or subdirectory
637 CPDF_Object* psub = pAP->GetElementValue(ap_entry);
638 if (psub == NULL) return FALSE;
643 void CPDFSDK_Annot::DrawBorder(CFX_RenderDevice* pDevice, const CPDF_Matrix* pUser2Device,
644 const CPDF_RenderOptions* pOptions)
646 ASSERT(m_pAnnot != NULL);
647 m_pAnnot->DrawBorder(pDevice, pUser2Device, pOptions);
650 void CPDFSDK_Annot::ClearCachedAP()
652 ASSERT(m_pAnnot != NULL);
653 m_pAnnot->ClearCachedAP();
656 void CPDFSDK_Annot::SetContents(const CFX_WideString& sContents)
658 ASSERT(m_pAnnot != NULL);
659 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
661 if (sContents.IsEmpty())
662 m_pAnnot->m_pAnnotDict->RemoveAt("Contents");
664 m_pAnnot->m_pAnnotDict->SetAtString("Contents", PDF_EncodeText(sContents));
667 CFX_WideString CPDFSDK_Annot::GetContents() const
669 ASSERT(m_pAnnot != NULL);
670 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
672 return m_pAnnot->m_pAnnotDict->GetUnicodeText("Contents");
675 void CPDFSDK_Annot::SetAnnotName(const CFX_WideString& sName)
677 ASSERT(m_pAnnot != NULL);
678 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
681 m_pAnnot->m_pAnnotDict->RemoveAt("NM");
683 m_pAnnot->m_pAnnotDict->SetAtString("NM", PDF_EncodeText(sName));
686 CFX_WideString CPDFSDK_Annot::GetAnnotName() const
688 ASSERT(m_pAnnot != NULL);
689 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
691 return m_pAnnot->m_pAnnotDict->GetUnicodeText("NM");
694 void CPDFSDK_Annot::SetModifiedDate(const FX_SYSTEMTIME& st)
696 ASSERT(m_pAnnot != NULL);
697 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
699 CPDFSDK_DateTime dt(st);
700 CFX_ByteString str = dt.ToPDFDateTimeString();
703 m_pAnnot->m_pAnnotDict->RemoveAt("M");
705 m_pAnnot->m_pAnnotDict->SetAtString("M", str);
708 FX_SYSTEMTIME CPDFSDK_Annot::GetModifiedDate() const
710 ASSERT(m_pAnnot != NULL);
711 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
713 FX_SYSTEMTIME systime;
714 CFX_ByteString str = m_pAnnot->m_pAnnotDict->GetString("M");
716 CPDFSDK_DateTime dt(str);
717 dt.ToSystemTime(systime);
722 void CPDFSDK_Annot::SetFlags(int nFlags)
724 ASSERT(m_pAnnot != NULL);
725 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
727 m_pAnnot->m_pAnnotDict->SetAtInteger("F", nFlags);
730 int CPDFSDK_Annot::GetFlags() const
732 ASSERT(m_pAnnot != NULL);
733 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
735 return m_pAnnot->m_pAnnotDict->GetInteger("F");
738 void CPDFSDK_Annot::SetAppState(const CFX_ByteString& str)
740 ASSERT(m_pAnnot != NULL);
741 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
744 m_pAnnot->m_pAnnotDict->RemoveAt("AS");
746 m_pAnnot->m_pAnnotDict->SetAtString("AS", str);
749 CFX_ByteString CPDFSDK_Annot::GetAppState() const
751 ASSERT(m_pAnnot != NULL);
752 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
754 return m_pAnnot->m_pAnnotDict->GetString("AS");
757 void CPDFSDK_Annot::SetStructParent(int key)
759 ASSERT(m_pAnnot != NULL);
760 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
762 m_pAnnot->m_pAnnotDict->SetAtInteger("StructParent", key);
765 int CPDFSDK_Annot::GetStructParent() const
767 ASSERT(m_pAnnot != NULL);
768 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
770 return m_pAnnot->m_pAnnotDict->GetInteger("StructParent");
774 void CPDFSDK_Annot::SetBorderWidth(int nWidth)
776 ASSERT(m_pAnnot != NULL);
777 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
779 CPDF_Array* pBorder = m_pAnnot->m_pAnnotDict->GetArray("Border");
783 pBorder->SetAt(2, FX_NEW CPDF_Number(nWidth));
787 CPDF_Dictionary* pBSDict = m_pAnnot->m_pAnnotDict->GetDict("BS");
791 pBSDict = FX_NEW CPDF_Dictionary;
792 m_pAnnot->m_pAnnotDict->SetAt("BS", pBSDict);
795 pBSDict->SetAtInteger("W", nWidth);
799 int CPDFSDK_Annot::GetBorderWidth() const
801 ASSERT(m_pAnnot != NULL);
802 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
804 CPDF_Array* pBorder = m_pAnnot->m_pAnnotDict->GetArray("Border");
808 return pBorder->GetInteger(2);
812 CPDF_Dictionary* pBSDict = m_pAnnot->m_pAnnotDict->GetDict("BS");
816 return pBSDict->GetInteger("W", 1);
822 void CPDFSDK_Annot::SetBorderStyle(int nStyle)
824 ASSERT(m_pAnnot != NULL);
825 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
827 CPDF_Dictionary* pBSDict = m_pAnnot->m_pAnnotDict->GetDict("BS");
830 pBSDict = FX_NEW CPDF_Dictionary;
831 m_pAnnot->m_pAnnotDict->SetAt("BS", pBSDict);
837 pBSDict->SetAtName("S", "S");
840 pBSDict->SetAtName("S", "D");
843 pBSDict->SetAtName("S", "B");
846 pBSDict->SetAtName("S", "I");
849 pBSDict->SetAtName("S", "U");
854 int CPDFSDK_Annot::GetBorderStyle() const
856 ASSERT(m_pAnnot != NULL);
857 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
859 CPDF_Dictionary* pBSDict = m_pAnnot->m_pAnnotDict->GetDict("BS");
862 CFX_ByteString sBorderStyle = pBSDict->GetString("S", "S");
863 if (sBorderStyle == "S") return BBS_SOLID;
864 if (sBorderStyle == "D") return BBS_DASH;
865 if (sBorderStyle == "B") return BBS_BEVELED;
866 if (sBorderStyle == "I") return BBS_INSET;
867 if (sBorderStyle == "U") return BBS_UNDERLINE;
870 CPDF_Array* pBorder = m_pAnnot->m_pAnnotDict->GetArray("Border");
873 if (pBorder->GetCount() >= 4)
875 CPDF_Array *pDP = pBorder->GetArray(3);
876 if (pDP && pDP->GetCount() > 0)
884 void CPDFSDK_Annot::SetBorderDash(const CFX_IntArray& array)
886 ASSERT(m_pAnnot != NULL);
887 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
889 CPDF_Dictionary* pBSDict = m_pAnnot->m_pAnnotDict->GetDict("BS");
892 pBSDict = FX_NEW CPDF_Dictionary;
893 m_pAnnot->m_pAnnotDict->SetAt("BS", pBSDict);
896 CPDF_Array* pArray = FX_NEW CPDF_Array;
897 for (int i=0,sz=array.GetSize(); i<sz; i++)
899 pArray->AddInteger(array[i]);
902 pBSDict->SetAt("D", pArray);
905 void CPDFSDK_Annot::GetBorderDash(CFX_IntArray& array) const
907 ASSERT(m_pAnnot != NULL);
908 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
910 CPDF_Array* pDash = NULL;
912 CPDF_Array* pBorder = m_pAnnot->m_pAnnotDict->GetArray("Border");
915 pDash = pBorder->GetArray(3);
919 CPDF_Dictionary* pBSDict = m_pAnnot->m_pAnnotDict->GetDict("BS");
922 pDash = pBSDict->GetArray("D");
928 for (int i=0,sz=pDash->GetCount(); i<sz; i++)
930 array.Add(pDash->GetInteger(i));
935 void CPDFSDK_Annot::SetColor(FX_COLORREF color)
937 ASSERT(m_pAnnot != NULL);
938 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
940 CPDF_Array* pArray = FX_NEW CPDF_Array;
941 pArray->AddNumber((FX_FLOAT)FXSYS_GetRValue(color) / 255.0f);
942 pArray->AddNumber((FX_FLOAT)FXSYS_GetGValue(color) / 255.0f);
943 pArray->AddNumber((FX_FLOAT)FXSYS_GetBValue(color) / 255.0f);
944 m_pAnnot->m_pAnnotDict->SetAt("C", pArray);
947 void CPDFSDK_Annot::RemoveColor()
949 ASSERT(m_pAnnot != NULL);
950 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
952 m_pAnnot->m_pAnnotDict->RemoveAt("C") ;
955 FX_BOOL CPDFSDK_Annot::GetColor(FX_COLORREF& color) const
957 ASSERT(m_pAnnot != NULL);
958 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
960 if (CPDF_Array* pEntry = m_pAnnot->m_pAnnotDict->GetArray("C"))
962 int nCount = pEntry->GetCount();
965 FX_FLOAT g = pEntry->GetNumber(0) * 255;
967 color = FXSYS_RGB((int)g, (int)g, (int)g);
971 else if (nCount == 3)
973 FX_FLOAT r = pEntry->GetNumber(0) * 255;
974 FX_FLOAT g = pEntry->GetNumber(1) * 255;
975 FX_FLOAT b = pEntry->GetNumber(2) * 255;
977 color = FXSYS_RGB((int)r, (int)g, (int)b);
981 else if (nCount == 4)
983 FX_FLOAT c = pEntry->GetNumber(0);
984 FX_FLOAT m = pEntry->GetNumber(1);
985 FX_FLOAT y = pEntry->GetNumber(2);
986 FX_FLOAT k = pEntry->GetNumber(3);
988 FX_FLOAT r = 1.0f - FX_MIN(1.0f, c + k);
989 FX_FLOAT g = 1.0f - FX_MIN(1.0f, m + k);
990 FX_FLOAT b = 1.0f - FX_MIN(1.0f, y + k);
992 color = FXSYS_RGB((int)(r * 255), (int)(g * 255), (int)(b * 255));
1002 void CPDFSDK_Annot::WriteAppearance(const CFX_ByteString& sAPType, const CPDF_Rect& rcBBox,
1003 const CPDF_Matrix& matrix, const CFX_ByteString& sContents,
1004 const CFX_ByteString& sAPState)
1006 ASSERT(m_pAnnot != NULL);
1007 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
1009 CPDF_Dictionary* pAPDict = m_pAnnot->m_pAnnotDict->GetDict("AP");
1013 pAPDict = FX_NEW CPDF_Dictionary;
1014 m_pAnnot->m_pAnnotDict->SetAt("AP", pAPDict);
1017 CPDF_Stream* pStream = NULL;
1018 CPDF_Dictionary* pParentDict = NULL;
1020 if (sAPState.IsEmpty())
1022 pParentDict = pAPDict;
1023 pStream = pAPDict->GetStream(sAPType);
1027 CPDF_Dictionary* pAPTypeDict = pAPDict->GetDict(sAPType);
1030 pAPTypeDict = FX_NEW CPDF_Dictionary;
1031 pAPDict->SetAt(sAPType, pAPTypeDict);
1034 pParentDict = pAPTypeDict;
1035 pStream = pAPTypeDict->GetStream(sAPState);
1040 ASSERT(m_pPageView != NULL);
1041 CPDF_Document* pDoc = m_pPageView->GetPDFDocument();
1042 ASSERT(pDoc != NULL);
1044 pStream = FX_NEW CPDF_Stream(NULL, 0, NULL);
1045 FX_INT32 objnum = pDoc->AddIndirectObject(pStream);
1046 //pAPDict->SetAtReference(sAPType, pDoc, objnum);
1047 ASSERT(pParentDict != NULL);
1048 pParentDict->SetAtReference(sAPType, pDoc, objnum);
1051 CPDF_Dictionary * pStreamDict = pStream->GetDict();
1055 pStreamDict = FX_NEW CPDF_Dictionary;
1056 pStreamDict->SetAtName("Type", "XObject");
1057 pStreamDict->SetAtName("Subtype", "Form");
1058 pStreamDict->SetAtInteger("FormType", 1);
1059 pStream->InitStream(NULL,0,pStreamDict);
1064 pStreamDict->SetAtMatrix("Matrix",matrix);
1065 pStreamDict->SetAtRect("BBox", rcBBox);
1068 pStream->SetData((FX_BYTE*)sContents.c_str(), sContents.GetLength(), FALSE, FALSE);
1071 #define BA_ANNOT_MINWIDTH 1
1072 #define BA_ANNOT_MINHEIGHT 1
1074 FX_FLOAT CPDFSDK_Annot::GetMinWidth() const
1076 return BA_ANNOT_MINWIDTH;
1079 FX_FLOAT CPDFSDK_Annot::GetMinHeight() const
1081 return BA_ANNOT_MINHEIGHT;
1084 FX_BOOL CPDFSDK_Annot::CreateFormFiller()
1088 FX_BOOL CPDFSDK_Annot::IsVisible() const
1090 int nFlags = GetFlags();
1091 return !((nFlags & ANNOTFLAG_INVISIBLE) || (nFlags & ANNOTFLAG_HIDDEN) || (nFlags & ANNOTFLAG_NOVIEW));
1094 CPDF_Action CPDFSDK_Annot::GetAction() const
1096 return CPDF_Action(m_pAnnot->m_pAnnotDict->GetDict("A"));
1099 void CPDFSDK_Annot::SetAction(const CPDF_Action& action)
1102 if ((CPDF_Action&)action != CPDF_Action(m_pAnnot->m_pAnnotDict->GetDict("A")))
1104 CPDF_Document* pDoc = m_pPageView->GetPDFDocument();
1105 CPDF_Dictionary* pDict = action.GetDict();
1106 if (pDict && pDict->GetObjNum() == 0) {
1107 pDoc->AddIndirectObject(pDict);
1109 m_pAnnot->m_pAnnotDict->SetAtReference("A", pDoc, pDict->GetObjNum());
1113 void CPDFSDK_Annot::RemoveAction()
1115 m_pAnnot->m_pAnnotDict->RemoveAt("A");
1118 CPDF_AAction CPDFSDK_Annot::GetAAction() const
1120 return m_pAnnot->m_pAnnotDict->GetDict("AA");
1123 void CPDFSDK_Annot::SetAAction(const CPDF_AAction& aa)
1125 ASSERT(m_pAnnot != NULL);
1126 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
1129 if ((CPDF_AAction&)aa != m_pAnnot->m_pAnnotDict->GetDict("AA"))
1130 m_pAnnot->m_pAnnotDict->SetAt("AA", (CPDF_AAction&)aa);
1133 void CPDFSDK_Annot::RemoveAAction()
1135 ASSERT(m_pAnnot != NULL);
1136 ASSERT(m_pAnnot->m_pAnnotDict != NULL);
1138 m_pAnnot->m_pAnnotDict->RemoveAt("AA");
1141 CPDF_Action CPDFSDK_Annot::GetAAction(CPDF_AAction::AActionType eAAT)
1143 CPDF_AAction AAction = GetAAction();
1145 if (AAction.ActionExist(eAAT))
1146 return AAction.GetAction(eAAT);
1148 if (eAAT == CPDF_AAction::ButtonUp)
1151 return CPDF_Action();
1154 void CPDFSDK_Annot::Annot_OnDraw(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device, CPDF_RenderOptions* pOptions)
1157 m_pAnnot->GetAPForm(m_pPageView->GetPDFPage(), CPDF_Annot::Normal);
1158 m_pAnnot->DrawAppearance(m_pPageView->GetPDFPage(), pDevice, pUser2Device, CPDF_Annot::Normal, NULL);
1163 CPDF_Page* CPDFSDK_Annot::GetPDFPage()
1166 return m_pPageView->GetPDFPage();