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